From 401540eefc50c5ed68063c22a65c7dbbc46de7f8 Mon Sep 17 00:00:00 2001 From: Alexander Popiak <alexander.popiak@parity.io> Date: Thu, 13 Jan 2022 21:08:24 +0100 Subject: [PATCH] Add `fast-runtime` Cargo Feature for Quick Test Runs (#4332) * add fast-runtime feature for reduced session times * make democracy periods fast on fast-runtime * propagate fast-runtime feature through cargo.toml files * add fast motion and term durations to Kusama * Update runtime/westend/Cargo.toml Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> * set session time to 2 minutes to avoid block production issues * formatting * update Substrate * set democracy fast periods back to 1min * set launch period and enactment period to 1 block in fast-runtime * remove unnecessary westend period configs * add prod_or_test macro to allow specifying prod, test and env values for parameter types * move prod_or_test macro into common module and use it consistently * rename macro to prod_or_fast * cargo +nightly fmt * bump impl_versions * newline Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> * add note that env variable is evaluated at compile time * newline Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> * newline Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> * cargo fmt * impl_version: 0 * impl_version: 0 * use prod_or_fast macro for LeasePeriod and LeaseOffset * use prod_or_fast macro in WND and ROC constants Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> Co-authored-by: Giles Cope <gilescope@gmail.com> --- polkadot/Cargo.toml | 2 +- polkadot/cli/Cargo.toml | 1 + polkadot/node/service/Cargo.toml | 7 +++ polkadot/runtime/common/src/lib.rs | 29 +++++++++++++ polkadot/runtime/kusama/Cargo.toml | 4 ++ polkadot/runtime/kusama/src/lib.rs | 43 ++++++++++++------- polkadot/runtime/polkadot/Cargo.toml | 4 ++ polkadot/runtime/polkadot/src/lib.rs | 43 ++++++++++++------- polkadot/runtime/rococo/Cargo.toml | 3 ++ polkadot/runtime/rococo/constants/src/lib.rs | 5 ++- polkadot/runtime/westend/Cargo.toml | 4 ++ polkadot/runtime/westend/constants/src/lib.rs | 4 +- polkadot/runtime/westend/src/lib.rs | 7 --- 13 files changed, 116 insertions(+), 40 deletions(-) diff --git a/polkadot/Cargo.toml b/polkadot/Cargo.toml index 43a6578bfb1..3f8a98f6d56 100644 --- a/polkadot/Cargo.toml +++ b/polkadot/Cargo.toml @@ -139,9 +139,9 @@ overflow-checks = true [features] runtime-benchmarks= [ "polkadot-cli/runtime-benchmarks" ] try-runtime = [ "polkadot-cli/try-runtime" ] +fast-runtime = [ "polkadot-cli/fast-runtime" ] runtime-metrics = [ "polkadot-cli/runtime-metrics" ] - # Configuration for building a .deb package - for use with `cargo-deb` [package.metadata.deb] name = "polkadot" diff --git a/polkadot/cli/Cargo.toml b/polkadot/cli/Cargo.toml index 6de73cd632c..6acd85bc421 100644 --- a/polkadot/cli/Cargo.toml +++ b/polkadot/cli/Cargo.toml @@ -56,6 +56,7 @@ runtime-benchmarks = [ "service/runtime-benchmarks", "polkadot-node-metrics/runt trie-memory-tracker = [ "sp-trie/memory-tracker" ] full-node = [ "service/full-node" ] try-runtime = [ "service/try-runtime" ] +fast-runtime = [ "service/fast-runtime" ] # Configure the native runtimes to use. Polkadot is enabled by default. # diff --git a/polkadot/node/service/Cargo.toml b/polkadot/node/service/Cargo.toml index eb051644b17..056287e5310 100644 --- a/polkadot/node/service/Cargo.toml +++ b/polkadot/node/service/Cargo.toml @@ -179,6 +179,13 @@ try-runtime = [ "westend-runtime/try-runtime", "rococo-runtime/try-runtime", ] +fast-runtime = [ + "polkadot-runtime/fast-runtime", + "kusama-runtime/fast-runtime", + "westend-runtime/fast-runtime", + "rococo-runtime/fast-runtime", +] + malus = ["full-node"] runtime-metrics = [ "polkadot-client/runtime-metrics", diff --git a/polkadot/runtime/common/src/lib.rs b/polkadot/runtime/common/src/lib.rs index 5ce7c89efb7..2170ba1c72e 100644 --- a/polkadot/runtime/common/src/lib.rs +++ b/polkadot/runtime/common/src/lib.rs @@ -190,6 +190,35 @@ impl pallet_staking::BenchmarkingConfig for StakingBenchmarkingConfig { type MaxNominators = ConstU32<1000>; } +/// Macro to set a value (e.g. when using the `parameter_types` macro) to either a production value +/// or to an environment variable or testing value (in case the `fast-runtime` feature is selected). +/// Note that the environment variable is evaluated _at compile time_. +/// +/// Usage: +/// ```Rust +/// parameter_types! { +/// // Note that the env variable version parameter cannot be const. +/// pub LaunchPeriod: BlockNumber = prod_or_fast!(7 * DAYS, 1, "KSM_LAUNCH_PERIOD"); +/// pub const VotingPeriod: BlockNumber = prod_or_fast!(7 * DAYS, 1 * MINUTES); +/// } +#[macro_export] +macro_rules! prod_or_fast { + ($prod:expr, $test:expr) => { + if cfg!(feature = "fast-runtime") { + $test + } else { + $prod + } + }; + ($prod:expr, $test:expr, $env:expr) => { + if cfg!(feature = "fast-runtime") { + core::option_env!($env).map(|s| s.parse().ok()).flatten().unwrap_or($test) + } else { + $prod + } + }; +} + #[cfg(test)] mod multiplier_tests { use super::*; diff --git a/polkadot/runtime/kusama/Cargo.toml b/polkadot/runtime/kusama/Cargo.toml index 89dbda5636a..e74a7e4c5a5 100644 --- a/polkadot/runtime/kusama/Cargo.toml +++ b/polkadot/runtime/kusama/Cargo.toml @@ -270,4 +270,8 @@ disable-runtime-api = [] on-chain-release-build = [ "sp-api/disable-logging", ] + +# Set timing constants (e.g. session period) to faster versions to speed up testing. +fast-runtime = [] + runtime-metrics = ["runtime-parachains/runtime-metrics", "sp-io/with-tracing"] diff --git a/polkadot/runtime/kusama/src/lib.rs b/polkadot/runtime/kusama/src/lib.rs index 5711545ee10..3af0cd996ee 100644 --- a/polkadot/runtime/kusama/src/lib.rs +++ b/polkadot/runtime/kusama/src/lib.rs @@ -33,8 +33,8 @@ use primitives::{ v2::SessionInfo, }; use runtime_common::{ - auctions, claims, crowdloan, impls::DealWithFees, paras_registrar, slots, BlockHashCount, - BlockLength, BlockWeights, CurrencyToVote, OffchainSolutionLengthLimit, + auctions, claims, crowdloan, impls::DealWithFees, paras_registrar, prod_or_fast, slots, + BlockHashCount, BlockLength, BlockWeights, CurrencyToVote, OffchainSolutionLengthLimit, OffchainSolutionWeightLimit, RocksDbWeight, SlowAdjustingFeeUpdate, }; use sp_core::u32_trait::{_1, _2, _3, _5}; @@ -249,9 +249,13 @@ impl pallet_preimage::Config for Runtime { } parameter_types! { - pub const EpochDuration: u64 = EPOCH_DURATION_IN_SLOTS as u64; + pub EpochDuration: u64 = prod_or_fast!( + EPOCH_DURATION_IN_SLOTS as u64, + 2 * MINUTES as u64, + "KSM_EPOCH_DURATION" + ); pub const ExpectedBlockTime: Moment = MILLISECS_PER_BLOCK; - pub const ReportLongevity: u64 = + pub ReportLongevity: u64 = BondingDuration::get() as u64 * SessionsPerEra::get() as u64 * EpochDuration::get(); } @@ -385,8 +389,17 @@ impl pallet_session::historical::Config for Runtime { parameter_types! { // phase durations. 1/4 of the last session for each. - pub const SignedPhase: u32 = EPOCH_DURATION_IN_SLOTS / 4; - pub const UnsignedPhase: u32 = EPOCH_DURATION_IN_SLOTS / 4; + // in testing: 1min or half of the session for each + pub SignedPhase: u32 = prod_or_fast!( + EPOCH_DURATION_IN_SLOTS / 4, + (1 * MINUTES).min(EpochDuration::get().saturated_into::<u32>() / 2), + "KSM_SIGNED_PHASE" + ); + pub UnsignedPhase: u32 = prod_or_fast!( + EPOCH_DURATION_IN_SLOTS / 4, + (1 * MINUTES).min(EpochDuration::get().saturated_into::<u32>() / 2), + "KSM_UNSIGNED_PHASE" + ); // signed config pub const SignedMaxSubmissions: u32 = 16; @@ -572,12 +585,12 @@ impl pallet_staking::Config for Runtime { } parameter_types! { - pub const LaunchPeriod: BlockNumber = 7 * DAYS; - pub const VotingPeriod: BlockNumber = 7 * DAYS; - pub const FastTrackVotingPeriod: BlockNumber = 3 * HOURS; + pub LaunchPeriod: BlockNumber = prod_or_fast!(7 * DAYS, 1, "KSM_LAUNCH_PERIOD"); + pub VotingPeriod: BlockNumber = prod_or_fast!(7 * DAYS, 1 * MINUTES, "KSM_VOTING_PERIOD"); + pub FastTrackVotingPeriod: BlockNumber = prod_or_fast!(3 * HOURS, 1 * MINUTES, "KSM_FAST_TRACK_VOTING_PERIOD"); pub const MinimumDeposit: Balance = 100 * CENTS; - pub const EnactmentPeriod: BlockNumber = 8 * DAYS; - pub const CooloffPeriod: BlockNumber = 7 * DAYS; + pub EnactmentPeriod: BlockNumber = prod_or_fast!(8 * DAYS, 1, "KSM_ENACTMENT_PERIOD"); + pub CooloffPeriod: BlockNumber = prod_or_fast!(7 * DAYS, 1 * MINUTES, "KSM_COOLOFF_PERIOD"); pub const InstantAllowed: bool = true; pub const MaxVotes: u32 = 100; pub const MaxProposals: u32 = 100; @@ -637,7 +650,7 @@ impl pallet_democracy::Config for Runtime { } parameter_types! { - pub const CouncilMotionDuration: BlockNumber = 3 * DAYS; + pub CouncilMotionDuration: BlockNumber = prod_or_fast!(3 * DAYS, 2 * MINUTES, "KSM_MOTION_DURATION"); pub const CouncilMaxProposals: u32 = 100; pub const CouncilMaxMembers: u32 = 100; } @@ -661,7 +674,7 @@ parameter_types! { // additional data per vote is 32 bytes (account id). pub const VotingBondFactor: Balance = deposit(0, 32); /// Daily council elections - pub const TermDuration: BlockNumber = 24 * HOURS; + pub TermDuration: BlockNumber = prod_or_fast!(24 * HOURS, 2 * MINUTES, "KSM_TERM_DURATION"); pub const DesiredMembers: u32 = 19; pub const DesiredRunnersUp: u32 = 19; pub const PhragmenElectionPalletId: LockIdentifier = *b"phrelect"; @@ -689,7 +702,7 @@ impl pallet_elections_phragmen::Config for Runtime { } parameter_types! { - pub const TechnicalMotionDuration: BlockNumber = 3 * DAYS; + pub TechnicalMotionDuration: BlockNumber = prod_or_fast!(3 * DAYS, 2 * MINUTES, "KSM_MOTION_DURATION"); pub const TechnicalMaxProposals: u32 = 100; pub const TechnicalMaxMembers: u32 = 100; } @@ -1252,7 +1265,7 @@ impl paras_registrar::Config for Runtime { parameter_types! { // 6 weeks - pub const LeasePeriod: BlockNumber = 6 * WEEKS; + pub LeasePeriod: BlockNumber = prod_or_fast!(6 * WEEKS, 6 * WEEKS, "KSM_LEASE_PERIOD"); } impl slots::Config for Runtime { diff --git a/polkadot/runtime/polkadot/Cargo.toml b/polkadot/runtime/polkadot/Cargo.toml index 0eabe799f6a..ee58079f0ca 100644 --- a/polkadot/runtime/polkadot/Cargo.toml +++ b/polkadot/runtime/polkadot/Cargo.toml @@ -257,4 +257,8 @@ disable-runtime-api = [] on-chain-release-build = [ "sp-api/disable-logging", ] + +# Set timing constants (e.g. session period) to faster versions to speed up testing. +fast-runtime = [] + runtime-metrics = ["runtime-parachains/runtime-metrics", "sp-io/with-tracing"] diff --git a/polkadot/runtime/polkadot/src/lib.rs b/polkadot/runtime/polkadot/src/lib.rs index 69c419e048c..7a0caf93e0c 100644 --- a/polkadot/runtime/polkadot/src/lib.rs +++ b/polkadot/runtime/polkadot/src/lib.rs @@ -22,8 +22,8 @@ use pallet_transaction_payment::CurrencyAdapter; use runtime_common::{ - auctions, claims, crowdloan, impls::DealWithFees, paras_registrar, slots, BlockHashCount, - BlockLength, BlockWeights, CurrencyToVote, OffchainSolutionLengthLimit, + auctions, claims, crowdloan, impls::DealWithFees, paras_registrar, prod_or_fast, slots, + BlockHashCount, BlockLength, BlockWeights, CurrencyToVote, OffchainSolutionLengthLimit, OffchainSolutionWeightLimit, RocksDbWeight, SlowAdjustingFeeUpdate, }; @@ -293,9 +293,13 @@ impl pallet_preimage::Config for Runtime { } parameter_types! { - pub const EpochDuration: u64 = EPOCH_DURATION_IN_SLOTS as u64; + pub EpochDuration: u64 = prod_or_fast!( + EPOCH_DURATION_IN_SLOTS as u64, + 2 * MINUTES as u64, + "KSM_EPOCH_DURATION" + ); pub const ExpectedBlockTime: Moment = MILLISECS_PER_BLOCK; - pub const ReportLongevity: u64 = + pub ReportLongevity: u64 = BondingDuration::get() as u64 * SessionsPerEra::get() as u64 * EpochDuration::get(); } @@ -425,8 +429,17 @@ impl pallet_session::historical::Config for Runtime { parameter_types! { // phase durations. 1/4 of the last session for each. - pub const SignedPhase: u32 = EPOCH_DURATION_IN_SLOTS / 4; - pub const UnsignedPhase: u32 = EPOCH_DURATION_IN_SLOTS / 4; + // in testing: 1min or half of the session for each + pub SignedPhase: u32 = prod_or_fast!( + EPOCH_DURATION_IN_SLOTS / 4, + (1 * MINUTES).min(EpochDuration::get().saturated_into::<u32>() / 2), + "DOT_SIGNED_PHASE" + ); + pub UnsignedPhase: u32 = prod_or_fast!( + EPOCH_DURATION_IN_SLOTS / 4, + (1 * MINUTES).min(EpochDuration::get().saturated_into::<u32>() / 2), + "DOT_UNSIGNED_PHASE" + ); // signed config pub const SignedMaxSubmissions: u32 = 16; @@ -593,12 +606,12 @@ impl pallet_identity::Config for Runtime { } parameter_types! { - pub const LaunchPeriod: BlockNumber = 28 * DAYS; - pub const VotingPeriod: BlockNumber = 28 * DAYS; - pub const FastTrackVotingPeriod: BlockNumber = 3 * HOURS; + pub LaunchPeriod: BlockNumber = prod_or_fast!(28 * DAYS, 1, "DOT_LAUNCH_PERIOD"); + pub VotingPeriod: BlockNumber = prod_or_fast!(28 * DAYS, 1 * MINUTES, "DOT_VOTING_PERIOD"); + pub FastTrackVotingPeriod: BlockNumber = prod_or_fast!(3 * HOURS, 1 * MINUTES, "DOT_FAST_TRACK_VOTING_PERIOD"); pub const MinimumDeposit: Balance = 100 * DOLLARS; - pub const EnactmentPeriod: BlockNumber = 28 * DAYS; - pub const CooloffPeriod: BlockNumber = 7 * DAYS; + pub EnactmentPeriod: BlockNumber = prod_or_fast!(28 * DAYS, 1, "DOT_ENACTMENT_PERIOD"); + pub CooloffPeriod: BlockNumber = prod_or_fast!(7 * DAYS, 1, "DOT_COOLOFF_PERIOD"); pub const InstantAllowed: bool = true; pub const MaxVotes: u32 = 100; pub const MaxProposals: u32 = 100; @@ -668,7 +681,7 @@ impl pallet_democracy::Config for Runtime { } parameter_types! { - pub const CouncilMotionDuration: BlockNumber = 7 * DAYS; + pub CouncilMotionDuration: BlockNumber = prod_or_fast!(7 * DAYS, 2 * MINUTES, "DOT_MOTION_DURATION"); pub const CouncilMaxProposals: u32 = 100; pub const CouncilMaxMembers: u32 = 100; } @@ -692,7 +705,7 @@ parameter_types! { // additional data per vote is 32 bytes (account id). pub const VotingBondFactor: Balance = deposit(0, 32); /// Weekly council elections; scaling up to monthly eventually. - pub const TermDuration: BlockNumber = 7 * DAYS; + pub TermDuration: BlockNumber = prod_or_fast!(7 * DAYS, 2 * MINUTES, "DOT_TERM_DURATION"); /// 13 members initially, to be increased to 23 eventually. pub const DesiredMembers: u32 = 13; pub const DesiredRunnersUp: u32 = 20; @@ -1236,13 +1249,13 @@ impl paras_registrar::Config for Runtime { parameter_types! { // 12 weeks = 3 months per lease period -> 8 lease periods ~ 2 years - pub const LeasePeriod: BlockNumber = 12 * WEEKS; + pub LeasePeriod: BlockNumber = prod_or_fast!(12 * WEEKS, 12 * WEEKS, "DOT_LEASE_PERIOD"); // Polkadot Genesis was on May 26, 2020. // Target Parachain Onboarding Date: Dec 15, 2021. // Difference is 568 days. // We want a lease period to start on the target onboarding date. // 568 % (12 * 7) = 64 day offset - pub const LeaseOffset: BlockNumber = 64 * DAYS; + pub LeaseOffset: BlockNumber = prod_or_fast!(64 * DAYS, 0, "DOT_LEASE_OFFSET"); } impl slots::Config for Runtime { diff --git a/polkadot/runtime/rococo/Cargo.toml b/polkadot/runtime/rococo/Cargo.toml index 9deb892ad46..2c40ff64710 100644 --- a/polkadot/runtime/rococo/Cargo.toml +++ b/polkadot/runtime/rococo/Cargo.toml @@ -205,4 +205,7 @@ try-runtime = [ "pallet-multisig/try-runtime", ] +# Set timing constants (e.g. session period) to faster versions to speed up testing. +fast-runtime = [] + runtime-metrics = ["runtime-parachains/runtime-metrics", "sp-io/with-tracing"] diff --git a/polkadot/runtime/rococo/constants/src/lib.rs b/polkadot/runtime/rococo/constants/src/lib.rs index 078e66fb661..bb0bfc4a974 100644 --- a/polkadot/runtime/rococo/constants/src/lib.rs +++ b/polkadot/runtime/rococo/constants/src/lib.rs @@ -33,10 +33,13 @@ pub mod currency { /// Time and blocks. pub mod time { use primitives::v0::{BlockNumber, Moment}; + use runtime_common::prod_or_fast; + pub const MILLISECS_PER_BLOCK: Moment = 6000; pub const SLOT_DURATION: Moment = MILLISECS_PER_BLOCK; + pub const DEFAULT_EPOCH_DURATION: BlockNumber = prod_or_fast!(1 * HOURS, 1 * MINUTES); frame_support::parameter_types! { - pub storage EpochDurationInBlocks: BlockNumber = 1 * HOURS; + pub storage EpochDurationInBlocks: BlockNumber = DEFAULT_EPOCH_DURATION; } // These time units are defined in number of blocks. diff --git a/polkadot/runtime/westend/Cargo.toml b/polkadot/runtime/westend/Cargo.toml index 8b36ab0f95b..75301f054b2 100644 --- a/polkadot/runtime/westend/Cargo.toml +++ b/polkadot/runtime/westend/Cargo.toml @@ -254,4 +254,8 @@ try-runtime = [ # runtime without clashing with the runtime API exported functions # in WASM. disable-runtime-api = [] + +# Set timing constants (e.g. session period) to faster versions to speed up testing. +fast-runtime = [] + runtime-metrics = ["runtime-parachains/runtime-metrics", "sp-io/with-tracing"] diff --git a/polkadot/runtime/westend/constants/src/lib.rs b/polkadot/runtime/westend/constants/src/lib.rs index 48831e507ac..46ce6d9e4af 100644 --- a/polkadot/runtime/westend/constants/src/lib.rs +++ b/polkadot/runtime/westend/constants/src/lib.rs @@ -36,9 +36,11 @@ pub mod currency { /// Time and blocks. pub mod time { use primitives::v0::{BlockNumber, Moment}; + use runtime_common::prod_or_fast; + pub const MILLISECS_PER_BLOCK: Moment = 6000; pub const SLOT_DURATION: Moment = MILLISECS_PER_BLOCK; - pub const EPOCH_DURATION_IN_SLOTS: BlockNumber = 1 * HOURS; + pub const EPOCH_DURATION_IN_SLOTS: BlockNumber = prod_or_fast!(1 * HOURS, 1 * MINUTES); // These time units are defined in number of blocks. pub const MINUTES: BlockNumber = 60_000 / (MILLISECS_PER_BLOCK as BlockNumber); diff --git a/polkadot/runtime/westend/src/lib.rs b/polkadot/runtime/westend/src/lib.rs index f7d1cb7d1f0..f6ada8b1161 100644 --- a/polkadot/runtime/westend/src/lib.rs +++ b/polkadot/runtime/westend/src/lib.rs @@ -477,13 +477,6 @@ impl pallet_staking::Config for Runtime { } parameter_types! { - pub const LaunchPeriod: BlockNumber = 7 * DAYS; - pub const VotingPeriod: BlockNumber = 7 * DAYS; - pub const FastTrackVotingPeriod: BlockNumber = 3 * HOURS; - pub const MinimumDeposit: Balance = 100 * CENTS; - pub const EnactmentPeriod: BlockNumber = 8 * DAYS; - pub const CooloffPeriod: BlockNumber = 7 * DAYS; - pub const InstantAllowed: bool = true; pub const MaxAuthorities: u32 = 100_000; } -- GitLab