Skip to content

Commit e89ba61

Browse files
committed
fix(node): add validation for block time
1 parent a1f36be commit e89ba61

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
@@ -350,6 +350,44 @@ impl State {
350350
})
351351
}
352352

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

0 commit comments

Comments
 (0)