diff --git a/manifest/main.ml b/manifest/main.ml index ad3e4357315d45612113ca70d89be90223259b37..cb4ef1445825ae7dc8b1881ab7ba9377a7908ded 100644 --- a/manifest/main.ml +++ b/manifest/main.ml @@ -4761,6 +4761,7 @@ end = struct [ ("test_constants", true); ("test_frozen_bonds", true); + ("test_adaptive_inflation_launch", N.(number >= 018)); ("test_liquidity_baking", true); ("test_storage_functions", true); ("test_storage", true); diff --git a/src/proto_alpha/lib_protocol/test/helpers/block.ml b/src/proto_alpha/lib_protocol/test/helpers/block.ml index d2097b98f182869ee0c35ff7f2258d1615455844..ad526565c288d5d49a42843c50946e4bad192683 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/block.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/block.ml @@ -832,6 +832,25 @@ let bake_with_metadata ?locked_round ?policy ?timestamp ?operation ?operations ?operations pred +let bake_n_with_metadata ?locked_round ?policy ?timestamp ?payload_round + ?check_size ?(baking_mode = Application) ?(allow_manager_failures = false) + ?liquidity_baking_toggle_vote ?adaptive_inflation_vote n pred = + let get_next b = + bake_with_metadata + ?locked_round + ?policy + ?timestamp + ?payload_round + ?check_size + ~baking_mode + ~allow_manager_failures + ?liquidity_baking_toggle_vote + ?adaptive_inflation_vote + b + in + get_next pred >>=? fun b -> + List.fold_left_es (fun (b, _metadata) _ -> get_next b) b (2 -- n) + let bake ?(baking_mode = Application) ?(allow_manager_failures = false) ?payload_round ?locked_round ?policy ?timestamp ?operation ?operations ?liquidity_baking_toggle_vote ?adaptive_inflation_vote ?check_size pred = @@ -868,19 +887,19 @@ let bake_n ?(baking_mode = Application) ?policy ?liquidity_baking_toggle_vote b (1 -- n) -let rec bake_while ?(baking_mode = Application) ?policy +let rec bake_while_with_metadata ?(baking_mode = Application) ?policy ?liquidity_baking_toggle_vote ?adaptive_inflation_vote predicate b = let open Lwt_result_syntax in - let* new_block = - bake + let* new_block, metadata = + bake_with_metadata ~baking_mode ?policy ?liquidity_baking_toggle_vote ?adaptive_inflation_vote b in - if predicate new_block then - (bake_while [@ocaml.tailcall]) + if predicate new_block metadata then + (bake_while_with_metadata [@ocaml.tailcall]) ~baking_mode ?policy ?liquidity_baking_toggle_vote @@ -889,6 +908,16 @@ let rec bake_while ?(baking_mode = Application) ?policy new_block else return b +let bake_while ?baking_mode ?policy ?liquidity_baking_toggle_vote + ?adaptive_inflation_vote predicate b = + bake_while_with_metadata + ?baking_mode + ?policy + ?liquidity_baking_toggle_vote + ?adaptive_inflation_vote + (fun block _metadata -> predicate block) + b + let bake_until_level ?(baking_mode = Application) ?policy ?liquidity_baking_toggle_vote ?adaptive_inflation_vote level b = bake_while diff --git a/src/proto_alpha/lib_protocol/test/helpers/block.mli b/src/proto_alpha/lib_protocol/test/helpers/block.mli index 672d749abda3d5f139428b2f6df4a58f7902915c..7eca613dbba5808b61fe495414a385a8c19fdaf4 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/block.mli +++ b/src/proto_alpha/lib_protocol/test/helpers/block.mli @@ -271,6 +271,35 @@ val bake_n_with_liquidity_baking_toggle_ema : (block * Alpha_context.Toggle_votes.Liquidity_baking_toggle_EMA.t) tzresult Lwt.t +(** Variant of [bake_n] that returns the block metadata of the last + baked block. [n] must be positive, otherwise a single block is baked. **) +val bake_n_with_metadata : + ?locked_round:Round.t option -> + ?policy:baker_policy -> + ?timestamp:Timestamp.time -> + ?payload_round:Round.t option -> + ?check_size:bool -> + ?baking_mode:baking_mode -> + ?allow_manager_failures:bool -> + ?liquidity_baking_toggle_vote:Toggle_votes_repr.toggle_vote -> + ?adaptive_inflation_vote:Toggle_votes_repr.toggle_vote -> + int -> + block -> + (block * block_header_metadata, Error_monad.tztrace) result Lwt.t + +(* Bake blocks while a predicate over the block and its metadata + holds. The returned block is the last one for which the predicate + holds; in case the predicate never holds, the input block is + returned. *) +val bake_while_with_metadata : + ?baking_mode:baking_mode -> + ?policy:baker_policy -> + ?liquidity_baking_toggle_vote:Toggle_votes_repr.toggle_vote -> + ?adaptive_inflation_vote:Toggle_votes_repr.toggle_vote -> + (block -> block_header_metadata -> bool) -> + block -> + block tzresult Lwt.t + val current_cycle : t -> Cycle.t tzresult Lwt.t (** Given a block [b] at level [l] bakes enough blocks to complete a cycle, diff --git a/src/proto_alpha/lib_protocol/test/integration/dune b/src/proto_alpha/lib_protocol/test/integration/dune index 3354358ec99d6b22ae050c524c44a3e9cee51628..6ff170d1b60dd9ad92199199f73a457801d076ca 100644 --- a/src/proto_alpha/lib_protocol/test/integration/dune +++ b/src/proto_alpha/lib_protocol/test/integration/dune @@ -31,6 +31,7 @@ (modules test_constants test_frozen_bonds + test_adaptive_inflation_launch test_liquidity_baking test_storage_functions test_storage diff --git a/src/proto_alpha/lib_protocol/test/integration/test_adaptive_inflation_launch.ml b/src/proto_alpha/lib_protocol/test/integration/test_adaptive_inflation_launch.ml new file mode 100644 index 0000000000000000000000000000000000000000..2b08db272b871db46fbbc3390d2e5b1a1404424b --- /dev/null +++ b/src/proto_alpha/lib_protocol/test/integration/test_adaptive_inflation_launch.ml @@ -0,0 +1,111 @@ +(*****************************************************************************) +(* *) +(* Open Source License *) +(* Copyright (c) 2023 Nomadic Labs *) +(* *) +(* Permission is hereby granted, free of charge, to any person obtaining a *) +(* copy of this software and associated documentation files (the "Software"),*) +(* to deal in the Software without restriction, including without limitation *) +(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *) +(* and/or sell copies of the Software, and to permit persons to whom the *) +(* Software is furnished to do so, subject to the following conditions: *) +(* *) +(* The above copyright notice and this permission notice shall be included *) +(* in all copies or substantial portions of the Software. *) +(* *) +(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*) +(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *) +(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *) +(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*) +(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *) +(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *) +(* DEALINGS IN THE SOFTWARE. *) +(* *) +(*****************************************************************************) + +(** Testing + ------- + Component: Adaptive Inflation, launch vote + Invocation: dune exec src/proto_alpha/lib_protocol/test/integration/main.exe \ + -- --file test_adaptive_inflation_launch.ml + Subject: Test the launch vote feature of Adaptive Inflation. +*) + +let assert_level ~loc (blk : Block.t) expected = + let current_level = blk.header.shell.level in + Assert.equal_int32 ~loc current_level expected + +(* Test that the EMA of the adaptive inflation vote reaches the + threshold after the expected duration. *) +let test_ema_reaches_threshold threshold expected_vote_duration () = + let open Lwt_result_syntax in + let assert_ema_above_threshold ~loc + (metadata : Protocol.Main.block_header_metadata) = + let ema = + Protocol.Alpha_context.Toggle_votes.Adaptive_inflation_launch_EMA.to_int32 + metadata.adaptive_inflation_toggle_ema + in + Assert.lt_int32 ~loc threshold ema + in + let* block, _contract = + let default_constants = Default_parameters.constants_test in + let adaptive_inflation = + { + default_constants.adaptive_inflation with + launch_ema_threshold = threshold; + } + in + let consensus_threshold = 0 in + Context.init_with_constants1 + {default_constants with consensus_threshold; adaptive_inflation} + in + let* block = + Block.bake_while_with_metadata + ~adaptive_inflation_vote:Toggle_vote_on + (fun _block metadata -> + let ema = + Protocol.Alpha_context.Toggle_votes.Adaptive_inflation_launch_EMA + .to_int32 + metadata.adaptive_inflation_toggle_ema + in + Compare.Int32.(ema < threshold)) + block + in + let* () = + assert_level ~loc:__LOC__ block (Int32.pred expected_vote_duration) + in + let* block, metadata = + Block.bake_n_with_metadata ~adaptive_inflation_vote:Toggle_vote_on 1 block + in + let* () = assert_ema_above_threshold ~loc:__LOC__ metadata in + let* () = assert_level ~loc:__LOC__ block expected_vote_duration in + return_unit + +let tests = + [ + Tztest.tztest + "the EMA reaches the vote threshold at the expected level (very low \ + threshold)" + `Quick + (test_ema_reaches_threshold + 1000000l (* This means that the threshold is set at 0.05% *) + 59l); + Tztest.tztest + "the EMA reaches the vote threshold at the expected level (realistic \ + threshold)" + `Slow + (test_ema_reaches_threshold + Default_parameters.constants_test.adaptive_inflation + .launch_ema_threshold + 187259l + (* This vote duration is consistent with the result of the + unit test for this EMA in + ../unit/test_adaptive_inflation_ema.ml*)); + ] + +let () = + Alcotest_lwt.run + ~__FILE__ + Protocol.name + [("adaptive inflation launch", tests)] + |> Lwt_main.run