Skip to content

Commit f63e909

Browse files
committed
fix(node): add validation for block time
1 parent 589826a commit f63e909

File tree

4 files changed

+52
-1
lines changed

4 files changed

+52
-1
lines changed

mina-p2p-messages/src/v2/manual.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1531,6 +1531,12 @@ impl From<OffsetDateTime> for BlockTimeTimeStableV1 {
15311531
}
15321532
}
15331533

1534+
impl MinaBlockHeaderStableV2 {
1535+
pub fn genesis_state_hash(&self) -> &StateHash {
1536+
&self.protocol_state.body.genesis_state_hash
1537+
}
1538+
}
1539+
15341540
impl StagedLedgerDiffBodyStableV1 {
15351541
pub fn diff(&self) -> &StagedLedgerDiffDiffDiffStableV2 {
15361542
&self.staged_ledger_diff.diff

node/src/consensus/consensus_actions.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ impl redux::EnablingCondition<crate::State> for ConsensusAction {
6969
hash: hash.clone(),
7070
block: block.clone()
7171
};
72-
!block.is_genesis() && !state.consensus.blocks.contains_key(hash)
72+
!block.is_genesis()
73+
&& !state.consensus.blocks.contains_key(hash)
74+
&& state.prevalidate_block(&block)
7375
},
7476
ConsensusAction::BlockChainProofUpdate { hash, .. } => {
7577
(state.consensus.best_tip.as_ref() == Some(hash)

node/src/p2p/peer/p2p_peer_actions.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ use super::*;
22

33
impl redux::EnablingCondition<crate::State> for P2pPeerAction {
44
fn is_enabled(&self, state: &crate::State, time: redux::Timestamp) -> bool {
5+
if let P2pPeerAction::BestTipUpdate { best_tip, .. } = self {
6+
if !state.prevalidate_block(best_tip) {
7+
return false;
8+
}
9+
}
510
state.p2p.is_enabled(self, time)
611
}
712
}

node/src/state.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,44 @@ impl State {
349349
})
350350
}
351351

352+
pub fn prevalidate_block(&self, block: &ArcBlockWithHash) -> bool {
353+
let Some((genesis, cur_global_slot)) =
354+
None.or_else(|| Some((self.genesis_block()?, self.cur_global_slot()?)))
355+
else {
356+
// we don't have genesis block. This should be impossible
357+
// because we don't even know chain_id before we have genesis
358+
// block, so we can't be connected to any peers from which
359+
// we would receive a block.
360+
return false;
361+
};
362+
363+
// received_at_valid_time
364+
// https://github.com/minaprotocol/mina/blob/6af211ad58e9356f00ea4a636cea70aa8267c072/src/lib/consensus/proof_of_stake.ml#L2746
365+
{
366+
let block_global_slot = block.global_slot();
367+
368+
let delta = genesis.constants().delta.as_u32();
369+
if cur_global_slot < block_global_slot {
370+
// Too_early
371+
return false;
372+
} else if cur_global_slot.saturating_sub(block_global_slot) > delta {
373+
// Too_late
374+
return false;
375+
}
376+
}
377+
378+
if block.constants() != genesis.constants() {
379+
return false;
380+
}
381+
382+
if block.header().genesis_state_hash() != genesis.hash() {
383+
return false;
384+
}
385+
386+
// TODO(binier): more checks.
387+
true
388+
}
389+
352390
pub fn should_log_node_id(&self) -> bool {
353391
self.config.testing_run
354392
}

0 commit comments

Comments
 (0)