diff --git a/docs/protocols/alpha.rst b/docs/protocols/alpha.rst index 93ee416f1b79840237f47455780d066ba5c92016..fa3c5e378dc8a69297d740c277345de619f5d957 100644 --- a/docs/protocols/alpha.rst +++ b/docs/protocols/alpha.rst @@ -97,17 +97,6 @@ Breaking Changes baker's spendable balance to their frozen deposits. Bakers need to use the ``stake`` pseudo-operation to increase their frozen deposits. (MR :gl:`!8087`) -- The Exponential Moving Average of the Liquidity Baking Toggle Vote - is now represented on 64 bits (instead of 32 bits in previous - versions of the protocol). This affects the binary and JSON - encodings of block receipts, the - ``liquidity_baking_toggle_ema_threshold`` parameter, and the - ``liquidity_baking_escape_ema`` field of the context. Note that in - JSON, 32-bit integers are represented as JSON numbers but 64-bit - integers are represented as JSON strings. (MR :gl:`!8987`) - -- The context field ``liquidity_baking_escape_ema`` is renamed - ``liquidity_baking_toggle_ema``. (MR :gl:`!8987`) RPC Changes ----------- diff --git a/src/proto_alpha/lib_parameters/default_parameters.ml b/src/proto_alpha/lib_parameters/default_parameters.ml index 80b8a541f77fefd45b44110c862b89edc3e1742f..8e98489a479fd35cf6f4ad297ccc862e9764f61c 100644 --- a/src/proto_alpha/lib_parameters/default_parameters.ml +++ b/src/proto_alpha/lib_parameters/default_parameters.ml @@ -163,7 +163,7 @@ let constants_mainnet = min_proposal_quorum = 5_00l; (* 1/2 window size of 2000 blocks with precision of 1_000_000 for integer computation *) - liquidity_baking_toggle_ema_threshold = 1_000_000_000L; + liquidity_baking_toggle_ema_threshold = 1_000_000_000l; (* The rationale behind the value of this constant is that an operation should be considered alive for about one hour: @@ -293,7 +293,7 @@ let constants_mainnet = staking_over_baking_limit = 5; max_costaking_baker_count = 5; staking_over_delegation_edge = 2; - launch_ema_threshold = 1_600_000_000L; + launch_ema_threshold = 1_600_000_000l; }; } diff --git a/src/proto_alpha/lib_protocol/adaptive_inflation_storage.ml b/src/proto_alpha/lib_protocol/adaptive_inflation_storage.ml index 3cac81c8408e55fd1fa36f37803673d0097e5b13..eb2136ddd122abba5a7ceaaad635aab5af89b5ba 100644 --- a/src/proto_alpha/lib_protocol/adaptive_inflation_storage.ml +++ b/src/proto_alpha/lib_protocol/adaptive_inflation_storage.ml @@ -203,7 +203,7 @@ let load_reward_coeff ctxt = let init ctxt = let open Lwt_result_syntax in - let* ctxt = Storage.Adaptive_inflation.Launch_ema.init ctxt 0L in + let* ctxt = Storage.Adaptive_inflation.Launch_ema.init ctxt 0l in Storage.Adaptive_inflation.Activation.init ctxt None let activate ctxt ~cycle = @@ -213,7 +213,7 @@ let launch_cycle ctxt = Storage.Adaptive_inflation.Activation.get ctxt let update_ema ctxt ~vote = Storage.Adaptive_inflation.Launch_ema.get ctxt >>=? fun old_ema -> - Toggle_votes_repr.Adaptive_inflation_launch_EMA.of_int64 old_ema + Toggle_votes_repr.Adaptive_inflation_launch_EMA.of_int32 old_ema >>=? fun old_ema -> let new_ema = Toggle_votes_repr.compute_new_adaptive_inflation_ema @@ -222,7 +222,7 @@ let update_ema ctxt ~vote = in Storage.Adaptive_inflation.Launch_ema.update ctxt - (Toggle_votes_repr.Adaptive_inflation_launch_EMA.to_int64 new_ema) + (Toggle_votes_repr.Adaptive_inflation_launch_EMA.to_int32 new_ema) >>=? fun ctxt -> let open Constants_storage in (if diff --git a/src/proto_alpha/lib_protocol/alpha_context.mli b/src/proto_alpha/lib_protocol/alpha_context.mli index 64fab0c1bfe585318f30cd80ea22e56852387106..7e4db329f6ce4b525bb681efd3bb8e93317d974b 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/alpha_context.mli @@ -847,7 +847,7 @@ module Constants : sig staking_over_baking_limit : int; max_costaking_baker_count : int; staking_over_delegation_edge : int; - launch_ema_threshold : int64; + launch_ema_threshold : int32; } type reward_weights = { @@ -879,7 +879,7 @@ module Constants : sig quorum_min : int32; quorum_max : int32; min_proposal_quorum : int32; - liquidity_baking_toggle_ema_threshold : int64; + liquidity_baking_toggle_ema_threshold : int32; max_operations_time_to_live : int; minimal_block_delay : Period.t; delay_increment_per_round : Period.t; @@ -956,7 +956,7 @@ module Constants : sig val min_proposal_quorum : context -> int32 - val liquidity_baking_toggle_ema_threshold : context -> int64 + val liquidity_baking_toggle_ema_threshold : context -> int32 val minimal_block_delay : context -> Period.t @@ -4725,15 +4725,15 @@ module Toggle_EMA : sig module type T = sig type t - val of_int64 : Int64.t -> t tzresult Lwt.t + val of_int32 : Int32.t -> t tzresult Lwt.t val zero : t - val to_int64 : t -> Int64.t + val to_int32 : t -> Int32.t val encoding : t Data_encoding.t - val ( < ) : t -> Int64.t -> bool + val ( < ) : t -> Int32.t -> bool val update_ema_up : t -> t diff --git a/src/proto_alpha/lib_protocol/constants_parametric_repr.ml b/src/proto_alpha/lib_protocol/constants_parametric_repr.ml index f9e9dd33bf36bda40eb1b712c0d2c564112be407..d7937893a040842688e80ead53529f1144320488 100644 --- a/src/proto_alpha/lib_protocol/constants_parametric_repr.ml +++ b/src/proto_alpha/lib_protocol/constants_parametric_repr.ml @@ -128,7 +128,7 @@ type adaptive_inflation = { staking_over_baking_limit : int; max_costaking_baker_count : int; staking_over_delegation_edge : int; - launch_ema_threshold : int64; + launch_ema_threshold : int32; } type reward_weights = { @@ -160,7 +160,7 @@ type t = { quorum_min : int32; quorum_max : int32; min_proposal_quorum : int32; - liquidity_baking_toggle_ema_threshold : int64; + liquidity_baking_toggle_ema_threshold : int32; max_operations_time_to_live : int; minimal_block_delay : Period_repr.t; delay_increment_per_round : Period_repr.t; @@ -369,7 +369,7 @@ let adaptive_inflation_encoding = (req "staking_over_baking_limit" uint8) (req "max_costaking_baker_count" uint16) (req "staking_over_delegation_edge" uint8) - (req "adaptive_inflation_launch_ema_threshold" int64)) + (req "adaptive_inflation_launch_ema_threshold" int32)) let reward_weights_encoding = let open Data_encoding in @@ -563,7 +563,7 @@ let encoding = (obj8 (req "quorum_max" int32) (req "min_proposal_quorum" int32) - (req "liquidity_baking_toggle_ema_threshold" int64) + (req "liquidity_baking_toggle_ema_threshold" int32) (req "max_operations_time_to_live" int16) (req "minimal_block_delay" Period_repr.encoding) (req "delay_increment_per_round" Period_repr.encoding) diff --git a/src/proto_alpha/lib_protocol/constants_parametric_repr.mli b/src/proto_alpha/lib_protocol/constants_parametric_repr.mli index 04c8b5b47d2f28dd85926d486c1a468d70b20d31..be72e6331394621f44e444fd11016a1d2c01addf 100644 --- a/src/proto_alpha/lib_protocol/constants_parametric_repr.mli +++ b/src/proto_alpha/lib_protocol/constants_parametric_repr.mli @@ -146,7 +146,7 @@ type adaptive_inflation = { max_costaking_baker_count : (* Maximal number of bakers an account can costake to. *) int; staking_over_delegation_edge : (* Weight of staking over delegation. *) int; - launch_ema_threshold : (* Threshold of the activation vote *) int64; + launch_ema_threshold : (* Threshold of the activation vote *) int32; } type reward_weights = { @@ -185,7 +185,7 @@ type t = { (* in centile of a percentage *) quorum_max : int32; min_proposal_quorum : int32; - liquidity_baking_toggle_ema_threshold : int64; + liquidity_baking_toggle_ema_threshold : int32; max_operations_time_to_live : int; minimal_block_delay : Period_repr.t; delay_increment_per_round : Period_repr.t; diff --git a/src/proto_alpha/lib_protocol/constants_storage.mli b/src/proto_alpha/lib_protocol/constants_storage.mli index e74570d6468916c4dd5f41243451b61909c396fb..c79a94d054341ec888f3b1b8885874ec0dddec5d 100644 --- a/src/proto_alpha/lib_protocol/constants_storage.mli +++ b/src/proto_alpha/lib_protocol/constants_storage.mli @@ -66,7 +66,7 @@ val quorum_max : Raw_context.t -> int32 val min_proposal_quorum : Raw_context.t -> int32 -val liquidity_baking_toggle_ema_threshold : Raw_context.t -> int64 +val liquidity_baking_toggle_ema_threshold : Raw_context.t -> int32 val parametric : Raw_context.t -> Constants_parametric_repr.t @@ -168,6 +168,6 @@ val adaptive_inflation_staking_over_baking_limit : Raw_context.t -> int val adaptive_inflation_staking_over_delegation_edge : Raw_context.t -> int -val adaptive_inflation_launch_ema_threshold : Raw_context.t -> int64 +val adaptive_inflation_launch_ema_threshold : Raw_context.t -> int32 val freeze_rewards : Raw_context.t -> bool diff --git a/src/proto_alpha/lib_protocol/init_storage.ml b/src/proto_alpha/lib_protocol/init_storage.ml index b69a0df63d0028918a158e086a6d194841bc05d7..d04930f018e0aa28a2b9dae1571ce7473f3b5e18 100644 --- a/src/proto_alpha/lib_protocol/init_storage.ml +++ b/src/proto_alpha/lib_protocol/init_storage.ml @@ -166,44 +166,6 @@ let init_delegates_pseudotokens_for_o ctxt = ctxt (Contract_repr.Implicit delegate)) -(** Migration of the context field storing the Liquidity Baking - EMA. The key of the field is renamed from - "liquidity_baking_escape_ema" to "liquidity_baking_toggle_ema" and - it is now stored on an int64 instead of an int32. *) -let migrate_liquidity_baking_ema ctxt = - let legacy_key = ["liquidity_baking_escape_ema"] in - let get_and_remove_legacy_field ctxt = - Raw_context.get ctxt legacy_key >>=? fun ema_bytes -> - Raw_context.remove_existing ctxt legacy_key >>=? fun ctxt -> - return (ctxt, ema_bytes) - in - (get_and_remove_legacy_field ctxt >|= function - | Ok (ctxt, ema_bytes) -> ( - match - Data_encoding.Binary.of_bytes_opt Data_encoding.int32 ema_bytes - with - | Some ema_i32 -> (ctxt, ema_i32) - | None -> - Logging.log - Logging.Error - "Migration of Liquidity Baking EMA: decoding error, the field %S \ - was expected to be an int32 but it is %a. Defaulting to 0." - (String.concat "/" legacy_key) - Hex.pp - (Hex.of_bytes ema_bytes) ; - (ctxt, 0l)) - | Error trace -> - Logging.log - Logging.Error - "%a@.Migration of Liquidity Baking EMA: the key %S was not found in \ - context so defaulting to 0." - pp_trace - trace - (String.concat "/" legacy_key) ; - (ctxt, 0l)) - >>= fun (ctxt, ema_i32) -> - Storage.Liquidity_baking.Toggle_ema.init ctxt (Int64.of_int32 ema_i32) - let prepare_first_block _chain_id ctxt ~typecheck_smart_contract ~typecheck_smart_rollup ~level ~timestamp ~predecessor = Raw_context.prepare_first_block ~level ~timestamp ctxt @@ -283,7 +245,6 @@ let prepare_first_block _chain_id ctxt ~typecheck_smart_contract initialize_total_supply_for_o ctxt >>= fun ctxt -> Remove_zero_amount_ticket_migration_for_o.remove_zero_ticket_entries ctxt >>= fun ctxt -> - migrate_liquidity_baking_ema ctxt >>=? fun ctxt -> Adaptive_inflation_storage.init ctxt >>=? fun ctxt -> init_delegates_pseudotokens_for_o ctxt >>=? fun ctxt -> return (ctxt, [])) >>=? fun (ctxt, balance_updates) -> diff --git a/src/proto_alpha/lib_protocol/liquidity_baking_migration.ml b/src/proto_alpha/lib_protocol/liquidity_baking_migration.ml index bdce19c0b1c9a79727cf666e80c6261a1c6a8bb0..59a8eb2c654908aa7ef58e382f4b65080f1fb10c 100644 --- a/src/proto_alpha/lib_protocol/liquidity_baking_migration.ml +++ b/src/proto_alpha/lib_protocol/liquidity_baking_migration.ml @@ -198,7 +198,7 @@ let init ctxt ~typecheck = (* We use a custom origination nonce because it is unset when stitching from 009 *) let nonce = Operation_hash.hash_string ["Drip, drip, drip."] in let ctxt = Raw_context.init_origination_nonce ctxt nonce in - Storage.Liquidity_baking.Toggle_ema.init ctxt 0L >>=? fun ctxt -> + Storage.Liquidity_baking.Toggle_ema.init ctxt 0l >>=? fun ctxt -> let current_level = Raw_level_repr.to_int32 (Level_storage.current ctxt).level in diff --git a/src/proto_alpha/lib_protocol/liquidity_baking_storage.ml b/src/proto_alpha/lib_protocol/liquidity_baking_storage.ml index c6e87ab09e765df1d2586c107a2700c07ec495a1..1650758e8eaa382c48175539b24d4e39ad7c2b6e 100644 --- a/src/proto_alpha/lib_protocol/liquidity_baking_storage.ml +++ b/src/proto_alpha/lib_protocol/liquidity_baking_storage.ml @@ -30,7 +30,7 @@ let get_cpmm_address = Storage.Liquidity_baking.Cpmm_address.get let get_toggle_ema ctxt = Storage.Liquidity_baking.Toggle_ema.get ctxt >>=? fun ema -> - Liquidity_baking_toggle_EMA.of_int64 ema + Liquidity_baking_toggle_EMA.of_int32 ema let on_cpmm_exists ctxt f = get_cpmm_address ctxt >>=? fun cpmm_contract -> @@ -46,7 +46,7 @@ let update_toggle_ema ctxt ~toggle_vote = let new_ema = compute_new_liquidity_baking_ema ~toggle_vote old_ema in Storage.Liquidity_baking.Toggle_ema.update ctxt - (Liquidity_baking_toggle_EMA.to_int64 new_ema) + (Liquidity_baking_toggle_EMA.to_int32 new_ema) >|=? fun ctxt -> (ctxt, new_ema) let check_ema_below_threshold ctxt ema = diff --git a/src/proto_alpha/lib_protocol/raw_context.ml b/src/proto_alpha/lib_protocol/raw_context.ml index a6672ffe1ac60dec7b7fa5a59100eabeca2e440b..780a7e378ce1fa39c83d41ddcc83083555ac3ebf 100644 --- a/src/proto_alpha/lib_protocol/raw_context.ml +++ b/src/proto_alpha/lib_protocol/raw_context.ml @@ -1000,7 +1000,7 @@ let prepare_first_block ~level ~timestamp ctxt = max_costaking_baker_count = 5; staking_over_delegation_edge = 2; launch_ema_threshold = - (* 80% of the max ema (which is 2 billion) *) 1_600_000_000L; + (* 80% of the max ema (which is 2 billion) *) 1_600_000_000l; } in @@ -1055,7 +1055,7 @@ let prepare_first_block ~level ~timestamp ctxt = quorum_max = c.quorum_max; min_proposal_quorum = c.min_proposal_quorum; liquidity_baking_toggle_ema_threshold = - Int64.of_int32 c.liquidity_baking_toggle_ema_threshold; + c.liquidity_baking_toggle_ema_threshold; minimal_block_delay = c.minimal_block_delay; delay_increment_per_round = c.delay_increment_per_round; consensus_committee_size = c.consensus_committee_size; diff --git a/src/proto_alpha/lib_protocol/storage.ml b/src/proto_alpha/lib_protocol/storage.ml index 847d90308847555ed8ba4fabc7e66149a889e993..05f6cd50c0d696773970b9487438fd121327504c 100644 --- a/src/proto_alpha/lib_protocol/storage.ml +++ b/src/proto_alpha/lib_protocol/storage.ml @@ -1647,9 +1647,10 @@ module Liquidity_baking = struct module Toggle_ema = Make_single_data_storage (Registered) (Raw_context) (struct - let name = ["liquidity_baking_toggle_ema"] + (* The old "escape" name is kept here to avoid migrating this. *) + let name = ["liquidity_baking_escape_ema"] end) - (Encoding.Int64) + (Encoding.Int32) module Cpmm_address = Make_single_data_storage (Registered) (Raw_context) @@ -1670,7 +1671,7 @@ module Adaptive_inflation = struct (struct let name = ["adaptive_inflation_ema"] end) - (Encoding.Int64) + (Encoding.Int32) module Activation = Make_single_data_storage (Registered) (Raw_context) diff --git a/src/proto_alpha/lib_protocol/storage.mli b/src/proto_alpha/lib_protocol/storage.mli index d3e598b087eebf0e2ccd7082a0319294427520ea..9ca01943cf820954e3b61a0a50ed948eb31b2fb0 100644 --- a/src/proto_alpha/lib_protocol/storage.mli +++ b/src/proto_alpha/lib_protocol/storage.mli @@ -722,7 +722,7 @@ module Liquidity_baking : sig The liquidity baking subsidy is not sent to the CPMM if this EMA is above the threshold set in constants. **) module Toggle_ema : - Single_data_storage with type t := Raw_context.t and type value = Int64.t + Single_data_storage with type t := Raw_context.t and type value = Int32.t (** Constant product market maker contract that receives liquidity baking subsidy. **) module Cpmm_address : @@ -736,7 +736,7 @@ module Adaptive_inflation : sig protocol_data.contents. Once the feature is activated, it can no longer be deactivated without a protocol amendment. **) module Launch_ema : - Single_data_storage with type t := Raw_context.t and type value = Int64.t + Single_data_storage with type t := Raw_context.t and type value = Int32.t (** Cycle [Some c] from which adaptive inflation is (or will be) active, or [None] if the feature is not yet planned to activate. **) diff --git a/src/proto_alpha/lib_protocol/test/helpers/assert.ml b/src/proto_alpha/lib_protocol/test/helpers/assert.ml index d2da6c5c26b7803aca1814b0306dde2badec69bd..4e491c3b294741829e0c40583ddcc4c50954858c 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/assert.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/assert.ml @@ -136,9 +136,6 @@ let not_equal_int64 ~loc (a : int64) (b : int64) = let leq_int64 ~loc (a : int64) (b : int64) = leq ~loc Compare.Int64.compare "Int64 comparison" Int64.pp a b -let lt_int64 ~loc (a : int64) (b : int64) = - lt ~loc Compare.Int64.compare "Int64 comparison" Int64.pp a b - let equal_z ~loc (a : Z.t) (b : Z.t) = equal ~loc Compare.Z.( = ) "Z are not equal" Z.pp_print a b diff --git a/src/proto_alpha/lib_protocol/test/integration/test_liquidity_baking.ml b/src/proto_alpha/lib_protocol/test/integration/test_liquidity_baking.ml index 190d73665fe6ab649ee5ab0113632b83cc05f0da..61bdf96ffc017f593c196db77ed5255dee39eb7e 100644 --- a/src/proto_alpha/lib_protocol/test/integration/test_liquidity_baking.ml +++ b/src/proto_alpha/lib_protocol/test/integration/test_liquidity_baking.ml @@ -258,8 +258,8 @@ let liquidity_baking_toggle_ema n_vote_on n_vote_off level bake_after Assert.leq_int ~loc:__LOC__ (toggle_ema - |> Alpha_context.Toggle_votes.Liquidity_baking_toggle_EMA.to_int64 - |> Int64.to_int) + |> Alpha_context.Toggle_votes.Liquidity_baking_toggle_EMA.to_int32 + |> Int32.to_int) expected_toggle_ema >>=? fun () -> return_unit diff --git a/src/proto_alpha/lib_protocol/test/unit/test_liquidity_baking_repr.ml b/src/proto_alpha/lib_protocol/test/unit/test_liquidity_baking_repr.ml index 388873529b049f52fca8906a6db036e6b7c91cd1..4caf766f8fb18e9d50b9e2afa9f0a63f0b58b374 100644 --- a/src/proto_alpha/lib_protocol/test/unit/test_liquidity_baking_repr.ml +++ b/src/proto_alpha/lib_protocol/test/unit/test_liquidity_baking_repr.ml @@ -34,13 +34,13 @@ open Protocol module Toggle_EMA = Toggle_votes_repr.Liquidity_baking_toggle_EMA -let ema_of_int64 ema = Toggle_EMA.of_int64 ema >|= Environment.wrap_tzresult +let ema_of_int32 ema = Toggle_EMA.of_int32 ema >|= Environment.wrap_tzresult -let ema_to_int64 = Toggle_EMA.to_int64 +let ema_to_int32 = Toggle_EMA.to_int32 let compute_new_ema ~toggle_vote ema = Toggle_votes_repr.compute_new_liquidity_baking_ema ~toggle_vote ema - |> ema_to_int64 + |> ema_to_int32 (* Folds compute_new_ema on a list of votes *) let compute_new_ema_n toggle_votes initial_ema = @@ -49,55 +49,55 @@ let compute_new_ema_n toggle_votes initial_ema = Toggle_votes_repr.compute_new_liquidity_baking_ema ~toggle_vote ema) initial_ema toggle_votes - |> ema_to_int64 + |> ema_to_int32 let ema_range = [ - 0L; - 1L; - 10L; - 100L; - 1000L; - 10_000L; - 100_000L; - 1_000_000L; - 10_000_000L; - 100_000_000L; - 200_000_000L; - 300_000_000L; - 400_000_000L; - 500_000_000L; - 600_000_000L; - 760_000_000L; - 800_000_000L; - 900_000_000L; - 1_000_000_000L; - 1_100_000_000L; - 1_200_000_000L; - 1_300_000_000L; - 1_400_000_000L; - 1_500_000_000L; - 1_600_000_000L; - 1_700_000_000L; - 1_800_000_000L; - 1_900_000_000L; - 1_990_000_000L; - 1_999_000_000L; - 1_999_900_000L; - 1_999_990_000L; - 1_999_999_000L; - 1_999_999_900L; - 1_999_999_990L; - 1_999_999_999L; - 2_000_000_000L; + 0l; + 1l; + 10l; + 100l; + 1000l; + 10_000l; + 100_000l; + 1_000_000l; + 10_000_000l; + 100_000_000l; + 200_000_000l; + 300_000_000l; + 400_000_000l; + 500_000_000l; + 600_000_000l; + 760_000_000l; + 800_000_000l; + 900_000_000l; + 1_000_000_000l; + 1_100_000_000l; + 1_200_000_000l; + 1_300_000_000l; + 1_400_000_000l; + 1_500_000_000l; + 1_600_000_000l; + 1_700_000_000l; + 1_800_000_000l; + 1_900_000_000l; + 1_990_000_000l; + 1_999_000_000l; + 1_999_900_000l; + 1_999_990_000l; + 1_999_999_000l; + 1_999_999_900l; + 1_999_999_990l; + 1_999_999_999l; + 2_000_000_000l; ] (* Test that new_ema = old_ema when voting Pass. *) let test_ema_pass () = List.iter_es (fun old_ema -> - ema_of_int64 old_ema >>=? fun ema -> - Assert.equal_int64 + ema_of_int32 old_ema >>=? fun ema -> + Assert.equal_int32 ~loc:__LOC__ (compute_new_ema ~toggle_vote:Toggle_vote_pass ema) old_ema) @@ -107,10 +107,10 @@ let test_ema_pass () = let test_ema_in_bound_off () = List.iter_es (fun old_ema -> - ema_of_int64 old_ema >>=? fun ema -> + ema_of_int32 old_ema >>=? fun ema -> let new_ema = compute_new_ema ~toggle_vote:Toggle_vote_off ema in - Assert.leq_int64 ~loc:__LOC__ 0L new_ema >>=? fun () -> - Assert.leq_int64 ~loc:__LOC__ new_ema 2_000_000_000L) + Assert.leq_int32 ~loc:__LOC__ 0l new_ema >>=? fun () -> + Assert.leq_int32 ~loc:__LOC__ new_ema 2_000_000_000l) ema_range (* Test that new_ema > old_ema when voting Off, except if old_ema is @@ -118,32 +118,32 @@ let test_ema_in_bound_off () = let test_ema_increases_off () = List.iter_es (fun old_ema -> - ema_of_int64 old_ema >>=? fun ema -> - Assert.lt_int64 + ema_of_int32 old_ema >>=? fun ema -> + Assert.lt_int32 ~loc:__LOC__ old_ema (compute_new_ema ~toggle_vote:Toggle_vote_off ema)) - (List.filter (fun ema -> Compare.Int64.(ema < 1_999_999_000L)) ema_range) + (List.filter (fun ema -> Compare.Int32.(ema < 1_999_999_000l)) ema_range) (* Test that the increase in EMA caused by an Off vote is bounded by 1,000,000 *) let test_ema_increases_off_bound () = List.iter_es (fun old_ema -> - ema_of_int64 old_ema >>=? fun ema -> - Assert.leq_int64 + ema_of_int32 old_ema >>=? fun ema -> + Assert.leq_int32 ~loc:__LOC__ - (Int64.sub (compute_new_ema ~toggle_vote:Toggle_vote_off ema) old_ema) - 1_000_000L) + (Int32.sub (compute_new_ema ~toggle_vote:Toggle_vote_off ema) old_ema) + 1_000_000l) ema_range (* Test that new_ema is still between 0 and 2,000,000,000 after an Off vote. *) let test_ema_in_bound_on () = List.iter_es (fun old_ema -> - ema_of_int64 old_ema >>=? fun ema -> + ema_of_int32 old_ema >>=? fun ema -> let new_ema = compute_new_ema ~toggle_vote:Toggle_vote_on ema in - Assert.leq_int64 ~loc:__LOC__ 0L new_ema >>=? fun () -> - Assert.leq_int64 ~loc:__LOC__ new_ema 2_000_000_000L) + Assert.leq_int32 ~loc:__LOC__ 0l new_ema >>=? fun () -> + Assert.leq_int32 ~loc:__LOC__ new_ema 2_000_000_000l) ema_range (* Test that new_ema < old_ema when voting On, except if old_ema is @@ -151,38 +151,38 @@ let test_ema_in_bound_on () = let test_ema_decreases_on () = List.iter_es (fun old_ema -> - ema_of_int64 old_ema >>=? fun ema -> - Assert.lt_int64 + ema_of_int32 old_ema >>=? fun ema -> + Assert.lt_int32 ~loc:__LOC__ (compute_new_ema ~toggle_vote:Toggle_vote_on ema) old_ema) - (List.filter (fun ema -> Compare.Int64.(ema > 1000L)) ema_range) + (List.filter (fun ema -> Compare.Int32.(ema > 1000l)) ema_range) (* Test that the decrease in EMA caused by an On vote is bounded by 1,000,000 *) let test_ema_decreases_on_bound () = List.iter_es (fun old_ema -> - ema_of_int64 old_ema >>=? fun ema -> - Assert.leq_int64 + ema_of_int32 old_ema >>=? fun ema -> + Assert.leq_int32 ~loc:__LOC__ - (Int64.sub (compute_new_ema ~toggle_vote:Toggle_vote_on ema) old_ema) - 1_000_000L) + (Int32.sub (compute_new_ema ~toggle_vote:Toggle_vote_on ema) old_ema) + 1_000_000l) ema_range (* Test that 1385 Off votes are needed to reach the threshold from 0. *) let test_ema_many_off () = let open Toggle_votes_repr in - ema_of_int64 0L >>=? fun initial_ema -> - Assert.leq_int64 + ema_of_int32 0l >>=? fun initial_ema -> + Assert.leq_int32 ~loc:__LOC__ (compute_new_ema_n (Stdlib.List.init 1385 (fun _ -> Toggle_vote_off)) initial_ema) - 1_000_000_000L + 1_000_000_000l >>=? fun () -> - Assert.leq_int64 + Assert.leq_int32 ~loc:__LOC__ - 1_000_000_000L + 1_000_000_000l (compute_new_ema_n (Stdlib.List.init 1386 (fun _ -> Toggle_vote_off)) initial_ema) @@ -190,20 +190,20 @@ let test_ema_many_off () = (* Test that 1385 On votes are needed to reach the threshold from the max value of the EMA (2,000,000,000). *) let test_ema_many_on () = let open Toggle_votes_repr in - ema_of_int64 2_000_000_000L >>=? fun initial_ema -> - Assert.leq_int64 + ema_of_int32 2_000_000_000l >>=? fun initial_ema -> + Assert.leq_int32 ~loc:__LOC__ - 1_000_000_000L + 1_000_000_000l (compute_new_ema_n (Stdlib.List.init 1385 (fun _ -> Toggle_vote_on)) initial_ema) >>=? fun () -> - Assert.leq_int64 + Assert.leq_int32 ~loc:__LOC__ (compute_new_ema_n (Stdlib.List.init 1386 (fun _ -> Toggle_vote_on)) initial_ema) - 1_000_000_000L + 1_000_000_000l (* Test that the EMA update function is symmetric: from two dual values of the EMA (that is, two values x and y such that @@ -213,17 +213,17 @@ let test_ema_many_on () = let test_ema_symmetry () = List.iter_es (fun ema -> - let opposite_ema = Int64.(sub 2_000_000_000L ema) in - ema_of_int64 ema >>=? fun ema -> - ema_of_int64 opposite_ema >>=? fun opposite_ema -> + let opposite_ema = Int32.(sub 2_000_000_000l ema) in + ema_of_int32 ema >>=? fun ema -> + ema_of_int32 opposite_ema >>=? fun opposite_ema -> let new_ema = compute_new_ema ~toggle_vote:Toggle_vote_on ema in let new_opposite_ema = compute_new_ema ~toggle_vote:Toggle_vote_off opposite_ema in - Assert.equal_int64 + Assert.equal_int32 ~loc:__LOC__ - Int64.(add new_ema new_opposite_ema) - 2_000_000_000L) + Int32.(add new_ema new_opposite_ema) + 2_000_000_000l) ema_range let tests = diff --git a/src/proto_alpha/lib_protocol/toggle_EMA.ml b/src/proto_alpha/lib_protocol/toggle_EMA.ml index 99f0b01b4d02701a34af7af984c8e8b0dcbb9cde..7fa796d4234026afcee7aa723fae33c21193d2c1 100644 --- a/src/proto_alpha/lib_protocol/toggle_EMA.ml +++ b/src/proto_alpha/lib_protocol/toggle_EMA.ml @@ -36,7 +36,7 @@ We use exponential moving averages (EMA for short) because they can easily and efficiently be implemented because a single value needs to be stored in the context for each average. Each EMA is updated once per - block and stored in the context. It is represented using a 64-bit + block and stored in the context. It is represented using a 32-bit signed integer but it can only take non-negative values in a range of the form 0...ema_max where the constant ema_max, the maximum value of the EMA, is a parameter of this module. To update an EMA, @@ -55,7 +55,7 @@ module type EMA_PARAMETERS = sig val baker_contribution : Z.t - val ema_max : Int64.t + val ema_max : Int32.t (* We don't need to parameterize by the attenuation factor because it can be computed from the two other parameters with the @@ -68,15 +68,15 @@ end module type T = sig type t - val of_int64 : Int64.t -> t tzresult Lwt.t + val of_int32 : Int32.t -> t tzresult Lwt.t val zero : t - val to_int64 : t -> Int64.t + val to_int32 : t -> Int32.t val encoding : t Data_encoding.t - val ( < ) : t -> Int64.t -> bool + val ( < ) : t -> Int32.t -> bool val update_ema_up : t -> t @@ -84,29 +84,29 @@ module type T = sig end module Make (EMA_parameters : EMA_PARAMETERS) : T = struct - type t = Int64.t - (* Invariant 0L <= ema <= EMA_Parameters.ema_max *) + type t = Int32.t + (* Invariant 0l <= ema <= EMA_Parameters.ema_max *) (* This error is not registered because we don't expect it to be raised. *) - type error += Toggle_ema_out_of_bound of Int64.t + type error += Toggle_ema_out_of_bound of Int32.t - let check_bounds x = Compare.Int64.(0L <= x && x <= EMA_parameters.ema_max) + let check_bounds x = Compare.Int32.(0l <= x && x <= EMA_parameters.ema_max) - let of_int64 (x : Int64.t) : t tzresult Lwt.t = + let of_int32 (x : Int32.t) : t tzresult Lwt.t = if check_bounds x then return x else tzfail @@ Toggle_ema_out_of_bound x - let zero : t = Int64.zero + let zero : t = Int32.zero (* The conv_with_guard combinator of Data_encoding expects a (_, string) result. *) - let of_int64_for_encoding x = + let of_int32_for_encoding x = if check_bounds x then Ok x else Error "out of bounds" - let to_int64 (ema : t) : Int64.t = ema + let to_int32 (ema : t) : Int32.t = ema (* We perform the computations in Z to avoid overflows. *) - let ema_max_z = Z.of_int64 EMA_parameters.ema_max + let ema_max_z = Z.of_int32 EMA_parameters.ema_max let attenuation_numerator = Z.(sub ema_max_z (mul (of_int 2) EMA_parameters.baker_contribution)) @@ -118,7 +118,7 @@ module Make (EMA_parameters : EMA_PARAMETERS) : T = struct let half_ema_max_z = Z.(div ema_max_z (of_int 2)) - (* Outside of this module, the EMA is always between 0L and ema_max. + (* Outside of this module, the EMA is always between 0l and ema_max. This [recenter] wrappers, puts it in between -ema_max/2 and ema_max/2. The goal of this recentering around zero is to make [update_ema_off] and [update_ema_on] behave symmetrically with @@ -126,21 +126,21 @@ module Make (EMA_parameters : EMA_PARAMETERS) : T = struct let recenter f ema = Z.(add half_ema_max_z (f (sub ema half_ema_max_z))) let update_ema_up (ema : t) : t = - let ema = Z.of_int64 ema in + let ema = Z.of_int32 ema in recenter (fun ema -> Z.add (attenuate ema) EMA_parameters.baker_contribution) ema - |> Z.to_int64 + |> Z.to_int32 let update_ema_down (ema : t) : t = - let ema = Z.of_int64 ema in + let ema = Z.of_int32 ema in recenter (fun ema -> Z.sub (attenuate ema) EMA_parameters.baker_contribution) ema - |> Z.to_int64 + |> Z.to_int32 - let ( < ) : t -> Int64.t -> bool = Compare.Int64.( < ) + let ( < ) : t -> Int32.t -> bool = Compare.Int32.( < ) let encoding : t Data_encoding.t = - Data_encoding.(conv_with_guard to_int64 of_int64_for_encoding int64) + Data_encoding.(conv_with_guard to_int32 of_int32_for_encoding int32) end diff --git a/src/proto_alpha/lib_protocol/toggle_EMA.mli b/src/proto_alpha/lib_protocol/toggle_EMA.mli index 86c39893ed32fb20437a166cb94fd80cd3a1e744..bca19dbb44eb9e7e2cd584cc9d75976d62abca2a 100644 --- a/src/proto_alpha/lib_protocol/toggle_EMA.mli +++ b/src/proto_alpha/lib_protocol/toggle_EMA.mli @@ -32,21 +32,21 @@ module type EMA_PARAMETERS = sig (* This is the maximum value that the EMA can take. The minimal value cannot be set, it is 0L. *) - val ema_max : Int64.t + val ema_max : Int32.t end module type T = sig type t - val of_int64 : Int64.t -> t tzresult Lwt.t + val of_int32 : Int32.t -> t tzresult Lwt.t val zero : t - val to_int64 : t -> Int64.t + val to_int32 : t -> Int32.t val encoding : t Data_encoding.t - val ( < ) : t -> Int64.t -> bool + val ( < ) : t -> Int32.t -> bool val update_ema_up : t -> t diff --git a/src/proto_alpha/lib_protocol/toggle_votes_repr.ml b/src/proto_alpha/lib_protocol/toggle_votes_repr.ml index 5a3c9f29ab46d59af59ad769a6c3714b4a27f45e..38909c6c4017349af1d4dcebc9ce49f9b56ae1c3 100644 --- a/src/proto_alpha/lib_protocol/toggle_votes_repr.ml +++ b/src/proto_alpha/lib_protocol/toggle_votes_repr.ml @@ -90,13 +90,13 @@ let toggle_votes_encoding = module Liquidity_baking_toggle_EMA = Toggle_EMA.Make (struct let baker_contribution = Z.of_int 500_000 - let ema_max = 2_000_000_000L + let ema_max = 2_000_000_000l end) module Adaptive_inflation_launch_EMA = Toggle_EMA.Make (struct let baker_contribution = Z.of_int 500_000 - let ema_max = 2_000_000_000L + let ema_max = 2_000_000_000l end) let compute_new_liquidity_baking_ema ~toggle_vote ema = diff --git a/tezt/tests/expected/RPC_test.ml/Alpha- (mode client) RPC regression tests- misc_protocol.out b/tezt/tests/expected/RPC_test.ml/Alpha- (mode client) RPC regression tests- misc_protocol.out index f871f256776ce35d7ec367112dc7d0567c2532cc..2b049572078fce137d286aa3362ac2010ebcbace 100644 --- a/tezt/tests/expected/RPC_test.ml/Alpha- (mode client) RPC regression tests- misc_protocol.out +++ b/tezt/tests/expected/RPC_test.ml/Alpha- (mode client) RPC regression tests- misc_protocol.out @@ -24,7 +24,7 @@ "seed_nonce_revelation_tip_weight": 1, "vdf_revelation_tip_weight": 1 }, "cost_per_byte": "250", "hard_storage_limit_per_operation": "60000", "quorum_min": 2000, "quorum_max": 7000, "min_proposal_quorum": 500, - "liquidity_baking_toggle_ema_threshold": "1000000000", + "liquidity_baking_toggle_ema_threshold": 1000000000, "max_operations_time_to_live": 240, "minimal_block_delay": "1", "delay_increment_per_round": "1", "consensus_committee_size": 256, "consensus_threshold": 0, @@ -67,7 +67,7 @@ "zk_rollup_origination_size": 4000, "zk_rollup_min_pending_to_process": 10, "adaptive_inflation_enable": false, "staking_over_baking_limit": 5, "max_costaking_baker_count": 5, "staking_over_delegation_edge": 2, - "adaptive_inflation_launch_ema_threshold": "1600000000" } + "adaptive_inflation_launch_ema_threshold": 1600000000 } ./octez-client rpc get /chains/main/blocks/head/helpers/baking_rights [ { "level": 2, "delegate": "[PUBLIC_KEY_HASH]", diff --git a/tezt/tests/expected/RPC_test.ml/Alpha- (mode light) RPC regression tests- misc_protocol.out b/tezt/tests/expected/RPC_test.ml/Alpha- (mode light) RPC regression tests- misc_protocol.out index e78ae19711bc121241ac7d6dbf819fd3fc752d26..777c90d878b45989b91820eb30b53e4bea86dd53 100644 --- a/tezt/tests/expected/RPC_test.ml/Alpha- (mode light) RPC regression tests- misc_protocol.out +++ b/tezt/tests/expected/RPC_test.ml/Alpha- (mode light) RPC regression tests- misc_protocol.out @@ -24,7 +24,7 @@ "seed_nonce_revelation_tip_weight": 1, "vdf_revelation_tip_weight": 1 }, "cost_per_byte": "250", "hard_storage_limit_per_operation": "60000", "quorum_min": 2000, "quorum_max": 7000, "min_proposal_quorum": 500, - "liquidity_baking_toggle_ema_threshold": "1000000000", + "liquidity_baking_toggle_ema_threshold": 1000000000, "max_operations_time_to_live": 240, "minimal_block_delay": "1", "delay_increment_per_round": "1", "consensus_committee_size": 256, "consensus_threshold": 0, @@ -67,7 +67,7 @@ "zk_rollup_origination_size": 4000, "zk_rollup_min_pending_to_process": 10, "adaptive_inflation_enable": false, "staking_over_baking_limit": 5, "max_costaking_baker_count": 5, "staking_over_delegation_edge": 2, - "adaptive_inflation_launch_ema_threshold": "1600000000" } + "adaptive_inflation_launch_ema_threshold": 1600000000 } ./octez-client --mode light rpc get /chains/main/blocks/head/helpers/baking_rights [ { "level": 2, "delegate": "[PUBLIC_KEY_HASH]", diff --git a/tezt/tests/expected/RPC_test.ml/Alpha- (mode proxy) RPC regression tests- misc_protocol.out b/tezt/tests/expected/RPC_test.ml/Alpha- (mode proxy) RPC regression tests- misc_protocol.out index 0fc0ccbd2b9d48437e41bbc1c1e3dd139608f0df..74efc36dd422ee68b8f822bbaca9e86da54f4cff 100644 --- a/tezt/tests/expected/RPC_test.ml/Alpha- (mode proxy) RPC regression tests- misc_protocol.out +++ b/tezt/tests/expected/RPC_test.ml/Alpha- (mode proxy) RPC regression tests- misc_protocol.out @@ -24,7 +24,7 @@ "seed_nonce_revelation_tip_weight": 1, "vdf_revelation_tip_weight": 1 }, "cost_per_byte": "250", "hard_storage_limit_per_operation": "60000", "quorum_min": 2000, "quorum_max": 7000, "min_proposal_quorum": 500, - "liquidity_baking_toggle_ema_threshold": "1000000000", + "liquidity_baking_toggle_ema_threshold": 1000000000, "max_operations_time_to_live": 240, "minimal_block_delay": "1", "delay_increment_per_round": "1", "consensus_committee_size": 256, "consensus_threshold": 0, @@ -67,7 +67,7 @@ "zk_rollup_origination_size": 4000, "zk_rollup_min_pending_to_process": 10, "adaptive_inflation_enable": false, "staking_over_baking_limit": 5, "max_costaking_baker_count": 5, "staking_over_delegation_edge": 2, - "adaptive_inflation_launch_ema_threshold": "1600000000" } + "adaptive_inflation_launch_ema_threshold": 1600000000 } ./octez-client --mode proxy rpc get /chains/main/blocks/head/helpers/baking_rights [ { "level": 2, "delegate": "[PUBLIC_KEY_HASH]", diff --git a/tezt/tests/expected/RPC_test.ml/Alpha- (mode proxy_server_data_dir) RPC regression tests- misc_protocol.out b/tezt/tests/expected/RPC_test.ml/Alpha- (mode proxy_server_data_dir) RPC regression tests- misc_protocol.out index ce9a02becea58f53aeab369d2d1cca5dcfb63d45..dc485a1d4b3a3db51d8af23449e3d133926e23c4 100644 --- a/tezt/tests/expected/RPC_test.ml/Alpha- (mode proxy_server_data_dir) RPC regression tests- misc_protocol.out +++ b/tezt/tests/expected/RPC_test.ml/Alpha- (mode proxy_server_data_dir) RPC regression tests- misc_protocol.out @@ -24,7 +24,7 @@ "seed_nonce_revelation_tip_weight": 1, "vdf_revelation_tip_weight": 1 }, "cost_per_byte": "250", "hard_storage_limit_per_operation": "60000", "quorum_min": 2000, "quorum_max": 7000, "min_proposal_quorum": 500, - "liquidity_baking_toggle_ema_threshold": "1000000000", + "liquidity_baking_toggle_ema_threshold": 1000000000, "max_operations_time_to_live": 240, "minimal_block_delay": "1", "delay_increment_per_round": "1", "consensus_committee_size": 256, "consensus_threshold": 0, @@ -67,7 +67,7 @@ "zk_rollup_origination_size": 4000, "zk_rollup_min_pending_to_process": 10, "adaptive_inflation_enable": false, "staking_over_baking_limit": 5, "max_costaking_baker_count": 5, "staking_over_delegation_edge": 2, - "adaptive_inflation_launch_ema_threshold": "1600000000" } + "adaptive_inflation_launch_ema_threshold": 1600000000 } ./octez-client rpc get /chains/main/blocks/head/helpers/baking_rights [ { "level": 3, "delegate": "[PUBLIC_KEY_HASH]", diff --git a/tezt/tests/expected/RPC_test.ml/Alpha- (mode proxy_server_rpc) RPC regression tests- misc_protocol.out b/tezt/tests/expected/RPC_test.ml/Alpha- (mode proxy_server_rpc) RPC regression tests- misc_protocol.out index ce9a02becea58f53aeab369d2d1cca5dcfb63d45..dc485a1d4b3a3db51d8af23449e3d133926e23c4 100644 --- a/tezt/tests/expected/RPC_test.ml/Alpha- (mode proxy_server_rpc) RPC regression tests- misc_protocol.out +++ b/tezt/tests/expected/RPC_test.ml/Alpha- (mode proxy_server_rpc) RPC regression tests- misc_protocol.out @@ -24,7 +24,7 @@ "seed_nonce_revelation_tip_weight": 1, "vdf_revelation_tip_weight": 1 }, "cost_per_byte": "250", "hard_storage_limit_per_operation": "60000", "quorum_min": 2000, "quorum_max": 7000, "min_proposal_quorum": 500, - "liquidity_baking_toggle_ema_threshold": "1000000000", + "liquidity_baking_toggle_ema_threshold": 1000000000, "max_operations_time_to_live": 240, "minimal_block_delay": "1", "delay_increment_per_round": "1", "consensus_committee_size": 256, "consensus_threshold": 0, @@ -67,7 +67,7 @@ "zk_rollup_origination_size": 4000, "zk_rollup_min_pending_to_process": 10, "adaptive_inflation_enable": false, "staking_over_baking_limit": 5, "max_costaking_baker_count": 5, "staking_over_delegation_edge": 2, - "adaptive_inflation_launch_ema_threshold": "1600000000" } + "adaptive_inflation_launch_ema_threshold": 1600000000 } ./octez-client rpc get /chains/main/blocks/head/helpers/baking_rights [ { "level": 3, "delegate": "[PUBLIC_KEY_HASH]",