From 9590455a86970b5aca88849619af1d7b00276fd1 Mon Sep 17 00:00:00 2001 From: Julien Tesson Date: Thu, 8 Feb 2024 13:22:55 +0100 Subject: [PATCH 01/11] proto: add pending_denunciations_storage --- src/proto_alpha/lib_protocol/TEZOS_PROTOCOL | 1 + src/proto_alpha/lib_protocol/dune | 4 ++++ .../lib_protocol/pending_denunciations_storage.ml | 6 ++++++ .../lib_protocol/pending_denunciations_storage.mli | 6 ++++++ 4 files changed, 17 insertions(+) create mode 100644 src/proto_alpha/lib_protocol/pending_denunciations_storage.ml create mode 100644 src/proto_alpha/lib_protocol/pending_denunciations_storage.mli diff --git a/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL b/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL index d9fe1c4a6424..219574dce74e 100644 --- a/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL +++ b/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL @@ -165,6 +165,7 @@ "Contract_delegate_storage", "Stake_storage", "Unstaked_frozen_deposits_storage", + "Pending_denunciations_storage", "Unstake_requests_storage", "Staking_pseudotokens_storage", diff --git a/src/proto_alpha/lib_protocol/dune b/src/proto_alpha/lib_protocol/dune index f026fdf0eaca..e12415dba87c 100644 --- a/src/proto_alpha/lib_protocol/dune +++ b/src/proto_alpha/lib_protocol/dune @@ -182,6 +182,7 @@ Contract_delegate_storage Stake_storage Unstaked_frozen_deposits_storage + Pending_denunciations_storage Unstake_requests_storage Staking_pseudotokens_storage Contract_storage @@ -478,6 +479,7 @@ contract_delegate_storage.ml contract_delegate_storage.mli stake_storage.ml stake_storage.mli unstaked_frozen_deposits_storage.ml unstaked_frozen_deposits_storage.mli + pending_denunciations_storage.ml pending_denunciations_storage.mli unstake_requests_storage.ml unstake_requests_storage.mli staking_pseudotokens_storage.ml staking_pseudotokens_storage.mli contract_storage.ml contract_storage.mli @@ -776,6 +778,7 @@ contract_delegate_storage.ml contract_delegate_storage.mli stake_storage.ml stake_storage.mli unstaked_frozen_deposits_storage.ml unstaked_frozen_deposits_storage.mli + pending_denunciations_storage.ml pending_denunciations_storage.mli unstake_requests_storage.ml unstake_requests_storage.mli staking_pseudotokens_storage.ml staking_pseudotokens_storage.mli contract_storage.ml contract_storage.mli @@ -1058,6 +1061,7 @@ contract_delegate_storage.ml contract_delegate_storage.mli stake_storage.ml stake_storage.mli unstaked_frozen_deposits_storage.ml unstaked_frozen_deposits_storage.mli + pending_denunciations_storage.ml pending_denunciations_storage.mli unstake_requests_storage.ml unstake_requests_storage.mli staking_pseudotokens_storage.ml staking_pseudotokens_storage.mli contract_storage.ml contract_storage.mli diff --git a/src/proto_alpha/lib_protocol/pending_denunciations_storage.ml b/src/proto_alpha/lib_protocol/pending_denunciations_storage.ml new file mode 100644 index 000000000000..5f6aa39ffbbc --- /dev/null +++ b/src/proto_alpha/lib_protocol/pending_denunciations_storage.ml @@ -0,0 +1,6 @@ +(*****************************************************************************) +(* *) +(* SPDX-License-Identifier: MIT *) +(* Copyright (c) 2024 Nomadic Labs. *) +(* *) +(*****************************************************************************) diff --git a/src/proto_alpha/lib_protocol/pending_denunciations_storage.mli b/src/proto_alpha/lib_protocol/pending_denunciations_storage.mli new file mode 100644 index 000000000000..5f6aa39ffbbc --- /dev/null +++ b/src/proto_alpha/lib_protocol/pending_denunciations_storage.mli @@ -0,0 +1,6 @@ +(*****************************************************************************) +(* *) +(* SPDX-License-Identifier: MIT *) +(* Copyright (c) 2024 Nomadic Labs. *) +(* *) +(*****************************************************************************) -- GitLab From 1d3bd9efd2dab700ee9da4fe0f38649e8dc5c155 Mon Sep 17 00:00:00 2001 From: Julien Tesson Date: Thu, 8 Feb 2024 13:14:50 +0100 Subject: [PATCH 02/11] proto/pending_denunciations: encapsulate find --- .../lib_protocol/pending_denunciations_storage.ml | 5 +++++ .../lib_protocol/pending_denunciations_storage.mli | 8 ++++++++ src/proto_alpha/lib_protocol/unstake_requests_storage.ml | 5 +---- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/proto_alpha/lib_protocol/pending_denunciations_storage.ml b/src/proto_alpha/lib_protocol/pending_denunciations_storage.ml index 5f6aa39ffbbc..d7282bbd3558 100644 --- a/src/proto_alpha/lib_protocol/pending_denunciations_storage.ml +++ b/src/proto_alpha/lib_protocol/pending_denunciations_storage.ml @@ -4,3 +4,8 @@ (* Copyright (c) 2024 Nomadic Labs. *) (* *) (*****************************************************************************) + +let find ctxt delegate = + let open Lwt_result_syntax in + let* denunciations_opt = Storage.Pending_denunciations.find ctxt delegate in + return @@ Option.value denunciations_opt ~default:[] diff --git a/src/proto_alpha/lib_protocol/pending_denunciations_storage.mli b/src/proto_alpha/lib_protocol/pending_denunciations_storage.mli index 5f6aa39ffbbc..e74be7ed5167 100644 --- a/src/proto_alpha/lib_protocol/pending_denunciations_storage.mli +++ b/src/proto_alpha/lib_protocol/pending_denunciations_storage.mli @@ -4,3 +4,11 @@ (* Copyright (c) 2024 Nomadic Labs. *) (* *) (*****************************************************************************) + +(** Returns the pending denunciations list of the given delegate. + It returns an empty list if none are registered. + *) +val find : + Raw_context.t -> + Signature.public_key_hash -> + Denunciations_repr.item list tzresult Lwt.t diff --git a/src/proto_alpha/lib_protocol/unstake_requests_storage.ml b/src/proto_alpha/lib_protocol/unstake_requests_storage.ml index ec1e78f7fdf9..817b5c8d5ea8 100644 --- a/src/proto_alpha/lib_protocol/unstake_requests_storage.ml +++ b/src/proto_alpha/lib_protocol/unstake_requests_storage.ml @@ -192,10 +192,7 @@ module For_RPC = struct and remove them from the slashing events (since they haven't been applied yet). Another solution would be to add the slashing cycle in Storage.Contract.Slashed_deposits, but since it's only used for this specific RPC, let's not. *) - let* denunciations_opt = - Storage.Pending_denunciations.find ctxt delegate - in - let denunciations = Option.value denunciations_opt ~default:[] in + let* denunciations = Pending_denunciations_storage.find ctxt delegate in let not_yet_slashed_pct = if is_last_of_cycle then Percentage.p0 else -- GitLab From dd240816b28bb591c455ea51f80c4700cc56665b Mon Sep 17 00:00:00 2001 From: Julien Tesson Date: Thu, 8 Feb 2024 13:33:33 +0100 Subject: [PATCH 03/11] proto/pending_denunciations_storage: add_denunciation --- .../delegate_slashed_deposits_storage.ml | 21 ++++++------------- .../pending_denunciations_storage.ml | 16 ++++++++++++++ .../pending_denunciations_storage.mli | 9 ++++++++ 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/proto_alpha/lib_protocol/delegate_slashed_deposits_storage.ml b/src/proto_alpha/lib_protocol/delegate_slashed_deposits_storage.ml index f14b5231cbba..13980d132ea7 100644 --- a/src/proto_alpha/lib_protocol/delegate_slashed_deposits_storage.ml +++ b/src/proto_alpha/lib_protocol/delegate_slashed_deposits_storage.ml @@ -114,21 +114,12 @@ let punish_double_signing ctxt ~operation_hash if Percentage.(Compare.(previously_slashed_this_cycle >= p100)) then (* Do not store denunciations that have no effects .*) return ctxt else - let* denunciations_opt = - Storage.Pending_denunciations.find ctxt delegate - in - let denunciations = Option.value denunciations_opt ~default:[] in - let denunciations = - Denunciations_repr.add - operation_hash - rewarded - misbehaviour - denunciations - in - let*! ctxt = - Storage.Pending_denunciations.add ctxt delegate denunciations - in - return ctxt + Pending_denunciations_storage.add_denunciation + ctxt + ~misbehaving_delegate:delegate + operation_hash + ~rewarded_delegate:rewarded + misbehaviour in return ctxt diff --git a/src/proto_alpha/lib_protocol/pending_denunciations_storage.ml b/src/proto_alpha/lib_protocol/pending_denunciations_storage.ml index d7282bbd3558..40ef4114d3aa 100644 --- a/src/proto_alpha/lib_protocol/pending_denunciations_storage.ml +++ b/src/proto_alpha/lib_protocol/pending_denunciations_storage.ml @@ -9,3 +9,19 @@ let find ctxt delegate = let open Lwt_result_syntax in let* denunciations_opt = Storage.Pending_denunciations.find ctxt delegate in return @@ Option.value denunciations_opt ~default:[] + +let add_denunciation ctxt ~misbehaving_delegate operation_hash + ~rewarded_delegate misbehaviour = + let open Lwt_result_syntax in + let* denunciations = find ctxt misbehaving_delegate in + let denunciations = + Denunciations_repr.add + operation_hash + rewarded_delegate + misbehaviour + denunciations + in + let*! ctxt = + Storage.Pending_denunciations.add ctxt misbehaving_delegate denunciations + in + return ctxt diff --git a/src/proto_alpha/lib_protocol/pending_denunciations_storage.mli b/src/proto_alpha/lib_protocol/pending_denunciations_storage.mli index e74be7ed5167..a8a6fc391968 100644 --- a/src/proto_alpha/lib_protocol/pending_denunciations_storage.mli +++ b/src/proto_alpha/lib_protocol/pending_denunciations_storage.mli @@ -12,3 +12,12 @@ val find : Raw_context.t -> Signature.public_key_hash -> Denunciations_repr.item list tzresult Lwt.t + +(** Add a denunciation in the list of the given delegate *) +val add_denunciation : + Raw_context.t -> + misbehaving_delegate:Signature.public_key_hash -> + Operation_hash.t -> + rewarded_delegate:Signature.public_key_hash -> + Misbehaviour_repr.t -> + Raw_context.t tzresult Lwt.t -- GitLab From 36f7b8cbed0053c97a6e91ee5010dcbbde613bf4 Mon Sep 17 00:00:00 2001 From: Julien Tesson Date: Thu, 8 Feb 2024 13:38:36 +0100 Subject: [PATCH 04/11] proto/pending_denunciations_storage: set_denunciations --- .../lib_protocol/delegate_slashed_deposits_storage.ml | 2 +- .../lib_protocol/pending_denunciations_storage.ml | 5 +++++ .../lib_protocol/pending_denunciations_storage.mli | 9 +++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/proto_alpha/lib_protocol/delegate_slashed_deposits_storage.ml b/src/proto_alpha/lib_protocol/delegate_slashed_deposits_storage.ml index 13980d132ea7..429f6a5936aa 100644 --- a/src/proto_alpha/lib_protocol/delegate_slashed_deposits_storage.ml +++ b/src/proto_alpha/lib_protocol/delegate_slashed_deposits_storage.ml @@ -334,7 +334,7 @@ let apply_and_clear_denunciations ctxt = match current_cycle_denunciations with | [] -> Lwt.return ctxt | _ -> - Storage.Pending_denunciations.add + Pending_denunciations_storage.set_denunciations ctxt delegate current_cycle_denunciations) diff --git a/src/proto_alpha/lib_protocol/pending_denunciations_storage.ml b/src/proto_alpha/lib_protocol/pending_denunciations_storage.ml index 40ef4114d3aa..984e01c453f3 100644 --- a/src/proto_alpha/lib_protocol/pending_denunciations_storage.ml +++ b/src/proto_alpha/lib_protocol/pending_denunciations_storage.ml @@ -25,3 +25,8 @@ let add_denunciation ctxt ~misbehaving_delegate operation_hash Storage.Pending_denunciations.add ctxt misbehaving_delegate denunciations in return ctxt + +let set_denunciations ctxt delegate denunciations = + match denunciations with + | [] -> Storage.Pending_denunciations.remove ctxt delegate + | _ -> Storage.Pending_denunciations.add ctxt delegate denunciations diff --git a/src/proto_alpha/lib_protocol/pending_denunciations_storage.mli b/src/proto_alpha/lib_protocol/pending_denunciations_storage.mli index a8a6fc391968..c31b42a2b011 100644 --- a/src/proto_alpha/lib_protocol/pending_denunciations_storage.mli +++ b/src/proto_alpha/lib_protocol/pending_denunciations_storage.mli @@ -21,3 +21,12 @@ val add_denunciation : rewarded_delegate:Signature.public_key_hash -> Misbehaviour_repr.t -> Raw_context.t tzresult Lwt.t + +(** Set the denunciation list of the given delegate. + Previously set denunciations would be erased. +*) +val set_denunciations : + Raw_context.t -> + Signature.public_key_hash -> + Denunciations_repr.t -> + Raw_context.t Lwt.t -- GitLab From 9609e80f7a9e11342338c0fecd2887e0228c046e Mon Sep 17 00:00:00 2001 From: Julien Tesson Date: Thu, 8 Feb 2024 13:41:18 +0100 Subject: [PATCH 05/11] proto/pending_denunciations_storage: has_pending_denunciations --- .../lib_protocol/forbidden_delegates_storage.ml | 13 +++---------- .../lib_protocol/pending_denunciations_storage.ml | 4 ++++ .../lib_protocol/pending_denunciations_storage.mli | 4 ++++ 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/proto_alpha/lib_protocol/forbidden_delegates_storage.ml b/src/proto_alpha/lib_protocol/forbidden_delegates_storage.ml index 4dca1b42b820..b45185f28291 100644 --- a/src/proto_alpha/lib_protocol/forbidden_delegates_storage.ml +++ b/src/proto_alpha/lib_protocol/forbidden_delegates_storage.ml @@ -42,21 +42,14 @@ let set_forbidden_delegates ctxt forbidden_delegates = in return ctxt -let has_pending_denunciations ctxt delegate = - let open Lwt_result_syntax in - let* pending_denunciations = - Storage.Pending_denunciations.find ctxt delegate - in - match pending_denunciations with - | None | Some [] -> return_false - | Some (_ :: _) -> return_true - let should_unforbid ctxt delegate ~selection_for_new_cycle = let open Lwt_result_syntax in (* A delegate who has pending denunciations for which slashing has not been applied yet should stay forbidden, because their frozen deposits are going to decrease by a yet unknown amount. *) - let* has_pending_denunciations = has_pending_denunciations ctxt delegate in + let*! has_pending_denunciations = + Pending_denunciations_storage.has_pending_denunciations ctxt delegate + in if has_pending_denunciations then return_false else (* To get unforbidden, a delegate's current frozen deposits must diff --git a/src/proto_alpha/lib_protocol/pending_denunciations_storage.ml b/src/proto_alpha/lib_protocol/pending_denunciations_storage.ml index 984e01c453f3..50a42f729cec 100644 --- a/src/proto_alpha/lib_protocol/pending_denunciations_storage.ml +++ b/src/proto_alpha/lib_protocol/pending_denunciations_storage.ml @@ -30,3 +30,7 @@ let set_denunciations ctxt delegate denunciations = match denunciations with | [] -> Storage.Pending_denunciations.remove ctxt delegate | _ -> Storage.Pending_denunciations.add ctxt delegate denunciations + +let has_pending_denunciations ctxt delegate = + (* we rely here on the fact that we never insert an empty list in the table *) + Storage.Pending_denunciations.mem ctxt delegate diff --git a/src/proto_alpha/lib_protocol/pending_denunciations_storage.mli b/src/proto_alpha/lib_protocol/pending_denunciations_storage.mli index c31b42a2b011..f01a315d79be 100644 --- a/src/proto_alpha/lib_protocol/pending_denunciations_storage.mli +++ b/src/proto_alpha/lib_protocol/pending_denunciations_storage.mli @@ -30,3 +30,7 @@ val set_denunciations : Signature.public_key_hash -> Denunciations_repr.t -> Raw_context.t Lwt.t + +(** Tells if the given delegate has some pending denunciations *) +val has_pending_denunciations : + Raw_context.t -> Signature.public_key_hash -> bool Lwt.t -- GitLab From fd65d6f1028c9319efb013d9f8fd61980f09798d Mon Sep 17 00:00:00 2001 From: Julien Tesson Date: Thu, 8 Feb 2024 13:44:44 +0100 Subject: [PATCH 06/11] proto/pending_denunciations_storage: encapsulate fold --- .../delegate_slashed_deposits_storage.ml | 2 +- .../lib_protocol/pending_denunciations_storage.ml | 2 ++ .../lib_protocol/pending_denunciations_storage.mli | 12 ++++++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/proto_alpha/lib_protocol/delegate_slashed_deposits_storage.ml b/src/proto_alpha/lib_protocol/delegate_slashed_deposits_storage.ml index 429f6a5936aa..ffb476dce958 100644 --- a/src/proto_alpha/lib_protocol/delegate_slashed_deposits_storage.ml +++ b/src/proto_alpha/lib_protocol/delegate_slashed_deposits_storage.ml @@ -167,7 +167,7 @@ let apply_and_clear_denunciations ctxt = {reward; amount_to_burn} in let* ctxt, balance_updates, remaining_denunciations = - Storage.Pending_denunciations.fold + Pending_denunciations_storage.fold ctxt ~order:`Undefined ~init:(Ok (ctxt, [], [])) diff --git a/src/proto_alpha/lib_protocol/pending_denunciations_storage.ml b/src/proto_alpha/lib_protocol/pending_denunciations_storage.ml index 50a42f729cec..59f9d63c429f 100644 --- a/src/proto_alpha/lib_protocol/pending_denunciations_storage.ml +++ b/src/proto_alpha/lib_protocol/pending_denunciations_storage.ml @@ -34,3 +34,5 @@ let set_denunciations ctxt delegate denunciations = let has_pending_denunciations ctxt delegate = (* we rely here on the fact that we never insert an empty list in the table *) Storage.Pending_denunciations.mem ctxt delegate + +let fold = Storage.Pending_denunciations.fold diff --git a/src/proto_alpha/lib_protocol/pending_denunciations_storage.mli b/src/proto_alpha/lib_protocol/pending_denunciations_storage.mli index f01a315d79be..b21ddd14e3db 100644 --- a/src/proto_alpha/lib_protocol/pending_denunciations_storage.mli +++ b/src/proto_alpha/lib_protocol/pending_denunciations_storage.mli @@ -34,3 +34,15 @@ val set_denunciations : (** Tells if the given delegate has some pending denunciations *) val has_pending_denunciations : Raw_context.t -> Signature.public_key_hash -> bool Lwt.t + +(** See {!Storage.Pending_denunciations.fold} *) +val fold : + Raw_context.t -> + order:[`Sorted | `Undefined] -> + init:'a -> + f: + (Signature.public_key_hash -> + Denunciations_repr.item list -> + 'a -> + 'a Lwt.t) -> + 'a Lwt.t -- GitLab From 17b000d0913f75aebb681da1bd9d6fd804791b7b Mon Sep 17 00:00:00 2001 From: Julien Tesson Date: Thu, 8 Feb 2024 14:08:06 +0100 Subject: [PATCH 07/11] proto/pending_denunciations_storage: encapsulate clear --- .../lib_protocol/delegate_slashed_deposits_storage.ml | 2 +- src/proto_alpha/lib_protocol/init_storage.ml | 2 +- src/proto_alpha/lib_protocol/pending_denunciations_storage.ml | 2 ++ src/proto_alpha/lib_protocol/pending_denunciations_storage.mli | 3 +++ 4 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/proto_alpha/lib_protocol/delegate_slashed_deposits_storage.ml b/src/proto_alpha/lib_protocol/delegate_slashed_deposits_storage.ml index ffb476dce958..bdf27ae56059 100644 --- a/src/proto_alpha/lib_protocol/delegate_slashed_deposits_storage.ml +++ b/src/proto_alpha/lib_protocol/delegate_slashed_deposits_storage.ml @@ -327,7 +327,7 @@ let apply_and_clear_denunciations ctxt = balance_updates, (delegate, denunciations_to_delay) :: remaining_denunciations )) in - let*! ctxt = Storage.Pending_denunciations.clear ctxt in + let*! ctxt = Pending_denunciations_storage.clear ctxt in let*! ctxt = List.fold_left_s (fun ctxt (delegate, current_cycle_denunciations) -> diff --git a/src/proto_alpha/lib_protocol/init_storage.ml b/src/proto_alpha/lib_protocol/init_storage.ml index a45af41e6d0d..1418a8e75fa2 100644 --- a/src/proto_alpha/lib_protocol/init_storage.ml +++ b/src/proto_alpha/lib_protocol/init_storage.ml @@ -313,7 +313,7 @@ let prepare_first_block chain_id ctxt ~typecheck_smart_contract Possible consequence: the slashing history could be inconsistent with the pending denunciations, i.e., there could be unstaked_frozen_deposits that are not slashed whereas unstake_requests are slashed. *) - let*! ctxt = Storage.Pending_denunciations.clear ctxt in + let*! ctxt = Pending_denunciations_storage.clear ctxt in let*! ctxt = migrate_already_denounced_from_Oxford ctxt in let* ctxt = migrate_staking_balance_and_active_delegates_for_p ctxt in let* ctxt = clean_frozen_deposits_for_p ctxt in diff --git a/src/proto_alpha/lib_protocol/pending_denunciations_storage.ml b/src/proto_alpha/lib_protocol/pending_denunciations_storage.ml index 59f9d63c429f..a13a0f82ca9d 100644 --- a/src/proto_alpha/lib_protocol/pending_denunciations_storage.ml +++ b/src/proto_alpha/lib_protocol/pending_denunciations_storage.ml @@ -36,3 +36,5 @@ let has_pending_denunciations ctxt delegate = Storage.Pending_denunciations.mem ctxt delegate let fold = Storage.Pending_denunciations.fold + +let clear ctxt = Storage.Pending_denunciations.clear ctxt diff --git a/src/proto_alpha/lib_protocol/pending_denunciations_storage.mli b/src/proto_alpha/lib_protocol/pending_denunciations_storage.mli index b21ddd14e3db..383a8ebd7b8f 100644 --- a/src/proto_alpha/lib_protocol/pending_denunciations_storage.mli +++ b/src/proto_alpha/lib_protocol/pending_denunciations_storage.mli @@ -46,3 +46,6 @@ val fold : 'a -> 'a Lwt.t) -> 'a Lwt.t + +(** See {!Storage.Pending_denunciations.clear} *) +val clear : Raw_context.t -> Raw_context.t Lwt.t -- GitLab From 8be198026d7f2554abfbf3481add4e969a4dba45 Mon Sep 17 00:00:00 2001 From: Julien Tesson Date: Thu, 8 Feb 2024 17:18:30 +0100 Subject: [PATCH 08/11] proto/pending_denunciations_storage: mv RPC related functions --- src/proto_alpha/lib_protocol/alpha_context.ml | 4 +++- .../delegate_slashed_deposits_storage.ml | 15 --------------- .../delegate_slashed_deposits_storage.mli | 13 ------------- .../lib_protocol/pending_denunciations_storage.ml | 7 +++++++ .../pending_denunciations_storage.mli | 7 +++++++ 5 files changed, 17 insertions(+), 29 deletions(-) diff --git a/src/proto_alpha/lib_protocol/alpha_context.ml b/src/proto_alpha/lib_protocol/alpha_context.ml index 5d1a46e276f4..ef3b7f310635 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.ml +++ b/src/proto_alpha/lib_protocol/alpha_context.ml @@ -560,7 +560,9 @@ module Delegate = struct include For_RPC include Delegate_storage.For_RPC include Delegate_missed_attestations_storage.For_RPC - include Delegate_slashed_deposits_storage.For_RPC + include Pending_denunciations_storage.For_RPC + + let pending_denunciations = Pending_denunciations_storage.find end end diff --git a/src/proto_alpha/lib_protocol/delegate_slashed_deposits_storage.ml b/src/proto_alpha/lib_protocol/delegate_slashed_deposits_storage.ml index bdf27ae56059..fe14425c5c08 100644 --- a/src/proto_alpha/lib_protocol/delegate_slashed_deposits_storage.ml +++ b/src/proto_alpha/lib_protocol/delegate_slashed_deposits_storage.ml @@ -342,18 +342,3 @@ let apply_and_clear_denunciations ctxt = remaining_denunciations in return (ctxt, balance_updates) - -module For_RPC = struct - let pending_denunciations ctxt delegate = - let open Lwt_result_syntax in - let+ denunciations = Storage.Pending_denunciations.find ctxt delegate in - Option.value denunciations ~default:[] - - let pending_denunciations_list ctxt = - let open Lwt_syntax in - let* r = Storage.Pending_denunciations.bindings ctxt in - let r = - List.map (fun (x, l) -> List.map (fun y -> (x, y)) l) r |> List.flatten - in - return r -end diff --git a/src/proto_alpha/lib_protocol/delegate_slashed_deposits_storage.mli b/src/proto_alpha/lib_protocol/delegate_slashed_deposits_storage.mli index 528d3f624aa5..9d45a438aaa7 100644 --- a/src/proto_alpha/lib_protocol/delegate_slashed_deposits_storage.mli +++ b/src/proto_alpha/lib_protocol/delegate_slashed_deposits_storage.mli @@ -90,16 +90,3 @@ val clear_outdated_already_denounced : val apply_and_clear_denunciations : Raw_context.t -> (Raw_context.t * Receipt_repr.balance_updates) tzresult Lwt.t - -module For_RPC : sig - (** Returns the pending denunciations for the given delegate. *) - val pending_denunciations : - Raw_context.t -> - Signature.Public_key_hash.t -> - Denunciations_repr.t tzresult Lwt.t - - (** Returns all the pending denunciations. *) - val pending_denunciations_list : - Raw_context.t -> - (Signature.Public_key_hash.t * Denunciations_repr.item) list Lwt.t -end diff --git a/src/proto_alpha/lib_protocol/pending_denunciations_storage.ml b/src/proto_alpha/lib_protocol/pending_denunciations_storage.ml index a13a0f82ca9d..60bd70d862d9 100644 --- a/src/proto_alpha/lib_protocol/pending_denunciations_storage.ml +++ b/src/proto_alpha/lib_protocol/pending_denunciations_storage.ml @@ -38,3 +38,10 @@ let has_pending_denunciations ctxt delegate = let fold = Storage.Pending_denunciations.fold let clear ctxt = Storage.Pending_denunciations.clear ctxt + +module For_RPC = struct + let pending_denunciations_list ctxt = + let open Lwt_syntax in + let+ r = Storage.Pending_denunciations.bindings ctxt in + List.map (fun (x, l) -> List.map (fun y -> (x, y)) l) r |> List.flatten +end diff --git a/src/proto_alpha/lib_protocol/pending_denunciations_storage.mli b/src/proto_alpha/lib_protocol/pending_denunciations_storage.mli index 383a8ebd7b8f..84987e7c710f 100644 --- a/src/proto_alpha/lib_protocol/pending_denunciations_storage.mli +++ b/src/proto_alpha/lib_protocol/pending_denunciations_storage.mli @@ -49,3 +49,10 @@ val fold : (** See {!Storage.Pending_denunciations.clear} *) val clear : Raw_context.t -> Raw_context.t Lwt.t + +module For_RPC : sig + (** Returns a list of all denunciations paired with the offending delegate pkh. *) + val pending_denunciations_list : + Raw_context.t -> + (Signature.public_key_hash * Denunciations_repr.item) list Lwt.t +end -- GitLab From ca56aef7b5e9e685fbae865689bfffb785ee0a54 Mon Sep 17 00:00:00 2001 From: Julien Tesson Date: Thu, 8 Feb 2024 14:04:02 +0100 Subject: [PATCH 09/11] proto/alpha_context: remove redondant inclusion of For_RPC module the unqualified module For_RPC is actually refering to the module Delegate_missed_attestations_storage.For_RPC included two lines bellow --- src/proto_alpha/lib_protocol/alpha_context.ml | 1 - 1 file changed, 1 deletion(-) diff --git a/src/proto_alpha/lib_protocol/alpha_context.ml b/src/proto_alpha/lib_protocol/alpha_context.ml index ef3b7f310635..0070f7fd9456 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.ml +++ b/src/proto_alpha/lib_protocol/alpha_context.ml @@ -557,7 +557,6 @@ module Delegate = struct module Shared_stake = Shared_stake module For_RPC = struct - include For_RPC include Delegate_storage.For_RPC include Delegate_missed_attestations_storage.For_RPC include Pending_denunciations_storage.For_RPC -- GitLab From 8fac4cd57f581f2478e19a7362204cb32bfa5141 Mon Sep 17 00:00:00 2001 From: Julien Tesson Date: Tue, 6 Feb 2024 09:49:19 +0100 Subject: [PATCH 10/11] proto/delegate_cycle: do not autostake when there are pending slashes --- .../lib_protocol/delegate_cycles.ml | 90 ++++++++++--------- 1 file changed, 49 insertions(+), 41 deletions(-) diff --git a/src/proto_alpha/lib_protocol/delegate_cycles.ml b/src/proto_alpha/lib_protocol/delegate_cycles.ml index 5244a8052c98..62665da782ed 100644 --- a/src/proto_alpha/lib_protocol/delegate_cycles.ml +++ b/src/proto_alpha/lib_protocol/delegate_cycles.ml @@ -128,49 +128,57 @@ let adjust_frozen_stakes ctxt ~deactivated_delegates : ~order:`Undefined ~init:(ctxt, []) ~f:(fun delegate (ctxt, balance_updates) -> - let* full_staking_balance = - Stake_storage.get_full_staking_balance ctxt delegate + let*! has_been_denounced = + Pending_denunciations_storage.has_pending_denunciations ctxt delegate in - let own_frozen = - Full_staking_balance_repr.own_frozen full_staking_balance - in - let*? optimal_frozen = - Stake_context.optimal_frozen_wrt_delegated_without_ai - ctxt - full_staking_balance - in - let* deposit_limit = - Delegate_storage.frozen_deposits_limit ctxt delegate - in - let optimal_frozen = - match deposit_limit with - | None -> optimal_frozen - | Some deposit_limit -> Tez_repr.min optimal_frozen deposit_limit - in - let* ctxt, new_balance_updates = - if Tez_repr.(optimal_frozen > own_frozen) then - let*? optimal_to_stake = Tez_repr.(optimal_frozen -? own_frozen) in - Staking.stake - ctxt - ~for_next_cycle_use_only_after_slashing:true - ~amount:(`At_most optimal_to_stake) - ~sender:delegate - ~delegate - else if Tez_repr.(optimal_frozen < own_frozen) then - let*? to_unstake = Tez_repr.(own_frozen -? optimal_frozen) in - Staking.request_unstake - ctxt - ~for_next_cycle_use_only_after_slashing:true - ~sender_contract:Contract_repr.(Implicit delegate) - ~delegate - to_unstake - else - Staking.finalize_unstake + if has_been_denounced then return (ctxt, balance_updates) + (* we don't autostake on behalf of delegates who will be slashed *) + else + let* full_staking_balance = + Stake_storage.get_full_staking_balance ctxt delegate + in + let own_frozen = + Full_staking_balance_repr.own_frozen full_staking_balance + in + let*? optimal_frozen = + Stake_context.optimal_frozen_wrt_delegated_without_ai ctxt - ~for_next_cycle_use_only_after_slashing:true - Contract_repr.(Implicit delegate) - in - return (ctxt, new_balance_updates @ balance_updates)) + full_staking_balance + in + let* deposit_limit = + Delegate_storage.frozen_deposits_limit ctxt delegate + in + let optimal_frozen = + match deposit_limit with + | None -> optimal_frozen + | Some deposit_limit -> Tez_repr.min optimal_frozen deposit_limit + in + let* ctxt, new_balance_updates = + if Tez_repr.(optimal_frozen > own_frozen) then + let*? optimal_to_stake = + Tez_repr.(optimal_frozen -? own_frozen) + in + Staking.stake + ctxt + ~for_next_cycle_use_only_after_slashing:true + ~amount:(`At_most optimal_to_stake) + ~sender:delegate + ~delegate + else if Tez_repr.(optimal_frozen < own_frozen) then + let*? to_unstake = Tez_repr.(own_frozen -? optimal_frozen) in + Staking.request_unstake + ctxt + ~for_next_cycle_use_only_after_slashing:true + ~sender_contract:Contract_repr.(Implicit delegate) + ~delegate + to_unstake + else + Staking.finalize_unstake + ctxt + ~for_next_cycle_use_only_after_slashing:true + Contract_repr.(Implicit delegate) + in + return (ctxt, new_balance_updates @ balance_updates)) in List.fold_left_es (fun (ctxt, balance_updates) delegate -> -- GitLab From 74be3bf8a1ca428410f2d51e318405f3fe3e4649 Mon Sep 17 00:00:00 2001 From: Julien Tesson Date: Thu, 8 Feb 2024 17:10:38 +0100 Subject: [PATCH 11/11] proto/pending_denunciations_storage: document the module purpose --- .../lib_protocol/pending_denunciations_storage.mli | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/proto_alpha/lib_protocol/pending_denunciations_storage.mli b/src/proto_alpha/lib_protocol/pending_denunciations_storage.mli index 84987e7c710f..ffb5d4e16306 100644 --- a/src/proto_alpha/lib_protocol/pending_denunciations_storage.mli +++ b/src/proto_alpha/lib_protocol/pending_denunciations_storage.mli @@ -4,6 +4,15 @@ (* Copyright (c) 2024 Nomadic Labs. *) (* *) (*****************************************************************************) +(** This module deals with pending denunciations before they are used to slash + delegates. + + This module is responsible for maintaining the table + {!Storage.Pending_denunciations} + + In particular, it maintains the invariant that no key is pointing to an + empty denunciation list. +*) (** Returns the pending denunciations list of the given delegate. It returns an empty list if none are registered. -- GitLab