From 41707f58d82f386c745147cc39a1a4e54ac22ae0 Mon Sep 17 00:00:00 2001 From: Benjamin Canou Date: Mon, 18 Jun 2018 18:56:52 +0200 Subject: [PATCH] Baker: do not generate empty endorsements --- .../lib_delegate/client_baking_endorsement.ml | 64 ++++++++++--------- .../lib_delegate/client_baking_forge.ml | 9 +-- 2 files changed, 40 insertions(+), 33 deletions(-) diff --git a/src/proto_alpha/lib_delegate/client_baking_endorsement.ml b/src/proto_alpha/lib_delegate/client_baking_endorsement.ml index 89905992f..c1a6d317a 100644 --- a/src/proto_alpha/lib_delegate/client_baking_endorsement.ml +++ b/src/proto_alpha/lib_delegate/client_baking_endorsement.ml @@ -19,8 +19,8 @@ let get_signing_slots cctxt ?(chain = `Main) block delegate level = ~levels:[level] ~delegates:[delegate] (chain, block) >>=? function - | [{ slots }] -> return slots - | _ -> (* TODO? log this case *) return [] + | [{ slots }] -> return (Some slots) + | _ -> return None let inject_endorsement (cctxt : #Proto_alpha.full) @@ -73,8 +73,8 @@ let forge_endorsement (cctxt : #Proto_alpha.full) | None -> get_signing_slots cctxt ~chain block src_pkh level >>=? function - | [] -> cctxt#error "No slot found at level %a" Raw_level.pp level - | slots -> return slots + | None -> cctxt#error "No slot found at level %a" Raw_level.pp level + | Some slots -> return slots end >>=? fun slots -> Shell_services.Blocks.hash cctxt ~chain ~block () >>=? fun hash -> inject_endorsement cctxt ~chain ?async block hash level src_sk slots src_pkh >>=? fun oph -> @@ -167,33 +167,39 @@ let allowed_to_endorse cctxt state (block: Client_baking_blocks.block_info) dele let b = `Hash (block.hash, 0) in let level = block.level in get_signing_slots cctxt b delegate level >>=? fun slots -> - lwt_debug "Found slots for %a/%s (%d)" - Block_hash.pp_short block.hash name (List.length slots) >>= fun () -> - previously_endorsed_level cctxt delegate level >>=? function - | true -> - lwt_debug "Level %a (or higher) previously endorsed: do not endorse." - Raw_level.pp level >>= return - | false -> - match Client_baking_scheduling.sleep_until time with - | None -> - lwt_debug "Endorsment opportunity is passed." >>= fun () -> - return () - | Some timeout -> - let neu = { time ; timeout ; delegate ; block; slots } in - match List.find_opt (fun { delegate = d } -> delegate = d) state.to_endorse with + match slots with + | None -> + lwt_debug "No slot found for %a/%s" + Block_hash.pp_short block.hash name >>= fun () -> + return () + | Some slots -> + lwt_debug "Found slots for %a/%s (%d)" + Block_hash.pp_short block.hash name (List.length slots) >>= fun () -> + previously_endorsed_level cctxt delegate level >>=? function + | true -> + lwt_debug "Level %a (or higher) previously endorsed: do not endorse." + Raw_level.pp level >>= return + | false -> + match Client_baking_scheduling.sleep_until time with | None -> - state.to_endorse <- neu :: state.to_endorse; + lwt_debug "Endorsment opportunity is passed." >>= fun () -> return () - | Some old -> - if Fitness.compare old.block.fitness neu.block.fitness < 0 then begin - let without_old = - List.filter (fun to_end -> to_end <> old) state.to_endorse in - state.to_endorse <- neu :: without_old; - return () - end else - lwt_debug "Block %a is not the fittest: do not endorse." - Block_hash.pp_short neu.block.hash >>= fun () -> - return () + | Some timeout -> + let neu = { time ; timeout ; delegate ; block; slots } in + match List.find_opt (fun { delegate = d } -> delegate = d) state.to_endorse with + | None -> + state.to_endorse <- neu :: state.to_endorse; + return () + | Some old -> + if Fitness.compare old.block.fitness neu.block.fitness < 0 then begin + let without_old = + List.filter (fun to_end -> to_end <> old) state.to_endorse in + state.to_endorse <- neu :: without_old; + return () + end else + lwt_debug "Block %a is not the fittest: do not endorse." + Block_hash.pp_short neu.block.hash >>= fun () -> + return () let prepare_endorsement (cctxt : #Proto_alpha.full) ~(max_past:int64) state bi = get_delegates cctxt state >>=? fun delegates -> diff --git a/src/proto_alpha/lib_delegate/client_baking_forge.ml b/src/proto_alpha/lib_delegate/client_baking_forge.ml index 738900c01..715e82cb4 100644 --- a/src/proto_alpha/lib_delegate/client_baking_forge.ml +++ b/src/proto_alpha/lib_delegate/client_baking_forge.ml @@ -514,7 +514,8 @@ let pop_baking_slots state = let filter_invalid_operations (cctxt : #full) state block_info (operations : packed_operation list list) = let open Client_baking_simulator in - lwt_debug "Starting client-side validation" >>= fun () -> + lwt_debug "Starting client-side validation %a" + Block_hash.pp block_info.Client_baking_blocks.hash >>= fun () -> begin_construction cctxt state.index block_info >>=? fun initial_inc -> let endorsements = List.nth operations 0 in let votes = List.nth operations 1 in @@ -656,9 +657,8 @@ let fit_enough (state: state) (shell_header: Block_header.shell_header) = || (Fitness.compare state.best.fitness shell_header.fitness = 0 && Time.compare shell_header.timestamp state.best.timestamp < 0) -let record_nonce_hash cctxt level block_hash seed_nonce seed_nonce_hash pkh = +let record_nonce_hash cctxt block_hash seed_nonce seed_nonce_hash = if seed_nonce_hash <> None then - State.record cctxt pkh level >>=? fun () -> Client_baking_nonces.add cctxt block_hash seed_nonce |> trace_exn (Failure "Error while recording block") else @@ -712,7 +712,8 @@ let bake (cctxt : #Proto_alpha.full) state = (* /core function; back to logging and info *) |> trace_exn (Failure "Error while injecting block") >>=? fun block_hash -> - record_nonce_hash cctxt level block_hash seed_nonce seed_nonce_hash src_pkh >>=? fun () -> + State.record cctxt src_pkh level >>=? fun () -> + record_nonce_hash cctxt block_hash seed_nonce seed_nonce_hash >>=? fun () -> Client_keys.Public_key_hash.name cctxt delegate >>=? fun name -> cctxt#message "Injected block %a for %s after %a (level %a, slot %d, fitness %a, operations %a)"