From 809a17e41aa5e7a41535eb2fe25b37b072cfe02d Mon Sep 17 00:00:00 2001 From: Aryan Tikarya Date: Thu, 29 Oct 2026 15:44:46 +0530 Subject: [PATCH 1/7] refactor(rpc): add Request::new and build reflect requests with it --- src/rpc/reflect/mod.rs | 20 +++++--------------- src/rpc/request.rs | 14 +++++++++++++- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/rpc/reflect/mod.rs b/src/rpc/reflect/mod.rs index e2f38b56df6..f7e2378a18d 100644 --- a/src/rpc/reflect/mod.rs +++ b/src/rpc/reflect/mod.rs @@ -286,14 +286,9 @@ pub trait RpcMethodExt: RpcMethod { fn request(params: Self::Params) -> serde_json::Result> { // hardcode calling convention because lotus is by-position only let params = Self::request_params(params)?; - Ok(crate::rpc::Request { - method_name: Self::NAME.into(), - params, - result_type: std::marker::PhantomData, - api_path: crate::rpc::Request::::max_api_path(Self::API_PATHS) - .map_err(serde_json::Error::custom)?, - timeout: *crate::rpc::DEFAULT_REQUEST_TIMEOUT, - }) + let api_path = crate::rpc::Request::::max_api_path(Self::API_PATHS) + .map_err(serde_json::Error::custom)?; + Ok(crate::rpc::Request::new(Self::NAME, params, api_path)) } fn request_params(params: Self::Params) -> serde_json::Result { @@ -329,13 +324,8 @@ pub trait RpcMethodExt: RpcMethod { Self::NAME }; - Ok(crate::rpc::Request { - method_name: name.into(), - params, - result_type: std::marker::PhantomData, - api_path: crate::rpc::Request::::max_api_path(Self::API_PATHS)?, - timeout: *crate::rpc::DEFAULT_REQUEST_TIMEOUT, - }) + let api_path = crate::rpc::Request::::max_api_path(Self::API_PATHS)?; + Ok(crate::rpc::Request::new(name, params, api_path)) } fn call_raw( client: &crate::rpc::client::Client, diff --git a/src/rpc/request.rs b/src/rpc/request.rs index 1ea25ce4ac9..b0af6b7204f 100644 --- a/src/rpc/request.rs +++ b/src/rpc/request.rs @@ -9,7 +9,8 @@ use serde::{Deserialize, Serialize}; use std::{marker::PhantomData, time::Duration}; /// An at-rest description of a remote procedure call, created using -/// [`rpc::RpcMethodExt`](crate::rpc::RpcMethodExt::request), and called using [`rpc::Client::call`](crate::rpc::Client::call). +/// [`rpc::RpcMethodExt`](crate::rpc::RpcMethodExt::request) for a method Forest defines or +/// [`Request::new`] for any other, and called using [`rpc::Client::call`](crate::rpc::Client::call). #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Request { pub method_name: std::borrow::Cow<'static, str>, @@ -23,6 +24,17 @@ pub struct Request { } impl Request { + /// Creates a request for `method_name` with the default timeout. + pub fn new(method_name: &'static str, params: serde_json::Value, api_path: ApiPaths) -> Self { + Self { + method_name: method_name.into(), + params, + result_type: PhantomData, + api_path, + timeout: *super::DEFAULT_REQUEST_TIMEOUT, + } + } + pub fn set_timeout(&mut self, timeout: Duration) { self.timeout = timeout; } From 68342b190e94c05b80160e9afa030401230174da Mon Sep 17 00:00:00 2001 From: Aryan Tikarya Date: Thu, 29 Oct 2026 15:44:47 +0530 Subject: [PATCH 2/7] chore(devnet): export MINER_API_INFO and DEVNET_DIR from the test harness --- scripts/devnet/README.md | 3 ++- scripts/devnet/test_harness.sh | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/scripts/devnet/README.md b/scripts/devnet/README.md index 010dbc4dd6a..94d328dfc74 100644 --- a/scripts/devnet/README.md +++ b/scripts/devnet/README.md @@ -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`. diff --git a/scripts/devnet/test_harness.sh b/scripts/devnet/test_harness.sh index 62b2f1e6f68..3a0bd893cbd 100644 --- a/scripts/devnet/test_harness.sh +++ b/scripts/devnet/test_harness.sh @@ -8,6 +8,8 @@ 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}}" +# The miner suites read it to refuse any devnet but the forest-produced one. +export DEVNET_DIR # 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 @@ -22,6 +24,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 miner's repo token is its admin token; `LOTUS_MINER_PATH` is set in the container. + 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)" @@ -43,6 +49,7 @@ function devnet_test_env_init { echo "Devnet test env initialised:" echo " FULLNODE_API_INFO=:/ip4/127.0.0.1/tcp/${FOREST_RPC_PORT}/http" + echo " MINER_API_INFO=:/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}..." From f075854e357c2a6239442e20cd28beabf1a25441 Mon Sep 17 00:00:00 2001 From: Aryan Tikarya Date: Thu, 29 Oct 2026 15:46:46 +0530 Subject: [PATCH 3/7] refactor(dev): rebuild the devnet miner suite on a parity core and a typed MinerApi --- src/dev/subcommands/devnet_cmd/miner.rs | 285 +----------------- src/dev/subcommands/devnet_cmd/miner/api.rs | 264 ++++++++++++++++ .../subcommands/devnet_cmd/miner/devnet.rs | 124 ++++++++ .../devnet_cmd/miner/onboarding.rs | 98 ++---- src/dev/subcommands/tests_cmd/helpers.rs | 7 - src/rpc/mod.rs | 2 +- src/tool/subcommands/api_cmd.rs | 1 + 7 files changed, 430 insertions(+), 351 deletions(-) create mode 100644 src/dev/subcommands/devnet_cmd/miner/api.rs create mode 100644 src/dev/subcommands/devnet_cmd/miner/devnet.rs diff --git a/src/dev/subcommands/devnet_cmd/miner.rs b/src/dev/subcommands/devnet_cmd/miner.rs index 156dcbdac49..ab66dcf7648 100644 --- a/src/dev/subcommands/devnet_cmd/miner.rs +++ b/src/dev/subcommands/devnet_cmd/miner.rs @@ -4,23 +4,15 @@ //! 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 anyhow::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::path::Path; /// Lotus Miner integration tests on Forest (`scripts/devnet-forest-miner` only) #[derive(Debug, clap::Args)] @@ -40,6 +32,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(), @@ -58,265 +55,3 @@ fn tests(group: Group) -> Vec { Group::Onboarding => onboarding::tests(), } } - -fn genesis_miner() -> anyhow::Result
{ - 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 { - 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 { - 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( - client: &Client, - miner: Address, - sector: u64, -) -> anyhow::Result> { - 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 { - 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( - &self, - method: &'static str, - params: serde_json::Value, - ) -> anyhow::Result { - let request = Request:: { - 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 { - let id: SectorId = self.call("Filecoin.PledgeSector", json!([])).await?; - Ok(id.number) - } - - async fn sectors_status(&self, sector: u64) -> anyhow::Result { - 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::(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, -} - -/// 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 - ))) -} diff --git a/src/dev/subcommands/devnet_cmd/miner/api.rs b/src/dev/subcommands/devnet_cmd/miner/api.rs new file mode 100644 index 00000000000..c61c9706ffa --- /dev/null +++ b/src/dev/subcommands/devnet_cmd/miner/api.rs @@ -0,0 +1,264 @@ +// Copyright 2019-2026 ChainSafe Systems +// SPDX-License-Identifier: Apache-2.0, MIT + +//! The miner the trials drive: a `lotus-miner` whose full node is Forest, reached through the +//! Lotus Miner JSON-RPC API, which Forest's RPC does not define. + +use crate::dev::subcommands::tests_cmd::helpers::{harness_env, poll}; +use crate::rpc::{ApiPaths, Client, Request, humanize_rpc_error}; +use crate::shim::address::Address; +use crate::utils::UrlFromMultiAddr; +use anyhow::{Context as _, bail}; +use itertools::Itertools as _; +use serde::Deserialize; +use serde::de::{DeserializeOwned, IgnoredAny}; +use serde_json::json; +use std::str::FromStr as _; + +/// A `lotus-miner` whose full node is Forest: its actor on chain and its API. +pub(super) struct TestMiner { + pub actor: Address, + pub api: MinerApi, +} + +impl TestMiner { + /// Returns the genesis miner the compose devnet runs, from `MINER_ACTOR_ADDRESS` and `MINER_API_INFO`. + pub fn genesis() -> anyhow::Result { + let raw = harness_env("MINER_ACTOR_ADDRESS")?; + let actor = Address::from_str(&raw) + .with_context(|| format!("invalid MINER_ACTOR_ADDRESS `{raw}`"))?; + Ok(Self { + actor, + api: MinerApi::connect()?, + }) + } +} + +/// The Lotus Miner API (`/rpc/v0`) with the admin token that flushing the sealing batches needs. +pub(super) struct MinerApi(Client); + +impl MinerApi { + /// Connects to `MINER_API_INFO` (`:`, the shape of Lotus's own variable). + pub fn connect() -> anyhow::Result { + let raw = harness_env("MINER_API_INFO")?; + let UrlFromMultiAddr(url) = raw.parse().context("parsing MINER_API_INFO")?; + Ok(Self(Client::from_url(url))) + } + + async fn call( + &self, + method: &'static str, + params: serde_json::Value, + ) -> anyhow::Result { + let result: serde_json::Value = self + .0 + .call(Request::new(method, params, ApiPaths::V0)) + .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. + pub async fn pledge_sector(&self) -> anyhow::Result { + let id: SectorId = self.call("Filecoin.PledgeSector", json!([])).await?; + Ok(id.number) + } + + async fn sectors_status(&self, sector: u64) -> anyhow::Result { + self.call("Filecoin.SectorsStatus", json!([sector, false])) + .await + } + + /// Flushes the pre-commit and commit batches, which Lotus may otherwise hold for up to a day. + async fn flush_sealing_batches(&self) -> anyhow::Result<()> { + for method in [ + "Filecoin.SectorPreCommitFlush", + "Filecoin.SectorCommitFlush", + ] { + self.call::(method, json!([])).await?; + } + Ok(()) + } + + /// Waits until `sector` is `Proving`, flushing the sealing batches on every poll. + /// + /// # Errors + /// The sector enters a state that never leads to `Proving`, or the poll budget passes; both + /// errors carry the sector log, whose trace lines hold Lotus's error text. + pub async fn wait_until_proving(&self, sector: u64) -> anyhow::Result<()> { + let waited = poll(&format!("sector {sector} to reach Proving"), || async { + self.flush_sealing_batches().await?; + let status = self.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) = self.sectors_status(sector).await else { + return Err(e); + }; + let log = status + .log + .iter() + .map(|l| { + if l.trace.is_empty() { + format!("{} {}", l.kind, l.message) + } else { + format!("{} {}\n{}", l.kind, l.message, l.trace) + } + }) + .join("\n"); + Err(e.context(format!( + "sector {sector} is in {:?}\nsector log:\n{log}", + status.state + ))) + } +} + +/// 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, + log: Vec, +} + +/// Lotus `api.SectorLog` (): +/// `message` is the marshalled event, `trace` the error text when the event carried one. +#[derive(Deserialize)] +#[serde(rename_all = "PascalCase")] +struct SectorLog { + kind: String, + message: String, + trace: String, +} + +/// Lotus `api.SectorState` +/// (). +/// Unknown 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, +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn sector_status_decodes_the_lotus_shape() { + let status: SectorStatus = serde_json::from_value(json!({ + "State": "", + "LastErr": "", + "Log": [{"Kind": "event;sealing.SectorCommitFailed", "Timestamp": 1, "Trace": "boom", "Message": "{\"User\":{}}"}], + })) + .unwrap(); + assert!(matches!(status.state, SectorState::Undefined)); + assert_eq!(status.log[0].trace, "boom"); + } +} diff --git a/src/dev/subcommands/devnet_cmd/miner/devnet.rs b/src/dev/subcommands/devnet_cmd/miner/devnet.rs new file mode 100644 index 00000000000..a726a920edf --- /dev/null +++ b/src/dev/subcommands/devnet_cmd/miner/devnet.rs @@ -0,0 +1,124 @@ +// Copyright 2019-2026 ChainSafe Systems +// SPDX-License-Identifier: Apache-2.0, MIT + +//! The two full nodes of the devnet and the pinned comparison the trials assert with: Forest +//! answers, and Lotus, which validates the chain Forest produces, must answer the same. + +use crate::blocks::Tipset; +use crate::dev::subcommands::tests_cmd::helpers::{forest_client, lotus_client, poll}; +use crate::lotus_json::HasLotusJson; +use crate::rpc::prelude::*; +use crate::rpc::types::ApiTipsetKey; +use crate::rpc::{Client, Request, humanize_rpc_error}; +use crate::tool::subcommands::api_cmd::generate_diff; +use anyhow::{Context as _, bail, ensure}; +use std::fmt::Debug; + +/// Forest under test and the Lotus node that validates the chain Forest produces. +pub(super) struct Devnet { + pub forest: Client, + pub lotus: Client, +} + +impl Devnet { + pub fn connect() -> anyhow::Result { + Ok(Self { + forest: forest_client()?, + lotus: lotus_client()?, + }) + } + + /// Waits until Lotus has Forest's current head and returns that head as the tipset to compare at. + /// + /// # Errors + /// Lotus holds a different tipset at Forest's head epoch, or has marked one of its blocks bad; + /// both fail at once instead of retrying, since they mean Lotus rejected what Forest produced. + /// Lotus not reaching that epoch within the poll budget also fails. + pub async fn synced(&self) -> anyhow::Result> { + let head = call(&self.forest, "forest", ChainHead::request(())?).await?; + poll( + &format!("lotus to reach epoch {}", head.epoch()), + || async { + let lotus_head = call(&self.lotus, "lotus", ChainHead::request(())?).await?; + Ok((lotus_head.epoch() >= head.epoch()).then_some(())) + }, + ) + .await?; + let lotus_at_height = call( + &self.lotus, + "lotus", + 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 = call(&self.lotus, "lotus", SyncCheckBad::request((cid,))?).await?; + ensure!( + reason.is_empty(), + "Lotus rejected Forest block {cid} at epoch {}: {reason}", + head.epoch() + ); + } + Ok(At { devnet: self, head }) + } +} + +/// A tipset both nodes have; every comparison through it is pinned to that tipset. +pub(super) struct At<'a> { + devnet: &'a Devnet, + head: Tipset, +} + +impl At<'_> { + /// Calls the request, built with this tipset's key, on both nodes and returns the answer they agree on. + /// + /// # Errors + /// Either node fails the call, or the answers differ; the message then carries a line diff of the two. + pub async fn same( + &self, + request: impl Fn(ApiTipsetKey) -> serde_json::Result>, + ) -> anyhow::Result + where + T: HasLotusJson + PartialEq + Debug, + { + let key = ApiTipsetKey(Some(self.head.key().clone())); + let forest_request = request(key.clone())?; + let lotus_request = request(key)?; + let method = forest_request.method_name.clone(); + let (forest, lotus) = tokio::try_join!( + call(&self.devnet.forest, "forest", forest_request), + call(&self.devnet.lotus, "lotus", lotus_request), + )?; + if forest == lotus { + return Ok(forest); + } + let diff = generate_diff( + &forest.into_lotus_json_value()?, + &lotus.into_lotus_json_value()?, + ); + bail!( + "{method} at epoch {} differs (-forest +lotus):\n{diff}", + self.head.epoch() + ) + } +} + +/// Calls `request` on `node`, naming the node and the method in the error. +async fn call( + client: &Client, + node: &str, + request: Request, +) -> anyhow::Result { + let method = request.method_name.clone(); + client + .call(request) + .await + .with_context(|| format!("{node} {method}")) + .map_err(humanize_rpc_error) +} diff --git a/src/dev/subcommands/devnet_cmd/miner/onboarding.rs b/src/dev/subcommands/devnet_cmd/miner/onboarding.rs index 4ba562fec8b..9cfecfd20fb 100644 --- a/src/dev/subcommands/devnet_cmd/miner/onboarding.rs +++ b/src/dev/subcommands/devnet_cmd/miner/onboarding.rs @@ -1,12 +1,12 @@ // Copyright 2019-2026 ChainSafe Systems // SPDX-License-Identifier: Apache-2.0, MIT -//! Lotus Miner creating a miner and pledging a sector through Forest's RPC. Mirrors Lotus -//! `testNonGenesisMiner` and `TestPledgeSectors` (single sector, real 2 KiB proofs). +//! Lotus Miner creating a miner and pledging a sector through Forest's RPC. Mirrors the creation +//! step of Lotus `testNonGenesisMiner` (which also starts the new miner) and `TestPledgeSectors` +//! (single sector, real 2 KiB proofs). -use super::{ - MinerApi, genesis_miner, lotus_accepts_forest_head, miner_info, sector_info, wait_until_proving, -}; +use super::api::TestMiner; +use super::devnet::Devnet; use crate::dev::subcommands::tests_cmd::helpers::*; use crate::key_management::generate_key; use crate::rpc::Client; @@ -37,37 +37,23 @@ async fn list_miners(client: &Client) -> anyhow::Result> { } async fn create_miner() -> anyhow::Result<()> { - let forest = forest_client()?; - let lotus = lotus_client()?; + let net = Devnet::connect()?; let owner_key = Address::from_str(&FOREST_TEST_PRELOADED_ADDRESS) .context("parsing FOREST_TEST_PRELOADED_ADDRESS")?; - let owner_id = forest + let owner_id = net + .forest .call(StateLookupID::request((owner_key, ApiTipsetKey(None)))?) .await?; // The miner actor requires a BLS worker; the harness wallet is secp256k1. let worker_key = generate_key(SignatureType::Bls)?.address; - // Lotus trails Forest; query both at Lotus's head so the tipset exists on Forest. - let lotus_head = lotus.call(ChainHead::request(())?).await?; - let at_lotus_head = ApiTipsetKey(Some(lotus_head.key().clone())); - let deposit = StateMinerCreationDeposit::request((at_lotus_head.clone(),))?; - let forest_deposit = forest.call(deposit.clone()).await?; - let lotus_deposit = lotus.call(deposit).await?; - ensure!( - forest_deposit == lotus_deposit, - "StateMinerCreationDeposit at epoch {}: forest {forest_deposit}, lotus {lotus_deposit}", - lotus_head.epoch() - ); - let supply = StateVMCirculatingSupplyInternal::request((at_lotus_head,))?; - let forest_supply = forest.call(supply.clone()).await?; - let lotus_supply = lotus.call(supply).await?; - ensure!( - forest_supply == lotus_supply, - "StateVMCirculatingSupplyInternal at epoch {}:\nforest: {forest_supply:?}\nlotus: {lotus_supply:?}", - lotus_head.epoch() - ); + let at = net.synced().await?; + at.same(|tsk| StateMinerCreationDeposit::request((tsk,))) + .await?; + at.same(|tsk| StateVMCirculatingSupplyInternal::request((tsk,))) + .await?; - let before = list_miners(&forest).await?; + let before = list_miners(&net.forest).await?; let repo = format!( "/tmp/forest-onboarding-{}", SystemTime::now().duration_since(UNIX_EPOCH)?.as_millis() @@ -76,7 +62,7 @@ async fn create_miner() -> anyhow::Result<()> { let _ = docker(&["exec", "lotus-miner", "rm", "-rf", &repo]); init?; - let created: Vec
= list_miners(&forest) + let created: Vec
= list_miners(&net.forest) .await? .difference(&before) .copied() @@ -85,10 +71,15 @@ async fn create_miner() -> anyhow::Result<()> { bail!("expected exactly one new miner after `lotus-miner init`, found {created:?}"); }; - let worker_id = forest + let worker_id = net + .forest .call(StateLookupID::request((worker_key, ApiTipsetKey(None)))?) .await?; - let info = miner_info(&forest, *miner).await?; + let info = net + .synced() + .await? + .same(|tsk| StateMinerInfo::request((*miner, tsk))) + .await?; ensure!( info.owner == owner_id && info.worker == worker_id, "miner {miner}: owner {} and worker {}, expected {owner_id} and {worker_id}", @@ -104,19 +95,6 @@ async fn create_miner() -> anyhow::Result<()> { !info.peer_id.is_empty(), "miner {miner}: no peer ID recorded" ); - - poll(&format!("lotus StateGetActor {miner}"), || async { - if !lotus_accepts_forest_head(&forest, &lotus).await? { - return Ok(None); - } - get_actor(&lotus, *miner).await - }) - .await?; - let lotus_info = miner_info(&lotus, *miner).await?; - ensure!( - lotus_info == info, - "StateMinerInfo for {miner} differs:\nforest: {info:?}\nlotus: {lotus_info:?}" - ); Ok(()) } @@ -136,30 +114,14 @@ fn init_miner(repo: &str, owner: Address, worker: Address) -> anyhow::Result<()> /// Pledges one CC sector; the chain verifies the seal proof on prove-commit. async fn pledge_sector() -> anyhow::Result<()> { - let forest = forest_client()?; - let lotus = lotus_client()?; - let miner_api = MinerApi::new()?; - let miner = genesis_miner()?; - let sector = miner_api.pledge_sector().await?; - - wait_until_proving(&miner_api, sector).await?; - - let forest_info = sector_info(&forest, miner, sector) + let net = Devnet::connect()?; + let miner = TestMiner::genesis()?; + let sector = miner.api.pledge_sector().await?; + miner.api.wait_until_proving(sector).await?; + net.synced() .await? - .with_context(|| format!("sector {sector} is Proving, but Forest has no info for it"))?; - let lotus_info = poll( - &format!("lotus StateSectorGetInfo {miner} {sector}"), - || async { - if !lotus_accepts_forest_head(&forest, &lotus).await? { - return Ok(None); - } - sector_info(&lotus, miner, sector).await - }, - ) - .await?; - ensure!( - forest_info == lotus_info, - "StateSectorGetInfo for sector {sector} differs:\nforest: {forest_info:?}\nlotus: {lotus_info:?}" - ); + .same(|tsk| StateSectorGetInfo::request((miner.actor, sector, tsk))) + .await? + .with_context(|| format!("sector {sector} is Proving, but the chain has no info for it"))?; Ok(()) } diff --git a/src/dev/subcommands/tests_cmd/helpers.rs b/src/dev/subcommands/tests_cmd/helpers.rs index 8083bb7e141..fb4932c9277 100644 --- a/src/dev/subcommands/tests_cmd/helpers.rs +++ b/src/dev/subcommands/tests_cmd/helpers.rs @@ -434,13 +434,6 @@ pub fn lotus_exec(args: &[&str]) -> anyhow::Result { docker(&full) } -/// Runs `lotus-miner ` inside the `lotus-miner` container on the docker devnet. -pub fn lotus_miner_exec(args: &[&str]) -> anyhow::Result { - let mut full = vec!["exec", "lotus-miner", "lotus-miner"]; - full.extend_from_slice(args); - docker(&full) -} - /// POST a JSON-RPC v1 request and return the `result` field, or `None` if /// the server responded without one. pub async fn rpc_call_opt(method: &str, params: Value) -> anyhow::Result> { diff --git a/src/rpc/mod.rs b/src/rpc/mod.rs index 6140d45d271..b484aa4cbd0 100644 --- a/src/rpc/mod.rs +++ b/src/rpc/mod.rs @@ -468,7 +468,7 @@ use openrpc_types::{self, ParamStructure}; pub const DEFAULT_PORT: u16 = 2345; /// Request timeout read from environment variables -pub(crate) static DEFAULT_REQUEST_TIMEOUT: LazyLock = LazyLock::new(|| { +static DEFAULT_REQUEST_TIMEOUT: LazyLock = LazyLock::new(|| { env::var("FOREST_RPC_DEFAULT_TIMEOUT") .ok() .and_then(|it| Duration::from_secs(it.parse().ok()?).into()) diff --git a/src/tool/subcommands/api_cmd.rs b/src/tool/subcommands/api_cmd.rs index d2203db5346..a9ca929f03d 100644 --- a/src/tool/subcommands/api_cmd.rs +++ b/src/tool/subcommands/api_cmd.rs @@ -4,6 +4,7 @@ mod api_compare_tests; pub(crate) mod generate_test_snapshot; mod report; +pub(crate) use report::generate_diff; mod state_decode_params_tests; mod stateful_tests; mod test_snapshot; From 62561ce329d13e13988c5f966eb5292e2073158f Mon Sep 17 00:00:00 2001 From: Aryan Tikarya Date: Thu, 29 Oct 2026 15:47:22 +0530 Subject: [PATCH 4/7] chore(devnet): keep volumes on with_devnet.sh exit and document test:devnet:miner --- scripts/devnet-forest-miner/README.md | 6 ++++++ scripts/devnet/with_devnet.sh | 3 ++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/scripts/devnet-forest-miner/README.md b/scripts/devnet-forest-miner/README.md index ff9ce176fb2..91f7c905e56 100644 --- a/scripts/devnet-forest-miner/README.md +++ b/scripts/devnet-forest-miner/README.md @@ -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 +``` diff --git a/scripts/devnet/with_devnet.sh b/scripts/devnet/with_devnet.sh index 6a21af3be83..ea8cc7c08e2 100755 --- a/scripts/devnet/with_devnet.sh +++ b/scripts/devnet/with_devnet.sh @@ -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 From cc9111977c6e0fe89f99331e0c459a074289af75 Mon Sep 17 00:00:00 2001 From: Aryan Tikarya Date: Thu, 29 Oct 2026 15:47:40 +0530 Subject: [PATCH 5/7] refactor(dev): move the onboarding helpers below the trials --- .../devnet_cmd/miner/onboarding.rs | 44 +++++++++---------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/src/dev/subcommands/devnet_cmd/miner/onboarding.rs b/src/dev/subcommands/devnet_cmd/miner/onboarding.rs index 9cfecfd20fb..434934eccc5 100644 --- a/src/dev/subcommands/devnet_cmd/miner/onboarding.rs +++ b/src/dev/subcommands/devnet_cmd/miner/onboarding.rs @@ -28,14 +28,6 @@ pub(super) fn tests() -> Vec { ] } -async fn list_miners(client: &Client) -> anyhow::Result> { - Ok(client - .call(StateListMiners::request((ApiTipsetKey(None),))?) - .await? - .into_iter() - .collect()) -} - async fn create_miner() -> anyhow::Result<()> { let net = Devnet::connect()?; let owner_key = Address::from_str(&FOREST_TEST_PRELOADED_ADDRESS) @@ -98,20 +90,6 @@ async fn create_miner() -> anyhow::Result<()> { Ok(()) } -fn init_miner(repo: &str, owner: Address, worker: Address) -> anyhow::Result<()> { - let port = harness_env("FOREST_RPC_PORT")?; - let data_dir = harness_env("FOREST_DATA_DIR")?; - let script = format!( - "export LOTUS_MINER_PATH='{repo}' \ - FULLNODE_API_INFO=\"$(cat '{data_dir}/token.jwt'):/dns/forest/tcp/{port}/http\" && \ - exec lotus-miner init --owner='{owner}' --worker='{worker}' --sector-size=2KiB \ - --no-local-storage --nosync" - ); - docker(&["exec", "lotus-miner", "sh", "-c", &script]) - .context("`lotus-miner init` against Forest failed")?; - Ok(()) -} - /// Pledges one CC sector; the chain verifies the seal proof on prove-commit. async fn pledge_sector() -> anyhow::Result<()> { let net = Devnet::connect()?; @@ -125,3 +103,25 @@ async fn pledge_sector() -> anyhow::Result<()> { .with_context(|| format!("sector {sector} is Proving, but the chain has no info for it"))?; Ok(()) } + +async fn list_miners(client: &Client) -> anyhow::Result> { + Ok(client + .call(StateListMiners::request((ApiTipsetKey(None),))?) + .await? + .into_iter() + .collect()) +} + +fn init_miner(repo: &str, owner: Address, worker: Address) -> anyhow::Result<()> { + let port = harness_env("FOREST_RPC_PORT")?; + let data_dir = harness_env("FOREST_DATA_DIR")?; + let script = format!( + "export LOTUS_MINER_PATH='{repo}' \ + FULLNODE_API_INFO=\"$(cat '{data_dir}/token.jwt'):/dns/forest/tcp/{port}/http\" && \ + exec lotus-miner init --owner='{owner}' --worker='{worker}' --sector-size=2KiB \ + --no-local-storage --nosync" + ); + docker(&["exec", "lotus-miner", "sh", "-c", &script]) + .context("`lotus-miner init` against Forest failed")?; + Ok(()) +} From a064df83f954ecc1874f5286c3043c4923309933 Mon Sep 17 00:00:00 2001 From: Aryan Tikarya Date: Thu, 29 Oct 2026 17:18:56 +0530 Subject: [PATCH 6/7] fix linter issue --- src/dev/subcommands/devnet_cmd/miner/api.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dev/subcommands/devnet_cmd/miner/api.rs b/src/dev/subcommands/devnet_cmd/miner/api.rs index c61c9706ffa..135135c489e 100644 --- a/src/dev/subcommands/devnet_cmd/miner/api.rs +++ b/src/dev/subcommands/devnet_cmd/miner/api.rs @@ -157,7 +157,7 @@ struct SectorStatus { } /// Lotus `api.SectorLog` (): -/// `message` is the marshalled event, `trace` the error text when the event carried one. +/// `message` is the `marshalled` event, `trace` the error text when the event carried one. #[derive(Deserialize)] #[serde(rename_all = "PascalCase")] struct SectorLog { From 94844b104822a240b73f9d38792868a0479c23de Mon Sep 17 00:00:00 2001 From: Aryan Tikarya Date: Mon, 2 Nov 2026 15:42:08 +0530 Subject: [PATCH 7/7] fix(dev): check Lotus's bad-block cache while waiting and share the labelled call --- scripts/devnet/test_harness.sh | 6 +-- src/dev/subcommands/devnet_cmd/miner.rs | 19 ++++++- src/dev/subcommands/devnet_cmd/miner/api.rs | 47 +++++++++--------- .../subcommands/devnet_cmd/miner/devnet.rs | 49 +++++-------------- 4 files changed, 57 insertions(+), 64 deletions(-) diff --git a/scripts/devnet/test_harness.sh b/scripts/devnet/test_harness.sh index 3a0bd893cbd..d975741b819 100644 --- a/scripts/devnet/test_harness.sh +++ b/scripts/devnet/test_harness.sh @@ -7,9 +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}}" -# The miner suites read it to refuse any devnet but the forest-produced one. -export DEVNET_DIR +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 @@ -24,7 +22,7 @@ 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 miner's repo token is its admin token; `LOTUS_MINER_PATH` is set in the container. + # 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" diff --git a/src/dev/subcommands/devnet_cmd/miner.rs b/src/dev/subcommands/devnet_cmd/miner.rs index ab66dcf7648..e1e0718ab90 100644 --- a/src/dev/subcommands/devnet_cmd/miner.rs +++ b/src/dev/subcommands/devnet_cmd/miner.rs @@ -9,9 +9,12 @@ mod devnet; mod onboarding; use crate::dev::subcommands::tests_cmd::helpers::harness_env; -use anyhow::ensure; +use crate::lotus_json::HasLotusJson; +use crate::rpc::{Client, Request, humanize_rpc_error}; +use anyhow::{Context as _, ensure}; use clap::ValueEnum as _; use libtest_mimic::{Arguments, Trial}; +use std::fmt::Debug; use std::path::Path; /// Lotus Miner integration tests on Forest (`scripts/devnet-forest-miner` only) @@ -55,3 +58,17 @@ fn tests(group: Group) -> Vec { Group::Onboarding => onboarding::tests(), } } + +/// Calls `request` on `node`, naming the node and the method in the error. +async fn call( + client: &Client, + node: &str, + request: Request, +) -> anyhow::Result { + let method = request.method_name.clone(); + client + .call(request) + .await + .with_context(|| format!("{node} {method}")) + .map_err(humanize_rpc_error) +} diff --git a/src/dev/subcommands/devnet_cmd/miner/api.rs b/src/dev/subcommands/devnet_cmd/miner/api.rs index 135135c489e..8094adf30ed 100644 --- a/src/dev/subcommands/devnet_cmd/miner/api.rs +++ b/src/dev/subcommands/devnet_cmd/miner/api.rs @@ -5,7 +5,7 @@ //! Lotus Miner JSON-RPC API, which Forest's RPC does not define. use crate::dev::subcommands::tests_cmd::helpers::{harness_env, poll}; -use crate::rpc::{ApiPaths, Client, Request, humanize_rpc_error}; +use crate::rpc::{ApiPaths, Client, Request}; use crate::shim::address::Address; use crate::utils::UrlFromMultiAddr; use anyhow::{Context as _, bail}; @@ -14,15 +14,16 @@ use serde::Deserialize; use serde::de::{DeserializeOwned, IgnoredAny}; use serde_json::json; use std::str::FromStr as _; +use std::sync::atomic::{AtomicU8, Ordering}; -/// A `lotus-miner` whose full node is Forest: its actor on chain and its API. +/// The miner's on-chain actor and its API. pub(super) struct TestMiner { pub actor: Address, pub api: MinerApi, } impl TestMiner { - /// Returns the genesis miner the compose devnet runs, from `MINER_ACTOR_ADDRESS` and `MINER_API_INFO`. + /// Returns the genesis miner the compose devnet runs. pub fn genesis() -> anyhow::Result { let raw = harness_env("MINER_ACTOR_ADDRESS")?; let actor = Address::from_str(&raw) @@ -34,11 +35,11 @@ impl TestMiner { } } -/// The Lotus Miner API (`/rpc/v0`) with the admin token that flushing the sealing batches needs. +/// The Lotus Miner API; its token must be admin, since the batch flushes are admin methods. pub(super) struct MinerApi(Client); impl MinerApi { - /// Connects to `MINER_API_INFO` (`:`, the shape of Lotus's own variable). + /// Connects to `MINER_API_INFO`. pub fn connect() -> anyhow::Result { let raw = harness_env("MINER_API_INFO")?; let UrlFromMultiAddr(url) = raw.parse().context("parsing MINER_API_INFO")?; @@ -50,12 +51,12 @@ impl MinerApi { method: &'static str, params: serde_json::Value, ) -> anyhow::Result { - let result: serde_json::Value = self - .0 - .call(Request::new(method, params, ApiPaths::V0)) - .await - .with_context(|| format!("Lotus Miner {method}")) - .map_err(humanize_rpc_error)?; + let result: serde_json::Value = super::call( + &self.0, + "Lotus Miner", + Request::new(method, params, ApiPaths::V0), + ) + .await?; serde_json::from_value(result) .with_context(|| format!("decoding the Lotus Miner {method} result")) } @@ -71,7 +72,8 @@ impl MinerApi { .await } - /// Flushes the pre-commit and commit batches, which Lotus may otherwise hold for up to a day. + /// Flushes the pre-commit and commit batches, which Lotus may otherwise hold for up to a day + /// (). async fn flush_sealing_batches(&self) -> anyhow::Result<()> { for method in [ "Filecoin.SectorPreCommitFlush", @@ -82,19 +84,19 @@ impl MinerApi { Ok(()) } - /// Waits until `sector` is `Proving`, flushing the sealing batches on every poll. - /// - /// # Errors - /// The sector enters a state that never leads to `Proving`, or the poll budget passes; both - /// errors carry the sector log, whose trace lines hold Lotus's error text. + /// Waits until `sector` is `Proving` or `Available`, flushing the sealing batches on every + /// poll; a state that never leads to `Proving` fails at once. pub async fn wait_until_proving(&self, sector: u64) -> anyhow::Result<()> { + // `poll` logs every attempt, so the state is printed only when it changes. + let last_state = AtomicU8::new(u8::MAX); let waited = poll(&format!("sector {sector} to reach Proving"), || async { self.flush_sealing_batches().await?; let status = self.sectors_status(sector).await?; - eprintln!("sector {sector} state: {:?}", status.state); + if last_state.swap(status.state as u8, Ordering::Relaxed) != status.state as u8 { + 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 @@ -156,8 +158,8 @@ struct SectorStatus { log: Vec, } -/// Lotus `api.SectorLog` (): -/// `message` is the `marshalled` event, `trace` the error text when the event carried one. +/// Lotus `api.SectorLog` (); +/// `trace` is the error text when the event carried one. #[derive(Deserialize)] #[serde(rename_all = "PascalCase")] struct SectorLog { @@ -168,8 +170,7 @@ struct SectorLog { /// Lotus `api.SectorState` /// (). -/// Unknown values fail to decode. -#[derive(Debug, Deserialize)] +#[derive(Debug, Clone, Copy, Deserialize)] enum SectorState { /// A new sector, before Lotus processes its first event. #[serde(rename = "")] diff --git a/src/dev/subcommands/devnet_cmd/miner/devnet.rs b/src/dev/subcommands/devnet_cmd/miner/devnet.rs index a726a920edf..c676dda3854 100644 --- a/src/dev/subcommands/devnet_cmd/miner/devnet.rs +++ b/src/dev/subcommands/devnet_cmd/miner/devnet.rs @@ -1,17 +1,15 @@ // Copyright 2019-2026 ChainSafe Systems // SPDX-License-Identifier: Apache-2.0, MIT -//! The two full nodes of the devnet and the pinned comparison the trials assert with: Forest -//! answers, and Lotus, which validates the chain Forest produces, must answer the same. - +use super::call; use crate::blocks::Tipset; use crate::dev::subcommands::tests_cmd::helpers::{forest_client, lotus_client, poll}; use crate::lotus_json::HasLotusJson; use crate::rpc::prelude::*; use crate::rpc::types::ApiTipsetKey; -use crate::rpc::{Client, Request, humanize_rpc_error}; +use crate::rpc::{Client, Request}; use crate::tool::subcommands::api_cmd::generate_diff; -use anyhow::{Context as _, bail, ensure}; +use anyhow::{bail, ensure}; use std::fmt::Debug; /// Forest under test and the Lotus node that validates the chain Forest produces. @@ -28,17 +26,21 @@ impl Devnet { }) } - /// Waits until Lotus has Forest's current head and returns that head as the tipset to compare at. - /// - /// # Errors - /// Lotus holds a different tipset at Forest's head epoch, or has marked one of its blocks bad; - /// both fail at once instead of retrying, since they mean Lotus rejected what Forest produced. - /// Lotus not reaching that epoch within the poll budget also fails. + /// Waits until Lotus has Forest's current head, failing at once if Lotus rejected one of + /// Forest's blocks, and returns that head as the tipset to compare at. pub async fn synced(&self) -> anyhow::Result> { let head = call(&self.forest, "forest", ChainHead::request(())?).await?; poll( &format!("lotus to reach epoch {}", head.epoch()), || async { + for cid in head.cids() { + let reason = call(&self.lotus, "lotus", SyncCheckBad::request((cid,))?).await?; + ensure!( + reason.is_empty(), + "Lotus rejected Forest block {cid} at epoch {}: {reason}", + head.epoch() + ); + } let lotus_head = call(&self.lotus, "lotus", ChainHead::request(())?).await?; Ok((lotus_head.epoch() >= head.epoch()).then_some(())) }, @@ -57,14 +59,6 @@ impl Devnet { head.key(), lotus_at_height.key() ); - for cid in head.cids() { - let reason = call(&self.lotus, "lotus", SyncCheckBad::request((cid,))?).await?; - ensure!( - reason.is_empty(), - "Lotus rejected Forest block {cid} at epoch {}: {reason}", - head.epoch() - ); - } Ok(At { devnet: self, head }) } } @@ -77,9 +71,6 @@ pub(super) struct At<'a> { impl At<'_> { /// Calls the request, built with this tipset's key, on both nodes and returns the answer they agree on. - /// - /// # Errors - /// Either node fails the call, or the answers differ; the message then carries a line diff of the two. pub async fn same( &self, request: impl Fn(ApiTipsetKey) -> serde_json::Result>, @@ -108,17 +99,3 @@ impl At<'_> { ) } } - -/// Calls `request` on `node`, naming the node and the method in the error. -async fn call( - client: &Client, - node: &str, - request: Request, -) -> anyhow::Result { - let method = request.method_name.clone(); - client - .call(request) - .await - .with_context(|| format!("{node} {method}")) - .map_err(humanize_rpc_error) -}