Skip to content

Commit 780129d

Browse files
biniertizoc
authored andcommitted
fix(node): add validation for block time
1 parent 150b1bb commit 780129d

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
@@ -1532,6 +1532,12 @@ impl From<OffsetDateTime> for BlockTimeTimeStableV1 {
15321532
}
15331533
}
15341534

1535+
impl MinaBlockHeaderStableV2 {
1536+
pub fn genesis_state_hash(&self) -> &StateHash {
1537+
&self.protocol_state.body.genesis_state_hash
1538+
}
1539+
}
1540+
15351541
impl StagedLedgerDiffBodyStableV1 {
15361542
pub fn diff(&self) -> &StagedLedgerDiffDiffDiffStableV2 {
15371543
&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
@@ -356,6 +356,44 @@ impl State {
356356
})
357357
}
358358

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

0 commit comments

Comments
 (0)