Skip to content

Commit 3dffa89

Browse files
committed
feat(consensus): Prevalidate blocks in reducers not enabling conditions
1 parent 384d24c commit 3dffa89

File tree

5 files changed

+53
-12
lines changed

5 files changed

+53
-12
lines changed

node/src/action_kind.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,8 @@ pub enum ActionKind {
166166
ConsensusBlockSnarkVerifyError,
167167
ConsensusBlockSnarkVerifyPending,
168168
ConsensusBlockSnarkVerifySuccess,
169+
ConsensusBlockValidateError,
170+
ConsensusBlockValidateSuccess,
169171
ConsensusDetectForkRange,
170172
ConsensusLongRangeForkResolve,
171173
ConsensusP2pBestTipUpdate,
@@ -716,7 +718,7 @@ pub enum ActionKind {
716718
}
717719

718720
impl ActionKind {
719-
pub const COUNT: u16 = 599;
721+
pub const COUNT: u16 = 601;
720722
}
721723

722724
impl std::fmt::Display for ActionKind {
@@ -857,6 +859,8 @@ impl ActionKindGet for ConsensusAction {
857859
fn kind(&self) -> ActionKind {
858860
match self {
859861
Self::BlockReceived { .. } => ActionKind::ConsensusBlockReceived,
862+
Self::BlockValidateSuccess { .. } => ActionKind::ConsensusBlockValidateSuccess,
863+
Self::BlockValidateError { .. } => ActionKind::ConsensusBlockValidateError,
860864
Self::BlockChainProofUpdate { .. } => ActionKind::ConsensusBlockChainProofUpdate,
861865
Self::BlockSnarkVerifyPending { .. } => ActionKind::ConsensusBlockSnarkVerifyPending,
862866
Self::BlockSnarkVerifySuccess { .. } => ActionKind::ConsensusBlockSnarkVerifySuccess,

node/src/consensus/consensus_actions.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ pub enum ConsensusAction {
2424
block: Arc<MinaBlockBlockStableV2>,
2525
chain_proof: Option<(Vec<StateHash>, ArcBlockWithHash)>,
2626
},
27+
BlockValidateSuccess {
28+
hash: StateHash,
29+
},
30+
BlockValidateError {
31+
hash: StateHash,
32+
error: String,
33+
},
2734
BlockChainProofUpdate {
2835
hash: StateHash,
2936
chain_proof: (Vec<StateHash>, ArcBlockWithHash),
@@ -69,10 +76,14 @@ impl redux::EnablingCondition<crate::State> for ConsensusAction {
6976
hash: hash.clone(),
7077
block: block.clone()
7178
};
72-
!block.is_genesis()
73-
&& !state.consensus.blocks.contains_key(hash)
74-
&& state.prevalidate_block(&block)
79+
!block.is_genesis() && !state.consensus.blocks.contains_key(hash)
7580
},
81+
ConsensusAction::BlockValidateSuccess { hash }
82+
| ConsensusAction::BlockValidateError { hash, .. } => state
83+
.consensus
84+
.blocks
85+
.get(hash)
86+
.map_or(false, |block| block.status.is_received()),
7687
ConsensusAction::BlockChainProofUpdate { hash, .. } => {
7788
(state.consensus.best_tip.as_ref() == Some(hash)
7889
&& state.consensus.best_tip_chain_proof.is_none())
@@ -87,7 +98,7 @@ impl redux::EnablingCondition<crate::State> for ConsensusAction {
8798
.consensus
8899
.blocks
89100
.get(hash)
90-
.map_or(false, |block| block.status.is_received())
101+
.map_or(false, |block| block.status.is_validated())
91102
&& state.snark.block_verify.jobs.contains(*req_id)
92103
},
93104
ConsensusAction::BlockSnarkVerifySuccess { hash } => {

node/src/consensus/consensus_reducer.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use openmina_core::{
2-
block::BlockHash,
2+
block::{ArcBlockWithHash, BlockHash},
33
bug_condition,
44
consensus::{is_short_range_fork, long_range_fork_take, short_range_fork_take},
55
};
@@ -48,9 +48,31 @@ impl ConsensusState {
4848
);
4949

5050
// Dispatch
51+
let (dispatcher, state) = state_context.into_dispatcher_and_state();
52+
53+
let hash = hash.clone();
54+
let block = ArcBlockWithHash {
55+
hash: hash.clone(),
56+
block: block.clone(),
57+
};
58+
if state.prevalidate_block(&block) {
59+
dispatcher.push(ConsensusAction::BlockValidateSuccess { hash });
60+
} else {
61+
let error = "Failed".into();
62+
dispatcher.push(ConsensusAction::BlockValidateError { hash, error });
63+
}
64+
}
65+
ConsensusAction::BlockValidateSuccess { hash } => {
66+
let Some(block) = state.blocks.get_mut(hash) else {
67+
return;
68+
};
69+
block.status = ConsensusBlockStatus::Validated;
70+
71+
// Dispatch
72+
let block = (hash.clone(), block.block.clone()).into();
5173
let dispatcher = state_context.into_dispatcher();
5274
dispatcher.push(SnarkBlockVerifyAction::Init {
53-
block: (hash.clone(), block.clone()).into(),
75+
block,
5476
on_init: redux::callback!(
5577
on_received_block_snark_verify_init((hash: BlockHash, req_id: SnarkBlockVerifyId)) -> crate::Action {
5678
ConsensusAction::BlockSnarkVerifyPending { hash, req_id }
@@ -65,6 +87,9 @@ impl ConsensusState {
6587
}),
6688
});
6789
}
90+
ConsensusAction::BlockValidateError { hash, .. } => {
91+
state.blocks.remove(hash);
92+
}
6893
ConsensusAction::BlockChainProofUpdate { hash, chain_proof } => {
6994
if state.best_tip.as_ref() == Some(hash) {
7095
state.best_tip_chain_proof = Some(chain_proof.clone());

node/src/consensus/consensus_state.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ pub enum ConsensusBlockStatus {
4444
Received {
4545
time: redux::Timestamp,
4646
},
47+
Validated,
4748
SnarkVerifyPending {
4849
time: redux::Timestamp,
4950
req_id: SnarkBlockVerifyId,
@@ -73,6 +74,10 @@ impl ConsensusBlockStatus {
7374
matches!(self, Self::Received { .. })
7475
}
7576

77+
pub fn is_validated(&self) -> bool {
78+
matches!(self, Self::Validated)
79+
}
80+
7681
pub fn is_snark_verify_pending(&self) -> bool {
7782
matches!(self, Self::SnarkVerifyPending { .. })
7883
}
@@ -167,6 +172,7 @@ impl ConsensusState {
167172
};
168173
match &candidate.status {
169174
ConsensusBlockStatus::Received { .. } => false,
175+
ConsensusBlockStatus::Validated => false,
170176
ConsensusBlockStatus::SnarkVerifyPending { .. } => false,
171177
ConsensusBlockStatus::SnarkVerifySuccess { .. } => false,
172178
ConsensusBlockStatus::ForkRangeDetected { .. } => false,

node/src/p2p/peer/p2p_peer_actions.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@ 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-
}
105
state.p2p.is_enabled(self, time)
116
}
127
}

0 commit comments

Comments
 (0)