Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove pallet::getter usage from pallet-timestamp #3374

Merged
merged 12 commits into from
Jun 17, 2024
4 changes: 2 additions & 2 deletions substrate/frame/timestamp/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use frame_support::{ensure, traits::OnFinalize};
use frame_system::RawOrigin;
use sp_storage::TrackedStorageKey;

use crate::Pallet as Timestamp;
use crate::{Now, Pallet as Timestamp};

const MAX_TIME: u32 = 100;

Expand All @@ -42,7 +42,7 @@ benchmarks! {
});
}: _(RawOrigin::None, t.into())
verify {
ensure!(Timestamp::<T>::now() == t.into(), "Time was not set.");
ensure!(Now::<T>::get() == t.into(), "Time was not set.");
}

on_finalize {
Expand Down
13 changes: 6 additions & 7 deletions substrate/frame/timestamp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,6 @@ pub mod pallet {

/// The current time for the current block.
#[pallet::storage]
#[pallet::getter(fn now)]
pub type Now<T: Config> = StorageValue<_, T::Moment, ValueQuery>;

/// Whether the timestamp has been updated in this block.
Expand Down Expand Up @@ -261,7 +260,7 @@ pub mod pallet {
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();
let prev = Now::<T>::get();
assert!(
prev.is_zero() || now >= prev + T::MinimumPeriod::get(),
"Timestamp must increment by at least <MinimumPeriod> between sequential blocks"
Expand Down Expand Up @@ -296,7 +295,7 @@ pub mod pallet {
.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());
let next_time = cmp::max(data, Now::<T>::get() + T::MinimumPeriod::get());
Some(Call::set { now: next_time })
}

Expand All @@ -317,7 +316,7 @@ pub mod pallet {
.expect("Timestamp inherent data not correctly encoded")
.expect("Timestamp inherent data must be provided");

let minimum = (Self::now() + T::MinimumPeriod::get()).saturated_into::<u64>();
let minimum = (Now::<T>::get() + T::MinimumPeriod::get()).saturated_into::<u64>();
if t > *(data + MAX_TIMESTAMP_DRIFT_MILLIS) {
Err(InherentError::TooFarInFuture)
} else if t < minimum {
Expand All @@ -339,7 +338,7 @@ impl<T: Config> Pallet<T> {
/// NOTE: if this function is called prior to setting the timestamp,
/// it will return the timestamp of the previous block.
pub fn get() -> T::Moment {
Self::now()
Now::<T>::get()
}

/// Set the timestamp to something in particular. Only used for tests.
Expand All @@ -356,7 +355,7 @@ impl<T: Config> Time for Pallet<T> {
type Moment = T::Moment;

fn now() -> Self::Moment {
Self::now()
Now::<T>::get()
}
}

Expand All @@ -367,7 +366,7 @@ impl<T: Config> UnixTime for Pallet<T> {
fn now() -> core::time::Duration {
// now is duration since unix epoch in millisecond as documented in
// `sp_timestamp::InherentDataProvider`.
let now = Self::now();
let now = Now::<T>::get();
sp_std::if_std! {
if now == T::Moment::zero() {
log::error!(
Expand Down
2 changes: 1 addition & 1 deletion substrate/frame/timestamp/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fn timestamp_works() {
new_test_ext().execute_with(|| {
crate::Now::<Test>::put(46);
assert_ok!(Timestamp::set(RuntimeOrigin::none(), 69));
assert_eq!(Timestamp::now(), 69);
assert_eq!(crate::Now::<Test>::get(), 69);
assert_eq!(Some(69), get_captured_moment());
});
}
Expand Down
Loading