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

feat(rpc): implement Filecoin.StateSectorPartition #4381

Merged
merged 7 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
35 changes: 33 additions & 2 deletions src/rpc/methods/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ use crate::cid_collections::CidHashSet;
use crate::libp2p::NetworkMessage;
use crate::lotus_json::lotus_json_with_self;
use crate::networks::{ChainConfig, NetworkChain};
use crate::shim::actors::miner::PartitionExt as _;
use crate::shim::actors::{market::BalanceTableExt as _, miner::MinerStateExt as _};
use crate::shim::actors::{
market::BalanceTableExt as _,
miner::{MinerStateExt as _, PartitionExt as _},
};
use crate::shim::message::Message;
use crate::shim::piece::PaddedPieceSize;
use crate::shim::state_tree::StateTree;
Expand Down Expand Up @@ -1793,6 +1795,35 @@ impl RpcMethod<3> for StateSectorExpiration {
}
}

pub enum StateSectorPartition {}

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

type Params = (Address, u64, ApiTipsetKey);
type Ok = SectorLocation;

async fn handle(
ctx: Ctx<impl Blockstore>,
(miner_address, sector_number, ApiTipsetKey(tsk)): Self::Params,
) -> Result<Self::Ok, ServerError> {
let store = ctx.store();
let policy = &ctx.state_manager.chain_config().policy;
let ts = ctx
.state_manager
.chain_store()
.load_required_tipset_or_heaviest(&tsk)?;
let actor = ctx
.state_manager
.get_required_actor(&miner_address, *ts.parent_state())?;
let state = miner::State::load(store, actor.code, actor.state)?;
Ok(state.find_sector(store, sector_number, policy)?)
}
}

/// Looks back and returns all messages with a matching to or from address, stopping at the given height.
pub enum StateListMessages {}

