From 85b01ea8029c454d6abe64fd9f8ceed4effb7e2a Mon Sep 17 00:00:00 2001 From: Ryan Tan Date: Mon, 5 Aug 2024 12:31:29 +0100 Subject: [PATCH] Nonce worker: only reveal nonce if cycle position is less than nonce_revelation_threshold --- src/proto_alpha/lib_delegate/baking_nonces.ml | 38 ++++++++++++------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/src/proto_alpha/lib_delegate/baking_nonces.ml b/src/proto_alpha/lib_delegate/baking_nonces.ml index 7d38c52f8db9..2cd8adf11b34 100644 --- a/src/proto_alpha/lib_delegate/baking_nonces.ml +++ b/src/proto_alpha/lib_delegate/baking_nonces.ml @@ -310,10 +310,10 @@ let try_migrate_legacy_nonces state = - nonces that are live Nonces that are not relevant can be dropped. *) -let partition_unrevealed_nonces {cctxt; chain; _} nonces current_cycle - current_level = +let partition_unrevealed_nonces {cctxt; chain; _} nonces + (current_level : Level.t) proto_nonce_revelation_threshold = let open Lwt_result_syntax in - match Cycle.pred current_cycle with + match Cycle.pred current_level.cycle with | None -> (* This will be [None] iff [current_cycle = 0] which only occurs during genesis. *) @@ -325,8 +325,13 @@ let partition_unrevealed_nonces {cctxt; chain; _} nonces current_cycle (fun _hash nonce_data (nonces_to_reveal, live) -> let {nonce_hash; cycle; level; nonce_state; _} = nonce_data in match cycle with - | cycle when Cycle.(cycle = previous_cycle) -> ( - (* Only process nonces that are part of previous cycle. *) + | cycle + when Cycle.(cycle = previous_cycle) + && current_level.cycle_position + < proto_nonce_revelation_threshold -> ( + (* Only process nonces produced in the previous cycle and where + nonce_revelation_threshold has not passed. See [Nonce_storage.check_unrevealed] + *) let+ nonce_info = Alpha_services.Nonce.get cctxt (chain, `Head 0) level in @@ -341,7 +346,7 @@ let partition_unrevealed_nonces {cctxt; chain; _} nonces current_cycle | Revealed injection_level, Missing expected_nonce_hash when Nonce_hash.(nonce_hash = expected_nonce_hash) -> if - Raw_level.diff current_level injection_level + Raw_level.diff current_level.level injection_level > reinjection_threshold then (* [reinjection_threshold] levels have passed since nonce revelation @@ -354,13 +359,13 @@ let partition_unrevealed_nonces {cctxt; chain; _} nonces current_cycle | Revealed _injection_level, Forgotten | Revealed _injection_level, Revealed _ -> (nonces_to_reveal, live)) - | cycle when Cycle.(cycle = current_cycle) -> + | cycle when Cycle.(cycle = current_level.cycle) -> (* Nothing to do if nonce was committed as part of current cycle. *) return (nonces_to_reveal, add live nonce_data) | _ -> - (* Nonces not part of current or previous cycles are orphaned and can - be dropped. *) + (* Nonces that are not part of current or previous cycle or where cycle position has + passed `nonce_revelation_threshold` can be dropped. *) return (nonces_to_reveal, live)) nonces (empty, empty) @@ -487,11 +492,13 @@ let reveal_potential_nonces state new_proposal = let*! () = Events.(emit cannot_read_nonces err) in return_unit | Ok nonces -> ( - let* {cycle; level; _} = - Plugin.RPC.current_level cctxt (chain, `Head 0) - in + let* current_level = Plugin.RPC.current_level cctxt (chain, `Head 0) in let*! partitioned_nonces = - partition_unrevealed_nonces state nonces cycle level + partition_unrevealed_nonces + state + nonces + current_level + state.constants.parametric.nonce_revelation_threshold in match partitioned_nonces with | Error err -> @@ -523,7 +530,10 @@ let reveal_potential_nonces state new_proposal = let nonce_with_new_states = Nonce_hash.Map.map (fun nonce_data -> - {nonce_data with nonce_state = Revealed level}) + { + nonce_data with + nonce_state = Revealed current_level.level; + }) nonces_to_reveal in Nonce_hash.Map.fold -- GitLab