Skip to content

Commit

Permalink
consensus: reintroduce timestamp checks
Browse files Browse the repository at this point in the history
See also #1649
See also dusk-network/dips#13
  • Loading branch information
herr-seppia committed May 10, 2024
1 parent 6c5cd88 commit f627aac
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
7 changes: 7 additions & 0 deletions consensus/src/commons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub struct RoundUpdate {
seed: Seed,
hash: [u8; 32],
cert: Certificate,
timestamp: u64,

pub base_timeouts: TimeoutSet,
}
Expand All @@ -53,6 +54,7 @@ impl RoundUpdate {
cert: mrb_header.cert,
hash: mrb_header.hash,
seed: mrb_header.seed,
timestamp: mrb_header.timestamp,
base_timeouts,
}
}
Expand All @@ -68,6 +70,10 @@ impl RoundUpdate {
pub fn cert(&self) -> &Certificate {
&self.cert
}

pub fn timestamp(&self) -> u64 {
self.timestamp
}
}

#[derive(Debug, Clone, Copy, Error)]
Expand Down Expand Up @@ -100,6 +106,7 @@ pub enum ConsensusError {
InvalidQuorumType,
InvalidVote(Vote),
InvalidMsgIteration(u8),
InvalidTimestamp,
FutureEvent,
PastEvent,
NotCommitteeMember,
Expand Down
18 changes: 16 additions & 2 deletions consensus/src/proposal/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
//
// Copyright (c) DUSK NETWORK. All rights reserved.

use crate::commons::{ConsensusError, Database, RoundUpdate};
use crate::commons::{
get_current_timestamp, ConsensusError, Database, RoundUpdate,
};
use crate::merkle::merkle_root;
use crate::msg_handler::{HandleMsgOutput, MsgHandler};
use crate::user::committee::Committee;
Expand Down Expand Up @@ -42,11 +44,16 @@ impl<D: Database> MsgHandler for ProposalHandler<D> {
async fn collect(
&mut self,
msg: Message,
_ru: &RoundUpdate,
ru: &RoundUpdate,
_committee: &Committee,
) -> Result<HandleMsgOutput, ConsensusError> {
// store candidate block
let p = Self::unwrap_msg(&msg)?;

if p.candidate.header().timestamp < ru.timestamp() {
return Err(ConsensusError::InvalidTimestamp);
}

self.db
.lock()
.await
Expand Down Expand Up @@ -84,10 +91,17 @@ impl<D: Database> ProposalHandler<D> {
// Verify new_block msg signature
p.verify_signature()?;

// We only check the consistency of the declared hash.
// The actual bound with the tip.block_hash is done by
// MsgHandler.is_valid()
if msg.header.prev_block_hash != p.candidate.header().prev_block_hash {
return Err(ConsensusError::InvalidBlockHash);
}

if p.candidate.header().timestamp > get_current_timestamp() {
return Err(ConsensusError::InvalidTimestamp);
}

let tx_hashes: Vec<[u8; 32]> =
p.candidate.txs().iter().map(|t| t.hash()).collect();
let tx_root = merkle_root(&tx_hashes[..]);
Expand Down

0 comments on commit f627aac

Please sign in to comment.