Skip to content

Commit

Permalink
Power actor: Add exported getters for raw power
Browse files Browse the repository at this point in the history
  • Loading branch information
arajasek committed Nov 7, 2022
1 parent 5af4348 commit 29f7987
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 8 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions actors/power/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ crate-type = ["cdylib", "lib"]

[dependencies]
fil_actors_runtime = { version = "10.0.0-alpha.1", path = "../../runtime" }
frc42_dispatch = "1.0.0"
fvm_shared = { version = "2.0.0-alpha.2", default-features = false }
fvm_ipld_hamt = "0.5.1"
num-traits = "0.2.14"
Expand Down
39 changes: 38 additions & 1 deletion actors/power/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use fvm_shared::bigint::bigint_ser::BigIntSer;
use fvm_shared::econ::TokenAmount;
use fvm_shared::error::ExitCode;
use fvm_shared::reward::ThisEpochRewardReturn;
use fvm_shared::sector::SealVerifyInfo;
use fvm_shared::sector::{SealVerifyInfo, StoragePower};
use fvm_shared::{MethodNum, HAMT_BIT_WIDTH, METHOD_CONSTRUCTOR};
use log::{debug, error};
use num_derive::FromPrimitive;
Expand Down Expand Up @@ -61,6 +61,9 @@ pub enum Method {
// OnConsensusFault = 7,
SubmitPoRepForBulkVerify = 8,
CurrentTotalPower = 9,
// Method numbers derived from FRC-XXXX standards
NetworkRawPowerExported = frc42_dispatch::method_hash!("NetworkRawPower"),
MinerRawPowerExported = frc42_dispatch::method_hash!("MinerRawPower"),
}

pub const ERR_TOO_MANY_PROVE_COMMITS: ExitCode = ExitCode::new(32);
Expand Down Expand Up @@ -364,6 +367,32 @@ impl Actor {
})
}

/// Returns the total raw power of the network.
fn network_raw_power(rt: &mut impl Runtime) -> Result<NetworkRawPowerReturn, ActorError> {
rt.validate_immediate_caller_accept_any()?;
let st: State = rt.state()?;

Ok(NetworkRawPowerReturn { raw_byte_power: st.total_raw_byte_power })
}

/// Returns the raw power of the specified miner.
fn miner_raw_power(
rt: &mut impl Runtime,
params: MinerRawPowerParams,
) -> Result<MinerRawPowerReturn, ActorError> {
rt.validate_immediate_caller_accept_any()?;
let st: State = rt.state()?;

let raw_byte_power = st
.get_claim(rt.store(), &params.miner)
.map_err(|e| {
actor_error!(illegal_state, "failed to get claim for miner {}: {}", params.miner, e)
})?
.map_or(StoragePower::zero(), |c| c.raw_byte_power);

Ok(MinerRawPowerReturn { raw_byte_power })
}

fn process_batch_proof_verifies(
rt: &mut impl Runtime,
rewret: &ThisEpochRewardReturn,
Expand Down Expand Up @@ -660,6 +689,14 @@ impl ActorCode for Actor {
let res = Self::current_total_power(rt)?;
Ok(RawBytes::serialize(res)?)
}
Some(Method::NetworkRawPowerExported) => {
let res = Self::network_raw_power(rt)?;
Ok(RawBytes::serialize(res)?)
}
Some(Method::MinerRawPowerExported) => {
let res = Self::miner_raw_power(rt, cbor::deserialize_params(params)?)?;
Ok(RawBytes::serialize(res)?)
}
None => Err(actor_error!(unhandled_message; "Invalid method")),
}
}
Expand Down
24 changes: 24 additions & 0 deletions actors/power/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub struct CreateMinerParams {
pub peer: Vec<u8>,
pub multiaddrs: Vec<BytesDe>,
}

impl Cbor for CreateMinerParams {}

#[derive(Serialize_tuple, Deserialize_tuple)]
Expand Down Expand Up @@ -65,3 +66,26 @@ pub struct CurrentTotalPowerReturn {
pub pledge_collateral: TokenAmount,
pub quality_adj_power_smoothed: FilterEstimate,
}

#[derive(Serialize_tuple, Deserialize_tuple)]
pub struct NetworkRawPowerReturn {
#[serde(with = "bigint_ser")]
pub raw_byte_power: StoragePower,
}

impl Cbor for NetworkRawPowerReturn {}

