File tree Expand file tree Collapse file tree 4 files changed +52
-1
lines changed
Expand file tree Collapse file tree 4 files changed +52
-1
lines changed Original file line number Diff line number Diff 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+
15341540impl StagedLedgerDiffBodyStableV1 {
15351541 pub fn diff ( & self ) -> & StagedLedgerDiffDiffDiffStableV2 {
15361542 & self . staged_ledger_diff . diff
Original file line number Diff line number Diff 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)
Original file line number Diff line number Diff line change @@ -2,6 +2,11 @@ use super::*;
22
33impl 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}
Original file line number Diff line number Diff line change @@ -351,6 +351,44 @@ impl State {
351351 } )
352352 }
353353
354+ pub fn prevalidate_block ( & self , block : & ArcBlockWithHash ) -> bool {
355+ let Some ( ( genesis, cur_global_slot) ) =
356+ None . or_else ( || Some ( ( self . genesis_block ( ) ?, self . cur_global_slot ( ) ?) ) )
357+ else {
358+ // we don't have genesis block. This should be impossible
359+ // because we don't even know chain_id before we have genesis
360+ // block, so we can't be connected to any peers from which
361+ // we would receive a block.
362+ return false ;
363+ } ;
364+
365+ // received_at_valid_time
366+ // https://github.com/minaprotocol/mina/blob/6af211ad58e9356f00ea4a636cea70aa8267c072/src/lib/consensus/proof_of_stake.ml#L2746
367+ {
368+ let block_global_slot = block. global_slot ( ) ;
369+
370+ let delta = genesis. constants ( ) . delta . as_u32 ( ) ;
371+ if cur_global_slot < block_global_slot {
372+ // Too_early
373+ return false ;
374+ } else if cur_global_slot. saturating_sub ( block_global_slot) > delta {
375+ // Too_late
376+ return false ;
377+ }
378+ }
379+
380+ if block. constants ( ) != genesis. constants ( ) {
381+ return false ;
382+ }
383+
384+ if block. header ( ) . genesis_state_hash ( ) != genesis. hash ( ) {
385+ return false ;
386+ }
387+
388+ // TODO(binier): more checks.
389+ true
390+ }
391+
354392 pub fn should_log_node_id ( & self ) -> bool {
355393 self . config . testing_run
356394 }
You can’t perform that action at this time.
0 commit comments