Expand Down
8 changes: 8 additions & 0 deletions src/rpc/methods/state/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,11 @@ pub struct SectorExpiration {
pub early: ChainEpoch,
}
lotus_json_with_self!(SectorExpiration);

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "PascalCase")]
pub struct SectorLocation {
pub deadline: u64,
pub partition: u64,
}
lotus_json_with_self!(SectorLocation);
1 change: 1 addition & 0 deletions src/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ macro_rules! for_each_method {
$callback!(crate::rpc::state::StateVerifierStatus);
$callback!(crate::rpc::state::StateGetClaim);
$callback!(crate::rpc::state::StateSectorExpiration);
$callback!(crate::rpc::state::StateSectorPartition);

// sync vertical
$callback!(crate::rpc::sync::SyncCheckBad);
Expand Down
276 changes: 276 additions & 0 deletions src/shim/actors/convert.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,276 @@
// Copyright 2019-2024 ChainSafe Systems
Copy link
Member

Choose a reason for hiding this comment

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

Could you please explain why you copied https://github.com/ChainSafe/fil-actor-states/blob/main/fil_actor_interface/src/convert.rs here? If you intend to move it out of fil-actor-states, let's have a separate PR for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

These methods are not publicly exported.

Copy link
Member

Choose a reason for hiding this comment

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

So right now we have two lengthy implementations doing basically the same thing. I'd rather we have only one.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

Copy link
Member

Choose a reason for hiding this comment

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

Thank you!

// SPDX-License-Identifier: Apache-2.0, MIT

use fil_actors_shared::v10::runtime::Policy as PolicyV10;
use fil_actors_shared::v11::runtime::Policy as PolicyV11;
use fil_actors_shared::v11::runtime::ProofSet as ProofSetV11;
use fil_actors_shared::v12::runtime::Policy as PolicyV12;
use fil_actors_shared::v12::runtime::ProofSet as ProofSetV12;
use fil_actors_shared::v13::runtime::Policy as PolicyV13;
use fil_actors_shared::v9::runtime::Policy as PolicyV9;

pub fn from_policy_v13_to_v9(policy: &PolicyV13) -> PolicyV9 {
let valid_post_proof_type = policy
.valid_post_proof_type
.clone()
.into_inner()
.iter()
.enumerate()
.filter_map(|(i, &p)| if p { Some((i as i64).into()) } else { None })
.collect();

let valid_pre_commit_proof_type = policy
.valid_pre_commit_proof_type
.clone()
.into_inner()
.iter()
.enumerate()
.filter_map(|(i, &p)| if p { Some((i as i64).into()) } else { None })
.collect();

PolicyV9 {
max_aggregated_sectors: policy.max_aggregated_sectors,
min_aggregated_sectors: policy.min_aggregated_sectors,
max_aggregated_proof_size: policy.max_aggregated_proof_size,
max_replica_update_proof_size: policy.max_replica_update_proof_size,
pre_commit_sector_batch_max_size: policy.pre_commit_sector_batch_max_size,
prove_replica_updates_max_size: policy.prove_replica_updates_max_size,
expired_pre_commit_clean_up_delay: policy.expired_pre_commit_clean_up_delay,
wpost_proving_period: policy.wpost_proving_period,
wpost_challenge_window: policy.wpost_challenge_window,
wpost_period_deadlines: policy.wpost_period_deadlines,
wpost_max_chain_commit_age: policy.wpost_max_chain_commit_age,
wpost_dispute_window: policy.wpost_dispute_window,
sectors_max: policy.sectors_max,
max_partitions_per_deadline: policy.max_partitions_per_deadline,
max_control_addresses: policy.max_control_addresses,
max_peer_id_length: policy.max_peer_id_length,
max_multiaddr_data: policy.max_multiaddr_data,
addressed_partitions_max: policy.addressed_partitions_max,
declarations_max: policy.declarations_max,
addressed_sectors_max: policy.addressed_sectors_max,
max_pre_commit_randomness_lookback: policy.max_pre_commit_randomness_lookback,
pre_commit_challenge_delay: policy.pre_commit_challenge_delay,
wpost_challenge_lookback: policy.wpost_challenge_lookback,
fault_declaration_cutoff: policy.fault_declaration_cutoff,
fault_max_age: policy.fault_max_age,
worker_key_change_delay: policy.worker_key_change_delay,
min_sector_expiration: policy.min_sector_expiration,
max_sector_expiration_extension: policy.max_sector_expiration_extension,
deal_limit_denominator: policy.deal_limit_denominator,
consensus_fault_ineligibility_duration: policy.consensus_fault_ineligibility_duration,
new_sectors_per_period_max: policy.new_sectors_per_period_max,
chain_finality: policy.chain_finality,
valid_post_proof_type,
valid_pre_commit_proof_type,
minimum_verified_allocation_size: policy.minimum_verified_allocation_size.clone(),
minimum_verified_allocation_term: policy.minimum_verified_allocation_term,
maximum_verified_allocation_term: policy.maximum_verified_allocation_term,
maximum_verified_allocation_expiration: policy.maximum_verified_allocation_expiration,
end_of_life_claim_drop_period: policy.end_of_life_claim_drop_period,
deal_updates_interval: policy.deal_updates_interval,
prov_collateral_percent_supply_num: policy.prov_collateral_percent_supply_num,
prov_collateral_percent_supply_denom: policy.prov_collateral_percent_supply_denom,
market_default_allocation_term_buffer: policy.market_default_allocation_term_buffer,
minimum_consensus_power: policy.minimum_consensus_power.clone(),
}
}

pub fn from_policy_v13_to_v10(policy: &PolicyV13) -> PolicyV10 {
let valid_post_proof_type = policy
.valid_post_proof_type
.clone()
.into_inner()
.iter()
.enumerate()
.filter_map(|(i, &p)| if p { Some((i as i64).into()) } else { None })
.collect();

let valid_pre_commit_proof_type = policy
.valid_pre_commit_proof_type
.clone()
.into_inner()
.iter()
.enumerate()
.filter_map(|(i, &p)| if p { Some((i as i64).into()) } else { None })
.collect();

PolicyV10 {
max_aggregated_sectors: policy.max_aggregated_sectors,
min_aggregated_sectors: policy.min_aggregated_sectors,
max_aggregated_proof_size: policy.max_aggregated_proof_size,
max_replica_update_proof_size: policy.max_replica_update_proof_size,
pre_commit_sector_batch_max_size: policy.pre_commit_sector_batch_max_size,
prove_replica_updates_max_size: policy.prove_replica_updates_max_size,
expired_pre_commit_clean_up_delay: policy.expired_pre_commit_clean_up_delay,
wpost_proving_period: policy.wpost_proving_period,
wpost_challenge_window: policy.wpost_challenge_window,
wpost_period_deadlines: policy.wpost_period_deadlines,
wpost_max_chain_commit_age: policy.wpost_max_chain_commit_age,
wpost_dispute_window: policy.wpost_dispute_window,
sectors_max: policy.sectors_max,
max_partitions_per_deadline: policy.max_partitions_per_deadline,
max_control_addresses: policy.max_control_addresses,
max_peer_id_length: policy.max_peer_id_length,
max_multiaddr_data: policy.max_multiaddr_data,
addressed_partitions_max: policy.addressed_partitions_max,
declarations_max: policy.declarations_max,
addressed_sectors_max: policy.addressed_sectors_max,
max_pre_commit_randomness_lookback: policy.max_pre_commit_randomness_lookback,
pre_commit_challenge_delay: policy.pre_commit_challenge_delay,
wpost_challenge_lookback: policy.wpost_challenge_lookback,
fault_declaration_cutoff: policy.fault_declaration_cutoff,
fault_max_age: policy.fault_max_age,
worker_key_change_delay: policy.worker_key_change_delay,
min_sector_expiration: policy.min_sector_expiration,
max_sector_expiration_extension: policy.max_sector_expiration_extension,
deal_limit_denominator: policy.deal_limit_denominator,
consensus_fault_ineligibility_duration: policy.consensus_fault_ineligibility_duration,
new_sectors_per_period_max: policy.new_sectors_per_period_max,
chain_finality: policy.chain_finality,
valid_post_proof_type,
valid_pre_commit_proof_type,
minimum_verified_allocation_size: policy.minimum_verified_allocation_size.clone(),
minimum_verified_allocation_term: policy.minimum_verified_allocation_term,
maximum_verified_allocation_term: policy.maximum_verified_allocation_term,
maximum_verified_allocation_expiration: policy.maximum_verified_allocation_expiration,
end_of_life_claim_drop_period: policy.end_of_life_claim_drop_period,
deal_updates_interval: policy.deal_updates_interval,
prov_collateral_percent_supply_num: policy.prov_collateral_percent_supply_num,
prov_collateral_percent_supply_denom: policy.prov_collateral_percent_supply_denom,
market_default_allocation_term_buffer: policy.market_default_allocation_term_buffer,
minimum_consensus_power: policy.minimum_consensus_power.clone(),
}
}

pub fn from_policy_v13_to_v11(policy: &PolicyV13) -> PolicyV11 {
let mut valid_post_proof_type = ProofSetV11::default();
policy
.valid_post_proof_type
.clone()
.into_inner()
.iter()
.for_each(|proof| valid_post_proof_type.insert(*proof));

let mut valid_pre_commit_proof_type = ProofSetV11::default();
policy
.valid_pre_commit_proof_type
.clone()
.into_inner()
.iter()
.for_each(|proof| valid_pre_commit_proof_type.insert(*proof));

PolicyV11 {
max_aggregated_sectors: policy.max_aggregated_sectors,
min_aggregated_sectors: policy.min_aggregated_sectors,
max_aggregated_proof_size: policy.max_aggregated_proof_size,
max_replica_update_proof_size: policy.max_replica_update_proof_size,
pre_commit_sector_batch_max_size: policy.pre_commit_sector_batch_max_size,
prove_replica_updates_max_size: policy.prove_replica_updates_max_size,
expired_pre_commit_clean_up_delay: policy.expired_pre_commit_clean_up_delay,
wpost_proving_period: policy.wpost_proving_period,
wpost_challenge_window: policy.wpost_challenge_window,
wpost_period_deadlines: policy.wpost_period_deadlines,
wpost_max_chain_commit_age: policy.wpost_max_chain_commit_age,
wpost_dispute_window: policy.wpost_dispute_window,
sectors_max: policy.sectors_max,
max_partitions_per_deadline: policy.max_partitions_per_deadline,
max_control_addresses: policy.max_control_addresses,
max_peer_id_length: policy.max_peer_id_length,
max_multiaddr_data: policy.max_multiaddr_data,
addressed_partitions_max: policy.addressed_partitions_max,
declarations_max: policy.declarations_max,
addressed_sectors_max: policy.addressed_sectors_max,
max_pre_commit_randomness_lookback: policy.max_pre_commit_randomness_lookback,
pre_commit_challenge_delay: policy.pre_commit_challenge_delay,
wpost_challenge_lookback: policy.wpost_challenge_lookback,
fault_declaration_cutoff: policy.fault_declaration_cutoff,
fault_max_age: policy.fault_max_age,
worker_key_change_delay: policy.worker_key_change_delay,
min_sector_expiration: policy.min_sector_expiration,
max_sector_expiration_extension: policy.max_sector_expiration_extension,
deal_limit_denominator: policy.deal_limit_denominator,
consensus_fault_ineligibility_duration: policy.consensus_fault_ineligibility_duration,
new_sectors_per_period_max: policy.new_sectors_per_period_max,
chain_finality: policy.chain_finality,
valid_post_proof_type,
valid_pre_commit_proof_type,
minimum_verified_allocation_size: policy.minimum_verified_allocation_size.clone(),
minimum_verified_allocation_term: policy.minimum_verified_allocation_term,
maximum_verified_allocation_term: policy.maximum_verified_allocation_term,
maximum_verified_allocation_expiration: policy.maximum_verified_allocation_expiration,
end_of_life_claim_drop_period: policy.end_of_life_claim_drop_period,
deal_updates_interval: policy.deal_updates_interval,
prov_collateral_percent_supply_num: policy.prov_collateral_percent_supply_num,
prov_collateral_percent_supply_denom: policy.prov_collateral_percent_supply_denom,
market_default_allocation_term_buffer: policy.market_default_allocation_term_buffer,
minimum_consensus_power: policy.minimum_consensus_power.clone(),
}
}

#[allow(dead_code)]
pub fn from_policy_v13_to_v12(policy: &PolicyV13) -> PolicyV12 {
let mut valid_post_proof_type = ProofSetV12::default();
policy
.valid_post_proof_type
.clone()
.into_inner()
.iter()
.for_each(|proof| valid_post_proof_type.insert(*proof));

let mut valid_pre_commit_proof_type = ProofSetV12::default();
policy
.valid_pre_commit_proof_type
.clone()
.into_inner()
.iter()
.for_each(|proof| valid_pre_commit_proof_type.insert(*proof));

PolicyV12 {
max_aggregated_sectors: policy.max_aggregated_sectors,
min_aggregated_sectors: policy.min_aggregated_sectors,
max_aggregated_proof_size: policy.max_aggregated_proof_size,
max_replica_update_proof_size: policy.max_replica_update_proof_size,
pre_commit_sector_batch_max_size: policy.pre_commit_sector_batch_max_size,
prove_replica_updates_max_size: policy.prove_replica_updates_max_size,
expired_pre_commit_clean_up_delay: policy.expired_pre_commit_clean_up_delay,
wpost_proving_period: policy.wpost_proving_period,
wpost_challenge_window: policy.wpost_challenge_window,
wpost_period_deadlines: policy.wpost_period_deadlines,
wpost_max_chain_commit_age: policy.wpost_max_chain_commit_age,
wpost_dispute_window: policy.wpost_dispute_window,
sectors_max: policy.sectors_max,
max_partitions_per_deadline: policy.max_partitions_per_deadline,
max_control_addresses: policy.max_control_addresses,
max_peer_id_length: policy.max_peer_id_length,
max_multiaddr_data: policy.max_multiaddr_data,
addressed_partitions_max: policy.addressed_partitions_max,
declarations_max: policy.declarations_max,
addressed_sectors_max: policy.addressed_sectors_max,
max_pre_commit_randomness_lookback: policy.max_pre_commit_randomness_lookback,
pre_commit_challenge_delay: policy.pre_commit_challenge_delay,
wpost_challenge_lookback: policy.wpost_challenge_lookback,
fault_declaration_cutoff: policy.fault_declaration_cutoff,
fault_max_age: policy.fault_max_age,
worker_key_change_delay: policy.worker_key_change_delay,
min_sector_expiration: policy.min_sector_expiration,
max_sector_expiration_extension: policy.max_sector_expiration_extension,
deal_limit_denominator: policy.deal_limit_denominator,
consensus_fault_ineligibility_duration: policy.consensus_fault_ineligibility_duration,
new_sectors_per_period_max: policy.new_sectors_per_period_max,
chain_finality: policy.chain_finality,
valid_post_proof_type,
valid_pre_commit_proof_type,
minimum_verified_allocation_size: policy.minimum_verified_allocation_size.clone(),
minimum_verified_allocation_term: policy.minimum_verified_allocation_term,
maximum_verified_allocation_term: policy.maximum_verified_allocation_term,
maximum_verified_allocation_expiration: policy.maximum_verified_allocation_expiration,
end_of_life_claim_drop_period: policy.end_of_life_claim_drop_period,
deal_updates_interval: policy.deal_updates_interval,
prov_collateral_percent_supply_num: policy.prov_collateral_percent_supply_num,
prov_collateral_percent_supply_denom: policy.prov_collateral_percent_supply_denom,
market_default_allocation_term_buffer: policy.market_default_allocation_term_buffer,
minimum_consensus_power: policy.minimum_consensus_power.clone(),
posted_partitions_max: policy.posted_partitions_max,
}
}
13 changes: 12 additions & 1 deletion src/shim/actors/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ use fil_actor_interface::miner::State;
use fil_actors_shared::fvm_ipld_bitfield::BitField;
use fvm_ipld_blockstore::Blockstore;

use crate::rpc::types::SectorOnChainInfo;
use crate::{
rpc::{state::SectorLocation, types::SectorOnChainInfo},
shim::sector::SectorNumber,
};

pub trait MinerStateExt {
/// Loads sectors corresponding to the bitfield. If no bitfield is passed
Expand All @@ -19,6 +22,14 @@ pub trait MinerStateExt {
store: &BS,
sectors: Option<&BitField>,
) -> anyhow::Result<Vec<SectorOnChainInfo>>;

/// Returns the deadline and partition index for a sector number.
fn find_sector<BS: Blockstore>(
&self,
store: &BS,
sector_number: SectorNumber,
policy: &fil_actors_shared::v13::runtime::policy::Policy,
) -> anyhow::Result<SectorLocation>;
}

pub trait PartitionExt {
Expand Down
Loading
Loading