Skip to content

Commit

Permalink
remove pallet::getter usage from pallet-timestamp (#3374)
Browse files Browse the repository at this point in the history
this pr is a part of #3326

---------

Co-authored-by: Kian Paimani <[email protected]>
Co-authored-by: Bastian Köcher <[email protected]>
  • Loading branch information
3 people authored Jun 17, 2024
1 parent 5055294 commit 55a13ab
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 15 deletions.
4 changes: 2 additions & 2 deletions cumulus/test/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ use frame_system::{
pub use pallet_balances::Call as BalancesCall;
pub use pallet_glutton::Call as GluttonCall;
pub use pallet_sudo::Call as SudoCall;
pub use pallet_timestamp::Call as TimestampCall;
pub use pallet_timestamp::{Call as TimestampCall, Now};
#[cfg(any(feature = "std", test))]
pub use sp_runtime::BuildStorage;
pub use sp_runtime::{Perbill, Permill};
Expand Down Expand Up @@ -499,7 +499,7 @@ impl_runtime_apis! {

impl crate::GetLastTimestamp<Block> for Runtime {
fn get_last_timestamp() -> u64 {
Timestamp::now()
Now::<Runtime>::get()
}
}

Expand Down
3 changes: 2 additions & 1 deletion polkadot/runtime/test-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ use frame_support::{
};
use pallet_grandpa::{fg_primitives, AuthorityId as GrandpaId};
use pallet_session::historical as session_historical;
use pallet_timestamp::Now;
use pallet_transaction_payment::{FeeDetails, RuntimeDispatchInfo};
use polkadot_primitives::{
slashing, AccountId, AccountIndex, Balance, BlockNumber, CandidateEvent, CandidateHash,
Expand Down Expand Up @@ -1186,7 +1187,7 @@ sp_api::impl_runtime_apis! {

impl crate::GetLastTimestamp<Block> for Runtime {
fn get_last_timestamp() -> u64 {
Timestamp::now()
Now::<Runtime>::get()
}
}

Expand Down
13 changes: 13 additions & 0 deletions prdoc/pr_3374.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0
# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json

title: removed `pallet::getter` from `pallet-timestamp`

doc:
- audience: Runtime Dev
description: |
This PR removes all the `pallet::getter` usages from `pallet-timestamp`, and updates depdendant runtimes accordingly.
The syntax `StorageItem::<T, I>::get()` should be used instead.

crates:
- name: pallet-timestamp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub(crate) const LOG_TARGET: &str = "tests::e2e-epm";

use frame_support::{assert_err, assert_noop, assert_ok};
use mock::*;
use pallet_timestamp::Now;
use sp_core::Get;
use sp_runtime::Perbill;

Expand All @@ -46,7 +47,7 @@ fn log_current_time() {
Session::current_index(),
Staking::current_era(),
ElectionProviderMultiPhase::current_phase(),
Timestamp::now()
Now::<Runtime>::get()
);
}

Expand Down Expand Up @@ -209,7 +210,7 @@ fn continuous_slashes_below_offending_threshold() {
// failed due to election minimum score.
if start_next_active_era(pool_state.clone()).is_err() {
assert!(ElectionProviderMultiPhase::current_phase().is_emergency());
break
break;
}

active_validator_set = Session::validators();
Expand Down
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

0 comments on commit 55a13ab

Please sign in to comment.