#![cfg_attr(not(feature = "std"), no_std)]
mod benchmarking;
#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;
pub mod weights;
use frame_support::traits::{OnTimestampSet, Time, UnixTime};
use sp_runtime::traits::{AtLeast32Bit, SaturatedConversion, Scale, Zero};
use sp_std::{cmp, result};
use sp_timestamp::{InherentError, InherentType, INHERENT_IDENTIFIER};
pub use weights::WeightInfo;
pub use pallet::*;
#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
#[pallet::config]
pub trait Config: frame_system::Config {
type Moment: Parameter
+ Default
+ AtLeast32Bit
+ Scale<Self::BlockNumber, Output = Self::Moment>
+ Copy
+ MaxEncodedLen
+ scale_info::StaticTypeInfo;
type OnTimestampSet: OnTimestampSet<Self::Moment>;
#[pallet::constant]
type MinimumPeriod: Get<Self::Moment>;
type WeightInfo: WeightInfo;
}
#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
pub struct Pallet<T>(PhantomData<T>);
#[pallet::storage]
#[pallet::getter(fn now)]
pub type Now<T: Config> = StorageValue<_, T::Moment, ValueQuery>;
#[pallet::storage]
pub(super) type DidUpdate<T: Config> = StorageValue<_, bool, ValueQuery>;
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_initialize(_n: BlockNumberFor<T>) -> Weight {
T::WeightInfo::on_finalize()
}
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> {
#[pallet::call_index(0)]
#[pallet::weight((
T::WeightInfo::set(),
DispatchClass::Mandatory
))]
pub fn set(origin: OriginFor<T>, #[pallet::compact] now: T::Moment) -> DispatchResult {
ensure_none(origin)?;
assert!(!DidUpdate::<T>::exists(), "Timestamp must be updated only once in the block");
let prev = Self::now();
assert!(
prev.is_zero() || now >= prev + T::MinimumPeriod::get(),
"Timestamp must increment by at least <MinimumPeriod> between sequential blocks"
);
Now::<T>::put(now);
DidUpdate::<T>::put(true);
<T::OnTimestampSet as OnTimestampSet<_>>::on_timestamp_set(now);
Ok(())
}
}
#[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 inherent_data = data
.get_data::<InherentType>(&INHERENT_IDENTIFIER)
.expect("Timestamp inherent data not correctly encoded")
.expect("Timestamp inherent data must be provided");
let data = (*inherent_data).saturated_into::<T::Moment>();
let next_time = cmp::max(data, Self::now() + T::MinimumPeriod::get());
Some(Call::set { now: next_time })
}
fn check_inherent(
call: &Self::Call,
data: &InherentData,
) -> result::Result<(), Self::Error> {
const MAX_TIMESTAMP_DRIFT_MILLIS: sp_timestamp::Timestamp =
sp_timestamp::Timestamp::new(30 * 1000);
let t: u64 = match call {
Call::set { ref now } => (*now).saturated_into::<u64>(),
_ => return Ok(()),
};
let data = data
.get_data::<InherentType>(&INHERENT_IDENTIFIER)
.expect("Timestamp inherent data not correctly encoded")
.expect("Timestamp inherent data must be provided");
let minimum = (Self::now() + T::MinimumPeriod::get()).saturated_into::<u64>();
if t > *(data + MAX_TIMESTAMP_DRIFT_MILLIS) {
Err(InherentError::TooFarInFuture)
} else if t < minimum {
Err(InherentError::ValidAtTimestamp(minimum.into()))
} else {
Ok(())
}
}
fn is_inherent(call: &Self::Call) -> bool {
matches!(call, Call::set { .. })
}
}
}
impl<T: Config> Pallet<T> {
pub fn get() -> T::Moment {
Self::now()
}
#[cfg(any(feature = "runtime-benchmarks", feature = "std"))]
pub fn set_timestamp(now: T::Moment) {
Now::<T>::put(now);
DidUpdate::<T>::put(true);
<T::OnTimestampSet as OnTimestampSet<_>>::on_timestamp_set(now);
}
}
impl<T: Config> Time for Pallet<T> {
type Moment = T::Moment;
fn now() -> Self::Moment {
Self::now()
}
}
impl<T: Config> UnixTime for Pallet<T> {
fn now() -> core::time::Duration {
let now = Self::now();
sp_std::if_std! {
if now == T::Moment::zero() {
log::error!(
target: "runtime::timestamp",
"`pallet_timestamp::UnixTime::now` is called at genesis, invalid value returned: 0",
);
}
}
core::time::Duration::from_millis(now.saturated_into::<u64>())
}
}