Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bin/node-template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ parameter_types! {
pub const MaximumBlockWeight: Weight = 2 * WEIGHT_PER_SECOND;
pub const AvailableBlockRatio: Perbill = Perbill::from_percent(75);
/// Assume 10% of weight for average on_initialize calls.
pub const MaximumExtrinsicWeight: Weight = AvailableBlockRatio::get()
pub MaximumExtrinsicWeight: Weight = AvailableBlockRatio::get()
.saturating_sub(Perbill::from_percent(10)) * MaximumBlockWeight::get();
pub const MaximumBlockLength: u32 = 5 * 1024 * 1024;
pub const Version: RuntimeVersion = VERSION;
Expand Down
34 changes: 22 additions & 12 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub use node_primitives::{AccountId, Signature};
use node_primitives::{AccountIndex, Balance, BlockNumber, Hash, Index, Moment};
use sp_api::impl_runtime_apis;
use sp_runtime::{
Permill, Perbill, Perquintill, Percent, ApplyExtrinsicResult,
Permill, Perbill, Perquintill, Percent, PerThing, ApplyExtrinsicResult,
impl_opaque_keys, generic, create_runtime_str, ModuleId,
};
use sp_runtime::curve::PiecewiseLinear;
Expand All @@ -65,11 +65,11 @@ use static_assertions::const_assert;

#[cfg(any(feature = "std", test))]
pub use sp_runtime::BuildStorage;
pub use pallet_timestamp::Call as TimestampCall;
#[cfg(any(feature = "std", test))]
pub use pallet_balances::Call as BalancesCall;
#[cfg(any(feature = "std", test))]
pub use frame_system::Call as SystemCall;
pub use pallet_contracts::Gas;
pub use frame_support::StorageValue;
#[cfg(any(feature = "std", test))]
pub use pallet_staking::StakerStatus;

/// Implementations of some helper traits passed into runtime modules as associated types.
Expand Down Expand Up @@ -126,18 +126,22 @@ impl OnUnbalanced<NegativeImbalance> for DealWithFees {
}
}

const AVERAGE_ON_INITIALIZE_WEIGHT: Perbill = Perbill::from_percent(10);
parameter_types! {
pub const BlockHashCount: BlockNumber = 2400;
/// We allow for 2 seconds of compute with a 6 second average block time.
pub const MaximumBlockWeight: Weight = 2 * WEIGHT_PER_SECOND;
pub const AvailableBlockRatio: Perbill = Perbill::from_percent(75);
/// Assume 10% of weight for average on_initialize calls.
pub const MaximumExtrinsicWeight: Weight = AvailableBlockRatio::get()
.saturating_sub(Perbill::from_percent(10)) * MaximumBlockWeight::get();
pub MaximumExtrinsicWeight: Weight =
AvailableBlockRatio::get().saturating_sub(AVERAGE_ON_INITIALIZE_WEIGHT)
* MaximumBlockWeight::get();
pub const MaximumBlockLength: u32 = 5 * 1024 * 1024;
pub const Version: RuntimeVersion = VERSION;
}

const_assert!(AvailableBlockRatio::get().deconstruct() >= AVERAGE_ON_INITIALIZE_WEIGHT.deconstruct());

