Alpha/Endorser: wait for first block

This commit is contained in:
Raphaël Proust 2018-06-13 14:38:16 +08:00 committed by Grégoire Henry
parent f4cadd37a5
commit b0e9e44673

View File

@ -248,13 +248,16 @@ let check_error f =
| Ok () -> Lwt.return_unit | Ok () -> Lwt.return_unit
| Error errs -> lwt_log_error "Error while endorsing:@\n%a" pp_print_error errs | Error errs -> lwt_log_error "Error while endorsing:@\n%a" pp_print_error errs
let create (cctxt : #Proto_alpha.full) ?(max_past=110L) ~delay contracts (block_stream : Client_baking_blocks.block_info tzresult Lwt_stream.t) = let create
(cctxt: #Proto_alpha.full)
~max_past
~delay
contracts
block_stream
bi =
lwt_log_info "Preparing endorsement daemon" >>= fun () -> lwt_log_info "Preparing endorsement daemon" >>= fun () ->
Lwt_stream.get block_stream >>= function
| None | Some (Error _) ->
cctxt#error "Can't fetch the current block head."
| Some (Ok head) ->
(* statefulness setup *)
let last_get_block = ref None in let last_get_block = ref None in
let get_block () = let get_block () =
match !last_get_block with match !last_get_block with
@ -265,6 +268,7 @@ let create (cctxt : #Proto_alpha.full) ?(max_past=110L) ~delay contracts (block_
| Some t -> t in | Some t -> t in
let state = create_state contracts (Int64.of_int delay) in let state = create_state contracts (Int64.of_int delay) in
(* main loop *)
let rec worker_loop () = let rec worker_loop () =
begin begin
let timeout = compute_timeout state in let timeout = compute_timeout state in
@ -283,6 +287,26 @@ let create (cctxt : #Proto_alpha.full) ?(max_past=110L) ~delay contracts (block_
end >>= fun () -> end >>= fun () ->
worker_loop () in worker_loop () in
check_error (prepare_endorsement cctxt ~max_past state head) >>= fun () -> (* ignition *)
check_error (prepare_endorsement cctxt ~max_past state bi) >>= fun () ->
lwt_log_info "Starting endorsement daemon" >>= fun () -> lwt_log_info "Starting endorsement daemon" >>= fun () ->
worker_loop () worker_loop ()
(* A wrapper around the main create function (above) to wait for the initial
block. *)
let create
(cctxt: #Proto_alpha.full)
?(max_past=110L)
~delay
contracts
(block_stream: Client_baking_blocks.block_info tzresult Lwt_stream.t) =
let rec wait_for_first_block () =
Lwt_stream.get block_stream >>= function
| None | Some (Error _) ->
cctxt#message "Can't fetch the current block head. Retrying soon." >>= fun () ->
(* NOTE: this is not a tight loop because of Lwt_stream.get *)
wait_for_first_block ()
| Some (Ok bi) ->
create cctxt ~max_past ~delay contracts block_stream bi
in
wait_for_first_block ()