From 4e06ab80fdce3c88110d26ab042d67a0fbeca2cd Mon Sep 17 00:00:00 2001 From: Lucas Randazzo Date: Fri, 15 Mar 2024 15:50:47 +0100 Subject: [PATCH 1/3] Proto/tests: include attestations --- .../test/helpers/scenario_bake.ml | 24 +++++++++++++++++++ .../test/helpers/scenario_begin.ml | 5 ++-- .../lib_protocol/test/helpers/state.ml | 1 + .../integration/test_scenario_autostaking.ml | 11 +++++---- 4 files changed, 35 insertions(+), 6 deletions(-) diff --git a/src/proto_alpha/lib_protocol/test/helpers/scenario_bake.ml b/src/proto_alpha/lib_protocol/test/helpers/scenario_bake.ml index 48c7b1533c46..d3eeb0f52c85 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/scenario_bake.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/scenario_bake.ml @@ -124,6 +124,26 @@ let check_issuance_rpc block : unit tzresult Lwt.t = in return_unit +let attest_all_ = + let open Lwt_result_syntax in + fun (block, state) -> + let dlgs = + String.Map.bindings state.State.account_map + |> List.filter (fun (name, acc) -> + match acc.delegate with + | Some x -> String.equal x name + | None -> false) + |> List.map snd + in + let* ops = + List.map_es (fun dlg -> Op.attestation ~delegate:dlg.pkh block) dlgs + in + let state = State.add_pending_operations ops state in + return (block, state) + +(* Does not produce a new block *) +let attest_all = exec attest_all_ + (** Bake a block, with the given baker and the given operations. *) let bake ?baker : t -> t tzresult Lwt.t = fun (block, state) -> @@ -211,6 +231,10 @@ let bake ?baker : t -> t tzresult Lwt.t = else apply_end_cycle current_cycle previous_block block state in let* () = check_all_balances block state in + let* block, state = + if state.force_attest_all then attest_all_ (block, state) + else return (block, state) + in return (block, state) (** Bake until a cycle is reached, using [bake] instead of [Block.bake] *) diff --git a/src/proto_alpha/lib_protocol/test/helpers/scenario_begin.ml b/src/proto_alpha/lib_protocol/test/helpers/scenario_begin.ml index 3691f3b8aa89..2f6097f0ded4 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/scenario_begin.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/scenario_begin.ml @@ -117,8 +117,8 @@ let init_constants ?(default = Test) ?(reward_per_block = 0L) --> set S.Adaptive_issuance.ns_enable false (** Initialize the test, given some initial parameters *) -let begin_test ?(burn_rewards = false) delegates_name_list : - (constants, t) scenarios = +let begin_test ?(burn_rewards = false) ?(force_attest_all = false) + delegates_name_list : (constants, t) scenarios = exec (fun (constants : constants) -> let open Lwt_result_syntax in let bootstrap = "__bootstrap__" in @@ -181,6 +181,7 @@ let begin_test ?(burn_rewards = false) delegates_name_list : pending_slashes = []; double_signings = []; ai_activation_cycle = None; + force_attest_all; } in let* () = check_all_balances block state in diff --git a/src/proto_alpha/lib_protocol/test/helpers/state.ml b/src/proto_alpha/lib_protocol/test/helpers/state.ml index ffd2808c1474..e2989568f60f 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/state.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/state.ml @@ -33,6 +33,7 @@ type t = { (Signature.Public_key_hash.t * Protocol.Denunciations_repr.item) list; double_signings : double_signing_state list; ai_activation_cycle : Protocol.Alpha_context.Cycle.t option; + force_attest_all : bool; } (** Expected number of cycles before staking parameters get applied *) diff --git a/src/proto_alpha/lib_protocol/test/integration/test_scenario_autostaking.ml b/src/proto_alpha/lib_protocol/test/integration/test_scenario_autostaking.ml index bb2c4a3005ab..25cc7f121d54 100644 --- a/src/proto_alpha/lib_protocol/test/integration/test_scenario_autostaking.ml +++ b/src/proto_alpha/lib_protocol/test/integration/test_scenario_autostaking.ml @@ -146,29 +146,32 @@ let test_overdelegation = begin with 4M tz, with 5% staked *) init_constants () --> set S.Adaptive_issuance.autostaking_enable true + --> set S.consensus_committee_size 7000 --> activate_ai `No - --> begin_test ["delegate"; "faucet1"; "faucet2"; "faucet3"] + --> begin_test + ~force_attest_all:true + ["delegate"; "faucet1"; "faucet2"; "faucet3"] --> add_account_with_funds "delegator_to_fund" ~funder:"delegate" (Amount (Tez.of_mutez 3_600_000_000_000L)) (* Delegate has 200k staked and 200k liquid *) --> set_delegate "delegator_to_fund" (Some "delegate") - (* Delegate stake will not change at the end of cycle: same stake *) + (* Delegate stake will not change at the end of cycle: same stake *) --> next_cycle --> check_balance_field "delegate" `Staked (Tez.of_mutez 200_000_000_000L) --> transfer "faucet1" "delegator_to_fund" (Amount (Tez.of_mutez 3_600_000_000_000L)) - (* Delegate is not overdelegated, but will need to freeze 180k *) + (* Delegate is not overdelegated, but will need to freeze 180k *) --> next_cycle --> check_balance_field "delegate" `Staked (Tez.of_mutez 380_000_000_000L) --> transfer "faucet2" "delegator_to_fund" (Amount (Tez.of_mutez 3_600_000_000_000L)) - (* Delegate is now overdelegated, it will freeze 100% *) + (* Delegate is now overdelegated, it will freeze 100% *) --> next_cycle --> check_balance_field "delegate" `Staked (Tez.of_mutez 400_000_000_000L) --> transfer -- GitLab From 84cc72975a898c828becd1b359c88bd5682d7806 Mon Sep 17 00:00:00 2001 From: Marina Polubelova Date: Thu, 14 Mar 2024 17:46:13 +0100 Subject: [PATCH 2/3] proto/tests: check balances after decreasing delegation --- .../test/helpers/scenario_base.ml | 6 ++ .../integration/test_scenario_autostaking.ml | 58 +++++++++++++++++-- 2 files changed, 58 insertions(+), 6 deletions(-) diff --git a/src/proto_alpha/lib_protocol/test/helpers/scenario_base.ml b/src/proto_alpha/lib_protocol/test/helpers/scenario_base.ml index 1e8d25891506..d49ef42ba9b7 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/scenario_base.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/scenario_base.ml @@ -252,3 +252,9 @@ let check_balance_field src_name field amount : (t, t) scenarios = check src_total in return_unit) + +let check_balance_fields src_name ~liquid ~staked + ?(unstaked_frozen_total = Tez.zero) () = + check_balance_field src_name `Staked staked + --> check_balance_field src_name `Liquid liquid + --> check_balance_field src_name `Unstaked_frozen_total unstaked_frozen_total diff --git a/src/proto_alpha/lib_protocol/test/integration/test_scenario_autostaking.ml b/src/proto_alpha/lib_protocol/test/integration/test_scenario_autostaking.ml index 25cc7f121d54..5fe87fda0068 100644 --- a/src/proto_alpha/lib_protocol/test/integration/test_scenario_autostaking.ml +++ b/src/proto_alpha/lib_protocol/test/integration/test_scenario_autostaking.ml @@ -155,32 +155,78 @@ let test_overdelegation = "delegator_to_fund" ~funder:"delegate" (Amount (Tez.of_mutez 3_600_000_000_000L)) - (* Delegate has 200k staked and 200k liquid *) + (* Delegate has 200k = 5% * 4M staked and 200k liquid *) + --> check_balance_fields + "delegate" + ~liquid:(Tez.of_mutez 200_000_000_000L) + ~staked:(Tez.of_mutez 200_000_000_000L) + () --> set_delegate "delegator_to_fund" (Some "delegate") (* Delegate stake will not change at the end of cycle: same stake *) --> next_cycle - --> check_balance_field "delegate" `Staked (Tez.of_mutez 200_000_000_000L) + --> check_balance_fields + "delegate" + ~liquid:(Tez.of_mutez 200_000_000_000L) + ~staked:(Tez.of_mutez 200_000_000_000L) + () --> transfer "faucet1" "delegator_to_fund" (Amount (Tez.of_mutez 3_600_000_000_000L)) - (* Delegate is not overdelegated, but will need to freeze 180k *) + (* Delegate is not overdelegated, but will need to freeze 180k = 5% * 3.6M *) --> next_cycle - --> check_balance_field "delegate" `Staked (Tez.of_mutez 380_000_000_000L) + --> check_balance_fields + "delegate" + ~liquid:(Tez.of_mutez 20_000_000_000L) + ~staked:(Tez.of_mutez 380_000_000_000L) + () --> transfer "faucet2" "delegator_to_fund" (Amount (Tez.of_mutez 3_600_000_000_000L)) (* Delegate is now overdelegated, it will freeze 100% *) --> next_cycle - --> check_balance_field "delegate" `Staked (Tez.of_mutez 400_000_000_000L) + --> check_balance_fields + "delegate" + ~liquid:Tez.zero + ~staked:(Tez.of_mutez 400_000_000_000L) + () --> transfer "faucet3" "delegator_to_fund" (Amount (Tez.of_mutez 3_600_000_000_000L)) (* Delegate is overmegadelegated *) --> next_cycle - --> check_balance_field "delegate" `Staked (Tez.of_mutez 400_000_000_000L) + --> check_balance_field + "delegator_to_fund" + `Liquid + (Tez.of_mutez 14_400_000_000_000L) + --> check_balance_fields + "delegate" + ~liquid:Tez.zero + ~staked:(Tez.of_mutez 400_000_000_000L) + () + --> transfer + "delegator_to_fund" + "faucet1" + (Amount (Tez.of_mutez 7_200_000_000_000L)) + (* Delegate is not overdelegated anymore, it will freeze 380k = 5% * 7.2M + and unstake 20k *) + --> next_cycle + --> check_balance_fields + "delegate" + ~liquid:Tez.zero + ~staked:(Tez.of_mutez 380_000_000_000L) + ~unstaked_frozen_total:(Tez.of_mutez 20_000_000_000L) + () + (* Unfreezing will be done automatically in + (consensus_rights_delay + max_slashing_period) cycles *) + --> wait_n_cycles_f Test_scenario_stake.unstake_wait + --> check_balance_fields + "delegate" + ~liquid:(Tez.of_mutez 20_000_000_000L) + ~staked:(Tez.of_mutez 380_000_000_000L) + () let tests = tests_of_scenarios -- GitLab From fc925fe9443190d147ea73718a0c2b195f2098af Mon Sep 17 00:00:00 2001 From: Marina Polubelova Date: Tue, 19 Mar 2024 11:41:59 +0100 Subject: [PATCH 3/3] proto/tests: check balances for autostaking --- .../integration/test_scenario_autostaking.ml | 33 ++++++++++++++----- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/src/proto_alpha/lib_protocol/test/integration/test_scenario_autostaking.ml b/src/proto_alpha/lib_protocol/test/integration/test_scenario_autostaking.ml index 5fe87fda0068..ac7adbf0440e 100644 --- a/src/proto_alpha/lib_protocol/test/integration/test_scenario_autostaking.ml +++ b/src/proto_alpha/lib_protocol/test/integration/test_scenario_autostaking.ml @@ -40,6 +40,10 @@ let assert_balance_evolution ~loc ~for_accounts ~part ~name ~old_balance Log.debug ~color:warning_color "Balances changes failed:@." ; Log.debug "@[Old Balance@ %a@]@." balance_pp old_balance ; Log.debug "@[New Balance@ %a@]@." balance_pp new_balance ; + Log.debug + "@[Diff between balances@ %a@]@." + Q.pp_print + Q.(new_b - old_b) ; failwith "%s Unexpected stake evolution for %s" loc name) else ( Log.error @@ -47,6 +51,14 @@ let assert_balance_evolution ~loc ~for_accounts ~part ~name ~old_balance name ; assert false) +let gt_diff diff new_b old_b = + let diff = Q.of_int64 @@ Tez.to_mutez diff in + Q.equal new_b (Q.add old_b diff) + +let lt_diff diff new_b old_b = + let diff = Q.of_int64 @@ Tez.to_mutez diff in + Q.equal new_b (Q.sub old_b diff) + let delegate = "delegate" and delegator1 = "delegator1" @@ -74,40 +86,43 @@ let setup ~activate_ai = let test_autostaking = Tag "No Ai" --> setup ~activate_ai:false + (* Delegate will need to freeze 5% * 2k = 100 *) --> check_snapshot_balances ~f: (assert_balance_evolution ~loc:__LOC__ ~for_accounts:[delegate] ~part:`staked - Q.gt) + (gt_diff @@ Tez.of_mutez 100_000_000L)) "before delegation" --> snapshot_balances "before second delegation" [delegate] --> (Tag "increase delegation" --> set_delegate delegator2 (Some delegate) --> next_cycle + (* Delegate will need to freeze 5% * 2k = 100 *) --> check_snapshot_balances ~f: (assert_balance_evolution ~loc:__LOC__ ~for_accounts:[delegate] ~part:`staked - Q.gt) + (gt_diff @@ Tez.of_mutez 100_000_000L)) "before second delegation" |+ Tag "constant delegation" --> snapshot_balances "after stake change" [delegate] - --> wait_n_cycles 8 + --> wait_n_cycles 6 --> check_snapshot_balances "after stake change" |+ Tag "decrease delegation" --> set_delegate delegator1 None --> next_cycle + (* Delegate will need to unfreeze 5% * 2k = 100 *) --> check_snapshot_balances ~f: (assert_balance_evolution ~loc:__LOC__ ~for_accounts:[delegate] ~part:`staked - Q.lt) + (lt_diff @@ Tez.of_mutez 100_000_000L)) "before second delegation" --> check_snapshot_balances ~f: @@ -115,19 +130,19 @@ let test_autostaking = ~loc:__LOC__ ~for_accounts:[delegate] ~part:`unstaked_frozen - Q.gt) + (gt_diff @@ Tez.of_mutez 100_000_000L)) "before second delegation" --> snapshot_balances "after unstake" [delegate] --> next_cycle --> check_snapshot_balances "after unstake" - --> wait_n_cycles 4 + --> wait_n_cycles_f Test_scenario_stake.(unstake_wait -- 1) --> check_snapshot_balances ~f: (assert_balance_evolution ~loc:__LOC__ ~for_accounts:[delegate] ~part:`unstaked_frozen - Q.lt) + (lt_diff @@ Tez.of_mutez 100_000_000L)) "after unstake" (* finalizable are auto-finalize immediately *) --> check_snapshot_balances @@ -136,8 +151,8 @@ let test_autostaking = ~loc:__LOC__ ~for_accounts:[delegate] ~part:`liquid - Q.lt) - "before finalisation") + (gt_diff @@ Tez.of_mutez 100_000_000L)) + "after unstake") |+ Tag "Yes AI" --> setup ~activate_ai:true --> check_snapshot_balances "before delegation" -- GitLab