From cb0fa3336d2afd8f64162e571c3eb77ab574b299 Mon Sep 17 00:00:00 2001 From: Shashank Date: Sat, 14 Nov 2026 01:55:27 +0530 Subject: [PATCH 1/4] add terminate test --- .github/workflows/miner-tests.yml | 5 +- src/dev/subcommands/devnet_cmd/miner.rs | 3 + src/dev/subcommands/devnet_cmd/miner/api.rs | 108 ++++++++- .../subcommands/devnet_cmd/miner/terminate.rs | 221 ++++++++++++++++++ src/rpc/types/mod.rs | 4 +- 5 files changed, 336 insertions(+), 5 deletions(-) create mode 100644 src/dev/subcommands/devnet_cmd/miner/terminate.rs diff --git a/.github/workflows/miner-tests.yml b/.github/workflows/miner-tests.yml index 1ad0b4de46e..577398de667 100644 --- a/.github/workflows/miner-tests.yml +++ b/.github/workflows/miner-tests.yml @@ -19,6 +19,7 @@ env: CI: 1 CACHE_TIMEOUT_MINUTES: 5 SCRIPT_TIMEOUT_MINUTES: 30 + MINER_TEST_TIMEOUT_MINUTES: 240 FIL_PROOFS_PARAMETER_CACHE: /var/tmp/filecoin-proof-parameters SHELL_IMAGE: busybox @@ -84,7 +85,7 @@ jobs: fail-fast: false matrix: # The `forest-dev devnet miner --group` values. Add groups as more Lotus itests are ported. - group: [onboarding, wdpost] + group: [onboarding, wdpost, terminate] env: # We use a custom Dockerfile for CI to speed up the build process. FOREST_DOCKERFILE_OVERRIDE: scripts/devnet/forest_ci.dockerfile @@ -127,7 +128,7 @@ jobs: source ./scripts/devnet/test_harness.sh devnet_test_env_init forest-dev devnet miner --group "$MINER_TEST_GROUP" - timeout-minutes: 120 + timeout-minutes: ${{ fromJSON(env.MINER_TEST_TIMEOUT_MINUTES) }} - name: Dump docker logs if: always() uses: jwalton/gh-docker-logs@v2 diff --git a/src/dev/subcommands/devnet_cmd/miner.rs b/src/dev/subcommands/devnet_cmd/miner.rs index afabf8eabfb..b30cfdd3a51 100644 --- a/src/dev/subcommands/devnet_cmd/miner.rs +++ b/src/dev/subcommands/devnet_cmd/miner.rs @@ -7,6 +7,7 @@ mod api; mod devnet; mod onboarding; +mod terminate; mod wdpost; use crate::dev::subcommands::tests_cmd::helpers::harness_env; @@ -33,6 +34,7 @@ pub struct MinerTestCommand { enum Group { Onboarding, Wdpost, + Terminate, } impl MinerTestCommand { @@ -59,6 +61,7 @@ fn tests(group: Group) -> Vec { match group { Group::Onboarding => onboarding::tests(), Group::Wdpost => wdpost::tests(), + Group::Terminate => terminate::tests(), } } diff --git a/src/dev/subcommands/devnet_cmd/miner/api.rs b/src/dev/subcommands/devnet_cmd/miner/api.rs index 8094adf30ed..da206311558 100644 --- a/src/dev/subcommands/devnet_cmd/miner/api.rs +++ b/src/dev/subcommands/devnet_cmd/miner/api.rs @@ -5,10 +5,12 @@ //! Lotus Miner JSON-RPC API, which Forest's RPC does not define. use crate::dev::subcommands::tests_cmd::helpers::{harness_env, poll}; +use crate::lotus_json::LotusJson; use crate::rpc::{ApiPaths, Client, Request}; use crate::shim::address::Address; use crate::utils::UrlFromMultiAddr; -use anyhow::{Context as _, bail}; +use anyhow::{Context as _, bail, ensure}; +use cid::Cid; use itertools::Itertools as _; use serde::Deserialize; use serde::de::{DeserializeOwned, IgnoredAny}; @@ -139,6 +141,110 @@ impl MinerApi { status.state ))) } + + /// Queues `sector` for early termination. + pub async fn terminate_sector(&self, sector: u64) -> anyhow::Result<()> { + self.call::("Filecoin.SectorTerminate", json!([sector])) + .await?; + Ok(()) + } + + /// Flushes the terminate batch; returns the message CID when one was sent. + pub async fn terminate_flush(&self) -> anyhow::Result> { + Ok(self + .call::>>("Filecoin.SectorTerminateFlush", json!([])) + .await? + .map(|c| c.into_inner())) + } + + /// Sectors waiting in the terminate batch. + pub async fn terminate_pending(&self) -> anyhow::Result> { + let pending: Vec = self + .call("Filecoin.SectorTerminatePending", json!([])) + .await?; + Ok(pending.into_iter().map(|s| s.number).collect()) + } + + /// After [`Self::terminate_sector`], flushes the terminate batch and waits until `sector` is + /// `TerminateFinality`. Returns the `TerminateSectors` message CID. + pub async fn wait_until_terminated(&self, sector: u64) -> anyhow::Result { + let last_state = AtomicU8::new(u8::MAX); + let terminate_cid = std::sync::Mutex::new(None::); + let waited = poll( + &format!("sector {sector} to reach TerminateFinality"), + || async { + 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::Terminating => { + let pending = self.terminate_pending().await?; + ensure!( + pending.contains(§or), + "sector {sector} is Terminating but not in SectorTerminatePending ({pending:?})" + ); + if let Some(cid) = self.terminate_flush().await? { + eprintln!("terminate message: {cid}"); + *terminate_cid.lock().expect("terminate_cid lock") = Some(cid); + let pending = self.terminate_pending().await?; + ensure!( + !pending.contains(§or), + "sector {sector} still pending after terminate flush: {pending:?}" + ); + } + Ok(None) + } + SectorState::TerminateWait => Ok(None), + SectorState::TerminateFinality => { + let cid = terminate_cid + .lock() + .expect("terminate_cid lock") + .clone() + .with_context(|| { + format!( + "sector {sector} reached TerminateFinality without a terminate message CID" + ) + })?; + Ok(Some(cid)) + } + SectorState::TerminateFailed + | SectorState::Removing + | SectorState::RemoveFailed + | SectorState::Removed + | SectorState::FailedUnrecoverable => bail!( + "sector {sector} reached {:?}, which never leads to TerminateFinality", + status.state + ), + _ => Ok(None), + } + }, + ) + .await; + match waited { + Ok(cid) => Ok(cid), + Err(e) => { + 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` diff --git a/src/dev/subcommands/devnet_cmd/miner/terminate.rs b/src/dev/subcommands/devnet_cmd/miner/terminate.rs new file mode 100644 index 00000000000..d223f5104e5 --- /dev/null +++ b/src/dev/subcommands/devnet_cmd/miner/terminate.rs @@ -0,0 +1,221 @@ +// Copyright 2019-2026 ChainSafe Systems +// SPDX-License-Identifier: Apache-2.0, MIT + +//! Early termination of a pledged CC sector through Forest's RPC and VM. +//! +//! lotus-miner finds the sector's deadline and partition with Forest +//! (`StateSectorPartition`, `StateMinerPartitions`, `StateMinerProvingDeadline`) +//! and sends `TerminateSectors`. Forest applies the termination fee and power +//! removal. A wrong partition makes the actor reject the message — covered here +//! with a `StateCall` mutation check before the real terminate. +//! +//! Mirrors Lotus `TestTerminate` (happy path only; no FIP-0098 fee exports or +//! actor events). Terminates a pledged sector on the genesis miner, never a +//! preseal, so block production stays intact. + +use super::api::TestMiner; +use super::devnet::{At, Devnet}; +use crate::dev::subcommands::tests_cmd::helpers::{ + block_on, poll, poll_until_message_executed, trial, +}; +use crate::rpc::prelude::*; +use crate::shim::address::Address; +use crate::shim::message::Message; +use crate::shim::sector::SectorSize; +use anyhow::{Context as _, ensure}; +use fil_actor_miner_state::v19::{Method, TerminateSectorsParams, TerminationDeclaration}; +use fil_actors_shared::fvm_ipld_bitfield::BitField; +use fvm_ipld_encoding::RawBytes; +use libtest_mimic::Trial; +use num::BigInt; + +pub(super) fn tests() -> Vec { + vec![trial("terminate::pledged_sector", || { + block_on(pledged_sector()) + })] +} + +async fn pledged_sector() -> anyhow::Result<()> { + let net = Devnet::connect()?; + let miner = TestMiner::genesis()?; + + let at = net.synced().await?; + let before = at + .same(|tsk| StateMinerPower::request((miner.actor, tsk))) + .await?; + let info = at + .same(|tsk| StateMinerInfo::request((miner.actor, tsk))) + .await?; + ensure!( + info.sector_size == SectorSize::_2KiB, + "expected a 2 KiB miner, got {:?}", + info.sector_size + ); + let sector_power = BigInt::from(SectorSize::_2KiB as u64); + + let sector = miner.api.pledge_sector().await?; + miner.api.wait_until_proving(sector).await?; + + let location = net + .synced() + .await? + .same(|tsk| StateSectorPartition::request((miner.actor, sector, tsk))) + .await?; + let at = wait_for_sector_power( + &net, + miner.actor, + sector, + &before.miner_power.raw_byte_power, + ) + .await?; + let powered = at + .same(|tsk| StateMinerPower::request((miner.actor, tsk))) + .await?; + let expected_powered = &before.miner_power.raw_byte_power + §or_power; + ensure!( + powered.miner_power.raw_byte_power == expected_powered, + "after proving sector {sector}, raw power is {}, expected {expected_powered}", + powered.miner_power.raw_byte_power + ); + + // Pin the location RPCs lotus-miner will use to build TerminateSectors. + at.same(|tsk| StateMinerProvingDeadline::request((miner.actor, tsk))) + .await?; + let partitions = at + .same(|tsk| StateMinerPartitions::request((miner.actor, location.deadline, tsk))) + .await?; + let partition = partitions + .get(usize::try_from(location.partition)?) + .with_context(|| { + format!( + "StateSectorPartition says partition {}, but deadline {} has {} partitions", + location.partition, + location.deadline, + partitions.len() + ) + })?; + ensure!( + partition.live_sectors.get(sector), + "sector {sector} is not live in deadline {} partition {}", + location.deadline, + location.partition + ); + + // Mutation check: the same sector at the wrong partition must fail in Forest's VM. + let mut wrong_sectors = BitField::new(); + wrong_sectors.set(sector); + let wrong = Message::builder() + .from(info.worker) + .to(miner.actor) + .method_num(Method::TerminateSectors as u64) + .params(RawBytes::serialize(TerminateSectorsParams { + terminations: vec![TerminationDeclaration { + deadline: location.deadline, + partition: location.partition ^ 1, + sectors: wrong_sectors, + }], + })?) + .build(); + let invoc = at + .same(|tsk| StateCall::request((wrong.clone(), tsk))) + .await?; + let receipt = invoc + .msg_rct + .context("no receipt for the wrong-partition terminate StateCall")?; + ensure!( + !receipt.exit_code().is_success(), + "TerminateSectors with partition {} (sector is in {}) succeeded with exit 0; Forest's partition lookup would not be load-bearing", + location.partition ^ 1, + location.partition + ); + + miner.api.terminate_sector(sector).await?; + let terminate_cid = miner.api.wait_until_terminated(sector).await?; + let lookup = poll_until_message_executed(&net.forest, terminate_cid).await?; + ensure!( + lookup.receipt.exit_code().is_success(), + "TerminateSectors {terminate_cid} exited {}", + lookup.receipt.exit_code() + ); + let at = net.synced().await?; + let after = at + .same(|tsk| StateMinerPower::request((miner.actor, tsk))) + .await?; + ensure!( + after.miner_power.raw_byte_power == before.miner_power.raw_byte_power, + "after terminating sector {sector}, raw power is {}, expected the pre-pledge {}", + after.miner_power.raw_byte_power, + before.miner_power.raw_byte_power + ); + + let partitions = at + .same(|tsk| StateMinerPartitions::request((miner.actor, location.deadline, tsk))) + .await?; + let partition = partitions + .get(usize::try_from(location.partition)?) + .with_context(|| { + format!( + "deadline {} lost partition {} after terminate", + location.deadline, location.partition + ) + })?; + ensure!( + partition.all_sectors.get(sector), + "sector {sector} missing from AllSectors after terminate" + ); + ensure!( + !partition.live_sectors.get(sector), + "sector {sector} still in LiveSectors after terminate" + ); + ensure!( + !partition.active_sectors.get(sector), + "sector {sector} still in ActiveSectors after terminate" + ); + Ok(()) +} + +/// Waits until `miner`'s raw power is `baseline` plus one 2 KiB sector. Prove-commit usually adds +/// power immediately; if not, wait for the sector's next deadline close so WindowPoSt can. +async fn wait_for_sector_power<'a>( + net: &'a Devnet, + miner: Address, + sector: u64, + baseline: &BigInt, +) -> anyhow::Result> { + let target = baseline + BigInt::from(SectorSize::_2KiB as u64); + let at = net.synced().await?; + let power = at + .same(|tsk| StateMinerPower::request((miner, tsk))) + .await?; + if power.miner_power.raw_byte_power >= target { + return Ok(at); + } + + let location = at + .same(|tsk| StateSectorPartition::request((miner, sector, tsk))) + .await?; + let current = at + .same(|tsk| StateMinerProvingDeadline::request((miner, tsk))) + .await? + .0; + let deadline = current.for_index(location.deadline).next_not_elapsed(); + eprintln!( + "raw power still {}; waiting for deadline {} to close at epoch {} so WindowPoSt can claim sector {sector}", + power.miner_power.raw_byte_power, location.deadline, deadline.close + ); + net.wait_for_epoch(deadline.close).await?; + poll( + &format!( + "raw power {target} after deadline {} for sector {sector}", + location.deadline + ), + || async { + let at = net.synced().await?; + let power = at + .same(|tsk| StateMinerPower::request((miner, tsk))) + .await?; + Ok((power.miner_power.raw_byte_power >= target).then_some(at)) + }, + ) + .await +} diff --git a/src/rpc/types/mod.rs b/src/rpc/types/mod.rs index 5a9be22fcc1..8dc0dc4d471 100644 --- a/src/rpc/types/mod.rs +++ b/src/rpc/types/mod.rs @@ -393,7 +393,7 @@ lotus_json_with_self!(MinerSectors); pub struct MinerPartitions { #[schemars(with = "LotusJson")] #[serde(with = "crate::lotus_json")] - all_sectors: BitField, + pub all_sectors: BitField, #[schemars(with = "LotusJson")] #[serde(with = "crate::lotus_json")] faulty_sectors: BitField, @@ -405,7 +405,7 @@ pub struct MinerPartitions { pub live_sectors: BitField, #[schemars(with = "LotusJson")] #[serde(with = "crate::lotus_json")] - active_sectors: BitField, + pub active_sectors: BitField, } lotus_json_with_self!(MinerPartitions); From 102a06ba1b90a4d99ea48ddd79c2ea3f8f62c7c7 Mon Sep 17 00:00:00 2001 From: Shashank Date: Sat, 14 Nov 2026 02:14:37 +0530 Subject: [PATCH 2/4] lint fix --- src/dev/subcommands/devnet_cmd/miner/api.rs | 100 +++++++++--------- .../subcommands/devnet_cmd/miner/terminate.rs | 70 ++++++++---- 2 files changed, 102 insertions(+), 68 deletions(-) diff --git a/src/dev/subcommands/devnet_cmd/miner/api.rs b/src/dev/subcommands/devnet_cmd/miner/api.rs index da206311558..a57fc7055e4 100644 --- a/src/dev/subcommands/devnet_cmd/miner/api.rs +++ b/src/dev/subcommands/devnet_cmd/miner/api.rs @@ -165,13 +165,16 @@ impl MinerApi { Ok(pending.into_iter().map(|s| s.number).collect()) } - /// After [`Self::terminate_sector`], flushes the terminate batch and waits until `sector` is - /// `TerminateFinality`. Returns the `TerminateSectors` message CID. + /// After [`Self::terminate_sector`], waits until `sector` is `TerminateWait`, + /// `TerminateFinality`, or `Removed`, flushing the terminate batch once while + /// `Terminating`; a failed terminate exit fails at once. Returns the + /// `TerminateSectors` message CID. pub async fn wait_until_terminated(&self, sector: u64) -> anyhow::Result { let last_state = AtomicU8::new(u8::MAX); - let terminate_cid = std::sync::Mutex::new(None::); + // Lotus `msgTriggerred`: once-only flush latch via the captured CID. + let terminate_cid = parking_lot::Mutex::new(None::); let waited = poll( - &format!("sector {sector} to reach TerminateFinality"), + &format!("sector {sector} terminate success"), || async { let status = self.sectors_status(sector).await?; if last_state.swap(status.state as u8, Ordering::Relaxed) != status.state as u8 { @@ -179,41 +182,40 @@ impl MinerApi { } match status.state { SectorState::Terminating => { - let pending = self.terminate_pending().await?; - ensure!( - pending.contains(§or), - "sector {sector} is Terminating but not in SectorTerminatePending ({pending:?})" - ); - if let Some(cid) = self.terminate_flush().await? { - eprintln!("terminate message: {cid}"); - *terminate_cid.lock().expect("terminate_cid lock") = Some(cid); + if terminate_cid.lock().is_none() { let pending = self.terminate_pending().await?; ensure!( - !pending.contains(§or), - "sector {sector} still pending after terminate flush: {pending:?}" + pending.contains(§or), + "sector {sector} is Terminating but not in SectorTerminatePending ({pending:?})" ); + if let Some(cid) = self.terminate_flush().await? { + eprintln!("terminate message: {cid}"); + *terminate_cid.lock() = Some(cid); + let pending = self.terminate_pending().await?; + ensure!( + pending.is_empty(), + "SectorTerminatePending not empty after flush: {pending:?}" + ); + } } Ok(None) } - SectorState::TerminateWait => Ok(None), - SectorState::TerminateFinality => { - let cid = terminate_cid - .lock() - .expect("terminate_cid lock") - .clone() - .with_context(|| { - format!( - "sector {sector} reached TerminateFinality without a terminate message CID" - ) - })?; + SectorState::TerminateWait + | SectorState::TerminateFinality + | SectorState::Removed => { + let cid = terminate_cid.lock().ok_or_else(|| { + anyhow::anyhow!( + "sector {sector} reached {:?} without a terminate message CID", + status.state + ) + })?; Ok(Some(cid)) } SectorState::TerminateFailed | SectorState::Removing | SectorState::RemoveFailed - | SectorState::Removed | SectorState::FailedUnrecoverable => bail!( - "sector {sector} reached {:?}, which never leads to TerminateFinality", + "sector {sector} reached {:?}, which is not a successful terminate exit", status.state ), _ => Ok(None), @@ -221,29 +223,27 @@ impl MinerApi { }, ) .await; - match waited { - Ok(cid) => Ok(cid), - Err(e) => { - 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 - ))) - } - } + let Err(e) = waited else { + return waited; + }; + 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 + ))) } } diff --git a/src/dev/subcommands/devnet_cmd/miner/terminate.rs b/src/dev/subcommands/devnet_cmd/miner/terminate.rs index d223f5104e5..62da45dfdca 100644 --- a/src/dev/subcommands/devnet_cmd/miner/terminate.rs +++ b/src/dev/subcommands/devnet_cmd/miner/terminate.rs @@ -3,15 +3,13 @@ //! Early termination of a pledged CC sector through Forest's RPC and VM. //! -//! lotus-miner finds the sector's deadline and partition with Forest -//! (`StateSectorPartition`, `StateMinerPartitions`, `StateMinerProvingDeadline`) -//! and sends `TerminateSectors`. Forest applies the termination fee and power -//! removal. A wrong partition makes the actor reject the message — covered here -//! with a `StateCall` mutation check before the real terminate. +//! lotus-miner locates the sector via Forest, then sends `TerminateSectors`. A +//! wrong-location `StateCall` mutation runs before the real terminate. //! -//! Mirrors Lotus `TestTerminate` (happy path only; no FIP-0098 fee exports or -//! actor events). Terminates a pledged sector on the genesis miner, never a -//! preseal, so block production stays intact. +//! Mirrors Lotus +//! [`TestTerminate`](https://github.com/filecoin-project/lotus/blob/v1.39.1/itests/sector_terminate_test.go#L35). +//! Terminates a pledged sector on the genesis miner (not a pre-seal), so block +//! production stays intact. use super::api::TestMiner; use super::devnet::{At, Devnet}; @@ -20,7 +18,9 @@ use crate::dev::subcommands::tests_cmd::helpers::{ }; use crate::rpc::prelude::*; use crate::shim::address::Address; +use crate::shim::fvm_shared_latest::error::ExitCode; use crate::shim::message::Message; +use crate::shim::policy::policy_constants::WPOST_PERIOD_DEADLINES; use crate::shim::sector::SectorSize; use anyhow::{Context as _, ensure}; use fil_actor_miner_state::v19::{Method, TerminateSectorsParams, TerminationDeclaration}; @@ -78,7 +78,7 @@ async fn pledged_sector() -> anyhow::Result<()> { powered.miner_power.raw_byte_power ); - // Pin the location RPCs lotus-miner will use to build TerminateSectors. + // Same tipset for deadline/partition lookups lotus-miner will use. at.same(|tsk| StateMinerProvingDeadline::request((miner.actor, tsk))) .await?; let partitions = at @@ -101,7 +101,31 @@ async fn pledged_sector() -> anyhow::Result<()> { location.partition ); - // Mutation check: the same sector at the wrong partition must fail in Forest's VM. + // Wrong in-range location must fail in the VM, not via an OOB partition index. + let (wrong_deadline, wrong_partition) = if partitions.len() > 1 { + let idx = usize::try_from(location.partition)?; + ( + location.deadline, + u64::try_from((idx + 1) % partitions.len())?, + ) + } else { + let mut other = None; + for deadline in 0..WPOST_PERIOD_DEADLINES { + if deadline == location.deadline { + continue; + } + let parts = at + .same(|tsk| StateMinerPartitions::request((miner.actor, deadline, tsk))) + .await?; + if !parts.is_empty() { + other = Some((deadline, 0)); + break; + } + } + other.context( + "deadline has one partition and no other non-empty deadline for the mutation check", + )? + }; let mut wrong_sectors = BitField::new(); wrong_sectors.set(sector); let wrong = Message::builder() @@ -110,8 +134,8 @@ async fn pledged_sector() -> anyhow::Result<()> { .method_num(Method::TerminateSectors as u64) .params(RawBytes::serialize(TerminateSectorsParams { terminations: vec![TerminationDeclaration { - deadline: location.deadline, - partition: location.partition ^ 1, + deadline: wrong_deadline, + partition: wrong_partition, sectors: wrong_sectors, }], })?) @@ -121,11 +145,21 @@ async fn pledged_sector() -> anyhow::Result<()> { .await?; let receipt = invoc .msg_rct - .context("no receipt for the wrong-partition terminate StateCall")?; + .context("no receipt for the wrong-location terminate StateCall")?; + let rejected = invoc + .error + .contains("not all sectors are assigned to the partition") + || invoc.error.contains("can only terminate live sectors") + || invoc.error.contains("failed to find partition"); ensure!( - !receipt.exit_code().is_success(), - "TerminateSectors with partition {} (sector is in {}) succeeded with exit 0; Forest's partition lookup would not be load-bearing", - location.partition ^ 1, + receipt.exit_code() == ExitCode::USR_ILLEGAL_ARGUMENT && rejected, + "TerminateSectors at deadline {wrong_deadline} partition {wrong_partition} exits {} \ + with `{}`, expected {} with a sector/partition rejection; sector {sector} is in \ + deadline {} partition {}", + receipt.exit_code(), + invoc.error, + ExitCode::USR_ILLEGAL_ARGUMENT, + location.deadline, location.partition ); @@ -174,8 +208,8 @@ async fn pledged_sector() -> anyhow::Result<()> { Ok(()) } -/// Waits until `miner`'s raw power is `baseline` plus one 2 KiB sector. Prove-commit usually adds -/// power immediately; if not, wait for the sector's next deadline close so WindowPoSt can. +/// Waits until `miner`'s raw power is `baseline` plus one 2 KiB sector, waiting through the +/// sector's next deadline close when prove-commit has not added power yet. async fn wait_for_sector_power<'a>( net: &'a Devnet, miner: Address, From 49dcb7d5e156ab54021602b766d5c9833df1aaef Mon Sep 17 00:00:00 2001 From: Shashank Date: Sat, 14 Nov 2026 04:22:42 +0530 Subject: [PATCH 3/4] cleanup --- src/dev/subcommands/devnet_cmd/miner/api.rs | 7 +- .../subcommands/devnet_cmd/miner/terminate.rs | 94 +------------------ 2 files changed, 6 insertions(+), 95 deletions(-) diff --git a/src/dev/subcommands/devnet_cmd/miner/api.rs b/src/dev/subcommands/devnet_cmd/miner/api.rs index a57fc7055e4..97adcb63e8b 100644 --- a/src/dev/subcommands/devnet_cmd/miner/api.rs +++ b/src/dev/subcommands/devnet_cmd/miner/api.rs @@ -184,9 +184,12 @@ impl MinerApi { SectorState::Terminating => { if terminate_cid.lock().is_none() { let pending = self.terminate_pending().await?; + if pending.is_empty() { + return Ok(None); + } ensure!( - pending.contains(§or), - "sector {sector} is Terminating but not in SectorTerminatePending ({pending:?})" + pending.as_slice() == [sector], + "SectorTerminatePending is {pending:?}, expected exactly [{sector}]" ); if let Some(cid) = self.terminate_flush().await? { eprintln!("terminate message: {cid}"); diff --git a/src/dev/subcommands/devnet_cmd/miner/terminate.rs b/src/dev/subcommands/devnet_cmd/miner/terminate.rs index 62da45dfdca..35832343374 100644 --- a/src/dev/subcommands/devnet_cmd/miner/terminate.rs +++ b/src/dev/subcommands/devnet_cmd/miner/terminate.rs @@ -3,8 +3,7 @@ //! Early termination of a pledged CC sector through Forest's RPC and VM. //! -//! lotus-miner locates the sector via Forest, then sends `TerminateSectors`. A -//! wrong-location `StateCall` mutation runs before the real terminate. +//! lotus-miner locates the sector via Forest, then sends `TerminateSectors`. //! //! Mirrors Lotus //! [`TestTerminate`](https://github.com/filecoin-project/lotus/blob/v1.39.1/itests/sector_terminate_test.go#L35). @@ -18,14 +17,8 @@ use crate::dev::subcommands::tests_cmd::helpers::{ }; use crate::rpc::prelude::*; use crate::shim::address::Address; -use crate::shim::fvm_shared_latest::error::ExitCode; -use crate::shim::message::Message; -use crate::shim::policy::policy_constants::WPOST_PERIOD_DEADLINES; use crate::shim::sector::SectorSize; use anyhow::{Context as _, ensure}; -use fil_actor_miner_state::v19::{Method, TerminateSectorsParams, TerminationDeclaration}; -use fil_actors_shared::fvm_ipld_bitfield::BitField; -use fvm_ipld_encoding::RawBytes; use libtest_mimic::Trial; use num::BigInt; @@ -78,91 +71,6 @@ async fn pledged_sector() -> anyhow::Result<()> { powered.miner_power.raw_byte_power ); - // Same tipset for deadline/partition lookups lotus-miner will use. - at.same(|tsk| StateMinerProvingDeadline::request((miner.actor, tsk))) - .await?; - let partitions = at - .same(|tsk| StateMinerPartitions::request((miner.actor, location.deadline, tsk))) - .await?; - let partition = partitions - .get(usize::try_from(location.partition)?) - .with_context(|| { - format!( - "StateSectorPartition says partition {}, but deadline {} has {} partitions", - location.partition, - location.deadline, - partitions.len() - ) - })?; - ensure!( - partition.live_sectors.get(sector), - "sector {sector} is not live in deadline {} partition {}", - location.deadline, - location.partition - ); - - // Wrong in-range location must fail in the VM, not via an OOB partition index. - let (wrong_deadline, wrong_partition) = if partitions.len() > 1 { - let idx = usize::try_from(location.partition)?; - ( - location.deadline, - u64::try_from((idx + 1) % partitions.len())?, - ) - } else { - let mut other = None; - for deadline in 0..WPOST_PERIOD_DEADLINES { - if deadline == location.deadline { - continue; - } - let parts = at - .same(|tsk| StateMinerPartitions::request((miner.actor, deadline, tsk))) - .await?; - if !parts.is_empty() { - other = Some((deadline, 0)); - break; - } - } - other.context( - "deadline has one partition and no other non-empty deadline for the mutation check", - )? - }; - let mut wrong_sectors = BitField::new(); - wrong_sectors.set(sector); - let wrong = Message::builder() - .from(info.worker) - .to(miner.actor) - .method_num(Method::TerminateSectors as u64) - .params(RawBytes::serialize(TerminateSectorsParams { - terminations: vec![TerminationDeclaration { - deadline: wrong_deadline, - partition: wrong_partition, - sectors: wrong_sectors, - }], - })?) - .build(); - let invoc = at - .same(|tsk| StateCall::request((wrong.clone(), tsk))) - .await?; - let receipt = invoc - .msg_rct - .context("no receipt for the wrong-location terminate StateCall")?; - let rejected = invoc - .error - .contains("not all sectors are assigned to the partition") - || invoc.error.contains("can only terminate live sectors") - || invoc.error.contains("failed to find partition"); - ensure!( - receipt.exit_code() == ExitCode::USR_ILLEGAL_ARGUMENT && rejected, - "TerminateSectors at deadline {wrong_deadline} partition {wrong_partition} exits {} \ - with `{}`, expected {} with a sector/partition rejection; sector {sector} is in \ - deadline {} partition {}", - receipt.exit_code(), - invoc.error, - ExitCode::USR_ILLEGAL_ARGUMENT, - location.deadline, - location.partition - ); - miner.api.terminate_sector(sector).await?; let terminate_cid = miner.api.wait_until_terminated(sector).await?; let lookup = poll_until_message_executed(&net.forest, terminate_cid).await?; From 98cb12542e6dbe3aa4e53e9829c1ae823d89a5c3 Mon Sep 17 00:00:00 2001 From: Shashank Date: Sat, 14 Nov 2026 11:23:16 +0530 Subject: [PATCH 4/4] fmt --- src/dev/subcommands/devnet_cmd/miner/api.rs | 89 ++++++++++----------- 1 file changed, 43 insertions(+), 46 deletions(-) diff --git a/src/dev/subcommands/devnet_cmd/miner/api.rs b/src/dev/subcommands/devnet_cmd/miner/api.rs index 97adcb63e8b..17449779499 100644 --- a/src/dev/subcommands/devnet_cmd/miner/api.rs +++ b/src/dev/subcommands/devnet_cmd/miner/api.rs @@ -173,58 +173,55 @@ impl MinerApi { let last_state = AtomicU8::new(u8::MAX); // Lotus `msgTriggerred`: once-only flush latch via the captured CID. let terminate_cid = parking_lot::Mutex::new(None::); - let waited = poll( - &format!("sector {sector} terminate success"), - || async { - 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::Terminating => { - if terminate_cid.lock().is_none() { + let waited = poll(&format!("sector {sector} terminate success"), || async { + 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::Terminating => { + if terminate_cid.lock().is_none() { + let pending = self.terminate_pending().await?; + if pending.is_empty() { + return Ok(None); + } + ensure!( + pending.as_slice() == [sector], + "SectorTerminatePending is {pending:?}, expected exactly [{sector}]" + ); + if let Some(cid) = self.terminate_flush().await? { + eprintln!("terminate message: {cid}"); + *terminate_cid.lock() = Some(cid); let pending = self.terminate_pending().await?; - if pending.is_empty() { - return Ok(None); - } ensure!( - pending.as_slice() == [sector], - "SectorTerminatePending is {pending:?}, expected exactly [{sector}]" + pending.is_empty(), + "SectorTerminatePending not empty after flush: {pending:?}" ); - if let Some(cid) = self.terminate_flush().await? { - eprintln!("terminate message: {cid}"); - *terminate_cid.lock() = Some(cid); - let pending = self.terminate_pending().await?; - ensure!( - pending.is_empty(), - "SectorTerminatePending not empty after flush: {pending:?}" - ); - } } - Ok(None) } - SectorState::TerminateWait - | SectorState::TerminateFinality - | SectorState::Removed => { - let cid = terminate_cid.lock().ok_or_else(|| { - anyhow::anyhow!( - "sector {sector} reached {:?} without a terminate message CID", - status.state - ) - })?; - Ok(Some(cid)) - } - SectorState::TerminateFailed - | SectorState::Removing - | SectorState::RemoveFailed - | SectorState::FailedUnrecoverable => bail!( - "sector {sector} reached {:?}, which is not a successful terminate exit", - status.state - ), - _ => Ok(None), + Ok(None) } - }, - ) + SectorState::TerminateWait + | SectorState::TerminateFinality + | SectorState::Removed => { + let cid = terminate_cid.lock().ok_or_else(|| { + anyhow::anyhow!( + "sector {sector} reached {:?} without a terminate message CID", + status.state + ) + })?; + Ok(Some(cid)) + } + SectorState::TerminateFailed + | SectorState::Removing + | SectorState::RemoveFailed + | SectorState::FailedUnrecoverable => bail!( + "sector {sector} reached {:?}, which is not a successful terminate exit", + status.state + ), + _ => Ok(None), + } + }) .await; let Err(e) = waited else { return waited;