From 0e02830f0ca15f1cfd7f0b229e64e61d1c5ccc73 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Thu, 8 Feb 2024 20:42:19 -0800 Subject: [PATCH] feat: shared: remove a bunch of code - `BLOCK_GAS_LIMIT`: Neither the FVM nor the builtin actors care about the block gas limit. And if they did, it would have to be a runtime parameter passed from the client. - `EPOCH_DURATION_SECONDS`: Different networks may have different block times, this constant is useless. - `math`, `reward`, `smooth`: part of the reward actor calculations, not relevant to the FVM itself (moved to the builtin actors). - `TOTAL_FILECOIN`, `TOTAL_FILECOIN_BASE`: Network parameter, not a concern of the FVM. - `BLOCKS_PER_EPOCH`: protocol parameter, moved to the builtin actors. - `MAX_SECTOR_NUMBER`: moved to the builtin actors. - `Spacetime`, `SectorQuality`: moved to the builtin actors. - `ZERO_ADDRESS`, `*_LOOKBACK`, `ALLOWABLE_CLOCK_DRIFT`, `NetworkParams`, `DefaultNetworkParams`: unused. --- Cargo.lock | 1 - fvm/src/machine/mod.rs | 4 +- fvm/tests/dummy.rs | 2 +- shared/Cargo.toml | 1 - shared/src/clock/mod.rs | 8 -- shared/src/clock/quantize.rs | 53 ------------ shared/src/lib.rs | 61 +------------- shared/src/math.rs | 23 ----- shared/src/reward.rs | 15 ---- shared/src/sector/mod.rs | 10 --- shared/src/smooth/alpha_beta_filter.rs | 111 ------------------------- shared/src/smooth/mod.rs | 9 -- shared/src/smooth/smooth_func.rs | 96 --------------------- 13 files changed, 6 insertions(+), 388 deletions(-) delete mode 100644 shared/src/clock/quantize.rs delete mode 100644 shared/src/math.rs delete mode 100644 shared/src/reward.rs delete mode 100644 shared/src/smooth/alpha_beta_filter.rs delete mode 100644 shared/src/smooth/mod.rs delete mode 100644 shared/src/smooth/smooth_func.rs diff --git a/Cargo.lock b/Cargo.lock index 6e809aa5fb..6574c40f75 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2795,7 +2795,6 @@ dependencies = [ "filecoin-proofs-api", "fvm_ipld_encoding 0.4.0", "fvm_shared 4.1.2", - "lazy_static", "libsecp256k1", "multihash 0.18.1", "num-bigint", diff --git a/fvm/src/machine/mod.rs b/fvm/src/machine/mod.rs index ab851f63d6..dd6cb71288 100644 --- a/fvm/src/machine/mod.rs +++ b/fvm/src/machine/mod.rs @@ -192,7 +192,9 @@ impl NetworkConfig { epoch, timestamp, initial_state_root: initial_state, - circ_supply: fvm_shared::TOTAL_FILECOIN.clone(), + // This is just the default. The previous default of "total FIL supply" was incorrect as + // well, so we might as well be more neutral. + circ_supply: TokenAmount::zero(), tracing: false, } } diff --git a/fvm/tests/dummy.rs b/fvm/tests/dummy.rs index 1faf483c07..f2947ff9d8 100644 --- a/fvm/tests/dummy.rs +++ b/fvm/tests/dummy.rs @@ -200,7 +200,7 @@ pub struct TestData { pub charge_gas_calls: usize, } -const BLOCK_GAS_LIMIT: Gas = Gas::new(fvm_shared::BLOCK_GAS_LIMIT); +const BLOCK_GAS_LIMIT: Gas = Gas::new(10_000_000_000); impl DummyCallManager { pub fn new_stub() -> (Self, Rc>) { diff --git a/shared/Cargo.toml b/shared/Cargo.toml index 6169fe8fab..7d5804db95 100644 --- a/shared/Cargo.toml +++ b/shared/Cargo.toml @@ -12,7 +12,6 @@ blake2b_simd = { workspace = true } thiserror = { workspace = true } num-traits = { workspace = true } num-derive = { workspace = true } -lazy_static = { workspace = true } cid = { workspace = true, features = ["serde-codec", "std"] } multihash = { workspace = true } unsigned-varint = { workspace = true } diff --git a/shared/src/clock/mod.rs b/shared/src/clock/mod.rs index 6f71e1d9ea..791af462f8 100644 --- a/shared/src/clock/mod.rs +++ b/shared/src/clock/mod.rs @@ -2,14 +2,6 @@ // Copyright 2019-2022 ChainSafe Systems // SPDX-License-Identifier: Apache-2.0, MIT -mod quantize; -pub use quantize::*; - -const _ISO_FORMAT: &str = "%FT%X.%.9F"; - -/// Duration of each tipset epoch. -pub const EPOCH_DURATION_SECONDS: i64 = 30; - /// Epoch number of a chain. This acts as a proxy for time within the VM. pub type ChainEpoch = i64; diff --git a/shared/src/clock/quantize.rs b/shared/src/clock/quantize.rs deleted file mode 100644 index 85a7a2fd1b..0000000000 --- a/shared/src/clock/quantize.rs +++ /dev/null @@ -1,53 +0,0 @@ -// Copyright 2021-2023 Protocol Labs -// Copyright 2019-2022 ChainSafe Systems -// SPDX-License-Identifier: Apache-2.0, MIT - -use super::ChainEpoch; - -/// Constant defining the [QuantSpec] which performs no quantization. -pub const NO_QUANTIZATION: QuantSpec = QuantSpec { unit: 1, offset: 0 }; - -/// A spec for epoch quantization. -#[derive(Copy, Clone)] -pub struct QuantSpec { - /// The unit of quantization - pub unit: ChainEpoch, - /// The offset from zero from which to base the modulus - pub offset: ChainEpoch, -} - -impl QuantSpec { - /// Rounds `epoch` to the nearest exact multiple of the quantization unit offset by - /// `offset % unit`, rounding up. - /// - /// This function is equivalent to `unit * ceil(epoch - (offset % unit) / unit) + (offsetSeed % unit)` - /// with the variables/operations over real numbers instead of ints. - /// - /// Precondition: `unit >= 0` - pub fn quantize_up(&self, epoch: ChainEpoch) -> ChainEpoch { - let offset = self.offset % self.unit; - - let remainder = (epoch - offset) % self.unit; - let quotient = (epoch - offset) / self.unit; - - // Don't round if epoch falls on a quantization epoch - if remainder == 0 - // Negative truncating division rounds up - || epoch - offset < 0 - { - self.unit * quotient + offset - } else { - self.unit * (quotient + 1) + offset - } - } - - pub fn quantize_down(&self, epoch: ChainEpoch) -> ChainEpoch { - let next = self.quantize_up(epoch); - // QuantizeDown == QuantizeUp iff epoch is a fixed point of QuantizeUp - if epoch == next { - next - } else { - next - self.unit - } - } -} diff --git a/shared/src/lib.rs b/shared/src/lib.rs index a3cdbee722..68cffc6c67 100644 --- a/shared/src/lib.rs +++ b/shared/src/lib.rs @@ -4,9 +4,6 @@ #![cfg_attr(coverage_nightly, feature(coverage_attribute))] -#[macro_use] -extern crate lazy_static; - use address::Address; use cid::Cid; use clock::ChainEpoch; @@ -22,37 +19,23 @@ pub mod deal; pub mod econ; pub mod error; pub mod event; -pub mod math; pub mod message; pub mod piece; pub mod randomness; pub mod receipt; -pub mod reward; pub mod sector; -pub mod smooth; pub mod state; pub mod sys; pub mod upgrade; pub mod version; use crypto::hash::SupportedHashes; -use econ::TokenAmount; use fvm_ipld_encoding::ipld_block::IpldBlock; use fvm_ipld_encoding::DAG_CBOR; use multihash::Multihash; use crate::error::ExitCode; -lazy_static! { - /// Total Filecoin available to the network. - pub static ref TOTAL_FILECOIN: TokenAmount = TokenAmount::from_whole(TOTAL_FILECOIN_BASE); - - /// Zero address used to avoid allowing it to be used for verification. - /// This is intentionally disallowed because it is an edge case with Filecoin's BLS - /// signature verification. - pub static ref ZERO_ADDRESS: Address = address::Network::Mainnet.parse_address("f3yaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaby2smx7a").unwrap(); -} - /// Codec for raw data. pub const IPLD_RAW: u64 = 0x55; @@ -62,51 +45,11 @@ pub const IDENTITY_HASH: u64 = 0x0; /// The maximum supported CID size. pub const MAX_CID_LEN: usize = 100; -/// Identifier for Actors, includes builtin and initialized actors -pub type ActorID = u64; - /// Default bit width for the hamt in the filecoin protocol. pub const HAMT_BIT_WIDTH: u32 = 5; -/// Total gas limit allowed per block. This is shared across networks. -pub const BLOCK_GAS_LIMIT: u64 = 10_000_000_000; -/// Total Filecoin supply. -pub const TOTAL_FILECOIN_BASE: i64 = 2_000_000_000; - -// Epochs -/// Lookback height for retrieving ticket randomness. -pub const TICKET_RANDOMNESS_LOOKBACK: ChainEpoch = 1; -/// Epochs to look back for verifying PoSt proofs. -pub const WINNING_POST_SECTOR_SET_LOOKBACK: ChainEpoch = 10; - -/// The expected number of block producers in each epoch. -pub const BLOCKS_PER_EPOCH: u64 = 5; - -/// Allowable clock drift in validations. -pub const ALLOWABLE_CLOCK_DRIFT: u64 = 1; - -/// Config trait which handles different network configurations. -pub trait NetworkParams { - /// Total filecoin available to network. - const TOTAL_FILECOIN: i64; - - /// Available rewards for mining. - const MINING_REWARD_TOTAL: i64; - - /// Initial reward actor balance. This function is only called in genesis setting up state. - fn initial_reward_balance() -> TokenAmount { - TokenAmount::from_whole(Self::MINING_REWARD_TOTAL) - } -} - -/// Params for the network. This is now continued on into mainnet and is static across networks. -// * This can be removed in the future if the new testnet is configred at build time -// * but the reason to keep as is, is for an easier transition to runtime configuration. -pub struct DefaultNetworkParams; -impl NetworkParams for DefaultNetworkParams { - const TOTAL_FILECOIN: i64 = TOTAL_FILECOIN_BASE; - const MINING_REWARD_TOTAL: i64 = 1_400_000_000; -} +/// Identifier for Actors, includes builtin and initialized actors +pub type ActorID = u64; /// Method number indicator for calling actor methods. pub type MethodNum = u64; diff --git a/shared/src/math.rs b/shared/src/math.rs deleted file mode 100644 index 7330ef62fc..0000000000 --- a/shared/src/math.rs +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright 2021-2023 Protocol Labs -// Copyright 2019-2022 ChainSafe Systems -// SPDX-License-Identifier: Apache-2.0, MIT - -use crate::bigint::{BigInt, ParseBigIntError}; - -pub const PRECISION: u64 = 128; - -/// polyval evaluates a polynomial given by coefficients `p` in Q.128 format -/// at point `x` in Q.128 format. Output is in Q.128. -/// Coefficients should be ordered from the highest order coefficient to the lowest. -pub fn poly_val(poly: &[BigInt], x: &BigInt) -> BigInt { - let mut res = BigInt::default(); - - for coeff in poly { - res = ((res * x) >> PRECISION) + coeff; - } - res -} - -pub fn poly_parse(coefs: &[&str]) -> Result, ParseBigIntError> { - coefs.iter().map(|c| c.parse()).collect() -} diff --git a/shared/src/reward.rs b/shared/src/reward.rs deleted file mode 100644 index dc09f4da6e..0000000000 --- a/shared/src/reward.rs +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright 2021-2023 Protocol Labs -// SPDX-License-Identifier: Apache-2.0, MIT -use serde_tuple::*; - -use crate::bigint::bigint_ser; -use crate::sector::StoragePower; -use crate::smooth::FilterEstimate; - -#[derive(Clone, Debug, PartialEq, Eq, Serialize_tuple, Deserialize_tuple)] -pub struct ThisEpochRewardReturn { - // * Removed this_epoch_reward in v2 - pub this_epoch_reward_smoothed: FilterEstimate, - #[serde(with = "bigint_ser")] - pub this_epoch_baseline_power: StoragePower, -} diff --git a/shared/src/sector/mod.rs b/shared/src/sector/mod.rs index f55b9bb77f..1d7b118170 100644 --- a/shared/src/sector/mod.rs +++ b/shared/src/sector/mod.rs @@ -21,19 +21,9 @@ use crate::ActorID; /// SectorNumber is a numeric identifier for a sector. It is usually relative to a miner. pub type SectorNumber = u64; -/// The maximum assignable sector number. -/// Raising this would require modifying our AMT implementation. -pub const MAX_SECTOR_NUMBER: SectorNumber = i64::MAX as u64; - /// Unit of storage power (measured in bytes) pub type StoragePower = BigInt; -/// The unit of spacetime committed to the network -pub type Spacetime = BigInt; - -/// Unit of sector quality -pub type SectorQuality = BigInt; - /// SectorSize indicates one of a set of possible sizes in the network. #[derive(Clone, Debug, PartialEq, Eq, Copy, FromPrimitive, Serialize_repr, Deserialize_repr)] #[repr(u64)] diff --git a/shared/src/smooth/alpha_beta_filter.rs b/shared/src/smooth/alpha_beta_filter.rs deleted file mode 100644 index 109b916e6a..0000000000 --- a/shared/src/smooth/alpha_beta_filter.rs +++ /dev/null @@ -1,111 +0,0 @@ -// Copyright 2021-2023 Protocol Labs -// Copyright 2019-2022 ChainSafe Systems -// SPDX-License-Identifier: Apache-2.0, MIT - -use fvm_ipld_encoding::tuple::*; - -use crate::bigint::{bigint_ser, BigInt, Integer}; -use crate::clock::ChainEpoch; -use crate::math::PRECISION; - -#[derive(Default, Serialize_tuple, Deserialize_tuple, Clone, Debug, PartialEq, Eq)] -pub struct FilterEstimate { - #[serde(with = "bigint_ser")] - pub position: BigInt, - #[serde(with = "bigint_ser")] - pub velocity: BigInt, -} - -impl FilterEstimate { - /// Create a new filter estimate given two Q.0 format ints. - pub fn new(position: BigInt, velocity: BigInt) -> Self { - FilterEstimate { - position: position << PRECISION, - velocity: velocity << PRECISION, - } - } - - /// Returns the Q.0 position estimate of the filter - pub fn estimate(&self) -> BigInt { - &self.position >> PRECISION - } - - /// Extrapolate filter "position" delta epochs in the future. - pub fn extrapolate(&self, delta: ChainEpoch) -> BigInt { - let delta_t = BigInt::from(delta) << PRECISION; - let position = &self.position << PRECISION; - (&self.velocity * delta_t) + position - } -} - -pub struct AlphaBetaFilter<'a, 'b, 'f> { - alpha: &'a BigInt, - beta: &'b BigInt, - prev_est: &'f FilterEstimate, -} - -impl<'a, 'b, 'f> AlphaBetaFilter<'a, 'b, 'f> { - pub fn load(prev_est: &'f FilterEstimate, alpha: &'a BigInt, beta: &'b BigInt) -> Self { - Self { - alpha, - beta, - prev_est, - } - } - - pub fn next_estimate(&self, obs: &BigInt, epoch_delta: ChainEpoch) -> FilterEstimate { - let delta_t = BigInt::from(epoch_delta) << PRECISION; - let delta_x = (&delta_t * &self.prev_est.velocity) >> PRECISION; - let mut position = delta_x + &self.prev_est.position; - - let obs = obs << PRECISION; - let residual = obs - &position; - let revision_x = (self.alpha * &residual) >> PRECISION; - position += &revision_x; - - let revision_v = residual * self.beta; - let revision_v = revision_v.div_floor(&delta_t); - let velocity = revision_v + &self.prev_est.velocity; - FilterEstimate { position, velocity } - } -} - -#[cfg(test)] -mod tests { - use super::super::{DEFAULT_ALPHA, DEFAULT_BETA}; - use super::*; - - #[test] - fn rounding() { - // Calculations in this mod are under the assumption division is euclidean and not truncated - let dd: BigInt = BigInt::from(-100); - let dv: BigInt = BigInt::from(3); - assert_eq!(dd.div_floor(&dv), BigInt::from(-34)); - - let dd: BigInt = BigInt::from(200); - let dv: BigInt = BigInt::from(3); - assert_eq!(dd.div_floor(&dv), BigInt::from(66)); - } - - #[test] - fn rounding_issue() { - let fe = FilterEstimate { - position: "12340768897043811082913117521041414330876498465539749838848" - .parse() - .unwrap(), - velocity: "-37396269384748225153347462373739139597454335279104" - .parse() - .unwrap(), - }; - let filter_reward = AlphaBetaFilter::load(&fe, &DEFAULT_ALPHA, &DEFAULT_BETA); - let next = filter_reward.next_estimate(&36266252337034982540u128.into(), 3); - assert_eq!( - next.position.to_string(), - "12340768782449774548722755900999027209659079673176744001536" - ); - assert_eq!( - next.velocity.to_string(), - "-37396515542149801792802995707072472930787668612438" - ); - } -} diff --git a/shared/src/smooth/mod.rs b/shared/src/smooth/mod.rs deleted file mode 100644 index bff45375d6..0000000000 --- a/shared/src/smooth/mod.rs +++ /dev/null @@ -1,9 +0,0 @@ -// Copyright 2021-2023 Protocol Labs -// Copyright 2019-2022 ChainSafe Systems -// SPDX-License-Identifier: Apache-2.0, MIT - -pub use alpha_beta_filter::*; -pub use smooth_func::*; - -mod alpha_beta_filter; -mod smooth_func; diff --git a/shared/src/smooth/smooth_func.rs b/shared/src/smooth/smooth_func.rs deleted file mode 100644 index 827adb46b9..0000000000 --- a/shared/src/smooth/smooth_func.rs +++ /dev/null @@ -1,96 +0,0 @@ -// Copyright 2021-2023 Protocol Labs -// Copyright 2019-2022 ChainSafe Systems -// SPDX-License-Identifier: Apache-2.0, MIT - -use super::alpha_beta_filter::*; -use crate::bigint::{BigInt, Integer}; -use crate::clock::ChainEpoch; -use crate::math::{poly_parse, poly_val, PRECISION}; - -lazy_static! { - pub static ref NUM: Vec = poly_parse(&[ - "261417938209272870992496419296200268025", - "7266615505142943436908456158054846846897", - "32458783941900493142649393804518050491988", - "17078670566130897220338060387082146864806", - "-35150353308172866634071793531642638290419", - "-20351202052858059355702509232125230498980", - "-1563932590352680681114104005183375350999", - ]) - .unwrap(); - pub static ref DENOM: Vec = poly_parse(&[ - "49928077726659937662124949977867279384", - "2508163877009111928787629628566491583994", - "21757751789594546643737445330202599887121", - "53400635271583923415775576342898617051826", - "41248834748603606604000911015235164348839", - "9015227820322455780436733526367238305537", - "340282366920938463463374607431768211456", - ]) - .unwrap(); - pub static ref DEFAULT_ALPHA: BigInt = "314760000000000000000000000000000000".parse().unwrap(); - pub static ref DEFAULT_BETA: BigInt = "96640100000000000000000000000000".parse().unwrap(); - pub static ref LN_2: BigInt = "235865763225513294137944142764154484399".parse().unwrap(); - pub static ref EPSILON: BigInt = "302231454903657293676544".parse().unwrap(); -} - -fn get_bit_len(z: &BigInt) -> u64 { - z.bits() -} - -/// Extrapolate the CumSumRatio given two filters. -pub fn extrapolated_cum_sum_of_ratio( - delta: ChainEpoch, - relative_start: ChainEpoch, - est_num: &FilterEstimate, - est_denom: &FilterEstimate, -) -> BigInt { - let delta_t = BigInt::from(delta) << PRECISION; - let t0 = BigInt::from(relative_start) << PRECISION; - - let pos_1 = &est_num.position; - let pos_2 = &est_denom.position; - let velo_1 = &est_num.velocity; - let velo_2 = &est_denom.velocity; - - let squared_velo_2 = (velo_2 * velo_2) >> PRECISION; - - if squared_velo_2 > *EPSILON { - let mut x2a = ((velo_2 * t0) >> PRECISION) + pos_2; - let mut x2b = ((velo_2 * &delta_t) >> PRECISION) + &x2a; - x2a = ln(&x2a); - x2b = ln(&x2b); - - let m1 = ((&x2b - &x2a) * pos_1 * velo_2) >> PRECISION; - - let m2_l = (&x2a - &x2b) * pos_2; - let m2_r = velo_2 * &delta_t; - let m2: BigInt = ((m2_l + m2_r) * velo_1) >> PRECISION; - - return (m2 + m1).div_floor(&squared_velo_2); - } - - let half_delta = &delta_t >> 1; - let mut x1m: BigInt = velo_1 * (t0 + half_delta); - x1m = (x1m >> PRECISION) + pos_1; - - (x1m * delta_t).div_floor(pos_2) -} - -/// The natural log of Q.128 x. -pub fn ln(z: &BigInt) -> BigInt { - let k: i64 = get_bit_len(z) as i64 - 1 - PRECISION as i64; - - let x: BigInt = if k > 0 { z >> k } else { z << k.abs() }; - - (BigInt::from(k) * &*LN_2) + ln_between_one_and_two(x) -} - -/// The natural log of x, specified in Q.128 format -/// Should only use with 1 <= x <= 2 -/// Output is in Q.128 format. -fn ln_between_one_and_two(x: BigInt) -> BigInt { - let num = poly_val(&NUM, &x) << PRECISION; - let denom = poly_val(&DENOM, &x); - num.div_floor(&denom) -}