From 4ea027e45f79e09785b8fe803ad48020d78704c9 Mon Sep 17 00:00:00 2001 From: Julien Tesson Date: Mon, 15 Apr 2024 21:37:02 +0200 Subject: [PATCH 1/5] proto/test/helpers/block: expose last_level_of_cycle --- src/proto_alpha/lib_protocol/test/helpers/block.ml | 10 ++++++---- src/proto_alpha/lib_protocol/test/helpers/block.mli | 2 ++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/proto_alpha/lib_protocol/test/helpers/block.ml b/src/proto_alpha/lib_protocol/test/helpers/block.ml index 1a864b4d12dc..475bdc0f5a55 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/block.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/block.ml @@ -1264,12 +1264,14 @@ let current_cycle b = let current_level = b.header.shell.level in current_cycle_of_level ~blocks_per_cycle ~current_level -let last_block_of_cycle b = - let blocks_per_cycle = b.constants.blocks_per_cycle in - let current_level = b.header.shell.level in - let mod_plus_one = Int32.(rem (succ current_level) blocks_per_cycle) in +let last_level_of_cycle (constants : Constants.Parametric.t) ~level = + let blocks_per_cycle = constants.blocks_per_cycle in + let mod_plus_one = Int32.(rem (succ level) blocks_per_cycle) in Int32.(equal mod_plus_one zero) +let last_block_of_cycle b = + last_level_of_cycle b.constants ~level:b.header.shell.level + let bake_until_cycle ?baking_mode ?policy cycle (b : t) = let open Lwt_result_syntax in let* final_block_of_previous_cycle = diff --git a/src/proto_alpha/lib_protocol/test/helpers/block.mli b/src/proto_alpha/lib_protocol/test/helpers/block.mli index 9a07fc4fe371..e595b1e18b60 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/block.mli +++ b/src/proto_alpha/lib_protocol/test/helpers/block.mli @@ -356,6 +356,8 @@ val current_cycle_of_level : val current_cycle : block -> Cycle.t +val last_level_of_cycle : Constants.Parametric.t -> level:int32 -> bool + val last_block_of_cycle : block -> bool (** Given a block [b] at level [l] bakes enough blocks to complete a cycle, -- GitLab From 42fef51a3dbb681efdd249bc7c1ad39e94dbe5f0 Mon Sep 17 00:00:00 2001 From: Julien Tesson Date: Mon, 15 Apr 2024 23:45:29 +0200 Subject: [PATCH 2/5] proto/test/helpers: update activity at right cycle --- .../lib_protocol/test/helpers/account_helpers.ml | 5 +++++ .../lib_protocol/test/helpers/scenario_op.ml | 13 ++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) 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 ee6ce8b9fb4b..0525211984e3 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/account_helpers.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/account_helpers.ml @@ -539,3 +539,8 @@ let current_total_frozen_deposits_with_limits account_state = ~limit_of_staking_over_baking: account_state.parameters.limit_of_staking_over_baking account_state.frozen_deposits + +let update_activity account constants level current_cycle = + if not (Block.last_level_of_cycle constants ~level) then + {account with last_active_cycle = current_cycle} + else {account with last_active_cycle = Cycle.succ current_cycle} 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 aec95c26a42e..11629127e708 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/scenario_op.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/scenario_op.ml @@ -127,6 +127,13 @@ let set_delegate src_name delegate_name_opt : (t, t) scenarios = State.apply_finalize src_name state in let state = State.update_delegate src_name delegate_name_opt state in + let pred_level = block.header.shell.level in + let level = Int32.(succ pred_level) in + let activity_cycle = + Block.current_cycle_of_level + ~blocks_per_cycle:state.constants.blocks_per_cycle + ~current_level:level + in (* update delegate activation status *) let state = (* if self delegating *) @@ -135,7 +142,11 @@ let set_delegate src_name delegate_name_opt : (t, t) scenarios = ~f:(fun acc_map -> String.Map.add src_name - {src with last_active_cycle = current_cycle} + (Account_helpers.update_activity + src + state.constants + level + activity_cycle) acc_map) state else state -- GitLab From 5aed96522841ceef5b3a17925931621a810e30a0 Mon Sep 17 00:00:00 2001 From: Julien Tesson Date: Tue, 16 Apr 2024 14:53:35 +0200 Subject: [PATCH 3/5] proto/test/helpers: update baker activity on the right block --- .../test/helpers/scenario_bake.ml | 27 +++++++++++-------- 1 file changed, 16 insertions(+), 11 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 88a2ea6fc4c0..4978b402b44a 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/scenario_bake.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/scenario_bake.ml @@ -236,20 +236,10 @@ let bake ?baker : t -> t tzresult Lwt.t = Some (Block.By_account pkh) in let* baker, _, _, _ = Block.get_next_baker ?policy block in - let baker_name, ({contract = baker_contract; _} as baker_acc) = + let baker_name, {contract = baker_contract; _} = State.find_account_from_pkh baker state in let current_cycle = Block.current_cycle block in - (* update baker activity *) - let state = - State.update_map - ~f:(fun acc_map -> - String.Map.add - baker_name - {baker_acc with last_active_cycle = current_cycle} - acc_map) - state - in let* level = Plugin.RPC.current_level Block.rpc_ctxt block in assert (Protocol.Alpha_context.Cycle.(level.cycle = Block.current_cycle block)) ; Log.info @@ -290,6 +280,21 @@ let bake ?baker : t -> t tzresult Lwt.t = return (block, state) else return (block', state) in + let baker_acc = State.find_account baker_name state in + (* update baker activity *) + let update_activity delegate_account = + Account_helpers.update_activity + delegate_account + state.constants + block.header.shell.level + (Block.current_cycle block) + in + let state = + State.update_map + ~f:(fun acc_map -> + String.Map.add baker_name (update_activity baker_acc) acc_map) + state + in let* state = State_ai_flags.AI_Activation.check_activation_cycle block state in -- GitLab From fcc3c9adb360d5f233af7b3223204baef58a5261 Mon Sep 17 00:00:00 2001 From: Julien Tesson Date: Tue, 16 Apr 2024 14:54:33 +0200 Subject: [PATCH 4/5] proto/test/helpers: use named parameter --- src/proto_alpha/lib_protocol/test/helpers/account_helpers.ml | 2 +- src/proto_alpha/lib_protocol/test/helpers/scenario_bake.ml | 2 +- src/proto_alpha/lib_protocol/test/helpers/scenario_op.ml | 2 +- 3 files changed, 3 insertions(+), 3 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 0525211984e3..2378e47d5322 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/account_helpers.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/account_helpers.ml @@ -540,7 +540,7 @@ let current_total_frozen_deposits_with_limits account_state = account_state.parameters.limit_of_staking_over_baking account_state.frozen_deposits -let update_activity account constants level current_cycle = +let update_activity account constants ~level current_cycle = if not (Block.last_level_of_cycle constants ~level) then {account with last_active_cycle = current_cycle} else {account with last_active_cycle = Cycle.succ current_cycle} 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 4978b402b44a..3ee34ff26463 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/scenario_bake.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/scenario_bake.ml @@ -286,7 +286,7 @@ let bake ?baker : t -> t tzresult Lwt.t = Account_helpers.update_activity delegate_account state.constants - block.header.shell.level + ~level:block.header.shell.level (Block.current_cycle block) in let state = 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 11629127e708..fd2ab627f8a4 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/scenario_op.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/scenario_op.ml @@ -145,7 +145,7 @@ let set_delegate src_name delegate_name_opt : (t, t) scenarios = (Account_helpers.update_activity src state.constants - level + ~level activity_cycle) acc_map) state -- GitLab From 2db9cde1e3249b2cfe805a4e432b450c23daf543 Mon Sep 17 00:00:00 2001 From: Julien Tesson Date: Tue, 16 Apr 2024 14:55:48 +0200 Subject: [PATCH 5/5] proto/test/helpers: count attestations in model's delegate activity --- .../test/helpers/scenario_bake.ml | 35 +++++++++++++++++-- 1 file changed, 33 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 3ee34ff26463..bed3a210094d 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/scenario_bake.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/scenario_bake.ml @@ -281,7 +281,7 @@ let bake ?baker : t -> t tzresult Lwt.t = else return (block', state) in let baker_acc = State.find_account baker_name state in - (* update baker activity *) + (* update baker and attesters activity *) let update_activity delegate_account = Account_helpers.update_activity delegate_account @@ -289,10 +289,41 @@ let bake ?baker : t -> t tzresult Lwt.t = ~level:block.header.shell.level (Block.current_cycle block) in + let* attesters = + let open Tezos_raw_protocol_alpha.Alpha_context in + let* ctxt = Context.get_alpha_ctxt ?policy (B previous_block) in + List.filter_map_es + (fun op -> + let ({protocol_data = Operation_data protocol_data; _} + : packed_operation) = + op + in + match protocol_data.contents with + | Single (Attestation {consensus_content; _}) -> + let*@ _, owner = + Stake_distribution.slot_owner + ctxt + (Level.from_raw ctxt consensus_content.level) + consensus_content.slot + in + return_some owner.delegate + | _ -> return_none) + operations + in let state = State.update_map ~f:(fun acc_map -> - String.Map.add baker_name (update_activity baker_acc) acc_map) + let acc_map = + String.Map.add baker_name (update_activity baker_acc) acc_map + in + List.fold_left + (fun acc_map delegate_pkh -> + let delegate_name, delegate_acc = + State.find_account_from_pkh delegate_pkh state + in + String.Map.add delegate_name (update_activity delegate_acc) acc_map) + acc_map + attesters) state in let* state = -- GitLab