Alpha/Baker,Endorser: dynamically find keys

This commit is contained in:
Raphaël Proust 2018-06-21 09:22:25 +08:00
parent d4974aefa8
commit d71b1648d9
2 changed files with 22 additions and 22 deletions

View File

@ -77,7 +77,7 @@ let forge_endorsement (cctxt : #Proto_alpha.full)
(** Worker *) (** Worker *)
type state = { type state = {
delegates: public_key_hash list tzlazy ; delegates: public_key_hash list ;
delay: int64 ; delay: int64 ;
mutable pending: endorsements option ; mutable pending: endorsements option ;
} }
@ -92,6 +92,13 @@ and endorsements = {
let create_state delegates delay = let create_state delegates delay =
{ delegates ; delay ; pending = None } { delegates ; delay ; pending = None }
let get_delegates cctxt state = match state.delegates with
| [] ->
Client_keys.get_keys cctxt >>=? fun keys ->
let delegates = List.map (fun (_,pkh,_,_) -> pkh) keys in
return delegates
| (_ :: _) as delegates -> return delegates
let endorse_for_delegate cctxt block delegate = let endorse_for_delegate cctxt block delegate =
let { Client_baking_blocks.hash ; level } = block in let { Client_baking_blocks.hash ; level } = block in
let b = `Hash (hash, 0) in let b = `Hash (hash, 0) in
@ -143,7 +150,7 @@ let prepare_endorsement ~(max_past:int64) () (cctxt : #Proto_alpha.full) state b
Block_hash.pp_short bi.hash >>= fun () -> Block_hash.pp_short bi.hash >>= fun () ->
let time = Time.(add (now ()) state.delay) in let time = Time.(add (now ()) state.delay) in
let timeout = Lwt_unix.sleep (Int64.to_float state.delay) in let timeout = Lwt_unix.sleep (Int64.to_float state.delay) in
tzforce state.delegates >>=? fun delegates -> get_delegates cctxt state >>=? fun delegates ->
filter_p (allowed_to_endorse cctxt bi) delegates >>=? fun delegates -> filter_p (allowed_to_endorse cctxt bi) delegates >>=? fun delegates ->
state.pending <- Some { state.pending <- Some {
time ; time ;
@ -169,20 +176,12 @@ let create
(cctxt: #Proto_alpha.full) (cctxt: #Proto_alpha.full)
?(max_past=110L) ?(max_past=110L)
~delay ~delay
contracts delegates
block_stream block_stream
= =
let state_maker _ _ = let state_maker _ _ =
let contracts = match contracts with let state = create_state delegates (Int64.of_int delay) in
| [] ->
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
return state return state
in in

View File

@ -26,8 +26,10 @@ type state = {
genesis: Block_hash.t ; genesis: Block_hash.t ;
index : Context.index ; index : Context.index ;
(* see [get_delegates] below to find delegates when the list is empty *)
delegates: public_key_hash list ;
(* lazy-initialisation with retry-on-error *) (* lazy-initialisation with retry-on-error *)
delegates: public_key_hash list tzlazy ;
constants: Constants.t tzlazy ; constants: Constants.t tzlazy ;
(* truly mutable *) (* truly mutable *)
@ -45,6 +47,13 @@ let create_state genesis index delegates constants best =
future_slots = [] ; future_slots = [] ;
} }
let get_delegates cctxt state = match state.delegates with
| [] ->
Client_keys.get_keys cctxt >>=? fun keys ->
let delegates = List.map (fun (_,pkh,_,_) -> pkh) keys in
return delegates
| (_ :: _) as delegates -> return delegates
let generate_seed_nonce () = let generate_seed_nonce () =
match Nonce.of_bytes @@ match Nonce.of_bytes @@
Rand.generate Constants.nonce_length with Rand.generate Constants.nonce_length with
@ -456,7 +465,7 @@ let insert_block
drop_old_slots drop_old_slots
~before:(Time.add state.best.timestamp (-1800L)) state ; ~before:(Time.add state.best.timestamp (-1800L)) state ;
end ; end ;
tzforce state.delegates >>=? fun delegates -> get_delegates cctxt state >>=? fun delegates ->
get_baking_slot cctxt ?max_priority bi delegates >>= function get_baking_slot cctxt ?max_priority bi delegates >>= function
| [] -> | [] ->
lwt_debug lwt_debug
@ -736,14 +745,6 @@ let create
= =
let state_maker genesis_hash bi = let state_maker genesis_hash bi =
let delegates = match delegates with
| [] ->
tzlazy (fun () ->
Client_keys.get_keys cctxt >>=? fun keys ->
let delegates = List.map (fun (_,pkh,_,_) -> pkh) keys in
return delegates
)
| _ :: _ -> tzlazy (fun () -> return delegates) in
let constants = let constants =
tzlazy (fun () -> Alpha_services.Constants.all cctxt (`Main, `Head 0)) in tzlazy (fun () -> Alpha_services.Constants.all cctxt (`Main, `Head 0)) in
Client_baking_simulator.load_context ~context_path >>= fun index -> Client_baking_simulator.load_context ~context_path >>= fun index ->