1- use std:: sync:: Arc ;
2-
3- use mina_p2p_messages:: v2:: { MinaBlockBlockStableV2 , StateHash } ;
1+ use mina_p2p_messages:: v2:: StateHash ;
42use openmina_core:: block:: prevalidate:: BlockPrevalidationError ;
5- use openmina_core:: block:: { ArcBlockWithHash , BlockWithHash } ;
3+ use openmina_core:: block:: ArcBlockWithHash ;
64use openmina_core:: consensus:: consensus_take;
75use openmina_core:: { action_event, ActionEvent } ;
86use serde:: { Deserialize , Serialize } ;
97use snark:: block_verify:: SnarkBlockVerifyError ;
108
119use crate :: snark:: block_verify:: SnarkBlockVerifyId ;
1210
13- use super :: TransitionFrontierCandidateStatus ;
14-
1511pub type TransitionFrontierCandidateActionWithMeta =
1612 redux:: ActionWithMeta < TransitionFrontierCandidateAction > ;
1713pub type TransitionFrontierCandidateActionWithMetaRef < ' a > =
@@ -22,10 +18,11 @@ pub type TransitionFrontierCandidateActionWithMetaRef<'a> =
2218#[ derive( Serialize , Deserialize , Debug , Clone , ActionEvent ) ]
2319#[ action_event( level = debug, fields( debug( hash) , debug( error) ) ) ]
2420pub enum TransitionFrontierCandidateAction {
25- #[ action_event( level = info) ]
21+ P2pBestTipUpdate {
22+ best_tip : ArcBlockWithHash ,
23+ } ,
2624 BlockReceived {
27- hash : StateHash ,
28- block : Arc < MinaBlockBlockStableV2 > ,
25+ block : ArcBlockWithHash ,
2926 chain_proof : Option < ( Vec < StateHash > , ArcBlockWithHash ) > ,
3027 } ,
3128 BlockPrevalidateSuccess {
@@ -52,131 +49,49 @@ pub enum TransitionFrontierCandidateAction {
5249 hash : StateHash ,
5350 error : SnarkBlockVerifyError ,
5451 } ,
55- DetectForkRange {
56- hash : StateHash ,
57- } ,
58- ShortRangeForkResolve {
59- hash : StateHash ,
60- } ,
61- LongRangeForkResolve {
62- hash : StateHash ,
63- } ,
64- #[ action_event( level = info) ]
65- BestTipUpdate {
66- hash : StateHash ,
67- } ,
6852 TransitionFrontierSyncTargetUpdate ,
69- P2pBestTipUpdate {
70- best_tip : BlockWithHash < Arc < MinaBlockBlockStableV2 > > ,
71- } ,
7253 Prune ,
7354}
7455
7556impl redux:: EnablingCondition < crate :: State > for TransitionFrontierCandidateAction {
7657 fn is_enabled ( & self , state : & crate :: State , _time : redux:: Timestamp ) -> bool {
7758 match self {
78- TransitionFrontierCandidateAction :: BlockReceived { hash, block, .. } => {
79- let block = ArcBlockWithHash {
80- hash : hash. clone ( ) ,
81- block : block. clone ( )
82- } ;
83- !block. is_genesis ( ) && !state. transition_frontier . candidates . blocks . contains_key ( hash)
84- } ,
59+ TransitionFrontierCandidateAction :: P2pBestTipUpdate { .. } => true ,
60+ TransitionFrontierCandidateAction :: BlockReceived { block, .. } => {
61+ !block. is_genesis ( ) && !state. transition_frontier . candidates . contains ( block. hash ( ) )
62+ }
8563 TransitionFrontierCandidateAction :: BlockPrevalidateSuccess { hash }
8664 | TransitionFrontierCandidateAction :: BlockPrevalidateError { hash, .. } => state
87- . transition_frontier . candidates
88- . blocks
65+ . transition_frontier
66+ . candidates
8967 . get ( hash)
9068 . is_some_and ( |block| block. status . is_received ( ) ) ,
91- TransitionFrontierCandidateAction :: BlockChainProofUpdate { hash, .. } => {
92- ( state. transition_frontier . candidates . best_tip . as_ref ( ) == Some ( hash)
93- && state. transition_frontier . candidates . best_tip_chain_proof . is_none ( ) )
94- || state. transition_frontier
95- . candidates
96- . blocks
97- . get ( hash)
98- . is_some_and ( |b| b. status . is_pending ( ) && b. chain_proof . is_none ( ) )
99- } ,
69+ TransitionFrontierCandidateAction :: BlockChainProofUpdate { hash, .. } => state
70+ . transition_frontier
71+ . candidates
72+ . is_chain_proof_needed ( hash) ,
10073 TransitionFrontierCandidateAction :: BlockSnarkVerifyPending { req_id, hash } => {
10174 state
10275 . transition_frontier
10376 . candidates
104- . blocks
10577 . get ( hash)
106- . is_some_and ( |block| block. status . is_prevalidated ( ) )
78+ . is_some_and ( |block| block. status . is_prevalidated ( ) )
10779 && state. snark . block_verify . jobs . contains ( * req_id)
108- } ,
109- TransitionFrontierCandidateAction :: BlockSnarkVerifySuccess { hash } => {
110- state
111- . transition_frontier
112- . candidates
113- . blocks
114- . get ( hash)
115- . is_some_and ( |block| block. status . is_snark_verify_pending ( ) )
116- } ,
117- TransitionFrontierCandidateAction :: BlockSnarkVerifyError { hash, .. } => {
118- state
119- . transition_frontier
120- . candidates
121- . blocks
122- . get ( hash)
123- . is_some_and ( |block| block. status . is_snark_verify_pending ( ) )
124- } ,
125- TransitionFrontierCandidateAction :: DetectForkRange { hash } => {
126- state
127- . transition_frontier
128- . candidates
129- . blocks
130- . get ( hash)
131- . is_some_and ( |block| {
132- matches ! (
133- block. status,
134- TransitionFrontierCandidateStatus :: SnarkVerifySuccess { .. }
135- )
136- } )
137- } ,
138- TransitionFrontierCandidateAction :: ShortRangeForkResolve { hash } => {
139- state
140- . transition_frontier
141- . candidates
142- . blocks
143- . get ( hash)
144- . is_some_and ( |block| match state. transition_frontier . candidates . best_tip ( ) {
145- Some ( tip) => {
146- matches ! (
147- & block. status,
148- TransitionFrontierCandidateStatus :: ForkRangeDetected { compared_with, short_fork, .. }
149- if compared_with. as_ref( ) == Some ( tip. hash) && * short_fork
150- )
151- }
152- None => true ,
153- } )
154- } ,
155- TransitionFrontierCandidateAction :: LongRangeForkResolve { hash } => {
156- state
157- . transition_frontier
158- . candidates
159- . blocks
160- . get ( hash)
161- . is_some_and ( |block| match state. transition_frontier . candidates . best_tip ( ) {
162- Some ( tip) => {
163- matches ! (
164- & block. status,
165- TransitionFrontierCandidateStatus :: ForkRangeDetected { compared_with, short_fork, .. }
166- if compared_with. as_ref( ) == Some ( tip. hash) && !* short_fork
167- )
168- }
169- None => false ,
170- } )
171- } ,
172- TransitionFrontierCandidateAction :: BestTipUpdate { hash } => {
173- state
174- . transition_frontier
175- . candidates
176- . is_candidate_decided_to_use_as_tip ( hash)
177- } ,
80+ }
81+ TransitionFrontierCandidateAction :: BlockSnarkVerifySuccess { hash } => state
82+ . transition_frontier
83+ . candidates
84+ . get ( hash)
85+ . is_some_and ( |block| block. status . is_snark_verify_pending ( ) ) ,
86+ TransitionFrontierCandidateAction :: BlockSnarkVerifyError { hash, .. } => state
87+ . transition_frontier
88+ . candidates
89+ . get ( hash)
90+ . is_some_and ( |block| block. status . is_snark_verify_pending ( ) ) ,
17891 TransitionFrontierCandidateAction :: TransitionFrontierSyncTargetUpdate => {
179- let Some ( best_tip) = state. transition_frontier . candidates . best_tip_block_with_hash ( ) else {
92+ let Some ( best_candidate) =
93+ state. transition_frontier . candidates . best_verified_block ( )
94+ else {
18095 return false ;
18196 } ;
18297 // do not need to update transition frontier sync target.
@@ -185,18 +100,28 @@ impl redux::EnablingCondition<crate::State> for TransitionFrontierCandidateActio
185100 state. transition_frontier . sync . best_tip ( ) ,
186101 ] )
187102 . flatten ( )
188- . any ( |b| b. hash ( ) == best_tip. hash ( )
189- || !consensus_take ( b. consensus_state ( ) , best_tip. consensus_state ( ) , b. hash ( ) , best_tip. hash ( ) ) ) {
103+ . any ( |b| {
104+ b. hash ( ) == best_candidate. hash ( )
105+ || !consensus_take (
106+ b. consensus_state ( ) ,
107+ best_candidate. consensus_state ( ) ,
108+ b. hash ( ) ,
109+ best_candidate. hash ( ) ,
110+ )
111+ } ) {
190112 return false ;
191113 }
192114
193115 // has enough data
194- state. transition_frontier . candidates . best_tip_chain_proof ( & state. transition_frontier ) . is_some ( )
195- } ,
196- TransitionFrontierCandidateAction :: P2pBestTipUpdate { .. } => true ,
116+ state
117+ . transition_frontier
118+ . candidates
119+ . best_verified_block_chain_proof ( & state. transition_frontier )
120+ . is_some ( )
121+ }
197122 TransitionFrontierCandidateAction :: Prune => {
198- state. transition_frontier . candidates . best_tip ( ) . is_some ( )
199- } ,
123+ state. transition_frontier . candidates . best ( ) . is_some ( )
124+ }
200125 }
201126 }
202127}
0 commit comments