Skip to content

Commit

Permalink
feat(rpc): Implement Filecoin.StateMinerAvailableBalance (#4061)
Browse files Browse the repository at this point in the history
Co-authored-by: David Himmelstrup <[email protected]>
  • Loading branch information
hanabi1224 and lemmih authored Mar 15, 2024
1 parent 59ff9a4 commit 36c3514
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,10 @@ where
module.register_async_method(STATE_MINER_SECTOR_COUNT, state_miner_sector_count::<DB>)?;
module.register_async_method(STATE_MINER_FAULTS, state_miner_faults::<DB>)?;
module.register_async_method(STATE_MINER_RECOVERIES, state_miner_recoveries::<DB>)?;
module.register_async_method(
STATE_MINER_AVAILABLE_BALANCE,
state_miner_available_balance::<DB>,
)?;
module.register_async_method(STATE_MINER_POWER, state_miner_power::<DB>)?;
module.register_async_method(STATE_MINER_DEADLINES, state_miner_deadlines::<DB>)?;
module.register_async_method(STATE_LIST_MESSAGES, state_list_messages::<DB>)?;
Expand Down
48 changes: 48 additions & 0 deletions src/rpc/state_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,54 @@ pub async fn state_miner_recoveries<DB: Blockstore + Send + Sync + 'static>(
.map(|r| r.into())
}

pub async fn state_miner_available_balance<DB: Blockstore + Send + Sync + 'static>(
params: Params<'_>,
data: Data<RPCState<DB>>,
) -> Result<LotusJson<TokenAmount>, JsonRpcError> {
let LotusJson((miner_address, ApiTipsetKey(tsk))): LotusJson<(Address, ApiTipsetKey)> =
params.parse()?;

let store = data.chain_store.blockstore();
let ts = data
.state_manager
.chain_store()
.load_required_tipset_or_heaviest(&tsk)?;
let actor = data
.state_manager
.get_actor(&miner_address, *ts.parent_state())?
.ok_or_else(|| anyhow::anyhow!("Miner actor not found"))?;
let state = miner::State::load(store, actor.code, actor.state)?;
let actor_balance: TokenAmount = actor.balance.clone().into();
let (vested, available): (TokenAmount, TokenAmount) = match &state {
miner::State::V13(s) => (
s.check_vested_funds(store, ts.epoch())?.into(),
s.get_available_balance(&actor_balance.into())?.into(),
),
miner::State::V12(s) => (
s.check_vested_funds(store, ts.epoch())?.into(),
s.get_available_balance(&actor_balance.into())?.into(),
),
miner::State::V11(s) => (
s.check_vested_funds(store, ts.epoch())?.into(),
s.get_available_balance(&actor_balance.into())?.into(),
),
miner::State::V10(s) => (
s.check_vested_funds(store, ts.epoch())?.into(),
s.get_available_balance(&actor_balance.into())?.into(),
),
miner::State::V9(s) => (
s.check_vested_funds(store, ts.epoch())?.into(),
s.get_available_balance(&actor_balance.into())?.into(),
),
miner::State::V8(s) => (
s.check_vested_funds(store, ts.epoch())?.into(),
s.get_available_balance(&actor_balance.into())?.into(),
),
};

Ok(LotusJson(vested + available))
}

/// returns the message receipt for the given message
pub async fn state_get_receipt<DB: Blockstore + Send + Sync + 'static>(
params: Params<'_>,
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 @@ -96,6 +96,7 @@ pub static ACCESS_MAP: Lazy<HashMap<&str, Access>> = Lazy::new(|| {
access.insert(state_api::STATE_MINER_POWER, Access::Read);
access.insert(state_api::STATE_MINER_DEADLINES, Access::Read);
access.insert(state_api::STATE_MINER_PROVING_DEADLINE, Access::Read);
access.insert(state_api::STATE_MINER_AVAILABLE_BALANCE, Access::Read);
access.insert(state_api::STATE_GET_RECEIPT, Access::Read);
access.insert(state_api::STATE_WAIT_MSG, Access::Read);
access.insert(state_api::STATE_SEARCH_MSG, Access::Read);
Expand Down Expand Up @@ -378,6 +379,7 @@ pub mod state_api {
pub const STATE_MINER_POWER: &str = "Filecoin.StateMinerPower";
pub const STATE_MINER_DEADLINES: &str = "Filecoin.StateMinerDeadlines";
pub const STATE_MINER_PROVING_DEADLINE: &str = "Filecoin.StateMinerProvingDeadline";
pub const STATE_MINER_AVAILABLE_BALANCE: &str = "Filecoin.StateMinerAvailableBalance";
pub const STATE_GET_RECEIPT: &str = "Filecoin.StateGetReceipt";
pub const STATE_WAIT_MSG: &str = "Filecoin.StateWaitMsg";
pub const STATE_FETCH_ROOT: &str = "Filecoin.StateFetchRoot";
Expand Down
7 changes: 7 additions & 0 deletions src/rpc_client/state_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ impl ApiInfo {
RpcRequest::new(STATE_MINER_PROVING_DEADLINE, (miner, tsk))
}

pub fn state_miner_available_balance_req(
miner: Address,
tsk: ApiTipsetKey,
) -> RpcRequest<TokenAmount> {
RpcRequest::new(STATE_MINER_AVAILABLE_BALANCE, (miner, tsk))
}

pub fn state_get_randomness_from_tickets_req(
tsk: ApiTipsetKey,
personalization: DomainSeparationTag,
Expand Down
6 changes: 6 additions & 0 deletions src/tool/subcommands/api_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,12 @@ fn snapshot_tests(store: &ManyCar, n_tipsets: usize) -> anyhow::Result<Vec<RpcTe
tests.push(RpcTest::identity(
ApiInfo::state_miner_proving_deadline_req(block.miner_address, tipset.key().into()),
));
tests.push(RpcTest::identity(
ApiInfo::state_miner_available_balance_req(
block.miner_address,
tipset.key().into(),
),
));
tests.push(RpcTest::identity(ApiInfo::state_miner_faults_req(
block.miner_address,
tipset.key().into(),
Expand Down

0 comments on commit 36c3514

Please sign in to comment.