From 3f0da7668d8275c0ab3a150a6fd3d764cf49f728 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Proust?= Date: Tue, 19 Jun 2018 13:34:18 +0800 Subject: [PATCH] Alpha/Endorser: use lazy init for contracts This gives the Endorser the same semantic as the Baker: the delegate keys are retreived once if not explicitely given as arguments. --- .../lib_delegate/client_baking_endorsement.ml | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/proto_alpha/lib_delegate/client_baking_endorsement.ml b/src/proto_alpha/lib_delegate/client_baking_endorsement.ml index be0cabba0..a295bcd34 100644 --- a/src/proto_alpha/lib_delegate/client_baking_endorsement.ml +++ b/src/proto_alpha/lib_delegate/client_baking_endorsement.ml @@ -77,7 +77,7 @@ let forge_endorsement (cctxt : #Proto_alpha.full) (** Worker *) type state = { - delegates: public_key_hash list ; + delegates: public_key_hash list tzlazy ; delay: int64 ; mutable pending: endorsements option ; } @@ -92,14 +92,6 @@ and endorsements = { let create_state delegates delay = { delegates ; delay ; pending = None } -let get_delegates cctxt state = - match state.delegates with - | [] -> - Client_keys.get_keys cctxt >>=? fun keys -> - return (List.map (fun (_, pkh, _, _) -> pkh) keys) - | _ :: _ as delegates -> - return delegates - let endorse_for_delegate cctxt block delegate = let { Client_baking_blocks.hash ; level } = block in let b = `Hash (hash, 0) in @@ -151,7 +143,7 @@ let prepare_endorsement (cctxt : #Proto_alpha.full) ~(max_past:int64) state bi = Block_hash.pp_short bi.hash >>= fun () -> let time = Time.(add (now ()) state.delay) in let timeout = Lwt_unix.sleep (Int64.to_float state.delay) in - get_delegates cctxt state >>=? fun delegates -> + tzforce state.delegates >>=? fun delegates -> filter_map_p (fun delegate -> allowed_to_endorse cctxt bi delegate >>=? function @@ -195,6 +187,15 @@ let create last_get_block := Some t ; t | Some t -> t in + + let contracts = match contracts with + | [] -> + tzlazy (fun () -> + Client_keys.get_keys cctxt >>=? fun keys -> + return (List.map (fun (_, pkh, _, _) -> pkh) keys) + ) + | _ :: _ -> + tzlazy (fun () -> return contracts) in let state = create_state contracts (Int64.of_int delay) in (* main loop *)