Skip to content

Commit

Permalink
Miner actor: Add exported getters for info and monies
Browse files Browse the repository at this point in the history
  • Loading branch information
arajasek committed Nov 7, 2022
1 parent 29f7987 commit 050427f
Show file tree
Hide file tree
Showing 4 changed files with 384 additions and 1 deletion.
104 changes: 104 additions & 0 deletions actors/miner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ pub enum Method {
// Method numbers derived from FRC-XXXX standards
ChangeBenificiaryExported = frc42_dispatch::method_hash!("ChangeBeneficiary"),
GetBeneficiaryExported = frc42_dispatch::method_hash!("GetBeneficiary"),
GetOwnerExported = frc42_dispatch::method_hash!("GetOwner"),
GetWorkerExported = frc42_dispatch::method_hash!("GetWorker"),
GetControlsExported = frc42_dispatch::method_hash!("GetControls"),
GetSectorSizeExported = frc42_dispatch::method_hash!("GetSectorSize"),
GetPreCommitDepositExported = frc42_dispatch::method_hash!("GetPreCommitDeposit"),
GetInitialPledgeExported = frc42_dispatch::method_hash!("GetInitialPledge"),
GetFeeDebtExported = frc42_dispatch::method_hash!("GetFeeDebt"),
GetLockedFundsExported = frc42_dispatch::method_hash!("GetLockedFunds"),
}

pub const ERR_BALANCE_INVARIANTS_BROKEN: ExitCode = ExitCode::new(1000);
Expand Down Expand Up @@ -205,6 +213,7 @@ impl Actor {
Ok(())
}

/// Returns the "controlling" addresses: the owner, the worker, and all control addresses
fn control_addresses(rt: &mut impl Runtime) -> Result<GetControlAddressesReturn, ActorError> {
rt.validate_immediate_caller_accept_any()?;
let state: State = rt.state()?;
Expand All @@ -216,6 +225,69 @@ impl Actor {
})
}

/// Returns the owner address
fn get_owner(rt: &mut impl Runtime) -> Result<GetOwnerReturn, ActorError> {
rt.validate_immediate_caller_accept_any()?;
let state: State = rt.state()?;
let owner = get_miner_info(rt.store(), &state)?.owner;
Ok(GetOwnerReturn { owner })
}

/// Returns the worker address
fn get_worker(rt: &mut impl Runtime) -> Result<GetWorkerReturn, ActorError> {
rt.validate_immediate_caller_accept_any()?;
let state: State = rt.state()?;
let worker = get_miner_info(rt.store(), &state)?.worker;
Ok(GetWorkerReturn { worker })
}

/// Returns the control addresses
fn get_controls(rt: &mut impl Runtime) -> Result<GetControlsReturn, ActorError> {
rt.validate_immediate_caller_accept_any()?;
let state: State = rt.state()?;
let controls = get_miner_info(rt.store(), &state)?.control_addresses;
Ok(GetControlsReturn { controls })
}

/// Returns the miner's sector size
fn get_sector_size(rt: &mut impl Runtime) -> Result<GetSectorSizeReturn, ActorError> {
rt.validate_immediate_caller_accept_any()?;
let state: State = rt.state()?;
let sector_size = get_miner_info(rt.store(), &state)?.sector_size;
Ok(GetSectorSizeReturn { sector_size })
}

/// Returns the miner's total funds locked as pre_commit_deposit
fn get_pre_commit_deposit(
rt: &mut impl Runtime,
) -> Result<GetPreCommitDepositReturn, ActorError> {
rt.validate_immediate_caller_accept_any()?;
let state: State = rt.state()?;
Ok(GetPreCommitDepositReturn { pre_commit_deposit: state.pre_commit_deposits })
}

/// Returns the sum of the initial pledge requirements of all active sectors of this miner.
fn get_initial_pledge(rt: &mut impl Runtime) -> Result<GetInitialPledgeReturn, ActorError> {
rt.validate_immediate_caller_accept_any()?;
let state: State = rt.state()?;
Ok(GetInitialPledgeReturn { initial_pledge: state.initial_pledge })
}

/// Returns the absolute value of debt this miner owes from unpaid fees.
fn get_fee_debt(rt: &mut impl Runtime) -> Result<GetFeeDebtReturn, ActorError> {
rt.validate_immediate_caller_accept_any()?;
let state: State = rt.state()?;
Ok(GetFeeDebtReturn { fee_debt: state.fee_debt })
}

