From 2b2d92b49c6c8b51613644ee3dc6ffd284c01301 Mon Sep 17 00:00:00 2001 From: Lucas Randazzo Date: Thu, 22 Feb 2024 15:27:15 +0100 Subject: [PATCH 1/2] Proto: remove slashing storage conversion --- .../delegate_slashed_deposits_storage.ml | 56 +++++++++---------- .../delegate_slashed_deposits_storage.mli | 3 - src/proto_alpha/lib_protocol/init_storage.ml | 4 -- src/proto_alpha/lib_protocol/staking.ml | 13 +++++ .../lib_protocol/unstake_requests_storage.ml | 38 +++++++++++++ 5 files changed, 78 insertions(+), 36 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 b73cdc7a69e9..0fa0d65bdc6f 100644 --- a/src/proto_alpha/lib_protocol/delegate_slashed_deposits_storage.ml +++ b/src/proto_alpha/lib_protocol/delegate_slashed_deposits_storage.ml @@ -25,35 +25,6 @@ (* *) (*****************************************************************************) -(* TODO #6918: Remove after P *) -let update_slashing_storage_for_p ctxt = - let open Lwt_result_syntax in - Storage.Delegates.fold - ctxt - ~init:(Ok ctxt) - ~order:`Undefined - ~f:(fun delegate ctxt -> - let*? ctxt in - let delegate = Contract_repr.Implicit delegate in - let* slashed_history = - Storage.Contract.Slashed_deposits__Oxford.find ctxt delegate - in - match slashed_history with - | None -> return ctxt - | Some slashed_history -> - let slashed_history = - List.map - (fun (cycle, percentage) -> - (cycle, Percentage.convert_from_o_to_p percentage)) - slashed_history - in - let*! ctxt = - Storage.Contract.Slashed_deposits.add ctxt delegate slashed_history - in - Storage.Contract.Slashed_deposits__Oxford.remove_existing - ctxt - delegate) - type reward_and_burn = {reward : Tez_repr.t; amount_to_burn : Tez_repr.t} type punishing_amounts = { @@ -257,10 +228,37 @@ let apply_block_denunciations ctxt current_cycle block_denunciations_map = 0) ; (* Validate ensures that [denunciations] contains [delegate] at most once *) let delegate_contract = Contract_repr.Implicit delegate in + (* Oxford values *) + let* slash_history_opt_o = + Storage.Contract.Slashed_deposits__Oxford.find + ctxt + delegate_contract + in + let slash_history_o = + Option.value slash_history_opt_o ~default:[] + |> List.map (fun (a, b) -> (a, Percentage.convert_from_o_to_p b)) + in + let* slash_history_opt = Storage.Contract.Slashed_deposits.find ctxt delegate_contract in let slash_history = Option.value slash_history_opt ~default:[] in + + (* Concatenate both, Oxford first *) + let slash_history = + List.fold_left + (fun acc (cycle, percentage) -> + Storage.Slashed_deposits_history.add cycle percentage acc) + slash_history_o + slash_history + in + + let*! ctxt = + Storage.Contract.Slashed_deposits__Oxford.remove + ctxt + delegate_contract + in + let previous_total_slashing_percentage = Storage.Slashed_deposits_history.get level.cycle slash_history in 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 7e525f4dd162..83d4d2f09930 100644 --- a/src/proto_alpha/lib_protocol/delegate_slashed_deposits_storage.mli +++ b/src/proto_alpha/lib_protocol/delegate_slashed_deposits_storage.mli @@ -83,9 +83,6 @@ val punish_double_signing : val apply_and_clear_denunciations : Raw_context.t -> (Raw_context.t * Receipt_repr.balance_updates) tzresult Lwt.t -val update_slashing_storage_for_p : - Raw_context.t -> Raw_context.t tzresult Lwt.t - module For_RPC : sig (** [get_estimated_shared_pending_slashed_amount ctxt delegate] returns the estimated shared pending slashed amount of the given [delegate] diff --git a/src/proto_alpha/lib_protocol/init_storage.ml b/src/proto_alpha/lib_protocol/init_storage.ml index 5ee8149b4cd7..3cd96bd70696 100644 --- a/src/proto_alpha/lib_protocol/init_storage.ml +++ b/src/proto_alpha/lib_protocol/init_storage.ml @@ -343,10 +343,6 @@ let prepare_first_block chain_id ctxt ~typecheck_smart_contract let* ctxt = cleanup_values_for_protocol_p ctxt previous_proto_constants level in - (* Update the percentage representation of the stored slashes *) - let* ctxt = - Delegate_slashed_deposits_storage.update_slashing_storage_for_p ctxt - in let* ctxt = update_total_supply_for_p chain_id ctxt in return (ctxt, []) in diff --git a/src/proto_alpha/lib_protocol/staking.ml b/src/proto_alpha/lib_protocol/staking.ml index 7d66398ce24f..14f2408ac5b2 100644 --- a/src/proto_alpha/lib_protocol/staking.ml +++ b/src/proto_alpha/lib_protocol/staking.ml @@ -137,6 +137,19 @@ let can_stake_from_unstake ctxt ~for_next_cycle_use_only_after_slashing (Contract_repr.Implicit delegate) in let slashing_history = Option.value slashing_history_opt ~default:[] in + + let* slashing_history_opt_o = + Storage.Contract.Slashed_deposits__Oxford.find + ctxt + (Contract_repr.Implicit delegate) + in + let slashing_history_o = + Option.value slashing_history_opt_o ~default:[] + |> List.map (fun (a, b) -> (a, Percentage.convert_from_o_to_p b)) + in + + let slashing_history = slashing_history @ slashing_history_o in + let current_cycle = (Raw_context.current_level ctxt).cycle in let current_cycle = if for_next_cycle_use_only_after_slashing then Cycle_repr.succ current_cycle diff --git a/src/proto_alpha/lib_protocol/unstake_requests_storage.ml b/src/proto_alpha/lib_protocol/unstake_requests_storage.ml index 89508fb9e16c..bcb7e8a7b4f0 100644 --- a/src/proto_alpha/lib_protocol/unstake_requests_storage.ml +++ b/src/proto_alpha/lib_protocol/unstake_requests_storage.ml @@ -122,6 +122,24 @@ let prepare_finalize_unstake ctxt ~for_next_cycle_use_only_after_slashing let slashing_history = Option.value slashing_history_opt ~default:[] in + (* Oxford values *) + let* slashing_history_opt_o = + Storage.Contract.Slashed_deposits__Oxford.find + ctxt + (Contract_repr.Implicit delegate) + in + let slashing_history_o = + Option.value slashing_history_opt_o ~default:[] + |> List.map (fun (a, b) -> (a, Percentage.convert_from_o_to_p b)) + in + + let slashing_history = + List.fold_left + (fun acc (cycle, percentage) -> + Storage.Slashed_deposits_history.add cycle percentage acc) + slashing_history_o + slashing_history + in let finalizable, unfinalizable_requests = List.partition_map (fun request -> @@ -175,6 +193,26 @@ module For_RPC = struct (Contract_repr.Implicit delegate) in let slashing_history = Option.value slashing_history_opt ~default:[] in + + (* Oxford values *) + let* slashing_history_opt = + Storage.Contract.Slashed_deposits__Oxford.find + ctxt + (Contract_repr.Implicit delegate) + in + let slashing_history_o = + Option.value slashing_history_opt ~default:[] + |> List.map (fun (a, b) -> (a, Percentage.convert_from_o_to_p b)) + in + + let slashing_history = + List.fold_left + (fun acc (cycle, percentage) -> + Storage.Slashed_deposits_history.add cycle percentage acc) + slashing_history_o + slashing_history + in + List.map_es (fun (request_cycle, request_amount) -> let new_amount = -- GitLab From 922721ca753ba4291e5300e1c097fd28891d30a4 Mon Sep 17 00:00:00 2001 From: Lucas Randazzo Date: Mon, 4 Mar 2024 16:18:54 +0100 Subject: [PATCH 2/2] Docs/proto: update changelog --- docs/protocols/alpha.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/protocols/alpha.rst b/docs/protocols/alpha.rst index 51527678d642..d8b19e59f101 100644 --- a/docs/protocols/alpha.rst +++ b/docs/protocols/alpha.rst @@ -237,3 +237,6 @@ Internal ``adaptive_issuance_activation_delay``, ``tolerated_inactivity_period``, ``consensus_key_activation_delay``, ``slashable_deposits_period`` (MR :gl:`!11188`, :gl:`!11280`, :gl:`!11279`, :gl:`!11629`) + +- Internal representation of percentages are converted on the fly from their + Oxford representation to their new one. (MR :gl:`!12152`) -- GitLab