Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] StateMinerFaults RPC. #3726

Merged
merged 11 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@

- [#3720](https://github.com/ChainSafe/forest/pull/3720) Implement the
`Filecoin.GetParentMessages` lotus-compatible RPC API.
- [#3726](https://github.com/ChainSafe/forest/pull/3726) Implement the
`Filecoin.StateMinerFaults` lotus-compatible RPC API.
- [#3735](https://github.com/ChainSafe/forest/pull/3735) Implement the
`Filecoin.StateAccountKey` lotus-compatible RPC API.
- [#3727](https://github.com/ChainSafe/forest/pull/3727) Added glif.io calibnet
Expand Down
56 changes: 28 additions & 28 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 12 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,18 @@ derive_more = "0.99.17"
dialoguer = "0.11"
digest = "0.10.5"
directories = "5"
fil_actor_account_state = "7.0.0-rc.5"
fil_actor_cron_state = "7.0.0-rc.5"
fil_actor_datacap_state = "7.0.0-rc.5"
fil_actor_init_state = "7.0.0-rc.5"
fil_actor_interface = "7.0.0-rc.5"
fil_actor_market_state = "7.0.0-rc.5"
fil_actor_miner_state = "7.0.0-rc.5"
fil_actor_power_state = "7.0.0-rc.5"
fil_actor_reward_state = "7.0.0-rc.5"
fil_actor_system_state = "7.0.0-rc.5"
fil_actor_verifreg_state = "7.0.0-rc.5"
fil_actors_shared = "7.0.0-rc.5"
fil_actor_account_state = "7.0.0"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will probably clash with #3739

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might, we'll have several PRs depeding on this version bump.

fil_actor_cron_state = "7.0.0"
fil_actor_datacap_state = "7.0.0"
fil_actor_init_state = "7.0.0"
fil_actor_interface = "7.0.0"
fil_actor_market_state = "7.0.0"
fil_actor_miner_state = "7.0.0"
fil_actor_power_state = "7.0.0"
fil_actor_reward_state = "7.0.0"
fil_actor_system_state = "7.0.0"
fil_actor_verifreg_state = "7.0.0"
fil_actors_shared = { version = "7.0.0", features = ["json"] }
filecoin-proofs-api = { version = "16.0", default-features = false }
flume = "0.11"
fs_extra = "1.2"
Expand Down
15 changes: 15 additions & 0 deletions src/lotus_json/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@
use crate::ipld::{json::IpldJson, Ipld};
use derive_more::From;
use fil_actor_interface::power::Claim;
use fil_actors_shared::fvm_ipld_bitfield::json::BitFieldJson;
use fil_actors_shared::fvm_ipld_bitfield::BitField;
use serde::{de::DeserializeOwned, Deserialize, Deserializer, Serialize, Serializer};
use serde_json::json;
use std::{fmt::Display, str::FromStr};
Expand Down Expand Up @@ -503,3 +505,16 @@ impl HasLotusJson for Ipld {
lotus_json.0
}
}

impl HasLotusJson for BitField {
type LotusJson = BitFieldJson;
fn snapshots() -> Vec<(serde_json::Value, Self)> {
vec![]
}
fn into_lotus_json(self) -> Self::LotusJson {
BitFieldJson(self)
}
fn from_lotus_json(lotus_json: Self::LotusJson) -> Self {
lotus_json.0
}
}
1 change: 1 addition & 0 deletions src/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ where
.with_method(STATE_GET_ACTOR, state_get_actor::<DB>)
.with_method(STATE_MARKET_BALANCE, state_market_balance::<DB>)
.with_method(STATE_MARKET_DEALS, state_market_deals::<DB>)
.with_method(STATE_MINER_FAULTS, state_miner_faults::<DB>)
.with_method(STATE_MINER_POWER, state_miner_power::<DB>)
.with_method(STATE_GET_RECEIPT, state_get_receipt::<DB>)
.with_method(STATE_WAIT_MSG, state_wait_msg::<DB>)
Expand Down
17 changes: 17 additions & 0 deletions src/rpc/state_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use anyhow::Context as _;
use cid::Cid;
use fil_actor_interface::market;
use fil_actor_interface::miner::MinerPower;
use fil_actors_shared::fvm_ipld_bitfield::BitField;
use futures::StreamExt;
use fvm_ipld_blockstore::Blockstore;
use fvm_ipld_encoding::{CborStore, DAG_CBOR};
Expand Down Expand Up @@ -176,6 +177,22 @@ pub(in crate::rpc) async fn state_miner_power<DB: Blockstore + Send + Sync + 'st
.map_err(|e| e.into())
}

/// looks up the miner power of the given address.
pub(in crate::rpc) async fn state_miner_faults<DB: Blockstore + Send + Sync + 'static>(
data: Data<RPCState<DB>>,
Params(LotusJson((address, key))): Params<LotusJson<(Address, TipsetKeys)>>,
) -> Result<LotusJson<BitField>, JsonRpcError> {
let ts = data
.state_manager
.chain_store()
.load_required_tipset(&key)?;

data.state_manager
.miner_faults(&address, &ts)
.map_err(|e| e.into())
.map(|r| r.into())
Comment on lines +190 to +193
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: maybe make it more explicit what types we're converting into.

Suggested change
data.state_manager
.miner_faults(&address, &ts)
.map_err(|e| e.into())
.map(|r| r.into())
Ok(LotusJson(data.state_manager.miner_faults(&address, &ts)?))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It lines up with the other endpoints, I see no reason to be explicit here, while being implicit in other methods.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, we'll still need to do map_err.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need map_err?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, we don't, if we use the ?. I've basically used the same approach I did for the StateMinerPower, which was used in state_market_balance.

I'd say that it would be nice to go with the same approach for all the methods. I've just taken a look and we're pretty much 50/50. In my opinion the best course of action here is to let everybody go with their own flow and once we've finished the RPC compatibility routine - just do a small refactoring to make them look the same.

}

/// returns the message receipt for the given message
pub(in crate::rpc) async fn state_get_receipt<DB: Blockstore + Send + Sync + 'static>(
data: Data<RPCState<DB>>,
Expand Down
2 changes: 2 additions & 0 deletions src/rpc_api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ pub static ACCESS_MAP: Lazy<HashMap<&str, Access>> = Lazy::new(|| {
access.insert(state_api::STATE_GET_ACTOR, Access::Read);
access.insert(state_api::STATE_MARKET_BALANCE, Access::Read);
access.insert(state_api::STATE_MARKET_DEALS, Access::Read);
access.insert(state_api::STATE_MINER_FAULTS, Access::Read);
access.insert(state_api::STATE_MINER_POWER, Access::Read);
access.insert(state_api::STATE_GET_RECEIPT, Access::Read);
access.insert(state_api::STATE_WAIT_MSG, Access::Read);
Expand Down Expand Up @@ -232,6 +233,7 @@ pub mod state_api {
pub const STATE_GET_ACTOR: &str = "Filecoin.StateGetActor";
pub const STATE_MARKET_BALANCE: &str = "Filecoin.StateMarketBalance";
pub const STATE_MARKET_DEALS: &str = "Filecoin.StateMarketDeals";
pub const STATE_MINER_FAULTS: &str = "Filecoin.StateMinerFaults";
pub const STATE_MINER_POWER: &str = "Filecoin.StateMinerPower";
pub const STATE_GET_RECEIPT: &str = "Filecoin.StateGetReceipt";
pub const STATE_WAIT_MSG: &str = "Filecoin.StateWaitMsg";
Expand Down
5 changes: 5 additions & 0 deletions src/rpc_client/state_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::{
};
use cid::Cid;
use fil_actor_interface::miner::MinerPower;
use fil_actors_shared::fvm_ipld_bitfield::BitField;
use fil_actors_shared::v10::runtime::DomainSeparationTag;
use libipld_core::ipld::Ipld;

Expand Down Expand Up @@ -57,6 +58,10 @@ impl ApiInfo {
RpcRequest::new(STATE_NETWORK_NAME, ())
}

pub fn state_miner_faults_req(miner: Address, tsk: TipsetKeys) -> RpcRequest<BitField> {
RpcRequest::new(STATE_MINER_FAULTS, (miner, tsk))
}

pub fn state_miner_power_req(miner: Address, tsk: TipsetKeys) -> RpcRequest<MinerPower> {
RpcRequest::new(STATE_MINER_POWER, (miner, tsk))
}
Expand Down
25 changes: 25 additions & 0 deletions src/state_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ use cid::Cid;
use fil_actor_interface::miner::MinerPower;
use fil_actor_interface::*;
use fil_actors_shared::fvm_ipld_amt::Amtv0 as Amt;
use fil_actors_shared::fvm_ipld_bitfield::BitField;
use fil_actors_shared::v10::runtime::Policy;
use futures::{channel::oneshot, select, FutureExt};
use fvm_ipld_blockstore::Blockstore;
Expand Down Expand Up @@ -979,6 +980,30 @@ where
Ok(out)
}

/// Retrieves miner faults.
pub fn miner_faults(
self: &Arc<Self>,
addr: &Address,
ts: &Arc<Tipset>,
) -> Result<BitField, Error> {
let actor = self
.get_actor(addr, *ts.parent_state())?
.ok_or_else(|| Error::State("Miner actor not found".to_string()))?;

let state = miner::State::load(self.blockstore(), actor.code, actor.state)?;

let mut faults = Vec::new();

state.for_each_deadline(&self.chain_config.policy, self.blockstore(), |_, part| {
Copy link
Contributor

@elmattic elmattic Nov 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer a more explicit name here, like partition.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not partition. It's State.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, you are referring to part?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I'm referring to part.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like part should be deadline and dl should be partition.

part.for_each(self.blockstore(), |_, dl| {
faults.push(dl.faulty_sectors().clone());
Ok(())
})
})?;

Ok(BitField::union(faults.iter()))
}

/// Retrieves miner power.
pub fn miner_power(
self: &Arc<Self>,
Expand Down
4 changes: 4 additions & 0 deletions src/tool/subcommands/api_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,10 @@ fn snapshot_tests(store: &ManyCar, n_tipsets: usize) -> anyhow::Result<Vec<RpcTe
tests.push(RpcTest::identity(ApiInfo::state_miner_power_req(
*block.miner_address(),
tipset.key().clone(),
)));
tests.push(RpcTest::identity(ApiInfo::state_miner_faults_req(
*block.miner_address(),
tipset.key().clone(),
)))
}
tests.push(RpcTest::basic(ApiInfo::state_circulating_supply_req(
Expand Down
Loading