From 29a0b5daabfd45a8a6af40dbccd70162f41228d9 Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Fri, 15 Sep 2023 15:09:36 +0200 Subject: [PATCH 01/12] Proto/Stake_repr: move Full to a separate module --- src/proto_alpha/lib_protocol/TEZOS_PROTOCOL | 1 + src/proto_alpha/lib_protocol/dune | 4 +++ .../lib_protocol/full_staking_balance_repr.ml | 33 +++++++++++++++++++ .../full_staking_balance_repr.mli | 19 +++++++++++ src/proto_alpha/lib_protocol/stake_repr.ml | 29 +--------------- src/proto_alpha/lib_protocol/stake_repr.mli | 18 +--------- 6 files changed, 59 insertions(+), 45 deletions(-) create mode 100644 src/proto_alpha/lib_protocol/full_staking_balance_repr.ml create mode 100644 src/proto_alpha/lib_protocol/full_staking_balance_repr.mli diff --git a/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL b/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL index abb910d94484..fe5c1a234d84 100644 --- a/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL +++ b/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL @@ -115,6 +115,7 @@ "Parameters_repr", "Sapling_repr", "Lazy_storage_kind", + "Full_staking_balance_repr", "Stake_repr", "Receipt_repr", "Migration_repr", diff --git a/src/proto_alpha/lib_protocol/dune b/src/proto_alpha/lib_protocol/dune index a48f0ab0c059..d7b4f0ba1165 100644 --- a/src/proto_alpha/lib_protocol/dune +++ b/src/proto_alpha/lib_protocol/dune @@ -137,6 +137,7 @@ Parameters_repr Sapling_repr Lazy_storage_kind + Full_staking_balance_repr Stake_repr Receipt_repr Migration_repr @@ -424,6 +425,7 @@ parameters_repr.ml parameters_repr.mli sapling_repr.ml lazy_storage_kind.ml lazy_storage_kind.mli + full_staking_balance_repr.ml full_staking_balance_repr.mli stake_repr.ml stake_repr.mli receipt_repr.ml receipt_repr.mli migration_repr.ml migration_repr.mli @@ -713,6 +715,7 @@ parameters_repr.ml parameters_repr.mli sapling_repr.ml lazy_storage_kind.ml lazy_storage_kind.mli + full_staking_balance_repr.ml full_staking_balance_repr.mli stake_repr.ml stake_repr.mli receipt_repr.ml receipt_repr.mli migration_repr.ml migration_repr.mli @@ -986,6 +989,7 @@ parameters_repr.ml parameters_repr.mli sapling_repr.ml lazy_storage_kind.ml lazy_storage_kind.mli + full_staking_balance_repr.ml full_staking_balance_repr.mli stake_repr.ml stake_repr.mli receipt_repr.ml receipt_repr.mli migration_repr.ml migration_repr.mli diff --git a/src/proto_alpha/lib_protocol/full_staking_balance_repr.ml b/src/proto_alpha/lib_protocol/full_staking_balance_repr.ml new file mode 100644 index 000000000000..eb55d20d7f55 --- /dev/null +++ b/src/proto_alpha/lib_protocol/full_staking_balance_repr.ml @@ -0,0 +1,33 @@ +(*****************************************************************************) +(* *) +(* SPDX-License-Identifier: MIT *) +(* Copyright (c) 2023 Nomadic Labs, *) +(* *) +(*****************************************************************************) + +type t = { + own_frozen : Tez_repr.t; + staked_frozen : Tez_repr.t; + delegated : Tez_repr.t; +} + +let make ~own_frozen ~staked_frozen ~delegated = + {own_frozen; staked_frozen; delegated} + +let zero = + make + ~own_frozen:Tez_repr.zero + ~staked_frozen:Tez_repr.zero + ~delegated:Tez_repr.zero + +let encoding = + let open Data_encoding in + conv + (fun {own_frozen; staked_frozen; delegated} -> + (own_frozen, staked_frozen, delegated)) + (fun (own_frozen, staked_frozen, delegated) -> + {own_frozen; staked_frozen; delegated}) + (obj3 + (req "own_frozen" Tez_repr.encoding) + (req "staked_frozen" Tez_repr.encoding) + (req "delegated" Tez_repr.encoding)) diff --git a/src/proto_alpha/lib_protocol/full_staking_balance_repr.mli b/src/proto_alpha/lib_protocol/full_staking_balance_repr.mli new file mode 100644 index 000000000000..93a9f04909cd --- /dev/null +++ b/src/proto_alpha/lib_protocol/full_staking_balance_repr.mli @@ -0,0 +1,19 @@ +(*****************************************************************************) +(* *) +(* SPDX-License-Identifier: MIT *) +(* Copyright (c) 2023 Nomadic Labs, *) +(* *) +(*****************************************************************************) + +type t = private { + own_frozen : Tez_repr.t; + staked_frozen : Tez_repr.t; + delegated : Tez_repr.t; +} + +val make : + own_frozen:Tez_repr.t -> staked_frozen:Tez_repr.t -> delegated:Tez_repr.t -> t + +val zero : t + +val encoding : t Data_encoding.t diff --git a/src/proto_alpha/lib_protocol/stake_repr.ml b/src/proto_alpha/lib_protocol/stake_repr.ml index 1da5db39d16b..b6e968d1bb28 100644 --- a/src/proto_alpha/lib_protocol/stake_repr.ml +++ b/src/proto_alpha/lib_protocol/stake_repr.ml @@ -101,31 +101,4 @@ let ( +? ) {frozen = f1; delegated = d1} {frozen = f2; delegated = d2} = let+ delegated = Tez_repr.(d1 +? d2) in {frozen; delegated} -module Full = struct - type t = { - own_frozen : Tez_repr.t; - staked_frozen : Tez_repr.t; - delegated : Tez_repr.t; - } - - let make ~own_frozen ~staked_frozen ~delegated = - {own_frozen; staked_frozen; delegated} - - let zero = - make - ~own_frozen:Tez_repr.zero - ~staked_frozen:Tez_repr.zero - ~delegated:Tez_repr.zero - - let encoding = - let open Data_encoding in - conv - (fun {own_frozen; staked_frozen; delegated} -> - (own_frozen, staked_frozen, delegated)) - (fun (own_frozen, staked_frozen, delegated) -> - {own_frozen; staked_frozen; delegated}) - (obj3 - (req "own_frozen" Tez_repr.encoding) - (req "staked_frozen" Tez_repr.encoding) - (req "delegated" Tez_repr.encoding)) -end +module Full = Full_staking_balance_repr diff --git a/src/proto_alpha/lib_protocol/stake_repr.mli b/src/proto_alpha/lib_protocol/stake_repr.mli index de140565724e..af5e6d2bfbf4 100644 --- a/src/proto_alpha/lib_protocol/stake_repr.mli +++ b/src/proto_alpha/lib_protocol/stake_repr.mli @@ -54,20 +54,4 @@ val get_frozen : t -> Tez_repr.t val ( +? ) : t -> t -> t tzresult -module Full : sig - type t = private { - own_frozen : Tez_repr.t; - staked_frozen : Tez_repr.t; - delegated : Tez_repr.t; - } - - val make : - own_frozen:Tez_repr.t -> - staked_frozen:Tez_repr.t -> - delegated:Tez_repr.t -> - t - - val zero : t - - val encoding : t Data_encoding.t -end +module Full = Full_staking_balance_repr -- GitLab From 154c13209ae59b496e4b791c55dd25ab8ee0f23a Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Fri, 15 Sep 2023 15:11:32 +0200 Subject: [PATCH 02/12] Proto/Stake_repr: remove Full alias --- src/proto_alpha/lib_protocol/stake_context.ml | 5 ++-- .../lib_protocol/stake_context.mli | 7 ++--- src/proto_alpha/lib_protocol/stake_repr.ml | 2 -- src/proto_alpha/lib_protocol/stake_repr.mli | 2 -- src/proto_alpha/lib_protocol/stake_storage.ml | 26 +++++++++---------- .../lib_protocol/stake_storage.mli | 9 +++++-- src/proto_alpha/lib_protocol/storage.ml | 2 +- src/proto_alpha/lib_protocol/storage.mli | 2 +- 8 files changed, 29 insertions(+), 26 deletions(-) diff --git a/src/proto_alpha/lib_protocol/stake_context.ml b/src/proto_alpha/lib_protocol/stake_context.ml index d04989a38582..61f43a6c3960 100644 --- a/src/proto_alpha/lib_protocol/stake_context.ml +++ b/src/proto_alpha/lib_protocol/stake_context.ml @@ -38,13 +38,14 @@ let staking_weight ctxt {frozen; delegated} = let compare ctxt s1 s2 = Int64.compare (staking_weight ctxt s1) (staking_weight ctxt s2) -let voting_weight ctxt {Stake_repr.Full.own_frozen; staked_frozen; delegated} = +let voting_weight ctxt + {Full_staking_balance_repr.own_frozen; staked_frozen; delegated} = let open Result_syntax in let+ frozen = Tez_repr.(own_frozen +? staked_frozen) in staking_weight ctxt (Stake_repr.make ~frozen ~delegated) let apply_limits ctxt staking_parameters - {Stake_repr.Full.own_frozen; staked_frozen; delegated} = + {Full_staking_balance_repr.own_frozen; staked_frozen; delegated} = let open Result_syntax in let limit_of_delegation_over_baking = Int64.of_int (Constants_storage.limit_of_delegation_over_baking ctxt) diff --git a/src/proto_alpha/lib_protocol/stake_context.mli b/src/proto_alpha/lib_protocol/stake_context.mli index f794e5ce37e1..e9fc44b61eaa 100644 --- a/src/proto_alpha/lib_protocol/stake_context.mli +++ b/src/proto_alpha/lib_protocol/stake_context.mli @@ -31,7 +31,7 @@ val apply_limits : Raw_context.t -> Staking_parameters_repr.t -> - Stake_repr.Full.t -> + Full_staking_balance_repr.t -> Stake_repr.t tzresult (** The weight of a staker or a set of stakers. When adaptive @@ -42,13 +42,14 @@ val apply_limits : val staking_weight : Raw_context.t -> Stake_repr.t -> int64 (** The weight of a delegate used for voting rights. *) -val voting_weight : Raw_context.t -> Stake_repr.Full.t -> int64 tzresult +val voting_weight : + Raw_context.t -> Full_staking_balance_repr.t -> int64 tzresult (** The weight of a baker used for baking and attesting rights. *) val baking_weight : Raw_context.t -> Staking_parameters_repr.t -> - Stake_repr.Full.t -> + Full_staking_balance_repr.t -> int64 tzresult val compare : Raw_context.t -> Stake_repr.t -> Stake_repr.t -> int diff --git a/src/proto_alpha/lib_protocol/stake_repr.ml b/src/proto_alpha/lib_protocol/stake_repr.ml index b6e968d1bb28..993c08e23c17 100644 --- a/src/proto_alpha/lib_protocol/stake_repr.ml +++ b/src/proto_alpha/lib_protocol/stake_repr.ml @@ -100,5 +100,3 @@ let ( +? ) {frozen = f1; delegated = d1} {frozen = f2; delegated = d2} = let* frozen = Tez_repr.(f1 +? f2) in let+ delegated = Tez_repr.(d1 +? d2) in {frozen; delegated} - -module Full = Full_staking_balance_repr diff --git a/src/proto_alpha/lib_protocol/stake_repr.mli b/src/proto_alpha/lib_protocol/stake_repr.mli index af5e6d2bfbf4..ff9a1cf13b21 100644 --- a/src/proto_alpha/lib_protocol/stake_repr.mli +++ b/src/proto_alpha/lib_protocol/stake_repr.mli @@ -53,5 +53,3 @@ val encoding : t Data_encoding.t val get_frozen : t -> Tez_repr.t val ( +? ) : t -> t -> t tzresult - -module Full = Full_staking_balance_repr diff --git a/src/proto_alpha/lib_protocol/stake_storage.ml b/src/proto_alpha/lib_protocol/stake_storage.ml index 97a3f1ceda55..e82348aad9e5 100644 --- a/src/proto_alpha/lib_protocol/stake_storage.ml +++ b/src/proto_alpha/lib_protocol/stake_storage.ml @@ -76,7 +76,7 @@ end let get_full_staking_balance ctxt delegate = let open Lwt_result_syntax in let+ staking_balance_opt = Storage.Stake.Staking_balance.find ctxt delegate in - Option.value staking_balance_opt ~default:Stake_repr.Full.zero + Option.value staking_balance_opt ~default:Full_staking_balance_repr.zero let get_initialized_stake ctxt delegate = let open Lwt_result_syntax in @@ -84,12 +84,12 @@ let get_initialized_stake ctxt delegate = match balance_opt with | Some staking_balance -> return (staking_balance, ctxt) | None -> - let balance = Stake_repr.Full.zero in + let balance = Full_staking_balance_repr.zero in let* ctxt = Storage.Stake.Staking_balance.init ctxt delegate balance in return (balance, ctxt) let has_minimal_stake ctxt - {Stake_repr.Full.own_frozen; staked_frozen; delegated} = + {Full_staking_balance_repr.own_frozen; staked_frozen; delegated} = let open Result_syntax in let open Tez_repr in let minimal_stake = Constants_storage.minimal_stake ctxt in @@ -150,26 +150,26 @@ let remove_delegated_stake ctxt delegate amount = let open Result_syntax in update_stake ctxt delegate ~f:(fun {own_frozen; staked_frozen; delegated} -> let+ delegated = Tez_repr.(delegated -? amount) in - Stake_repr.Full.make ~own_frozen ~staked_frozen ~delegated) + Full_staking_balance_repr.make ~own_frozen ~staked_frozen ~delegated) let remove_own_frozen_stake ctxt delegate amount = let open Result_syntax in update_stake ctxt delegate ~f:(fun {own_frozen; staked_frozen; delegated} -> let+ own_frozen = Tez_repr.(own_frozen -? amount) in - Stake_repr.Full.make ~own_frozen ~staked_frozen ~delegated) + Full_staking_balance_repr.make ~own_frozen ~staked_frozen ~delegated) let remove_staked_frozen_stake ctxt delegate amount = let open Result_syntax in update_stake ctxt delegate ~f:(fun {own_frozen; staked_frozen; delegated} -> let+ staked_frozen = Tez_repr.(staked_frozen -? amount) in - Stake_repr.Full.make ~own_frozen ~staked_frozen ~delegated) + Full_staking_balance_repr.make ~own_frozen ~staked_frozen ~delegated) let remove_shared_frozen_stake ctxt delegate amount = let open Result_syntax in update_stake ctxt delegate ~f:(fun {own_frozen; staked_frozen; delegated} -> if Tez_repr.(staked_frozen = zero) then let+ own_frozen = Tez_repr.(own_frozen -? amount) in - Stake_repr.Full.make ~own_frozen ~staked_frozen ~delegated + Full_staking_balance_repr.make ~own_frozen ~staked_frozen ~delegated else let* total_frozen = Tez_repr.(own_frozen +? staked_frozen) in let* own_part = @@ -181,7 +181,7 @@ let remove_shared_frozen_stake ctxt delegate amount = let* own_frozen = Tez_repr.(own_frozen -? own_part) in let* staked_part = Tez_repr.(amount -? own_part) in let+ staked_frozen = Tez_repr.(staked_frozen -? staked_part) in - Stake_repr.Full.make ~own_frozen ~staked_frozen ~delegated) + Full_staking_balance_repr.make ~own_frozen ~staked_frozen ~delegated) let remove_frozen_stake ctxt staker amount = match staker with @@ -196,26 +196,26 @@ let add_delegated_stake ctxt delegate amount = let open Result_syntax in update_stake ctxt delegate ~f:(fun {own_frozen; staked_frozen; delegated} -> let+ delegated = Tez_repr.(delegated +? amount) in - Stake_repr.Full.make ~own_frozen ~staked_frozen ~delegated) + Full_staking_balance_repr.make ~own_frozen ~staked_frozen ~delegated) let add_own_frozen_stake ctxt delegate amount = let open Result_syntax in update_stake ctxt delegate ~f:(fun {own_frozen; staked_frozen; delegated} -> let+ own_frozen = Tez_repr.(own_frozen +? amount) in - Stake_repr.Full.make ~own_frozen ~staked_frozen ~delegated) + Full_staking_balance_repr.make ~own_frozen ~staked_frozen ~delegated) let add_staked_frozen_stake ctxt delegate amount = let open Result_syntax in update_stake ctxt delegate ~f:(fun {own_frozen; staked_frozen; delegated} -> let+ staked_frozen = Tez_repr.(staked_frozen +? amount) in - Stake_repr.Full.make ~own_frozen ~staked_frozen ~delegated) + Full_staking_balance_repr.make ~own_frozen ~staked_frozen ~delegated) let add_shared_frozen_stake ctxt delegate amount = let open Result_syntax in update_stake ctxt delegate ~f:(fun {own_frozen; staked_frozen; delegated} -> if Tez_repr.(staked_frozen = zero) then let+ own_frozen = Tez_repr.(own_frozen +? amount) in - Stake_repr.Full.make ~own_frozen ~staked_frozen ~delegated + Full_staking_balance_repr.make ~own_frozen ~staked_frozen ~delegated else let* total_frozen = Tez_repr.(own_frozen +? staked_frozen) in let* own_part = @@ -227,7 +227,7 @@ let add_shared_frozen_stake ctxt delegate amount = let* own_frozen = Tez_repr.(own_frozen +? own_part) in let* staked_part = Tez_repr.(amount -? own_part) in let+ staked_frozen = Tez_repr.(staked_frozen +? staked_part) in - Stake_repr.Full.make ~own_frozen ~staked_frozen ~delegated) + Full_staking_balance_repr.make ~own_frozen ~staked_frozen ~delegated) let add_frozen_stake ctxt staker amount = match staker with diff --git a/src/proto_alpha/lib_protocol/stake_storage.mli b/src/proto_alpha/lib_protocol/stake_storage.mli index ddb402cdefd1..11d1c8ead4f6 100644 --- a/src/proto_alpha/lib_protocol/stake_storage.mli +++ b/src/proto_alpha/lib_protocol/stake_storage.mli @@ -35,7 +35,9 @@ *) val get_full_staking_balance : - Raw_context.t -> Signature.public_key_hash -> Stake_repr.Full.t tzresult Lwt.t + Raw_context.t -> + Signature.public_key_hash -> + Full_staking_balance_repr.t tzresult Lwt.t val remove_delegated_stake : Raw_context.t -> @@ -87,7 +89,10 @@ val fold : val fold_snapshot : Raw_context.t -> index:int -> - f:(Signature.Public_key_hash.t * Stake_repr.Full.t -> 'a -> 'a tzresult Lwt.t) -> + f: + (Signature.Public_key_hash.t * Full_staking_balance_repr.t -> + 'a -> + 'a tzresult Lwt.t) -> init:'a -> 'a tzresult Lwt.t diff --git a/src/proto_alpha/lib_protocol/storage.ml b/src/proto_alpha/lib_protocol/storage.ml index f58e7ac7f001..2704060c0890 100644 --- a/src/proto_alpha/lib_protocol/storage.ml +++ b/src/proto_alpha/lib_protocol/storage.ml @@ -1322,7 +1322,7 @@ module Stake = struct end)) (Int31_index) (Public_key_hash_index) - (Stake_repr.Full) + (Full_staking_balance_repr) module Active_delegates_with_minimal_stake = Make_indexed_data_snapshotable_storage diff --git a/src/proto_alpha/lib_protocol/storage.mli b/src/proto_alpha/lib_protocol/storage.mli index ec0c55ea99cb..6fc5b3f1d793 100644 --- a/src/proto_alpha/lib_protocol/storage.mli +++ b/src/proto_alpha/lib_protocol/storage.mli @@ -511,7 +511,7 @@ module Stake : sig module Staking_balance : Indexed_data_snapshotable_storage with type key = Signature.Public_key_hash.t - and type value = Stake_repr.Full.t + and type value = Full_staking_balance_repr.t and type snapshot = int and type t := Raw_context.t -- GitLab From 6c2a806612386b63bc985c009125e4239505309e Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Fri, 15 Sep 2023 15:19:15 +0200 Subject: [PATCH 03/12] Proto/Stake_repr: move staker to a separate module --- src/proto_alpha/lib_protocol/TEZOS_PROTOCOL | 1 + src/proto_alpha/lib_protocol/dune | 4 ++ src/proto_alpha/lib_protocol/stake_repr.ml | 58 +----------------- src/proto_alpha/lib_protocol/stake_repr.mli | 17 +----- src/proto_alpha/lib_protocol/staker_repr.ml | 63 ++++++++++++++++++++ src/proto_alpha/lib_protocol/staker_repr.mli | 23 +++++++ 6 files changed, 93 insertions(+), 73 deletions(-) create mode 100644 src/proto_alpha/lib_protocol/staker_repr.ml create mode 100644 src/proto_alpha/lib_protocol/staker_repr.mli diff --git a/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL b/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL index fe5c1a234d84..2a86acdc0896 100644 --- a/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL +++ b/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL @@ -116,6 +116,7 @@ "Sapling_repr", "Lazy_storage_kind", "Full_staking_balance_repr", + "Staker_repr", "Stake_repr", "Receipt_repr", "Migration_repr", diff --git a/src/proto_alpha/lib_protocol/dune b/src/proto_alpha/lib_protocol/dune index d7b4f0ba1165..b7f947e6275e 100644 --- a/src/proto_alpha/lib_protocol/dune +++ b/src/proto_alpha/lib_protocol/dune @@ -138,6 +138,7 @@ Sapling_repr Lazy_storage_kind Full_staking_balance_repr + Staker_repr Stake_repr Receipt_repr Migration_repr @@ -426,6 +427,7 @@ sapling_repr.ml lazy_storage_kind.ml lazy_storage_kind.mli full_staking_balance_repr.ml full_staking_balance_repr.mli + staker_repr.ml staker_repr.mli stake_repr.ml stake_repr.mli receipt_repr.ml receipt_repr.mli migration_repr.ml migration_repr.mli @@ -716,6 +718,7 @@ sapling_repr.ml lazy_storage_kind.ml lazy_storage_kind.mli full_staking_balance_repr.ml full_staking_balance_repr.mli + staker_repr.ml staker_repr.mli stake_repr.ml stake_repr.mli receipt_repr.ml receipt_repr.mli migration_repr.ml migration_repr.mli @@ -990,6 +993,7 @@ sapling_repr.ml lazy_storage_kind.ml lazy_storage_kind.mli full_staking_balance_repr.ml full_staking_balance_repr.mli + staker_repr.ml staker_repr.mli stake_repr.ml stake_repr.mli receipt_repr.ml receipt_repr.mli migration_repr.ml migration_repr.mli diff --git a/src/proto_alpha/lib_protocol/stake_repr.ml b/src/proto_alpha/lib_protocol/stake_repr.ml index 993c08e23c17..9abe4528a65a 100644 --- a/src/proto_alpha/lib_protocol/stake_repr.ml +++ b/src/proto_alpha/lib_protocol/stake_repr.ml @@ -22,63 +22,7 @@ (* DEALINGS IN THE SOFTWARE. *) (* *) (*****************************************************************************) - -type staker = - | Single of Contract_repr.t * Signature.public_key_hash - | Shared of Signature.public_key_hash - -let staker_encoding = - let open Data_encoding in - let single_tag = 0 in - let single_encoding = - obj2 - (req "contract" Contract_repr.encoding) - (req "delegate" Signature.Public_key_hash.encoding) - in - let shared_tag = 1 in - let shared_encoding = - obj1 (req "delegate" Signature.Public_key_hash.encoding) - in - def - ~title:"staker" - ~description: - "Abstract notion of staker used in operation receipts, either a single \ - staker or all the stakers delegating to some delegate." - "staker" - @@ matching - (function - | Single (contract, delegate) -> - matched single_tag single_encoding (contract, delegate) - | Shared delegate -> matched shared_tag shared_encoding delegate) - [ - case - ~title:"Single" - (Tag single_tag) - single_encoding - (function - | Single (contract, delegate) -> Some (contract, delegate) - | _ -> None) - (fun (contract, delegate) -> Single (contract, delegate)); - case - ~title:"Shared" - (Tag shared_tag) - shared_encoding - (function Shared delegate -> Some delegate | _ -> None) - (fun delegate -> Shared delegate); - ] - -let compare_staker sa sb = - match (sa, sb) with - | Single (ca, da), Single (cb, db) -> - Compare.or_else (Contract_repr.compare ca cb) (fun () -> - Signature.Public_key_hash.compare da db) - | Shared da, Shared db -> Signature.Public_key_hash.compare da db - | Single _, Shared _ -> -1 - | Shared _, Single _ -> 1 - -let staker_delegate = function - | Single (_contract, delegate) -> delegate - | Shared delegate -> delegate +include Staker_repr type t = {frozen : Tez_repr.t; delegated : Tez_repr.t} diff --git a/src/proto_alpha/lib_protocol/stake_repr.mli b/src/proto_alpha/lib_protocol/stake_repr.mli index ff9a1cf13b21..82ea35d2d428 100644 --- a/src/proto_alpha/lib_protocol/stake_repr.mli +++ b/src/proto_alpha/lib_protocol/stake_repr.mli @@ -23,22 +23,7 @@ (* *) (*****************************************************************************) -(** Adding and removing stake can be done from/toward a delegate, one - of its staker, or both the delegate and all its stakers at - once. We need to distinguish these cases to enforce the staking - over baking limit. *) -type staker = - | Single of Contract_repr.t * Signature.public_key_hash - (* A single staker, either the delegate itself or one of its - staker. *) - | Shared of Signature.public_key_hash -(* The delegate and all its stakers simultaneously. *) - -val staker_encoding : staker Data_encoding.t - -val compare_staker : staker -> staker -> int - -val staker_delegate : staker -> Signature.public_key_hash +include module type of Staker_repr (** Stake of a delegate. *) type t = private {frozen : Tez_repr.t; delegated : Tez_repr.t} diff --git a/src/proto_alpha/lib_protocol/staker_repr.ml b/src/proto_alpha/lib_protocol/staker_repr.ml new file mode 100644 index 000000000000..138bb94a10d9 --- /dev/null +++ b/src/proto_alpha/lib_protocol/staker_repr.ml @@ -0,0 +1,63 @@ +(*****************************************************************************) +(* *) +(* SPDX-License-Identifier: MIT *) +(* Copyright (c) 2023 Nomadic Labs, *) +(* *) +(*****************************************************************************) + +type staker = + | Single of Contract_repr.t * Signature.public_key_hash + | Shared of Signature.public_key_hash + +let staker_encoding = + let open Data_encoding in + let single_tag = 0 in + let single_encoding = + obj2 + (req "contract" Contract_repr.encoding) + (req "delegate" Signature.Public_key_hash.encoding) + in + let shared_tag = 1 in + let shared_encoding = + obj1 (req "delegate" Signature.Public_key_hash.encoding) + in + def + ~title:"staker" + ~description: + "Abstract notion of staker used in operation receipts, either a single \ + staker or all the stakers delegating to some delegate." + "staker" + @@ matching + (function + | Single (contract, delegate) -> + matched single_tag single_encoding (contract, delegate) + | Shared delegate -> matched shared_tag shared_encoding delegate) + [ + case + ~title:"Single" + (Tag single_tag) + single_encoding + (function + | Single (contract, delegate) -> Some (contract, delegate) + | _ -> None) + (fun (contract, delegate) -> Single (contract, delegate)); + case + ~title:"Shared" + (Tag shared_tag) + shared_encoding + (function Shared delegate -> Some delegate | _ -> None) + (fun delegate -> Shared delegate); + ] + +let compare_staker sa sb = + match (sa, sb) with + | Single (ca, da), Single (cb, db) -> + Compare.or_else (Contract_repr.compare ca cb) (fun () -> + Signature.Public_key_hash.compare da db) + | Shared da, Shared db -> Signature.Public_key_hash.compare da db + | Single _, Shared _ -> -1 + | Shared _, Single _ -> 1 + +let staker_delegate = function + | Single (_contract, delegate) -> delegate + | Shared delegate -> delegate diff --git a/src/proto_alpha/lib_protocol/staker_repr.mli b/src/proto_alpha/lib_protocol/staker_repr.mli new file mode 100644 index 000000000000..b65383ac534e --- /dev/null +++ b/src/proto_alpha/lib_protocol/staker_repr.mli @@ -0,0 +1,23 @@ +(*****************************************************************************) +(* *) +(* SPDX-License-Identifier: MIT *) +(* Copyright (c) 2023 Nomadic Labs, *) +(* *) +(*****************************************************************************) + +(** Adding and removing stake can be done from/toward a delegate, one + of its staker, or both the delegate and all its stakers at + once. We need to distinguish these cases to enforce the staking + over baking limit. *) +type staker = + | Single of Contract_repr.t * Signature.public_key_hash + (** A single staker, either the delegate itself or one of its + staker. *) + | Shared of Signature.public_key_hash + (** The delegate and all its stakers simultaneously. *) + +val staker_encoding : staker Data_encoding.t + +val compare_staker : staker -> staker -> int + +val staker_delegate : staker -> Signature.public_key_hash -- GitLab From 222f2fe7020588b66dbb13a33f1ec2204a507028 Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Fri, 15 Sep 2023 15:26:15 +0200 Subject: [PATCH 04/12] Proto/Stake_repr: remove include --- src/proto_alpha/lib_client/protocol_client_context.ml | 2 +- src/proto_alpha/lib_protocol/bootstrap_storage.ml | 2 +- .../lib_protocol/delegate_staking_parameters.ml | 2 +- .../lib_protocol/frozen_deposits_storage.ml | 4 ++-- .../lib_protocol/frozen_deposits_storage.mli | 4 ++-- src/proto_alpha/lib_protocol/receipt_repr.ml | 10 +++++----- src/proto_alpha/lib_protocol/receipt_repr.mli | 2 +- src/proto_alpha/lib_protocol/stake_repr.ml | 2 -- src/proto_alpha/lib_protocol/stake_repr.mli | 2 -- src/proto_alpha/lib_protocol/stake_storage.ml | 4 ++-- src/proto_alpha/lib_protocol/stake_storage.mli | 4 ++-- src/proto_alpha/lib_protocol/token.ml | 4 ++-- src/proto_alpha/lib_protocol/token.mli | 4 ++-- .../lib_protocol/unstaked_frozen_deposits_storage.ml | 4 ++-- .../lib_protocol/unstaked_frozen_deposits_storage.mli | 4 ++-- 15 files changed, 25 insertions(+), 29 deletions(-) diff --git a/src/proto_alpha/lib_client/protocol_client_context.ml b/src/proto_alpha/lib_client/protocol_client_context.ml index 18f0d9b8ea46..786c9f55f422 100644 --- a/src/proto_alpha/lib_client/protocol_client_context.ml +++ b/src/proto_alpha/lib_client/protocol_client_context.ml @@ -166,7 +166,7 @@ let () = @@ def "script" ["loc"] Protocol.Alpha_context.Script.location_encoding ; register ~pp:Protocol.Alpha_context.Contract.pp @@ def "contract" [] Protocol.Alpha_context.Contract.encoding ; - register @@ def "staker" [] Protocol.Stake_repr.staker_encoding ; + register @@ def "staker" [] Protocol.Staker_repr.staker_encoding ; register @@ def "receipt" diff --git a/src/proto_alpha/lib_protocol/bootstrap_storage.ml b/src/proto_alpha/lib_protocol/bootstrap_storage.ml index 9df5d1b117e2..c4c08806bf1a 100644 --- a/src/proto_alpha/lib_protocol/bootstrap_storage.ml +++ b/src/proto_alpha/lib_protocol/bootstrap_storage.ml @@ -107,7 +107,7 @@ let init_account (ctxt, balance_updates) ctxt (`Contract contract) (`Frozen_deposits - (Stake_repr.Single + (Staker_repr.Single (Contract_repr.Implicit public_key_hash, public_key_hash))) amount_to_freeze) | None -> diff --git a/src/proto_alpha/lib_protocol/delegate_staking_parameters.ml b/src/proto_alpha/lib_protocol/delegate_staking_parameters.ml index 1b15ba70da19..3cdc38be0c9c 100644 --- a/src/proto_alpha/lib_protocol/delegate_staking_parameters.ml +++ b/src/proto_alpha/lib_protocol/delegate_staking_parameters.ml @@ -165,7 +165,7 @@ let pay_rewards ctxt ?active_stake ~source ~delegate rewards = Token.transfer ctxt source - (`Frozen_deposits (Stake_repr.Shared delegate)) + (`Frozen_deposits (Staker_repr.Shared delegate)) to_frozen in let+ ctxt, balance_updates_spendable_rewards = diff --git a/src/proto_alpha/lib_protocol/frozen_deposits_storage.ml b/src/proto_alpha/lib_protocol/frozen_deposits_storage.ml index c39d69feb61d..7242780dc67e 100644 --- a/src/proto_alpha/lib_protocol/frozen_deposits_storage.ml +++ b/src/proto_alpha/lib_protocol/frozen_deposits_storage.ml @@ -45,13 +45,13 @@ let update_balance ctxt delegate f amount = let credit_only_call_from_token ctxt staker amount = let open Lwt_result_syntax in - let delegate = Stake_repr.staker_delegate staker in + let delegate = Staker_repr.staker_delegate staker in let* ctxt = update_balance ctxt delegate Tez_repr.( +? ) amount in Stake_storage.add_frozen_stake ctxt staker amount let spend_only_call_from_token ctxt staker amount = let open Lwt_result_syntax in - let delegate = Stake_repr.staker_delegate staker in + let delegate = Staker_repr.staker_delegate staker in let* ctxt = update_balance ctxt delegate Tez_repr.( -? ) amount in Stake_storage.remove_frozen_stake ctxt staker amount diff --git a/src/proto_alpha/lib_protocol/frozen_deposits_storage.mli b/src/proto_alpha/lib_protocol/frozen_deposits_storage.mli index e223ee3b515f..cb46a606a2ab 100644 --- a/src/proto_alpha/lib_protocol/frozen_deposits_storage.mli +++ b/src/proto_alpha/lib_protocol/frozen_deposits_storage.mli @@ -37,7 +37,7 @@ val get : Raw_context.t -> Contract_repr.t -> Deposits_repr.t tzresult Lwt.t given [staker] increases by [tez]. *) val credit_only_call_from_token : Raw_context.t -> - Stake_repr.staker -> + Staker_repr.staker -> Tez_repr.t -> Raw_context.t tzresult Lwt.t @@ -46,7 +46,7 @@ val credit_only_call_from_token : represented by [delegate] decreases by [tez].*) val spend_only_call_from_token : Raw_context.t -> - Stake_repr.staker -> + Staker_repr.staker -> Tez_repr.t -> Raw_context.t tzresult Lwt.t diff --git a/src/proto_alpha/lib_protocol/receipt_repr.ml b/src/proto_alpha/lib_protocol/receipt_repr.ml index e0a42a4f09cf..0898f1a07523 100644 --- a/src/proto_alpha/lib_protocol/receipt_repr.ml +++ b/src/proto_alpha/lib_protocol/receipt_repr.ml @@ -24,7 +24,7 @@ (* *) (*****************************************************************************) -type staker = Stake_repr.staker = +type staker = Staker_repr.staker = | Single of Contract_repr.t * Signature.public_key_hash | Shared of Signature.public_key_hash @@ -91,7 +91,7 @@ let balance_encoding ~use_legacy_attestation_name = (obj3 (req "kind" (constant "freezer")) (req "category" (constant "deposits")) - (req "staker" Stake_repr.staker_encoding)) + (req "staker" Staker_repr.staker_encoding)) (function Deposits staker -> Some ((), (), staker) | _ -> None) (fun ((), (), staker) -> Deposits staker); case @@ -261,7 +261,7 @@ let balance_encoding ~use_legacy_attestation_name = (obj4 (req "kind" (constant "freezer")) (req "category" (constant "unstaked_deposits")) - (req "staker" Stake_repr.staker_encoding) + (req "staker" Staker_repr.staker_encoding) (req "cycle" Cycle_repr.encoding)) (function | Unstaked_deposits (staker, cycle) -> Some ((), (), staker, cycle) @@ -279,9 +279,9 @@ let is_not_zero c = not (Compare.Int.equal c 0) let compare_balance ba bb = match (ba, bb) with | Contract ca, Contract cb -> Contract_repr.compare ca cb - | Deposits sa, Deposits sb -> Stake_repr.compare_staker sa sb + | Deposits sa, Deposits sb -> Staker_repr.compare_staker sa sb | Unstaked_deposits (sa, ca), Unstaked_deposits (sb, cb) -> - Compare.or_else (Stake_repr.compare_staker sa sb) (fun () -> + Compare.or_else (Staker_repr.compare_staker sa sb) (fun () -> Cycle_repr.compare ca cb) | Lost_attesting_rewards (pkha, pa, ra), Lost_attesting_rewards (pkhb, pb, rb) -> diff --git a/src/proto_alpha/lib_protocol/receipt_repr.mli b/src/proto_alpha/lib_protocol/receipt_repr.mli index c6c16a6e7704..fbfa8974abe0 100644 --- a/src/proto_alpha/lib_protocol/receipt_repr.mli +++ b/src/proto_alpha/lib_protocol/receipt_repr.mli @@ -24,7 +24,7 @@ (* *) (*****************************************************************************) -type staker = Stake_repr.staker = +type staker = Staker_repr.staker = | Single of Contract_repr.t * Signature.public_key_hash | Shared of Signature.public_key_hash diff --git a/src/proto_alpha/lib_protocol/stake_repr.ml b/src/proto_alpha/lib_protocol/stake_repr.ml index 9abe4528a65a..bbcdbbfad358 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. *) (* *) (*****************************************************************************) -include Staker_repr - type t = {frozen : Tez_repr.t; delegated : Tez_repr.t} let make ~frozen ~delegated = {frozen; delegated} diff --git a/src/proto_alpha/lib_protocol/stake_repr.mli b/src/proto_alpha/lib_protocol/stake_repr.mli index 82ea35d2d428..9613536321c9 100644 --- a/src/proto_alpha/lib_protocol/stake_repr.mli +++ b/src/proto_alpha/lib_protocol/stake_repr.mli @@ -23,8 +23,6 @@ (* *) (*****************************************************************************) -include module type of Staker_repr - (** Stake of a delegate. *) type t = private {frozen : Tez_repr.t; delegated : Tez_repr.t} diff --git a/src/proto_alpha/lib_protocol/stake_storage.ml b/src/proto_alpha/lib_protocol/stake_storage.ml index e82348aad9e5..82196d4e6225 100644 --- a/src/proto_alpha/lib_protocol/stake_storage.ml +++ b/src/proto_alpha/lib_protocol/stake_storage.ml @@ -185,7 +185,7 @@ let remove_shared_frozen_stake ctxt delegate amount = let remove_frozen_stake ctxt staker amount = match staker with - | Stake_repr.Single (contract, delegate) + | Staker_repr.Single (contract, delegate) when Contract_repr.(contract = Implicit delegate) -> remove_own_frozen_stake ctxt delegate amount | Single (_staker, delegate) -> @@ -231,7 +231,7 @@ let add_shared_frozen_stake ctxt delegate amount = let add_frozen_stake ctxt staker amount = match staker with - | Stake_repr.Single (contract, delegate) + | Staker_repr.Single (contract, delegate) when Contract_repr.(contract = Implicit delegate) -> add_own_frozen_stake ctxt delegate amount | Single (_staker, delegate) -> add_staked_frozen_stake ctxt delegate amount diff --git a/src/proto_alpha/lib_protocol/stake_storage.mli b/src/proto_alpha/lib_protocol/stake_storage.mli index 11d1c8ead4f6..1069903d7f92 100644 --- a/src/proto_alpha/lib_protocol/stake_storage.mli +++ b/src/proto_alpha/lib_protocol/stake_storage.mli @@ -47,7 +47,7 @@ val remove_delegated_stake : val remove_frozen_stake : Raw_context.t -> - Stake_repr.staker -> + Staker_repr.staker -> Tez_repr.t -> Raw_context.t tzresult Lwt.t @@ -59,7 +59,7 @@ val add_delegated_stake : val add_frozen_stake : Raw_context.t -> - Stake_repr.staker -> + Staker_repr.staker -> Tez_repr.t -> Raw_context.t tzresult Lwt.t diff --git a/src/proto_alpha/lib_protocol/token.ml b/src/proto_alpha/lib_protocol/token.ml index 1a473df44673..fac33494cbdc 100644 --- a/src/proto_alpha/lib_protocol/token.ml +++ b/src/proto_alpha/lib_protocol/token.ml @@ -26,8 +26,8 @@ type container = [ `Contract of Contract_repr.t | `Collected_commitments of Blinded_public_key_hash.t - | `Frozen_deposits of Stake_repr.staker - | `Unstaked_frozen_deposits of Stake_repr.staker * Cycle_repr.t + | `Frozen_deposits of Staker_repr.staker + | `Unstaked_frozen_deposits of Staker_repr.staker * Cycle_repr.t | `Block_fees | `Frozen_bonds of Contract_repr.t * Bond_id_repr.t ] diff --git a/src/proto_alpha/lib_protocol/token.mli b/src/proto_alpha/lib_protocol/token.mli index 0eb8cc167ee8..4605b400c536 100644 --- a/src/proto_alpha/lib_protocol/token.mli +++ b/src/proto_alpha/lib_protocol/token.mli @@ -60,9 +60,9 @@ type container = | `Collected_commitments of Blinded_public_key_hash.t (** Pre-funded account waiting for the commited pkh and activation code to be revealed to unlock the funds *) - | `Frozen_deposits of Stake_repr.staker + | `Frozen_deposits of Staker_repr.staker (** Frozen tokens of a staker for consensus security deposits. *) - | `Unstaked_frozen_deposits of Stake_repr.staker * Cycle_repr.t + | `Unstaked_frozen_deposits of Staker_repr.staker * Cycle_repr.t (** Frozen tokens of a contract that have been unstaked at the given cycle. *) | `Block_fees (** Current block's fees collection *) diff --git a/src/proto_alpha/lib_protocol/unstaked_frozen_deposits_storage.ml b/src/proto_alpha/lib_protocol/unstaked_frozen_deposits_storage.ml index a100b42c97ba..bca591aba98b 100644 --- a/src/proto_alpha/lib_protocol/unstaked_frozen_deposits_storage.ml +++ b/src/proto_alpha/lib_protocol/unstaked_frozen_deposits_storage.ml @@ -70,7 +70,7 @@ let update_balance ~f ctxt delegate_contract cycle = let credit_only_call_from_token ctxt staker cycle amount = let open Lwt_result_syntax in - let delegate = Stake_repr.staker_delegate staker in + let delegate = Staker_repr.staker_delegate staker in let delegate_contract = Contract_repr.Implicit delegate in let f deposits = Deposits_repr.(deposits +? amount) in let* ctxt = Stake_storage.add_delegated_stake ctxt delegate amount in @@ -78,7 +78,7 @@ let credit_only_call_from_token ctxt staker cycle amount = let spend_only_call_from_token ctxt staker cycle amount = let open Lwt_result_syntax in - let delegate = Stake_repr.staker_delegate staker in + let delegate = Staker_repr.staker_delegate staker in let delegate_contract = Contract_repr.Implicit delegate in let f Deposits_repr.{initial_amount; current_amount} = let open Result_syntax in diff --git a/src/proto_alpha/lib_protocol/unstaked_frozen_deposits_storage.mli b/src/proto_alpha/lib_protocol/unstaked_frozen_deposits_storage.mli index 09c21c9870b5..7ef88e98dd08 100644 --- a/src/proto_alpha/lib_protocol/unstaked_frozen_deposits_storage.mli +++ b/src/proto_alpha/lib_protocol/unstaked_frozen_deposits_storage.mli @@ -52,7 +52,7 @@ val get : unslashable cycle. *) val credit_only_call_from_token : Raw_context.t -> - Stake_repr.staker -> + Staker_repr.staker -> Cycle_repr.t -> Tez_repr.t -> Raw_context.t tzresult Lwt.t @@ -65,7 +65,7 @@ val credit_only_call_from_token : too low. *) val spend_only_call_from_token : Raw_context.t -> - Stake_repr.staker -> + Staker_repr.staker -> Cycle_repr.t -> Tez_repr.t -> Raw_context.t tzresult Lwt.t -- GitLab From 9769a2f94a161624fc387d3446d1d980f54916e9 Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Fri, 15 Sep 2023 17:08:55 +0200 Subject: [PATCH 05/12] Proto/Stake_repr: replace delegated with weighted_delegated --- .../lib_protocol/delegate_cycles.ml | 2 +- .../delegate_staking_parameters.ml | 12 ++++---- src/proto_alpha/lib_protocol/stake_context.ml | 28 ++++++++++++------- src/proto_alpha/lib_protocol/stake_repr.ml | 19 +++++++------ src/proto_alpha/lib_protocol/stake_repr.mli | 4 +-- 5 files changed, 37 insertions(+), 28 deletions(-) diff --git a/src/proto_alpha/lib_protocol/delegate_cycles.ml b/src/proto_alpha/lib_protocol/delegate_cycles.ml index a9fd44e00acf..4e0ec7fe358c 100644 --- a/src/proto_alpha/lib_protocol/delegate_cycles.ml +++ b/src/proto_alpha/lib_protocol/delegate_cycles.ml @@ -72,7 +72,7 @@ let update_initial_frozen_deposits ctxt ~new_cycle = let* ctxt, delegates_to_remove = List.fold_left_es (fun (ctxt, delegates_to_remove) - (delegate, Stake_repr.{frozen; delegated = _}) -> + (delegate, Stake_repr.{frozen; weighted_delegated = _}) -> let delegates_to_remove = Signature.Public_key_hash.Set.remove delegate delegates_to_remove in diff --git a/src/proto_alpha/lib_protocol/delegate_staking_parameters.ml b/src/proto_alpha/lib_protocol/delegate_staking_parameters.ml index 3cdc38be0c9c..26ec02ae9ccb 100644 --- a/src/proto_alpha/lib_protocol/delegate_staking_parameters.ml +++ b/src/proto_alpha/lib_protocol/delegate_staking_parameters.ml @@ -89,22 +89,20 @@ Preconditions: - 0 <= [edge_of_baking_over_staking_billionth] <= 1_000_000_000 *) let compute_reward_distrib ~stake ~edge_of_baking_over_staking_billionth - ~edge_of_staking_over_delegation ~(rewards : Tez_repr.t) = - let ({frozen; delegated} : Stake_repr.t) = stake in + ~edge_of_staking_over_delegation:_ ~(rewards : Tez_repr.t) = + let ({frozen; weighted_delegated} : Stake_repr.t) = stake in (* convert into Q *) - let delegated = (* >= 0 *) Q.of_int64 @@ Tez_repr.to_mutez delegated in + let weighted_delegated = + (* >= 0 *) Q.of_int64 @@ Tez_repr.to_mutez weighted_delegated + in let frozen = (* >= 0 *) Q.of_int64 @@ Tez_repr.to_mutez frozen in let baking_over_staking_edge (* 0 <= baking_over_staking_edge <= 1 *) = Q.(of_int32 edge_of_baking_over_staking_billionth / of_int 1_000_000_000) in - let edge_of_staking_over_delegation (* > 0 ? *) = - Q.of_int edge_of_staking_over_delegation - in let rewards_q = Q.of_int64 @@ Tez_repr.to_mutez rewards in (* compute in Q *) let to_frozen = let open Q in - let weighted_delegated = delegated / edge_of_staking_over_delegation in let total_stake = weighted_delegated + frozen in if total_stake <= zero then zero else diff --git a/src/proto_alpha/lib_protocol/stake_context.ml b/src/proto_alpha/lib_protocol/stake_context.ml index 61f43a6c3960..4b8cccb4bd37 100644 --- a/src/proto_alpha/lib_protocol/stake_context.ml +++ b/src/proto_alpha/lib_protocol/stake_context.ml @@ -25,15 +25,10 @@ open Stake_repr -let staking_weight ctxt {frozen; delegated} = +let staking_weight _ctxt {frozen; weighted_delegated} = let frozen = Tez_repr.to_mutez frozen in - let delegated = Tez_repr.to_mutez delegated in - let edge_of_staking_over_delegation = - Constants_storage.adaptive_issuance_edge_of_staking_over_delegation ctxt - in - if Constants_storage.adaptive_issuance_enable ctxt then - Int64.(add frozen (div delegated (of_int edge_of_staking_over_delegation))) - else Int64.add frozen delegated + let weighted_delegated = Tez_repr.to_mutez weighted_delegated in + Int64.add frozen weighted_delegated let compare ctxt s1 s2 = Int64.compare (staking_weight ctxt s1) (staking_weight ctxt s2) @@ -42,7 +37,10 @@ let voting_weight ctxt {Full_staking_balance_repr.own_frozen; staked_frozen; delegated} = let open Result_syntax in let+ frozen = Tez_repr.(own_frozen +? staked_frozen) in - staking_weight ctxt (Stake_repr.make ~frozen ~delegated) + let (* we are cheating here but that's fine *) weighted_delegated = + delegated + in + staking_weight ctxt (Stake_repr.make ~frozen ~weighted_delegated) let apply_limits ctxt staking_parameters {Full_staking_balance_repr.own_frozen; staked_frozen; delegated} = @@ -90,8 +88,18 @@ let apply_limits ctxt staking_parameters | Ok max_allowed_delegated -> Tez_repr.min max_allowed_delegated delegated | Error _max_allowed_delegated_overflows -> delegated in + let* weighted_delegated = + if Constants_storage.adaptive_issuance_enable ctxt then + let edge_of_staking_over_delegation = + Int64.of_int + (Constants_storage.adaptive_issuance_edge_of_staking_over_delegation + ctxt) + in + Tez_repr.(delegated /? edge_of_staking_over_delegation) + else return delegated + in let+ frozen = Tez_repr.(own_frozen +? allowed_staked_frozen) in - Stake_repr.make ~frozen ~delegated + Stake_repr.make ~frozen ~weighted_delegated let baking_weight ctxt staking_parameters f = let open Result_syntax in diff --git a/src/proto_alpha/lib_protocol/stake_repr.ml b/src/proto_alpha/lib_protocol/stake_repr.ml index bbcdbbfad358..daefe84c4b19 100644 --- a/src/proto_alpha/lib_protocol/stake_repr.ml +++ b/src/proto_alpha/lib_protocol/stake_repr.ml @@ -22,23 +22,26 @@ (* DEALINGS IN THE SOFTWARE. *) (* *) (*****************************************************************************) -type t = {frozen : Tez_repr.t; delegated : Tez_repr.t} +(* 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 ~delegated = {frozen; delegated} +let make ~frozen ~weighted_delegated = {frozen; weighted_delegated} let get_frozen {frozen; _} = frozen let encoding = let open Data_encoding in conv - (fun {frozen; delegated} -> (frozen, delegated)) - (fun (frozen, delegated) -> {frozen; delegated}) + (fun {frozen; weighted_delegated} -> (frozen, weighted_delegated)) + (fun (frozen, weighted_delegated) -> {frozen; weighted_delegated}) (obj2 (req "frozen" Tez_repr.encoding) (req "delegated" Tez_repr.encoding)) -let zero = make ~frozen:Tez_repr.zero ~delegated:Tez_repr.zero +let zero = make ~frozen:Tez_repr.zero ~weighted_delegated:Tez_repr.zero -let ( +? ) {frozen = f1; delegated = d1} {frozen = f2; delegated = d2} = +let ( +? ) {frozen = f1; weighted_delegated = d1} + {frozen = f2; weighted_delegated = d2} = let open Result_syntax in let* frozen = Tez_repr.(f1 +? f2) in - let+ delegated = Tez_repr.(d1 +? d2) in - {frozen; delegated} + let+ weighted_delegated = Tez_repr.(d1 +? d2) in + {frozen; weighted_delegated} diff --git a/src/proto_alpha/lib_protocol/stake_repr.mli b/src/proto_alpha/lib_protocol/stake_repr.mli index 9613536321c9..332791f05444 100644 --- a/src/proto_alpha/lib_protocol/stake_repr.mli +++ b/src/proto_alpha/lib_protocol/stake_repr.mli @@ -24,11 +24,11 @@ (*****************************************************************************) (** Stake of a delegate. *) -type t = private {frozen : Tez_repr.t; delegated : Tez_repr.t} +type t = private {frozen : Tez_repr.t; weighted_delegated : Tez_repr.t} val zero : t -val make : frozen:Tez_repr.t -> delegated:Tez_repr.t -> t +val make : frozen:Tez_repr.t -> weighted_delegated:Tez_repr.t -> t val encoding : t Data_encoding.t -- GitLab From ebbcf5818a4203e14fee1c0c309f98625f535872 Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Fri, 15 Sep 2023 17:13:49 +0200 Subject: [PATCH 06/12] Proto/Stake_context: do not use ponderation for voting weight --- src/proto_alpha/lib_protocol/stake_context.ml | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/proto_alpha/lib_protocol/stake_context.ml b/src/proto_alpha/lib_protocol/stake_context.ml index 4b8cccb4bd37..bc7bc59c29ce 100644 --- a/src/proto_alpha/lib_protocol/stake_context.ml +++ b/src/proto_alpha/lib_protocol/stake_context.ml @@ -33,14 +33,12 @@ let staking_weight _ctxt {frozen; weighted_delegated} = let compare ctxt s1 s2 = Int64.compare (staking_weight ctxt s1) (staking_weight ctxt s2) -let voting_weight ctxt +let voting_weight _ctxt {Full_staking_balance_repr.own_frozen; staked_frozen; delegated} = let open Result_syntax in - let+ frozen = Tez_repr.(own_frozen +? staked_frozen) in - let (* we are cheating here but that's fine *) weighted_delegated = - delegated - in - staking_weight ctxt (Stake_repr.make ~frozen ~weighted_delegated) + let* frozen = Tez_repr.(own_frozen +? staked_frozen) in + let+ all = Tez_repr.(frozen +? delegated) in + Tez_repr.to_mutez all let apply_limits ctxt staking_parameters {Full_staking_balance_repr.own_frozen; staked_frozen; delegated} = -- GitLab From 56fbb5db0e2c5efc0b5d55d041dff9eb36b609ef Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Fri, 15 Sep 2023 17:16:38 +0200 Subject: [PATCH 07/12] Proto/Stake: move voting_weight --- src/proto_alpha/lib_protocol/full_staking_balance_repr.ml | 6 ++++++ src/proto_alpha/lib_protocol/full_staking_balance_repr.mli | 3 +++ src/proto_alpha/lib_protocol/stake_context.ml | 7 ------- src/proto_alpha/lib_protocol/stake_context.mli | 4 ---- src/proto_alpha/lib_protocol/vote_storage.ml | 2 +- 5 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/proto_alpha/lib_protocol/full_staking_balance_repr.ml b/src/proto_alpha/lib_protocol/full_staking_balance_repr.ml index eb55d20d7f55..6e80c0ea91c2 100644 --- a/src/proto_alpha/lib_protocol/full_staking_balance_repr.ml +++ b/src/proto_alpha/lib_protocol/full_staking_balance_repr.ml @@ -31,3 +31,9 @@ let encoding = (req "own_frozen" Tez_repr.encoding) (req "staked_frozen" Tez_repr.encoding) (req "delegated" Tez_repr.encoding)) + +let voting_weight {own_frozen; staked_frozen; delegated} = + let open Result_syntax in + let* frozen = Tez_repr.(own_frozen +? staked_frozen) in + let+ all = Tez_repr.(frozen +? delegated) in + Tez_repr.to_mutez all diff --git a/src/proto_alpha/lib_protocol/full_staking_balance_repr.mli b/src/proto_alpha/lib_protocol/full_staking_balance_repr.mli index 93a9f04909cd..70b2d580f732 100644 --- a/src/proto_alpha/lib_protocol/full_staking_balance_repr.mli +++ b/src/proto_alpha/lib_protocol/full_staking_balance_repr.mli @@ -17,3 +17,6 @@ val make : val zero : t val encoding : t Data_encoding.t + +(** The weight of a delegate used for voting rights. *) +val voting_weight : t -> Int64.t tzresult diff --git a/src/proto_alpha/lib_protocol/stake_context.ml b/src/proto_alpha/lib_protocol/stake_context.ml index bc7bc59c29ce..eebdb40c8407 100644 --- a/src/proto_alpha/lib_protocol/stake_context.ml +++ b/src/proto_alpha/lib_protocol/stake_context.ml @@ -33,13 +33,6 @@ let staking_weight _ctxt {frozen; weighted_delegated} = let compare ctxt s1 s2 = Int64.compare (staking_weight ctxt s1) (staking_weight ctxt s2) -let voting_weight _ctxt - {Full_staking_balance_repr.own_frozen; staked_frozen; delegated} = - let open Result_syntax in - let* frozen = Tez_repr.(own_frozen +? staked_frozen) in - let+ all = Tez_repr.(frozen +? delegated) in - Tez_repr.to_mutez all - let apply_limits ctxt staking_parameters {Full_staking_balance_repr.own_frozen; staked_frozen; delegated} = let open Result_syntax in diff --git a/src/proto_alpha/lib_protocol/stake_context.mli b/src/proto_alpha/lib_protocol/stake_context.mli index e9fc44b61eaa..7d175e144e41 100644 --- a/src/proto_alpha/lib_protocol/stake_context.mli +++ b/src/proto_alpha/lib_protocol/stake_context.mli @@ -41,10 +41,6 @@ val apply_limits : have been applied using [apply_limits] if necessary. *) val staking_weight : Raw_context.t -> Stake_repr.t -> int64 -(** The weight of a delegate used for voting rights. *) -val voting_weight : - Raw_context.t -> Full_staking_balance_repr.t -> int64 tzresult - (** The weight of a baker used for baking and attesting rights. *) val baking_weight : Raw_context.t -> diff --git a/src/proto_alpha/lib_protocol/vote_storage.ml b/src/proto_alpha/lib_protocol/vote_storage.ml index c7006d37674c..d6fc761a78ff 100644 --- a/src/proto_alpha/lib_protocol/vote_storage.ml +++ b/src/proto_alpha/lib_protocol/vote_storage.ml @@ -111,7 +111,7 @@ let listings_encoding = let get_current_voting_power_free ctxt delegate = let open Lwt_result_syntax in let* stake = Storage.Stake.Staking_balance.get ctxt delegate in - Lwt.return @@ Stake_context.voting_weight ctxt stake + Lwt.return @@ Full_staking_balance_repr.voting_weight stake let update_listings ctxt = let open Lwt_result_syntax in -- GitLab From 22945b6ed3003e32a958c44410cc4980ad22edbf Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Fri, 15 Sep 2023 17:17:15 +0200 Subject: [PATCH 08/12] Proto/Vote_storage: fix comment --- src/proto_alpha/lib_protocol/vote_storage.mli | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/proto_alpha/lib_protocol/vote_storage.mli b/src/proto_alpha/lib_protocol/vote_storage.mli index 16d9774f2cef..ae938d7b6115 100644 --- a/src/proto_alpha/lib_protocol/vote_storage.mli +++ b/src/proto_alpha/lib_protocol/vote_storage.mli @@ -105,9 +105,7 @@ val listings_encoding : (** Populates [!Storage.Vote.Listings] using the currently existing staking power and sets `Voting_power_in_listings`. Inactive delegates or delegates without the minimal required stake are not - included in the listings. - If adaptive issuance is enabled, voting power accounts for - {!Constants_parametric_repr.edge_of_staking_over_delegation}. *) + included in the listings. *) val update_listings : Raw_context.t -> Raw_context.t tzresult Lwt.t (** Verifies the presence of a delegate in the listing. *) -- GitLab From 09df1726957297c1f9cf7d63c31b337fb6effe6d Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Fri, 15 Sep 2023 17:32:13 +0200 Subject: [PATCH 09/12] Proto/Stake: move staking_weight --- src/proto_alpha/lib_protocol/delegate_cycles.ml | 6 ++---- .../delegate_missed_attestations_storage.ml | 10 ++++------ src/proto_alpha/lib_protocol/delegate_sampler.ml | 2 +- src/proto_alpha/lib_protocol/stake_context.ml | 13 +++---------- src/proto_alpha/lib_protocol/stake_context.mli | 7 ------- src/proto_alpha/lib_protocol/stake_repr.ml | 5 +++++ src/proto_alpha/lib_protocol/stake_repr.mli | 5 +++++ 7 files changed, 20 insertions(+), 28 deletions(-) diff --git a/src/proto_alpha/lib_protocol/delegate_cycles.ml b/src/proto_alpha/lib_protocol/delegate_cycles.ml index 4e0ec7fe358c..cdcf5dbdf247 100644 --- a/src/proto_alpha/lib_protocol/delegate_cycles.ml +++ b/src/proto_alpha/lib_protocol/delegate_cycles.ml @@ -123,7 +123,7 @@ let distribute_attesting_rewards ctxt last_cycle unrevealed_nonces = Stake_storage.get_total_active_stake ctxt last_cycle in let total_active_stake_weight = - Stake_context.staking_weight ctxt total_active_stake + Stake_repr.staking_weight total_active_stake in let* delegates = Stake_storage.get_selected_distribution ctxt last_cycle in List.fold_left_es @@ -137,9 +137,7 @@ let distribute_attesting_rewards ctxt last_cycle unrevealed_nonces = let has_revealed_nonces = delegate_has_revealed_nonces delegate unrevealed_nonces_set in - let active_stake_weight = - Stake_context.staking_weight ctxt active_stake - in + let active_stake_weight = Stake_repr.staking_weight active_stake in let expected_slots = Delegate_missed_attestations_storage .expected_slots_for_given_active_stake diff --git a/src/proto_alpha/lib_protocol/delegate_missed_attestations_storage.ml b/src/proto_alpha/lib_protocol/delegate_missed_attestations_storage.ml index 044bf2fd812a..085321527549 100644 --- a/src/proto_alpha/lib_protocol/delegate_missed_attestations_storage.ml +++ b/src/proto_alpha/lib_protocol/delegate_missed_attestations_storage.ml @@ -84,10 +84,10 @@ let record_attesting_participation ctxt ~delegate ~participation in let expected_slots = let active_stake_weight = - Stake_context.staking_weight ctxt active_stake + Stake_repr.staking_weight active_stake in let total_active_stake_weight = - Stake_context.staking_weight ctxt total_active_stake + Stake_repr.staking_weight total_active_stake in expected_slots_for_given_active_stake ctxt @@ -195,11 +195,9 @@ let participation_info ctxt delegate = Stake_storage.get_total_active_stake ctxt level.cycle in let expected_cycle_activity = - let active_stake_weight = - Stake_context.staking_weight ctxt active_stake - in + let active_stake_weight = Stake_repr.staking_weight active_stake in let total_active_stake_weight = - Stake_context.staking_weight ctxt total_active_stake + Stake_repr.staking_weight total_active_stake in expected_slots_for_given_active_stake ctxt diff --git a/src/proto_alpha/lib_protocol/delegate_sampler.ml b/src/proto_alpha/lib_protocol/delegate_sampler.ml index 79faece3ab79..67288dd95000 100644 --- a/src/proto_alpha/lib_protocol/delegate_sampler.ml +++ b/src/proto_alpha/lib_protocol/delegate_sampler.ml @@ -213,7 +213,7 @@ let select_distribution_for_cycle ctxt cycle = let+ pk = Delegate_consensus_key.active_pubkey_for_cycle ctxt pkh cycle in - (pk, Stake_context.staking_weight ctxt stake) :: acc) + (pk, Stake_repr.staking_weight stake) :: acc) [] stakes in diff --git a/src/proto_alpha/lib_protocol/stake_context.ml b/src/proto_alpha/lib_protocol/stake_context.ml index eebdb40c8407..537f2f586c08 100644 --- a/src/proto_alpha/lib_protocol/stake_context.ml +++ b/src/proto_alpha/lib_protocol/stake_context.ml @@ -23,15 +23,8 @@ (* *) (*****************************************************************************) -open Stake_repr - -let staking_weight _ctxt {frozen; weighted_delegated} = - let frozen = Tez_repr.to_mutez frozen in - let weighted_delegated = Tez_repr.to_mutez weighted_delegated in - Int64.add frozen weighted_delegated - -let compare ctxt s1 s2 = - Int64.compare (staking_weight ctxt s1) (staking_weight ctxt s2) +let compare _ctxt s1 s2 = + Int64.compare (Stake_repr.staking_weight s1) (Stake_repr.staking_weight s2) let apply_limits ctxt staking_parameters {Full_staking_balance_repr.own_frozen; staked_frozen; delegated} = @@ -95,4 +88,4 @@ let apply_limits ctxt staking_parameters let baking_weight ctxt staking_parameters f = let open Result_syntax in let+ s = apply_limits ctxt staking_parameters f in - staking_weight ctxt s + Stake_repr.staking_weight s diff --git a/src/proto_alpha/lib_protocol/stake_context.mli b/src/proto_alpha/lib_protocol/stake_context.mli index 7d175e144e41..1dee908b0919 100644 --- a/src/proto_alpha/lib_protocol/stake_context.mli +++ b/src/proto_alpha/lib_protocol/stake_context.mli @@ -34,13 +34,6 @@ val apply_limits : Full_staking_balance_repr.t -> Stake_repr.t tzresult -(** The weight of a staker or a set of stakers. When adaptive - issuance is active, the delegated tez weight - edge_of_staking_over_delegation less than frozen ones. Since this - function is applied on a [Stake_repr.t], the limits should already - have been applied using [apply_limits] if necessary. *) -val staking_weight : Raw_context.t -> Stake_repr.t -> int64 - (** The weight of a baker used for baking and attesting rights. *) val baking_weight : Raw_context.t -> diff --git a/src/proto_alpha/lib_protocol/stake_repr.ml b/src/proto_alpha/lib_protocol/stake_repr.ml index daefe84c4b19..c99ac996c8e0 100644 --- a/src/proto_alpha/lib_protocol/stake_repr.ml +++ b/src/proto_alpha/lib_protocol/stake_repr.ml @@ -45,3 +45,8 @@ let ( +? ) {frozen = f1; weighted_delegated = d1} let* frozen = Tez_repr.(f1 +? f2) in let+ weighted_delegated = Tez_repr.(d1 +? d2) in {frozen; weighted_delegated} + +let staking_weight {frozen; weighted_delegated} = + let frozen = Tez_repr.to_mutez frozen in + let weighted_delegated = Tez_repr.to_mutez weighted_delegated in + Int64.add frozen weighted_delegated diff --git a/src/proto_alpha/lib_protocol/stake_repr.mli b/src/proto_alpha/lib_protocol/stake_repr.mli index 332791f05444..bc24dcda7201 100644 --- a/src/proto_alpha/lib_protocol/stake_repr.mli +++ b/src/proto_alpha/lib_protocol/stake_repr.mli @@ -36,3 +36,8 @@ val encoding : t Data_encoding.t val get_frozen : t -> Tez_repr.t val ( +? ) : t -> t -> t tzresult + +(** The weight of a staker or a set of stakers. Since this + function is applied on a [Stake_repr.t], the limits should already + have been applied using [apply_limits] if necessary. *) +val staking_weight : t -> int64 -- GitLab From 00bd9a02c77a8974ff86f85b15b9020eea7b3f04 Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Fri, 15 Sep 2023 17:33:57 +0200 Subject: [PATCH 10/12] Proto/Stake: move compare --- src/proto_alpha/lib_protocol/stake_context.ml | 3 --- src/proto_alpha/lib_protocol/stake_context.mli | 2 -- src/proto_alpha/lib_protocol/stake_repr.ml | 2 ++ src/proto_alpha/lib_protocol/stake_repr.mli | 2 ++ src/proto_alpha/lib_protocol/stake_storage.ml | 4 +--- 5 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/proto_alpha/lib_protocol/stake_context.ml b/src/proto_alpha/lib_protocol/stake_context.ml index 537f2f586c08..f0f0648a74d7 100644 --- a/src/proto_alpha/lib_protocol/stake_context.ml +++ b/src/proto_alpha/lib_protocol/stake_context.ml @@ -23,9 +23,6 @@ (* *) (*****************************************************************************) -let compare _ctxt s1 s2 = - Int64.compare (Stake_repr.staking_weight s1) (Stake_repr.staking_weight s2) - let apply_limits ctxt staking_parameters {Full_staking_balance_repr.own_frozen; staked_frozen; delegated} = let open Result_syntax in diff --git a/src/proto_alpha/lib_protocol/stake_context.mli b/src/proto_alpha/lib_protocol/stake_context.mli index 1dee908b0919..513a0f555863 100644 --- a/src/proto_alpha/lib_protocol/stake_context.mli +++ b/src/proto_alpha/lib_protocol/stake_context.mli @@ -40,5 +40,3 @@ val baking_weight : Staking_parameters_repr.t -> Full_staking_balance_repr.t -> int64 tzresult - -val compare : Raw_context.t -> Stake_repr.t -> Stake_repr.t -> int diff --git a/src/proto_alpha/lib_protocol/stake_repr.ml b/src/proto_alpha/lib_protocol/stake_repr.ml index c99ac996c8e0..a2890c52e7c0 100644 --- a/src/proto_alpha/lib_protocol/stake_repr.ml +++ b/src/proto_alpha/lib_protocol/stake_repr.ml @@ -50,3 +50,5 @@ let staking_weight {frozen; weighted_delegated} = let frozen = Tez_repr.to_mutez frozen in let weighted_delegated = Tez_repr.to_mutez weighted_delegated in Int64.add frozen weighted_delegated + +let compare s1 s2 = Int64.compare (staking_weight s1) (staking_weight s2) diff --git a/src/proto_alpha/lib_protocol/stake_repr.mli b/src/proto_alpha/lib_protocol/stake_repr.mli index bc24dcda7201..e7f58ea9c875 100644 --- a/src/proto_alpha/lib_protocol/stake_repr.mli +++ b/src/proto_alpha/lib_protocol/stake_repr.mli @@ -41,3 +41,5 @@ val ( +? ) : t -> t -> t tzresult function is applied on a [Stake_repr.t], the limits should already have been applied using [apply_limits] if necessary. *) val staking_weight : t -> int64 + +val compare : t -> t -> int diff --git a/src/proto_alpha/lib_protocol/stake_storage.ml b/src/proto_alpha/lib_protocol/stake_storage.ml index 82196d4e6225..76a511869ec7 100644 --- a/src/proto_alpha/lib_protocol/stake_storage.ml +++ b/src/proto_alpha/lib_protocol/stake_storage.ml @@ -266,9 +266,7 @@ let max_snapshot_index = Storage.Stake.Last_snapshot.get let set_selected_distribution_for_cycle ctxt cycle stakes total_stake = let open Lwt_result_syntax in - let stakes = - List.sort (fun (_, x) (_, y) -> Stake_context.compare ctxt y x) stakes - in + let stakes = List.sort (fun (_, x) (_, y) -> Stake_repr.compare y x) stakes in let* ctxt = Selected_distribution_for_cycle.init ctxt cycle stakes in let*! ctxt = Storage.Stake.Total_active_stake.add ctxt cycle total_stake in (* cleanup snapshots *) -- GitLab From d54a822bcc974bb4cbc10eb8d57178b781beb177 Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Fri, 15 Sep 2023 17:41:21 +0200 Subject: [PATCH 11/12] Proto/Delegate_staking_parameters: simplify compute_reward_distrib type --- .../lib_protocol/delegate_staking_parameters.ml | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/src/proto_alpha/lib_protocol/delegate_staking_parameters.ml b/src/proto_alpha/lib_protocol/delegate_staking_parameters.ml index 26ec02ae9ccb..3e5497cb9e98 100644 --- a/src/proto_alpha/lib_protocol/delegate_staking_parameters.ml +++ b/src/proto_alpha/lib_protocol/delegate_staking_parameters.ml @@ -79,17 +79,15 @@ type reward_distrib = {to_frozen : Tez_repr.t; to_spendable : Tez_repr.t} (** Compute the reward distribution between frozen and spendable according to: - the [stake] of the delegate composed of the [frozen] deposits and the - [delegated] tokens. + [weighted_delegated] tokens. - the [edge_of_baking_over_staking_billionth] parameter set by the baker in 1_000_000_000th - - the [edge_of_staking_over_delegation] constant. - the [rewards] to be distributed Preconditions: - - [edge_of_staking_over_delegation] > 0 - 0 <= [edge_of_baking_over_staking_billionth] <= 1_000_000_000 *) let compute_reward_distrib ~stake ~edge_of_baking_over_staking_billionth - ~edge_of_staking_over_delegation:_ ~(rewards : Tez_repr.t) = + ~(rewards : Tez_repr.t) = let ({frozen; weighted_delegated} : Stake_repr.t) = stake in (* convert into Q *) let weighted_delegated = @@ -130,16 +128,10 @@ let compute_reward_distrib ctxt delegate stake rewards = let edge_of_baking_over_staking_billionth = delegate_parameter.edge_of_baking_over_staking_billionth in - let edge_of_staking_over_delegation = - if Constants_storage.adaptive_issuance_enable ctxt then - Constants_storage.adaptive_issuance_edge_of_staking_over_delegation ctxt - else 1 - in Lwt.return @@ compute_reward_distrib ~stake ~edge_of_baking_over_staking_billionth - ~edge_of_staking_over_delegation ~rewards let pay_rewards ctxt ?active_stake ~source ~delegate rewards = -- GitLab From d95f7e03e3fee29cafe17b2be9db2034e0c1e0b4 Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Wed, 20 Sep 2023 11:05:07 +0200 Subject: [PATCH 12/12] Proto/Tests/AI: update assert_voting_power --- .../test/integration/test_adaptive_issuance_launch.ml | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/proto_alpha/lib_protocol/test/integration/test_adaptive_issuance_launch.ml b/src/proto_alpha/lib_protocol/test/integration/test_adaptive_issuance_launch.ml index 82a9b41a13fa..71c8b7d54f01 100644 --- a/src/proto_alpha/lib_protocol/test/integration/test_adaptive_issuance_launch.ml +++ b/src/proto_alpha/lib_protocol/test/integration/test_adaptive_issuance_launch.ml @@ -85,21 +85,22 @@ let assert_voting_power ~loc block delegate ~ai_enabled ~expected_staked Int64.of_int constants.parametric.adaptive_issuance.edge_of_staking_over_delegation in - let expected_power = + let expected_voting_power = Int64.add expected_frozen expected_liquid in + let expected_baking_power = if ai_enabled then Int64.add expected_frozen (Int64.div expected_liquid edge_of_staking_over_delegation) - else Int64.add expected_frozen expected_liquid + else expected_voting_power in let* actual_voting_power = Context.get_current_voting_power (B block) delegate in - let* () = Assert.equal_int64 ~loc actual_voting_power expected_power in + let* () = Assert.equal_int64 ~loc actual_voting_power expected_voting_power in let* actual_baking_power = Context.get_current_baking_power (B block) delegate in - Assert.equal_int64 ~loc actual_baking_power expected_power + Assert.equal_int64 ~loc actual_baking_power expected_baking_power (* Test that: - the EMA of the adaptive issuance vote reaches the threshold after the -- GitLab