From 34e1764bce29eaa52da758e8bd7ffc8e85ed8e06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Henry?= Date: Tue, 14 Feb 2017 10:33:34 +0100 Subject: [PATCH] Client/Baker: let the baking daemon reveal nonces --- .../bootstrap/baker/client_mining_forge.ml | 39 ++++++++++++++++++- .../bootstrap/baker/client_mining_forge.mli | 6 +++ .../bootstrap/baker/client_mining_main.ml | 16 ++------ .../baker/client_mining_revelation.ml | 26 ++----------- 4 files changed, 50 insertions(+), 37 deletions(-) diff --git a/src/client/embedded/bootstrap/baker/client_mining_forge.ml b/src/client/embedded/bootstrap/baker/client_mining_forge.ml index e21ee087d..cf46da47b 100644 --- a/src/client/embedded/bootstrap/baker/client_mining_forge.ml +++ b/src/client/embedded/bootstrap/baker/client_mining_forge.ml @@ -280,7 +280,44 @@ let compute_timeout { future_slots } = else Lwt_unix.sleep (Int64.to_float delay) -let insert_block cctxt ?max_priority state (bi: Client_mining_blocks.block_info) = +let get_unrevealed_nonces cctxt ?(force = false) block = + Client_proto_rpcs.Context.next_level cctxt block >>=? fun level -> + let cur_cycle = level.cycle in + match Cycle.pred cur_cycle with + | None -> return [] + | Some cycle -> + Client_mining_blocks.blocks_from_cycle + cctxt block cycle >>=? fun block_infos -> + map_filter_s (fun (bi : Client_mining_blocks.block_info) -> + Client_proto_nonces.find cctxt bi.hash >>= function + | None -> return None + | Some nonce -> + if force then + return (Some (bi.hash, (bi.level.level, nonce))) + else + Client_proto_rpcs.Context.Nonce.get + cctxt block bi.level.level >>=? function + | Missing nonce_hash + when Nonce.check_hash nonce nonce_hash -> + cctxt.warning "Found nonce for %a (level: %a)@." + Block_hash.pp_short bi.hash + Level.pp bi.level >>= fun () -> + return (Some (bi.hash, (bi.level.level, nonce))) + | Missing _nonce_hash -> + cctxt.error "Incoherent nonce for level %a" + Raw_level.pp bi.level.level >>= fun () -> + return None + | Forgotten -> return None + | Revealed _ -> return None) + block_infos + +let insert_block + cctxt ?max_priority state (bi: Client_mining_blocks.block_info) = + begin + get_unrevealed_nonces cctxt (`Hash bi.hash) >>=? fun nonces -> + Client_mining_revelation.forge_seed_nonce_revelation + cctxt ~force:true (`Hash bi.hash) (List.map snd nonces) + end >>= fun _ignore_error -> if Fitness.compare state.best_fitness bi.fitness < 0 then state.best_fitness <- bi.fitness ; get_mining_slot cctxt ?max_priority bi state.delegates >>= function diff --git a/src/client/embedded/bootstrap/baker/client_mining_forge.mli b/src/client/embedded/bootstrap/baker/client_mining_forge.mli index 2683f6094..f5643b873 100644 --- a/src/client/embedded/bootstrap/baker/client_mining_forge.mli +++ b/src/client/embedded/bootstrap/baker/client_mining_forge.mli @@ -52,3 +52,9 @@ val create: Client_mining_blocks.block_info list Lwt_stream.t -> Client_mining_operations.valid_endorsement Lwt_stream.t -> unit Lwt.t + +val get_unrevealed_nonces: + Client_commands.context -> + ?force:bool -> + Client_proto_rpcs.block -> + (Block_hash.t * (Raw_level.t * Nonce.t)) list tzresult Lwt.t diff --git a/src/client/embedded/bootstrap/baker/client_mining_main.ml b/src/client/embedded/bootstrap/baker/client_mining_main.ml index e416659f8..39bffb409 100644 --- a/src/client/embedded/bootstrap/baker/client_mining_main.ml +++ b/src/client/embedded/bootstrap/baker/client_mining_main.ml @@ -86,19 +86,9 @@ let reveal_block_nonces cctxt ?force block_hashes = let reveal_nonces cctxt ?force () = let block = Client_proto_args.block () in - Client_proto_rpcs.Context.next_level cctxt block >>=? fun level -> - let cur_cycle = level.cycle in - get_predecessor_cycle cctxt cur_cycle >>= fun cycle -> - Client_mining_blocks.blocks_from_cycle cctxt block cycle >>=? fun block_infos -> - map_filter_s (fun (bi : Client_mining_blocks.block_info) -> - Client_proto_nonces.find cctxt bi.hash >>= function - | None -> return None - | Some nonce -> - cctxt.warning "Found nonce for %a (level: %a)@." - Block_hash.pp_short bi.hash Level.pp bi.level >>= fun () -> - return (Some (bi.hash, (bi.level.level, nonce)))) - block_infos >>=? fun blocks -> - do_reveal cctxt ?force block blocks + Client_mining_forge.get_unrevealed_nonces + cctxt ?force block >>=? fun nonces -> + do_reveal cctxt ?force block nonces open Client_proto_args diff --git a/src/client/embedded/bootstrap/baker/client_mining_revelation.ml b/src/client/embedded/bootstrap/baker/client_mining_revelation.ml index c448e22f4..7548e4372 100644 --- a/src/client/embedded/bootstrap/baker/client_mining_revelation.ml +++ b/src/client/embedded/bootstrap/baker/client_mining_revelation.ml @@ -24,29 +24,9 @@ let inject_seed_nonce_revelation cctxt block ?force ?wait nonces = type Error_monad.error += Bad_revelation -let forge_seed_nonce_revelation cctxt - block ?(force = false) redempted_nonces = - begin - if force then return redempted_nonces else - map_filter_s (fun (level, nonce) -> - Client_proto_rpcs.Context.Nonce.get cctxt block level >>=? function - | Forgotten -> - cctxt.message "Too late revelation for level %a" - Raw_level.pp level >>= fun () -> - return None - | Revealed _ -> - cctxt.message "Ignoring previously-revealed nonce for level %a" - Raw_level.pp level >>= fun () -> - return None - | Missing nonce_hash -> - if Nonce.check_hash nonce nonce_hash then - return (Some (level, nonce)) - else - lwt_log_error "Incoherent nonce for level %a" - Raw_level.pp level >>= fun () -> - return None) - redempted_nonces - end >>=? fun nonces -> +let forge_seed_nonce_revelation + (cctxt: Client_commands.context) + block ?(force = false) nonces = match nonces with | [] -> cctxt.message "No nonce to reveal" >>= fun () ->