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/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..d975741b819 100644 --- a/scripts/devnet/test_harness.sh +++ b/scripts/devnet/test_harness.sh @@ -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 @@ -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)" @@ -43,6 +47,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}..." 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 diff --git a/src/dev/subcommands/devnet_cmd/miner.rs b/src/dev/subcommands/devnet_cmd/miner.rs index 156dcbdac49..e1e0718ab90 100644 --- a/src/dev/subcommands/devnet_cmd/miner.rs +++ b/src/dev/subcommands/devnet_cmd/miner.rs @@ -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)] @@ -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(), @@ -59,264 +59,16 @@ fn tests(group: Group) -> Vec { } } -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( +/// Calls `request` on `node`, naming the node and the method in the error. +async fn call( 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 - ))) + 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 new file mode 100644 index 00000000000..8094adf30ed --- /dev/null +++ b/src/dev/subcommands/devnet_cmd/miner/api.rs @@ -0,0 +1,265 @@ +// 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}; +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 _; +use std::sync::atomic::{AtomicU8, Ordering}; + +/// 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. + 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; its token must be admin, since the batch flushes are admin methods. +pub(super) struct MinerApi(Client); + +impl MinerApi { + /// 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")?; + Ok(Self(Client::from_url(url))) + } + + async fn call( + &self, + method: &'static str, + params: serde_json::Value, + ) -> anyhow::Result { + 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")) + } + + /// 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` 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?; + 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(())), + 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` (); +/// `trace` is 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` +/// (). +#[derive(Debug, Clone, Copy, 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..c676dda3854 --- /dev/null +++ b/src/dev/subcommands/devnet_cmd/miner/devnet.rs @@ -0,0 +1,101 @@ +// Copyright 2019-2026 ChainSafe Systems +// SPDX-License-Identifier: Apache-2.0, MIT + +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}; +use crate::tool::subcommands::api_cmd::generate_diff; +use anyhow::{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, 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(())) + }, + ) + .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() + ); + 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. + 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() + ) + } +} diff --git a/src/dev/subcommands/devnet_cmd/miner/onboarding.rs b/src/dev/subcommands/devnet_cmd/miner/onboarding.rs index 4ba562fec8b..434934eccc5 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; @@ -28,46 +28,24 @@ 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 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 +54,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 +63,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,22 +87,31 @@ async fn create_miner() -> anyhow::Result<()> { !info.peer_id.is_empty(), "miner {miner}: no peer ID recorded" ); + Ok(()) +} - 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:?}" - ); +/// Pledges one CC sector; the chain verifies the seal proof on prove-commit. +async fn pledge_sector() -> anyhow::Result<()> { + 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? + .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(()) } +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")?; @@ -133,33 +125,3 @@ fn init_miner(repo: &str, owner: Address, worker: Address) -> anyhow::Result<()> .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 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) - .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:?}" - ); - 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/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; } 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;