diff --git a/src/proto_alpha/lib_protocol/delegate_services.ml b/src/proto_alpha/lib_protocol/delegate_services.ml index e48962d5b71811ad0958ce78eaebc761e07388c9..5669121affbdaee0e4e1739197e8c59a3170e93f 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") 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 8755efd98aae609289c67789d12bc44eb3b1a2ca..620f200a85ee900de33060ef6f09b082b228e5a8 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 173790c34d635d0649722b9a8b51378bb614e0cb..48c7b1533c461f3c1dffcdf51e6770100cf401b6 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:(update_frozen_rights_cycle 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 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 e93f0999700c29d4b72c3c651f963556ae7b0fd3..4d9ef0ad945bf2bbe79de2d42bde088a5e2e6347 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 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 2711986651b94984e70d0433cb3f24a070a054f8..d2cfb46895a40f9c6fd8f06fe8829e7cd4cf8078 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 59389f1d0921f0c12e0a3469c7067877f6a58614..5a440257d540b1cd1d9ef015a9eda54d0f809463 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 = 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 5c14603fea508243d24f45024f5068e4e6a14963..b55096cfa031ff429a0a60ac18f149bc40692583 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