From 0378c9a11a6906f7ccd46b499ec583d206f8dc14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Cauderlier?= Date: Mon, 19 Jun 2023 16:57:38 +0200 Subject: [PATCH 01/19] Tests/Proto/Helpers: export the total_frozen_stake RPC --- src/proto_alpha/lib_protocol/test/helpers/context.ml | 3 +++ src/proto_alpha/lib_protocol/test/helpers/context.mli | 2 ++ 2 files changed, 5 insertions(+) diff --git a/src/proto_alpha/lib_protocol/test/helpers/context.ml b/src/proto_alpha/lib_protocol/test/helpers/context.ml index 721bb1b46ce7..65eb5847af24 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/context.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/context.ml @@ -254,6 +254,9 @@ let get_liquidity_baking_cpmm_address ctxt = let get_adaptive_inflation_launch_cycle ctxt = Adaptive_inflation_services.launch_cycle rpc_ctxt ctxt +let get_total_frozen_stake ctxt = + Adaptive_inflation_services.total_frozen_stake rpc_ctxt ctxt + let get_seed_nonce_revelation_tip ctxt = get_constants ctxt >>=? fun {Constants.parametric = csts; _} -> return diff --git a/src/proto_alpha/lib_protocol/test/helpers/context.mli b/src/proto_alpha/lib_protocol/test/helpers/context.mli index 9cc1b0dfbdc6..32f9b2fc6e1e 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/context.mli +++ b/src/proto_alpha/lib_protocol/test/helpers/context.mli @@ -118,6 +118,8 @@ val get_liquidity_baking_cpmm_address : t -> Contract_hash.t tzresult Lwt.t val get_adaptive_inflation_launch_cycle : t -> Cycle.t option tzresult Lwt.t +val get_total_frozen_stake : t -> Tez.t tzresult Lwt.t + val get_seed_nonce_revelation_tip : t -> Tez.t tzresult Lwt.t val get_vdf_revelation_tip : t -> Tez.t tzresult Lwt.t -- GitLab From a6bc6e5aa48cb93d67792134f9d971cc5fcc42ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Cauderlier?= Date: Tue, 20 Jun 2023 21:18:56 +0200 Subject: [PATCH 02/19] Tests/AI/Launch: put the constants in a named variable --- .../test/integration/test_adaptive_inflation_launch.ml | 6 +++--- 1 file changed, 3 insertions(+), 3 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 e83b840e067e..4b7d01a16ad4 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 @@ -115,7 +115,7 @@ let test_launch threshold expected_vote_duration () = Assert.lt_int32 ~loc threshold ema in (* Initialize the state with a single delegate. *) - let* block, delegate = + let constants = let default_constants = Default_parameters.constants_test in let adaptive_inflation = { @@ -124,9 +124,9 @@ let test_launch threshold expected_vote_duration () = } in let consensus_threshold = 0 in - Context.init_with_constants1 - {default_constants with consensus_threshold; adaptive_inflation} + {default_constants with consensus_threshold; adaptive_inflation} in + let* block, delegate = Context.init_with_constants1 constants in let delegate_pkh = Context.Contract.pkh delegate in let* () = assert_is_not_yet_set_to_launch ~loc:__LOC__ block in -- GitLab From 6955f102db748571b7e7ba5f2bf5c53e1faf4383 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Cauderlier?= Date: Thu, 22 Jun 2023 15:01:30 +0200 Subject: [PATCH 03/19] Tests/AI/Launch: keep all rewards liquid We set the baking over staking edge of the delegate to the max value, this means that 100% of the rewards of the delegate will go to its liquid balance. Keeping all rewards liquid simplifies reasoning about total_frozen_stake. --- .../integration/test_adaptive_inflation_launch.ml | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 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 4b7d01a16ad4..0228931681b0 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 @@ -136,14 +136,21 @@ let test_launch threshold expected_vote_duration () = explicitely set a positive staking_over_baking_limit to allow them. Setting this limit does not immediately take effect but can be done before the activation. For these reasons, we set it at - the beginning. *) + the beginning. + + The baking_over_staking_edge indicates the portion of the rewards + sent to the delegate's liquid balance. It's expressed in + billionth, with 0 meaning that everything is frozen and one + billion meaning that everything is liquid. We send all rewards to + the liquid part to ease reasoning about the total frozen + stake. *) let* block = let* operation = set_delegate_parameters (B block) delegate ~staking_over_baking_limit:1 - ~baking_over_staking_edge:0 + ~baking_over_staking_edge:1_000_000_000 in Block.bake ~operation ~adaptive_inflation_vote:Toggle_vote_on block in -- GitLab From 812ff807cd962b876257fff2a08c12d38ed7c780 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Cauderlier?= Date: Tue, 20 Jun 2023 21:43:36 +0200 Subject: [PATCH 04/19] Tests/AI/Launch: make the delegate self-stake almost everything We make the delegate self-stake most of its balance so that its costaker can stake as much stake. --- .../test/integration/test_adaptive_inflation_launch.ml | 10 ++++++++++ 1 file changed, 10 insertions(+) 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 0228931681b0..8bbdbf77e5f7 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 @@ -188,6 +188,16 @@ let test_launch threshold expected_vote_duration () = in Block.bake ~operation ~adaptive_inflation_vote:Toggle_vote_on block in + (* Self-staking most of the remaining balance. *) + let* block = + let* operation = + stake + (B block) + delegate + (Protocol.Alpha_context.Tez.of_mutez_exn 1_800_000_000_000L) + in + Block.bake ~operation ~adaptive_inflation_vote:Toggle_vote_on block + in (* We are now ready to activate the feature through by baking many more blocks voting in favor of the activation until the EMA -- GitLab From 78f51f7e2c16d1edf324d3e982280a2e3ef20eaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Cauderlier?= Date: Tue, 20 Jun 2023 21:22:48 +0200 Subject: [PATCH 05/19] Tests/AI/Launch: allow costaking for real The staking_over_baking_limit is expressed in per-million. Setting it to one prevented to actually costake much. We set it to 1 million which means that the delegate allows the costaker to costake up to the amount that the delegate self-stakes. --- .../test/integration/test_adaptive_inflation_launch.ml | 2 +- 1 file changed, 1 insertion(+), 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 8bbdbf77e5f7..91792556bc41 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 @@ -149,7 +149,7 @@ let test_launch threshold expected_vote_duration () = set_delegate_parameters (B block) delegate - ~staking_over_baking_limit:1 + ~staking_over_baking_limit:1_000_000 ~baking_over_staking_edge:1_000_000_000 in Block.bake ~operation ~adaptive_inflation_vote:Toggle_vote_on block -- GitLab From c2c2a7fac261b9f6a4f48eecff414786d6e6e0ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Cauderlier?= Date: Tue, 20 Jun 2023 21:23:08 +0200 Subject: [PATCH 06/19] Tests/AI/Launch: increase costaker initial balance This is to make the effect of costaking on total_frozen_stake more visible. --- .../test/integration/test_adaptive_inflation_launch.ml | 2 +- 1 file changed, 1 insertion(+), 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 91792556bc41..097fdf012aee 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 @@ -174,7 +174,7 @@ let test_launch threshold expected_vote_duration () = (B block) delegate wannabe_costaker - (Protocol.Alpha_context.Tez.of_mutez_exn 1_000_000_000L) + (Protocol.Alpha_context.Tez.of_mutez_exn 2_000_000_000_000L) in Block.bake ~operation ~adaptive_inflation_vote:Toggle_vote_on block in -- GitLab From 18790cdb29ccb1e3afaeba1deaeb4a7b9c8749a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Cauderlier?= Date: Tue, 20 Jun 2023 21:26:27 +0200 Subject: [PATCH 07/19] Tests/AI/Launch: costake almost everything This is also to make the effect of costaking on total_frozen_stake more visible. --- .../test_adaptive_inflation_launch.ml | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 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 097fdf012aee..30b49907adde 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 @@ -105,7 +105,7 @@ let set_delegate_parameters ctxt delegate ~staking_over_baking_limit - the launch cycle is not reset before it is reached, - once the launch cycle is reached, costaking is allowed. *) let test_launch threshold expected_vote_duration () = - let open Lwt_result_syntax in + let open Lwt_result_wrap_syntax in let assert_ema_above_threshold ~loc (metadata : Protocol.Main.block_header_metadata) = let ema = @@ -259,14 +259,13 @@ let test_launch threshold expected_vote_duration () = planned to happen. *) let* () = assert_current_cycle ~loc:__LOC__ block launch_cycle in - (* Test that the wannabe costaker is now allowed to stake a few - mutez. *) - let* operation = - stake - (B block) - wannabe_costaker - (Protocol.Alpha_context.Tez.of_mutez_exn 10L) - in + (* Test that the wannabe costaker is now allowed to stake almost all + its balance. It cannot totally costake it however because this is + considered by the protocol as an attempt to empty the account, and + emptying delegated accounts is forbidden. *) + let* balance = Context.Contract.balance (B block) wannabe_costaker in + let*?@ balance_to_stake = Protocol.Alpha_context.Tez.(balance -? one) in + let* operation = stake (B block) wannabe_costaker balance_to_stake in let* (_block : Block.t) = Block.bake ~operation block in return_unit -- GitLab From 775f8e0e4fa32bb3e68f3dd7ca52574d6ebcc41f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Cauderlier?= Date: Wed, 21 Jun 2023 15:21:48 +0200 Subject: [PATCH 08/19] Tests/AI/Launch: add a function to assert the total_frozen_stake --- .../test/integration/test_adaptive_inflation_launch.ml | 6 ++++++ 1 file changed, 6 insertions(+) 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 30b49907adde..671de0eaa3b0 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 @@ -98,6 +98,12 @@ let set_delegate_parameters ctxt delegate ~staking_over_baking_limit delegate Protocol.Alpha_context.Tez.zero +(** Assert that the staking balance is the expected one. *) +let assert_total_frozen_stake ~loc block expected = + let open Lwt_result_syntax in + let* actual = Context.get_total_frozen_stake (B block) in + Assert.equal_tez ~loc actual expected + (* Test that: - the EMA of the adaptive inflation vote reaches the threshold after the expected duration, -- GitLab From edbaf0604caf9e01e82e01f4067cdda4c230fd99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Cauderlier?= Date: Thu, 22 Jun 2023 15:02:46 +0200 Subject: [PATCH 09/19] Tests/AI/Launch: check the initial total_frozen_stake --- .../test/integration/test_adaptive_inflation_launch.ml | 6 ++++++ 1 file changed, 6 insertions(+) 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 671de0eaa3b0..0cb0f3dd15f4 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 @@ -135,6 +135,12 @@ let test_launch threshold expected_vote_duration () = let* block, delegate = Context.init_with_constants1 constants in let delegate_pkh = Context.Contract.pkh delegate in let* () = assert_is_not_yet_set_to_launch ~loc:__LOC__ block in + let* () = + assert_total_frozen_stake + ~loc:__LOC__ + block + (Protocol.Alpha_context.Tez.of_mutez_exn 200_000_000_000L) + in (* To test that adaptive inflation is active, we test that costaking, a feature only available after the activation, is -- GitLab From 293d2010f2500c5fe30a36f498dae295dd9616b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Cauderlier?= Date: Wed, 21 Jun 2023 17:36:35 +0200 Subject: [PATCH 10/19] Tests/AI/Launch: also assert total_frozen_stake before costaking This is to check that self-staking also affects total_frozen_stake. --- .../test/integration/test_adaptive_inflation_launch.ml | 9 +++++++++ 1 file changed, 9 insertions(+) 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 0cb0f3dd15f4..cde22a3ed96e 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 @@ -270,6 +270,15 @@ let test_launch threshold expected_vote_duration () = (* Check that the current cycle is the one at which the launch is planned to happen. *) let* () = assert_current_cycle ~loc:__LOC__ block launch_cycle in + (* At this point, only the delegate has frozen any stake and its + frozen balance is about 2 million tez (it started with 4 million, + sent half to its delegate, and staked the rest). *) + let* () = + assert_total_frozen_stake + ~loc:__LOC__ + block + (Protocol.Alpha_context.Tez.of_mutez_exn 2_000_000_000_000L) + in (* Test that the wannabe costaker is now allowed to stake almost all its balance. It cannot totally costake it however because this is -- GitLab From 5cd68e2377eb7817423366ec682b8fdd86a05925 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Cauderlier?= Date: Tue, 20 Jun 2023 21:27:45 +0200 Subject: [PATCH 11/19] Tests/AI/Launch: check total_frozen_stake changes after costaking `preserved_cycles` after a costaking event, the total_frozen_stake increases by the costaked amount. --- .../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 cde22a3ed96e..ab8fde0be9cb 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 @@ -132,6 +132,7 @@ let test_launch threshold expected_vote_duration () = let consensus_threshold = 0 in {default_constants with consensus_threshold; adaptive_inflation} in + let preserved_cycles = constants.preserved_cycles in let* block, delegate = Context.init_with_constants1 constants in let delegate_pkh = Context.Contract.pkh delegate in let* () = assert_is_not_yet_set_to_launch ~loc:__LOC__ block in @@ -287,7 +288,14 @@ let test_launch threshold expected_vote_duration () = let* balance = Context.Contract.balance (B block) wannabe_costaker in let*?@ balance_to_stake = Protocol.Alpha_context.Tez.(balance -? one) in let* operation = stake (B block) wannabe_costaker balance_to_stake in - let* (_block : Block.t) = Block.bake ~operation block in + let* block = Block.bake ~operation block in + let* block = Block.bake_until_n_cycle_end (preserved_cycles + 1) block in + let* () = + assert_total_frozen_stake + ~loc:__LOC__ + block + (Protocol.Alpha_context.Tez.of_mutez_exn 3_999_999_000_000L) + in return_unit let tests = -- GitLab From fad273db4c249549872242b58e2e4f65b52b8b6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Cauderlier?= Date: Wed, 21 Jun 2023 19:23:36 +0000 Subject: [PATCH 12/19] Tests/AI/Launch: update the test descriptions --- .../test/integration/test_adaptive_inflation_launch.ml | 7 ++++--- 1 file changed, 4 insertions(+), 3 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 ab8fde0be9cb..b75287971b71 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 @@ -109,7 +109,8 @@ let assert_total_frozen_stake ~loc block expected = expected duration, - the launch cycle is set as soon as the threshold is reached, - the launch cycle is not reset before it is reached, - - once the launch cycle is reached, costaking is allowed. *) + - once the launch cycle is reached, costaking is allowed, + - costaking increases total_frozen_stake. *) let test_launch threshold expected_vote_duration () = let open Lwt_result_wrap_syntax in let assert_ema_above_threshold ~loc @@ -302,14 +303,14 @@ let tests = [ Tztest.tztest "the EMA reaches the vote threshold at the expected level and adaptive \ - inflation launch cycle is set (very low threshold)" + inflation launches (very low threshold)" `Quick (test_launch 1000000l (* This means that the threshold is set at 0.05% *) 59l); Tztest.tztest "the EMA reaches the vote threshold at the expected level and adaptive \ - inflation launch cycle is set (realistic threshold)" + inflation launches (realistic threshold)" `Slow (test_launch Default_parameters.constants_test.adaptive_inflation -- GitLab From 6d29c1b68560459a030e246fd949c203056fa2ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Cauderlier?= Date: Wed, 21 Jun 2023 21:37:55 +0200 Subject: [PATCH 13/19] Tests/AI/Launch: set fees at 0 for stake pseudo-operations We set the fees of the "stake" pseudo-operation to 0 so that we can precisely test self-staking and costaking of full liquid balances. --- .../test/integration/test_adaptive_inflation_launch.ml | 8 +++++++- 1 file changed, 7 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 b75287971b71..3810bd3b7a99 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 @@ -72,7 +72,13 @@ let stake ctxt contract amount = let*?@ entrypoint = Protocol.Alpha_context.Entrypoint.of_string_strict ~loc:0 "stake" in - Op.transaction ctxt ~entrypoint contract contract amount + Op.transaction + ctxt + ~entrypoint + ~fee:Protocol.Alpha_context.Tez.zero + contract + contract + amount let set_delegate_parameters ctxt delegate ~staking_over_baking_limit ~baking_over_staking_edge = -- GitLab From c4b09570d2e78034bca3af3e5cd145eec406011e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Cauderlier?= Date: Thu, 22 Jun 2023 14:38:14 +0200 Subject: [PATCH 14/19] Tests/AI/Launch: also set fees at 0 when setting delegate params --- .../test/integration/test_adaptive_inflation_launch.ml | 1 + 1 file changed, 1 insertion(+) 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 3810bd3b7a99..57458ca2e2ec 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 @@ -100,6 +100,7 @@ let set_delegate_parameters ctxt delegate ~staking_over_baking_limit ctxt ~entrypoint ~parameters + ~fee:Protocol.Alpha_context.Tez.zero delegate delegate Protocol.Alpha_context.Tez.zero -- GitLab From d5c92c7648fe189b2adbaa117ce95f5ff9a2c803 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Cauderlier?= Date: Thu, 22 Jun 2023 16:02:20 +0200 Subject: [PATCH 15/19] Tests/Proto/Helpers: add invariant to bake_while_with_metadata Make bake_while_with_metadata check some user-provided invariant for each block it bakes. --- src/proto_alpha/lib_protocol/test/helpers/block.ml | 5 ++++- src/proto_alpha/lib_protocol/test/helpers/block.mli | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/proto_alpha/lib_protocol/test/helpers/block.ml b/src/proto_alpha/lib_protocol/test/helpers/block.ml index 69ff845c4c97..fbaac6c89abd 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/block.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/block.ml @@ -887,8 +887,10 @@ let bake_n ?(baking_mode = Application) ?policy ?liquidity_baking_toggle_vote (1 -- n) let rec bake_while_with_metadata ?(baking_mode = Application) ?policy - ?liquidity_baking_toggle_vote ?adaptive_inflation_vote predicate b = + ?liquidity_baking_toggle_vote ?adaptive_inflation_vote + ?(invariant = fun _ -> return_unit) predicate b = let open Lwt_result_syntax in + let* () = invariant b in let* new_block, metadata = bake_with_metadata ~baking_mode @@ -903,6 +905,7 @@ let rec bake_while_with_metadata ?(baking_mode = Application) ?policy ?policy ?liquidity_baking_toggle_vote ?adaptive_inflation_vote + ~invariant predicate new_block else return b diff --git a/src/proto_alpha/lib_protocol/test/helpers/block.mli b/src/proto_alpha/lib_protocol/test/helpers/block.mli index 7eca613dbba5..9535f8869960 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/block.mli +++ b/src/proto_alpha/lib_protocol/test/helpers/block.mli @@ -296,6 +296,7 @@ val bake_while_with_metadata : ?policy:baker_policy -> ?liquidity_baking_toggle_vote:Toggle_votes_repr.toggle_vote -> ?adaptive_inflation_vote:Toggle_votes_repr.toggle_vote -> + ?invariant:(block -> unit tzresult Lwt.t) -> (block -> block_header_metadata -> bool) -> block -> block tzresult Lwt.t -- GitLab From 61f265212e07beec144c72e7127c2b78b5d9987b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Cauderlier?= Date: Thu, 22 Jun 2023 16:09:45 +0200 Subject: [PATCH 16/19] Tests/Proto/Helpers: same for bake_while --- src/proto_alpha/lib_protocol/test/helpers/block.ml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/proto_alpha/lib_protocol/test/helpers/block.ml b/src/proto_alpha/lib_protocol/test/helpers/block.ml index fbaac6c89abd..7ca182a262aa 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/block.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/block.ml @@ -911,12 +911,13 @@ let rec bake_while_with_metadata ?(baking_mode = Application) ?policy else return b let bake_while ?baking_mode ?policy ?liquidity_baking_toggle_vote - ?adaptive_inflation_vote predicate b = + ?adaptive_inflation_vote ?invariant predicate b = bake_while_with_metadata ?baking_mode ?policy ?liquidity_baking_toggle_vote ?adaptive_inflation_vote + ?invariant (fun block _metadata -> predicate block) b -- GitLab From ab9797a1629d36db0a3aa8d8c9296e6311b3c994 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Cauderlier?= Date: Thu, 22 Jun 2023 16:11:54 +0200 Subject: [PATCH 17/19] Tests/Proto/Helpers: expose bake_while --- .../lib_protocol/test/helpers/block.mli | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/proto_alpha/lib_protocol/test/helpers/block.mli b/src/proto_alpha/lib_protocol/test/helpers/block.mli index 9535f8869960..6eb154433d5c 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/block.mli +++ b/src/proto_alpha/lib_protocol/test/helpers/block.mli @@ -287,10 +287,25 @@ 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. *) +(** Bake blocks while a predicate over the block holds. The returned + block is the last one for which the predicate holds; in case the + predicate never holds, the input block is returned. When the + optional [invariant] argument is provided, it is checked on the + input block and on each baked block, including the returned one + (the last one satisfy the predicate); it is however not checked + on the next block (the first one to invalidate the predicate). *) +val bake_while : + ?baking_mode:baking_mode -> + ?policy:baker_policy -> + ?liquidity_baking_toggle_vote:Toggle_votes_repr.toggle_vote -> + ?adaptive_inflation_vote:Toggle_votes_repr.toggle_vote -> + ?invariant:(block -> unit tzresult Lwt.t) -> + (block -> bool) -> + block -> + block tzresult Lwt.t + +(* Same as [bake_while] but the predicate also has access to the + metadata resulting from the application of the block. *) val bake_while_with_metadata : ?baking_mode:baking_mode -> ?policy:baker_policy -> -- GitLab From a2386431e16c2c193bb53ecece2223befee91bf0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Cauderlier?= Date: Thu, 22 Jun 2023 16:51:19 +0200 Subject: [PATCH 18/19] Tests/Proto/Helpers: keep constants inside Block.t --- .../lib_protocol/test/helpers/block.ml | 20 ++++++++++--------- .../lib_protocol/test/helpers/block.mli | 3 ++- .../lib_protocol/test/helpers/incremental.ml | 10 +++++++++- .../test/integration/gas/test_gas_levels.ml | 3 ++- .../test_adaptive_inflation_launch.ml | 3 +-- 5 files changed, 25 insertions(+), 14 deletions(-) diff --git a/src/proto_alpha/lib_protocol/test/helpers/block.ml b/src/proto_alpha/lib_protocol/test/helpers/block.ml index 7ca182a262aa..164fc047b9d1 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/block.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/block.ml @@ -35,6 +35,7 @@ type t = { header : Block_header.t; operations : Operation.packed list; context : Tezos_protocol_environment.Context.t; + constants : Constants.Parametric.t; } type block = t @@ -475,6 +476,7 @@ let genesis_with_parameters parameters = header = {shell; protocol_data = {contents; signature = Signature.zero}}; operations = []; context; + constants = parameters.constants; } let validate_bootstrap_accounts @@ -640,6 +642,7 @@ let genesis ?commitments ?consensus_threshold ?min_proposal_quorum header = {shell; protocol_data = {contents; signature = Signature.zero}}; operations = []; context; + constants; } let alpha_context ?commitments ?min_proposal_quorum @@ -788,9 +791,11 @@ let apply_with_metadata ?(policy = By_round 0) ?(check_size = true) ~baking_mode finalize_validation_and_application vstate (Some header.shell) >|= Environment.wrap_tzresult >|=? fun (validation, result) -> (validation.context, result) ) - >|=? fun (context, result) -> + >>=? fun (context, result) -> let hash = Block_header.hash header in - ({hash; header; operations; context}, result) + (* This function is duplicated from Context to avoid a cyclic dependency *) + Alpha_services.Constants.all rpc_ctxt pred >|=? fun constants -> + ({hash; header; operations; context; constants = constants.parametric}, result) let apply header ?(operations = []) ?(allow_manager_failures = false) pred = apply_with_metadata @@ -870,9 +875,6 @@ let bake ?(baking_mode = Application) ?(allow_manager_failures = false) (********** Cycles ****************) -(* This function is duplicated from Context to avoid a cyclic dependency *) -let get_constants b = Alpha_services.Constants.all rpc_ctxt b - let bake_n ?(baking_mode = Application) ?policy ?liquidity_baking_toggle_vote ?adaptive_inflation_vote n b = List.fold_left_es @@ -1033,7 +1035,7 @@ let bake_n_with_liquidity_baking_toggle_ema ?(baking_mode = Application) ?policy (1 -- n) let bake_until_cycle_end ?policy b = - get_constants b >>=? fun Constants.{parametric = {blocks_per_cycle; _}; _} -> + let blocks_per_cycle = b.constants.blocks_per_cycle in let current_level = b.header.shell.level in let current_level = Int32.rem current_level blocks_per_cycle in let delta = Int32.sub blocks_per_cycle current_level in @@ -1043,15 +1045,15 @@ let bake_until_n_cycle_end ?policy n b = List.fold_left_es (fun b _ -> bake_until_cycle_end ?policy b) b (1 -- n) let current_cycle b = - get_constants b >>=? fun Constants.{parametric = {blocks_per_cycle; _}; _} -> + let blocks_per_cycle = b.constants.blocks_per_cycle in let current_level = b.header.shell.level in let current_cycle = Int32.div current_level blocks_per_cycle in let current_cycle = Cycle.add Cycle.root (Int32.to_int current_cycle) in - return current_cycle + current_cycle let bake_until_cycle ?policy cycle (b : t) = let rec loop (b : t) = - current_cycle b >>=? fun current_cycle -> + let current_cycle = current_cycle b in if Cycle.equal cycle current_cycle then return b else bake_until_cycle_end ?policy b >>=? fun b -> loop b in diff --git a/src/proto_alpha/lib_protocol/test/helpers/block.mli b/src/proto_alpha/lib_protocol/test/helpers/block.mli index 6eb154433d5c..c7cafb6949b7 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/block.mli +++ b/src/proto_alpha/lib_protocol/test/helpers/block.mli @@ -33,6 +33,7 @@ type t = { header : Block_header.t; operations : Operation.packed list; context : Tezos_protocol_environment.Context.t; (** Resulting context *) + constants : Constants.Parametric.t; } type block = t @@ -316,7 +317,7 @@ val bake_while_with_metadata : block -> block tzresult Lwt.t -val current_cycle : t -> Cycle.t tzresult Lwt.t +val current_cycle : t -> Cycle.t (** Given a block [b] at level [l] bakes enough blocks to complete a cycle, that is [blocks_per_cycle - (l % blocks_per_cycle)]. *) diff --git a/src/proto_alpha/lib_protocol/test/helpers/incremental.ml b/src/proto_alpha/lib_protocol/test/helpers/incremental.ml index 08b512e6834d..0b191f089c67 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/incremental.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/incremental.ml @@ -278,7 +278,15 @@ let finalize_block st = } in let hash = Block_header.hash header in - return {Block.hash; header; operations; context = validation_result.context} + let* constants = Alpha_services.Constants.all Block.rpc_ctxt st.predecessor in + return + { + Block.hash; + header; + operations; + context = validation_result.context; + constants = constants.parametric; + } let assert_validate_operation_fails expect_failure op block = let open Lwt_result_syntax in diff --git a/src/proto_alpha/lib_protocol/test/integration/gas/test_gas_levels.ml b/src/proto_alpha/lib_protocol/test/integration/gas/test_gas_levels.ml index 7732e31ee87f..0a2d878826e3 100644 --- a/src/proto_alpha/lib_protocol/test/integration/gas/test_gas_levels.ml +++ b/src/proto_alpha/lib_protocol/test/integration/gas/test_gas_levels.ml @@ -233,7 +233,8 @@ let apply_with_gas header ?(operations = []) (pred : Block.t) = >|= Environment.wrap_tzresult >|=? fun (context, consumed_gas) -> let hash = Block_header.hash header in - ({Block.hash; header; operations; context}, consumed_gas) + ( {Block.hash; header; operations; context; constants = pred.constants}, + consumed_gas ) let bake_with_gas ?policy ?timestamp ?operation ?operations pred = let operations = 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 57458ca2e2ec..ed0d7ccdb159 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 @@ -63,8 +63,7 @@ let assert_cycle_eq ~loc c1 c2 = c2 let assert_current_cycle ~loc (blk : Block.t) expected = - let open Lwt_result_syntax in - let* current_cycle = Block.current_cycle blk in + let current_cycle = Block.current_cycle blk in assert_cycle_eq ~loc current_cycle expected let stake ctxt contract amount = -- GitLab From 22086dc89c729bdac4059561135931898211a021 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Cauderlier?= Date: Thu, 22 Jun 2023 17:08:26 +0200 Subject: [PATCH 19/19] Tests/AI/Launch: check staking balance not updated too soon --- .../test_adaptive_inflation_launch.ml | 19 ++++++++++++++++++- 1 file changed, 18 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 ed0d7ccdb159..54debbc98538 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 @@ -296,7 +296,24 @@ let test_launch threshold expected_vote_duration () = let*?@ balance_to_stake = Protocol.Alpha_context.Tez.(balance -? one) in let* operation = stake (B block) wannabe_costaker balance_to_stake in let* block = Block.bake ~operation block in - let* block = Block.bake_until_n_cycle_end (preserved_cycles + 1) block in + (* The costaking operation leads to an increase of the + total_frozen_stake but only preserved_cycles after the + operation. *) + let start_cycle = Block.current_cycle block in + let* block = + Block.bake_while + ~invariant:(fun block -> + assert_total_frozen_stake + ~loc:__LOC__ + block + (Protocol.Alpha_context.Tez.of_mutez_exn 2_000_000_000_000L)) + (fun block -> + let current_cycle = Block.current_cycle block in + Protocol.Alpha_context.Cycle.( + current_cycle <= add start_cycle preserved_cycles)) + block + in + let* block = Block.bake block in let* () = assert_total_frozen_stake ~loc:__LOC__ -- GitLab