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 1 commit
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
27 changes: 18 additions & 9 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 Down Expand Up @@ -132,12 +132,15 @@ 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()
.saturating_sub(Perbill::from_percent(10)) * MaximumBlockWeight::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;
}

const_assert!(AvailableBlockRatio::get_const().deconstruct() >= Perbill::from_percent(10).deconstruct());

impl frame_system::Trait for Runtime {
type Origin = Origin;
type Call = Call;
Expand Down Expand Up @@ -183,7 +186,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 +232,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_const().deconstruct() <
(AvailableBlockRatio::get_const().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 +408,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_const() <= pallet_collective::MAX_MEMBERS);

impl pallet_elections_phragmen::Trait for Runtime {
type ModuleId = ElectionsPhragmenModuleId;
Expand Down Expand Up @@ -586,7 +595,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
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
25 changes: 25 additions & 0 deletions frame/support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,38 @@ 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 fn get() -> $type {
$value
}
pub const fn get_const() -> $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
32 changes: 25 additions & 7 deletions primitives/arithmetic/src/per_things.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,9 +399,11 @@ 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`]
///
/// This can be called at compile time.
pub const fn one() -> Self {
Self::from_parts($max)
}

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

/// See [`PerThing::zero`].
pub fn zero() -> Self {
<Self as PerThing>::zero()
///
/// This can be called at compile time.
pub const fn zero() -> Self {
Self::from_parts(0)
}

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

/// See [`PerThing::deconstruct`].
pub fn deconstruct(self) -> $type {
PerThing::deconstruct(self)
///
/// This can be called at compile time.
pub const fn deconstruct(self) -> $type {
self.0
}

/// See [`PerThing::square`].
Expand Down Expand Up @@ -1130,6 +1136,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