Skip to content

Commit 67b21e8

Browse files
committed
refactor(consensus): Combine all action types of the consensus module into the parent action type
1 parent fb1f34a commit 67b21e8

File tree

6 files changed

+167
-239
lines changed

6 files changed

+167
-239
lines changed

node/src/consensus/consensus_actions.rs

Lines changed: 114 additions & 181 deletions
Original file line numberDiff line numberDiff line change
@@ -10,188 +10,121 @@ use crate::snark::block_verify::SnarkBlockVerifyId;
1010
pub type ConsensusActionWithMeta = redux::ActionWithMeta<ConsensusAction>;
1111
pub type ConsensusActionWithMetaRef<'a> = redux::ActionWithMeta<&'a ConsensusAction>;
1212

13-
#[derive(derive_more::From, Serialize, Deserialize, Debug, Clone)]
14-
pub enum ConsensusAction {
15-
BlockReceived(ConsensusBlockReceivedAction),
16-
BlockChainProofUpdate(ConsensusBlockChainProofUpdateAction),
17-
BlockSnarkVerifyPending(ConsensusBlockSnarkVerifyPendingAction),
18-
BlockSnarkVerifySuccess(ConsensusBlockSnarkVerifySuccessAction),
19-
DetectForkRange(ConsensusDetectForkRangeAction),
20-
ShortRangeForkResolve(ConsensusShortRangeForkResolveAction),
21-
LongRangeForkResolve(ConsensusLongRangeForkResolveAction),
22-
BestTipUpdate(ConsensusBestTipUpdateAction),
23-
Prune(ConsensusPruneAction),
24-
}
25-
26-
#[derive(Serialize, Deserialize, Debug, Clone)]
27-
pub struct ConsensusBlockReceivedAction {
28-
pub hash: StateHash,
29-
pub block: Arc<MinaBlockBlockStableV2>,
30-
pub chain_proof: Option<(Vec<StateHash>, ArcBlockWithHash)>,
31-
}
32-
33-
impl redux::EnablingCondition<crate::State> for ConsensusBlockReceivedAction {
34-
fn is_enabled(&self, state: &crate::State) -> bool {
35-
!state.consensus.blocks.contains_key(&self.hash)
36-
}
37-
}
38-
39-
#[derive(Serialize, Deserialize, Debug, Clone)]
40-
pub struct ConsensusBlockChainProofUpdateAction {
41-
pub hash: StateHash,
42-
pub chain_proof: (Vec<StateHash>, ArcBlockWithHash),
43-
}
44-
45-
impl redux::EnablingCondition<crate::State> for ConsensusBlockChainProofUpdateAction {
46-
fn is_enabled(&self, state: &crate::State) -> bool {
47-
(state.consensus.best_tip.as_ref() == Some(&self.hash)
48-
&& state.consensus.best_tip_chain_proof.is_none())
49-
|| state
50-
.consensus
51-
.blocks
52-
.get(&self.hash)
53-
.map_or(false, |b| b.status.is_pending() && b.chain_proof.is_none())
54-
}
55-
}
56-
5713
#[derive(Serialize, Deserialize, Debug, Clone)]
58-
pub struct ConsensusBlockSnarkVerifyPendingAction {
59-
pub req_id: SnarkBlockVerifyId,
60-
pub hash: StateHash,
61-
}
62-
63-
impl redux::EnablingCondition<crate::State> for ConsensusBlockSnarkVerifyPendingAction {
64-
fn is_enabled(&self, state: &crate::State) -> bool {
65-
state
66-
.consensus
67-
.blocks
68-
.get(&self.hash)
69-
.map_or(false, |block| block.status.is_received())
70-
&& state.snark.block_verify.jobs.contains(self.req_id)
71-
}
72-
}
73-
74-
#[derive(Serialize, Deserialize, Debug, Clone)]
75-
pub struct ConsensusBlockSnarkVerifySuccessAction {
76-
pub hash: StateHash,
77-
}
78-
79-
impl redux::EnablingCondition<crate::State> for ConsensusBlockSnarkVerifySuccessAction {
80-
fn is_enabled(&self, state: &crate::State) -> bool {
81-
state
82-
.consensus
83-
.blocks
84-
.get(&self.hash)
85-
.map_or(false, |block| block.status.is_snark_verify_pending())
86-
}
87-
}
88-
89-
#[derive(Serialize, Deserialize, Debug, Clone)]
90-
pub struct ConsensusDetectForkRangeAction {
91-
pub hash: StateHash,
92-
}
93-
94-
impl redux::EnablingCondition<crate::State> for ConsensusDetectForkRangeAction {
95-
fn is_enabled(&self, #[allow(unused_variables)] state: &crate::State) -> bool {
96-
state
97-
.consensus
98-
.blocks
99-
.get(&self.hash)
100-
.map_or(false, |block| {
101-
matches!(
102-
block.status,
103-
ConsensusBlockStatus::SnarkVerifySuccess { .. }
104-
)
105-
})
106-
}
107-
}
108-
109-
#[derive(Serialize, Deserialize, Debug, Clone)]
110-
pub struct ConsensusShortRangeForkResolveAction {
111-
pub hash: StateHash,
112-
}
113-
114-
impl redux::EnablingCondition<crate::State> for ConsensusShortRangeForkResolveAction {
115-
fn is_enabled(&self, state: &crate::State) -> bool {
116-
state
117-
.consensus
118-
.blocks
119-
.get(&self.hash)
120-
.map_or(false, |block| match state.consensus.best_tip() {
121-
Some(tip) => {
122-
matches!(
123-
&block.status,
124-
ConsensusBlockStatus::ForkRangeDetected { compared_with, short_fork, .. }
125-
if compared_with.as_ref() == Some(tip.hash) && *short_fork
126-
)
127-
}
128-
None => true,
129-
})
130-
}
131-
}
132-
133-
#[derive(Serialize, Deserialize, Debug, Clone)]
134-
pub struct ConsensusLongRangeForkResolveAction {
135-
pub hash: StateHash,
136-
}
137-
138-
impl redux::EnablingCondition<crate::State> for ConsensusLongRangeForkResolveAction {
139-
fn is_enabled(&self, state: &crate::State) -> bool {
140-
state
141-
.consensus
142-
.blocks
143-
.get(&self.hash)
144-
.map_or(false, |block| match state.consensus.best_tip() {
145-
Some(tip) => {
146-
matches!(
147-
&block.status,
148-
ConsensusBlockStatus::ForkRangeDetected { compared_with, short_fork, .. }
149-
if compared_with.as_ref() == Some(tip.hash) && !*short_fork
150-
)
151-
}
152-
None => false,
153-
})
154-
}
155-
}
156-
157-
#[derive(Serialize, Deserialize, Debug, Clone)]
158-
pub struct ConsensusBestTipUpdateAction {
159-
pub hash: StateHash,
160-
}
161-
162-
impl redux::EnablingCondition<crate::State> for ConsensusBestTipUpdateAction {
163-
fn is_enabled(&self, state: &crate::State) -> bool {
164-
state
165-
.consensus
166-
.is_candidate_decided_to_use_as_tip(&self.hash)
167-
}
168-
}
169-
170-
#[derive(Serialize, Deserialize, Debug, Clone)]
171-
pub struct ConsensusPruneAction {}
172-
173-
impl redux::EnablingCondition<crate::State> for ConsensusPruneAction {
14+
pub enum ConsensusAction {
15+
BlockReceived {
16+
hash: StateHash,
17+
block: Arc<MinaBlockBlockStableV2>,
18+
chain_proof: Option<(Vec<StateHash>, ArcBlockWithHash)>,
19+
},
20+
BlockChainProofUpdate {
21+
hash: StateHash,
22+
chain_proof: (Vec<StateHash>, ArcBlockWithHash),
23+
},
24+
BlockSnarkVerifyPending {
25+
req_id: SnarkBlockVerifyId,
26+
hash: StateHash,
27+
},
28+
BlockSnarkVerifySuccess {
29+
hash: StateHash,
30+
},
31+
DetectForkRange {
32+
hash: StateHash,
33+
},
34+
ShortRangeForkResolve {
35+
hash: StateHash,
36+
},
37+
LongRangeForkResolve {
38+
hash: StateHash,
39+
},
40+
BestTipUpdate {
41+
hash: StateHash,
42+
},
43+
Prune,
44+
}
45+
46+
impl redux::EnablingCondition<crate::State> for ConsensusAction {
17447
fn is_enabled(&self, state: &crate::State) -> bool {
175-
state.consensus.best_tip().is_some()
176-
}
177-
}
178-
179-
macro_rules! impl_into_global_action {
180-
($a:ty) => {
181-
impl From<$a> for crate::Action {
182-
fn from(value: $a) -> Self {
183-
Self::Consensus(value.into())
184-
}
48+
match self {
49+
ConsensusAction::BlockReceived { hash, .. } => {
50+
!state.consensus.blocks.contains_key(hash)
51+
},
52+
ConsensusAction::BlockChainProofUpdate { hash, .. } => {
53+
(state.consensus.best_tip.as_ref() == Some(hash)
54+
&& state.consensus.best_tip_chain_proof.is_none())
55+
|| state
56+
.consensus
57+
.blocks
58+
.get(hash)
59+
.map_or(false, |b| b.status.is_pending() && b.chain_proof.is_none())
60+
},
61+
ConsensusAction::BlockSnarkVerifyPending { req_id, hash } => {
62+
state
63+
.consensus
64+
.blocks
65+
.get(hash)
66+
.map_or(false, |block| block.status.is_received())
67+
&& state.snark.block_verify.jobs.contains(*req_id)
68+
},
69+
ConsensusAction::BlockSnarkVerifySuccess { hash } => {
70+
state
71+
.consensus
72+
.blocks
73+
.get(hash)
74+
.map_or(false, |block| block.status.is_snark_verify_pending())
75+
},
76+
ConsensusAction::DetectForkRange { hash } => {
77+
state
78+
.consensus
79+
.blocks
80+
.get(hash)
81+
.map_or(false, |block| {
82+
matches!(
83+
block.status,
84+
ConsensusBlockStatus::SnarkVerifySuccess { .. }
85+
)
86+
})
87+
},
88+
ConsensusAction::ShortRangeForkResolve { hash } => {
89+
state
90+
.consensus
91+
.blocks
92+
.get(hash)
93+
.map_or(false, |block| match state.consensus.best_tip() {
94+
Some(tip) => {
95+
matches!(
96+
&block.status,
97+
ConsensusBlockStatus::ForkRangeDetected { compared_with, short_fork, .. }
98+
if compared_with.as_ref() == Some(tip.hash) && *short_fork
99+
)
100+
}
101+
None => true,
102+
})
103+
},
104+
ConsensusAction::LongRangeForkResolve { hash } => {
105+
state
106+
.consensus
107+
.blocks
108+
.get(hash)
109+
.map_or(false, |block| match state.consensus.best_tip() {
110+
Some(tip) => {
111+
matches!(
112+
&block.status,
113+
ConsensusBlockStatus::ForkRangeDetected { compared_with, short_fork, .. }
114+
if compared_with.as_ref() == Some(tip.hash) && !*short_fork
115+
)
116+
}
117+
None => false,
118+
})
119+
},
120+
ConsensusAction::BestTipUpdate { hash } => {
121+
state
122+
.consensus
123+
.is_candidate_decided_to_use_as_tip(hash)
124+
},
125+
ConsensusAction::Prune => {
126+
state.consensus.best_tip().is_some()
127+
},
185128
}
186-
};
129+
}
187130
}
188-
189-
impl_into_global_action!(ConsensusBlockReceivedAction);
190-
impl_into_global_action!(ConsensusBlockChainProofUpdateAction);
191-
impl_into_global_action!(ConsensusBlockSnarkVerifyPendingAction);
192-
impl_into_global_action!(ConsensusBlockSnarkVerifySuccessAction);
193-
impl_into_global_action!(ConsensusDetectForkRangeAction);
194-
impl_into_global_action!(ConsensusShortRangeForkResolveAction);
195-
impl_into_global_action!(ConsensusLongRangeForkResolveAction);
196-
impl_into_global_action!(ConsensusBestTipUpdateAction);
197-
impl_into_global_action!(ConsensusPruneAction);

node/src/consensus/consensus_effects.rs

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,49 +8,40 @@ use crate::{
88
watched_accounts::WatchedAccountsBlockTransactionsIncludedAction,
99
};
1010

11-
use super::{
12-
ConsensusAction, ConsensusActionWithMeta, ConsensusBestTipUpdateAction,
13-
ConsensusBlockSnarkVerifyPendingAction, ConsensusDetectForkRangeAction,
14-
ConsensusLongRangeForkResolveAction, ConsensusShortRangeForkResolveAction,
15-
};
11+
use super::{ConsensusAction, ConsensusActionWithMeta};
1612

1713
pub fn consensus_effects<S: crate::Service>(store: &mut Store<S>, action: ConsensusActionWithMeta) {
1814
let (action, _) = action.split();
1915

2016
match action {
21-
ConsensusAction::BlockReceived(action) => {
17+
ConsensusAction::BlockReceived { hash, block, .. } => {
2218
let req_id = store.state().snark.block_verify.next_req_id();
2319
store.dispatch(SnarkBlockVerifyInitAction {
2420
req_id,
25-
block: (action.hash.clone(), action.block).into(),
26-
});
27-
store.dispatch(ConsensusBlockSnarkVerifyPendingAction {
28-
req_id,
29-
hash: action.hash,
21+
block: (hash.clone(), block).into(),
3022
});
23+
store.dispatch(ConsensusAction::BlockSnarkVerifyPending { req_id, hash });
3124
}
32-
ConsensusAction::BlockChainProofUpdate(a) => {
33-
if store.state().consensus.best_tip.as_ref() == Some(&a.hash) {
25+
ConsensusAction::BlockChainProofUpdate { hash, .. } => {
26+
if store.state().consensus.best_tip.as_ref() == Some(&hash) {
3427
transition_frontier_new_best_tip(store);
3528
}
3629
}
37-
ConsensusAction::BlockSnarkVerifyPending(_) => {}
38-
ConsensusAction::BlockSnarkVerifySuccess(a) => {
39-
store.dispatch(ConsensusDetectForkRangeAction { hash: a.hash });
30+
ConsensusAction::BlockSnarkVerifyPending { .. } => {}
31+
ConsensusAction::BlockSnarkVerifySuccess { hash } => {
32+
store.dispatch(ConsensusAction::DetectForkRange { hash });
4033
}
41-
ConsensusAction::DetectForkRange(a) => {
42-
store.dispatch(ConsensusShortRangeForkResolveAction {
43-
hash: a.hash.clone(),
44-
});
45-
store.dispatch(ConsensusLongRangeForkResolveAction { hash: a.hash });
34+
ConsensusAction::DetectForkRange { hash } => {
35+
store.dispatch(ConsensusAction::ShortRangeForkResolve { hash: hash.clone() });
36+
store.dispatch(ConsensusAction::LongRangeForkResolve { hash });
4637
}
47-
ConsensusAction::ShortRangeForkResolve(a) => {
48-
store.dispatch(ConsensusBestTipUpdateAction { hash: a.hash });
38+
ConsensusAction::ShortRangeForkResolve { hash } => {
39+
store.dispatch(ConsensusAction::BestTipUpdate { hash });
4940
}
50-
ConsensusAction::LongRangeForkResolve(a) => {
51-
store.dispatch(ConsensusBestTipUpdateAction { hash: a.hash });
41+
ConsensusAction::LongRangeForkResolve { hash } => {
42+
store.dispatch(ConsensusAction::BestTipUpdate { hash });
5243
}
53-
ConsensusAction::BestTipUpdate(_) => {
44+
ConsensusAction::BestTipUpdate { .. } => {
5445
let Some(block) = store.state.get().consensus.best_tip_block_with_hash() else {
5546
return;
5647
};
@@ -66,7 +57,7 @@ pub fn consensus_effects<S: crate::Service>(store: &mut Store<S>, action: Consen
6657

6758
transition_frontier_new_best_tip(store);
6859
}
69-
ConsensusAction::Prune(_) => {}
60+
ConsensusAction::Prune => {}
7061
}
7162
}
7263

0 commit comments

Comments
 (0)