diff --git a/docs/protocols/alpha.rst b/docs/protocols/alpha.rst index a3ac166308d42747d314754fc03b005f0987328a..1ca7866abd68facdf966676e734a259737cc68a2 100644 --- a/docs/protocols/alpha.rst +++ b/docs/protocols/alpha.rst @@ -91,6 +91,17 @@ 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 084af2915bbee3d5579e5180a191e6e5d221a112..e356a0ea0fa2b6cd4efd0c3ee6c308fd3ed0e9f7 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: diff --git a/src/proto_alpha/lib_protocol/alpha_context.mli b/src/proto_alpha/lib_protocol/alpha_context.mli index ed61572120d77f66abae2511c41823f271f03bd2..4bfabb1194e9d88cb2b0b89fa34bd0352e17b325 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/alpha_context.mli @@ -875,7 +875,7 @@ module Constants : sig quorum_min : int32; quorum_max : int32; min_proposal_quorum : int32; - liquidity_baking_toggle_ema_threshold : int32; + liquidity_baking_toggle_ema_threshold : int64; max_operations_time_to_live : int; minimal_block_delay : Period.t; delay_increment_per_round : Period.t; @@ -952,7 +952,7 @@ module Constants : sig val min_proposal_quorum : context -> int32 - val liquidity_baking_toggle_ema_threshold : context -> int32 + val liquidity_baking_toggle_ema_threshold : context -> int64 val minimal_block_delay : context -> Period.t @@ -4692,7 +4692,7 @@ module Toggle_EMA : sig val zero : t - val to_int32 : t -> Int32.t + val to_int64 : t -> Int64.t val encoding : t Data_encoding.t end diff --git a/src/proto_alpha/lib_protocol/constants_parametric_repr.ml b/src/proto_alpha/lib_protocol/constants_parametric_repr.ml index 9ae4402ed0708807d8a81d200a68066e656bddd8..0f8434b425268fa325bdfe8899dd8c791c3ff186 100644 --- a/src/proto_alpha/lib_protocol/constants_parametric_repr.ml +++ b/src/proto_alpha/lib_protocol/constants_parametric_repr.ml @@ -158,7 +158,7 @@ type t = { quorum_min : int32; quorum_max : int32; min_proposal_quorum : int32; - liquidity_baking_toggle_ema_threshold : int32; + liquidity_baking_toggle_ema_threshold : int64; max_operations_time_to_live : int; minimal_block_delay : Period_repr.t; delay_increment_per_round : Period_repr.t; @@ -545,7 +545,7 @@ let encoding = (obj8 (req "quorum_max" int32) (req "min_proposal_quorum" int32) - (req "liquidity_baking_toggle_ema_threshold" int32) + (req "liquidity_baking_toggle_ema_threshold" int64) (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 f90cd43a6988800d2addcaee705b2253b36b920d..f9c9c40b7e08822abb645190720b7dcc4adc113c 100644 --- a/src/proto_alpha/lib_protocol/constants_parametric_repr.mli +++ b/src/proto_alpha/lib_protocol/constants_parametric_repr.mli @@ -183,7 +183,7 @@ type t = { (* in centile of a percentage *) quorum_max : int32; min_proposal_quorum : int32; - liquidity_baking_toggle_ema_threshold : int32; + liquidity_baking_toggle_ema_threshold : int64; 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 43093986d2402ddc608d8fbceb7e7aa705c4ee63..d4820eed64d8a48d97bad839589cd5e42bd005da 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 -> int32 +val liquidity_baking_toggle_ema_threshold : Raw_context.t -> int64 val parametric : Raw_context.t -> Constants_parametric_repr.t diff --git a/src/proto_alpha/lib_protocol/init_storage.ml b/src/proto_alpha/lib_protocol/init_storage.ml index 6ece1c93ed0be58a54de80087dc96a1a4a9828d5..f9ce84c704c8fc73946c867ab133409d68d0719f 100644 --- a/src/proto_alpha/lib_protocol/init_storage.ml +++ b/src/proto_alpha/lib_protocol/init_storage.ml @@ -151,6 +151,44 @@ let initialize_total_supply_for_o ctxt = ctxt (Tez_repr.of_mutez_exn 940_000_000_000_000L) +(** 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 @@ -232,7 +270,8 @@ let prepare_first_block _chain_id ctxt ~typecheck_smart_contract migrate_stake_distribution_for_o ctxt >>=? fun ctxt -> initialize_total_supply_for_o ctxt >>= fun ctxt -> Remove_zero_amount_ticket_migration_for_o.remove_zero_ticket_entries ctxt - >>= fun ctxt -> return (ctxt, [])) + >>= fun ctxt -> + migrate_liquidity_baking_ema ctxt >>=? fun ctxt -> return (ctxt, [])) >>=? fun (ctxt, balance_updates) -> List.fold_left_es patch_script ctxt Legacy_script_patches.addresses_to_patch >>=? fun ctxt -> diff --git a/src/proto_alpha/lib_protocol/liquidity_baking_migration.ml b/src/proto_alpha/lib_protocol/liquidity_baking_migration.ml index 59a8eb2c654908aa7ef58e382f4b65080f1fb10c..bdce19c0b1c9a79727cf666e80c6261a1c6a8bb0 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 7dd169ceb5c7188002ffa020a74305d3a02b70b5..2c416fd82f8b4728a988dd9145fc817bbe896f88 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 -> - Toggle_EMA.of_int32 ema + Toggle_EMA.of_int64 ema let on_cpmm_exists ctxt f = get_cpmm_address ctxt >>=? fun cpmm_contract -> @@ -44,7 +44,7 @@ let on_cpmm_exists ctxt f = let update_toggle_ema ctxt ~toggle_vote = get_toggle_ema ctxt >>=? fun old_ema -> let new_ema = compute_new_ema ~toggle_vote old_ema in - Storage.Liquidity_baking.Toggle_ema.update ctxt (Toggle_EMA.to_int32 new_ema) + Storage.Liquidity_baking.Toggle_ema.update ctxt (Toggle_EMA.to_int64 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 8833613bf244d1da21b4fb047eef267abf502e6e..fcec1fb08537c40bbe67918d75832eb565da3e51 100644 --- a/src/proto_alpha/lib_protocol/raw_context.ml +++ b/src/proto_alpha/lib_protocol/raw_context.ml @@ -1052,7 +1052,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 = - c.liquidity_baking_toggle_ema_threshold; + Int64.of_int32 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 9e8e5acdbd831d5e27e54e19bf5000dcd9d459a6..0e927688dba14e53dd91d719e6414802b1159a16 100644 --- a/src/proto_alpha/lib_protocol/storage.ml +++ b/src/proto_alpha/lib_protocol/storage.ml @@ -1599,10 +1599,9 @@ module Liquidity_baking = struct module Toggle_ema = Make_single_data_storage (Registered) (Raw_context) (struct - (* The old "escape" name is kept here to avoid migrating this. *) - let name = ["liquidity_baking_escape_ema"] + let name = ["liquidity_baking_toggle_ema"] end) - (Encoding.Int32) + (Encoding.Int64) module Cpmm_address = 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 79e21499d427b545ed216e60ce0d3609f5b0ab33..d050121895fde2a59206d139a1845cb212cf000a 100644 --- a/src/proto_alpha/lib_protocol/storage.mli +++ b/src/proto_alpha/lib_protocol/storage.mli @@ -693,7 +693,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 = Int32.t + Single_data_storage with type t := Raw_context.t and type value = Int64.t (** Constant product market maker contract that receives liquidity baking subsidy. **) module Cpmm_address : diff --git a/src/proto_alpha/lib_protocol/test/helpers/assert.ml b/src/proto_alpha/lib_protocol/test/helpers/assert.ml index 4e491c3b294741829e0c40583ddcc4c50954858c..d2da6c5c26b7803aca1814b0306dde2badec69bd 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/assert.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/assert.ml @@ -136,6 +136,9 @@ 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 9a63b0b546d329fe7b2374cb93acbdefeb028258..1c51c7e0c3257d19fb6adb3142226df8adef274b 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 @@ -257,7 +257,7 @@ let liquidity_baking_toggle_ema n_vote_on n_vote_off level bake_after >>=? fun (_blk, toggle_ema) -> Assert.leq_int ~loc:__LOC__ - (toggle_ema |> Alpha_context.Toggle_EMA.to_int32 |> Int32.to_int) + (toggle_ema |> Alpha_context.Toggle_EMA.to_int64 |> Int64.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 11bd5db88e18de3f6c2b9800d78bfbd97c0264de..e3eee00daa1a2bb2fd724c789f72f00920ad9620 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 @@ -33,12 +33,12 @@ open Protocol -let ema_of_int32 ema = Toggle_EMA.of_int32 ema >|= Environment.wrap_tzresult +let ema_of_int64 ema = Toggle_EMA.of_int64 ema >|= Environment.wrap_tzresult -let ema_to_int32 = Toggle_EMA.to_int32 +let ema_to_int64 = Toggle_EMA.to_int64 let compute_new_ema ~toggle_vote ema = - Toggle_votes_repr.compute_new_ema ~toggle_vote ema |> ema_to_int32 + Toggle_votes_repr.compute_new_ema ~toggle_vote ema |> ema_to_int64 (* Folds compute_new_ema on a list of votes *) let compute_new_ema_n toggle_votes initial_ema = @@ -46,55 +46,55 @@ let compute_new_ema_n toggle_votes initial_ema = (fun ema toggle_vote -> Toggle_votes_repr.compute_new_ema ~toggle_vote ema) initial_ema toggle_votes - |> ema_to_int32 + |> ema_to_int64 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_int32 old_ema >>=? fun ema -> - Assert.equal_int32 + ema_of_int64 old_ema >>=? fun ema -> + Assert.equal_int64 ~loc:__LOC__ (compute_new_ema ~toggle_vote:Toggle_vote_pass ema) old_ema) @@ -104,10 +104,10 @@ let test_ema_pass () = let test_ema_in_bound_off () = List.iter_es (fun old_ema -> - ema_of_int32 old_ema >>=? fun ema -> + ema_of_int64 old_ema >>=? fun ema -> let new_ema = compute_new_ema ~toggle_vote:Toggle_vote_off ema in - Assert.leq_int32 ~loc:__LOC__ 0l new_ema >>=? fun () -> - Assert.leq_int32 ~loc:__LOC__ new_ema 2_000_000_000l) + Assert.leq_int64 ~loc:__LOC__ 0L new_ema >>=? fun () -> + Assert.leq_int64 ~loc:__LOC__ new_ema 2_000_000_000L) ema_range (* Test that new_ema > old_ema when voting Off, except if old_ema is @@ -115,32 +115,32 @@ let test_ema_in_bound_off () = let test_ema_increases_off () = List.iter_es (fun old_ema -> - ema_of_int32 old_ema >>=? fun ema -> - Assert.lt_int32 + ema_of_int64 old_ema >>=? fun ema -> + Assert.lt_int64 ~loc:__LOC__ old_ema (compute_new_ema ~toggle_vote:Toggle_vote_off ema)) - (List.filter (fun ema -> Compare.Int32.(ema < 1_999_999_000l)) ema_range) + (List.filter (fun ema -> Compare.Int64.(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_int32 old_ema >>=? fun ema -> - Assert.leq_int32 + ema_of_int64 old_ema >>=? fun ema -> + Assert.leq_int64 ~loc:__LOC__ - (Int32.sub (compute_new_ema ~toggle_vote:Toggle_vote_off ema) old_ema) - 1_000_000l) + (Int64.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_int32 old_ema >>=? fun ema -> + ema_of_int64 old_ema >>=? fun ema -> let new_ema = compute_new_ema ~toggle_vote:Toggle_vote_on ema in - Assert.leq_int32 ~loc:__LOC__ 0l new_ema >>=? fun () -> - Assert.leq_int32 ~loc:__LOC__ new_ema 2_000_000_000l) + Assert.leq_int64 ~loc:__LOC__ 0L new_ema >>=? fun () -> + Assert.leq_int64 ~loc:__LOC__ new_ema 2_000_000_000L) ema_range (* Test that new_ema < old_ema when voting On, except if old_ema is @@ -148,38 +148,38 @@ let test_ema_in_bound_on () = let test_ema_decreases_on () = List.iter_es (fun old_ema -> - ema_of_int32 old_ema >>=? fun ema -> - Assert.lt_int32 + ema_of_int64 old_ema >>=? fun ema -> + Assert.lt_int64 ~loc:__LOC__ (compute_new_ema ~toggle_vote:Toggle_vote_on ema) old_ema) - (List.filter (fun ema -> Compare.Int32.(ema > 1000l)) ema_range) + (List.filter (fun ema -> Compare.Int64.(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_int32 old_ema >>=? fun ema -> - Assert.leq_int32 + ema_of_int64 old_ema >>=? fun ema -> + Assert.leq_int64 ~loc:__LOC__ - (Int32.sub (compute_new_ema ~toggle_vote:Toggle_vote_on ema) old_ema) - 1_000_000l) + (Int64.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_int32 0l >>=? fun initial_ema -> - Assert.leq_int32 + ema_of_int64 0L >>=? fun initial_ema -> + Assert.leq_int64 ~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_int32 + Assert.leq_int64 ~loc:__LOC__ - 1_000_000_000l + 1_000_000_000L (compute_new_ema_n (Stdlib.List.init 1386 (fun _ -> Toggle_vote_off)) initial_ema) @@ -187,20 +187,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_int32 2_000_000_000l >>=? fun initial_ema -> - Assert.leq_int32 + ema_of_int64 2_000_000_000L >>=? fun initial_ema -> + Assert.leq_int64 ~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_int32 + Assert.leq_int64 ~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 @@ -210,17 +210,17 @@ let test_ema_many_on () = let test_ema_symmetry () = List.iter_es (fun 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 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 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_int32 + Assert.equal_int64 ~loc:__LOC__ - Int32.(add new_ema new_opposite_ema) - 2_000_000_000l) + Int64.(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 ab3fb3c5d0cfade3c8d3383080a719ad220e3180..86ff588368eda6fed6f61fad72418a7b379e85b7 100644 --- a/src/proto_alpha/lib_protocol/toggle_EMA.ml +++ b/src/proto_alpha/lib_protocol/toggle_EMA.ml @@ -24,29 +24,29 @@ (* *) (*****************************************************************************) -(** Exponential moving average of toggle votes. Represented as an int32 between +(** Exponential moving average of toggle votes. Represented as an int64 between 0 and 2,000,000,000. It is an exponential moving average of the "off" votes over a window of the most recent 2000 blocks that did not vote "pass". *) -(* The exponential moving average is represented as an Int32 between 0l and 2_000_000_000l *) -type t = Int32.t (* Invariant 0 <= ema <= 2_000_000_000l *) +(* The exponential moving average is represented as an Int64 between 0L and 2_000_000_000L *) +type t = Int64.t (* Invariant 0 <= ema <= 2_000_000_000L *) (* This error is not registered because we don't expect it to be raised. *) -type error += Toggle_ema_out_of_bound of Int32.t +type error += Toggle_ema_out_of_bound of Int64.t -let check_bounds x = Compare.Int32.(0l <= x && x <= 2_000_000_000l) +let check_bounds x = Compare.Int64.(0L <= x && x <= 2_000_000_000L) -let of_int32 (x : Int32.t) : t tzresult Lwt.t = +let of_int64 (x : Int64.t) : t tzresult Lwt.t = if check_bounds x then return x else tzfail @@ Toggle_ema_out_of_bound x -let zero : t = Int32.zero +let zero : t = Int64.zero (* The conv_with_guard combinator of Data_encoding expects a (_, string) result. *) -let of_int32_for_encoding x = +let of_int64_for_encoding x = if check_bounds x then Ok x else Error "out of bounds" -let to_int32 (ema : t) : Int32.t = ema +let to_int64 (ema : t) : Int64.t = ema (* We perform the computations in Z to avoid overflows. *) @@ -67,14 +67,14 @@ let recenter f ema = Z.(add z_1_000_000_000 (f (sub ema z_1_000_000_000))) let z_500_000 = Z.of_int 500_000 let update_ema_off (ema : t) : t = - let ema = Z.of_int32 ema in - recenter (fun ema -> Z.add (attenuate ema) z_500_000) ema |> Z.to_int32 + let ema = Z.of_int64 ema in + recenter (fun ema -> Z.add (attenuate ema) z_500_000) ema |> Z.to_int64 let update_ema_on (ema : t) : t = - let ema = Z.of_int32 ema in - recenter (fun ema -> Z.sub (attenuate ema) z_500_000) ema |> Z.to_int32 + let ema = Z.of_int64 ema in + recenter (fun ema -> Z.sub (attenuate ema) z_500_000) ema |> Z.to_int64 -let ( < ) : t -> Int32.t -> bool = Compare.Int32.( < ) +let ( < ) : t -> Int64.t -> bool = Compare.Int64.( < ) let encoding : t Data_encoding.t = - Data_encoding.(conv_with_guard to_int32 of_int32_for_encoding int32) + Data_encoding.(conv_with_guard to_int64 of_int64_for_encoding int64) diff --git a/src/proto_alpha/lib_protocol/toggle_EMA.mli b/src/proto_alpha/lib_protocol/toggle_EMA.mli index 0a68ba0f302a886929c8d4e2f87c69183ce0911f..bc1a56679c2c7a81ee6cc495cbf55f76b25b92c8 100644 --- a/src/proto_alpha/lib_protocol/toggle_EMA.mli +++ b/src/proto_alpha/lib_protocol/toggle_EMA.mli @@ -24,21 +24,21 @@ (* *) (*****************************************************************************) -(** Exponential moving average of toggle votes. Represented as an int32 between +(** Exponential moving average of toggle votes. Represented as an int64 between 0 and 2,000,000,000. It is an exponential moving average of the "off" votes over a window of the most recent 2000 blocks that did not vote "pass". *) type t -val of_int32 : Int32.t -> t tzresult Lwt.t +val of_int64 : Int64.t -> t tzresult Lwt.t val zero : t -val to_int32 : t -> Int32.t +val to_int64 : t -> Int64.t val encoding : t Data_encoding.t -val ( < ) : t -> Int32.t -> bool +val ( < ) : t -> Int64.t -> bool val update_ema_off : t -> t 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 ea3a7a036434cee8db0ed28646b9f0a7c9b4e0e4..2c1d8cb3775476e2d5c727cbd5df219828f7f9a0 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, 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 5c1aa74201e4e80eee0efba4bdec523b69108897..be1e5ca68614e718a75b9c205bf24fed55605b09 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, 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 9570e77408b3fb77208ca5399729257044b8d647..7c0173123e03ce2fb85be143366d85594be3abde 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, 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 288bf411f6594fcf667168dfa23c3c61db4419ea..bd5f1b32aa8904ad00e62c0ed8bf0cf1980693d7 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, 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 288bf411f6594fcf667168dfa23c3c61db4419ea..bd5f1b32aa8904ad00e62c0ed8bf0cf1980693d7 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,