diff --git a/substrate/node-template/runtime/src/lib.rs b/substrate/node-template/runtime/src/lib.rs index 5cf774a5e9..865299285a 100644 --- a/substrate/node-template/runtime/src/lib.rs +++ b/substrate/node-template/runtime/src/lib.rs @@ -33,7 +33,6 @@ pub use runtime_primitives::BuildStorage; pub use timestamp::Call as TimestampCall; pub use balances::Call as BalancesCall; pub use runtime_primitives::{Permill, Perbill}; -pub use timestamp::BlockPeriod; pub use support::{StorageValue, construct_runtime, parameter_types}; /// Alias to the signature scheme used for Aura authority signatures. @@ -155,10 +154,14 @@ impl indices::Trait for Runtime { type Event = Event; } +parameter_types! { + pub const MinimumPeriod: u64 = 5; +} impl timestamp::Trait for Runtime { /// A timestamp: seconds since the unix epoch. type Moment = u64; type OnTimestampSet = Aura; + type MinimumPeriod = MinimumPeriod; } parameter_types! { @@ -207,7 +210,7 @@ construct_runtime!( UncheckedExtrinsic = UncheckedExtrinsic { System: system::{Module, Call, Storage, Config, Event}, - Timestamp: timestamp::{Module, Call, Storage, Config, Inherent}, + Timestamp: timestamp::{Module, Call, Storage, Inherent}, Aura: aura::{Module, Config, Inherent(Timestamp)}, Indices: indices::{default, Config}, Balances: balances, diff --git a/substrate/node-template/src/chain_spec.rs b/substrate/node-template/src/chain_spec.rs index f4d5afb7c7..3970522b37 100644 --- a/substrate/node-template/src/chain_spec.rs +++ b/substrate/node-template/src/chain_spec.rs @@ -1,6 +1,6 @@ use primitives::{ed25519, sr25519, Pair}; use node_template_runtime::{ - AccountId, GenesisConfig, AuraConfig, TimestampConfig, BalancesConfig, + AccountId, GenesisConfig, AuraConfig, BalancesConfig, SudoConfig, IndicesConfig, SystemConfig, WASM_BINARY, AuraId }; use substrate_service; @@ -97,9 +97,6 @@ fn testnet_genesis(initial_authorities: Vec, endowed_accounts: Vec GenesisConfig { term_duration: 28 * DAYS, desired_seats: 0, }), - timestamp: Some(TimestampConfig { - minimum_period: SECS_PER_BLOCK / 2, // due to the nature of aura the slots are 2*period - }), contracts: Some(ContractsConfig { current_schedule: Default::default(), gas_price: 1 * MILLICENTS, @@ -281,9 +278,6 @@ pub fn testnet_genesis( term_duration: 1000000, desired_seats: desired_seats, }), - timestamp: Some(TimestampConfig { - minimum_period: 2, // 2*2=4 second block time. - }), contracts: Some(ContractsConfig { current_schedule: contracts::Schedule { enable_println, // this should only be enabled on development chains @@ -342,23 +336,15 @@ pub(crate) mod tests { use service_test; use crate::service::Factory; - fn local_testnet_genesis_instant() -> GenesisConfig { - let mut genesis = local_testnet_genesis(); - genesis.timestamp = Some(TimestampConfig { minimum_period: 1 }); - genesis - } - fn local_testnet_genesis_instant_single() -> GenesisConfig { - let mut genesis = testnet_genesis( + testnet_genesis( vec![ get_authority_keys_from_seed("Alice"), ], get_account_id_from_seed("Alice"), None, false, - ); - genesis.timestamp = Some(TimestampConfig { minimum_period: 1 }); - genesis + ) } /// Local testnet config (single validator - Alice) @@ -377,7 +363,7 @@ pub(crate) mod tests { /// Local testnet config (multivalidator Alice + Bob) pub fn integration_test_config_with_two_authorities() -> ChainSpec { - ChainSpec::from_genesis("Integration Test", "test", local_testnet_genesis_instant, vec![], None, None, None, None) + ChainSpec::from_genesis("Integration Test", "test", local_testnet_genesis, vec![], None, None, None, None) } #[test] diff --git a/substrate/node/cli/src/factory_impl.rs b/substrate/node/cli/src/factory_impl.rs index 0d94610362..211d16f148 100644 --- a/substrate/node/cli/src/factory_impl.rs +++ b/substrate/node/cli/src/factory_impl.rs @@ -38,7 +38,7 @@ use inherents::InherentData; use timestamp; use finality_tracker; -// TODO get via api: >::minimum_period(). See #2587. +// TODO get via api: ::MinimumPeriod::get(). See #2587. const MINIMUM_PERIOD: u64 = 99; pub struct FactoryState { diff --git a/substrate/node/executor/src/lib.rs b/substrate/node/executor/src/lib.rs index ae306d33e5..fbd537f475 100644 --- a/substrate/node/executor/src/lib.rs +++ b/substrate/node/executor/src/lib.rs @@ -346,7 +346,6 @@ mod tests { collective_Instance1: Some(Default::default()), collective_Instance2: Some(Default::default()), elections: Some(Default::default()), - timestamp: Some(Default::default()), contracts: Some(ContractsConfig { current_schedule: Default::default(), gas_price: 1 * MILLICENTS, diff --git a/substrate/node/runtime/src/lib.rs b/substrate/node/runtime/src/lib.rs index 04bc98cecb..f361f5c4e5 100644 --- a/substrate/node/runtime/src/lib.rs +++ b/substrate/node/runtime/src/lib.rs @@ -69,7 +69,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { // and set impl_version to equal spec_version. If only runtime // implementation changes and behavior does not, then leave spec_version as // is and increment impl_version. - spec_version: 109, + spec_version: 110, impl_version: 110, apis: RUNTIME_API_VERSIONS, }; @@ -161,9 +161,13 @@ impl balances::Trait for Runtime { type TransactionByteFee = TransactionByteFee; } +parameter_types! { + pub const MinimumPeriod: u64 = SECS_PER_BLOCK / 2; +} impl timestamp::Trait for Runtime { type Moment = Moment; type OnTimestampSet = Aura; + type MinimumPeriod = MinimumPeriod; } parameter_types! { @@ -407,7 +411,7 @@ construct_runtime!( { System: system::{Module, Call, Storage, Config, Event}, Aura: aura::{Module, Call, Storage, Config, Inherent(Timestamp)}, - Timestamp: timestamp::{Module, Call, Storage, Config, Inherent}, + Timestamp: timestamp::{Module, Call, Storage, Inherent}, Authorship: authorship::{Module, Call, Storage}, Indices: indices, Balances: balances, diff --git a/substrate/srml/aura/src/lib.rs b/substrate/srml/aura/src/lib.rs index 3323d87bb5..1e92d411f4 100644 --- a/substrate/srml/aura/src/lib.rs +++ b/substrate/srml/aura/src/lib.rs @@ -52,7 +52,7 @@ pub use timestamp; use rstd::{result, prelude::*}; use parity_codec::Encode; -use srml_support::{decl_storage, decl_module, Parameter, storage::StorageValue}; +use srml_support::{decl_storage, decl_module, Parameter, storage::StorageValue, traits::Get}; use primitives::{ traits::{SaturatedConversion, Saturating, Zero, One, Member, TypedKey}, generic::DigestItem, @@ -243,7 +243,7 @@ impl Module { pub fn slot_duration() -> T::Moment { // we double the minimum block-period so each author can always propose within // the majority of its slot. - >::minimum_period().saturating_mul(2.into()) + ::MinimumPeriod::get().saturating_mul(2.into()) } fn on_timestamp_set(now: T::Moment, slot_duration: T::Moment) { diff --git a/substrate/srml/aura/src/mock.rs b/substrate/srml/aura/src/mock.rs index fad511baba..0cce522c76 100644 --- a/substrate/srml/aura/src/mock.rs +++ b/substrate/srml/aura/src/mock.rs @@ -37,6 +37,7 @@ pub struct Test; parameter_types! { pub const BlockHashCount: u64 = 250; + pub const MinimumPeriod: u64 = 1; } impl system::Trait for Test { @@ -55,6 +56,7 @@ impl system::Trait for Test { impl timestamp::Trait for Test { type Moment = u64; type OnTimestampSet = Aura; + type MinimumPeriod = MinimumPeriod; } impl Trait for Test { @@ -64,9 +66,6 @@ impl Trait for Test { pub fn new_test_ext(authorities: Vec) -> runtime_io::TestExternalities { let mut t = system::GenesisConfig::default().build_storage::().unwrap().0; - t.extend(timestamp::GenesisConfig::{ - minimum_period: 1, - }.build_storage().unwrap().0); t.extend(GenesisConfig::{ authorities: authorities.into_iter().map(|a| UintAuthorityId(a)).collect(), }.build_storage().unwrap().0); diff --git a/substrate/srml/babe/src/lib.rs b/substrate/srml/babe/src/lib.rs index 14f750f3e2..f1c8894a4d 100644 --- a/substrate/srml/babe/src/lib.rs +++ b/substrate/srml/babe/src/lib.rs @@ -21,7 +21,7 @@ pub use timestamp; use rstd::{result, prelude::*}; -use srml_support::{decl_storage, decl_module, StorageValue, traits::FindAuthor}; +use srml_support::{decl_storage, decl_module, StorageValue, traits::FindAuthor, traits::Get}; use timestamp::{OnTimestampSet, Trait}; use primitives::{generic::DigestItem, traits::{SaturatedConversion, Saturating, RandomnessBeacon}}; use primitives::ConsensusEngineId; @@ -193,7 +193,7 @@ impl Module { pub fn slot_duration() -> T::Moment { // we double the minimum block-period so each author can always propose within // the majority of their slot. - >::minimum_period().saturating_mul(2.into()) + ::MinimumPeriod::get().saturating_mul(2.into()) } fn change_authorities(new: Vec) { diff --git a/substrate/srml/contracts/src/tests.rs b/substrate/srml/contracts/src/tests.rs index b195a74bf4..bd7ed1f4ff 100644 --- a/substrate/srml/contracts/src/tests.rs +++ b/substrate/srml/contracts/src/tests.rs @@ -127,9 +127,13 @@ impl balances::Trait for Test { type TransactionBaseFee = BalancesTransactionBaseFee; type TransactionByteFee = BalancesTransactionByteFee; } +parameter_types! { + pub const MinimumPeriod: u64 = 1; +} impl timestamp::Trait for Test { type Moment = u64; type OnTimestampSet = (); + type MinimumPeriod = MinimumPeriod; } parameter_types! { pub const SignedClaimHandicap: u64 = 2; diff --git a/substrate/srml/session/src/historical.rs b/substrate/srml/session/src/historical.rs index 99d7b8572b..c6755c3ba3 100644 --- a/substrate/srml/session/src/historical.rs +++ b/substrate/srml/session/src/historical.rs @@ -327,9 +327,6 @@ mod tests { fn new_test_ext() -> runtime_io::TestExternalities { let mut t = system::GenesisConfig::default().build_storage::().unwrap().0; - t.extend(timestamp::GenesisConfig:: { - minimum_period: 5, - }.build_storage().unwrap().0); let (storage, _child_storage) = crate::GenesisConfig:: { keys: NEXT_VALIDATORS.with(|l| l.borrow().iter().cloned().map(|i| (i, UintAuthorityId(i))).collect() diff --git a/substrate/srml/session/src/lib.rs b/substrate/srml/session/src/lib.rs index 1dfd0828cd..546513c953 100644 --- a/substrate/srml/session/src/lib.rs +++ b/substrate/srml/session/src/lib.rs @@ -565,9 +565,6 @@ mod tests { fn new_test_ext() -> runtime_io::TestExternalities { let mut t = system::GenesisConfig::default().build_storage::().unwrap(); - timestamp::GenesisConfig:: { - minimum_period: 5, - }.assimilate_storage(&mut t.0, &mut t.1).unwrap(); GenesisConfig:: { keys: NEXT_VALIDATORS.with(|l| l.borrow().iter().cloned().map(|i| (i, UintAuthorityId(i))).collect() diff --git a/substrate/srml/session/src/mock.rs b/substrate/srml/session/src/mock.rs index adb3772051..13d824c807 100644 --- a/substrate/srml/session/src/mock.rs +++ b/substrate/srml/session/src/mock.rs @@ -109,6 +109,7 @@ pub fn set_next_validators(next: Vec) { pub struct Test; parameter_types! { pub const BlockHashCount: u64 = 250; + pub const MinimumPeriod: u64 = 5; } impl system::Trait for Test { type Origin = Origin; @@ -125,6 +126,7 @@ impl system::Trait for Test { impl timestamp::Trait for Test { type Moment = u64; type OnTimestampSet = (); + type MinimumPeriod = MinimumPeriod; } diff --git a/substrate/srml/staking/src/mock.rs b/substrate/srml/staking/src/mock.rs index abad1752a6..246b6f96be 100644 --- a/substrate/srml/staking/src/mock.rs +++ b/substrate/srml/staking/src/mock.rs @@ -140,9 +140,13 @@ impl session::historical::Trait for Test { type FullIdentificationOf = crate::ExposureOf; } +parameter_types! { + pub const MinimumPeriod: u64 = 5; +} impl timestamp::Trait for Test { type Moment = u64; type OnTimestampSet = (); + type MinimumPeriod = MinimumPeriod; } parameter_types! { pub const SessionsPerEra: session::SessionIndex = 3; @@ -279,10 +283,6 @@ impl ExtBuilder { invulnerables: vec![], }.assimilate_storage(&mut t, &mut c); - let _ = timestamp::GenesisConfig::{ - minimum_period: 5, - }.assimilate_storage(&mut t, &mut c); - let _ = session::GenesisConfig:: { keys: validators.iter().map(|x| (*x, UintAuthorityId(*x))).collect(), }.assimilate_storage(&mut t, &mut c); diff --git a/substrate/srml/timestamp/src/lib.rs b/substrate/srml/timestamp/src/lib.rs index 7bffac0db9..48a0d04c32 100644 --- a/substrate/srml/timestamp/src/lib.rs +++ b/substrate/srml/timestamp/src/lib.rs @@ -44,7 +44,10 @@ //! //! * `get` - Gets the current time for the current block. If this function is called prior to //! setting the timestamp, it will return the timestamp of the previous block. -//! * `minimum_period` - Gets the minimum (and advised) period between blocks for the chain. +//! +//! ### Trait Getters +//! +//! * `MinimumPeriod` - Gets the minimum (and advised) period between blocks for the chain. //! //! ## Usage //! @@ -93,8 +96,7 @@ use parity_codec::Encode; use parity_codec::Decode; #[cfg(feature = "std")] use inherents::ProvideInherentData; -use srml_support::{StorageValue, Parameter, decl_storage, decl_module}; -use srml_support::for_each_tuple; +use srml_support::{StorageValue, Parameter, decl_storage, decl_module, for_each_tuple, traits::Get}; use runtime_primitives::traits::{SimpleArithmetic, Zero, SaturatedConversion}; use system::ensure_none; use inherents::{RuntimeString, InherentIdentifier, ProvideInherent, IsFatalError, InherentData}; @@ -208,23 +210,36 @@ pub trait Trait: system::Trait { /// Something which can be notified when the timestamp is set. Set this to `()` if not needed. type OnTimestampSet: OnTimestampSet; + + /// The minimum period between blocks. Beware that this is different to the *expected* period + /// that the block production apparatus provides. Your chosen consensus system will generally + /// work with this to determine a sensible block time. e.g. For Aura, it will be double this + /// period on default settings. + type MinimumPeriod: Get; } decl_module! { pub struct Module for enum Call where origin: T::Origin { + /// The minimum period between blocks. Beware that this is different to the *expected* period + /// that the block production apparatus provides. Your chosen consensus system will generally + /// work with this to determine a sensible block time. e.g. For Aura, it will be double this + /// period on default settings. + const MinimumPeriod: T::Moment = T::MinimumPeriod::get(); + /// Set the current time. /// - /// This call should be invoked exactly once per block. It will panic at the finalization phase, - /// if this call hasn't been invoked by that time. + /// This call should be invoked exactly once per block. It will panic at the finalization + /// phase, if this call hasn't been invoked by that time. /// - /// The timestamp should be greater than the previous one by the amount specified by `minimum_period`. + /// The timestamp should be greater than the previous one by the amount specified by + /// `MinimumPeriod`. /// /// The dispatch origin for this call must be `Inherent`. fn set(origin, #[compact] now: T::Moment) { ensure_none(origin)?; assert!(!::DidUpdate::exists(), "Timestamp must be updated only once in the block"); assert!( - Self::now().is_zero() || now >= Self::now() + >::get(), + Self::now().is_zero() || now >= Self::now() + T::MinimumPeriod::get(), "Timestamp must increment by at least between sequential blocks" ); ::Now::put(now.clone()); @@ -233,16 +248,6 @@ decl_module! { >::on_timestamp_set(now); } - // Manage upgrade. Remove after all networks upgraded. - // TODO: #2133 - fn on_initialize() { - if let Some(period) = >::take() { - if !>::exists() { - >::put(period) - } - } - } - fn on_finalize() { assert!(::DidUpdate::take(), "Timestamp must be updated once in the block"); } @@ -254,16 +259,6 @@ decl_storage! { /// Current time for the current block. pub Now get(now) build(|_| 0.into()): T::Moment; - /// Old storage item provided for compatibility. Remove after all networks upgraded. - // TODO: #2133 - pub BlockPeriod: Option; - - /// The minimum period between blocks. Beware that this is different to the *expected* period - /// that the block production apparatus provides. Your chosen consensus system will generally - /// work with this to determine a sensible block time. e.g. For Aura, it will be double this - /// period on default settings. - pub MinimumPeriod get(minimum_period) config(): T::Moment = 3.into(); - /// Did the timestamp get updated in this block? DidUpdate: bool; } @@ -301,7 +296,7 @@ impl ProvideInherent for Module { .expect("Gets and decodes timestamp inherent data") .saturated_into(); - let next_time = cmp::max(data, Self::now() + >::get()); + let next_time = cmp::max(data, Self::now() + T::MinimumPeriod::get()); Some(Call::set(next_time.into())) } @@ -315,7 +310,7 @@ impl ProvideInherent for Module { let data = extract_inherent_data(data).map_err(|e| InherentError::Other(e))?; - let minimum = (Self::now() + >::get()).saturated_into::(); + let minimum = (Self::now() + T::MinimumPeriod::get()).saturated_into::(); if t > data + MAX_TIMESTAMP_DRIFT { Err(InherentError::Other("Timestamp too far in future to accept".into())) } else if t < minimum { @@ -356,19 +351,19 @@ mod tests { type Event = (); type BlockHashCount = BlockHashCount; } + parameter_types! { + pub const MinimumPeriod: u64 = 5; + } impl Trait for Test { type Moment = u64; type OnTimestampSet = (); + type MinimumPeriod = MinimumPeriod; } type Timestamp = Module; #[test] fn timestamp_works() { - let mut t = system::GenesisConfig::default().build_storage::().unwrap(); - GenesisConfig:: { - minimum_period: 5, - }.assimilate_storage(&mut t.0, &mut t.1).unwrap(); - + let t = system::GenesisConfig::default().build_storage::().unwrap(); with_externalities(&mut TestExternalities::new_with_children(t), || { Timestamp::set_timestamp(42); assert_ok!(Timestamp::dispatch(Call::set(69), Origin::NONE)); @@ -379,11 +374,7 @@ mod tests { #[test] #[should_panic(expected = "Timestamp must be updated only once in the block")] fn double_timestamp_should_fail() { - let mut t = system::GenesisConfig::default().build_storage::().unwrap(); - GenesisConfig:: { - minimum_period: 5, - }.assimilate_storage(&mut t.0, &mut t.1).unwrap(); - + let t = system::GenesisConfig::default().build_storage::().unwrap(); with_externalities(&mut TestExternalities::new_with_children(t), || { Timestamp::set_timestamp(42); assert_ok!(Timestamp::dispatch(Call::set(69), Origin::NONE)); @@ -394,11 +385,7 @@ mod tests { #[test] #[should_panic(expected = "Timestamp must increment by at least between sequential blocks")] fn block_period_minimum_enforced() { - let mut t = system::GenesisConfig::default().build_storage::().unwrap(); - GenesisConfig:: { - minimum_period: 5, - }.assimilate_storage(&mut t.0, &mut t.1).unwrap(); - + let t = system::GenesisConfig::default().build_storage::().unwrap(); with_externalities(&mut TestExternalities::new_with_children(t), || { Timestamp::set_timestamp(42); let _ = Timestamp::dispatch(Call::set(46), Origin::NONE);