From f6f57386e6c1aa094cfdb272b9f89553d971ecee Mon Sep 17 00:00:00 2001 From: Julien Tesson Date: Wed, 17 Apr 2024 10:39:35 +0200 Subject: [PATCH 1/3] proto/tests/scenario_base: more log --- .../lib_protocol/test/helpers/scenario_base.ml | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 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 d49ef42ba9b7..ba773e34c827 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/scenario_base.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/scenario_base.ml @@ -43,13 +43,21 @@ let set_baker baker : (t, t) scenarios = (** Exclude a list of delegates from baking *) let exclude_bakers bakers : (t, t) scenarios = - log ~color:event_color "Excluding bakers: [ %s ]" (String.concat ", " bakers) - --> let open Lwt_result_syntax in exec_state (fun (_block, state) -> let bakers_pkh = List.map (fun baker -> (State.find_account baker state).pkh) bakers in + let log_list = + List.combine_drop bakers bakers_pkh + |> List.map (fun (name, pkh) -> + Format.asprintf "%s(%a)" name Signature.Public_key_hash.pp pkh) + in + Log.log + ~level:Cli.Logs.Info + ~color:event_color + "Excluding bakers: [ %s ]" + (String.concat ", " log_list) ; return {state with State.baking_policy = Some (Block.Excluding bakers_pkh)}) -- GitLab From f6b748867dd2a6c771deefeb4c8377f9117e7589 Mon Sep 17 00:00:00 2001 From: Julien Tesson Date: Mon, 15 Apr 2024 23:10:26 +0200 Subject: [PATCH 2/3] proto/tests/helpers: log inactivity cycle --- .../test/helpers/scenario_bake.ml | 2 +- .../test/helpers/state_ai_flags.ml | 161 ++++++++++-------- .../test/helpers/state_ai_flags.mli | 2 +- 3 files changed, 92 insertions(+), 73 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 bed3a210094d..fdb94a894655 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/scenario_bake.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/scenario_bake.ml @@ -24,7 +24,7 @@ let apply_end_cycle current_cycle previous_block block state : state in (* Apply autostaking *) - let*? state = State_ai_flags.Autostake.run_at_cycle_end block state in + 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 *) diff --git a/src/proto_alpha/lib_protocol/test/helpers/state_ai_flags.ml b/src/proto_alpha/lib_protocol/test/helpers/state_ai_flags.ml index 17d84d8c1743..878552e6d57d 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/state_ai_flags.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/state_ai_flags.ml @@ -92,7 +92,7 @@ module Autostake = struct Tez.pp (Tez_helpers.of_mutez amount) - let apply_autostake ~name ~old_cycle + let apply_autostake (block : Block.t) ~name ~old_cycle ({ pkh; contract = _; @@ -110,7 +110,7 @@ module Autostake = struct last_active_cycle; } : account_state) state = - let open Result_syntax in + let open Lwt_result_syntax in (* TODO: use Protocol.Constants_storage.tolerated_inactivity_period *) let tolerated_inactivity_period = (2 * state.constants.consensus_rights_delay) + 1 @@ -121,84 +121,103 @@ module Autostake = struct name (Option.value ~default:"None" delegate) ; return state) - else if - Cycle.(old_cycle = add last_active_cycle tolerated_inactivity_period) - then - return - @@ update_map - ~f:(apply_unstake (Cycle.succ old_cycle) Tez.max_tez name) - state - else if - Cycle.(old_cycle > add last_active_cycle tolerated_inactivity_period) - then return state else - let* current_liquid_delegated = liquid_delegated ~name state in - let current_frozen = Frozen_tez.total_current frozen_deposits in - let current_unstaked_frozen_delegated = - Unstaked_frozen.sum_current unstaked_frozen - in - let current_unstaked_final_delegated = - Unstaked_finalizable.total unstaked_finalizable + let* ({grace_period; _} : Context.Delegate.info) = + Context.Delegate.info (B block) pkh in - let power = - Tez.( - current_liquid_delegated +! current_frozen - +! current_unstaked_frozen_delegated - +! current_unstaked_final_delegated - |> to_mutez |> Z.of_int64) + let model_grace_period = + Cycle.add last_active_cycle tolerated_inactivity_period in - let optimal = - Tez.of_z - (Z.cdiv - power - (Z.of_int (state.constants.limit_of_delegation_over_baking + 1))) - in - let autostaked = - Int64.(sub (Tez.to_mutez optimal) (Tez.to_mutez current_frozen)) - in - let state = State.apply_unslashable (Cycle.succ old_cycle) name state in - let state = State.apply_finalize name state in - (* stake or unstake *) - let new_state = - if autostaked > 0L then ( - log_model_autostake ~optimal name pkh old_cycle "stake" autostaked ; - State.apply_stake - Tez.(min liquid (of_mutez autostaked)) - (Cycle.succ old_cycle) - name - state) - else if autostaked < 0L then ( - log_model_autostake - ~optimal - name - pkh - old_cycle - "unstake" - (Int64.neg autostaked) ; - State.apply_unstake - (Cycle.succ old_cycle) - (Tez_helpers.of_mutez Int64.(neg autostaked)) - name + Log.debug + "Model Autostaking for %s: current cycle is %a, grace cycle is %a, \ + last_active_cycle is %a (grace %a)@." + name + Cycle.pp + old_cycle + Cycle.pp + grace_period + Cycle.pp + last_active_cycle + Cycle.pp + model_grace_period ; + + if Cycle.(old_cycle = model_grace_period) then ( + Log.debug "Model Autostaking: %s, deactivation -> unstaking all@." name ; + return + @@ update_map + ~f:(apply_unstake (Cycle.succ old_cycle) Tez.max_tez name) + state) + else if Cycle.(old_cycle > model_grace_period) then ( + Log.debug "Model Autostaking: %s, ignored (inactive)@." name ; + return state) + else + let*? current_liquid_delegated = liquid_delegated ~name state in + let current_frozen = Frozen_tez.total_current frozen_deposits in + let current_unstaked_frozen_delegated = + Unstaked_frozen.sum_current unstaked_frozen + in + let current_unstaked_final_delegated = + Unstaked_finalizable.total unstaked_finalizable + in + let power = + Tez.( + current_liquid_delegated +! current_frozen + +! current_unstaked_frozen_delegated + +! current_unstaked_final_delegated + |> to_mutez |> Z.of_int64) + in + let optimal = + Tez.of_z + (Z.cdiv + power + (Z.of_int (state.constants.limit_of_delegation_over_baking + 1))) + in + let autostaked = + Int64.(sub (Tez.to_mutez optimal) (Tez.to_mutez current_frozen)) + in + let state = State.apply_unslashable (Cycle.succ old_cycle) name state in + let state = State.apply_finalize name state in + (* stake or unstake *) + let new_state = + if autostaked > 0L then ( + log_model_autostake ~optimal name pkh old_cycle "stake" autostaked ; + State.apply_stake + Tez.(min liquid (of_mutez autostaked)) + (Cycle.succ old_cycle) + name + state) + else if autostaked < 0L then ( + log_model_autostake + ~optimal + name + pkh + old_cycle + "unstake" + (Int64.neg autostaked) ; + State.apply_unstake + (Cycle.succ old_cycle) + (Tez_helpers.of_mutez Int64.(neg autostaked)) + name + state) + else ( + log_model_autostake + ~optimal + name + pkh + old_cycle + "only finalize" + autostaked ; state) - else ( - log_model_autostake - ~optimal - name - pkh - old_cycle - "only finalize" - autostaked ; - state) - in - return new_state + in + return new_state let run_at_cycle_end block state = - let open Result_syntax in + let open Lwt_result_syntax in if enabled block state then let current_cycle = Block.current_cycle block in - String.Map.fold_e + String.Map.fold_es (fun name account state -> - apply_autostake ~name ~old_cycle:current_cycle account state) + apply_autostake block ~name ~old_cycle:current_cycle account state) state.account_map state else return state diff --git a/src/proto_alpha/lib_protocol/test/helpers/state_ai_flags.mli b/src/proto_alpha/lib_protocol/test/helpers/state_ai_flags.mli index 04e28933d234..af1808d26400 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/state_ai_flags.mli +++ b/src/proto_alpha/lib_protocol/test/helpers/state_ai_flags.mli @@ -40,7 +40,7 @@ module Autostake : sig (** Runs the autostake operations at cycle end. Does nothing if [enabled = false]. *) - val run_at_cycle_end : Block.t -> State.t -> State.t tzresult + val run_at_cycle_end : Block.t -> State.t -> State.t tzresult Lwt.t end module Delayed_slashing : sig -- GitLab From a9a133bb8e3e8555e8d8890596405a4ee5c4665a Mon Sep 17 00:00:00 2001 From: Julien Tesson Date: Mon, 15 Apr 2024 22:08:54 +0200 Subject: [PATCH 3/3] proto/test/helpers/scenario_op: fix current_cycle --- .../lib_protocol/test/helpers/scenario_op.ml | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) 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 fd2ab627f8a4..f37a506d3dcb 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/scenario_op.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/scenario_op.ml @@ -89,6 +89,12 @@ let transfer src_name dst_name amount : (t, t) scenarios = let state = State.apply_transfer amount src_name dst_name state in return (state, [operation])) +let current_cycle block = + (* operation will be baked in next block *) + let predecessor_cycle = Block.current_cycle block in + if Block.last_block_of_cycle block then Cycle.succ predecessor_cycle + else predecessor_cycle + (** Set delegate for src. If [delegate_name_opt = None], then unset current delegate *) let set_delegate src_name delegate_name_opt : (t, t) scenarios = exec_op (fun (block, state) -> @@ -111,7 +117,7 @@ let set_delegate src_name delegate_name_opt : (t, t) scenarios = let is_not_changing_delegate = Option.equal String.equal delegate_name_opt src.delegate in - let current_cycle = Block.current_cycle block in + let current_cycle = current_cycle block in let* operation = Op.delegation ~fee:Tez.zero (B block) src.contract delegate_pkh_opt in @@ -167,7 +173,7 @@ let stake src_name stake_value : (t, t) scenarios = (* Stake applies finalize *before* the stake *) let state = State.apply_finalize src_name state in let amount = quantity_to_tez src.liquid stake_value in - let current_cycle = Block.current_cycle block in + let current_cycle = current_cycle block in let* operation = stake (B block) src.contract amount in let state = State.apply_stake amount current_cycle src_name state in return (state, [operation])) @@ -189,7 +195,7 @@ let unstake src_name unstake_value : (t, t) scenarios = in let amount = quantity_to_tez stake_balance unstake_value in let* operation = unstake (B block) src.contract amount in - let cycle = Block.current_cycle block in + let cycle = current_cycle block in let balance = balance_of_account src_name state.account_map in let state = if Q.(equal balance.staked_b zero) then state -- GitLab