/// Returns the absolute value of the locked funds in this miner's vesting table.
/// Note that this does NOT include other types of locked funds like precommit deposits.
fn get_locked_funds(rt: &mut impl Runtime) -> Result<GetLockedFundsReturn, ActorError> {
rt.validate_immediate_caller_accept_any()?;
let state: State = rt.state()?;
Ok(GetLockedFundsReturn { locked_funds: state.locked_funds })
}

/// Will ALWAYS overwrite the existing control addresses with the control addresses passed in the params.
/// If an empty addresses vector is passed, the control addresses will be cleared.
/// A worker change will be scheduled if the worker passed in the params is different from the existing worker.
Expand Down Expand Up @@ -5001,6 +5073,38 @@ impl ActorCode for Actor {
Ok(RawBytes::default())
}
None => Err(actor_error!(unhandled_message, "Invalid method")),
Some(Method::GetOwnerExported) => {
let res = Self::get_owner(rt)?;
Ok(RawBytes::serialize(res)?)
}
Some(Method::GetWorkerExported) => {
let res = Self::get_worker(rt)?;
Ok(RawBytes::serialize(res)?)
}
Some(Method::GetControlsExported) => {
let res = Self::get_controls(rt)?;
Ok(RawBytes::serialize(res)?)
}
Some(Method::GetSectorSizeExported) => {
let res = Self::get_sector_size(rt)?;
Ok(RawBytes::serialize(res)?)
}
Some(Method::GetPreCommitDepositExported) => {
let res = Self::get_pre_commit_deposit(rt)?;
Ok(RawBytes::serialize(res)?)
}
Some(Method::GetInitialPledgeExported) => {
let res = Self::get_initial_pledge(rt)?;
Ok(RawBytes::serialize(res)?)
}
Some(Method::GetFeeDebtExported) => {
let res = Self::get_fee_debt(rt)?;
Ok(RawBytes::serialize(res)?)
}
Some(Method::GetLockedFundsExported) => {
let res = Self::get_locked_funds(rt)?;
Ok(RawBytes::serialize(res)?)
}
}
}
}
Expand Down
58 changes: 57 additions & 1 deletion actors/miner/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use fvm_shared::econ::TokenAmount;
use fvm_shared::randomness::Randomness;
use fvm_shared::sector::{
PoStProof, RegisteredPoStProof, RegisteredSealProof, RegisteredUpdateProof, SectorNumber,
StoragePower,
SectorSize, StoragePower,
};
use fvm_shared::smooth::FilterEstimate;

Expand Down Expand Up @@ -482,3 +482,59 @@ pub struct GetBeneficiaryReturn {
}

impl Cbor for GetBeneficiaryReturn {}

#[derive(Serialize_tuple, Deserialize_tuple)]
pub struct GetOwnerReturn {
pub owner: Address,
}

impl Cbor for GetOwnerReturn {}

#[derive(Serialize_tuple, Deserialize_tuple)]
pub struct GetWorkerReturn {
pub worker: Address,
}

impl Cbor for GetWorkerReturn {}

#[derive(Serialize_tuple, Deserialize_tuple)]
pub struct GetControlsReturn {
pub controls: Vec<Address>,
}

impl Cbor for GetControlsReturn {}

#[derive(Serialize_tuple, Deserialize_tuple)]
pub struct GetSectorSizeReturn {
pub sector_size: SectorSize,
}

impl Cbor for GetSectorSizeReturn {}

#[derive(Serialize_tuple, Deserialize_tuple)]
pub struct GetPreCommitDepositReturn {
pub pre_commit_deposit: TokenAmount,
}

impl Cbor for GetPreCommitDepositReturn {}

#[derive(Serialize_tuple, Deserialize_tuple)]
pub struct GetInitialPledgeReturn {
pub initial_pledge: TokenAmount,
}

impl Cbor for GetInitialPledgeReturn {}

#[derive(Serialize_tuple, Deserialize_tuple)]
pub struct GetFeeDebtReturn {
pub fee_debt: TokenAmount,
}

impl Cbor for GetFeeDebtReturn {}

#[derive(Serialize_tuple, Deserialize_tuple)]
pub struct GetLockedFundsReturn {
pub locked_funds: TokenAmount,
}

impl Cbor for GetLockedFundsReturn {}
Loading

0 comments on commit 050427f

Please sign in to comment.