Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

pallet-timestamp: Remove ValidAtTimestamp error variant #13346

Merged
merged 4 commits into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion frame/timestamp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ pub mod pallet {
if t > *(data + MAX_TIMESTAMP_DRIFT_MILLIS) {
Err(InherentError::TooFarInFuture)
} else if t < minimum {
Err(InherentError::ValidAtTimestamp(minimum.into()))
Err(InherentError::TimeBetweenBlocksTooShort)
} else {
Ok(())
}
Expand Down
41 changes: 7 additions & 34 deletions primitives/timestamp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,9 @@ impl From<Duration> for Timestamp {
#[derive(Encode, sp_runtime::RuntimeDebug)]
#[cfg_attr(feature = "std", derive(Decode, thiserror::Error))]
pub enum InherentError {
/// The timestamp is valid in the future.
/// This is a non-fatal-error and will not stop checking the inherents.
#[cfg_attr(feature = "std", error("Block will be valid at {0}."))]
ValidAtTimestamp(InherentType),
/// The time between the blocks is too short.
#[cfg_attr(feature = "std", error("The time between the blocks is too short."))]
bkchr marked this conversation as resolved.
Show resolved Hide resolved
TimeBetweenBlocksTooShort,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could also be TooEarly.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I hated my naming as well, as always xD

/// The block timestamp is too far in the future
#[cfg_attr(feature = "std", error("The timestamp of the block is too far in the future."))]
TooFarInFuture,
Expand All @@ -146,7 +145,7 @@ pub enum InherentError {
impl IsFatalError for InherentError {
fn is_fatal_error(&self) -> bool {
match self {
InherentError::ValidAtTimestamp(_) => false,
InherentError::TimeBetweenBlocksTooShort => true,
InherentError::TooFarInFuture => true,
}
}
Expand Down Expand Up @@ -240,34 +239,8 @@ impl sp_inherents::InherentDataProvider for InherentDataProvider {
identifier: &InherentIdentifier,
error: &[u8],
) -> Option<Result<(), sp_inherents::Error>> {
if *identifier != INHERENT_IDENTIFIER {
return None
}

match InherentError::try_from(&INHERENT_IDENTIFIER, error)? {
InherentError::ValidAtTimestamp(valid) => {
let max_drift = self.max_drift;
let timestamp = self.timestamp;
// halt import until timestamp is valid.
// reject when too far ahead.
if valid > timestamp + max_drift {
return Some(Err(sp_inherents::Error::Application(Box::from(
InherentError::TooFarInFuture,
))))
}

let diff = valid.checked_sub(timestamp).unwrap_or_default();
log::info!(
target: "timestamp",
"halting for block {} milliseconds in the future",
diff.0,
);

futures_timer::Delay::new(diff.as_duration()).await;

Some(Ok(()))
},
o => Some(Err(sp_inherents::Error::Application(Box::from(o)))),
}
Some(Err(sp_inherents::Error::Application(Box::from(InherentError::try_from(
davxy marked this conversation as resolved.
Show resolved Hide resolved
identifier, error,
)?))))
}
}