From 11d1af8f72b3064c836ecb5dd92ba5a52ad01cca Mon Sep 17 00:00:00 2001 From: Diane Gallois-Wong Date: Thu, 21 Mar 2024 13:41:19 +0100 Subject: [PATCH 1/5] Proto/AI: fix typo in frozen_deposits RPC description --- src/proto_alpha/lib_protocol/delegate_services.ml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/proto_alpha/lib_protocol/delegate_services.ml b/src/proto_alpha/lib_protocol/delegate_services.ml index e48962d5b718..5669121affbd 100644 --- a/src/proto_alpha/lib_protocol/delegate_services.ml +++ b/src/proto_alpha/lib_protocol/delegate_services.ml @@ -354,7 +354,7 @@ module S = struct RPC_service.get_service ~description: "Returns the amount (in mutez) frozen as a deposit at the time the \ - staking rights for the current cycle where computed." + staking rights for the current cycle were computed." ~query:RPC_query.empty ~output:Tez.encoding RPC_path.(path / "frozen_deposits") -- GitLab From f4f9532d1314d2e88ba9637f352febf9fb1d5884 Mon Sep 17 00:00:00 2001 From: Diane Gallois-Wong Date: Wed, 20 Mar 2024 17:35:31 +0100 Subject: [PATCH 2/5] Proto/test/AI: actually compute the frozen rights used in the state but still check they're consistent with the initial_frozen_deposits RPC This breaks the tests because of some other problems, that are fixed in the next two commits --- .../test/helpers/account_helpers.ml | 15 +++++++ .../test/helpers/scenario_bake.ml | 2 +- .../test/helpers/state_account.ml | 41 +++++++++++++++---- .../test/helpers/tez_staking_helpers.ml | 16 ++++++-- 4 files changed, 61 insertions(+), 13 deletions(-) diff --git a/src/proto_alpha/lib_protocol/test/helpers/account_helpers.ml b/src/proto_alpha/lib_protocol/test/helpers/account_helpers.ml index 8755efd98aae..620f200a85ee 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/account_helpers.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/account_helpers.ml @@ -40,6 +40,15 @@ type account_state = { staking_delegator_numerator : Z.t; staking_delegate_denominator : Z.t; frozen_rights : Tez.t CycleMap.t; + (** The portion of rights that comes from staking, used for + baking/attesting during the specified cycle. + + At the end of cycle [c], the current frozen deposits of the + delegate (own + co-staked, taking + limit_of_staking_over_baking into account) are added to this + table for cycle [c + consensus_rights_delay + 1]. The table + is unmodified if at that time, the account is not a delegate + or is a deactivated delegate. *) slashed_cycles : Cycle.t list; } @@ -521,3 +530,9 @@ let log_debug_balance_update account_name old_account_map new_account_map : unit old_total_balance Tez.pp new_total_balance + +let current_total_frozen_deposits_with_limits account_state = + Frozen_tez.total_current_with_limits + ~limit_of_staking_over_baking: + account_state.parameters.limit_of_staking_over_baking + account_state.frozen_deposits 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 173790c34d63..d3edf8050334 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/scenario_bake.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/scenario_bake.ml @@ -17,7 +17,7 @@ let apply_end_cycle current_cycle previous_block block state : let open Lwt_result_syntax in Log.debug ~color:time_color "Ending cycle %a" Cycle.pp current_cycle ; (* Sets initial frozen for future cycle *) - let* state = update_map_es ~f:(update_frozen_rights_cycle block) state in + let* state = update_map_es ~f:(compute_future_frozen_rights block) state in (* Apply all slashes *) let* state = Slashing_helpers.apply_all_slashes_at_cycle_end diff --git a/src/proto_alpha/lib_protocol/test/helpers/state_account.ml b/src/proto_alpha/lib_protocol/test/helpers/state_account.ml index 2711986651b9..d2cfb46895a4 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/state_account.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/state_account.ml @@ -376,9 +376,10 @@ let apply_finalize staker_name account_map = account_map account_map -(* Given cycle is the cycle for which the rights are computed, usually current + - consensus rights delay *) -let update_frozen_rights_cycle block account_map = +(** Compute the staking rights for [current_cycle + + consensus_rights_delay + 1] and save them into + [account.frozen_rights] for each delegate. *) +let compute_future_frozen_rights block account_map = let open Lwt_result_syntax in String.Map.fold_es (fun key acc acc_map -> @@ -395,15 +396,37 @@ let update_frozen_rights_cycle block account_map = in if delegate_pkh = acc.pkh then (* Account is a delegate *) - let cycle = Block.current_cycle block in - let* frozen_deposits = + let current_cycle = Block.current_cycle block in + (* Check the rights of current cycle. *) + let current_rights_state = + CycleMap.find (Block.current_cycle block) acc.frozen_rights + |> Option.value ~default:Tez.zero + in + let* current_rights_rpc = Context.Delegate.initial_frozen_deposits (B block) acc.pkh in - - let frozen_rights = - CycleMap.add cycle frozen_deposits acc.frozen_rights + let* () = + Assert.equal_tez + ~loc:__LOC__ + current_rights_state + current_rights_rpc in - return (String.Map.add key {acc with frozen_rights} acc_map) + (* Fill in the rights for future cycle. *) + let* deactivated = Context.Delegate.deactivated (B block) acc.pkh in + if deactivated then return acc_map + else + let future_cycle = + Cycle.add + current_cycle + (block.constants.consensus_rights_delay + 1) + in + let frozen_rights = + CycleMap.add + future_cycle + (current_total_frozen_deposits_with_limits acc) + acc.frozen_rights + in + return (String.Map.add key {acc with frozen_rights} acc_map) else return acc_map) account_map account_map diff --git a/src/proto_alpha/lib_protocol/test/helpers/tez_staking_helpers.ml b/src/proto_alpha/lib_protocol/test/helpers/tez_staking_helpers.ml index 59389f1d0921..5a440257d540 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/tez_staking_helpers.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/tez_staking_helpers.ml @@ -121,11 +121,21 @@ module Frozen_tez = struct co_current Partial_tez.zero - let total_current a = - let r = total_co_current_q a.co_current in + let total_co_current t = + let r = total_co_current_q t.co_current in let tez, rem = Partial_tez.to_tez_rem r in assert (Q.(equal rem zero)) ; - Tez.(tez +! a.self_current) + tez + + let total_current t = Tez.(t.self_current +! total_co_current t) + + let total_current_with_limits ~limit_of_staking_over_baking t = + let max_co_current = + Tez.mul_q t.self_current limit_of_staking_over_baking + |> Tez.of_q ~round:`Down + in + let co_current = Tez.min (total_co_current t) max_co_current in + Tez.(t.self_current +! co_current) (* Precondition: 0 <= quantity < 1 && co_current + quantity is int *) let add_q_to_all_co_current quantity co_current = -- GitLab From 16e654ba92224cdfdde93e53f8e1d81b157cdce6 Mon Sep 17 00:00:00 2001 From: Diane Gallois-Wong Date: Wed, 20 Mar 2024 15:33:45 +0100 Subject: [PATCH 3/5] Proto/test/AI: fix delay before new delegate parameters are activated --- src/proto_alpha/lib_protocol/test/helpers/scenario_op.ml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/proto_alpha/lib_protocol/test/helpers/scenario_op.ml b/src/proto_alpha/lib_protocol/test/helpers/scenario_op.ml index e93f0999700c..4d9ef0ad945b 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/scenario_op.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/scenario_op.ml @@ -32,7 +32,7 @@ let set_delegate_params delegate_name parameters : (t, t) scenarios = set_delegate_parameters (B block) delegate.contract ~parameters in (* Update state *) - let wait = state.constants.delegate_parameters_activation_delay - 1 in + let wait = state.constants.delegate_parameters_activation_delay in let state = { state with -- GitLab From 408076ed627ed5bf1a9846604d10b708babc7d13 Mon Sep 17 00:00:00 2001 From: Diane Gallois-Wong Date: Wed, 20 Mar 2024 16:46:36 +0100 Subject: [PATCH 4/5] Proto/test/AI: fix order of end-of-cycle computations --- src/proto_alpha/lib_protocol/test/helpers/scenario_bake.ml | 4 ++-- 1 file changed, 2 insertions(+), 2 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 d3edf8050334..48c7b1533c46 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/scenario_bake.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/scenario_bake.ml @@ -16,8 +16,6 @@ let apply_end_cycle current_cycle previous_block block state : State.t tzresult Lwt.t = let open Lwt_result_syntax in Log.debug ~color:time_color "Ending cycle %a" Cycle.pp current_cycle ; - (* Sets initial frozen for future cycle *) - let* state = update_map_es ~f:(compute_future_frozen_rights block) state in (* Apply all slashes *) let* state = Slashing_helpers.apply_all_slashes_at_cycle_end @@ -27,6 +25,8 @@ let apply_end_cycle current_cycle previous_block block state : in (* Apply autostaking *) let*? state = State_ai_flags.Autostake.run_at_cycle_end block state in + (* Sets initial frozen for future cycle *) + let* state = update_map_es ~f:(compute_future_frozen_rights block) state in (* Apply parameter changes *) let state, param_requests = List.fold_left -- GitLab From cf26dfaea29a412384c54354a1b5552d13faf433 Mon Sep 17 00:00:00 2001 From: Diane Gallois-Wong Date: Wed, 20 Mar 2024 16:38:49 +0100 Subject: [PATCH 5/5] Proto/test/AI: remove unnecessary set_baker This commit is not necessary for the tests to pass. But there is no reason to restrict bakers in test_scenario_stake.ml, since there is no slashing, meaning no forbidden delegates. --- .../lib_protocol/test/integration/test_scenario_stake.ml | 1 - 1 file changed, 1 deletion(-) diff --git a/src/proto_alpha/lib_protocol/test/integration/test_scenario_stake.ml b/src/proto_alpha/lib_protocol/test/integration/test_scenario_stake.ml index 5c14603fea50..b55096cfa031 100644 --- a/src/proto_alpha/lib_protocol/test/integration/test_scenario_stake.ml +++ b/src/proto_alpha/lib_protocol/test/integration/test_scenario_stake.ml @@ -38,7 +38,6 @@ let init_staker_delegate_or_external = --> Scenario_begin.activate_ai `Force --> begin_test [name] --> set_delegate_params name init_params - --> set_baker "__bootstrap__" in Tag "AI activated" --> (Tag "self stake" --> begin_test ~self_stake:true -- GitLab