11use openmina_core:: {
22 block:: BlockHash ,
3+ bug_condition,
34 consensus:: { is_short_range_fork, long_range_fork_take, short_range_fork_take} ,
45} ;
56use snark:: block_verify:: { SnarkBlockVerifyAction , SnarkBlockVerifyError } ;
@@ -12,7 +13,7 @@ use crate::{
1213 } ,
1314 TransitionFrontierSyncAction ,
1415 } ,
15- Action , State , WatchedAccountsAction ,
16+ WatchedAccountsAction ,
1617} ;
1718
1819use super :: {
@@ -78,7 +79,7 @@ impl ConsensusState {
7879 return ;
7980 }
8081
81- transition_frontier_new_best_tip_handler ( global_state , dispatcher) ;
82+ dispatcher. push ( ConsensusAction :: TransitionFrontierSyncTargetUpdate ) ;
8283 }
8384 ConsensusAction :: BlockSnarkVerifyPending { req_id, hash } => {
8485 if let Some ( block) = state. blocks . get_mut ( hash) {
@@ -240,7 +241,36 @@ impl ConsensusState {
240241 } ) ;
241242 }
242243
243- transition_frontier_new_best_tip_handler ( global_state, dispatcher) ;
244+ dispatcher. push ( ConsensusAction :: TransitionFrontierSyncTargetUpdate ) ;
245+ }
246+ ConsensusAction :: TransitionFrontierSyncTargetUpdate => {
247+ let ( dispatcher, state) = state_context. into_dispatcher_and_state ( ) ;
248+ let Some ( best_tip) = state. consensus . best_tip_block_with_hash ( ) else {
249+ bug_condition ! (
250+ "ConsensusAction::TransitionFrontierSyncTargetUpdate | no chosen best tip"
251+ ) ;
252+ return ;
253+ } ;
254+
255+ let Some ( ( blocks_inbetween, root_block) ) = state
256+ . consensus
257+ . best_tip_chain_proof ( & state. transition_frontier )
258+ else {
259+ bug_condition ! ( "ConsensusAction::TransitionFrontierSyncTargetUpdate | no best tip chain proof" ) ;
260+ return ;
261+ } ;
262+
263+ let previous_root_snarked_ledger_hash = state
264+ . transition_frontier
265+ . root ( )
266+ . map ( |b| b. snarked_ledger_hash ( ) . clone ( ) ) ;
267+
268+ dispatcher. push ( TransitionFrontierSyncAction :: BestTipUpdate {
269+ previous_root_snarked_ledger_hash,
270+ best_tip,
271+ root_block,
272+ blocks_inbetween,
273+ } ) ;
244274 }
245275 ConsensusAction :: P2pBestTipUpdate { best_tip } => {
246276 let dispatcher = state_context. into_dispatcher ( ) ;
@@ -278,56 +308,3 @@ impl ConsensusState {
278308 }
279309 }
280310}
281-
282- fn transition_frontier_new_best_tip_handler (
283- state : & State ,
284- dispatcher : & mut redux:: Dispatcher < Action , State > ,
285- ) {
286- let Some ( best_tip) = state. consensus . best_tip_block_with_hash ( ) else {
287- return ;
288- } ;
289- let pred_hash = best_tip. pred_hash ( ) ;
290-
291- let Some ( ( blocks_inbetween, root_block) ) =
292- state. consensus . best_tip_chain_proof . clone ( ) . or_else ( || {
293- let old_best_tip = state. transition_frontier . best_tip ( ) ?;
294- let mut iter = state. transition_frontier . best_chain . iter ( ) ;
295- if old_best_tip. hash ( ) == pred_hash {
296- if old_best_tip. height ( ) > old_best_tip. constants ( ) . k . as_u32 ( ) {
297- iter. next ( ) ;
298- }
299- let root_block = iter. next ( ) ?. block_with_hash ( ) . clone ( ) ;
300- let hashes = iter. map ( |b| b. hash ( ) . clone ( ) ) . collect ( ) ;
301- Some ( ( hashes, root_block) )
302- } else if old_best_tip. pred_hash ( ) == pred_hash {
303- let root_block = iter. next ( ) ?. block_with_hash ( ) . clone ( ) ;
304- let hashes = iter. rev ( ) . skip ( 1 ) . rev ( ) . map ( |b| b. hash ( ) . clone ( ) ) . collect ( ) ;
305- Some ( ( hashes, root_block) )
306- } else {
307- None
308- }
309- } )
310- else {
311- return ;
312- } ;
313-
314- if !state. transition_frontier . sync . is_pending ( ) && !state. transition_frontier . sync . is_synced ( ) {
315- dispatcher. push ( TransitionFrontierSyncAction :: Init {
316- best_tip,
317- root_block,
318- blocks_inbetween,
319- } ) ;
320- } else {
321- let previous_root_snarked_ledger_hash = state
322- . transition_frontier
323- . root ( )
324- . map ( |b| b. snarked_ledger_hash ( ) . clone ( ) ) ;
325-
326- dispatcher. push ( TransitionFrontierSyncAction :: BestTipUpdate {
327- previous_root_snarked_ledger_hash,
328- best_tip,
329- root_block,
330- blocks_inbetween,
331- } ) ;
332- }
333- }
0 commit comments