From 8b73e812ac5d07a0565a5480365ec0972fec79a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Proust?= Date: Fri, 15 Jun 2018 16:06:48 +0800 Subject: [PATCH] Alpha/Baker: load protocol constants lazily This avoids the baker refusing to start when the protocol alpha is not yet activated. --- .../lib_delegate/client_baking_forge.ml | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/proto_alpha/lib_delegate/client_baking_forge.ml b/src/proto_alpha/lib_delegate/client_baking_forge.ml index 69fc4acb2..29e758ae2 100644 --- a/src/proto_alpha/lib_delegate/client_baking_forge.ml +++ b/src/proto_alpha/lib_delegate/client_baking_forge.ml @@ -15,8 +15,12 @@ include Logging.Make(struct let name = "client.baking" end) type state = { genesis: Block_hash.t ; index : Context.index ; + + (* Only mutated once for caching/lazy initialisation *) mutable delegates: public_key_hash list ; - constants : Constants.t ; + mutable constants : Constants.t option ; + + (* truly mutable *) mutable best: Client_baking_blocks.block_info ; mutable future_slots: (Time.t * (Client_baking_blocks.block_info * int * public_key_hash)) list ; @@ -500,6 +504,14 @@ let get_delegates cctxt state = return delegates | _ :: _ as delegates -> return delegates +let get_constants cctxt state = + match state.constants with + | None -> + Alpha_services.Constants.all cctxt (`Main, `Head 0) >>=? fun constants -> + state.constants <- Some constants; + return constants + | Some constants -> return constants + let insert_block @@ -595,8 +607,9 @@ let filter_invalid_operations (cctxt : #full) state block_info (operations : pac | Ok () -> let quota : Alpha_environment.Updater.quota list = Main.validation_passes in (* This shouldn't happen *) + get_constants cctxt state >>=? fun constants -> let endorsements = - List.sub (List.rev endorsements) state.constants.Constants.parametric.endorsers_per_block + List.sub (List.rev endorsements) constants.Constants.parametric.endorsers_per_block in let votes = retain_operations_up_to_quota (List.rev votes) (List.nth quota 1).max_size in @@ -785,8 +798,7 @@ let create | Some t -> t in lwt_debug "Opening shell context" >>= fun () -> Client_baking_simulator.load_context ~context_path >>= fun index -> - Alpha_services.Constants.all cctxt (`Main, `Head 0) >>=? fun constants -> - let state = create_state genesis_hash index delegates constants bi in + let state = create_state genesis_hash index delegates None bi in check_error @@ insert_block cctxt ?max_priority state bi >>= fun () -> (* main loop *)