From bdb88a34bdbf60919f8c215e36121d276b9aae9c Mon Sep 17 00:00:00 2001 From: Gabriel Moise Date: Fri, 6 Jun 2025 12:33:58 +0100 Subject: [PATCH 1/4] Tezt: Double_consensus: Rename variable --- tezt/tests/double_consensus.ml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tezt/tests/double_consensus.ml b/tezt/tests/double_consensus.ml index a13c687633e7..6dcb8ffdc97e 100644 --- a/tezt/tests/double_consensus.ml +++ b/tezt/tests/double_consensus.ml @@ -81,7 +81,7 @@ let get_double_consensus_denounciation_hash protocol consensus_name client = | None -> failwith "Denunciation not found in the mempool" | Some op -> return op -let double_attestation_init +let double_consensus_init (consensus_for : ?endpoint:Client.endpoint -> ?protocol:Protocol.t -> @@ -143,7 +143,7 @@ let preattest_utils = let double_consensus_wrong_block_payload_hash (consensus_for, mk_consensus, consensus_waiter, consensus_name) protocol = let* (client, accuser), (branch, level, round, slots, _block_payload_hash) = - double_attestation_init consensus_for consensus_name protocol () + double_consensus_init consensus_for consensus_name protocol () in let* header = Client.RPC.call client @@ RPC.get_chain_block_header ~block:"head~2" () @@ -229,7 +229,7 @@ let double_preattestation_wrong_block_payload_hash = let double_consensus_wrong_branch (consensus_for, mk_consensus, consensus_waiter, consensus_name) protocol = let* (client, accuser), (_branch, level, round, slots, block_payload_hash) = - double_attestation_init consensus_for consensus_name protocol () + double_consensus_init consensus_for consensus_name protocol () in let* branch = Operation.Manager.get_branch ~offset:4 client in Log.info "Inject an invalid %s and wait for denounciation" consensus_name ; -- GitLab From be96804ef256a1df87586b4715bb0538165a4c11 Mon Sep 17 00:00:00 2001 From: Gabriel Moise Date: Fri, 6 Jun 2025 12:34:40 +0100 Subject: [PATCH 2/4] Tezt: Double_consensus: Replace Tag.layer1 with team --- tezt/tests/double_consensus.ml | 28 ++++++++-------------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/tezt/tests/double_consensus.ml b/tezt/tests/double_consensus.ml index 6dcb8ffdc97e..a44e73737a76 100644 --- a/tezt/tests/double_consensus.ml +++ b/tezt/tests/double_consensus.ml @@ -197,14 +197,7 @@ let double_attestation_wrong_block_payload_hash = ~__FILE__ ~title:"double attestation using wrong block_payload_hash" ~tags: - [ - Tag.layer1; - "double"; - "attestation"; - "accuser"; - "block_payload_hash"; - "node"; - ] + [team; "double"; "attestation"; "accuser"; "block_payload_hash"; "node"] ~uses:(fun protocol -> [Protocol.accuser protocol]) @@ fun protocol -> double_consensus_wrong_block_payload_hash attest_utils protocol @@ -215,12 +208,7 @@ let double_preattestation_wrong_block_payload_hash = ~title:"double preattestation using wrong block_payload_hash" ~tags: [ - Tag.layer1; - "double"; - "preattestation"; - "accuser"; - "block_payload_hash"; - "node"; + team; "double"; "preattestation"; "accuser"; "block_payload_hash"; "node"; ] ~uses:(fun protocol -> [Protocol.accuser protocol]) @@ fun protocol -> @@ -276,7 +264,7 @@ let double_attestation_wrong_branch = Protocol.register_test ~__FILE__ ~title:"double attestation using wrong branch" - ~tags:[Tag.layer1; "double"; "attestation"; "accuser"; "branch"; "node"] + ~tags:[team; "double"; "attestation"; "accuser"; "branch"; "node"] ~uses:(fun protocol -> [Protocol.accuser protocol]) @@ fun protocol -> double_consensus_wrong_branch attest_utils protocol @@ -284,7 +272,7 @@ let double_preattestation_wrong_branch = Protocol.register_test ~__FILE__ ~title:"double preattestation using wrong branch" - ~tags:[Tag.layer1; "double"; "preattestation"; "accuser"; "branch"; "node"] + ~tags:[team; "double"; "preattestation"; "accuser"; "branch"; "node"] ~uses:(fun protocol -> [Protocol.accuser protocol]) @@ fun protocol -> double_consensus_wrong_branch preattest_utils protocol @@ -295,7 +283,7 @@ let operation_too_old = Protocol.register_test ~__FILE__ ~title:"operation too old" - ~tags:[Tag.layer1; "accuser"; "old"; "operation"] + ~tags:[team; "accuser"; "old"; "operation"] ~uses:(fun protocol -> [Protocol.accuser protocol]) @@ fun protocol -> let* node, client = Client.init_with_protocol ~protocol `Client () in @@ -358,7 +346,7 @@ let operation_too_far_in_future = Protocol.register_test ~__FILE__ ~title:"operation too far in the future" - ~tags:[Tag.layer1; "accuser"; "future"; "operation"] + ~tags:[team; "accuser"; "future"; "operation"] ~uses:(fun protocol -> [Protocol.accuser protocol]) @@ fun protocol -> let* node, client = Client.init_with_protocol ~protocol `Client () in @@ -533,7 +521,7 @@ let attestation_and_aggregation_wrong_payload_hash = Protocol.register_test ~__FILE__ ~title:"attestation and aggregation wrong payload hash" - ~tags:[Tag.layer1; "attestation"; "aggregation"] + ~tags:[team; "attestation"; "aggregation"] ~supports:Protocol.(From_protocol 023) ~uses:(fun protocol -> [Protocol.accuser protocol]) @@ fun protocol -> @@ -642,7 +630,7 @@ let double_aggregation_wrong_payload_hash = Protocol.register_test ~__FILE__ ~title:"double aggregation wrong payload hash" - ~tags:[Tag.layer1; "double"; "aggregation"] + ~tags:[team; "double"; "aggregation"] ~supports:Protocol.(From_protocol 023) ~uses:(fun protocol -> [Protocol.accuser protocol]) @@ fun protocol -> -- GitLab From 7b705bd2cd36527fc214d9974c7f356ac49c3e6a Mon Sep 17 00:00:00 2001 From: Gabriel Moise Date: Fri, 6 Jun 2025 12:36:59 +0100 Subject: [PATCH 3/4] Tezt: Double_consensus: Add migration test for accusers --- tezt/tests/double_consensus.ml | 54 ++++++++++++++++++++++++++++++++++ tezt/tests/main.ml | 2 ++ 2 files changed, 56 insertions(+) diff --git a/tezt/tests/double_consensus.ml b/tezt/tests/double_consensus.ml index a44e73737a76..fb53f88c7f95 100644 --- a/tezt/tests/double_consensus.ml +++ b/tezt/tests/double_consensus.ml @@ -775,6 +775,60 @@ let double_aggregation_wrong_payload_hash = in unit +let accusers_migration_test ~migrate_from ~migrate_to = + let parameters = JSON.parse_file (Protocol.parameter_file migrate_to) in + (* Migration level is set arbitrarily *) + let migration_level = JSON.(get "blocks_per_cycle" parameters |> as_int) in + Test.register + ~__FILE__ + ~title: + (Format.asprintf + "accuser works correctly under migration from %s to %s" + (Protocol.tag migrate_from) + (Protocol.tag migrate_to)) + ~tags: + [team; "migration"; Protocol.tag migrate_from; Protocol.tag migrate_to] + ~uses:[Protocol.accuser migrate_from; Protocol.accuser migrate_to] + @@ fun () -> + let* client, node = + Protocol_migration.user_migratable_node_init ~migration_level ~migrate_to () + in + let* () = Client.activate_protocol ~protocol:migrate_from client in + + Log.info "Initialise accusers for the two protocols" ; + let* accuser1 = + Accuser.init ~protocol:migrate_from ~event_level:`Debug node + in + let* accuser2 = Accuser.init ~protocol:migrate_to ~event_level:`Debug node in + + Log.info + "Bake %d levels to migrate from %s to %s" + migration_level + (Protocol.tag migrate_from) + (Protocol.tag migrate_to) ; + let* () = + repeat migration_level (fun () -> Client.bake_for_and_wait client) + in + + Log.info "Bake a few more levels into the new protocol" ; + let* () = + repeat 5 (fun () -> + let* () = + Client.attest_for + ~protocol:migrate_to + ~force:true + ~key:[Constant.bootstrap1.alias] + client + in + Client.bake_for_and_wait client) + in + let* () = Accuser.terminate accuser1 in + let* () = Accuser.terminate accuser2 in + unit + +let register_migration ~migrate_from ~migrate_to = + accusers_migration_test ~migrate_from ~migrate_to + let register ~protocols = double_attestation_wrong_block_payload_hash protocols ; double_preattestation_wrong_block_payload_hash protocols ; diff --git a/tezt/tests/main.ml b/tezt/tests/main.ml index 3d2ca7be83a2..8568527d1dc2 100644 --- a/tezt/tests/main.ml +++ b/tezt/tests/main.ml @@ -69,6 +69,7 @@ let register_protocol_independent_tests () = let register_protocol_migration_tests () = let migrate_from = Option.get @@ Protocol.previous_protocol migrate_to in Agnostic_baker_test.register_migration ~migrate_from ~migrate_to ; + Double_consensus.register_migration ~migrate_from ~migrate_to ; Mockup.register_constant_migration ~migrate_from ~migrate_to ; Protocol_migration.register ~migrate_from ~migrate_to ; Weeklynet.register () ; @@ -101,6 +102,7 @@ let register_old_protocol_migration_tests () = | None, _ -> () | Some migrate_from, migrate_to -> Agnostic_baker_test.register_migration ~migrate_from ~migrate_to ; + Double_consensus.register_migration ~migrate_from ~migrate_to ; Sc_rollup_migration.register ~migrate_from ~migrate_to ; Dal.register_migration ~migrate_from ~migrate_to) Protocol.all -- GitLab From 2349288c4e38052e6852d0426eba0d3400cf7dcc Mon Sep 17 00:00:00 2001 From: Gabriel Moise Date: Fri, 6 Jun 2025 13:11:05 +0100 Subject: [PATCH 4/4] Accuser: Move next_protocol filtering to worker_loop Accuser: Move next_protocol filtering to worker_loop to stop operations processing from next_protocol --- .../lib_delegate/client_baking_blocks.ml | 4 +- .../lib_delegate/client_baking_blocks.mli | 1 - .../client_baking_denunciation.ml | 75 ++++++++++--------- .../lib_delegate/client_daemon.ml | 6 +- .../lib_delegate/delegate_events.ml | 8 ++ .../lib_delegate/client_baking_blocks.ml | 4 +- .../lib_delegate/client_baking_blocks.mli | 1 - .../client_baking_denunciation.ml | 75 ++++++++++--------- .../lib_delegate/client_daemon.ml | 6 +- .../lib_delegate/delegate_events.ml | 8 ++ .../lib_delegate/client_baking_blocks.ml | 4 +- .../lib_delegate/client_baking_blocks.mli | 1 - .../client_baking_denunciation.ml | 75 ++++++++++--------- src/proto_alpha/lib_delegate/client_daemon.ml | 6 +- .../lib_delegate/delegate_events.ml | 8 ++ tezt/tests/double_consensus.ml | 30 ++++++-- 16 files changed, 170 insertions(+), 142 deletions(-) diff --git a/src/proto_022_PsRiotum/lib_delegate/client_baking_blocks.ml b/src/proto_022_PsRiotum/lib_delegate/client_baking_blocks.ml index 18f7dd7efc61..35d43f6d97b7 100644 --- a/src/proto_022_PsRiotum/lib_delegate/client_baking_blocks.ml +++ b/src/proto_022_PsRiotum/lib_delegate/client_baking_blocks.ml @@ -152,10 +152,10 @@ module Block_seen_event = struct module Event = Internal_event.Make (Definition) end -let monitor_applied_blocks cctxt ?chains ?protocols ~next_protocols () = +let monitor_applied_blocks cctxt ?chains ?protocols () = let open Lwt_result_syntax in let* block_stream, stop = - Monitor_services.applied_blocks cctxt ?chains ?protocols ?next_protocols () + Monitor_services.applied_blocks cctxt ?chains ?protocols () in return ( Lwt_stream.map_s diff --git a/src/proto_022_PsRiotum/lib_delegate/client_baking_blocks.mli b/src/proto_022_PsRiotum/lib_delegate/client_baking_blocks.mli index 8426c19b8994..d9c50296db14 100644 --- a/src/proto_022_PsRiotum/lib_delegate/client_baking_blocks.mli +++ b/src/proto_022_PsRiotum/lib_delegate/client_baking_blocks.mli @@ -49,7 +49,6 @@ val monitor_applied_blocks : #Protocol_client_context.rpc_context -> ?chains:Chain_services.chain list -> ?protocols:Protocol_hash.t list -> - next_protocols:Protocol_hash.t list option -> unit -> (block_info tzresult Lwt_stream.t * Tezos_rpc.Context.stopper) tzresult Lwt.t diff --git a/src/proto_022_PsRiotum/lib_delegate/client_baking_denunciation.ml b/src/proto_022_PsRiotum/lib_delegate/client_baking_denunciation.ml index 1972f8c4c78f..59a6349541f1 100644 --- a/src/proto_022_PsRiotum/lib_delegate/client_baking_denunciation.ml +++ b/src/proto_022_PsRiotum/lib_delegate/client_baking_denunciation.ml @@ -536,42 +536,38 @@ let cleanup_old_operations state = - Check that every baker (pre)attested only once at the block's level and round *) let process_new_block (cctxt : #Protocol_client_context.full) state - {hash; chain_id; level; protocol; next_protocol; _} = + {hash; chain_id; level; _} = let open Lwt_result_syntax in - if Protocol_hash.(protocol <> next_protocol) then - let*! () = Events.(emit protocol_change_detected) () in - return_unit - else - let*! () = Events.(emit accuser_saw_block) (level, hash) in - let chain = `Hash chain_id in - let block = `Hash (hash, 0) in - state.highest_level_encountered <- - Raw_level.max level state.highest_level_encountered ; - (* Processing blocks *) - let* () = - let*! block_info = Alpha_block_services.info cctxt ~chain ~block () in - match block_info with - | Ok block_info -> ( - let* () = process_block cctxt state block_info in - (* Processing (pre)attestations in the block *) - match block_info.operations with - | consensus_ops :: _ -> - let packed_op {Alpha_block_services.shell; protocol_data; _} = - {shell; protocol_data} - in - process_operations cctxt state consensus_ops ~packed_op chain_id - | _ -> - (* Should not happen as a block should contain 4 lists of - operations, the first list being dedicated to consensus - operations. *) - let*! () = Events.(emit fetch_operations_error hash) in - return_unit) - | Error errs -> - let*! () = Events.(emit accuser_block_error) (hash, errs) in - return_unit - in - cleanup_old_operations state ; - return_unit + let*! () = Events.(emit accuser_saw_block) (level, hash) in + let chain = `Hash chain_id in + let block = `Hash (hash, 0) in + state.highest_level_encountered <- + Raw_level.max level state.highest_level_encountered ; + (* Processing blocks *) + let* () = + let*! block_info = Alpha_block_services.info cctxt ~chain ~block () in + match block_info with + | Ok block_info -> ( + let* () = process_block cctxt state block_info in + (* Processing (pre)attestations in the block *) + match block_info.operations with + | consensus_ops :: _ -> + let packed_op {Alpha_block_services.shell; protocol_data; _} = + {shell; protocol_data} + in + process_operations cctxt state consensus_ops ~packed_op chain_id + | _ -> + (* Should not happen as a block should contain 4 lists of + operations, the first list being dedicated to consensus + operations. *) + let*! () = Events.(emit fetch_operations_error hash) in + return_unit) + | Error errs -> + let*! () = Events.(emit accuser_block_error) (hash, errs) in + return_unit + in + cleanup_old_operations state ; + return_unit let process_new_block cctxt state bi = let open Lwt_syntax in @@ -663,8 +659,13 @@ let create (cctxt : #Protocol_client_context.full) ?canceler ~preserved_levels tzfail Baking_errors.Node_connection_lost | `Block (Some (Ok bi)) -> last_get_block := None ; - let*! () = process_new_block cctxt state bi in - worker_loop () + (* When protocol changes, we stop current accuser process. *) + if Protocol_hash.(bi.next_protocol <> Protocol.hash) then + let*! () = B_Events.(emit daemon_stop) name in + return_unit + else + let*! () = process_new_block cctxt state bi in + worker_loop () | `Operations None -> (* restart a new operations monitor stream *) last_get_ops := None ; diff --git a/src/proto_022_PsRiotum/lib_delegate/client_daemon.ml b/src/proto_022_PsRiotum/lib_delegate/client_daemon.ml index c30105c11f9d..944a5862626f 100644 --- a/src/proto_022_PsRiotum/lib_delegate/client_daemon.ml +++ b/src/proto_022_PsRiotum/lib_delegate/client_daemon.ml @@ -181,11 +181,7 @@ module Accuser = struct Protocol.hash in let* valid_blocks_stream, _ = - Client_baking_blocks.monitor_applied_blocks - ~next_protocols:(Some [Protocol.hash]) - cctxt - ~chains:[chain] - () + Client_baking_blocks.monitor_applied_blocks cctxt ~chains:[chain] () in let canceler = Lwt_canceler.create () in let _ = diff --git a/src/proto_022_PsRiotum/lib_delegate/delegate_events.ml b/src/proto_022_PsRiotum/lib_delegate/delegate_events.ml index 1efa1dd56e68..8bb0665ea09b 100644 --- a/src/proto_022_PsRiotum/lib_delegate/delegate_events.ml +++ b/src/proto_022_PsRiotum/lib_delegate/delegate_events.ml @@ -253,4 +253,12 @@ module Baking_scheduling = struct ~name:"daemon_start" ~msg:"starting {worker} daemon" ("worker", Data_encoding.string) + + let daemon_stop = + declare_1 + ~section + ~level:Notice + ~name:"daemon_stop" + ~msg:"stopping {worker} daemon" + ("worker", Data_encoding.string) end diff --git a/src/proto_023_PtSeouLo/lib_delegate/client_baking_blocks.ml b/src/proto_023_PtSeouLo/lib_delegate/client_baking_blocks.ml index 18f7dd7efc61..35d43f6d97b7 100644 --- a/src/proto_023_PtSeouLo/lib_delegate/client_baking_blocks.ml +++ b/src/proto_023_PtSeouLo/lib_delegate/client_baking_blocks.ml @@ -152,10 +152,10 @@ module Block_seen_event = struct module Event = Internal_event.Make (Definition) end -let monitor_applied_blocks cctxt ?chains ?protocols ~next_protocols () = +let monitor_applied_blocks cctxt ?chains ?protocols () = let open Lwt_result_syntax in let* block_stream, stop = - Monitor_services.applied_blocks cctxt ?chains ?protocols ?next_protocols () + Monitor_services.applied_blocks cctxt ?chains ?protocols () in return ( Lwt_stream.map_s diff --git a/src/proto_023_PtSeouLo/lib_delegate/client_baking_blocks.mli b/src/proto_023_PtSeouLo/lib_delegate/client_baking_blocks.mli index 8426c19b8994..d9c50296db14 100644 --- a/src/proto_023_PtSeouLo/lib_delegate/client_baking_blocks.mli +++ b/src/proto_023_PtSeouLo/lib_delegate/client_baking_blocks.mli @@ -49,7 +49,6 @@ val monitor_applied_blocks : #Protocol_client_context.rpc_context -> ?chains:Chain_services.chain list -> ?protocols:Protocol_hash.t list -> - next_protocols:Protocol_hash.t list option -> unit -> (block_info tzresult Lwt_stream.t * Tezos_rpc.Context.stopper) tzresult Lwt.t diff --git a/src/proto_023_PtSeouLo/lib_delegate/client_baking_denunciation.ml b/src/proto_023_PtSeouLo/lib_delegate/client_baking_denunciation.ml index 0f8d983e563c..d2b1a462feb7 100644 --- a/src/proto_023_PtSeouLo/lib_delegate/client_baking_denunciation.ml +++ b/src/proto_023_PtSeouLo/lib_delegate/client_baking_denunciation.ml @@ -493,42 +493,38 @@ let cleanup_old_operations state = - Check that every baker (pre)attested only once at the block's level and round *) let process_new_block (cctxt : #Protocol_client_context.full) state - {hash; chain_id; level; protocol; next_protocol; _} = + {hash; chain_id; level; _} = let open Lwt_result_syntax in - if Protocol_hash.(protocol <> next_protocol) then - let*! () = Events.(emit protocol_change_detected) () in - return_unit - else - let*! () = Events.(emit accuser_saw_block) (level, hash) in - let chain = `Hash chain_id in - let block = `Hash (hash, 0) in - state.highest_level_encountered <- - Raw_level.max level state.highest_level_encountered ; - (* Processing blocks *) - let* () = - let*! block_info = Alpha_block_services.info cctxt ~chain ~block () in - match block_info with - | Ok block_info -> ( - let* () = process_block cctxt state block_info in - (* Processing (pre)attestations in the block *) - match block_info.operations with - | consensus_ops :: _ -> - let packed_op {Alpha_block_services.shell; protocol_data; _} = - {shell; protocol_data} - in - process_operations cctxt state consensus_ops ~packed_op chain_id - | _ -> - (* Should not happen as a block should contain 4 lists of - operations, the first list being dedicated to consensus - operations. *) - let*! () = Events.(emit fetch_operations_error hash) in - return_unit) - | Error errs -> - let*! () = Events.(emit accuser_block_error) (hash, errs) in - return_unit - in - cleanup_old_operations state ; - return_unit + let*! () = Events.(emit accuser_saw_block) (level, hash) in + let chain = `Hash chain_id in + let block = `Hash (hash, 0) in + state.highest_level_encountered <- + Raw_level.max level state.highest_level_encountered ; + (* Processing blocks *) + let* () = + let*! block_info = Alpha_block_services.info cctxt ~chain ~block () in + match block_info with + | Ok block_info -> ( + let* () = process_block cctxt state block_info in + (* Processing (pre)attestations in the block *) + match block_info.operations with + | consensus_ops :: _ -> + let packed_op {Alpha_block_services.shell; protocol_data; _} = + {shell; protocol_data} + in + process_operations cctxt state consensus_ops ~packed_op chain_id + | _ -> + (* Should not happen as a block should contain 4 lists of + operations, the first list being dedicated to consensus + operations. *) + let*! () = Events.(emit fetch_operations_error hash) in + return_unit) + | Error errs -> + let*! () = Events.(emit accuser_block_error) (hash, errs) in + return_unit + in + cleanup_old_operations state ; + return_unit let process_new_block cctxt state bi = let open Lwt_syntax in @@ -621,8 +617,13 @@ let create (cctxt : #Protocol_client_context.full) ?canceler ~preserved_levels tzfail Baking_errors.Node_connection_lost | `Block (Some (Ok bi)) -> last_get_block := None ; - let*! () = process_new_block cctxt state bi in - worker_loop () + (* When protocol changes, we stop current accuser process. *) + if Protocol_hash.(bi.next_protocol <> Protocol.hash) then + let*! () = B_Events.(emit daemon_stop) name in + return_unit + else + let*! () = process_new_block cctxt state bi in + worker_loop () | `Operations None -> (* restart a new operations monitor stream *) last_get_ops := None ; diff --git a/src/proto_023_PtSeouLo/lib_delegate/client_daemon.ml b/src/proto_023_PtSeouLo/lib_delegate/client_daemon.ml index 1624e9be149c..1e30f06735e2 100644 --- a/src/proto_023_PtSeouLo/lib_delegate/client_daemon.ml +++ b/src/proto_023_PtSeouLo/lib_delegate/client_daemon.ml @@ -181,11 +181,7 @@ module Accuser = struct Protocol.hash in let* valid_blocks_stream, _ = - Client_baking_blocks.monitor_applied_blocks - ~next_protocols:(Some [Protocol.hash]) - cctxt - ~chains:[chain] - () + Client_baking_blocks.monitor_applied_blocks cctxt ~chains:[chain] () in let canceler = Lwt_canceler.create () in let _ = diff --git a/src/proto_023_PtSeouLo/lib_delegate/delegate_events.ml b/src/proto_023_PtSeouLo/lib_delegate/delegate_events.ml index ff89e553760a..dde15e9acbcb 100644 --- a/src/proto_023_PtSeouLo/lib_delegate/delegate_events.ml +++ b/src/proto_023_PtSeouLo/lib_delegate/delegate_events.ml @@ -275,4 +275,12 @@ module Baking_scheduling = struct ~name:"daemon_start" ~msg:"starting {worker} daemon" ("worker", Data_encoding.string) + + let daemon_stop = + declare_1 + ~section + ~level:Notice + ~name:"daemon_stop" + ~msg:"stopping {worker} daemon" + ("worker", Data_encoding.string) end diff --git a/src/proto_alpha/lib_delegate/client_baking_blocks.ml b/src/proto_alpha/lib_delegate/client_baking_blocks.ml index 18f7dd7efc61..35d43f6d97b7 100644 --- a/src/proto_alpha/lib_delegate/client_baking_blocks.ml +++ b/src/proto_alpha/lib_delegate/client_baking_blocks.ml @@ -152,10 +152,10 @@ module Block_seen_event = struct module Event = Internal_event.Make (Definition) end -let monitor_applied_blocks cctxt ?chains ?protocols ~next_protocols () = +let monitor_applied_blocks cctxt ?chains ?protocols () = let open Lwt_result_syntax in let* block_stream, stop = - Monitor_services.applied_blocks cctxt ?chains ?protocols ?next_protocols () + Monitor_services.applied_blocks cctxt ?chains ?protocols () in return ( Lwt_stream.map_s diff --git a/src/proto_alpha/lib_delegate/client_baking_blocks.mli b/src/proto_alpha/lib_delegate/client_baking_blocks.mli index 8426c19b8994..d9c50296db14 100644 --- a/src/proto_alpha/lib_delegate/client_baking_blocks.mli +++ b/src/proto_alpha/lib_delegate/client_baking_blocks.mli @@ -49,7 +49,6 @@ val monitor_applied_blocks : #Protocol_client_context.rpc_context -> ?chains:Chain_services.chain list -> ?protocols:Protocol_hash.t list -> - next_protocols:Protocol_hash.t list option -> unit -> (block_info tzresult Lwt_stream.t * Tezos_rpc.Context.stopper) tzresult Lwt.t diff --git a/src/proto_alpha/lib_delegate/client_baking_denunciation.ml b/src/proto_alpha/lib_delegate/client_baking_denunciation.ml index 0f8d983e563c..d2b1a462feb7 100644 --- a/src/proto_alpha/lib_delegate/client_baking_denunciation.ml +++ b/src/proto_alpha/lib_delegate/client_baking_denunciation.ml @@ -493,42 +493,38 @@ let cleanup_old_operations state = - Check that every baker (pre)attested only once at the block's level and round *) let process_new_block (cctxt : #Protocol_client_context.full) state - {hash; chain_id; level; protocol; next_protocol; _} = + {hash; chain_id; level; _} = let open Lwt_result_syntax in - if Protocol_hash.(protocol <> next_protocol) then - let*! () = Events.(emit protocol_change_detected) () in - return_unit - else - let*! () = Events.(emit accuser_saw_block) (level, hash) in - let chain = `Hash chain_id in - let block = `Hash (hash, 0) in - state.highest_level_encountered <- - Raw_level.max level state.highest_level_encountered ; - (* Processing blocks *) - let* () = - let*! block_info = Alpha_block_services.info cctxt ~chain ~block () in - match block_info with - | Ok block_info -> ( - let* () = process_block cctxt state block_info in - (* Processing (pre)attestations in the block *) - match block_info.operations with - | consensus_ops :: _ -> - let packed_op {Alpha_block_services.shell; protocol_data; _} = - {shell; protocol_data} - in - process_operations cctxt state consensus_ops ~packed_op chain_id - | _ -> - (* Should not happen as a block should contain 4 lists of - operations, the first list being dedicated to consensus - operations. *) - let*! () = Events.(emit fetch_operations_error hash) in - return_unit) - | Error errs -> - let*! () = Events.(emit accuser_block_error) (hash, errs) in - return_unit - in - cleanup_old_operations state ; - return_unit + let*! () = Events.(emit accuser_saw_block) (level, hash) in + let chain = `Hash chain_id in + let block = `Hash (hash, 0) in + state.highest_level_encountered <- + Raw_level.max level state.highest_level_encountered ; + (* Processing blocks *) + let* () = + let*! block_info = Alpha_block_services.info cctxt ~chain ~block () in + match block_info with + | Ok block_info -> ( + let* () = process_block cctxt state block_info in + (* Processing (pre)attestations in the block *) + match block_info.operations with + | consensus_ops :: _ -> + let packed_op {Alpha_block_services.shell; protocol_data; _} = + {shell; protocol_data} + in + process_operations cctxt state consensus_ops ~packed_op chain_id + | _ -> + (* Should not happen as a block should contain 4 lists of + operations, the first list being dedicated to consensus + operations. *) + let*! () = Events.(emit fetch_operations_error hash) in + return_unit) + | Error errs -> + let*! () = Events.(emit accuser_block_error) (hash, errs) in + return_unit + in + cleanup_old_operations state ; + return_unit let process_new_block cctxt state bi = let open Lwt_syntax in @@ -621,8 +617,13 @@ let create (cctxt : #Protocol_client_context.full) ?canceler ~preserved_levels tzfail Baking_errors.Node_connection_lost | `Block (Some (Ok bi)) -> last_get_block := None ; - let*! () = process_new_block cctxt state bi in - worker_loop () + (* When protocol changes, we stop current accuser process. *) + if Protocol_hash.(bi.next_protocol <> Protocol.hash) then + let*! () = B_Events.(emit daemon_stop) name in + return_unit + else + let*! () = process_new_block cctxt state bi in + worker_loop () | `Operations None -> (* restart a new operations monitor stream *) last_get_ops := None ; diff --git a/src/proto_alpha/lib_delegate/client_daemon.ml b/src/proto_alpha/lib_delegate/client_daemon.ml index 1624e9be149c..1e30f06735e2 100644 --- a/src/proto_alpha/lib_delegate/client_daemon.ml +++ b/src/proto_alpha/lib_delegate/client_daemon.ml @@ -181,11 +181,7 @@ module Accuser = struct Protocol.hash in let* valid_blocks_stream, _ = - Client_baking_blocks.monitor_applied_blocks - ~next_protocols:(Some [Protocol.hash]) - cctxt - ~chains:[chain] - () + Client_baking_blocks.monitor_applied_blocks cctxt ~chains:[chain] () in let canceler = Lwt_canceler.create () in let _ = diff --git a/src/proto_alpha/lib_delegate/delegate_events.ml b/src/proto_alpha/lib_delegate/delegate_events.ml index ff89e553760a..dde15e9acbcb 100644 --- a/src/proto_alpha/lib_delegate/delegate_events.ml +++ b/src/proto_alpha/lib_delegate/delegate_events.ml @@ -275,4 +275,12 @@ module Baking_scheduling = struct ~name:"daemon_start" ~msg:"starting {worker} daemon" ("worker", Data_encoding.string) + + let daemon_stop = + declare_1 + ~section + ~level:Notice + ~name:"daemon_stop" + ~msg:"stopping {worker} daemon" + ("worker", Data_encoding.string) end diff --git a/tezt/tests/double_consensus.ml b/tezt/tests/double_consensus.ml index fb53f88c7f95..d0f77292debe 100644 --- a/tezt/tests/double_consensus.ml +++ b/tezt/tests/double_consensus.ml @@ -775,6 +775,12 @@ let double_aggregation_wrong_payload_hash = in unit +let accuser_processed_block accuser = + Accuser.wait_for accuser "accuser_processed_block.v0" (fun _json -> Some ()) + +let daemon_stop accuser = + Accuser.wait_for accuser "daemon_stop.v0" (fun _json -> Some ()) + let accusers_migration_test ~migrate_from ~migrate_to = let parameters = JSON.parse_file (Protocol.parameter_file migrate_to) in (* Migration level is set arbitrarily *) @@ -799,16 +805,26 @@ let accusers_migration_test ~migrate_from ~migrate_to = let* accuser1 = Accuser.init ~protocol:migrate_from ~event_level:`Debug node in + let accuser1_processed_block = accuser_processed_block accuser1 in + let accuser1_stop = daemon_stop accuser1 in let* accuser2 = Accuser.init ~protocol:migrate_to ~event_level:`Debug node in + let accuser2_processed_block = accuser_processed_block accuser2 in + + Log.info "Bake %d levels" (migration_level - 1) ; + let* () = + repeat (migration_level - 1) (fun () -> + let* () = Client.bake_for_and_wait client in + accuser1_processed_block) + in Log.info - "Bake %d levels to migrate from %s to %s" - migration_level + "Bake one more level to migrate from %s to %s" (Protocol.tag migrate_from) (Protocol.tag migrate_to) ; - let* () = - repeat migration_level (fun () -> Client.bake_for_and_wait client) - in + let* () = Client.bake_for_and_wait client in + + Log.info "After migration, old protocol accuser should have stopped" ; + let* () = accuser1_stop in Log.info "Bake a few more levels into the new protocol" ; let* () = @@ -820,9 +836,9 @@ let accusers_migration_test ~migrate_from ~migrate_to = ~key:[Constant.bootstrap1.alias] client in - Client.bake_for_and_wait client) + let* () = Client.bake_for_and_wait client in + accuser2_processed_block) in - let* () = Accuser.terminate accuser1 in let* () = Accuser.terminate accuser2 in unit -- GitLab