From f3bac55f094554b0f2e3d08e9a35e307c635e167 Mon Sep 17 00:00:00 2001 From: Diane Gallois-Wong Date: Thu, 22 Feb 2024 15:58:15 +0100 Subject: [PATCH] Proto/AI: move Slashed_deposits table outside of Contract storage --- .../delegate_slashed_deposits_storage.ml | 7 +-- src/proto_alpha/lib_protocol/staking.ml | 6 +-- src/proto_alpha/lib_protocol/storage.ml | 19 ++++---- src/proto_alpha/lib_protocol/storage.mli | 45 +++++++++---------- .../lib_protocol/unstake_requests_storage.ml | 10 +---- 5 files changed, 37 insertions(+), 50 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 0fa0d65bdc6f..c22b7d0b0474 100644 --- a/src/proto_alpha/lib_protocol/delegate_slashed_deposits_storage.ml +++ b/src/proto_alpha/lib_protocol/delegate_slashed_deposits_storage.ml @@ -240,7 +240,7 @@ let apply_block_denunciations ctxt current_cycle block_denunciations_map = in let* slash_history_opt = - Storage.Contract.Slashed_deposits.find ctxt delegate_contract + Storage.Slashed_deposits.find ctxt delegate in let slash_history = Option.value slash_history_opt ~default:[] in @@ -269,10 +269,7 @@ let apply_block_denunciations ctxt current_cycle block_denunciations_map = slash_history in let*! ctxt = - Storage.Contract.Slashed_deposits.add - ctxt - delegate_contract - slash_history + Storage.Slashed_deposits.add ctxt delegate slash_history in let new_total_slashing_percentage = Storage.Slashed_deposits_history.get level.cycle slash_history diff --git a/src/proto_alpha/lib_protocol/staking.ml b/src/proto_alpha/lib_protocol/staking.ml index 14f2408ac5b2..6b7fce09b3e4 100644 --- a/src/proto_alpha/lib_protocol/staking.ml +++ b/src/proto_alpha/lib_protocol/staking.ml @@ -131,11 +131,7 @@ let finalize_unstake ctxt ~for_next_cycle_use_only_after_slashing contract = let can_stake_from_unstake ctxt ~for_next_cycle_use_only_after_slashing ~delegate = let open Lwt_result_syntax in - let* slashing_history_opt = - Storage.Contract.Slashed_deposits.find - ctxt - (Contract_repr.Implicit delegate) - in + let* slashing_history_opt = Storage.Slashed_deposits.find ctxt delegate in let slashing_history = Option.value slashing_history_opt ~default:[] in let* slashing_history_opt_o = diff --git a/src/proto_alpha/lib_protocol/storage.ml b/src/proto_alpha/lib_protocol/storage.ml index 340498959323..ad2b76ee94cb 100644 --- a/src/proto_alpha/lib_protocol/storage.ml +++ b/src/proto_alpha/lib_protocol/storage.ml @@ -153,6 +153,7 @@ module Missed_attestations_info = struct (obj2 (req "remaining_slots" int31) (req "missed_levels" int31)) end +(* TODO #6918: move closer to its only use left after P *) module Slashed_deposits_history = struct type slashed_percentage = Percentage.t @@ -487,15 +488,6 @@ module Contract = struct end) (Tez_repr) - (* TODO #6918: change name back after P *) - module Slashed_deposits = - Indexed_context.Make_map - (Registered) - (struct - let name = ["slashed_deposits_p"] - end) - (Slashed_deposits_history) - (* TODO #6918: Remove after P *) module Slashed_deposits__Oxford = Indexed_context.Make_map @@ -1110,6 +1102,15 @@ module Pending_denunciations = (Public_key_hash_index) (Denunciations_repr) +module Slashed_deposits = + Make_indexed_data_storage + (Make_subcontext (Registered) (Raw_context) + (struct + let name = ["slashed_deposits"] + end)) + (Public_key_hash_index) + (Slashed_deposits_history) + (** Per cycle storage *) (* TODO #6957: Remove this from protocol Q. *) diff --git a/src/proto_alpha/lib_protocol/storage.mli b/src/proto_alpha/lib_protocol/storage.mli index 6b3767147498..2b905fcce5e4 100644 --- a/src/proto_alpha/lib_protocol/storage.mli +++ b/src/proto_alpha/lib_protocol/storage.mli @@ -264,29 +264,6 @@ module Contract : sig and type value = Z.t and type t := Raw_context.t - (** History of slashed deposits: an associative list of cycles to slashed - percentages. - - This storage is inefficient but is not expected to grow large (as of - 2023-11-28, the last slashing on mainnet dates back to: - - 2021-12-17 for double baking (154 events in total), - - 2019-08-08 for double endorsing (24 events in total). - Since slashings are here grouped by baker and cycle, there would only be - a few elements in each list. - - The slashing percentages are used to compute the real value of stake - withdrawals. - Currently there is no limit to the age of the events we need to store - because there is no such limit for stake withdrawals. - At worst we can revisit this decision in a later protocol amendment (in - 25 cycles) or clean up this storage manually or automatically. - *) - module Slashed_deposits : - Indexed_data_storage - with type key = Contract_repr.t - and type value = Slashed_deposits_history.t - and type t := Raw_context.t - (* TODO #6918: Remove after P *) module Slashed_deposits__Oxford : Indexed_data_storage @@ -485,6 +462,28 @@ module Pending_denunciations : and type key = Signature.public_key_hash and type value = Denunciations_repr.t +(** History of slashed deposits: an associative list of cycles to slashed + percentages. + + This storage is inefficient but is not expected to grow large (as of + 2023-11-28, the last slashing on mainnet dates back to: + - 2021-12-17 for double baking (154 events in total), + - 2019-08-08 for double endorsing (24 events in total). + Since slashings are here grouped by baker and cycle, there would only be + a few elements in each list. + + The slashing percentages are used to compute the real value of stake + withdrawals. + Currently there is no limit to the age of the events we need to store + because there is no such limit for stake withdrawals. + At worst we can revisit this decision in a later protocol amendment (in + 25 cycles) or clean up this storage manually or automatically. *) +module Slashed_deposits : + Indexed_data_storage + with type t := Raw_context.t + and type key = Signature.public_key_hash + and type value = Slashed_deposits_history.t + (** Needed for the stitching from Oxford to P. TODO #6957: Remove this from protocol Q. *) type denounced__Oxford = {for_double_attesting : bool; for_double_baking : bool} diff --git a/src/proto_alpha/lib_protocol/unstake_requests_storage.ml b/src/proto_alpha/lib_protocol/unstake_requests_storage.ml index bcb7e8a7b4f0..d6ca6d6deb5b 100644 --- a/src/proto_alpha/lib_protocol/unstake_requests_storage.ml +++ b/src/proto_alpha/lib_protocol/unstake_requests_storage.ml @@ -115,9 +115,7 @@ let prepare_finalize_unstake ctxt ~for_next_cycle_use_only_after_slashing return_some {finalizable = []; unfinalizable = {delegate; requests}} | Some greatest_finalizable_cycle -> let* slashing_history_opt = - Storage.Contract.Slashed_deposits.find - ctxt - (Contract_repr.Implicit delegate) + Storage.Slashed_deposits.find ctxt delegate in let slashing_history = Option.value slashing_history_opt ~default:[] @@ -187,11 +185,7 @@ module For_RPC = struct let slashable_deposits_period = Constants_storage.slashable_deposits_period ctxt in - let* slashing_history_opt = - Storage.Contract.Slashed_deposits.find - ctxt - (Contract_repr.Implicit delegate) - in + let* slashing_history_opt = Storage.Slashed_deposits.find ctxt delegate in let slashing_history = Option.value slashing_history_opt ~default:[] in (* Oxford values *) -- GitLab