Skip to content

Commit

Permalink
reify min consensus power to runtime policy
Browse files Browse the repository at this point in the history
  • Loading branch information
vyzo committed Apr 22, 2022
1 parent 64d3b1b commit 3f56d86
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 19 deletions.
5 changes: 3 additions & 2 deletions actors/power/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ impl Actor {
})?;
st.miner_count += 1;

st.update_stats_for_new_miner(window_post_proof_type).map_err(|e| {
st.update_stats_for_new_miner(rt.policy(), window_post_proof_type).map_err(|e| {
actor_error!(
illegal_state,
"failed to update power stats for new miner {}: {}",
Expand Down Expand Up @@ -175,6 +175,7 @@ impl Actor {
)?;

st.add_to_claim(
rt.policy(),
&mut claims,
&miner_addr,
&params.raw_byte_delta,
Expand Down Expand Up @@ -630,7 +631,7 @@ impl Actor {

// Remove power and leave miner frozen
for miner_addr in failed_miner_crons {
if let Err(e) = st.delete_claim(&mut claims, &miner_addr) {
if let Err(e) = st.delete_claim(rt.policy(), &mut claims, &miner_addr) {
error!(
"failed to delete claim for miner {} after\
failing on deferred cron event: {}",
Expand Down
32 changes: 15 additions & 17 deletions actors/power/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::ops::Neg;

use anyhow::{anyhow, Context};
use cid::Cid;
use fil_actors_runtime::runtime::Policy;
use fil_actors_runtime::{
actor_error, make_empty_map, make_map_with_root, make_map_with_root_and_bitwidth,
ActorDowncast, ActorError, Map, Multimap,
Expand Down Expand Up @@ -102,6 +103,7 @@ impl State {
/// Checks power actor state for if miner meets minimum consensus power.
pub fn miner_nominal_power_meets_consensus_minimum<BS: Blockstore>(
&self,
policy: &Policy,
s: &BS,
miner: &Address,
) -> anyhow::Result<bool> {
Expand All @@ -111,7 +113,7 @@ impl State {
get_claim(&claims, miner)?.ok_or_else(|| anyhow!("no claim for actor: {}", miner))?;

let miner_nominal_power = &claim.raw_byte_power;
let miner_min_power = consensus_miner_min_power(claim.window_post_proof_type)
let miner_min_power = consensus_miner_min_power(policy, claim.window_post_proof_type)
.context("could not get miner min power from proof type: {}")?;

if miner_nominal_power >= &miner_min_power {
Expand All @@ -137,6 +139,7 @@ impl State {

pub(super) fn add_to_claim<BS: Blockstore>(
&mut self,
policy: &Policy,
claims: &mut Map<BS, Claim>,
miner: &Address,
power: &StoragePower,
Expand All @@ -154,7 +157,8 @@ impl State {
window_post_proof_type: old_claim.window_post_proof_type,
};

let min_power: StoragePower = consensus_miner_min_power(old_claim.window_post_proof_type)?;
let min_power: StoragePower =
consensus_miner_min_power(policy, old_claim.window_post_proof_type)?;
let prev_below: bool = old_claim.raw_byte_power < min_power;
let still_below: bool = new_claim.raw_byte_power < min_power;

Expand Down Expand Up @@ -247,9 +251,10 @@ impl State {
/// when new added miner starts above the minimum.
pub(super) fn update_stats_for_new_miner(
&mut self,
policy: &Policy,
window_post_proof: RegisteredPoStProof,
) -> anyhow::Result<()> {
let min_power = consensus_miner_min_power(window_post_proof)?;
let min_power = consensus_miner_min_power(policy, window_post_proof)?;

if !min_power.is_positive() {
self.miner_above_min_power_count += 1;
Expand Down Expand Up @@ -299,6 +304,7 @@ impl State {

pub(super) fn delete_claim<BS: Blockstore>(
&mut self,
policy: &Policy,
claims: &mut Map<BS, Claim>,
miner: &Address,
) -> anyhow::Result<()> {
Expand All @@ -311,7 +317,7 @@ impl State {
};

// Subtract from stats to remove power
self.add_to_claim(claims, miner, &rbp.neg(), &qap.neg())
self.add_to_claim(policy, claims, miner, &rbp.neg(), &qap.neg())
.map_err(|e| e.downcast_wrap("failed to subtract miner power before deleting claim"))?;

claims
Expand Down Expand Up @@ -400,7 +406,10 @@ pub struct CronEvent {
impl Cbor for CronEvent {}

/// Returns the minimum storage power required for each seal proof types.
pub fn consensus_miner_min_power(p: RegisteredPoStProof) -> anyhow::Result<StoragePower> {
pub fn consensus_miner_min_power(
policy: &Policy,
p: RegisteredPoStProof,
) -> anyhow::Result<StoragePower> {
use RegisteredPoStProof::*;
match p {
StackedDRGWinning2KiBV1
Expand All @@ -412,18 +421,7 @@ pub fn consensus_miner_min_power(p: RegisteredPoStProof) -> anyhow::Result<Stora
| StackedDRGWindow8MiBV1
| StackedDRGWindow512MiBV1
| StackedDRGWindow32GiBV1
| StackedDRGWindow64GiBV1 => {
let power: u64 = if cfg!(feature = "min-power-2k") {
2 << 10
} else if cfg!(feature = "min-power-2g") {
2 << 30
} else if cfg!(feature = "min-power-32g") {
32 << 30
} else {
10 << 40
};
Ok(StoragePower::from(power))
}
| StackedDRGWindow64GiBV1 => Ok(policy.minimum_consensus_power.clone()),
Invalid(i) => Err(anyhow::anyhow!("unsupported proof type: {}", i)),
}
}
Expand Down
19 changes: 19 additions & 0 deletions actors/runtime/src/runtime/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ pub struct Policy {
/// Denominator of the percentage of normalized cirulating
/// supply that must be covered by provider collateral
pub prov_collateral_percent_supply_denom: i64,

// --- power ---
/// Minimum miner consensus power
pub minimum_consensus_power: StoragePower,
}

impl Default for Policy {
Expand Down Expand Up @@ -222,6 +226,8 @@ impl Default for Policy {
policy_constants::PROV_COLLATERAL_PERCENT_SUPPLY_NUM,
prov_collateral_percent_supply_denom:
policy_constants::PROV_COLLATERAL_PERCENT_SUPPLY_DENOM,

minimum_consensus_power: StoragePower::from(policy_constants::MINIMUM_CONSENSUS_POWER),
}
}
}
Expand Down Expand Up @@ -369,4 +375,17 @@ mod policy_constants {
/// Denominator of the percentage of normalized cirulating
/// supply that must be covered by provider collateral
pub const PROV_COLLATERAL_PERCENT_SUPPLY_DENOM: i64 = 100;

#[cfg(feature = "min-power-2k")]
pub const MINIMUM_CONSENSUS_POWER: i64 = 2 << 10;
#[cfg(feature = "min-power-2g")]
pub const MINIMUM_CONSENSUS_POWER: i64 = 2 << 30;
#[cfg(feature = "min-power-32g")]
pub const MINIMUM_CONSENSUS_POWER: i64 = 32 << 30;
#[cfg(not(any(
feature = "min-power-2k",
feature = "min-power-2g",
feature = "min-power-32g"
)))]
pub const MINIMUM_CONSENSUS_POWER: i64 = 10 << 40;
}

0 comments on commit 3f56d86

Please sign in to comment.