impl frame_system::Trait for Runtime {
type Origin = Origin;
type Call = Call;
Expand Down Expand Up @@ -183,7 +187,7 @@ impl pallet_utility::Trait for Runtime {
}

parameter_types! {
pub const MaximumSchedulerWeight: Weight = Perbill::from_percent(80) * MaximumBlockWeight::get();
pub MaximumSchedulerWeight: Weight = Perbill::from_percent(80) * MaximumBlockWeight::get();
}

impl pallet_scheduler::Trait for Runtime {
Expand Down Expand Up @@ -229,10 +233,16 @@ impl pallet_balances::Trait for Runtime {

parameter_types! {
pub const TransactionByteFee: Balance = 10 * MILLICENTS;
// for a sane configuration, this should always be less than `AvailableBlockRatio`.
pub const TargetBlockFullness: Perquintill = Perquintill::from_percent(25);
}

// for a sane configuration, this should always be less than `AvailableBlockRatio`.
const_assert!(
TargetBlockFullness::get().deconstruct() <
(AvailableBlockRatio::get().deconstruct() as <Perquintill as PerThing>::Inner)
* (<Perquintill as PerThing>::ACCURACY / <Perbill as PerThing>::ACCURACY as <Perquintill as PerThing>::Inner)
);

impl pallet_transaction_payment::Trait for Runtime {
type Currency = Balances;
type OnTransactionPayment = DealWithFees;
Expand Down Expand Up @@ -399,17 +409,17 @@ impl pallet_collective::Trait<CouncilCollective> for Runtime {
type MaxProposals = CouncilMaxProposals;
}

const DESIRED_MEMBERS: u32 = 13;
parameter_types! {
pub const CandidacyBond: Balance = 10 * DOLLARS;
pub const VotingBond: Balance = 1 * DOLLARS;
pub const TermDuration: BlockNumber = 7 * DAYS;
pub const DesiredMembers: u32 = DESIRED_MEMBERS;
pub const DesiredMembers: u32 = 13;
pub const DesiredRunnersUp: u32 = 7;
pub const ElectionsPhragmenModuleId: LockIdentifier = *b"phrelect";
}

// Make sure that there are no more than `MAX_MEMBERS` members elected via phragmen.
const_assert!(DESIRED_MEMBERS <= pallet_collective::MAX_MEMBERS);
const_assert!(DesiredMembers::get() <= pallet_collective::MAX_MEMBERS);

impl pallet_elections_phragmen::Trait for Runtime {
type ModuleId = ElectionsPhragmenModuleId;
Expand Down Expand Up @@ -586,7 +596,7 @@ impl pallet_im_online::Trait for Runtime {
}

parameter_types! {
pub const OffencesWeightSoftLimit: Weight = Perbill::from_percent(60) * MaximumBlockWeight::get();
pub OffencesWeightSoftLimit: Weight = Perbill::from_percent(60) * MaximumBlockWeight::get();
}

impl pallet_offences::Trait for Runtime {
Expand Down
8 changes: 4 additions & 4 deletions bin/node/testing/src/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use sp_keyring::{Ed25519Keyring, Sr25519Keyring};
use node_runtime::{
GenesisConfig, BalancesConfig, SessionConfig, StakingConfig, SystemConfig,
GrandpaConfig, IndicesConfig, ContractsConfig, SocietyConfig, WASM_BINARY,
AccountId,
AccountId, StakerStatus,
};
use node_runtime::constants::currency::*;
use sp_core::ChangesTrieConfiguration;
Expand Down Expand Up @@ -87,9 +87,9 @@ pub fn config_endowed(
}),
pallet_staking: Some(StakingConfig {
stakers: vec![
(dave(), alice(), 111 * DOLLARS, pallet_staking::StakerStatus::Validator),
(eve(), bob(), 100 * DOLLARS, pallet_staking::StakerStatus::Validator),
(ferdie(), charlie(), 100 * DOLLARS, pallet_staking::StakerStatus::Validator)
(dave(), alice(), 111 * DOLLARS, StakerStatus::Validator),
(eve(), bob(), 100 * DOLLARS, StakerStatus::Validator),
(ferdie(), charlie(), 100 * DOLLARS, StakerStatus::Validator)
],
validator_count: 3,
minimum_validator_count: 0,
Expand Down
2 changes: 1 addition & 1 deletion frame/democracy/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl frame_system::Trait for Test {
type OnKilledAccount = ();
}
parameter_types! {
pub const MaximumSchedulerWeight: Weight = Perbill::from_percent(80) * MaximumBlockWeight::get();
pub MaximumSchedulerWeight: Weight = Perbill::from_percent(80) * MaximumBlockWeight::get();
}
impl pallet_scheduler::Trait for Test {
type Event = Event;
Expand Down
2 changes: 1 addition & 1 deletion frame/grandpa/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ impl staking::Trait for Test {
}

parameter_types! {
pub const OffencesWeightSoftLimit: Weight = Perbill::from_percent(60) * MaximumBlockWeight::get();
pub OffencesWeightSoftLimit: Weight = Perbill::from_percent(60) * MaximumBlockWeight::get();
}

impl offences::Trait for Test {
Expand Down
2 changes: 1 addition & 1 deletion frame/offences/benchmarking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ impl pallet_im_online::Trait for Test {
}

parameter_types! {
pub const OffencesWeightSoftLimit: Weight = Perbill::from_percent(60) * MaximumBlockWeight::get();
pub OffencesWeightSoftLimit: Weight = Perbill::from_percent(60) * MaximumBlockWeight::get();
}

impl pallet_offences::Trait for Test {
Expand Down
2 changes: 1 addition & 1 deletion frame/offences/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ impl frame_system::Trait for Runtime {
}

parameter_types! {
pub const OffencesWeightSoftLimit: Weight = Perbill::from_percent(60) * MaximumBlockWeight::get();
pub OffencesWeightSoftLimit: Weight = Perbill::from_percent(60) * MaximumBlockWeight::get();
}

impl Trait for Runtime {
Expand Down
2 changes: 1 addition & 1 deletion frame/scheduler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ mod tests {
type Event = ();
}
parameter_types! {
pub const MaximumSchedulerWeight: Weight = Perbill::from_percent(80) * MaximumBlockWeight::get();
pub MaximumSchedulerWeight: Weight = Perbill::from_percent(80) * MaximumBlockWeight::get();
}
impl Trait for Test {
type Event = ();
Expand Down
55 changes: 51 additions & 4 deletions frame/support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,34 +84,81 @@ pub use sp_runtime::{self, ConsensusEngineId, print, traits::Printable};
#[derive(Debug)]
pub enum Never {}

/// Macro for easily creating a new implementation of the `Get` trait. Use similarly to
/// how you would declare a `const`:
/// Macro for easily creating a new implementation of the `Get` trait. If `const` token is used, the
/// rhs of the expression must be `const`-only, and get is implemented as `const`:
///
/// ```no_compile
/// ```
/// # use frame_support::traits::Get;
/// # use frame_support::parameter_types;
/// // This function cannot be used in a const context.
/// fn non_const_expression() -> u64 { 99 }
///
/// const FIXED_VALUE: u64 = 10;
/// parameter_types! {
/// pub const Argument: u64 = 42;
/// pub const Argument: u64 = 42 + FIXED_VALUE;
/// pub OtherArgument: u64 = non_const_expression();
/// }
///
/// trait Config {
/// type Parameter: Get<u64>;
/// type OtherParameter: Get<u64>;
/// }
///
/// struct Runtime;
/// impl Config for Runtime {
/// type Parameter = Argument;
/// type OtherParameter = OtherArgument;
/// }
/// ```
///
/// Invalid example:
///
/// ```compile_fail
/// # use frame_support::traits::Get;
/// # use frame_support::parameter_types;
/// // This function cannot be used in a const context.
/// fn non_const_expression() -> u64 { 99 }
///
/// parameter_types! {
/// pub const Argument: u64 = non_const_expression();
/// }
/// ```

#[macro_export]
macro_rules! parameter_types {
(
$( #[ $attr:meta ] )*
$vis:vis const $name:ident: $type:ty = $value:expr;
$( $rest:tt )*
) => (
$( #[ $attr ] )*
$vis struct $name;
$crate::parameter_types!{IMPL_CONST $name , $type , $value}
$crate::parameter_types!{ $( $rest )* }
);
(
$( #[ $attr:meta ] )*
$vis:vis $name:ident: $type:ty = $value:expr;
$( $rest:tt )*
) => (
$( #[ $attr ] )*
$vis struct $name;
$crate::parameter_types!{IMPL $name , $type , $value}
$crate::parameter_types!{ $( $rest )* }
);
() => ();
(IMPL_CONST $name:ident , $type:ty , $value:expr) => {
impl $name {
pub const fn get() -> $type {
$value
}
}
impl<I: From<$type>> $crate::traits::Get<I> for $name {
fn get() -> I {
I::from($value)
}
}
};
(IMPL $name:ident , $type:ty , $value:expr) => {
impl $name {
pub fn get() -> $type {
Expand Down
2 changes: 1 addition & 1 deletion frame/system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1877,7 +1877,7 @@ pub(crate) mod tests {
pub const MaximumExtrinsicWeight: Weight = 768;
pub const AvailableBlockRatio: Perbill = Perbill::from_percent(75);
pub const MaximumBlockLength: u32 = 1024;
pub const Version: RuntimeVersion = RuntimeVersion {
pub Version: RuntimeVersion = RuntimeVersion {
spec_name: sp_version::create_runtime_str!("test"),
impl_name: sp_version::create_runtime_str!("system-test"),
authoring_version: 1,
Expand Down
27 changes: 19 additions & 8 deletions primitives/arithmetic/src/per_things.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,6 @@ macro_rules! implement_per_thing {
impl $name {
/// From an explicitly defined number of parts per maximum of the type.
///
/// This can be called at compile time.
// needed only for peru16. Since peru16 is the only type in which $max ==
// $type::max_value(), rustc is being a smart-a** here by warning that the comparison
// is not needed.
Expand All @@ -399,9 +398,9 @@ macro_rules! implement_per_thing {
Self(([x, 100][(x > 100) as usize] as $upper_type * $max as $upper_type / 100) as $type)
}

/// See [`PerThing::one`].
pub fn one() -> Self {
<Self as PerThing>::one()
/// See [`PerThing::one`]
pub const fn one() -> Self {
Self::from_parts($max)
}

/// See [`PerThing::is_one`].
Expand All @@ -410,8 +409,8 @@ macro_rules! implement_per_thing {
}

/// See [`PerThing::zero`].
pub fn zero() -> Self {
<Self as PerThing>::zero()
pub const fn zero() -> Self {
Self::from_parts(0)
}

/// See [`PerThing::is_zero`].
Expand All @@ -420,8 +419,8 @@ macro_rules! implement_per_thing {
}

/// See [`PerThing::deconstruct`].
pub fn deconstruct(self) -> $type {
PerThing::deconstruct(self)
pub const fn deconstruct(self) -> $type {
self.0
}

/// See [`PerThing::square`].
Expand Down Expand Up @@ -1130,6 +1129,18 @@ macro_rules! implement_per_thing {
1,
);
}

#[test]
#[allow(unused)]
fn const_fns_work() {
const C1: $name = $name::from_percent(50);
const C2: $name = $name::one();
const C3: $name = $name::zero();
const C4: $name = $name::from_parts(1);

// deconstruct is also const, hence it can be called in const rhs.
const C5: bool = C1.deconstruct() == 0;
}
}
};
}
Expand Down