MinaProtocol / mina

Mina is a cryptocurrency protocol with a constant size blockchain, improving scaling while maintaining decentralization and security.
https://minaprotocol.com
Apache License 2.0
1.99k stars 529 forks source link

More aggressive refactor of bootstrap controller #15923

Open mrmr1993 opened 1 month ago

mrmr1993 commented 1 month ago

This PR builds upon #15921 and #15922, with a series of commits incrementally splitting up bootstrap controller into stages.

Importantly, this refactor also lifts the pipe logic out of any of the individual stages, in preparation for the pipes to be lifted out of this module entirely.

This refactor makes the core control flow approximately

(* step 1. download snarked_ledger *)
let%bind stage_1 = download_snarked_ledger t stage_0 in
(* step 2. Download scan state and pending coinbases. *)
let%bind.Deferred.Result stage_2 =
  download_scan_state_and_pending_coinbases t stage_1
in
(* step 3. Construct staged ledger from snarked ledger, scan state
   and pending coinbases. *)
(* Construct the staged ledger before constructing the transition
 * frontier in order to verify the scan state we received.
 * TODO: reorganize the code to avoid doing this twice (#3480) *)
let%bind stage_3 = construct_root_staged_ledger t stage_2 in
(* step 4. Synchronize consensus local state if necessary *)
let%bind.Deferred.Result () =
  synchronize_consensus_local_state t consensus_local_state
in
(* step 5. Close the old frontier and reload a new one from disk. *)
let%map res =
  close_and_reload_frontier t ~consensus_local_state ~persistent_root
    ~persistent_frontier ~catchup_mode stage_3
in

(with some slight adjustments for resource management), where t is a Bootstrap_controller.t and stage_0 is the initialisation state. From this point, it should be natural to rework the transition router's surrounding logic to be roughly

let bootstrap_controller = Bootstrap_controller.create ... in
don't_wait_for
  (Reader.iter boostrap_controller_reader
    ~f:(Boostrap_controller.handle_incoming_transition bootstrap_controller) ) ;
let%bind (new_frontier, collected_transitions) =
  Boostrap_controller.run boostrap_controller ...
in
...

and thus start unwinding some of the pipe spaghetti there.

Reviewer note: this refactor may be easier to review commit by commit to check for correctness. Even without this, the logical flow is more consistent when reviewing with 'ignore whitespace changes' enabled.