Skip to content

Commit

Permalink
Check timestamp first when validating header (#2137)
Browse files Browse the repository at this point in the history
* Check timestamp first when validating header
* log timestamp drift validation error

(cherry picked from commit ebe8a26)
  • Loading branch information
sveitser committed Oct 9, 2024
1 parent 8ca4657 commit f3114e6
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions types/src/v0/impls/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,17 +330,6 @@ pub fn validate_proposal(
});
}

// Validate timestamp hasn't drifted too much from system time.
let system_time: u64 = OffsetDateTime::now_utc().unix_timestamp() as u64;
// TODO 12 seconds of tolerance should be enough for reasonably
// configured nodes, but we should make this configurable.
if proposal.timestamp().abs_diff(system_time) > 12 {
return Err(ProposalValidationError::InvalidTimestampDrift {
proposal_timestamp: proposal.timestamp(),
local_timestamp: system_time,
});
}

let ValidatedState {
block_merkle_tree,
fee_merkle_tree,
Expand Down Expand Up @@ -680,6 +669,22 @@ impl HotShotState<SeqTypes> for ValidatedState {
vid_common: VidCommon,
version: Version,
) -> Result<(Self, Self::Delta), Self::Error> {
// Validate timestamp hasn't drifted too much from system time.
// Do this check first so we don't add unnecessary drift.
let system_time: u64 = OffsetDateTime::now_utc().unix_timestamp() as u64;
// TODO 12 seconds of tolerance should be enough for reasonably
// configured nodes, but we should make this configurable.
let diff = proposed_header.timestamp().abs_diff(system_time);
if diff > 12 {
tracing::warn!(
"Timestamp drift too high proposed={} system={} diff={}",
proposed_header.timestamp(),
system_time,
diff
);
return Err(BlockError::InvalidBlockHeader);
}

//validate builder fee
if let Err(err) = validate_builder_fee(proposed_header) {
tracing::error!("invalid builder fee: {err:#}");
Expand Down

0 comments on commit f3114e6

Please sign in to comment.