diff --git a/Cargo.lock b/Cargo.lock index 36f391a..935ad26 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6698,7 +6698,6 @@ dependencies = [ "scale-info", "smallvec", "sp-api", - "sp-arithmetic", "sp-block-builder", "sp-consensus-aura", "sp-core", diff --git a/runtime/Cargo.toml b/runtime/Cargo.toml index 49f24be..5c903d4 100644 --- a/runtime/Cargo.toml +++ b/runtime/Cargo.toml @@ -44,7 +44,6 @@ pallet-transaction-payment = { git = "https://github.com/paritytech/substrate", pallet-transaction-payment-rpc-runtime-api = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.42" } pallet-utility = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.42" } sp-api = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.42" } -sp-arithmetic = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.42" } sp-block-builder = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.42" } sp-consensus-aura = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.42" } sp-core = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.42" } @@ -116,7 +115,6 @@ std = [ "polkadot-parachain/std", "polkadot-runtime-common/std", "sp-api/std", - "sp-arithmetic/std", "sp-block-builder/std", "sp-consensus-aura/std", "sp-core/std", diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index a46b768..772ac90 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -114,11 +114,11 @@ pub type Executive = frame_executive::Executive< pub mod fee { use super::{currency::MILLIUNIT, Balance, ExtrinsicBaseWeight}; use frame_support::weights::{ - Weight, WeightToFeeCoefficient, WeightToFeeCoefficients, WeightToFeePolynomial, + FeePolynomial, Weight, WeightToFeeCoefficient, WeightToFeeCoefficients, + WeightToFeePolynomial, }; use smallvec::smallvec; - use sp_arithmetic::traits::{BaseArithmetic, Unsigned}; - use sp_runtime::{Perbill, SaturatedConversion}; + use sp_runtime::Perbill; /// Handles converting a weight scalar to a fee value, based on the scale and granularity of the /// node's balance type. @@ -135,20 +135,14 @@ pub mod fee { type Balance = Balance; fn weight_to_fee(weight: &Weight) -> Self::Balance { - let ref_time = Balance::saturated_from(weight.ref_time()); - let proof_size = Balance::saturated_from(weight.proof_size()); - - let ref_polynomial = RefTimeToFee::polynomial(); - let proof_polynomial = ProofSizeToFee::polynomial(); + let ref_polynomial: FeePolynomial = RefTimeToFee::polynomial().into(); + let proof_polynomial: FeePolynomial = ProofSizeToFee::polynomial().into(); // Get fee amount from ref_time based on the RefTime polynomial - let ref_fee: Balance = - ref_polynomial.iter().fold(0, |acc, term| term.saturating_eval(acc, ref_time)); + let ref_fee: Balance = ref_polynomial.eval(weight.ref_time()); // Get fee amount from proof_size based on the ProofSize polynomial - let proof_fee: Balance = proof_polynomial - .iter() - .fold(0, |acc, term| term.saturating_eval(acc, proof_size)); + let proof_fee: Balance = proof_polynomial.eval(weight.proof_size()); // Take the maximum instead of the sum to charge by the more scarce resource. ref_fee.max(proof_fee) @@ -190,34 +184,6 @@ pub mod fee { }] } } - - // TODO: Refactor out this code to use `FeePolynomial` on versions using polkadot-v0.9.42 and above: - pub trait WeightCoefficientCalc { - fn saturating_eval(&self, result: Balance, x: Balance) -> Balance; - } - - impl WeightCoefficientCalc for WeightToFeeCoefficient - where - Balance: BaseArithmetic + From + Copy + Unsigned + SaturatedConversion, - { - fn saturating_eval(&self, mut result: Balance, x: Balance) -> Balance { - let power = x.saturating_pow(self.degree.into()); - - let frac = self.coeff_frac * power; // Overflow safe since coeff_frac is strictly less than 1. - let integer = self.coeff_integer.saturating_mul(power); - // Do not add them together here to avoid an underflow. - - if self.negative { - result = result.saturating_sub(frac); - result = result.saturating_sub(integer); - } else { - result = result.saturating_add(frac); - result = result.saturating_add(integer); - } - - result - } - } } /// Opaque types. These are used by the CLI to instantiate machinery that don't need to know /// the specifics of the runtime. They can then be made to be agnostic over specific formats