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] RPC: Filecoin.StateDealProviderCollateralBounds. #4084

Merged
merged 8 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@

### Added

- [#4084](https://github.com/ChainSafe/forest/pull/4084) Add support for the
`Filecoin.StateDealProviderCollateralBounds` RPC method.

### Changed

### Removed
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 @@ -52,18 +52,18 @@ dialoguer = "0.11"
digest = "0.10.5"
directories = "5"
ethereum-types = "0.14.1"
fil_actor_account_state = { version = "10.1.0" }
fil_actor_cron_state = { version = "10.1.0" }
fil_actor_datacap_state = { version = "10.1.0" }
fil_actor_init_state = { version = "10.1.0" }
fil_actor_interface = { version = "10.1.0" }
fil_actor_market_state = { version = "10.1.0" }
fil_actor_miner_state = { version = "10.1.0" }
fil_actor_power_state = { version = "10.1.0" }
fil_actor_reward_state = { version = "10.1.0" }
fil_actor_system_state = { version = "10.1.0" }
fil_actor_verifreg_state = { version = "10.1.0" }
fil_actors_shared = { version = "10.1.0", features = ["json"] }
fil_actor_account_state = { version = "11.0.0" }
fil_actor_cron_state = { version = "11.0.0" }
fil_actor_datacap_state = { version = "11.0.0" }
fil_actor_init_state = { version = "11.0.0" }
fil_actor_interface = { version = "11.0.0" }
fil_actor_market_state = { version = "11.0.0" }
fil_actor_miner_state = { version = "11.0.0" }
fil_actor_power_state = { version = "11.0.0" }
fil_actor_reward_state = { version = "11.0.0" }
fil_actor_system_state = { version = "11.0.0" }
fil_actor_verifreg_state = { version = "11.0.0" }
fil_actors_shared = { version = "11.0.0", features = ["json"] }
filecoin-proofs-api = { version = "16.0", default-features = false }
flume = "0.11"
frc46_token = "10.0.0"
Expand Down
2 changes: 2 additions & 0 deletions src/lotus_json/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ use derive_more::From;
use fil_actor_interface::{miner::DeadlineInfo, power::Claim};
use fil_actors_shared::fvm_ipld_bitfield::json::BitFieldJson;
use fil_actors_shared::fvm_ipld_bitfield::BitField;
use fvm_shared2::piece::PaddedPieceSize;
use schemars::{gen::SchemaGenerator, schema::Schema, JsonSchema};
use serde::{de::DeserializeOwned, Deserialize, Deserializer, Serialize, Serializer};
#[cfg(test)]
Expand Down Expand Up @@ -475,6 +476,7 @@ lotus_json_with_self!(
std::path::PathBuf,
bool,
DeadlineInfo,
PaddedPieceSize,
);

#[derive(Default, Debug, Serialize, Deserialize)]
Expand Down
4 changes: 4 additions & 0 deletions src/rpc/auth_layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ static ACCESS_MAP: Lazy<HashMap<&str, Access>> = Lazy::new(|| {
);
access.insert(state_api::MSIG_GET_AVAILABLE_BALANCE, Access::Read);
access.insert(state_api::MSIG_GET_PENDING, Access::Read);
access.insert(
state_api::STATE_DEAL_PROVIDER_COLLATERAL_BOUNDS,
Access::Read,
);

// Gas API
access.insert(gas_api::GAS_ESTIMATE_GAS_LIMIT, Access::Read);
Expand Down
5 changes: 5 additions & 0 deletions src/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,11 @@ where
module.register_async_method(STATE_MINER_DEADLINES, state_miner_deadlines::<DB>)?;
module.register_async_method(STATE_LIST_MESSAGES, state_list_messages::<DB>)?;
module.register_async_method(STATE_LIST_MINERS, state_list_miners::<DB>)?;
module.register_async_method(
STATE_DEAL_PROVIDER_COLLATERAL_BOUNDS,
state_deal_provider_collateral_bounds::<DB>,
)?;

module.register_async_method(
STATE_MINER_PROVING_DEADLINE,
state_miner_proving_deadline::<DB>,
Expand Down
70 changes: 68 additions & 2 deletions src/rpc/state_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use fil_actor_interface::miner::DeadlineInfo;
use fil_actor_interface::{
market, miner,
miner::{MinerInfo, MinerPower},
multisig, power,
multisig, power, reward,
};
use fil_actors_shared::fvm_ipld_bitfield::BitField;
use futures::StreamExt;
Expand All @@ -35,7 +35,9 @@ use jsonrpsee::types::{error::ErrorObject, Params};
use libipld_core::ipld::Ipld;
use nonempty::{nonempty, NonEmpty};
use num_bigint::BigInt;
use num_traits::Euclid;
use parking_lot::Mutex;
use std::ops::Mul;
use std::path::PathBuf;
use std::{sync::Arc, time::Duration};
use tokio::task::JoinSet;
Expand Down Expand Up @@ -77,6 +79,8 @@ pub const STATE_MINER_SECTOR_COUNT: &str = "Filecoin.StateMinerSectorCount";
pub const STATE_VERIFIED_CLIENT_STATUS: &str = "Filecoin.StateVerifiedClientStatus";
pub const STATE_VM_CIRCULATING_SUPPLY_INTERNAL: &str = "Filecoin.StateVMCirculatingSupplyInternal";
pub const STATE_MARKET_STORAGE_DEAL: &str = "Filecoin.StateMarketStorageDeal";
pub const STATE_DEAL_PROVIDER_COLLATERAL_BOUNDS: &str =
"Filecoin.StateDealProviderCollateralBounds";
pub const MSIG_GET_AVAILABLE_BALANCE: &str = "Filecoin.MsigGetAvailableBalance";
pub const MSIG_GET_PENDING: &str = "Filecoin.MsigGetPending";

Expand Down Expand Up @@ -1072,10 +1076,72 @@ pub async fn state_market_storage_deal<DB: Blockstore + Send + Sync + 'static>(
.context("Market actor not found")?;
let market_state = market::State::load(store, actor.code, actor.state)?;
let proposals = market_state.proposals(store)?;
let proposal = proposals.get(deal_id)?.ok_or_else(|| anyhow::anyhow!("deal {deal_id} not found - deal may not have completed sealing before deal proposal start epoch, or deal may have been slashed"))?;
let proposal = proposals.get(deal_id)?.ok_or_else(|| anyhow::anyhow!("deal {deal_id} not found - deal may not have completed sealing before deal proposal start epoch, or deal may have been slashed"))?;

let states = market_state.states(store)?;
let state = states.get(deal_id)?.unwrap_or_else(DealState::empty);

Ok(MarketDeal { proposal, state }.into())
}
pub async fn state_deal_provider_collateral_bounds<DB: Blockstore + Send + Sync + 'static>(
params: Params<'_>,
data: Ctx<DB>,
) -> Result<DealCollateralBounds, JsonRpcError> {
let deal_provider_collateral_num = BigInt::from(110);
let deal_provider_collateral_denom = BigInt::from(100);

let LotusJson((size, verified, ApiTipsetKey(tsk))) = params.parse()?;

// This is more eloquent than giving the whole match pattern a type.
let _: bool = verified;

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

let power_actor = state_manager
.get_actor(&Address::POWER_ACTOR, *ts.parent_state())?
.context("Power actor address could not be resolved")?;

let reward_actor = state_manager
.get_actor(&Address::REWARD_ACTOR, *ts.parent_state())?
.context("Power actor address could not be resolved")?;

let store = state_manager.blockstore();

let power_state = power::State::load(store, power_actor.code, power_actor.state)?;
let reward_state = reward::State::load(store, reward_actor.code, reward_actor.state)?;

let genesis_info = GenesisInfo::from_chain_config(state_manager.chain_config());

let supply = genesis_info.get_vm_circulating_supply(
ts.epoch(),
&data.state_manager.blockstore_owned(),
ts.parent_state(),
)?;

let power_claim = power_state.total_power();

let policy = &state_manager.chain_config().policy;

let baseline_power = reward_state.this_epoch_baseline_power();

let (min, max) = reward_state.deal_provider_collateral_bounds(
policy,
size,
&power_claim.raw_byte_power,
baseline_power,
&supply.into(),
);

let min = min
.atto()
.mul(deal_provider_collateral_num)
.div_euclid(&deal_provider_collateral_denom);

Ok(DealCollateralBounds {
max: max.into(),
min: TokenAmount::from_atto(min),
})
}
11 changes: 11 additions & 0 deletions src/rpc/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1069,6 +1069,17 @@ pub struct ApiHeadChange {

lotus_json_with_self!(ApiHeadChange);

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct DealCollateralBounds {
#[serde(with = "crate::lotus_json")]
pub min: TokenAmount,
#[serde(with = "crate::lotus_json")]
pub max: TokenAmount,
}

lotus_json_with_self!(DealCollateralBounds);

#[cfg(test)]
mod tests {
use super::*;
Expand Down
9 changes: 9 additions & 0 deletions src/rpc_client/state_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use cid::Cid;
use fil_actor_interface::miner::{DeadlineInfo, MinerInfo, MinerPower};
use fil_actors_shared::fvm_ipld_bitfield::BitField;
use fil_actors_shared::v10::runtime::DomainSeparationTag;
use fvm_shared2::piece::PaddedPieceSize;
use libipld_core::ipld::Ipld;
use num_bigint::BigInt;

Expand Down Expand Up @@ -215,6 +216,14 @@ impl ApiInfo {
RpcRequest::new(STATE_LIST_MINERS, (tsk,))
}

pub fn state_deal_provider_collateral_bounds_req(
size: PaddedPieceSize,
verified: bool,
tsk: ApiTipsetKey,
) -> RpcRequest<DealCollateralBounds> {
RpcRequest::new(STATE_DEAL_PROVIDER_COLLATERAL_BOUNDS, (size, verified, tsk))
}

pub fn state_list_messages_req(
from_to: MessageFilter,
tsk: ApiTipsetKey,
Expand Down
Loading
Loading