From 40ac605d9d1b960be1dd749da19258c33f9b6859 Mon Sep 17 00:00:00 2001 From: Philippe Wang Date: Thu, 8 Jun 2023 15:27:07 -0700 Subject: [PATCH 1/6] Proto/Test: add Block.bake_n_with_metadata MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Raphaƫl Cauderlier --- .../lib_protocol/test/helpers/block.ml | 19 +++++++++++++++++++ .../lib_protocol/test/helpers/block.mli | 16 ++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/proto_alpha/lib_protocol/test/helpers/block.ml b/src/proto_alpha/lib_protocol/test/helpers/block.ml index d2097b98f182..d8b37d003b91 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 = diff --git a/src/proto_alpha/lib_protocol/test/helpers/block.mli b/src/proto_alpha/lib_protocol/test/helpers/block.mli index 672d749abda3..e901ce6f311c 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/block.mli +++ b/src/proto_alpha/lib_protocol/test/helpers/block.mli @@ -271,6 +271,22 @@ 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 + val current_cycle : t -> Cycle.t tzresult Lwt.t (** Given a block [b] at level [l] bakes enough blocks to complete a cycle, -- GitLab From a9a085f4a907ac2557abfcbbba8d629fcf8d9463 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Cauderlier?= Date: Thu, 15 Jun 2023 14:42:58 +0200 Subject: [PATCH 2/6] Tests/Proto/Helpers: generalize Block.bake_while with metadata This commit adds a variant of the Block.bake_while helper function whose predicate can inspect the metadata of the block. --- .../lib_protocol/test/helpers/block.ml | 20 ++++++++++++++----- .../lib_protocol/test/helpers/block.mli | 13 ++++++++++++ 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/proto_alpha/lib_protocol/test/helpers/block.ml b/src/proto_alpha/lib_protocol/test/helpers/block.ml index d8b37d003b91..ad526565c288 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/block.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/block.ml @@ -887,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 @@ -908,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 e901ce6f311c..7eca613dbba5 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/block.mli +++ b/src/proto_alpha/lib_protocol/test/helpers/block.mli @@ -287,6 +287,19 @@ val bake_n_with_metadata : 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, -- GitLab From 2255d0174859b3794f5d28f54276a727af0dc497 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Cauderlier?= Date: Thu, 8 Jun 2023 22:07:42 +0200 Subject: [PATCH 3/6] Tests/AI: integration test, AI "On" votes increase the EMA This commit introduces an integration test for the activation vote of the adaptive inflation feature. We test that, by voting "On" many times, the threshold of the vote can be reached. --- manifest/main.ml | 1 + .../lib_protocol/test/integration/dune | 1 + .../test_adaptive_inflation_launch.ml | 90 +++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 src/proto_alpha/lib_protocol/test/integration/test_adaptive_inflation_launch.ml diff --git a/manifest/main.ml b/manifest/main.ml index ad3e4357315d..cb4ef1445825 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/integration/dune b/src/proto_alpha/lib_protocol/test/integration/dune index 3354358ec99d..6ff170d1b60d 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 000000000000..b7378cfa7fea --- /dev/null +++ b/src/proto_alpha/lib_protocol/test/integration/test_adaptive_inflation_launch.ml @@ -0,0 +1,90 @@ +(*****************************************************************************) +(* *) +(* 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 + +let threshold = + Default_parameters.constants_test.adaptive_inflation.launch_ema_threshold + +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 + +(* Test that the EMA of the adaptive inflation vote reaches the + threshold after exactly 187259 blocks, which is consistent with the + result of the unit test for this EMA in + ../unit/test_adaptive_inflation_ema.ml +*) +let test_ema_reaches_threshold () = + let open Lwt_result_syntax in + let* block, _contract = Context.init1 ~consensus_threshold:0 () 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 187258l 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 187259l in + return_unit + +let tests = + [ + Tztest.tztest + "the EMA reaches the vote threshold at the expected level" + `Slow + test_ema_reaches_threshold; + ] + +let () = + Alcotest_lwt.run + ~__FILE__ + Protocol.name + [("adaptive inflation launch", tests)] + |> Lwt_main.run -- GitLab From 5bfb3963df6381e388ed2f2b0bce84e34afcb8c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Cauderlier?= Date: Fri, 16 Jun 2023 09:47:05 +0200 Subject: [PATCH 4/6] Test/Proto/AI/Vote: initialize the context with a custom threshold --- .../integration/test_adaptive_inflation_launch.ml | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) 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 index b7378cfa7fea..4f6b9e7f03f1 100644 --- 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 @@ -53,7 +53,18 @@ let assert_ema_above_threshold ~loc *) let test_ema_reaches_threshold () = let open Lwt_result_syntax in - let* block, _contract = Context.init1 ~consensus_threshold:0 () 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 -- GitLab From 0ab176c04d1d8f5c497c76c368dd250974ab83df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Cauderlier?= Date: Fri, 16 Jun 2023 09:43:48 +0200 Subject: [PATCH 5/6] Tests/Proto/AI/Vote: parametrize the test by threshold and duration --- .../test_adaptive_inflation_launch.ml | 40 ++++++++++--------- 1 file changed, 21 insertions(+), 19 deletions(-) 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 index 4f6b9e7f03f1..a64a57eefd01 100644 --- 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 @@ -35,24 +35,18 @@ let assert_level ~loc (blk : Block.t) expected = let current_level = blk.header.shell.level in Assert.equal_int32 ~loc current_level expected -let threshold = - Default_parameters.constants_test.adaptive_inflation.launch_ema_threshold - -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 - (* Test that the EMA of the adaptive inflation vote reaches the - threshold after exactly 187259 blocks, which is consistent with the - result of the unit test for this EMA in - ../unit/test_adaptive_inflation_ema.ml -*) -let test_ema_reaches_threshold () = + 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 = @@ -77,12 +71,14 @@ let test_ema_reaches_threshold () = Compare.Int32.(ema < threshold)) block in - let* () = assert_level ~loc:__LOC__ block 187258l 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 187259l in + let* () = assert_level ~loc:__LOC__ block expected_vote_duration in return_unit let tests = @@ -90,7 +86,13 @@ let tests = Tztest.tztest "the EMA reaches the vote threshold at the expected level" `Slow - test_ema_reaches_threshold; + (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 () = -- GitLab From 1ebefc05dbd946ebf9f0e967a91f2fd69099391c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Cauderlier?= Date: Fri, 16 Jun 2023 10:34:14 +0200 Subject: [PATCH 6/6] Tests/Proto/AI/Vote: add a quick test using a low threshold This is to provide a quick feedback loop. --- .../test/integration/test_adaptive_inflation_launch.ml | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) 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 index a64a57eefd01..2b08db272b87 100644 --- 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 @@ -84,7 +84,15 @@ let test_ema_reaches_threshold threshold expected_vote_duration () = let tests = [ Tztest.tztest - "the EMA reaches the vote threshold at the expected level" + "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 -- GitLab