Skip to content
This repository was archived by the owner on Mar 30, 2023. It is now read-only.

Commit

Permalink
Adjust common functions (#167)
Browse files Browse the repository at this point in the history
* WeightToFee

* darwinia_deposit

* Move darwinia_deposit to primitives

* fix review

* remove unused smallvec
  • Loading branch information
Guantong authored Jan 4, 2023
1 parent 015eed1 commit 2e78ca7
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 128 deletions.
4 changes: 1 addition & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion runtime/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ version = "6.0.0"

[dependencies]
# crates.io
codec = { package = "parity-scale-codec", version = "3.2", default-features = false }
codec = { package = "parity-scale-codec", version = "3.2", default-features = false }
smallvec = { version = "1.10" }

# darwinia
dc-primitives = { default-features = false, path = "../../core/primitives" }
Expand Down
47 changes: 47 additions & 0 deletions runtime/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ pub use bp_darwinia_core as bp_crab;
pub use bp_darwinia_core as bp_darwinia;
pub use bp_darwinia_core as bp_pangolin;

// darwinia
use dc_primitives::*;
// substrate
use frame_support::{
sp_runtime::Perbill,
weights::{
constants::ExtrinsicBaseWeight, WeightToFeeCoefficient, WeightToFeeCoefficients,
WeightToFeePolynomial,
},
};

#[macro_export]
macro_rules! fast_runtime_or_not {
($name:ident, $development_type:ty, $production_type:ty) => {
Expand All @@ -36,3 +47,39 @@ macro_rules! fast_runtime_or_not {
type $name = $production_type;
};
}

/// Handles converting a weight scalar to a fee value, based on the scale and granularity of the
/// node's balance type.
///
/// This should typically create a mapping between the following ranges:
/// - `[0, MAXIMUM_BLOCK_WEIGHT]`
/// - `[Balance::min, Balance::max]`
///
/// Yet, it can be used for any other sort of change to weight-fee. Some examples being:
/// - Setting it to `0` will essentially disable the weight fee.
/// - Setting it to `1` will cause the literal `#[weight = x]` values to be charged.
pub struct WeightToFee;
impl WeightToFeePolynomial for WeightToFee {
type Balance = Balance;

fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {
// in Rococo, extrinsic base weight (smallest non-zero weight) is mapped to 1 MILLIUNIT:
// here, we map to 1/10 of that, or 1/10 MILLIUNIT
let p = MILLIUNIT / 10;
let q = 100 * Balance::from(ExtrinsicBaseWeight::get().ref_time());
smallvec::smallvec![WeightToFeeCoefficient {
degree: 1,
negative: false,
coeff_frac: Perbill::from_rational(p % q, q),
coeff_integer: p / q,
}]
}
}

/// Deposit calculator for Darwinia.
/// 100 UNIT for the base fee, 102.4 UNIT/MB.
pub const fn darwinia_deposit(items: u32, bytes: u32) -> Balance {
// First try.
items as Balance * 100 * UNIT + (bytes as Balance) * 100 * MICROUNIT
// items as Balance * 100 * UNIT + (bytes as Balance) * 100 * MILLIUNIT
}
1 change: 0 additions & 1 deletion runtime/crab/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ substrate-wasm-builder = { git = "https://github.com/paritytech/substrate", bran
# crates.io
codec = { package = "parity-scale-codec", version = "3.2", default-features = false, features = ["derive"] }
scale-info = { version = "2.3", default-features = false, features = ["derive"] }
smallvec = { version = "1.10" }
static_assertions = { version = "1.1" }
# crates.io optional
array-bytes = { version = "6.0", optional = true }
Expand Down
41 changes: 1 addition & 40 deletions runtime/crab/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ use xcm_executor::XcmExecutor;
use frame_support::{
dispatch::DispatchClass,
traits::{Imbalance, OnUnbalanced},
weights::{
ConstantMultiplier, Weight, WeightToFeeCoefficient, WeightToFeeCoefficients,
WeightToFeePolynomial,
},
weights::{ConstantMultiplier, Weight},
};
use frame_system::EnsureRoot;
use sp_core::{crypto::KeyTypeId, OpaqueMetadata, H160, H256, U256};
Expand Down Expand Up @@ -114,14 +111,6 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
state_version: 0,
};

/// Deposit calculator for Darwinia.
/// 100 UNIT for the base fee, 102.4 UNIT/MB.
pub const fn darwinia_deposit(items: u32, bytes: u32) -> Balance {
// First try.
items as Balance * 100 * UNIT + (bytes as Balance) * 100 * MICROUNIT
// items as Balance * 100 * UNIT + (bytes as Balance) * 100 * MILLIUNIT
}

// TODO: move to impl.rs
pub struct DealWithFees<R>(sp_std::marker::PhantomData<R>);
impl<R> frame_support::traits::OnUnbalanced<pallet_balances::NegativeImbalance<R>>
Expand Down Expand Up @@ -218,34 +207,6 @@ impl fp_self_contained::SelfContainedCall for RuntimeCall {
}
}

/// Handles converting a weight scalar to a fee value, based on the scale and granularity of the
/// node's balance type.
///
/// This should typically create a mapping between the following ranges:
/// - `[0, MAXIMUM_BLOCK_WEIGHT]`
/// - `[Balance::min, Balance::max]`
///
/// Yet, it can be used for any other sort of change to weight-fee. Some examples being:
/// - Setting it to `0` will essentially disable the weight fee.
/// - Setting it to `1` will cause the literal `#[weight = x]` values to be charged.
pub struct WeightToFee;
impl WeightToFeePolynomial for WeightToFee {
type Balance = Balance;

fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {
// in Rococo, extrinsic base weight (smallest non-zero weight) is mapped to 1 MILLIUNIT:
// here, we map to 1/10 of that, or 1/10 MILLIUNIT
let p = MILLIUNIT / 10;
let q = 100 * Balance::from(weights::ExtrinsicBaseWeight::get().ref_time());
smallvec::smallvec![WeightToFeeCoefficient {
degree: 1,
negative: false,
coeff_frac: Perbill::from_rational(p % q, q),
coeff_integer: p / q,
}]
}
}

/// The version information used to identify this runtime when compiled natively.
#[cfg(feature = "std")]
pub fn native_version() -> NativeVersion {
Expand Down
1 change: 0 additions & 1 deletion runtime/darwinia/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ substrate-wasm-builder = { git = "https://github.com/paritytech/substrate", bran
# crates.io
codec = { package = "parity-scale-codec", version = "3.2", default-features = false, features = ["derive"] }
scale-info = { version = "2.3", default-features = false, features = ["derive"] }
smallvec = { version = "1.10" }
static_assertions = { version = "1.1" }
# crates.io optional
array-bytes = { version = "6.0", optional = true }
Expand Down
43 changes: 2 additions & 41 deletions runtime/darwinia/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,15 @@ use xcm_executor::XcmExecutor;
use frame_support::{
dispatch::DispatchClass,
traits::{Imbalance, OnUnbalanced},
weights::{
ConstantMultiplier, Weight, WeightToFeeCoefficient, WeightToFeeCoefficients,
WeightToFeePolynomial,
},
weights::{ConstantMultiplier, Weight},
};
use frame_system::EnsureRoot;
use sp_core::{crypto::KeyTypeId, OpaqueMetadata, H160, H256, U256};
use sp_runtime::{
generic,
traits::Block as BlockT,
transaction_validity::{TransactionSource, TransactionValidity},
ApplyExtrinsicResult, MultiAddress, Perbill, Permill,
ApplyExtrinsicResult, Perbill, Permill,
};
use sp_std::prelude::*;
#[cfg(feature = "std")]
Expand Down Expand Up @@ -114,14 +111,6 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
state_version: 0,
};

/// Deposit calculator for Darwinia.
/// 100 UNIT for the base fee, 102.4 UNIT/MB.
pub const fn darwinia_deposit(items: u32, bytes: u32) -> Balance {
// First try.
items as Balance * 100 * UNIT + (bytes as Balance) * 100 * MICROUNIT
// items as Balance * 100 * UNIT + (bytes as Balance) * 100 * MILLIUNIT
}

// TODO: move to impl.rs
pub struct DealWithFees<R>(sp_std::marker::PhantomData<R>);
impl<R> frame_support::traits::OnUnbalanced<pallet_balances::NegativeImbalance<R>>
Expand Down Expand Up @@ -218,34 +207,6 @@ impl fp_self_contained::SelfContainedCall for RuntimeCall {
}
}

/// Handles converting a weight scalar to a fee value, based on the scale and granularity of the
/// node's balance type.
///
/// This should typically create a mapping between the following ranges:
/// - `[0, MAXIMUM_BLOCK_WEIGHT]`
/// - `[Balance::min, Balance::max]`
///
/// Yet, it can be used for any other sort of change to weight-fee. Some examples being:
/// - Setting it to `0` will essentially disable the weight fee.
/// - Setting it to `1` will cause the literal `#[weight = x]` values to be charged.
pub struct WeightToFee;
impl WeightToFeePolynomial for WeightToFee {
type Balance = Balance;

fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {
// in Rococo, extrinsic base weight (smallest non-zero weight) is mapped to 1 MILLIUNIT:
// here, we map to 1/10 of that, or 1/10 MILLIUNIT
let p = MILLIUNIT / 10;
let q = 100 * Balance::from(weights::ExtrinsicBaseWeight::get().ref_time());
smallvec::smallvec![WeightToFeeCoefficient {
degree: 1,
negative: false,
coeff_frac: Perbill::from_rational(p % q, q),
coeff_integer: p / q,
}]
}
}

/// The version information used to identify this runtime when compiled natively.
#[cfg(feature = "std")]
pub fn native_version() -> NativeVersion {
Expand Down
1 change: 0 additions & 1 deletion runtime/pangolin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ substrate-wasm-builder = { git = "https://github.com/paritytech/substrate", bran
# crates.io
codec = { package = "parity-scale-codec", version = "3.2", default-features = false, features = ["derive"] }
scale-info = { version = "2.3", default-features = false, features = ["derive"] }
smallvec = { version = "1.10" }
static_assertions = { version = "1.1" }
# crates.io optional
array-bytes = { version = "6.0", optional = true }
Expand Down
41 changes: 1 addition & 40 deletions runtime/pangolin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ use xcm_executor::XcmExecutor;
use frame_support::{
dispatch::DispatchClass,
traits::{Imbalance, OnUnbalanced},
weights::{
ConstantMultiplier, Weight, WeightToFeeCoefficient, WeightToFeeCoefficients,
WeightToFeePolynomial,
},
weights::{ConstantMultiplier, Weight},
};
use frame_system::EnsureRoot;
use sp_core::{crypto::KeyTypeId, OpaqueMetadata, H160, H256, U256};
Expand Down Expand Up @@ -111,14 +108,6 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
state_version: 0,
};

/// Deposit calculator for Darwinia.
/// 100 UNIT for the base fee, 102.4 UNIT/MB.
pub const fn darwinia_deposit(items: u32, bytes: u32) -> Balance {
// First try.
items as Balance * 100 * UNIT + (bytes as Balance) * 100 * MICROUNIT
// items as Balance * 100 * UNIT + (bytes as Balance) * 100 * MILLIUNIT
}

// TODO: move to impl.rs
pub struct DealWithFees<R>(sp_std::marker::PhantomData<R>);
impl<R> frame_support::traits::OnUnbalanced<pallet_balances::NegativeImbalance<R>>
Expand Down Expand Up @@ -215,34 +204,6 @@ impl fp_self_contained::SelfContainedCall for RuntimeCall {
}
}

/// Handles converting a weight scalar to a fee value, based on the scale and granularity of the
/// node's balance type.
///
/// This should typically create a mapping between the following ranges:
/// - `[0, MAXIMUM_BLOCK_WEIGHT]`
/// - `[Balance::min, Balance::max]`
///
/// Yet, it can be used for any other sort of change to weight-fee. Some examples being:
/// - Setting it to `0` will essentially disable the weight fee.
/// - Setting it to `1` will cause the literal `#[weight = x]` values to be charged.
pub struct WeightToFee;
impl WeightToFeePolynomial for WeightToFee {
type Balance = Balance;

fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {
// in Rococo, extrinsic base weight (smallest non-zero weight) is mapped to 1 MILLIUNIT:
// here, we map to 1/10 of that, or 1/10 MILLIUNIT
let p = MILLIUNIT / 10;
let q = 100 * Balance::from(weights::ExtrinsicBaseWeight::get().ref_time());
smallvec::smallvec![WeightToFeeCoefficient {
degree: 1,
negative: false,
coeff_frac: Perbill::from_rational(p % q, q),
coeff_integer: p / q,
}]
}
}

/// The version information used to identify this runtime when compiled natively.
#[cfg(feature = "std")]
pub fn native_version() -> NativeVersion {
Expand Down

0 comments on commit 2e78ca7

Please sign in to comment.