Skip to content

Commit

Permalink
fix(rpc): Filecoin.StateSectorGetInfo (#4236)
Browse files Browse the repository at this point in the history
  • Loading branch information
hanabi1224 authored Apr 18, 2024
1 parent 5f4b5c3 commit 87dffb2
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 51 deletions.
1 change: 0 additions & 1 deletion scripts/tests/api_compare/filter-list
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@
!Filecoin.MpoolGetNonce
!Filecoin.StateCall
!Filecoin.StateListMessages
!Filecoin.StateSectorGetInfo
1 change: 0 additions & 1 deletion scripts/tests/api_compare/filter-list-offline
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
!Filecoin.MpoolGetNonce
!Filecoin.StateCall
!Filecoin.StateListMessages
!Filecoin.StateSectorGetInfo
!Filecoin.EthSyncing
!Filecoin.NetAddrsListen
!Filecoin.NetAgentVersion
Expand Down
2 changes: 1 addition & 1 deletion src/rpc/auth_layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ static ACCESS_MAP: Lazy<HashMap<&str, Access>> = Lazy::new(|| {
access.insert(state::STATE_GET_RANDOMNESS_FROM_BEACON, Access::Read);
access.insert(state::STATE_READ_STATE, Access::Read);
access.insert(state::STATE_CIRCULATING_SUPPLY, Access::Read);
access.insert(state::STATE_SECTOR_GET_INFO, Access::Read);
access.insert(state::StateSectorGetInfo::NAME, Access::Read);
access.insert(state::STATE_LIST_MESSAGES, Access::Read);
access.insert(state::STATE_LIST_MINERS, Access::Read);
access.insert(state::STATE_MINER_SECTOR_COUNT, Access::Read);
Expand Down
74 changes: 47 additions & 27 deletions src/rpc/methods/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ macro_rules! for_each_method {
($callback:ident) => {
$callback!(crate::rpc::state::StateGetBeaconEntry);
$callback!(crate::rpc::state::StateSectorPreCommitInfo);
$callback!(crate::rpc::state::StateSectorGetInfo);
};
}
pub(crate) use for_each_method;
Expand Down Expand Up @@ -81,7 +82,6 @@ pub const STATE_LOOKUP_ID: &str = "Filecoin.StateLookupID";
pub const STATE_ACCOUNT_KEY: &str = "Filecoin.StateAccountKey";
pub const STATE_CIRCULATING_SUPPLY: &str = "Filecoin.StateCirculatingSupply";
pub const STATE_DECODE_PARAMS: &str = "Filecoin.StateDecodeParams";
pub const STATE_SECTOR_GET_INFO: &str = "Filecoin.StateSectorGetInfo";
pub const STATE_SEARCH_MSG: &str = "Filecoin.StateSearchMsg";
pub const STATE_SEARCH_MSG_LIMITED: &str = "Filecoin.StateSearchMsgLimited";
pub const STATE_LIST_MESSAGES: &str = "Filecoin.StateListMessages";
Expand Down Expand Up @@ -994,26 +994,6 @@ pub async fn msig_get_pending<DB: Blockstore + Send + Sync + 'static>(
Ok(LotusJson(txns))
}

/// Get state sector info using sector no
pub async fn state_sector_get_info<DB: Blockstore + Send + Sync + 'static>(
params: Params<'_>,
data: Ctx<DB>,
) -> Result<LotusJson<SectorOnChainInfo>, ServerError> {
let LotusJson((addr, sector_no, ApiTipsetKey(tsk))): LotusJson<(Address, u64, ApiTipsetKey)> =
params.parse()?;

let ts = data.chain_store.load_required_tipset_or_heaviest(&tsk)?;

Ok(LotusJson(
data.state_manager
.get_all_sectors(&addr, &ts)?
.into_iter()
.find(|info| info.sector_number == sector_no)
.map(SectorOnChainInfo::from)
.context(format!("Info for sector number {sector_no} not found"))?,
))
}

pub(in crate::rpc) async fn state_verified_client_status<DB: Blockstore + Send + Sync + 'static>(
params: Params<'_>,
data: Ctx<DB>,
Expand Down Expand Up @@ -1312,12 +1292,7 @@ impl StateSectorPreCommitInfo {
) -> anyhow::Result<Vec<u64>> {
let mut sectors = vec![];
let state_tree = StateTree::new_from_root(store.clone(), tipset.parent_state())?;
let actor = state_tree.get_actor(miner_address)?.with_context(|| {
format!(
"Failed to load actor with addr={miner_address}, state_cid={}",
tipset.parent_state()
)
})?;
let actor = state_tree.get_required_actor(miner_address)?;
let state = miner::State::load(store, actor.code, actor.state)?;
match &state {
miner::State::V8(s) => {
Expand Down Expand Up @@ -1385,3 +1360,48 @@ impl StateSectorPreCommitInfo {
Ok(sectors)
}
}

pub enum StateSectorGetInfo {}

impl RpcMethod<3> for StateSectorGetInfo {
const NAME: &'static str = "Filecoin.StateSectorGetInfo";
const PARAM_NAMES: [&'static str; 3] = ["miner_address", "sector_number", "tipset_key"];
const API_VERSION: ApiVersion = ApiVersion::V0;

type Params = (LotusJson<Address>, LotusJson<u64>, LotusJson<ApiTipsetKey>);
type Ok = SectorOnChainInfo;

async fn handle(
ctx: Ctx<impl Blockstore>,
(LotusJson(miner_address), LotusJson(sector_number), LotusJson(ApiTipsetKey(tsk))): Self::Params,
) -> Result<Self::Ok, ServerError> {
let ts = ctx
.state_manager
.chain_store()
.load_required_tipset_or_heaviest(&tsk)?;
Ok(ctx
.state_manager
.get_all_sectors(&miner_address, &ts)?
.into_iter()
.find(|info| info.sector_number == sector_number)
.map(SectorOnChainInfo::from)
.context(format!("Info for sector number {sector_number} not found"))?)
}
}

impl StateSectorGetInfo {
pub fn get_sectors(
store: &Arc<impl Blockstore>,
miner_address: &Address,
tipset: &Tipset,
) -> anyhow::Result<Vec<u64>> {
let state_tree = StateTree::new_from_root(store.clone(), tipset.parent_state())?;
let actor = state_tree.get_required_actor(miner_address)?;
let state = miner::State::load(store, actor.code, actor.state)?;
Ok(state
.load_sectors(store, None)?
.into_iter()
.map(|s| s.sector_number)
.collect())
}
}
1 change: 0 additions & 1 deletion src/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,6 @@ where
)?;
module.register_async_method(STATE_READ_STATE, state_read_state::<DB>)?;
module.register_async_method(STATE_CIRCULATING_SUPPLY, state_circulating_supply::<DB>)?;
module.register_async_method(STATE_SECTOR_GET_INFO, state_sector_get_info::<DB>)?;
module.register_async_method(
STATE_VERIFIED_CLIENT_STATUS,
state_verified_client_status::<DB>,
Expand Down
21 changes: 14 additions & 7 deletions src/rpc/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,21 +275,22 @@ struct ApiState {

lotus_json_with_self!(ApiState);

#[derive(Clone, Serialize, Deserialize, PartialEq, Eq)]
#[derive(Clone, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "PascalCase")]
pub struct SectorOnChainInfo {
pub sector_number: SectorNumber,

#[schemars(with = "i64")]
/// The seal proof type implies the PoSt proofs
pub seal_proof: RegisteredSealProof,

#[serde(with = "crate::lotus_json")]
#[serde(rename = "SealedCID")]
#[schemars(with = "LotusJson<Cid>")]
#[serde(with = "crate::lotus_json", rename = "SealedCID")]
/// `CommR`
pub sealed_cid: Cid,

#[serde(rename = "DealIDs")]
#[serde(with = "crate::lotus_json")]
#[schemars(with = "LotusJson<Vec<DealID>>")]
#[serde(with = "crate::lotus_json", rename = "DealIDs")]
pub deal_ids: Vec<DealID>,

/// Epoch during which the sector proof was accepted
Expand All @@ -298,35 +299,41 @@ pub struct SectorOnChainInfo {
/// Epoch during which the sector expires
pub expiration: ChainEpoch,

#[schemars(with = "LotusJson<BigInt>")]
#[serde(with = "crate::lotus_json")]
/// Integral of active deals over sector lifetime
pub deal_weight: BigInt,

#[schemars(with = "LotusJson<BigInt>")]
#[serde(with = "crate::lotus_json")]
/// Integral of active verified deals over sector lifetime
pub verified_deal_weight: BigInt,

#[schemars(with = "LotusJson<TokenAmount>")]
#[serde(with = "crate::lotus_json")]
/// Pledge collected to commit this sector
pub initial_pledge: TokenAmount,

#[schemars(with = "LotusJson<TokenAmount>")]
#[serde(with = "crate::lotus_json")]
/// Expected one day projection of reward for sector computed at activation
/// time
pub expected_day_reward: TokenAmount,

#[schemars(with = "LotusJson<TokenAmount>")]
#[serde(with = "crate::lotus_json")]
/// Expected twenty day projection of reward for sector computed at
/// activation time
pub expected_storage_pledge: TokenAmount,

pub replaced_sector_age: ChainEpoch,

#[schemars(with = "LotusJson<TokenAmount>")]
#[serde(with = "crate::lotus_json")]
pub replaced_day_reward: TokenAmount,

#[serde(with = "crate::lotus_json")]
#[serde(rename = "SectorKeyCID")]
#[schemars(with = "LotusJson<Option<Cid>>")]
#[serde(with = "crate::lotus_json", rename = "SectorKeyCID")]
pub sector_key_cid: Option<Cid>,

#[serde(rename = "SimpleQAPower")]
Expand Down
8 changes: 0 additions & 8 deletions src/rpc_client/state_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,6 @@ impl ApiInfo {
RpcRequest::new(STATE_DECODE_PARAMS, (recipient, method_number, params, tsk))
}

pub fn state_sector_get_info_req(
addr: Address,
sector_no: u64,
tsk: ApiTipsetKey,
) -> RpcRequest<SectorOnChainInfo> {
RpcRequest::new(STATE_SECTOR_GET_INFO, (addr, sector_no, tsk))
}

pub fn state_wait_msg_req(msg_cid: Cid, confidence: i64) -> RpcRequest<Option<MessageLookup>> {
// This API is meant to be blocking when the message is missing from the blockstore
RpcRequest::new(STATE_WAIT_MSG, (msg_cid, confidence)).with_timeout(Duration::MAX)
Expand Down
6 changes: 6 additions & 0 deletions src/shim/state_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ where
}
}

/// Get required actor state from an address. Will be resolved to ID address.
pub fn get_required_actor(&self, addr: &Address) -> anyhow::Result<ActorState> {
self.get_actor(addr)?
.with_context(|| format!("Actor not found: addr={addr}"))
}

/// Get actor state from an address. Will be resolved to ID address.
pub fn get_actor(&self, addr: &Address) -> anyhow::Result<Option<ActorState>> {
match self {
Expand Down
17 changes: 12 additions & 5 deletions src/tool/subcommands/api_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,11 +571,6 @@ fn state_tests_with_tipset(shared_tipset: &Tipset) -> Vec<RpcTest> {
shared_tipset.key().into(),
)),
RpcTest::identity(ApiInfo::state_list_miners_req(shared_tipset.key().into())),
RpcTest::identity(ApiInfo::state_sector_get_info_req(
shared_block.miner_address,
101,
shared_tipset.key().into(),
)),
RpcTest::identity(ApiInfo::state_miner_sectors_req(
shared_block.miner_address,
sectors,
Expand Down Expand Up @@ -738,8 +733,20 @@ fn snapshot_tests(store: Arc<ManyCar>, n_tipsets: usize) -> anyhow::Result<Vec<R
block.miner_address,
shared_tipset_key.into(),
)));
for sector in StateSectorGetInfo::get_sectors(&store, &block.miner_address, &tipset)?
.into_iter()
.take(5)
{
tests.push(RpcTest::identity_raw(StateSectorGetInfo::request((
block.miner_address.into(),
sector.into(),
LotusJson(tipset.key().into()),
))?));
}
for sector in
StateSectorPreCommitInfo::get_sectors(&store, &block.miner_address, &tipset)?
.into_iter()
.take(5)
{
tests.push(RpcTest::identity_raw(StateSectorPreCommitInfo::request((
block.miner_address.into(),
Expand Down

0 comments on commit 87dffb2

Please sign in to comment.