Convert unnecessary storage item to static. (#3093)

* Convert unnecessary storage item to static.

* Polish

* 6 second blocks.

* Compile fixes

* Bump runtime

* Fix

* Another fix

* Import `srml_support::traits::Get`

* Export MinimumPeriod from `decl_module!`

* Remove `config` from Timestamp

* Clean up warnings
This commit is contained in:
Gavin Wood
2019-07-11 23:03:33 +02:00
committed by GitHub
parent b3dc472a9b
commit e4d4548121
15 changed files with 65 additions and 90 deletions
+2 -2
View File
@@ -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<T: Trait> Module<T> {
pub fn slot_duration() -> T::Moment {
// we double the minimum block-period so each author can always propose within
// the majority of its slot.
<timestamp::Module<T>>::minimum_period().saturating_mul(2.into())
<T as timestamp::Trait>::MinimumPeriod::get().saturating_mul(2.into())
}
fn on_timestamp_set<H: HandleReport>(now: T::Moment, slot_duration: T::Moment) {
+2 -3
View File
@@ -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<u64>) -> runtime_io::TestExternalities<Blake2Hasher> {
let mut t = system::GenesisConfig::default().build_storage::<Test>().unwrap().0;
t.extend(timestamp::GenesisConfig::<Test>{
minimum_period: 1,
}.build_storage().unwrap().0);
t.extend(GenesisConfig::<Test>{
authorities: authorities.into_iter().map(|a| UintAuthorityId(a)).collect(),
}.build_storage().unwrap().0);
+2 -2
View File
@@ -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<T: Trait> Module<T> {
pub fn slot_duration() -> T::Moment {
// we double the minimum block-period so each author can always propose within
// the majority of their slot.
<timestamp::Module<T>>::minimum_period().saturating_mul(2.into())
<T as timestamp::Trait>::MinimumPeriod::get().saturating_mul(2.into())
}
fn change_authorities(new: Vec<AuthorityId>) {
+4
View File
@@ -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;
-3
View File
@@ -327,9 +327,6 @@ mod tests {
fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
let mut t = system::GenesisConfig::default().build_storage::<Test>().unwrap().0;
t.extend(timestamp::GenesisConfig::<Test> {
minimum_period: 5,
}.build_storage().unwrap().0);
let (storage, _child_storage) = crate::GenesisConfig::<Test> {
keys: NEXT_VALIDATORS.with(|l|
l.borrow().iter().cloned().map(|i| (i, UintAuthorityId(i))).collect()
-3
View File
@@ -565,9 +565,6 @@ mod tests {
fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
let mut t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
timestamp::GenesisConfig::<Test> {
minimum_period: 5,
}.assimilate_storage(&mut t.0, &mut t.1).unwrap();
GenesisConfig::<Test> {
keys: NEXT_VALIDATORS.with(|l|
l.borrow().iter().cloned().map(|i| (i, UintAuthorityId(i))).collect()
+2
View File
@@ -109,6 +109,7 @@ pub fn set_next_validators(next: Vec<u64>) {
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;
}
+4 -4
View File
@@ -140,9 +140,13 @@ impl session::historical::Trait for Test {
type FullIdentificationOf = crate::ExposureOf<Test>;
}
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::<Test>{
minimum_period: 5,
}.assimilate_storage(&mut t, &mut c);
let _ = session::GenesisConfig::<Test> {
keys: validators.iter().map(|x| (*x, UintAuthorityId(*x))).collect(),
}.assimilate_storage(&mut t, &mut c);
+31 -44
View File
@@ -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<Self::Moment>;
/// 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<Self::Moment>;
}
decl_module! {
pub struct Module<T: Trait> 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!(!<Self as Store>::DidUpdate::exists(), "Timestamp must be updated only once in the block");
assert!(
Self::now().is_zero() || now >= Self::now() + <MinimumPeriod<T>>::get(),
Self::now().is_zero() || now >= Self::now() + T::MinimumPeriod::get(),
"Timestamp must increment by at least <MinimumPeriod> between sequential blocks"
);
<Self as Store>::Now::put(now.clone());
@@ -233,16 +248,6 @@ decl_module! {
<T::OnTimestampSet as OnTimestampSet<_>>::on_timestamp_set(now);
}
// Manage upgrade. Remove after all networks upgraded.
// TODO: #2133
fn on_initialize() {
if let Some(period) = <BlockPeriod<T>>::take() {
if !<MinimumPeriod<T>>::exists() {
<MinimumPeriod<T>>::put(period)
}
}
}
fn on_finalize() {
assert!(<Self as Store>::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<T::Moment>;
/// 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<T: Trait> ProvideInherent for Module<T> {
.expect("Gets and decodes timestamp inherent data")
.saturated_into();
let next_time = cmp::max(data, Self::now() + <MinimumPeriod<T>>::get());
let next_time = cmp::max(data, Self::now() + T::MinimumPeriod::get());
Some(Call::set(next_time.into()))
}
@@ -315,7 +310,7 @@ impl<T: Trait> ProvideInherent for Module<T> {
let data = extract_inherent_data(data).map_err(|e| InherentError::Other(e))?;
let minimum = (Self::now() + <MinimumPeriod<T>>::get()).saturated_into::<u64>();
let minimum = (Self::now() + T::MinimumPeriod::get()).saturated_into::<u64>();
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>;
#[test]
fn timestamp_works() {
let mut t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
GenesisConfig::<Test> {
minimum_period: 5,
}.assimilate_storage(&mut t.0, &mut t.1).unwrap();
let t = system::GenesisConfig::default().build_storage::<Test>().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::<Test>().unwrap();
GenesisConfig::<Test> {
minimum_period: 5,
}.assimilate_storage(&mut t.0, &mut t.1).unwrap();
let t = system::GenesisConfig::default().build_storage::<Test>().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 <MinimumPeriod> between sequential blocks")]
fn block_period_minimum_enforced() {
let mut t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
GenesisConfig::<Test> {
minimum_period: 5,
}.assimilate_storage(&mut t.0, &mut t.1).unwrap();
let t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
with_externalities(&mut TestExternalities::new_with_children(t), || {
Timestamp::set_timestamp(42);
let _ = Timestamp::dispatch(Call::set(46), Origin::NONE);