From 3e71d2171344b65077df5a878cd5162dfa207da0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Proust?= Date: Thu, 14 Jun 2018 09:36:48 +0800 Subject: [PATCH] Alpha/Baker: better naming for error management --- .../lib_delegate/client_baking_endorsement.ml | 2 +- .../lib_delegate/client_baking_forge.ml | 25 ++++++++----------- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/src/proto_alpha/lib_delegate/client_baking_endorsement.ml b/src/proto_alpha/lib_delegate/client_baking_endorsement.ml index 1017736d6..a1bf3b641 100644 --- a/src/proto_alpha/lib_delegate/client_baking_endorsement.ml +++ b/src/proto_alpha/lib_delegate/client_baking_endorsement.ml @@ -304,7 +304,7 @@ let create | `Hash (Some (Ok bi)) -> Lwt.cancel timeout; last_get_block := None; - check_error (prepare_endorsement cctxt ~max_past state bi) + check_error @@ prepare_endorsement cctxt ~max_past state bi | `Timeout -> begin endorse_for cctxt state.to_endorse >>= function diff --git a/src/proto_alpha/lib_delegate/client_baking_forge.ml b/src/proto_alpha/lib_delegate/client_baking_forge.ml index ef6321d0d..ea40999ad 100644 --- a/src/proto_alpha/lib_delegate/client_baking_forge.ml +++ b/src/proto_alpha/lib_delegate/client_baking_forge.ml @@ -438,6 +438,8 @@ let get_delegates cctxt state = return (List.map (fun (_,pkh,_,_) -> pkh) keys) | _ :: _ as delegates -> return delegates + + let insert_block (cctxt : #Proto_alpha.full) ?max_priority state (bi: Client_baking_blocks.block_info) = begin @@ -476,13 +478,6 @@ let pop_baking_slots state = state.future_slots <- future_slots ; slots -let insert_blocks cctxt ?max_priority state bi = - insert_block cctxt ?max_priority state bi >>= function - | Ok () -> - Lwt.return_unit - | Error err -> - lwt_log_error "Error: %a" pp_print_error err - let bake_slot cctxt state @@ -612,6 +607,10 @@ let bake (cctxt : #Proto_alpha.full) state = lwt_debug "No valid candidates." >>= fun () -> return () +let check_error p = + p >>= function + | Ok () -> Lwt.return_unit + | Error errs -> lwt_log_error "Error while baking:@\n%a" pp_print_error errs (* [create] starts the main loop of the baker. The loop monitors new blocks and starts individual baking operations when baking-slots are available to any of @@ -636,7 +635,7 @@ let create t | Some t -> t in let state = create_state genesis_hash delegates bi in - insert_blocks cctxt ?max_priority state bi >>= fun () -> + check_error @@ insert_block cctxt ?max_priority state bi >>= fun () -> (* main loop *) let rec worker_loop () = @@ -660,18 +659,14 @@ let create lwt_debug "Discoverered block: %a" Block_hash.pp_short bi.Client_baking_blocks.hash >>= fun () -> - insert_blocks cctxt ?max_priority state bi + check_error @@ insert_block cctxt ?max_priority state bi end | `Timeout -> (* main event: it's baking time *) lwt_debug "Waking up for baking..." >>= fun () -> - begin - (* core functionality *) - bake cctxt state >>= function - | Ok () -> Lwt.return_unit - | Error errs -> lwt_log_error "Error while baking:@\n%a" pp_print_error errs - end + (* core functionality *) + check_error @@ bake cctxt state end >>= fun () -> (* and restart *)