Skip to content

Commit 5921189

Browse files
authored
consolidate verifreg and market policy constants into runtime policy (#253)
1 parent d1bf1f9 commit 5921189

File tree

9 files changed

+217
-162
lines changed

9 files changed

+217
-162
lines changed

actors/market/src/lib.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use num_derive::FromPrimitive;
2222
use num_traits::{FromPrimitive, Signed, Zero};
2323

2424
use fil_actors_runtime::cbor::serialize_vec;
25-
use fil_actors_runtime::runtime::{ActorCode, Runtime};
25+
use fil_actors_runtime::runtime::{ActorCode, Policy, Runtime};
2626
use fil_actors_runtime::{
2727
actor_error, cbor, ActorDowncast, ActorError, BURNT_FUNDS_ACTOR_ADDR, CRON_ACTOR_ADDR,
2828
REWARD_ACTOR_ADDR, STORAGE_POWER_ACTOR_ADDR, SYSTEM_ACTOR_ADDR, VERIFIED_REGISTRY_ACTOR_ADDR,
@@ -431,7 +431,8 @@ impl Actor {
431431

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

436437
msm.deals_by_epoch.as_mut().unwrap().put(process_epoch, id).map_err(|e| {
437438
e.downcast_default(
@@ -901,7 +902,7 @@ impl Actor {
901902
}
902903

903904
let (slash_amount, next_epoch, remove_deal) =
904-
msm.update_pending_deal_state(&state, &deal, curr_epoch)?;
905+
msm.update_pending_deal_state(rt.policy(), &state, &deal, curr_epoch)?;
905906
if slash_amount.is_negative() {
906907
return Err(actor_error!(
907908
illegal_state,
@@ -1101,9 +1102,9 @@ where
11011102
Ok((total_deal_space_time, total_verified_space_time, total_deal_space))
11021103
}
11031104

1104-
fn gen_rand_next_epoch(start_epoch: ChainEpoch, deal_id: DealID) -> ChainEpoch {
1105-
let offset = deal_id as i64 % DEAL_UPDATES_INTERVAL;
1106-
let q = QuantSpec { unit: DEAL_UPDATES_INTERVAL, offset: 0 };
1105+
fn gen_rand_next_epoch(policy: &Policy, start_epoch: ChainEpoch, deal_id: DealID) -> ChainEpoch {
1106+
let offset = deal_id as i64 % policy.deal_updates_interval;
1107+
let q = QuantSpec { unit: policy.deal_updates_interval, offset: 0 };
11071108
let prev_day = q.quantize_down(start_epoch);
11081109
if prev_day + offset >= start_epoch {
11091110
return prev_day + offset;
@@ -1205,6 +1206,7 @@ where
12051206
};
12061207

12071208
let (min_provider_collateral, max_provider_collateral) = deal_provider_collateral_bounds(
1209+
rt.policy(),
12081210
proposal.piece_size,
12091211
network_raw_power,
12101212
baseline_power,

actors/market/src/policy.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use std::cmp::max;
55

66
use fil_actors_runtime::network::EPOCHS_IN_DAY;
7+
use fil_actors_runtime::runtime::Policy;
78
use fil_actors_runtime::DealWeight;
89
use fvm_shared::bigint::Integer;
910
use fvm_shared::clock::ChainEpoch;
@@ -15,17 +16,6 @@ use num_traits::Zero;
1516

1617
use super::deal::DealProposal;
1718

18-
/// DealUpdatesInterval is the number of blocks between payouts for deals
19-
pub const DEAL_UPDATES_INTERVAL: i64 = EPOCHS_IN_DAY;
20-
21-
/// Numerator of the percentage of normalized cirulating
22-
/// supply that must be covered by provider collateral
23-
const PROV_COLLATERAL_PERCENT_SUPPLY_NUM: i64 = 1;
24-
25-
/// Denominator of the percentage of normalized cirulating
26-
/// supply that must be covered by provider collateral
27-
const PROV_COLLATERAL_PERCENT_SUPPLY_DENOM: i64 = 100;
28-
2919
/// Maximum length of a deal label.
3020
pub(super) const DEAL_MAX_LABEL_SIZE: usize = 256;
3121

@@ -42,6 +32,7 @@ pub(super) fn deal_price_per_epoch_bounds(
4232
}
4333

4434
pub(super) fn deal_provider_collateral_bounds(
35+
policy: &Policy,
4536
size: PaddedPieceSize,
4637
network_raw_power: &StoragePower,
4738
baseline_power: &StoragePower,
@@ -51,12 +42,12 @@ pub(super) fn deal_provider_collateral_bounds(
5142
// normalizedCirculatingSupply = networkCirculatingSupply * dealPowerShare
5243
// dealPowerShare = dealRawPower / max(BaselinePower(t), NetworkRawPower(t), dealRawPower)
5344

54-
let lock_target_num = network_circulating_supply * PROV_COLLATERAL_PERCENT_SUPPLY_NUM;
45+
let lock_target_num = network_circulating_supply * policy.prov_collateral_percent_supply_num;
5546
let power_share_num = TokenAmount::from(size.0);
5647
let power_share_denom = max(max(network_raw_power, baseline_power), &power_share_num).clone();
5748

5849
let num: TokenAmount = power_share_num * lock_target_num;
59-
let denom: TokenAmount = power_share_denom * PROV_COLLATERAL_PERCENT_SUPPLY_DENOM;
50+
let denom: TokenAmount = power_share_denom * policy.prov_collateral_percent_supply_denom;
6051
((num.div_floor(&denom)), TOTAL_FILECOIN.clone())
6152
}
6253

actors/market/src/state.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use crate::balance_table::BalanceTable;
55
use anyhow::anyhow;
66
use cid::Cid;
7+
use fil_actors_runtime::runtime::Policy;
78
use fil_actors_runtime::{
89
actor_error, make_empty_map, ActorDowncast, ActorError, Array, Set, SetMultimap,
910
};
@@ -21,7 +22,7 @@ use num_traits::{Signed, Zero};
2122

2223
use super::policy::*;
2324
use super::types::*;
24-
use super::{DealProposal, DealState, DEAL_UPDATES_INTERVAL};
25+
use super::{DealProposal, DealState};
2526

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

443445
Ok((TokenAmount::zero(), next, false))
444446
}

actors/runtime/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,7 @@ min-power-2k = []
6363
# Lower the minimum power requirement to 2g
6464
min-power-2g = []
6565

66+
# no collateral for deals (for testing)
67+
no-provider-deal-collateral = []
68+
6669
test_utils = ["hex"]

actors/runtime/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ static NETWORKS: &[(&str, &[&str])] = &[
2525
"small-deals",
2626
"short-precommit",
2727
"min-power-2k",
28+
"no-provider-deal-collateral",
2829
],
2930
),
3031
];

actors/runtime/src/runtime/policy.rs

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use std::collections::HashSet;
22

33
use fvm_shared::clock::ChainEpoch;
4-
use fvm_shared::sector::{RegisteredPoStProof, RegisteredSealProof};
4+
use fvm_shared::sector::{RegisteredPoStProof, RegisteredSealProof, StoragePower};
5+
use num_traits::FromPrimitive;
56

67
// A trait for runtime policy configuration
78
pub trait RuntimePolicy {
@@ -130,12 +131,27 @@ pub struct Policy {
130131

131132
/// Allowed pre commit proof types for new miners
132133
pub valid_pre_commit_proof_type: HashSet<RegisteredSealProof>,
134+
135+
// --- verifreg policy
136+
/// Minimum verified deal size
137+
pub minimum_verified_deal_size: StoragePower,
138+
139+
// --- market policy ---
140+
/// The number of blocks between payouts for deals
141+
pub deal_updates_interval: i64,
142+
143+
/// Numerator of the percentage of normalized cirulating
144+
/// supply that must be covered by provider collateral
145+
pub prov_collateral_percent_supply_num: i64,
146+
147+
/// Denominator of the percentage of normalized cirulating
148+
/// supply that must be covered by provider collateral
149+
pub prov_collateral_percent_supply_denom: i64,
133150
}
134151

135152
impl Default for Policy {
136153
fn default() -> Policy {
137-
#[allow(unused_mut)] // for devnet mutation below
138-
let mut policy = Policy {
154+
Policy {
139155
max_aggregated_sectors: policy_constants::MAX_AGGREGATED_SECTORS,
140156
min_aggregated_sectors: policy_constants::MIN_AGGREGATED_SECTORS,
141157
max_aggregated_proof_size: policy_constants::MAX_AGGREGATED_PROOF_SIZE,
@@ -195,9 +211,18 @@ impl Default for Policy {
195211
#[cfg(feature = "sector-64g")]
196212
RegisteredSealProof::StackedDRG64GiBV1P1,
197213
]),
198-
};
199214

200-
policy
215+
minimum_verified_deal_size: StoragePower::from_i32(
216+
policy_constants::MINIMUM_VERIFIED_DEAL_SIZE,
217+
)
218+
.unwrap(),
219+
220+
deal_updates_interval: policy_constants::DEAL_UPDATES_INTERVAL,
221+
prov_collateral_percent_supply_num:
222+
policy_constants::PROV_COLLATERAL_PERCENT_SUPPLY_NUM,
223+
prov_collateral_percent_supply_denom:
224+
policy_constants::PROV_COLLATERAL_PERCENT_SUPPLY_DENOM,
225+
}
201226
}
202227
}
203228

@@ -325,4 +350,23 @@ mod policy_constants {
325350
/// Epochs after which chain state is final with overwhelming probability (hence the likelihood of two fork of this size is negligible)
326351
/// This is a conservative value that is chosen via simulations of all known attacks.
327352
pub const CHAIN_FINALITY: ChainEpoch = 900;
353+
354+
#[cfg(not(feature = "small-deals"))]
355+
pub const MINIMUM_VERIFIED_DEAL_SIZE: i32 = 1 << 20;
356+
#[cfg(feature = "small-deals")]
357+
pub const MINIMUM_VERIFIED_DEAL_SIZE: i32 = 256;
358+
359+
/// DealUpdatesInterval is the number of blocks between payouts for deals
360+
pub const DEAL_UPDATES_INTERVAL: i64 = EPOCHS_IN_DAY;
361+
362+
/// Numerator of the percentage of normalized cirulating
363+
/// supply that must be covered by provider collateral
364+
#[cfg(not(feature = "no-provider-deal-collateral"))]
365+
pub const PROV_COLLATERAL_PERCENT_SUPPLY_NUM: i64 = 1;
366+
#[cfg(feature = "no-provider-deal-collateral")]
367+
pub const PROV_COLLATERAL_PERCENT_SUPPLY_NUM: i64 = 0;
368+
369+
/// Denominator of the percentage of normalized cirulating
370+
/// supply that must be covered by provider collateral
371+
pub const PROV_COLLATERAL_PERCENT_SUPPLY_DENOM: i64 = 100;
328372
}

actors/verifreg/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ impl Actor {
6868
BS: Blockstore,
6969
RT: Runtime<BS>,
7070
{
71-
if &params.allowance < &MINIMUM_VERIFIED_DEAL_SIZE {
71+
if params.allowance < rt.policy().minimum_verified_deal_size {
7272
return Err(actor_error!(
7373
illegal_argument,
7474
"Allowance {} below minimum deal size for add verifier {}",
@@ -189,7 +189,7 @@ impl Actor {
189189
// The caller will be verified by checking table below
190190
rt.validate_immediate_caller_accept_any()?;
191191

192-
if params.allowance < *MINIMUM_VERIFIED_DEAL_SIZE {
192+
if params.allowance < rt.policy().minimum_verified_deal_size {
193193
return Err(actor_error!(
194194
illegal_argument,
195195
"Allowance {} below MinVerifiedDealSize for add verified client {}",
@@ -326,7 +326,7 @@ impl Actor {
326326
)
327327
})?;
328328

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

375375
let new_vc_cap = vc_cap - &params.deal_size;
376-
if new_vc_cap < *MINIMUM_VERIFIED_DEAL_SIZE {
376+
if new_vc_cap < rt.policy().minimum_verified_deal_size {
377377
// Delete entry if remaining DataCap is less than MinVerifiedDealSize.
378378
// Will be restored later if the deal did not get activated with a ProvenSector.
379379
verified_clients
@@ -419,7 +419,7 @@ impl Actor {
419419
RT: Runtime<BS>,
420420
{
421421
rt.validate_immediate_caller_is(std::iter::once(&*STORAGE_MARKET_ACTOR_ADDR))?;
422-
if params.deal_size < *MINIMUM_VERIFIED_DEAL_SIZE {
422+
if params.deal_size < rt.policy().minimum_verified_deal_size {
423423
return Err(actor_error!(
424424
illegal_argument,
425425
"Below minimum VerifiedDealSize requested in RestoreBytes: {}",

actors/verifreg/src/types.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,8 @@ use fvm_shared::address::Address;
66
use fvm_shared::bigint::bigint_ser;
77
use fvm_shared::crypto::signature::Signature;
88
use fvm_shared::sector::StoragePower;
9-
use lazy_static::lazy_static;
10-
use num_traits::FromPrimitive;
119
use serde::{Deserialize, Serialize};
1210

13-
#[cfg(not(feature = "small-deals"))]
14-
lazy_static! {
15-
pub static ref MINIMUM_VERIFIED_DEAL_SIZE: StoragePower = StoragePower::from_i32(1 << 20).unwrap(); // placeholder
16-
}
17-
18-
#[cfg(feature = "small-deals")]
19-
lazy_static! {
20-
pub static ref MINIMUM_VERIFIED_DEAL_SIZE: StoragePower = StoragePower::from_i32(256).unwrap(); // placeholder
21-
}
22-
2311
#[derive(Clone, Debug, PartialEq, Eq, Serialize_tuple, Deserialize_tuple)]
2412
pub struct VerifierParams {
2513
pub address: Address,

0 commit comments

Comments
 (0)