Migrate pallet-timestamp to pallet attribute macro. (#8078)

* Migrate pallet-timestamp to pallet attribute macro.

* Migrate inherent.

* Unify private visbility.

* Update benchmarking.

* Update storage usages.
This commit is contained in:
Shaun Wang
2021-02-11 03:00:41 +13:00
committed by GitHub
parent c763df4bb1
commit 3fbe22f552
2 changed files with 113 additions and 105 deletions
@@ -33,7 +33,7 @@ benchmarks! {
set { set {
let t = MAX_TIME; let t = MAX_TIME;
// Ignore write to `DidUpdate` since it transient. // Ignore write to `DidUpdate` since it transient.
let did_update_key = crate::DidUpdate::hashed_key().to_vec(); let did_update_key = crate::DidUpdate::<T>::hashed_key().to_vec();
frame_benchmarking::benchmarking::add_to_whitelist(TrackedStorageKey { frame_benchmarking::benchmarking::add_to_whitelist(TrackedStorageKey {
key: did_update_key, key: did_update_key,
has_been_read: false, has_been_read: false,
@@ -47,13 +47,13 @@ benchmarks! {
on_finalize { on_finalize {
let t = MAX_TIME; let t = MAX_TIME;
Timestamp::<T>::set(RawOrigin::None.into(), t.into())?; Timestamp::<T>::set(RawOrigin::None.into(), t.into())?;
ensure!(DidUpdate::exists(), "Time was not set."); ensure!(DidUpdate::<T>::exists(), "Time was not set.");
// Ignore read/write to `DidUpdate` since it is transient. // Ignore read/write to `DidUpdate` since it is transient.
let did_update_key = crate::DidUpdate::hashed_key().to_vec(); let did_update_key = crate::DidUpdate::<T>::hashed_key().to_vec();
frame_benchmarking::benchmarking::add_to_whitelist(did_update_key.into()); frame_benchmarking::benchmarking::add_to_whitelist(did_update_key.into());
}: { Timestamp::<T>::on_finalize(t.into()); } }: { Timestamp::<T>::on_finalize(t.into()); }
verify { verify {
ensure!(!DidUpdate::exists(), "Time was not removed."); ensure!(!DidUpdate::<T>::exists(), "Time was not removed.");
} }
} }
+109 -101
View File
@@ -15,23 +15,23 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
//! # Timestamp Module //! # Timestamp Pallet
//! //!
//! The Timestamp module provides functionality to get and set the on-chain time. //! The Timestamp pallet provides functionality to get and set the on-chain time.
//! //!
//! - [`timestamp::Config`](./trait.Config.html) //! - [`timestamp::Config`](./trait.Config.html)
//! - [`Call`](./enum.Call.html) //! - [`Call`](./enum.Call.html)
//! - [`Module`](./struct.Module.html) //! - [`Pallet`](./struct.Pallet.html)
//! //!
//! ## Overview //! ## Overview
//! //!
//! The Timestamp module allows the validators to set and validate a timestamp with each block. //! The Timestamp pallet allows the validators to set and validate a timestamp with each block.
//! //!
//! It uses inherents for timestamp data, which is provided by the block author and validated/verified //! It uses inherents for timestamp data, which is provided by the block author and validated/verified
//! by other validators. The timestamp can be set only once per block and must be set each block. //! by other validators. The timestamp can be set only once per block and must be set each block.
//! There could be a constraint on how much time must pass before setting the new timestamp. //! There could be a constraint on how much time must pass before setting the new timestamp.
//! //!
//! **NOTE:** The Timestamp module is the recommended way to query the on-chain time instead of using //! **NOTE:** The Timestamp pallet is the recommended way to query the on-chain time instead of using
//! an approach based on block numbers. The block number based time measurement can cause issues //! an approach based on block numbers. The block number based time measurement can cause issues
//! because of cumulative calculation errors and hence should be avoided. //! because of cumulative calculation errors and hence should be avoided.
//! //!
@@ -52,11 +52,11 @@
//! //!
//! ## Usage //! ## Usage
//! //!
//! The following example shows how to use the Timestamp module in your custom module to query the current timestamp. //! The following example shows how to use the Timestamp pallet in your custom pallet to query the current timestamp.
//! //!
//! ### Prerequisites //! ### Prerequisites
//! //!
//! Import the Timestamp module into your custom module and derive the module configuration //! Import the Timestamp pallet into your custom pallet and derive the pallet configuration
//! trait from the timestamp trait. //! trait from the timestamp trait.
//! //!
//! ### Get current timestamp //! ### Get current timestamp
@@ -83,10 +83,10 @@
//! //!
//! ### Example from the FRAME //! ### Example from the FRAME
//! //!
//! The [Session module](https://github.com/paritytech/substrate/blob/master/frame/session/src/lib.rs) uses //! The [Session pallet](https://github.com/paritytech/substrate/blob/master/frame/session/src/lib.rs) uses
//! the Timestamp module for session management. //! the Timestamp pallet for session management.
//! //!
//! ## Related Modules //! ## Related Pallets
//! //!
//! * [Session](../pallet_session/index.html) //! * [Session](../pallet_session/index.html)
@@ -96,54 +96,83 @@ mod benchmarking;
pub mod weights; pub mod weights;
use sp_std::{result, cmp}; use sp_std::{result, cmp};
use sp_inherents::{ProvideInherent, InherentData, InherentIdentifier}; use sp_inherents::InherentData;
#[cfg(feature = "std")] #[cfg(feature = "std")]
use frame_support::debug; use frame_support::debug;
use frame_support::{ use frame_support::traits::{Time, UnixTime};
Parameter, decl_storage, decl_module,
traits::{Time, UnixTime, Get},
weights::{DispatchClass, Weight},
};
use sp_runtime::{ use sp_runtime::{
RuntimeString, RuntimeString,
traits::{ traits::{
AtLeast32Bit, Zero, SaturatedConversion, Scale, AtLeast32Bit, Zero, SaturatedConversion, Scale,
} }
}; };
use frame_system::ensure_none;
use sp_timestamp::{ use sp_timestamp::{
InherentError, INHERENT_IDENTIFIER, InherentType, InherentError, INHERENT_IDENTIFIER, InherentType,
OnTimestampSet, OnTimestampSet,
}; };
pub use weights::WeightInfo; pub use weights::WeightInfo;
/// The module configuration trait pub use pallet::*;
pub trait Config: frame_system::Config {
/// Type used for expressing timestamp.
type Moment: Parameter + Default + AtLeast32Bit
+ Scale<Self::BlockNumber, Output = Self::Moment> + Copy;
/// Something which can be notified when the timestamp is set. Set this to `()` if not needed. #[frame_support::pallet]
type OnTimestampSet: OnTimestampSet<Self::Moment>; pub mod pallet {
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
use super::*;
/// The minimum period between blocks. Beware that this is different to the *expected* period /// The pallet configuration trait
/// that the block production apparatus provides. Your chosen consensus system will generally #[pallet::config]
/// work with this to determine a sensible block time. e.g. For Aura, it will be double this pub trait Config: frame_system::Config {
/// period on default settings. /// Type used for expressing timestamp.
type MinimumPeriod: Get<Self::Moment>; type Moment: Parameter + Default + AtLeast32Bit
+ Scale<Self::BlockNumber, Output = Self::Moment> + Copy;
/// Weight information for extrinsics in this pallet. /// Something which can be notified when the timestamp is set. Set this to `()` if not needed.
type WeightInfo: WeightInfo; type OnTimestampSet: OnTimestampSet<Self::Moment>;
}
decl_module! {
pub struct Module<T: Config> for enum Call where origin: T::Origin {
/// The minimum period between blocks. Beware that this is different to the *expected* period /// 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 /// 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 /// work with this to determine a sensible block time. e.g. For Aura, it will be double this
/// period on default settings. /// period on default settings.
const MinimumPeriod: T::Moment = T::MinimumPeriod::get(); #[pallet::constant]
type MinimumPeriod: Get<Self::Moment>;
/// Weight information for extrinsics in this pallet.
type WeightInfo: WeightInfo;
}
#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
pub struct Pallet<T>(PhantomData<T>);
/// Current time for the current block.
#[pallet::storage]
#[pallet::getter(fn now)]
pub type Now<T: Config> = StorageValue<_, T::Moment, ValueQuery>;
/// Did the timestamp get updated in this block?
#[pallet::storage]
pub(super) type DidUpdate<T: Config> = StorageValue<_, bool, ValueQuery>;
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
/// dummy `on_initialize` to return the weight used in `on_finalize`.
fn on_initialize(_n: BlockNumberFor<T>) -> Weight {
// weight of `on_finalize`
T::WeightInfo::on_finalize()
}
/// # <weight>
/// - `O(1)`
/// - 1 storage deletion (codec `O(1)`).
/// # </weight>
fn on_finalize(_n: BlockNumberFor<T>) {
assert!(DidUpdate::<T>::take(), "Timestamp must be updated once in the block");
}
}
#[pallet::call]
impl<T: Config> Pallet<T> {
/// Set the current time. /// Set the current time.
/// ///
/// This call should be invoked exactly once per block. It will panic at the finalization /// This call should be invoked exactly once per block. It will panic at the finalization
@@ -159,51 +188,65 @@ decl_module! {
/// - 1 storage read and 1 storage mutation (codec `O(1)`). (because of `DidUpdate::take` in `on_finalize`) /// - 1 storage read and 1 storage mutation (codec `O(1)`). (because of `DidUpdate::take` in `on_finalize`)
/// - 1 event handler `on_timestamp_set`. Must be `O(1)`. /// - 1 event handler `on_timestamp_set`. Must be `O(1)`.
/// # </weight> /// # </weight>
#[weight = ( #[pallet::weight((
T::WeightInfo::set(), T::WeightInfo::set(),
DispatchClass::Mandatory DispatchClass::Mandatory
)] ))]
fn set(origin, #[compact] now: T::Moment) { pub(super) fn set(origin: OriginFor<T>, #[pallet::compact] now: T::Moment) -> DispatchResultWithPostInfo {
ensure_none(origin)?; ensure_none(origin)?;
assert!(!<Self as Store>::DidUpdate::exists(), "Timestamp must be updated only once in the block"); assert!(!DidUpdate::<T>::exists(), "Timestamp must be updated only once in the block");
let prev = Self::now(); let prev = Self::now();
assert!( assert!(
prev.is_zero() || now >= prev + T::MinimumPeriod::get(), prev.is_zero() || now >= prev + T::MinimumPeriod::get(),
"Timestamp must increment by at least <MinimumPeriod> between sequential blocks" "Timestamp must increment by at least <MinimumPeriod> between sequential blocks"
); );
<Self as Store>::Now::put(now); Now::<T>::put(now);
<Self as Store>::DidUpdate::put(true); DidUpdate::<T>::put(true);
<T::OnTimestampSet as OnTimestampSet<_>>::on_timestamp_set(now); <T::OnTimestampSet as OnTimestampSet<_>>::on_timestamp_set(now);
Ok(().into())
}
}
#[pallet::inherent]
impl<T: Config> ProvideInherent for Pallet<T> {
type Call = Call<T>;
type Error = InherentError;
const INHERENT_IDENTIFIER: InherentIdentifier = INHERENT_IDENTIFIER;
fn create_inherent(data: &InherentData) -> Option<Self::Call> {
let data: T::Moment = extract_inherent_data(data)
.expect("Gets and decodes timestamp inherent data")
.saturated_into();
let next_time = cmp::max(data, Self::now() + T::MinimumPeriod::get());
Some(Call::set(next_time.into()))
} }
/// dummy `on_initialize` to return the weight used in `on_finalize`. fn check_inherent(call: &Self::Call, data: &InherentData) -> result::Result<(), Self::Error> {
fn on_initialize() -> Weight { const MAX_TIMESTAMP_DRIFT_MILLIS: u64 = 30 * 1000;
// weight of `on_finalize`
T::WeightInfo::on_finalize()
}
/// # <weight> let t: u64 = match call {
/// - `O(1)` Call::set(ref t) => t.clone().saturated_into::<u64>(),
/// - 1 storage deletion (codec `O(1)`). _ => return Ok(()),
/// # </weight> };
fn on_finalize() {
assert!(<Self as Store>::DidUpdate::take(), "Timestamp must be updated once in the block"); let data = extract_inherent_data(data).map_err(|e| InherentError::Other(e))?;
let minimum = (Self::now() + T::MinimumPeriod::get()).saturated_into::<u64>();
if t > data + MAX_TIMESTAMP_DRIFT_MILLIS {
Err(InherentError::Other("Timestamp too far in future to accept".into()))
} else if t < minimum {
Err(InherentError::ValidAtTimestamp(minimum))
} else {
Ok(())
}
} }
} }
} }
decl_storage! { impl<T: Config> Pallet<T> {
trait Store for Module<T: Config> as Timestamp {
/// Current time for the current block.
pub Now get(fn now): T::Moment;
/// Did the timestamp get updated in this block?
DidUpdate: bool;
}
}
impl<T: Config> Module<T> {
/// Get the current time for the current block. /// Get the current time for the current block.
/// ///
/// NOTE: if this function is called prior to setting the timestamp, /// NOTE: if this function is called prior to setting the timestamp,
@@ -215,7 +258,7 @@ impl<T: Config> Module<T> {
/// Set the timestamp to something in particular. Only used for tests. /// Set the timestamp to something in particular. Only used for tests.
#[cfg(feature = "std")] #[cfg(feature = "std")]
pub fn set_timestamp(now: T::Moment) { pub fn set_timestamp(now: T::Moment) {
<Self as Store>::Now::put(now); Now::<T>::put(now);
} }
} }
@@ -225,42 +268,7 @@ fn extract_inherent_data(data: &InherentData) -> Result<InherentType, RuntimeStr
.ok_or_else(|| "Timestamp inherent data is not provided.".into()) .ok_or_else(|| "Timestamp inherent data is not provided.".into())
} }
impl<T: Config> ProvideInherent for Module<T> { impl<T: Config> Time for Pallet<T> {
type Call = Call<T>;
type Error = InherentError;
const INHERENT_IDENTIFIER: InherentIdentifier = INHERENT_IDENTIFIER;
fn create_inherent(data: &InherentData) -> Option<Self::Call> {
let data: T::Moment = extract_inherent_data(data)
.expect("Gets and decodes timestamp inherent data")
.saturated_into();
let next_time = cmp::max(data, Self::now() + T::MinimumPeriod::get());
Some(Call::set(next_time.into()))
}
fn check_inherent(call: &Self::Call, data: &InherentData) -> result::Result<(), Self::Error> {
const MAX_TIMESTAMP_DRIFT_MILLIS: u64 = 30 * 1000;
let t: u64 = match call {
Call::set(ref t) => t.clone().saturated_into::<u64>(),
_ => return Ok(()),
};
let data = extract_inherent_data(data).map_err(|e| InherentError::Other(e))?;
let minimum = (Self::now() + T::MinimumPeriod::get()).saturated_into::<u64>();
if t > data + MAX_TIMESTAMP_DRIFT_MILLIS {
Err(InherentError::Other("Timestamp too far in future to accept".into()))
} else if t < minimum {
Err(InherentError::ValidAtTimestamp(minimum))
} else {
Ok(())
}
}
}
impl<T: Config> Time for Module<T> {
type Moment = T::Moment; type Moment = T::Moment;
/// Before the first set of now with inherent the value returned is zero. /// Before the first set of now with inherent the value returned is zero.
@@ -272,7 +280,7 @@ impl<T: Config> Time for Module<T> {
/// Before the timestamp inherent is applied, it returns the time of previous block. /// Before the timestamp inherent is applied, it returns the time of previous block.
/// ///
/// On genesis the time returned is not valid. /// On genesis the time returned is not valid.
impl<T: Config> UnixTime for Module<T> { impl<T: Config> UnixTime for Pallet<T> {
fn now() -> core::time::Duration { fn now() -> core::time::Duration {
// now is duration since unix epoch in millisecond as documented in // now is duration since unix epoch in millisecond as documented in
// `sp_timestamp::InherentDataProvider`. // `sp_timestamp::InherentDataProvider`.