@@ -10,188 +10,121 @@ use crate::snark::block_verify::SnarkBlockVerifyId;
10
10
pub type ConsensusActionWithMeta = redux:: ActionWithMeta < ConsensusAction > ;
11
11
pub type ConsensusActionWithMetaRef < ' a > = redux:: ActionWithMeta < & ' a ConsensusAction > ;
12
12
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
-
57
13
#[ 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 {
174
47
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
+ } ,
185
128
}
186
- } ;
129
+ }
187
130
}
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 ) ;
0 commit comments