From 444d7e0225c40bc34d08ad32280dc6321a9558eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20El=20Siba=C3=AFe?= Date: Mon, 28 Aug 2023 11:01:41 +0200 Subject: [PATCH 1/3] lib_delegate: makes VDF use monitor_heads instead of applied_block --- .../lib_delegate/baking_vdf.ml | 5 ++--- .../lib_delegate/client_baking_blocks.ml | 17 ++++++++++------- .../lib_delegate/client_baking_blocks.mli | 2 +- .../lib_delegate/baking_vdf.ml | 5 ++--- .../lib_delegate/client_baking_blocks.ml | 13 +++++++------ .../lib_delegate/client_baking_blocks.mli | 2 +- src/proto_alpha/lib_delegate/baking_vdf.ml | 7 +++---- .../lib_delegate/client_baking_blocks.ml | 13 +++++++------ .../lib_delegate/client_baking_blocks.mli | 2 +- 9 files changed, 34 insertions(+), 32 deletions(-) diff --git a/src/proto_017_PtNairob/lib_delegate/baking_vdf.ml b/src/proto_017_PtNairob/lib_delegate/baking_vdf.ml index be1efccf954b..408f818d0753 100644 --- a/src/proto_017_PtNairob/lib_delegate/baking_vdf.ml +++ b/src/proto_017_PtNairob/lib_delegate/baking_vdf.ml @@ -51,11 +51,10 @@ type 'a state = { } let init_block_stream_with_stopper cctxt chain = - Client_baking_blocks.monitor_applied_blocks + Client_baking_blocks.monitor_heads ~next_protocols:(Some [Protocol.hash]) cctxt - ~chains:[chain] - () + chain let stop_block_stream state = Option.iter diff --git a/src/proto_017_PtNairob/lib_delegate/client_baking_blocks.ml b/src/proto_017_PtNairob/lib_delegate/client_baking_blocks.ml index 12b9f83b0b40..1655719777cf 100644 --- a/src/proto_017_PtNairob/lib_delegate/client_baking_blocks.ml +++ b/src/proto_017_PtNairob/lib_delegate/client_baking_blocks.ml @@ -161,14 +161,17 @@ let monitor_applied_blocks cctxt ?chains ?protocols ~next_protocols () = stop ) let monitor_heads cctxt ~next_protocols chain = - Monitor_services.heads cctxt ?next_protocols chain - >>=? fun (block_stream, _stop) -> + let open Lwt_result_syntax in + let* block_stream, stop = + Monitor_services.heads cctxt ?next_protocols chain + in return - (Lwt_stream.map_s - (fun (block, ({Tezos_base.Block_header.shell; _} as header)) -> - Block_seen_event.(Event.emit (make block header `Heads)) >>=? fun () -> - raw_info cctxt ~chain block shell) - block_stream) + ( Lwt_stream.map_s + (fun (block, ({Tezos_base.Block_header.shell; _} as header)) -> + let* () = Block_seen_event.(Event.emit (make block header `Heads)) in + raw_info cctxt ~chain block shell) + block_stream, + stop ) type error += | Unexpected_empty_block_list of { diff --git a/src/proto_017_PtNairob/lib_delegate/client_baking_blocks.mli b/src/proto_017_PtNairob/lib_delegate/client_baking_blocks.mli index 41b9f70b8619..8426c19b8994 100644 --- a/src/proto_017_PtNairob/lib_delegate/client_baking_blocks.mli +++ b/src/proto_017_PtNairob/lib_delegate/client_baking_blocks.mli @@ -57,7 +57,7 @@ val monitor_heads : #Protocol_client_context.rpc_context -> next_protocols:Protocol_hash.t list option -> Chain_services.chain -> - block_info tzresult Lwt_stream.t tzresult Lwt.t + (block_info tzresult Lwt_stream.t * Tezos_rpc.Context.stopper) tzresult Lwt.t val blocks_from_current_cycle : #Protocol_client_context.rpc_context -> diff --git a/src/proto_018_Proxford/lib_delegate/baking_vdf.ml b/src/proto_018_Proxford/lib_delegate/baking_vdf.ml index e64316cbba81..9e6699b0f942 100644 --- a/src/proto_018_Proxford/lib_delegate/baking_vdf.ml +++ b/src/proto_018_Proxford/lib_delegate/baking_vdf.ml @@ -51,11 +51,10 @@ type 'a state = { } let init_block_stream_with_stopper cctxt chain = - Client_baking_blocks.monitor_applied_blocks + Client_baking_blocks.monitor_heads ~next_protocols:(Some [Protocol.hash]) cctxt - ~chains:[chain] - () + chain let stop_block_stream state = Option.iter diff --git a/src/proto_018_Proxford/lib_delegate/client_baking_blocks.ml b/src/proto_018_Proxford/lib_delegate/client_baking_blocks.ml index dbcc764908a5..1b36a8d1cd4d 100644 --- a/src/proto_018_Proxford/lib_delegate/client_baking_blocks.ml +++ b/src/proto_018_Proxford/lib_delegate/client_baking_blocks.ml @@ -170,15 +170,16 @@ let monitor_applied_blocks cctxt ?chains ?protocols ~next_protocols () = let monitor_heads cctxt ~next_protocols chain = let open Lwt_result_syntax in - let* block_stream, _stop = + let* block_stream, stop = Monitor_services.heads cctxt ?next_protocols chain in return - (Lwt_stream.map_s - (fun (block, ({Tezos_base.Block_header.shell; _} as header)) -> - let* () = Block_seen_event.(Event.emit (make block header `Heads)) in - raw_info cctxt ~chain block shell) - block_stream) + ( Lwt_stream.map_s + (fun (block, ({Tezos_base.Block_header.shell; _} as header)) -> + let* () = Block_seen_event.(Event.emit (make block header `Heads)) in + raw_info cctxt ~chain block shell) + block_stream, + stop ) type error += | Unexpected_empty_block_list of { diff --git a/src/proto_018_Proxford/lib_delegate/client_baking_blocks.mli b/src/proto_018_Proxford/lib_delegate/client_baking_blocks.mli index 41b9f70b8619..8426c19b8994 100644 --- a/src/proto_018_Proxford/lib_delegate/client_baking_blocks.mli +++ b/src/proto_018_Proxford/lib_delegate/client_baking_blocks.mli @@ -57,7 +57,7 @@ val monitor_heads : #Protocol_client_context.rpc_context -> next_protocols:Protocol_hash.t list option -> Chain_services.chain -> - block_info tzresult Lwt_stream.t tzresult Lwt.t + (block_info tzresult Lwt_stream.t * Tezos_rpc.Context.stopper) tzresult Lwt.t val blocks_from_current_cycle : #Protocol_client_context.rpc_context -> diff --git a/src/proto_alpha/lib_delegate/baking_vdf.ml b/src/proto_alpha/lib_delegate/baking_vdf.ml index e64316cbba81..949ca818d1da 100644 --- a/src/proto_alpha/lib_delegate/baking_vdf.ml +++ b/src/proto_alpha/lib_delegate/baking_vdf.ml @@ -51,11 +51,10 @@ type 'a state = { } let init_block_stream_with_stopper cctxt chain = - Client_baking_blocks.monitor_applied_blocks - ~next_protocols:(Some [Protocol.hash]) + Client_baking_blocks.monitor_heads cctxt - ~chains:[chain] - () + ~next_protocols:(Some [Protocol.hash]) + chain let stop_block_stream state = Option.iter diff --git a/src/proto_alpha/lib_delegate/client_baking_blocks.ml b/src/proto_alpha/lib_delegate/client_baking_blocks.ml index dbcc764908a5..1b36a8d1cd4d 100644 --- a/src/proto_alpha/lib_delegate/client_baking_blocks.ml +++ b/src/proto_alpha/lib_delegate/client_baking_blocks.ml @@ -170,15 +170,16 @@ let monitor_applied_blocks cctxt ?chains ?protocols ~next_protocols () = let monitor_heads cctxt ~next_protocols chain = let open Lwt_result_syntax in - let* block_stream, _stop = + let* block_stream, stop = Monitor_services.heads cctxt ?next_protocols chain in return - (Lwt_stream.map_s - (fun (block, ({Tezos_base.Block_header.shell; _} as header)) -> - let* () = Block_seen_event.(Event.emit (make block header `Heads)) in - raw_info cctxt ~chain block shell) - block_stream) + ( Lwt_stream.map_s + (fun (block, ({Tezos_base.Block_header.shell; _} as header)) -> + let* () = Block_seen_event.(Event.emit (make block header `Heads)) in + raw_info cctxt ~chain block shell) + block_stream, + stop ) type error += | Unexpected_empty_block_list of { diff --git a/src/proto_alpha/lib_delegate/client_baking_blocks.mli b/src/proto_alpha/lib_delegate/client_baking_blocks.mli index 41b9f70b8619..8426c19b8994 100644 --- a/src/proto_alpha/lib_delegate/client_baking_blocks.mli +++ b/src/proto_alpha/lib_delegate/client_baking_blocks.mli @@ -57,7 +57,7 @@ val monitor_heads : #Protocol_client_context.rpc_context -> next_protocols:Protocol_hash.t list option -> Chain_services.chain -> - block_info tzresult Lwt_stream.t tzresult Lwt.t + (block_info tzresult Lwt_stream.t * Tezos_rpc.Context.stopper) tzresult Lwt.t val blocks_from_current_cycle : #Protocol_client_context.rpc_context -> -- GitLab From 31b6ce5ef8cda9ba3c949f31ae7abcf834785a50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20El=20Siba=C3=AFe?= Date: Mon, 28 Aug 2023 15:29:55 +0200 Subject: [PATCH 2/3] lib_delegate: check for protocol changing before checking metadata --- src/proto_017_PtNairob/lib_delegate/baking_vdf.ml | 8 ++++---- src/proto_018_Proxford/lib_delegate/baking_vdf.ml | 8 ++++---- src/proto_alpha/lib_delegate/baking_vdf.ml | 8 ++++---- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/proto_017_PtNairob/lib_delegate/baking_vdf.ml b/src/proto_017_PtNairob/lib_delegate/baking_vdf.ml index 408f818d0753..67f59f451532 100644 --- a/src/proto_017_PtNairob/lib_delegate/baking_vdf.ml +++ b/src/proto_017_PtNairob/lib_delegate/baking_vdf.ml @@ -353,10 +353,6 @@ let check_new_cycle state (level_info : Level.t) = let process_new_block (cctxt : #Protocol_client_context.full) state {hash; chain_id; protocol; next_protocol; level; _} = let open Lwt_result_syntax in - let* level_info = get_level_info cctxt level in - (* If head is in a new cycle record it in [state.cycle] and reset - * [state.computation_status] to [Not_started]. *) - let* () = check_new_cycle state level_info in if Protocol_hash.(protocol <> next_protocol) then (* If the protocol has changed, emit an event on every new block and take * no further action. It is expected that the daemon corresponding to @@ -364,6 +360,10 @@ let process_new_block (cctxt : #Protocol_client_context.full) state let*! () = Delegate_events.Denunciator.(emit protocol_change_detected) () in return_unit else + let* level_info = get_level_info cctxt level in + (* If head is in a new cycle record it in [state.cycle] and reset + * [state.computation_status] to [Not_started]. *) + let* () = check_new_cycle state level_info in (* If the chain is in the nonce revelation stage, there is nothing to do. *) let* out = is_in_nonce_revelation_stage state.constants level_info in if out then diff --git a/src/proto_018_Proxford/lib_delegate/baking_vdf.ml b/src/proto_018_Proxford/lib_delegate/baking_vdf.ml index 9e6699b0f942..c4c368f537b2 100644 --- a/src/proto_018_Proxford/lib_delegate/baking_vdf.ml +++ b/src/proto_018_Proxford/lib_delegate/baking_vdf.ml @@ -353,10 +353,6 @@ let check_new_cycle state (level_info : Level.t) = let process_new_block (cctxt : #Protocol_client_context.full) state {hash; chain_id; protocol; next_protocol; level; _} = let open Lwt_result_syntax in - let* level_info = get_level_info cctxt level in - (* If head is in a new cycle record it in [state.cycle] and reset - * [state.computation_status] to [Not_started]. *) - let* () = check_new_cycle state level_info in if Protocol_hash.(protocol <> next_protocol) then (* If the protocol has changed, emit an event on every new block and take * no further action. It is expected that the daemon corresponding to @@ -364,6 +360,10 @@ let process_new_block (cctxt : #Protocol_client_context.full) state let*! () = Delegate_events.Denunciator.(emit protocol_change_detected) () in return_unit else + let* level_info = get_level_info cctxt level in + (* If head is in a new cycle record it in [state.cycle] and reset + * [state.computation_status] to [Not_started]. *) + let* () = check_new_cycle state level_info in (* If the chain is in the nonce revelation stage, there is nothing to do. *) let* out = is_in_nonce_revelation_stage state.constants level_info in if out then diff --git a/src/proto_alpha/lib_delegate/baking_vdf.ml b/src/proto_alpha/lib_delegate/baking_vdf.ml index 949ca818d1da..003e3115c125 100644 --- a/src/proto_alpha/lib_delegate/baking_vdf.ml +++ b/src/proto_alpha/lib_delegate/baking_vdf.ml @@ -353,10 +353,6 @@ let check_new_cycle state (level_info : Level.t) = let process_new_block (cctxt : #Protocol_client_context.full) state {hash; chain_id; protocol; next_protocol; level; _} = let open Lwt_result_syntax in - let* level_info = get_level_info cctxt level in - (* If head is in a new cycle record it in [state.cycle] and reset - * [state.computation_status] to [Not_started]. *) - let* () = check_new_cycle state level_info in if Protocol_hash.(protocol <> next_protocol) then (* If the protocol has changed, emit an event on every new block and take * no further action. It is expected that the daemon corresponding to @@ -364,6 +360,10 @@ let process_new_block (cctxt : #Protocol_client_context.full) state let*! () = Delegate_events.Denunciator.(emit protocol_change_detected) () in return_unit else + let* level_info = get_level_info cctxt level in + (* If head is in a new cycle record it in [state.cycle] and reset + * [state.computation_status] to [Not_started]. *) + let* () = check_new_cycle state level_info in (* If the chain is in the nonce revelation stage, there is nothing to do. *) let* out = is_in_nonce_revelation_stage state.constants level_info in if out then -- GitLab From 8cca4ff4ef12f11525bac42c22f9a6f6ecb73e75 Mon Sep 17 00:00:00 2001 From: Victor Dumitrescu Date: Tue, 29 Aug 2023 17:39:37 +0200 Subject: [PATCH 3/3] Tezt/VDF test: override `vdf_difficulty` protocol constant Fixes introduced in !9922 also result in a speed-up of the VDF daemon, which invalidate one of its tests. Increasing this constant restores the original test case. --- tezt/tests/vdf_test.ml | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/tezt/tests/vdf_test.ml b/tezt/tests/vdf_test.ml index ab94327b2f31..8ddbed1b75b1 100644 --- a/tezt/tests/vdf_test.ml +++ b/tezt/tests/vdf_test.ml @@ -226,10 +226,17 @@ let test_vdf : Protocol.t list -> unit = * checked using [check_n_cycles] *) Protocol.register_test ~__FILE__ ~title:"VDF daemon" ~tags:["vdf"] @@ fun protocol -> - let* node = Node.init [Synchronisation_threshold 0; Private_mode] in - let* client = Client.init ~endpoint:(Node node) () in - let* () = Client.activate_protocol ~protocol client - and* vdf_baker = Vdf.init ~protocol node in + (* Override the `vdf_difficulty` constant in order to preserve the test *) + (* which checks that a computation started too late is eventually canceled. *) + let parameters = [(["vdf_difficulty"], `String "100000")] in + let* parameter_file = + Protocol.write_parameter_file ~base:(Right (protocol, None)) parameters + in + + let* node, client = + Client.init_with_protocol ~parameter_file ~protocol `Client () + in + let* vdf_baker = Vdf.init ~protocol node in (* Track whether a VDF revelation has been injected during the correct stage. * It is set to [false] at the beginning of [bake_vdf_revelation_stage] and -- GitLab