Skip to content

Commit

Permalink
consolidate verifreg and market policy constants into runtime policy (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
vyzo authored Apr 21, 2022
1 parent d1bf1f9 commit 5921189
Show file tree
Hide file tree
Showing 9 changed files with 217 additions and 162 deletions.
14 changes: 8 additions & 6 deletions actors/market/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use num_derive::FromPrimitive;
use num_traits::{FromPrimitive, Signed, Zero};

use fil_actors_runtime::cbor::serialize_vec;
use fil_actors_runtime::runtime::{ActorCode, Runtime};
use fil_actors_runtime::runtime::{ActorCode, Policy, Runtime};
use fil_actors_runtime::{
actor_error, cbor, ActorDowncast, ActorError, BURNT_FUNDS_ACTOR_ADDR, CRON_ACTOR_ADDR,
REWARD_ACTOR_ADDR, STORAGE_POWER_ACTOR_ADDR, SYSTEM_ACTOR_ADDR, VERIFIED_REGISTRY_ACTOR_ADDR,
Expand Down Expand Up @@ -431,7 +431,8 @@ impl Actor {

// We randomize the first epoch for when the deal will be processed so an attacker isn't able to
// schedule too many deals for the same tick.
let process_epoch = gen_rand_next_epoch(valid_deal.proposal.start_epoch, id);
let process_epoch =
gen_rand_next_epoch(rt.policy(), valid_deal.proposal.start_epoch, id);

msm.deals_by_epoch.as_mut().unwrap().put(process_epoch, id).map_err(|e| {
e.downcast_default(
Expand Down Expand Up @@ -901,7 +902,7 @@ impl Actor {
}

let (slash_amount, next_epoch, remove_deal) =
msm.update_pending_deal_state(&state, &deal, curr_epoch)?;
msm.update_pending_deal_state(rt.policy(), &state, &deal, curr_epoch)?;
if slash_amount.is_negative() {
return Err(actor_error!(
illegal_state,
Expand Down Expand Up @@ -1101,9 +1102,9 @@ where
Ok((total_deal_space_time, total_verified_space_time, total_deal_space))
}

fn gen_rand_next_epoch(start_epoch: ChainEpoch, deal_id: DealID) -> ChainEpoch {
let offset = deal_id as i64 % DEAL_UPDATES_INTERVAL;
let q = QuantSpec { unit: DEAL_UPDATES_INTERVAL, offset: 0 };
fn gen_rand_next_epoch(policy: &Policy, start_epoch: ChainEpoch, deal_id: DealID) -> ChainEpoch {
let offset = deal_id as i64 % policy.deal_updates_interval;
let q = QuantSpec { unit: policy.deal_updates_interval, offset: 0 };
let prev_day = q.quantize_down(start_epoch);
if prev_day + offset >= start_epoch {
return prev_day + offset;
Expand Down Expand Up @@ -1205,6 +1206,7 @@ where
};

let (min_provider_collateral, max_provider_collateral) = deal_provider_collateral_bounds(
rt.policy(),
proposal.piece_size,
network_raw_power,
baseline_power,
Expand Down
17 changes: 4 additions & 13 deletions actors/market/src/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use std::cmp::max;

use fil_actors_runtime::network::EPOCHS_IN_DAY;
use fil_actors_runtime::runtime::Policy;
use fil_actors_runtime::DealWeight;
use fvm_shared::bigint::Integer;
use fvm_shared::clock::ChainEpoch;
Expand All @@ -15,17 +16,6 @@ use num_traits::Zero;

use super::deal::DealProposal;

/// DealUpdatesInterval is the number of blocks between payouts for deals
pub const DEAL_UPDATES_INTERVAL: i64 = EPOCHS_IN_DAY;

/// Numerator of the percentage of normalized cirulating
/// supply that must be covered by provider collateral
const PROV_COLLATERAL_PERCENT_SUPPLY_NUM: i64 = 1;

/// Denominator of the percentage of normalized cirulating
/// supply that must be covered by provider collateral
const PROV_COLLATERAL_PERCENT_SUPPLY_DENOM: i64 = 100;

/// Maximum length of a deal label.
pub(super) const DEAL_MAX_LABEL_SIZE: usize = 256;

Expand All @@ -42,6 +32,7 @@ pub(super) fn deal_price_per_epoch_bounds(
}

pub(super) fn deal_provider_collateral_bounds(
policy: &Policy,
size: PaddedPieceSize,
network_raw_power: &StoragePower,
baseline_power: &StoragePower,
Expand All @@ -51,12 +42,12 @@ pub(super) fn deal_provider_collateral_bounds(
// normalizedCirculatingSupply = networkCirculatingSupply * dealPowerShare
// dealPowerShare = dealRawPower / max(BaselinePower(t), NetworkRawPower(t), dealRawPower)

let lock_target_num = network_circulating_supply * PROV_COLLATERAL_PERCENT_SUPPLY_NUM;
let lock_target_num = network_circulating_supply * policy.prov_collateral_percent_supply_num;
let power_share_num = TokenAmount::from(size.0);
let power_share_denom = max(max(network_raw_power, baseline_power), &power_share_num).clone();

let num: TokenAmount = power_share_num * lock_target_num;
let denom: TokenAmount = power_share_denom * PROV_COLLATERAL_PERCENT_SUPPLY_DENOM;
let denom: TokenAmount = power_share_denom * policy.prov_collateral_percent_supply_denom;
((num.div_floor(&denom)), TOTAL_FILECOIN.clone())
}

Expand Down
6 changes: 4 additions & 2 deletions actors/market/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use crate::balance_table::BalanceTable;
use anyhow::anyhow;
use cid::Cid;
use fil_actors_runtime::runtime::Policy;
use fil_actors_runtime::{
actor_error, make_empty_map, ActorDowncast, ActorError, Array, Set, SetMultimap,
};
Expand All @@ -21,7 +22,7 @@ use num_traits::{Signed, Zero};

use super::policy::*;
use super::types::*;
use super::{DealProposal, DealState, DEAL_UPDATES_INTERVAL};
use super::{DealProposal, DealState};

/// Market actor state
#[derive(Clone, Default, Serialize_tuple, Deserialize_tuple)]
Expand Down Expand Up @@ -344,6 +345,7 @@ where
#[allow(clippy::too_many_arguments)]
pub(super) fn update_pending_deal_state(
&mut self,
policy: &Policy,
state: &DealState,
deal: &DealProposal,
epoch: ChainEpoch,
Expand Down Expand Up @@ -438,7 +440,7 @@ where
// We're explicitly not inspecting the end epoch and may process a deal's expiration late,
// in order to prevent an outsider from loading a cron tick by activating too many deals
// with the same end epoch.
let next = epoch + DEAL_UPDATES_INTERVAL;
let next = epoch + policy.deal_updates_interval;

Ok((TokenAmount::zero(), next, false))
}
Expand Down
3 changes: 3 additions & 0 deletions actors/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,7 @@ min-power-2k = []
# Lower the minimum power requirement to 2g
min-power-2g = []

# no collateral for deals (for testing)
no-provider-deal-collateral = []

test_utils = ["hex"]
1 change: 1 addition & 0 deletions actors/runtime/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ static NETWORKS: &[(&str, &[&str])] = &[
"small-deals",
"short-precommit",
"min-power-2k",
"no-provider-deal-collateral",
],
),
];
Expand Down
54 changes: 49 additions & 5 deletions actors/runtime/src/runtime/policy.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::collections::HashSet;

use fvm_shared::clock::ChainEpoch;
use fvm_shared::sector::{RegisteredPoStProof, RegisteredSealProof};
use fvm_shared::sector::{RegisteredPoStProof, RegisteredSealProof, StoragePower};
use num_traits::FromPrimitive;

// A trait for runtime policy configuration
pub trait RuntimePolicy {
Expand Down Expand Up @@ -130,12 +131,27 @@ pub struct Policy {

/// Allowed pre commit proof types for new miners
pub valid_pre_commit_proof_type: HashSet<RegisteredSealProof>,

// --- verifreg policy
/// Minimum verified deal size
pub minimum_verified_deal_size: StoragePower,

// --- market policy ---
/// The number of blocks between payouts for deals
pub deal_updates_interval: i64,

/// Numerator of the percentage of normalized cirulating
/// supply that must be covered by provider collateral
pub prov_collateral_percent_supply_num: i64,

/// Denominator of the percentage of normalized cirulating
/// supply that must be covered by provider collateral
pub prov_collateral_percent_supply_denom: i64,
}

impl Default for Policy {
fn default() -> Policy {
#[allow(unused_mut)] // for devnet mutation below
let mut policy = Policy {
Policy {
max_aggregated_sectors: policy_constants::MAX_AGGREGATED_SECTORS,
min_aggregated_sectors: policy_constants::MIN_AGGREGATED_SECTORS,
max_aggregated_proof_size: policy_constants::MAX_AGGREGATED_PROOF_SIZE,
Expand Down Expand Up @@ -195,9 +211,18 @@ impl Default for Policy {
#[cfg(feature = "sector-64g")]
RegisteredSealProof::StackedDRG64GiBV1P1,
]),
};

policy
minimum_verified_deal_size: StoragePower::from_i32(
policy_constants::MINIMUM_VERIFIED_DEAL_SIZE,
)
.unwrap(),

deal_updates_interval: policy_constants::DEAL_UPDATES_INTERVAL,
prov_collateral_percent_supply_num:
policy_constants::PROV_COLLATERAL_PERCENT_SUPPLY_NUM,
prov_collateral_percent_supply_denom:
policy_constants::PROV_COLLATERAL_PERCENT_SUPPLY_DENOM,
}
}
}

Expand Down Expand Up @@ -325,4 +350,23 @@ mod policy_constants {
/// Epochs after which chain state is final with overwhelming probability (hence the likelihood of two fork of this size is negligible)
/// This is a conservative value that is chosen via simulations of all known attacks.
pub const CHAIN_FINALITY: ChainEpoch = 900;

#[cfg(not(feature = "small-deals"))]
pub const MINIMUM_VERIFIED_DEAL_SIZE: i32 = 1 << 20;
#[cfg(feature = "small-deals")]
pub const MINIMUM_VERIFIED_DEAL_SIZE: i32 = 256;

/// DealUpdatesInterval is the number of blocks between payouts for deals
pub const DEAL_UPDATES_INTERVAL: i64 = EPOCHS_IN_DAY;

/// Numerator of the percentage of normalized cirulating
/// supply that must be covered by provider collateral
#[cfg(not(feature = "no-provider-deal-collateral"))]
pub const PROV_COLLATERAL_PERCENT_SUPPLY_NUM: i64 = 1;
#[cfg(feature = "no-provider-deal-collateral")]
pub const PROV_COLLATERAL_PERCENT_SUPPLY_NUM: i64 = 0;

/// Denominator of the percentage of normalized cirulating
/// supply that must be covered by provider collateral
pub const PROV_COLLATERAL_PERCENT_SUPPLY_DENOM: i64 = 100;
}
10 changes: 5 additions & 5 deletions actors/verifreg/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl Actor {
BS: Blockstore,
RT: Runtime<BS>,
{
if &params.allowance < &MINIMUM_VERIFIED_DEAL_SIZE {
if params.allowance < rt.policy().minimum_verified_deal_size {
return Err(actor_error!(
illegal_argument,
"Allowance {} below minimum deal size for add verifier {}",
Expand Down Expand Up @@ -189,7 +189,7 @@ impl Actor {
// The caller will be verified by checking table below
rt.validate_immediate_caller_accept_any()?;

if params.allowance < *MINIMUM_VERIFIED_DEAL_SIZE {
if params.allowance < rt.policy().minimum_verified_deal_size {
return Err(actor_error!(
illegal_argument,
"Allowance {} below MinVerifiedDealSize for add verified client {}",
Expand Down Expand Up @@ -326,7 +326,7 @@ impl Actor {
)
})?;

if params.deal_size < *MINIMUM_VERIFIED_DEAL_SIZE {
if params.deal_size < rt.policy().minimum_verified_deal_size {
return Err(actor_error!(
illegal_argument,
"Verified Dealsize {} is below minimum in usedbytes",
Expand Down Expand Up @@ -373,7 +373,7 @@ impl Actor {
};

let new_vc_cap = vc_cap - &params.deal_size;
if new_vc_cap < *MINIMUM_VERIFIED_DEAL_SIZE {
if new_vc_cap < rt.policy().minimum_verified_deal_size {
// Delete entry if remaining DataCap is less than MinVerifiedDealSize.
// Will be restored later if the deal did not get activated with a ProvenSector.
verified_clients
Expand Down Expand Up @@ -419,7 +419,7 @@ impl Actor {
RT: Runtime<BS>,
{
rt.validate_immediate_caller_is(std::iter::once(&*STORAGE_MARKET_ACTOR_ADDR))?;
if params.deal_size < *MINIMUM_VERIFIED_DEAL_SIZE {
if params.deal_size < rt.policy().minimum_verified_deal_size {
return Err(actor_error!(
illegal_argument,
"Below minimum VerifiedDealSize requested in RestoreBytes: {}",
Expand Down
12 changes: 0 additions & 12 deletions actors/verifreg/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,8 @@ use fvm_shared::address::Address;
use fvm_shared::bigint::bigint_ser;
use fvm_shared::crypto::signature::Signature;
use fvm_shared::sector::StoragePower;
use lazy_static::lazy_static;
use num_traits::FromPrimitive;
use serde::{Deserialize, Serialize};

#[cfg(not(feature = "small-deals"))]
lazy_static! {
pub static ref MINIMUM_VERIFIED_DEAL_SIZE: StoragePower = StoragePower::from_i32(1 << 20).unwrap(); // placeholder
}

#[cfg(feature = "small-deals")]
lazy_static! {
pub static ref MINIMUM_VERIFIED_DEAL_SIZE: StoragePower = StoragePower::from_i32(256).unwrap(); // placeholder
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize_tuple, Deserialize_tuple)]
pub struct VerifierParams {
pub address: Address,
Expand Down
Loading

0 comments on commit 5921189

Please sign in to comment.