Baker: do not generate empty endorsements

This commit is contained in:
Benjamin Canou 2018-06-18 18:56:52 +02:00 committed by Grégoire Henry
parent 7500743a4d
commit 41707f58d8
2 changed files with 40 additions and 33 deletions

View File

@ -19,8 +19,8 @@ let get_signing_slots cctxt ?(chain = `Main) block delegate level =
~levels:[level] ~levels:[level]
~delegates:[delegate] ~delegates:[delegate]
(chain, block) >>=? function (chain, block) >>=? function
| [{ slots }] -> return slots | [{ slots }] -> return (Some slots)
| _ -> (* TODO? log this case *) return [] | _ -> return None
let inject_endorsement let inject_endorsement
(cctxt : #Proto_alpha.full) (cctxt : #Proto_alpha.full)
@ -73,8 +73,8 @@ let forge_endorsement (cctxt : #Proto_alpha.full)
| None -> | None ->
get_signing_slots get_signing_slots
cctxt ~chain block src_pkh level >>=? function cctxt ~chain block src_pkh level >>=? function
| [] -> cctxt#error "No slot found at level %a" Raw_level.pp level | None -> cctxt#error "No slot found at level %a" Raw_level.pp level
| slots -> return slots | Some slots -> return slots
end >>=? fun slots -> end >>=? fun slots ->
Shell_services.Blocks.hash cctxt ~chain ~block () >>=? fun hash -> Shell_services.Blocks.hash cctxt ~chain ~block () >>=? fun hash ->
inject_endorsement cctxt ~chain ?async block hash level src_sk slots src_pkh >>=? fun oph -> 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 b = `Hash (block.hash, 0) in
let level = block.level in let level = block.level in
get_signing_slots cctxt b delegate level >>=? fun slots -> get_signing_slots cctxt b delegate level >>=? fun slots ->
lwt_debug "Found slots for %a/%s (%d)" match slots with
Block_hash.pp_short block.hash name (List.length slots) >>= fun () -> | None ->
previously_endorsed_level cctxt delegate level >>=? function lwt_debug "No slot found for %a/%s"
| true -> Block_hash.pp_short block.hash name >>= fun () ->
lwt_debug "Level %a (or higher) previously endorsed: do not endorse." return ()
Raw_level.pp level >>= return | Some slots ->
| false -> lwt_debug "Found slots for %a/%s (%d)"
match Client_baking_scheduling.sleep_until time with Block_hash.pp_short block.hash name (List.length slots) >>= fun () ->
| None -> previously_endorsed_level cctxt delegate level >>=? function
lwt_debug "Endorsment opportunity is passed." >>= fun () -> | true ->
return () lwt_debug "Level %a (or higher) previously endorsed: do not endorse."
| Some timeout -> Raw_level.pp level >>= return
let neu = { time ; timeout ; delegate ; block; slots } in | false ->
match List.find_opt (fun { delegate = d } -> delegate = d) state.to_endorse with match Client_baking_scheduling.sleep_until time with
| None -> | None ->
state.to_endorse <- neu :: state.to_endorse; lwt_debug "Endorsment opportunity is passed." >>= fun () ->
return () return ()
| Some old -> | Some timeout ->
if Fitness.compare old.block.fitness neu.block.fitness < 0 then begin let neu = { time ; timeout ; delegate ; block; slots } in
let without_old = match List.find_opt (fun { delegate = d } -> delegate = d) state.to_endorse with
List.filter (fun to_end -> to_end <> old) state.to_endorse in | None ->
state.to_endorse <- neu :: without_old; state.to_endorse <- neu :: state.to_endorse;
return () return ()
end else | Some old ->
lwt_debug "Block %a is not the fittest: do not endorse." if Fitness.compare old.block.fitness neu.block.fitness < 0 then begin
Block_hash.pp_short neu.block.hash >>= fun () -> let without_old =
return () 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 = let prepare_endorsement (cctxt : #Proto_alpha.full) ~(max_past:int64) state bi =
get_delegates cctxt state >>=? fun delegates -> get_delegates cctxt state >>=? fun delegates ->

View File

@ -514,7 +514,8 @@ let pop_baking_slots state =
let filter_invalid_operations (cctxt : #full) state block_info (operations : packed_operation list list) = let filter_invalid_operations (cctxt : #full) state block_info (operations : packed_operation list list) =
let open Client_baking_simulator in 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 -> begin_construction cctxt state.index block_info >>=? fun initial_inc ->
let endorsements = List.nth operations 0 in let endorsements = List.nth operations 0 in
let votes = List.nth operations 1 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 || (Fitness.compare state.best.fitness shell_header.fitness = 0
&& Time.compare shell_header.timestamp state.best.timestamp < 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 if seed_nonce_hash <> None then
State.record cctxt pkh level >>=? fun () ->
Client_baking_nonces.add cctxt block_hash seed_nonce Client_baking_nonces.add cctxt block_hash seed_nonce
|> trace_exn (Failure "Error while recording block") |> trace_exn (Failure "Error while recording block")
else else
@ -712,7 +712,8 @@ let bake (cctxt : #Proto_alpha.full) state =
(* /core function; back to logging and info *) (* /core function; back to logging and info *)
|> trace_exn (Failure "Error while injecting block") >>=? fun block_hash -> |> 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 -> Client_keys.Public_key_hash.name cctxt delegate >>=? fun name ->
cctxt#message cctxt#message
"Injected block %a for %s after %a (level %a, slot %d, fitness %a, operations %a)" "Injected block %a for %s after %a (level %a, slot %d, fitness %a, operations %a)"