Update Runtime Weights and Fee Calculation (#1064)

* Update Runtime Weights and Fee Calculation

* bump spec

* Order of operations avoids saturation

* Update runtime/polkadot/src/constants.rs

* Use frame consts

* Update Cargo.lock

* Update Cargo.lock

* Update Cargo.lock

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
Shawn Tabrizi
2020-05-05 09:41:41 +02:00
committed by GitHub
parent a7f76b4edd
commit f64c2a940f
8 changed files with 90 additions and 45 deletions
+4 -4
View File
@@ -31,9 +31,11 @@ use primitives::BlockNumber;
use sp_runtime::Perbill; use sp_runtime::Perbill;
use frame_support::{ use frame_support::{
parameter_types, traits::Currency, parameter_types, traits::Currency,
weights::Weight, weights::{Weight, constants::WEIGHT_PER_SECOND},
}; };
pub use frame_support::weights::constants::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight};
#[cfg(feature = "std")] #[cfg(feature = "std")]
pub use staking::StakerStatus; pub use staking::StakerStatus;
#[cfg(any(feature = "std", test))] #[cfg(any(feature = "std", test))]
@@ -50,9 +52,7 @@ pub type NegativeImbalance<T> = <balances::Module<T> as Currency<<T as system::T
parameter_types! { parameter_types! {
pub const BlockHashCount: BlockNumber = 250; pub const BlockHashCount: BlockNumber = 250;
pub const MaximumBlockWeight: Weight = 2_000_000_000_000; pub const MaximumBlockWeight: Weight = 2 * WEIGHT_PER_SECOND;
pub const AvailableBlockRatio: Perbill = Perbill::from_percent(75); pub const AvailableBlockRatio: Perbill = Perbill::from_percent(75);
pub const MaximumBlockLength: u32 = 5 * 1024 * 1024; pub const MaximumBlockLength: u32 = 5 * 1024 * 1024;
pub const ExtrinsicBaseWeight: Weight = 100_000_000; // TODO: Confirm/Update
pub const BlockExecutionWeight: Weight = 1_000_000_000; // TODO: Confirm/Update
} }
+25 -2
View File
@@ -54,6 +54,7 @@ pub mod fee {
use primitives::Balance; use primitives::Balance;
use frame_support::weights::Weight; use frame_support::weights::Weight;
use sp_runtime::traits::Convert; use sp_runtime::traits::Convert;
use runtime_common::ExtrinsicBaseWeight;
/// The block saturation level. Fees will be updates based on this value. /// The block saturation level. Fees will be updates based on this value.
pub const TARGET_BLOCK_FULLNESS: Perbill = Perbill::from_percent(25); pub const TARGET_BLOCK_FULLNESS: Perbill = Perbill::from_percent(25);
@@ -71,8 +72,30 @@ pub mod fee {
pub struct WeightToFee; pub struct WeightToFee;
impl Convert<Weight, Balance> for WeightToFee { impl Convert<Weight, Balance> for WeightToFee {
fn convert(x: Weight) -> Balance { fn convert(x: Weight) -> Balance {
// in Kusama a weight of 10_000_000 (smallest non-zero weight) is mapped to 1/10 CENT: // in Kusama, extrinsic base weight (smallest non-zero weight) is mapped to 1/10 CENT:
Balance::from(x).saturating_mul(super::currency::CENTS / (10 * 10_000_000)) Balance::from(x).saturating_mul(super::currency::CENTS / 10) / Balance::from(ExtrinsicBaseWeight::get())
} }
} }
} }
#[cfg(test)]
mod tests {
use sp_runtime::traits::Convert;
use runtime_common::{MaximumBlockWeight, ExtrinsicBaseWeight};
use super::fee::WeightToFee;
use super::currency::{CENTS, DOLLARS};
#[test]
// This function tests that the fee for `MaximumBlockWeight` of weight is correct
fn full_block_fee_is_correct() {
// A full block should cost 16 DOLLARS
assert_eq!(WeightToFee::convert(MaximumBlockWeight::get()), 16 * DOLLARS)
}
#[test]
// This function tests that the fee for `ExtrinsicBaseWeight` of weight is correct
fn extrinsic_base_fee_is_correct() {
// `ExtrinsicBaseWeight` should cost 1/10 of a CENT
assert_eq!(WeightToFee::convert(ExtrinsicBaseWeight::get()), CENTS / 10)
}
}
+3 -11
View File
@@ -30,7 +30,7 @@ use primitives::{
use runtime_common::{attestations, claims, parachains, registrar, slots, use runtime_common::{attestations, claims, parachains, registrar, slots,
impls::{CurrencyToVoteHandler, TargetedFeeAdjustment, ToAuthor}, impls::{CurrencyToVoteHandler, TargetedFeeAdjustment, ToAuthor},
NegativeImbalance, BlockHashCount, MaximumBlockWeight, AvailableBlockRatio, NegativeImbalance, BlockHashCount, MaximumBlockWeight, AvailableBlockRatio,
MaximumBlockLength, BlockExecutionWeight, ExtrinsicBaseWeight, MaximumBlockLength, BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight,
}; };
use sp_runtime::{ use sp_runtime::{
create_runtime_str, generic, impl_opaque_keys, ModuleId, create_runtime_str, generic, impl_opaque_keys, ModuleId,
@@ -55,7 +55,6 @@ use sp_staking::SessionIndex;
use frame_support::{ use frame_support::{
parameter_types, construct_runtime, debug, parameter_types, construct_runtime, debug,
traits::{KeyOwnerProofSystem, SplitTwoWays, Randomness, LockIdentifier}, traits::{KeyOwnerProofSystem, SplitTwoWays, Randomness, LockIdentifier},
weights::RuntimeDbWeight,
}; };
use im_online::sr25519::AuthorityId as ImOnlineId; use im_online::sr25519::AuthorityId as ImOnlineId;
use authority_discovery_primitives::AuthorityId as AuthorityDiscoveryId; use authority_discovery_primitives::AuthorityId as AuthorityDiscoveryId;
@@ -84,7 +83,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("kusama"), spec_name: create_runtime_str!("kusama"),
impl_name: create_runtime_str!("parity-kusama"), impl_name: create_runtime_str!("parity-kusama"),
authoring_version: 2, authoring_version: 2,
spec_version: 1061, spec_version: 1062,
impl_version: 0, impl_version: 0,
apis: RUNTIME_API_VERSIONS, apis: RUNTIME_API_VERSIONS,
transaction_version: 1, transaction_version: 1,
@@ -132,13 +131,6 @@ parameter_types! {
pub const Version: RuntimeVersion = VERSION; pub const Version: RuntimeVersion = VERSION;
} }
parameter_types! {
pub const DbWeight: RuntimeDbWeight = RuntimeDbWeight {
read: 60_000_000,
write: 200_000_000,
};
}
impl system::Trait for Runtime { impl system::Trait for Runtime {
type Origin = Origin; type Origin = Origin;
type Call = Call; type Call = Call;
@@ -152,7 +144,7 @@ impl system::Trait for Runtime {
type Event = Event; type Event = Event;
type BlockHashCount = BlockHashCount; type BlockHashCount = BlockHashCount;
type MaximumBlockWeight = MaximumBlockWeight; type MaximumBlockWeight = MaximumBlockWeight;
type DbWeight = DbWeight; type DbWeight = RocksDbWeight;
type BlockExecutionWeight = BlockExecutionWeight; type BlockExecutionWeight = BlockExecutionWeight;
type ExtrinsicBaseWeight = ExtrinsicBaseWeight; type ExtrinsicBaseWeight = ExtrinsicBaseWeight;
type MaximumBlockLength = MaximumBlockLength; type MaximumBlockLength = MaximumBlockLength;
+25 -2
View File
@@ -46,6 +46,7 @@ pub mod fee {
use primitives::Balance; use primitives::Balance;
use frame_support::weights::Weight; use frame_support::weights::Weight;
use sp_runtime::traits::Convert; use sp_runtime::traits::Convert;
use runtime_common::ExtrinsicBaseWeight;
/// The block saturation level. Fees will be updates based on this value. /// The block saturation level. Fees will be updates based on this value.
pub const TARGET_BLOCK_FULLNESS: Perbill = Perbill::from_percent(25); pub const TARGET_BLOCK_FULLNESS: Perbill = Perbill::from_percent(25);
@@ -63,8 +64,30 @@ pub mod fee {
pub struct WeightToFee; pub struct WeightToFee;
impl Convert<Weight, Balance> for WeightToFee { impl Convert<Weight, Balance> for WeightToFee {
fn convert(x: Weight) -> Balance { fn convert(x: Weight) -> Balance {
// in Polkadot a weight of 10_000_000 (smallest non-zero weight) is mapped to 1/10 CENT: // in Polkadot, extrinsic base weight (smallest non-zero weight) is mapped to 1/10 CENT:
Balance::from(x).saturating_mul(super::currency::CENTS / (10 * 10_000_000)) Balance::from(x).saturating_mul(super::currency::CENTS / 10) / Balance::from(ExtrinsicBaseWeight::get())
} }
} }
} }
#[cfg(test)]
mod tests {
use sp_runtime::traits::Convert;
use runtime_common::{MaximumBlockWeight, ExtrinsicBaseWeight};
use super::fee::WeightToFee;
use super::currency::{CENTS, DOLLARS};
#[test]
// This function tests that the fee for `MaximumBlockWeight` of weight is correct
fn full_block_fee_is_correct() {
// A full block should cost 16 DOLLARS
assert_eq!(WeightToFee::convert(MaximumBlockWeight::get()), 16 * DOLLARS)
}
#[test]
// This function tests that the fee for `ExtrinsicBaseWeight` of weight is correct
fn extrinsic_base_fee_is_correct() {
// `ExtrinsicBaseWeight` should cost 1/10 of a CENT
assert_eq!(WeightToFee::convert(ExtrinsicBaseWeight::get()), CENTS / 10)
}
}
+3 -11
View File
@@ -23,7 +23,7 @@
use runtime_common::{attestations, claims, parachains, registrar, slots, use runtime_common::{attestations, claims, parachains, registrar, slots,
impls::{CurrencyToVoteHandler, TargetedFeeAdjustment, ToAuthor}, impls::{CurrencyToVoteHandler, TargetedFeeAdjustment, ToAuthor},
NegativeImbalance, BlockHashCount, MaximumBlockWeight, AvailableBlockRatio, NegativeImbalance, BlockHashCount, MaximumBlockWeight, AvailableBlockRatio,
MaximumBlockLength, BlockExecutionWeight, ExtrinsicBaseWeight, MaximumBlockLength, BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight,
}; };
use sp_std::prelude::*; use sp_std::prelude::*;
@@ -57,7 +57,6 @@ use sp_staking::SessionIndex;
use frame_support::{ use frame_support::{
parameter_types, construct_runtime, debug, parameter_types, construct_runtime, debug,
traits::{KeyOwnerProofSystem, SplitTwoWays, Randomness, LockIdentifier}, traits::{KeyOwnerProofSystem, SplitTwoWays, Randomness, LockIdentifier},
weights::RuntimeDbWeight,
}; };
use im_online::sr25519::AuthorityId as ImOnlineId; use im_online::sr25519::AuthorityId as ImOnlineId;
use authority_discovery_primitives::AuthorityId as AuthorityDiscoveryId; use authority_discovery_primitives::AuthorityId as AuthorityDiscoveryId;
@@ -87,7 +86,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("polkadot"), spec_name: create_runtime_str!("polkadot"),
impl_name: create_runtime_str!("parity-polkadot"), impl_name: create_runtime_str!("parity-polkadot"),
authoring_version: 2, authoring_version: 2,
spec_version: 1008, spec_version: 1009,
impl_version: 0, impl_version: 0,
apis: RUNTIME_API_VERSIONS, apis: RUNTIME_API_VERSIONS,
transaction_version: 1, transaction_version: 1,
@@ -138,13 +137,6 @@ parameter_types! {
pub const Version: RuntimeVersion = VERSION; pub const Version: RuntimeVersion = VERSION;
} }
parameter_types! {
pub const DbWeight: RuntimeDbWeight = RuntimeDbWeight {
read: 60_000_000,
write: 200_000_000,
};
}
impl system::Trait for Runtime { impl system::Trait for Runtime {
type Origin = Origin; type Origin = Origin;
type Call = Call; type Call = Call;
@@ -158,7 +150,7 @@ impl system::Trait for Runtime {
type Event = Event; type Event = Event;
type BlockHashCount = BlockHashCount; type BlockHashCount = BlockHashCount;
type MaximumBlockWeight = MaximumBlockWeight; type MaximumBlockWeight = MaximumBlockWeight;
type DbWeight = DbWeight; type DbWeight = RocksDbWeight;
type BlockExecutionWeight = BlockExecutionWeight; type BlockExecutionWeight = BlockExecutionWeight;
type ExtrinsicBaseWeight = ExtrinsicBaseWeight; type ExtrinsicBaseWeight = ExtrinsicBaseWeight;
type MaximumBlockLength = MaximumBlockLength; type MaximumBlockLength = MaximumBlockLength;
+1 -1
View File
@@ -81,7 +81,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("polkadot-test-runtime"), spec_name: create_runtime_str!("polkadot-test-runtime"),
impl_name: create_runtime_str!("parity-polkadot-test-runtime"), impl_name: create_runtime_str!("parity-polkadot-test-runtime"),
authoring_version: 2, authoring_version: 2,
spec_version: 1051, spec_version: 1052,
impl_version: 0, impl_version: 0,
apis: RUNTIME_API_VERSIONS, apis: RUNTIME_API_VERSIONS,
transaction_version: 1, transaction_version: 1,
+25 -2
View File
@@ -46,6 +46,7 @@ pub mod fee {
use primitives::Balance; use primitives::Balance;
use frame_support::weights::Weight; use frame_support::weights::Weight;
use sp_runtime::traits::Convert; use sp_runtime::traits::Convert;
use runtime_common::ExtrinsicBaseWeight;
/// The block saturation level. Fees will be updates based on this value. /// The block saturation level. Fees will be updates based on this value.
pub const TARGET_BLOCK_FULLNESS: Perbill = Perbill::from_percent(25); pub const TARGET_BLOCK_FULLNESS: Perbill = Perbill::from_percent(25);
@@ -63,8 +64,30 @@ pub mod fee {
pub struct WeightToFee; pub struct WeightToFee;
impl Convert<Weight, Balance> for WeightToFee { impl Convert<Weight, Balance> for WeightToFee {
fn convert(x: Weight) -> Balance { fn convert(x: Weight) -> Balance {
// in Kusama a weight of 10_000 (smallest non-zero weight) is mapped to 1/10 CENT: // in Westend, extrinsic base weight (smallest non-zero weight) is mapped to 1/10 CENT:
Balance::from(x).saturating_mul(super::currency::CENTS / (10 * 10_000)) Balance::from(x).saturating_mul(super::currency::CENTS / 10) / Balance::from(ExtrinsicBaseWeight::get())
} }
} }
} }
#[cfg(test)]
mod tests {
use sp_runtime::traits::Convert;
use runtime_common::{MaximumBlockWeight, ExtrinsicBaseWeight};
use super::fee::WeightToFee;
use super::currency::{CENTS, DOLLARS};
#[test]
// This function tests that the fee for `MaximumBlockWeight` of weight is correct
fn full_block_fee_is_correct() {
// A full block should cost 16 DOLLARS
assert_eq!(WeightToFee::convert(MaximumBlockWeight::get()), 16 * DOLLARS)
}
#[test]
// This function tests that the fee for `ExtrinsicBaseWeight` of weight is correct
fn extrinsic_base_fee_is_correct() {
// `ExtrinsicBaseWeight` should cost 1/10 of a CENT
assert_eq!(WeightToFee::convert(ExtrinsicBaseWeight::get()), CENTS / 10)
}
}
+3 -11
View File
@@ -29,7 +29,7 @@ use primitives::{
use runtime_common::{attestations, parachains, registrar, use runtime_common::{attestations, parachains, registrar,
impls::{CurrencyToVoteHandler, TargetedFeeAdjustment, ToAuthor}, impls::{CurrencyToVoteHandler, TargetedFeeAdjustment, ToAuthor},
BlockHashCount, MaximumBlockWeight, AvailableBlockRatio, MaximumBlockLength, BlockHashCount, MaximumBlockWeight, AvailableBlockRatio, MaximumBlockLength,
BlockExecutionWeight, ExtrinsicBaseWeight BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight,
}; };
use sp_runtime::{ use sp_runtime::{
create_runtime_str, generic, impl_opaque_keys, create_runtime_str, generic, impl_opaque_keys,
@@ -55,7 +55,6 @@ use sp_staking::SessionIndex;
use frame_support::{ use frame_support::{
parameter_types, construct_runtime, debug, parameter_types, construct_runtime, debug,
traits::{KeyOwnerProofSystem, Randomness}, traits::{KeyOwnerProofSystem, Randomness},
weights::RuntimeDbWeight,
}; };
use im_online::sr25519::AuthorityId as ImOnlineId; use im_online::sr25519::AuthorityId as ImOnlineId;
use authority_discovery_primitives::AuthorityId as AuthorityDiscoveryId; use authority_discovery_primitives::AuthorityId as AuthorityDiscoveryId;
@@ -84,7 +83,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("westend"), spec_name: create_runtime_str!("westend"),
impl_name: create_runtime_str!("parity-westend"), impl_name: create_runtime_str!("parity-westend"),
authoring_version: 2, authoring_version: 2,
spec_version: 6, spec_version: 7,
impl_version: 0, impl_version: 0,
apis: RUNTIME_API_VERSIONS, apis: RUNTIME_API_VERSIONS,
transaction_version: 1, transaction_version: 1,
@@ -132,13 +131,6 @@ parameter_types! {
pub const Version: RuntimeVersion = VERSION; pub const Version: RuntimeVersion = VERSION;
} }
parameter_types! {
pub const DbWeight: RuntimeDbWeight = RuntimeDbWeight {
read: 60_000_000,
write: 200_000_000,
};
}
impl system::Trait for Runtime { impl system::Trait for Runtime {
type Origin = Origin; type Origin = Origin;
type Call = Call; type Call = Call;
@@ -152,7 +144,7 @@ impl system::Trait for Runtime {
type Event = Event; type Event = Event;
type BlockHashCount = BlockHashCount; type BlockHashCount = BlockHashCount;
type MaximumBlockWeight = MaximumBlockWeight; type MaximumBlockWeight = MaximumBlockWeight;
type DbWeight = DbWeight; type DbWeight = RocksDbWeight;
type BlockExecutionWeight = BlockExecutionWeight; type BlockExecutionWeight = BlockExecutionWeight;
type ExtrinsicBaseWeight = ExtrinsicBaseWeight; type ExtrinsicBaseWeight = ExtrinsicBaseWeight;
type MaximumBlockLength = MaximumBlockLength; type MaximumBlockLength = MaximumBlockLength;