#[derive(Serialize_tuple, Deserialize_tuple)]
pub struct MinerRawPowerParams {
pub miner: Address,
}

impl Cbor for MinerRawPowerParams {}

#[derive(Serialize_tuple, Deserialize_tuple)]
pub struct MinerRawPowerReturn {
#[serde(with = "bigint_ser")]
pub raw_byte_power: StoragePower,
}

impl Cbor for MinerRawPowerReturn {}
4 changes: 1 addition & 3 deletions actors/power/tests/harness/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,7 @@ impl Harness {

pub fn get_claim(&self, rt: &MockRuntime, miner: &Address) -> Option<Claim> {
let st: State = rt.get_state();
let claims =
make_map_with_root_and_bitwidth(&st.claims, rt.store(), HAMT_BIT_WIDTH).unwrap();
claims.get(&miner.to_bytes()).unwrap().cloned()
st.get_claim(rt.store(), miner).unwrap()
}

pub fn delete_claim(&mut self, rt: &mut MockRuntime, miner: &Address) {
Expand Down
51 changes: 47 additions & 4 deletions actors/power/tests/power_actor_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use fil_actor_power::ext::init::{ExecParams, EXEC_METHOD};
use fil_actor_power::ext::miner::MinerConstructorParams;
use fil_actors_runtime::runtime::builtins::Type;
use fil_actors_runtime::test_utils::{
expect_abort, expect_abort_contains_message, ACCOUNT_ACTOR_CODE_ID, MINER_ACTOR_CODE_ID,
SYSTEM_ACTOR_CODE_ID,
expect_abort, expect_abort_contains_message, make_identity_cid, ACCOUNT_ACTOR_CODE_ID,
MINER_ACTOR_CODE_ID, SYSTEM_ACTOR_CODE_ID,
};
use fil_actors_runtime::{runtime::Policy, CALLER_TYPES_SIGNABLE, INIT_ACTOR_ADDR};
use fvm_ipld_encoding::{BytesDe, RawBytes};
Expand All @@ -17,9 +17,11 @@ use num_traits::Zero;
use std::ops::Neg;

use fil_actor_power::{
consensus_miner_min_power, Actor as PowerActor, CreateMinerParams, EnrollCronEventParams,
Method, State, UpdateClaimedPowerParams, CONSENSUS_MINER_MIN_MINERS,
consensus_miner_min_power, Actor as PowerActor, Actor, CreateMinerParams,
EnrollCronEventParams, Method, MinerRawPowerParams, MinerRawPowerReturn, NetworkRawPowerReturn,
State, UpdateClaimedPowerParams, CONSENSUS_MINER_MIN_MINERS,
};
use fil_actors_runtime::cbor::serialize;

use crate::harness::*;

Expand Down Expand Up @@ -589,6 +591,47 @@ fn claimed_power_is_externally_available() {
h.check_state(&rt);
}

#[test]
fn get_network_and_miner_power() {
let power_unit = &consensus_miner_min_power(
&Policy::default(),
RegisteredPoStProof::StackedDRGWindow32GiBV1,
)
.unwrap();

let (mut h, mut rt) = setup();

h.create_miner_basic(&mut rt, *OWNER, *OWNER, MINER1).unwrap();
h.update_claimed_power(&mut rt, MINER1, power_unit, power_unit);

// set caller to not-builtin
rt.set_caller(make_identity_cid(b"1234"), Address::new_id(1234));

rt.expect_validate_caller_any();
let network_power: NetworkRawPowerReturn = rt
.call::<Actor>(Method::NetworkRawPowerExported as u64, &RawBytes::default())
.unwrap()
.deserialize()
.unwrap();

assert_eq!(power_unit, &network_power.raw_byte_power);

rt.expect_validate_caller_any();
let miner_power: MinerRawPowerReturn = rt
.call::<Actor>(
Method::MinerRawPowerExported as u64,
&serialize(&MinerRawPowerParams { miner: MINER1 }, "serializing MinerRawPowerParams")
.unwrap(),
)
.unwrap()
.deserialize()
.unwrap();

assert_eq!(power_unit, &miner_power.raw_byte_power);

h.check_state(&rt);
}

#[test]
fn given_no_miner_claim_update_pledge_total_should_abort() {
let (mut h, mut rt) = setup();
Expand Down

0 comments on commit 29f7987

Please sign in to comment.