Skip to content

Commit

Permalink
split timestamp validation into two assertions
Browse files Browse the repository at this point in the history
This way we can tell which one failed
  • Loading branch information
tbro committed Oct 1, 2024
1 parent 064eda5 commit 6cb12e9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
2 changes: 1 addition & 1 deletion types/src/v0/impls/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1390,7 +1390,7 @@ mod test_headers {
validated_state.block_merkle_tree = block_merkle_tree.clone();
*parent_header.block_merkle_tree_root_mut() = block_merkle_tree_root;
let mut proposal = parent_header.clone();
*proposal.timestamp_mut() = 5;
*proposal.timestamp_mut() = OffsetDateTime::now_utc().unix_timestamp() as u64;
*proposal.l1_head_mut() = 5;

let ver = StaticVersion::<0, 1>::version();
Expand Down
25 changes: 18 additions & 7 deletions types/src/v0/impls/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,15 @@ pub enum ProposalValidationError {
InvalidNsTable { err: NsTableValidationError },
#[error("Some fee amount or their sum total out of range")]
SomeFeeAmountOutOfRange,
#[error("Invalid timestamp: proposal={proposal_timestamp}, parent={parent_timestamp}")]
InvalidTimestampNonIncrementing {
proposal_timestamp: u64,
parent_timestamp: u64,
},
#[error("Invalid timestamp: local:={local_timestamp}, proposal={proposal_timestamp}")]
InvalidTimestamp {
local_timestamp: u64,
InvalidTimestampDrift {
proposal_timestamp: u64,
local_timestamp: u64,
},
#[error("l1_finalized has `None` value")]
L1FinalizedNotFound,
Expand Down Expand Up @@ -316,12 +321,18 @@ pub fn validate_proposal(
});
}

// Check if timestamp is increasing.
// Validate timestamp increasing.
if proposal.timestamp() < parent_header.timestamp() {
return Err(ProposalValidationError::InvalidTimestampNonIncrementing {
proposal_timestamp: proposal.timestamp(),
parent_timestamp: parent_header.timestamp(),
});
}

// Validate timestamp hasn't drifted too much from system time.
let system_time: u64 = OffsetDateTime::now_utc().unix_timestamp() as u64;
if proposal.timestamp() < parent_header.timestamp()
|| proposal.timestamp().abs_diff(system_time) > 2
{
return Err(ProposalValidationError::InvalidTimestamp {
if proposal.timestamp().abs_diff(system_time) > 2 {
return Err(ProposalValidationError::InvalidTimestampDrift {
proposal_timestamp: proposal.timestamp(),
local_timestamp: system_time,
});
Expand Down

0 comments on commit 6cb12e9

Please sign in to comment.