mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-12 08:51:09 +00:00
Convert timestamp to unit type wrapper (#8333)
The timestamp inherent type was up to now just a simple `u64`. This worked, but doesn't give you that much guarantees at compile time about the type. This pr changes that by converting this type to a unit type wrapper, similar to what we have done for `Slot`. This is required for some future pr that touches quite a lot of the inherents stuff :) Besides this unit wrapper type, this pr also moves the `OnTimestampSet` trait to `frame_support::traits`.
This commit is contained in:
@@ -22,7 +22,6 @@ sp-runtime = { version = "3.0.0", default-features = false, path = "../../primit
|
||||
frame-support = { version = "3.0.0", default-features = false, path = "../support" }
|
||||
sp-consensus-aura = { version = "0.9.0", path = "../../primitives/consensus/aura", default-features = false }
|
||||
frame-system = { version = "3.0.0", default-features = false, path = "../system" }
|
||||
sp-timestamp = { version = "3.0.0", default-features = false, path = "../../primitives/timestamp" }
|
||||
pallet-timestamp = { version = "3.0.0", default-features = false, path = "../timestamp" }
|
||||
|
||||
[dev-dependencies]
|
||||
@@ -42,7 +41,6 @@ std = [
|
||||
"frame-support/std",
|
||||
"sp-consensus-aura/std",
|
||||
"frame-system/std",
|
||||
"sp-timestamp/std",
|
||||
"pallet-timestamp/std",
|
||||
]
|
||||
try-runtime = ["frame-support/try-runtime"]
|
||||
|
||||
@@ -39,12 +39,13 @@
|
||||
|
||||
use sp_std::prelude::*;
|
||||
use codec::{Encode, Decode};
|
||||
use frame_support::{Parameter, traits::{Get, FindAuthor, OneSessionHandler}, ConsensusEngineId};
|
||||
use frame_support::{
|
||||
Parameter, traits::{Get, FindAuthor, OneSessionHandler, OnTimestampSet}, ConsensusEngineId,
|
||||
};
|
||||
use sp_runtime::{
|
||||
RuntimeAppPublic,
|
||||
traits::{SaturatedConversion, Saturating, Zero, Member, IsMember}, generic::DigestItem,
|
||||
};
|
||||
use sp_timestamp::OnTimestampSet;
|
||||
use sp_consensus_aura::{AURA_ENGINE_ID, ConsensusLog, AuthorityIndex, Slot};
|
||||
|
||||
mod mock;
|
||||
|
||||
@@ -29,7 +29,6 @@ sp-runtime = { version = "3.0.0", default-features = false, path = "../../primit
|
||||
sp-session = { version = "3.0.0", default-features = false, path = "../../primitives/session" }
|
||||
sp-staking = { version = "3.0.0", default-features = false, path = "../../primitives/staking" }
|
||||
sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" }
|
||||
sp-timestamp = { version = "3.0.0", default-features = false, path = "../../primitives/timestamp" }
|
||||
log = { version = "0.4.14", default-features = false }
|
||||
|
||||
[dev-dependencies]
|
||||
@@ -59,7 +58,6 @@ std = [
|
||||
"sp-session/std",
|
||||
"sp-staking/std",
|
||||
"sp-std/std",
|
||||
"sp-timestamp/std",
|
||||
"log/std",
|
||||
]
|
||||
runtime-benchmarks = ["frame-benchmarking"]
|
||||
|
||||
@@ -25,7 +25,7 @@ use codec::{Decode, Encode};
|
||||
use frame_support::{
|
||||
decl_error, decl_module, decl_storage,
|
||||
dispatch::DispatchResultWithPostInfo,
|
||||
traits::{FindAuthor, Get, KeyOwnerProofSystem, OneSessionHandler},
|
||||
traits::{FindAuthor, Get, KeyOwnerProofSystem, OneSessionHandler, OnTimestampSet},
|
||||
weights::{Pays, Weight},
|
||||
Parameter,
|
||||
};
|
||||
@@ -38,7 +38,6 @@ use sp_runtime::{
|
||||
};
|
||||
use sp_session::{GetSessionNumber, GetValidatorCount};
|
||||
use sp_std::prelude::*;
|
||||
use sp_timestamp::OnTimestampSet;
|
||||
|
||||
use sp_consensus_babe::{
|
||||
digests::{NextConfigDescriptor, NextEpochDescriptor, PreDigest},
|
||||
|
||||
@@ -2291,6 +2291,13 @@ pub trait ExecuteBlock<Block: BlockT> {
|
||||
fn execute_block(block: Block);
|
||||
}
|
||||
|
||||
/// A trait which is called when the timestamp is set in the runtime.
|
||||
#[impl_trait_for_tuples::impl_for_tuples(30)]
|
||||
pub trait OnTimestampSet<Moment> {
|
||||
/// Called when the timestamp is set.
|
||||
fn on_timestamp_set(moment: Moment);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
@@ -97,7 +97,7 @@ pub mod weights;
|
||||
|
||||
use sp_std::{result, cmp};
|
||||
use sp_inherents::InherentData;
|
||||
use frame_support::traits::{Time, UnixTime};
|
||||
use frame_support::traits::{Time, UnixTime, OnTimestampSet};
|
||||
use sp_runtime::{
|
||||
RuntimeString,
|
||||
traits::{
|
||||
@@ -106,7 +106,6 @@ use sp_runtime::{
|
||||
};
|
||||
use sp_timestamp::{
|
||||
InherentError, INHERENT_IDENTIFIER, InherentType,
|
||||
OnTimestampSet,
|
||||
};
|
||||
pub use weights::WeightInfo;
|
||||
|
||||
@@ -214,16 +213,17 @@ pub mod pallet {
|
||||
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 inherent_data = extract_inherent_data(data)
|
||||
.expect("Gets and decodes timestamp inherent data");
|
||||
let data = (*inherent_data).saturated_into::<T::Moment>();
|
||||
|
||||
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;
|
||||
const MAX_TIMESTAMP_DRIFT_MILLIS: sp_timestamp::Timestamp =
|
||||
sp_timestamp::Timestamp::new(30 * 1000);
|
||||
|
||||
let t: u64 = match call {
|
||||
Call::set(ref t) => t.clone().saturated_into::<u64>(),
|
||||
@@ -233,10 +233,10 @@ pub mod pallet {
|
||||
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 {
|
||||
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))
|
||||
Err(InherentError::ValidAtTimestamp(minimum.into()))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user