From 487d22ae103cdd9e617271d6a2940548c48485b8 Mon Sep 17 00:00:00 2001 From: Julien Tesson Date: Tue, 19 Sep 2023 18:59:07 +0200 Subject: [PATCH 1/4] Proto/stake_repr: compute new AI stake from an old one --- src/proto_alpha/lib_protocol/stake_repr.ml | 11 +++++++++-- src/proto_alpha/lib_protocol/stake_repr.mli | 3 +++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/proto_alpha/lib_protocol/stake_repr.ml b/src/proto_alpha/lib_protocol/stake_repr.ml index a2890c52e7c0..42f2a95d3967 100644 --- a/src/proto_alpha/lib_protocol/stake_repr.ml +++ b/src/proto_alpha/lib_protocol/stake_repr.ml @@ -22,8 +22,6 @@ (* DEALINGS IN THE SOFTWARE. *) (* *) (*****************************************************************************) -(* TODO: https://gitlab.com/tezos/tezos/-/issues/6380 - Stitching is needed for current Stake_repr *) type t = {frozen : Tez_repr.t; weighted_delegated : Tez_repr.t} let make ~frozen ~weighted_delegated = {frozen; weighted_delegated} @@ -52,3 +50,12 @@ let staking_weight {frozen; weighted_delegated} = Int64.add frozen weighted_delegated let compare s1 s2 = Int64.compare (staking_weight s1) (staking_weight s2) + +let migrate_stake_from_O_to_P ~edge_of_staking_over_delegation + ({frozen; weighted_delegated = actually_unweighted_delegated} : t) : + t tzresult = + let open Result_syntax in + let* weighted_delegated = + Tez_repr.(actually_unweighted_delegated /? edge_of_staking_over_delegation) + in + return @@ {frozen; weighted_delegated} diff --git a/src/proto_alpha/lib_protocol/stake_repr.mli b/src/proto_alpha/lib_protocol/stake_repr.mli index e7f58ea9c875..f949f56f1a3d 100644 --- a/src/proto_alpha/lib_protocol/stake_repr.mli +++ b/src/proto_alpha/lib_protocol/stake_repr.mli @@ -43,3 +43,6 @@ val ( +? ) : t -> t -> t tzresult val staking_weight : t -> int64 val compare : t -> t -> int + +val migrate_stake_from_O_to_P : + edge_of_staking_over_delegation:int64 -> t -> t tzresult -- GitLab From e55664371422bdba28d37b29844c8cfb6b8adbc7 Mon Sep 17 00:00:00 2001 From: Julien Tesson Date: Tue, 19 Sep 2023 19:00:18 +0200 Subject: [PATCH 2/4] proto/stake_storage: fold over stake in the provided cycles --- src/proto_alpha/lib_protocol/stake_storage.ml | 30 +++++++++++++++++++ .../lib_protocol/stake_storage.mli | 8 +++++ 2 files changed, 38 insertions(+) diff --git a/src/proto_alpha/lib_protocol/stake_storage.ml b/src/proto_alpha/lib_protocol/stake_storage.ml index 76a511869ec7..3ce3c36dbc56 100644 --- a/src/proto_alpha/lib_protocol/stake_storage.ml +++ b/src/proto_alpha/lib_protocol/stake_storage.ml @@ -71,6 +71,33 @@ module Selected_distribution_for_cycle = struct let id = identifier_of_cycle cycle in let*? ctxt = Cache.update ctxt id None in Storage.Stake.Selected_distribution_for_cycle.remove_existing ctxt cycle + + (* updating [compute_new_value] to all stored stake in all selected distribution at + cycles greater or equal to [from_cycle] *) + let migrate_storage_to_weighted_value_for_O_to_P ctxt compute_new_value + ~from_cycle = + let open Lwt_result_syntax in + let*! cycles = Storage.Stake.Selected_distribution_for_cycle.keys ctxt in + let cycles = + List.filter (fun cycle -> Cycle_repr.(cycle >= from_cycle)) cycles + in + List.fold_left_es + (fun ctxt cycle -> + let* old_value = get ctxt cycle in + let*? new_value = + let open Result_syntax in + List.map_e + (fun (pkh, stake) -> + let* new_stake = compute_new_value stake in + return (pkh, new_stake)) + old_value + in + Storage.Stake.Selected_distribution_for_cycle.update + ctxt + cycle + new_value) + ctxt + cycles end let get_full_staking_balance ctxt delegate = @@ -332,6 +359,9 @@ let prepare_stake_distribution ctxt = ctxt stake_distribution) +let migrate_storage_to_weighted_value_for_O_to_P = + Selected_distribution_for_cycle.migrate_storage_to_weighted_value_for_O_to_P + let get_total_active_stake = Storage.Stake.Total_active_stake.get let remove_contract_delegated_stake ctxt contract amount = diff --git a/src/proto_alpha/lib_protocol/stake_storage.mli b/src/proto_alpha/lib_protocol/stake_storage.mli index 1069903d7f92..b9287f3f663d 100644 --- a/src/proto_alpha/lib_protocol/stake_storage.mli +++ b/src/proto_alpha/lib_protocol/stake_storage.mli @@ -135,6 +135,14 @@ val find_selected_distribution : context. *) val prepare_stake_distribution : Raw_context.t -> Raw_context.t tzresult Lwt.t +(** Migrating all stored stake using [compute_new_value] in all selected + distribution at cycles greater or equal to [from_cycle] *) +val migrate_storage_to_weighted_value_for_O_to_P : + Raw_context.t -> + (Stake_repr.t -> Stake_repr.t tzresult) -> + from_cycle:Cycle_repr.t -> + Raw_context.t tzresult Lwt.t + (** [get_total_active_stake ctxt cycle] retrieves the amount in Tez of the active stake at [cycle] from [ctxt]. *) val get_total_active_stake : -- GitLab From 4ae19e306cfe4ecedb3e287a06b9a1bcf0a26887 Mon Sep 17 00:00:00 2001 From: Julien Tesson Date: Tue, 19 Sep 2023 19:02:22 +0200 Subject: [PATCH 3/4] proto/adaptive_issuance: add storage(s) migration function --- .../lib_protocol/adaptive_issuance_storage.ml | 22 +++++++++++++++++++ .../adaptive_issuance_storage.mli | 3 +++ 2 files changed, 25 insertions(+) diff --git a/src/proto_alpha/lib_protocol/adaptive_issuance_storage.ml b/src/proto_alpha/lib_protocol/adaptive_issuance_storage.ml index 200d91b9fe32..e7e5ef7bd56f 100644 --- a/src/proto_alpha/lib_protocol/adaptive_issuance_storage.ml +++ b/src/proto_alpha/lib_protocol/adaptive_issuance_storage.ml @@ -324,6 +324,28 @@ let update_ema ctxt ~vote = in (ctxt, launch_cycle, new_ema) +let migrate_adaptive_issuance_storages_from_O_to_P ctxt = + let open Lwt_result_syntax in + let* activation = Storage.Adaptive_issuance.Activation.get ctxt in + let edge_of_staking_over_delegation = + Int64.of_int + (Constants_storage.adaptive_issuance_edge_of_staking_over_delegation ctxt) + in + match activation with + | None -> return ctxt + | Some activation_cycle -> + (* Distributions selected at the end of cycle C will be used for cycle + C+1+Preserved_cycles, so AI weighting only takes effect + preserved_cycles+1 after AI activation cycle. *) + let cycle_of_first_distribution_set_after_activation = + Cycle_repr.( + add (succ activation_cycle) (Constants_storage.preserved_cycles ctxt)) + in + Stake_storage.migrate_storage_to_weighted_value_for_O_to_P + ctxt + (Stake_repr.migrate_stake_from_O_to_P ~edge_of_staking_over_delegation) + ~from_cycle:cycle_of_first_distribution_set_after_activation + module For_RPC = struct let get_reward_coeff = get_reward_coeff end diff --git a/src/proto_alpha/lib_protocol/adaptive_issuance_storage.mli b/src/proto_alpha/lib_protocol/adaptive_issuance_storage.mli index 251634b05de9..92a3ebf65b63 100644 --- a/src/proto_alpha/lib_protocol/adaptive_issuance_storage.mli +++ b/src/proto_alpha/lib_protocol/adaptive_issuance_storage.mli @@ -67,6 +67,9 @@ val update_ema : voted to be activated (yet). *) val launch_cycle : Raw_context.t -> Cycle_repr.t option tzresult Lwt.t +val migrate_adaptive_issuance_storages_from_O_to_P : + Raw_context.t -> Raw_context.t tzresult Lwt.t + module For_RPC : sig (** [get_reward_coeff ctxt cycle] reads the reward coeff for the given cycle from the storage. -- GitLab From f2df42c3cbedbd6354c01588bd15fe1b74c0890d Mon Sep 17 00:00:00 2001 From: Julien Tesson Date: Tue, 19 Sep 2023 19:02:55 +0200 Subject: [PATCH 4/4] proto/init_storage: call adaptive issuance storage migration --- src/proto_alpha/lib_protocol/init_storage.ml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/proto_alpha/lib_protocol/init_storage.ml b/src/proto_alpha/lib_protocol/init_storage.ml index 27d7e71e2432..b8632ba21f4f 100644 --- a/src/proto_alpha/lib_protocol/init_storage.ml +++ b/src/proto_alpha/lib_protocol/init_storage.ml @@ -204,6 +204,11 @@ let prepare_first_block chain_id ctxt ~typecheck_smart_contract let* ctxt = Sc_rollup_refutation_storage.migrate_clean_refutation_games ctxt in + let* ctxt = + Adaptive_issuance_storage + .migrate_adaptive_issuance_storages_from_O_to_P + ctxt + in return (ctxt, []) in let* ctxt = -- GitLab