Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions scripts/devnet-forest-miner/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,9 @@ validates and follows.
```shell
mise run test:devnet:forest-miner
```

The Lotus Miner suites (`forest-dev devnet miner`) run only against this devnet:

```shell
mise run test:devnet:miner
```
3 changes: 2 additions & 1 deletion scripts/devnet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ mise run test:devnet

Under the hood this sources `test_harness.sh`, which reads the admin token and
the funded genesis key from the running `forest` container, exports
`FULLNODE_API_INFO` (Forest RPC on port 3456) and `FOREST_TEST_PRELOADED_ADDRESS`,
`FULLNODE_API_INFO` (Forest RPC on port 3456), `MINER_API_INFO` (Lotus Miner RPC on
port 2345) and `FOREST_TEST_PRELOADED_ADDRESS`,
then runs `forest-dev tests mpool`, `forest-dev tests wallet`,
`forest-dev devnet eth-gas`, `forest-dev devnet eth-skip-sender`,
`forest-dev devnet state-call` and `forest-dev devnet eth-pending-tx`.
Expand Down
7 changes: 6 additions & 1 deletion scripts/devnet/test_harness.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
TEST_HARNESS_PARENT_PATH=$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P )
# Devnet directory whose `.env`/config to load. Defaults to this harness's own directory (the
# lotus-produced devnet); the forest-produced devnet sets `DEVNET_DIR` to reuse this same harness.
DEVNET_DIR="${DEVNET_DIR:-${TEST_HARNESS_PARENT_PATH}}"
export DEVNET_DIR="${DEVNET_DIR:-${TEST_HARNESS_PARENT_PATH}}"
# Export everything in `.env` so the test binaries inherit the devnet config (ports, data dirs)
# as environment variables; `source` on its own would only set shell variables.
set -a
Expand All @@ -22,6 +22,10 @@ function devnet_test_env_init {
local token
token=$(docker exec forest cat "${FOREST_DATA_DIR}/token.jwt")
export FULLNODE_API_INFO="${token}:/ip4/127.0.0.1/tcp/${FOREST_RPC_PORT}/http"
# The repo token carries all permissions (https://github.com/filecoin-project/lotus/blob/v1.38.3/node/modules/core.go#L165-L173).
local miner_token
miner_token=$(docker exec lotus-miner sh -c 'cat "$LOTUS_MINER_PATH/token"')
export MINER_API_INFO="${miner_token}:/ip4/127.0.0.1/tcp/${MINER_RPC_PORT}/http"

local tmp
tmp="$(mktemp --directory)"
Expand All @@ -43,6 +47,7 @@ function devnet_test_env_init {

echo "Devnet test env initialised:"
echo " FULLNODE_API_INFO=<token>:/ip4/127.0.0.1/tcp/${FOREST_RPC_PORT}/http"
echo " MINER_API_INFO=<token>:/ip4/127.0.0.1/tcp/${MINER_RPC_PORT}/http"
echo " FOREST_TEST_PRELOADED_ADDRESS=${FOREST_TEST_PRELOADED_ADDRESS}"
echo " Funding ${test_addr} with ${DEVNET_TEST_FUND_AMT} from ${genesis_addr}..."

Expand Down
3 changes: 2 additions & 1 deletion scripts/devnet/with_devnet.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export DEVNET_DIR
REPO_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)
cd "${REPO_ROOT}"

trap 'docker compose --project-directory "${REPO_ROOT}/${DEVNET_DIR}" -f "${REPO_ROOT}/${DEVNET_DIR}/docker-compose.yml" down --remove-orphans --volumes || true' EXIT
# Volumes stay: `setup.sh` removes the data volumes on the next run and keeps the proof params.
trap 'docker compose --project-directory "${REPO_ROOT}/${DEVNET_DIR}" -f "${REPO_ROOT}/${DEVNET_DIR}/docker-compose.yml" down --remove-orphans || true' EXIT

pushd "${DEVNET_DIR}" >/dev/null
./setup.sh
Expand Down
296 changes: 24 additions & 272 deletions src/dev/subcommands/devnet_cmd/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,18 @@
//! Lotus Miner integration tests, ported from Lotus `itests`. They need
//! `scripts/devnet-forest-miner`, where Lotus Miner seals and mines through Forest's RPC.

mod api;
mod devnet;
mod onboarding;

use crate::dev::subcommands::tests_cmd::helpers::*;
use crate::rpc::prelude::*;
use crate::rpc::types::{ApiTipsetKey, SectorOnChainInfo};
use crate::rpc::{ApiPaths, Client, DEFAULT_REQUEST_TIMEOUT, Request, humanize_rpc_error};
use crate::shim::actors::miner::MinerInfo;
use crate::shim::address::Address;
use anyhow::{Context as _, bail, ensure};
use crate::dev::subcommands::tests_cmd::helpers::harness_env;
use crate::lotus_json::HasLotusJson;
use crate::rpc::{Client, Request, humanize_rpc_error};
use anyhow::{Context as _, ensure};
use clap::ValueEnum as _;
use itertools::Itertools as _;
use libtest_mimic::{Arguments, Trial};
use serde::Deserialize;
use serde::de::{DeserializeOwned, IgnoredAny};
use serde_json::json;
use std::marker::PhantomData;
use std::str::FromStr as _;
use std::fmt::Debug;
use std::path::Path;

/// Lotus Miner integration tests on Forest (`scripts/devnet-forest-miner` only)
#[derive(Debug, clap::Args)]
Expand All @@ -40,6 +35,11 @@ enum Group {

impl MinerTestCommand {
pub async fn run(self) -> anyhow::Result<()> {
let devnet_dir = harness_env("DEVNET_DIR")?;
ensure!(
Path::new(&devnet_dir).ends_with("devnet-forest-miner"),
"`devnet miner` drives lotus-miner over Forest's RPC, which only scripts/devnet-forest-miner does; DEVNET_DIR is `{devnet_dir}`"
);
let groups = match self.group {
Some(group) => vec![group],
None => Group::value_variants().to_vec(),
Expand All @@ -59,264 +59,16 @@ fn tests(group: Group) -> Vec<Trial> {
}
}

fn genesis_miner() -> anyhow::Result<Address> {
let raw = harness_env("MINER_ACTOR_ADDRESS")?;
Address::from_str(&raw).with_context(|| format!("invalid MINER_ACTOR_ADDRESS `{raw}`"))
}

async fn miner_info(client: &Client, miner: Address) -> anyhow::Result<MinerInfo> {
Ok(client
.call(StateMinerInfo::request((miner, ApiTipsetKey(None)))?)
.await?)
}

/// `Ok(true)` if Lotus shares Forest's head tipset and has not marked it bad; `Ok(false)` if Lotus still trails.
async fn lotus_accepts_forest_head(forest: &Client, lotus: &Client) -> anyhow::Result<bool> {
let head = forest.call(ChainHead::request(())?).await?;
let lotus_head = lotus.call(ChainHead::request(())?).await?;
if lotus_head.epoch() < head.epoch() {
return Ok(false);
}
let lotus_at_height = lotus
.call(ChainGetTipSetByHeight::request((
head.epoch(),
ApiTipsetKey(None),
))?)
.await?;
ensure!(
lotus_at_height.key() == head.key(),
"Lotus tipset at Forest head epoch {} differs: forest {}, lotus {}",
head.epoch(),
head.key(),
lotus_at_height.key()
);
for cid in head.cids() {
let reason = lotus.call(SyncCheckBad::request((cid,))?).await?;
ensure!(
reason.is_empty(),
"Lotus rejected Forest block {cid} at epoch {}: {reason}",
head.epoch()
);
}
Ok(true)
}

async fn sector_info(
/// Calls `request` on `node`, naming the node and the method in the error.
async fn call<T: HasLotusJson + Debug>(
client: &Client,
miner: Address,
sector: u64,
) -> anyhow::Result<Option<SectorOnChainInfo>> {
Ok(client
.call(StateSectorGetInfo::request((
miner,
sector,
ApiTipsetKey(None),
))?)
.await?)
}

/// The Lotus Miner API, which Forest's RPC does not define.
struct MinerApi(Client);

impl MinerApi {
/// Connects with an admin token, which flushing sealing batches needs.
fn new() -> anyhow::Result<Self> {
let port = harness_env("MINER_RPC_PORT")?;
let token = lotus_miner_exec(&["auth", "create-token", "--perm", "admin"])?;
let mut url: url::Url = format!("http://127.0.0.1:{port}/").parse()?;
url.set_password(Some(token.as_str()))
.map_err(|()| anyhow::anyhow!("cannot set the Lotus Miner token on {url}"))?;
Ok(Self(Client::from_url(url)))
}

async fn call<T: DeserializeOwned>(
&self,
method: &'static str,
params: serde_json::Value,
) -> anyhow::Result<T> {
let request = Request::<serde_json::Value> {
method_name: method.into(),
params,
result_type: PhantomData,
api_path: ApiPaths::V0,
timeout: *DEFAULT_REQUEST_TIMEOUT,
};
let result = self
.0
.call(request)
.await
.with_context(|| format!("Lotus Miner {method}"))
.map_err(humanize_rpc_error)?;
serde_json::from_value(result)
.with_context(|| format!("decoding the Lotus Miner {method} result"))
}

/// Starts sealing a new CC sector and returns its number.
async fn pledge_sector(&self) -> anyhow::Result<u64> {
let id: SectorId = self.call("Filecoin.PledgeSector", json!([])).await?;
Ok(id.number)
}

async fn sectors_status(&self, sector: u64) -> anyhow::Result<SectorStatus> {
self.call("Filecoin.SectorsStatus", json!([sector, false]))
.await
}

/// Flush pre-commit/commit batches (Lotus may hold them for up to a day).
async fn flush_sealing_batches(&self) -> anyhow::Result<()> {
for method in [
"Filecoin.SectorPreCommitFlush",
"Filecoin.SectorCommitFlush",
] {
self.call::<IgnoredAny>(method, json!([])).await?;
}
Ok(())
}
}

/// Lotus `abi.SectorID`.
#[derive(Deserialize)]
#[serde(rename_all = "PascalCase")]
struct SectorId {
number: u64,
}

/// The fields of Lotus `api.SectorInfo` these tests read.
#[derive(Deserialize)]
#[serde(rename_all = "PascalCase")]
struct SectorStatus {
state: SectorState,
last_err: String,
log: Vec<SectorLog>,
}

/// Lotus `api.SectorState`. Unknown Lotus values fail to decode.
#[derive(Debug, Deserialize)]
enum SectorState {
/// A new sector, before Lotus processes its first event.
#[serde(rename = "")]
Undefined,
Empty,
WaitDeals,
AddPiece,
Packing,
GetTicket,
PreCommit1,
PreCommit2,
PreCommitting,
PreCommitWait,
SubmitPreCommitBatch,
PreCommitBatchWait,
WaitSeed,
Committing,
CommitFinalize,
CommitFinalizeFailed,
SubmitCommit,
CommitWait,
SubmitCommitAggregate,
CommitAggregateWait,
FinalizeSector,
Proving,
Available,

SnapDealsWaitDeals,
SnapDealsAddPiece,
SnapDealsPacking,
UpdateReplica,
ProveReplicaUpdate,
SubmitReplicaUpdate,
WaitMutable,
ReplicaUpdateWait,
FinalizeReplicaUpdate,
UpdateActivating,
ReleaseSectorKey,

ReceiveSector,

FailedUnrecoverable,
AddPieceFailed,
SealPreCommit1Failed,
SealPreCommit2Failed,
PreCommitFailed,
ComputeProofFailed,
RemoteCommitFailed,
CommitFailed,
PackingFailed,
FinalizeFailed,
DealsExpired,
RecoverDealIDs,

SnapDealsAddPieceFailed,
SnapDealsDealsExpired,
SnapDealsRecoverDealIDs,
AbortUpgrade,
ReplicaUpdateFailed,
ReleaseSectorKeyFailed,
FinalizeReplicaUpdateFailed,

Faulty,
FaultReported,
FaultedFinal,

Terminating,
TerminateWait,
TerminateFinality,
TerminateFailed,

Removing,
RemoveFailed,
Removed,
}

/// Lotus `api.SectorLog`.
#[derive(Deserialize)]
#[serde(rename_all = "PascalCase")]
struct SectorLog {
kind: String,
message: String,
}

async fn wait_until_proving(miner_api: &MinerApi, sector: u64) -> anyhow::Result<()> {
let waited = poll(&format!("sector {sector} to reach Proving"), || async {
miner_api.flush_sealing_batches().await?;
let status = miner_api.sectors_status(sector).await?;
eprintln!("sector {sector} state: {:?}", status.state);
match status.state {
SectorState::Proving | SectorState::Available => Ok(Some(())),
// Never reaches Proving from these.
SectorState::FailedUnrecoverable
| SectorState::PackingFailed
| SectorState::DealsExpired
| SectorState::Faulty
| SectorState::FaultReported
| SectorState::FaultedFinal
| SectorState::Terminating
| SectorState::TerminateWait
| SectorState::TerminateFinality
| SectorState::TerminateFailed
| SectorState::Removing
| SectorState::RemoveFailed
| SectorState::Removed => bail!(
"sector {sector} reached {:?}, which never leads to Proving",
status.state
),
_ => Ok(None),
}
})
.await;
let Err(e) = waited else {
return Ok(());
};
let Ok(status) = miner_api.sectors_status(sector).await else {
return Err(e);
};
let log = status
.log
.iter()
.map(|l| format!("{} {}", l.kind, l.message))
.join("\n");
Err(e.context(format!(
"sector {sector} is in {:?}, last error: {:?}\nsector log:\n{log}",
status.state, status.last_err
)))
node: &str,
request: Request<T>,
) -> anyhow::Result<T> {
let method = request.method_name.clone();
client
.call(request)
.await
.with_context(|| format!("{node} {method}"))
.map_err(humanize_rpc_error)
}
Loading
Loading