From 7c09d391c90b50ba73d7e5a261d163c38ac84449 Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Thu, 12 Oct 2023 13:56:56 +0200 Subject: [PATCH 1/2] Proto/Storage: make Staking_balance non-snapshotable --- src/proto_alpha/lib_protocol/init_storage.ml | 13 +++++++++++++ src/proto_alpha/lib_protocol/storage.ml | 5 ++--- src/proto_alpha/lib_protocol/storage.mli | 3 +-- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/proto_alpha/lib_protocol/init_storage.ml b/src/proto_alpha/lib_protocol/init_storage.ml index a63583e0c230..9cd20352dd0a 100644 --- a/src/proto_alpha/lib_protocol/init_storage.ml +++ b/src/proto_alpha/lib_protocol/init_storage.ml @@ -134,6 +134,18 @@ let migrate_already_denounced_from_Oxford ctxt = | None -> return ctxt | Some previous_cycle -> migrate_cycle ctxt previous_cycle +(* This removes snapshots and moves the current [staking_balance] one level + up. *) +let migrate_staking_balance_for_p ctxt = + let open Lwt_result_syntax in + let* staking_balance_tree = + Raw_context.get_tree ctxt ["staking_balance"; "current"] + in + let*! ctxt = + Raw_context.add_tree ctxt ["staking_balance"] staking_balance_tree + in + return ctxt + let prepare_first_block chain_id ctxt ~typecheck_smart_contract ~typecheck_smart_rollup ~level ~timestamp ~predecessor = let open Lwt_result_syntax in @@ -233,6 +245,7 @@ let prepare_first_block chain_id ctxt ~typecheck_smart_contract that are not slashed whereas unstake_requests are slashed. *) let*! ctxt = Storage.Pending_denunciations.clear ctxt in let*! ctxt = migrate_already_denounced_from_Oxford ctxt in + let* ctxt = migrate_staking_balance_for_p ctxt in return (ctxt, []) in let* ctxt = diff --git a/src/proto_alpha/lib_protocol/storage.ml b/src/proto_alpha/lib_protocol/storage.ml index 301ea5b28444..f62ae1ce49bd 100644 --- a/src/proto_alpha/lib_protocol/storage.ml +++ b/src/proto_alpha/lib_protocol/storage.ml @@ -1287,13 +1287,12 @@ module Pending_staking_parameters = Cycle.Pending_staking_parameters module Stake = struct module Staking_balance = - Make_indexed_data_snapshotable_storage + Make_indexed_data_storage (Make_subcontext (Registered) (Raw_context) (struct let name = ["staking_balance"] end)) - (Int31_index) - (Public_key_hash_index) + (Public_key_hash_index) (Full_staking_balance_repr) module Active_delegates_with_minimal_stake = diff --git a/src/proto_alpha/lib_protocol/storage.mli b/src/proto_alpha/lib_protocol/storage.mli index 624308c5e63b..3f3d8031cfc6 100644 --- a/src/proto_alpha/lib_protocol/storage.mli +++ b/src/proto_alpha/lib_protocol/storage.mli @@ -507,10 +507,9 @@ module Stake : sig less than {!Constants_parametric_repr.minimal_stake}. It might be large. *) module Staking_balance : - Indexed_data_snapshotable_storage + Indexed_data_storage with type key = Signature.Public_key_hash.t and type value = Full_staking_balance_repr.t - and type snapshot = int and type t := Raw_context.t (** This is a set, encoded in a map with value unit. This should be -- GitLab From 92a9952bf3732508a906a72d30af61112de4f7f7 Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Thu, 12 Oct 2023 16:47:55 +0200 Subject: [PATCH 2/2] Proto/Storage: make Activate_delegates_with_minimal_stake non-snapshotable --- src/proto_alpha/lib_protocol/delegate_cycles.ml | 2 +- src/proto_alpha/lib_protocol/init_storage.ml | 17 ++++++++++++++--- src/proto_alpha/lib_protocol/stake_storage.ml | 8 ++++---- src/proto_alpha/lib_protocol/stake_storage.mli | 2 +- src/proto_alpha/lib_protocol/storage.ml | 14 +++----------- src/proto_alpha/lib_protocol/storage.mli | 9 +++------ 6 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/proto_alpha/lib_protocol/delegate_cycles.ml b/src/proto_alpha/lib_protocol/delegate_cycles.ml index 9547c9fe549e..bc72d26f6ec3 100644 --- a/src/proto_alpha/lib_protocol/delegate_cycles.ml +++ b/src/proto_alpha/lib_protocol/delegate_cycles.ml @@ -35,7 +35,7 @@ let update_activity ctxt last_cycle = ctxt ~order:`Sorted ~init:(Ok (ctxt, [])) - ~f:(fun delegate () acc -> + ~f:(fun delegate acc -> let*? ctxt, deactivated = acc in let* cycle = Delegate_activation_storage.last_cycle_before_deactivation diff --git a/src/proto_alpha/lib_protocol/init_storage.ml b/src/proto_alpha/lib_protocol/init_storage.ml index 9cd20352dd0a..ab5cec87df84 100644 --- a/src/proto_alpha/lib_protocol/init_storage.ml +++ b/src/proto_alpha/lib_protocol/init_storage.ml @@ -135,8 +135,9 @@ let migrate_already_denounced_from_Oxford ctxt = | Some previous_cycle -> migrate_cycle ctxt previous_cycle (* This removes snapshots and moves the current [staking_balance] one level - up. *) -let migrate_staking_balance_for_p ctxt = + up. Same thing for active delegates with minimal stake but renames the key + at the same time. *) +let migrate_staking_balance_and_active_delegates_for_p ctxt = let open Lwt_result_syntax in let* staking_balance_tree = Raw_context.get_tree ctxt ["staking_balance"; "current"] @@ -144,6 +145,16 @@ let migrate_staking_balance_for_p ctxt = let*! ctxt = Raw_context.add_tree ctxt ["staking_balance"] staking_balance_tree in + let* active_delegates_tree = + Raw_context.get_tree ctxt ["active_delegate_with_one_roll"; "current"] + in + let*! ctxt = Raw_context.remove ctxt ["active_delegate_with_one_roll"] in + let*! ctxt = + Raw_context.add_tree + ctxt + ["active_delegates_with_minimal_stake"] + active_delegates_tree + in return ctxt let prepare_first_block chain_id ctxt ~typecheck_smart_contract @@ -245,7 +256,7 @@ let prepare_first_block chain_id ctxt ~typecheck_smart_contract that are not slashed whereas unstake_requests are slashed. *) let*! ctxt = Storage.Pending_denunciations.clear ctxt in let*! ctxt = migrate_already_denounced_from_Oxford ctxt in - let* ctxt = migrate_staking_balance_for_p ctxt in + let* ctxt = migrate_staking_balance_and_active_delegates_for_p ctxt in return (ctxt, []) in let* ctxt = diff --git a/src/proto_alpha/lib_protocol/stake_storage.ml b/src/proto_alpha/lib_protocol/stake_storage.ml index 5475a531010a..0d1e84ab716b 100644 --- a/src/proto_alpha/lib_protocol/stake_storage.ml +++ b/src/proto_alpha/lib_protocol/stake_storage.ml @@ -94,7 +94,7 @@ let initialize_delegate ctxt delegate ~delegated = let* ctxt = Storage.Stake.Staking_balance.init ctxt delegate balance in if has_minimal_stake ctxt balance then let*! ctxt = - Storage.Stake.Active_delegates_with_minimal_stake.add ctxt delegate () + Storage.Stake.Active_delegates_with_minimal_stake.add ctxt delegate in return ctxt else return ctxt @@ -135,7 +135,7 @@ let update_stake ~f ctxt delegate = return ctxt else let*! ctxt = - Storage.Stake.Active_delegates_with_minimal_stake.add ctxt delegate () + Storage.Stake.Active_delegates_with_minimal_stake.add ctxt delegate in return ctxt | false, false | true, true -> return ctxt @@ -193,7 +193,7 @@ let set_active ctxt delegate = let* staking_balance = get_full_staking_balance ctxt delegate in if has_minimal_stake ctxt staking_balance then let*! ctxt = - Storage.Stake.Active_delegates_with_minimal_stake.add ctxt delegate () + Storage.Stake.Active_delegates_with_minimal_stake.add ctxt delegate in return ctxt else return ctxt @@ -213,7 +213,7 @@ let fold_on_active_delegates_with_minimal_stake_es ctxt ~f ~order ~init = ctxt ~order ~init:(Ok init) - ~f:(fun delegate () acc -> + ~f:(fun delegate acc -> let*? acc in f delegate acc) diff --git a/src/proto_alpha/lib_protocol/stake_storage.mli b/src/proto_alpha/lib_protocol/stake_storage.mli index 2c959aed04ac..3b51335efb73 100644 --- a/src/proto_alpha/lib_protocol/stake_storage.mli +++ b/src/proto_alpha/lib_protocol/stake_storage.mli @@ -105,7 +105,7 @@ val fold_on_active_delegates_with_minimal_stake_s : Raw_context.t -> order:[`Sorted | `Undefined] -> init:'a -> - f:(Signature.Public_key_hash.t -> unit -> 'a -> 'a Lwt.t) -> + f:(Signature.Public_key_hash.t -> 'a -> 'a Lwt.t) -> 'a Lwt.t val get_selected_distribution : diff --git a/src/proto_alpha/lib_protocol/storage.ml b/src/proto_alpha/lib_protocol/storage.ml index f62ae1ce49bd..c57acf3949e6 100644 --- a/src/proto_alpha/lib_protocol/storage.ml +++ b/src/proto_alpha/lib_protocol/storage.ml @@ -1296,20 +1296,12 @@ module Stake = struct (Full_staking_balance_repr) module Active_delegates_with_minimal_stake = - Make_indexed_data_snapshotable_storage + Make_data_set_storage (Make_subcontext (Registered) (Raw_context) (struct - (* This name is for historical reasons, when the stake was - expressed in rolls (that is, pre-Ithaca). *) - let name = ["active_delegate_with_one_roll"] + let name = ["active_delegates_with_minimal_stake"] end)) - (Int31_index) - (Public_key_hash_index) - (struct - type t = unit - - let encoding = Data_encoding.unit - end) + (Public_key_hash_index) module Selected_distribution_for_cycle = Cycle.Selected_stake_distribution module Total_active_stake = Cycle.Total_active_stake diff --git a/src/proto_alpha/lib_protocol/storage.mli b/src/proto_alpha/lib_protocol/storage.mli index 3f3d8031cfc6..d75ce4ffaae1 100644 --- a/src/proto_alpha/lib_protocol/storage.mli +++ b/src/proto_alpha/lib_protocol/storage.mli @@ -512,13 +512,10 @@ module Stake : sig and type value = Full_staking_balance_repr.t and type t := Raw_context.t - (** This is a set, encoded in a map with value unit. This should be - fairly small compared to staking balance *) + (** This should be fairly small compared to staking balance *) module Active_delegates_with_minimal_stake : - Indexed_data_snapshotable_storage - with type key = Signature.Public_key_hash.t - and type value = unit - and type snapshot = int + Data_set_storage + with type elt = Signature.Public_key_hash.t and type t := Raw_context.t (** Counter of stake storage snapshots taken since last cycle *) -- GitLab