From 4ea454e5b00e994019a941e87afef4236be42cb7 Mon Sep 17 00:00:00 2001 From: Gabriel Moise Date: Tue, 18 Nov 2025 16:46:38 +0000 Subject: [PATCH 1/4] Alpha/Baker: Update DAL attestable slots streams at DAL profiles registration --- src/proto_alpha/lib_delegate/baking_scheduling.ml | 15 +++++++++++++-- .../lib_delegate/baking_state_types.ml | 2 ++ .../lib_delegate/baking_state_types.mli | 3 +++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/proto_alpha/lib_delegate/baking_scheduling.ml b/src/proto_alpha/lib_delegate/baking_scheduling.ml index d3b342044bf1..8237d06f491e 100644 --- a/src/proto_alpha/lib_delegate/baking_scheduling.ml +++ b/src/proto_alpha/lib_delegate/baking_scheduling.ml @@ -958,7 +958,8 @@ let try_resolve_consensus_keys cctxt key = in try_find_delegate_key levels_to_inspect -let register_dal_profiles cctxt dal_node_rpc_ctxt delegates = +let register_dal_profiles cctxt dal_node_rpc_ctxt dal_attestable_slots_worker + delegates = let open Lwt_result_syntax in let*! delegates = List.map_s (try_resolve_consensus_keys cctxt) delegates in let register dal_ctxt = @@ -978,7 +979,16 @@ let register_dal_profiles cctxt dal_node_rpc_ctxt delegates = warn () else Lwt.return_unit in - Node_rpc.register_dal_profiles dal_ctxt delegates + let* () = Node_rpc.register_dal_profiles dal_ctxt delegates in + let delegate_ids = List.map Delegate_id.of_pkh delegates in + let*! () = + (* This is the earliest moment we know the final attesters and we have a live DAL RPC. *) + Dal_attestable_slots_worker.update_streams_subscriptions + dal_attestable_slots_worker + dal_ctxt + ~delegate_ids + in + return_unit in Option.iter_es (fun dal_ctxt -> @@ -1050,6 +1060,7 @@ let run cctxt ?dal_node_rpc_ctxt ?canceler ?(stop_on_event = fun _ -> false) register_dal_profiles cctxt initial_state.global_state.dal_node_rpc_ctxt + dal_attestable_slots_worker delegates in let cloned_block_stream = Lwt_stream.clone heads_stream in diff --git a/src/proto_alpha/lib_delegate/baking_state_types.ml b/src/proto_alpha/lib_delegate/baking_state_types.ml index 64c76c254df2..3feb824ccc69 100644 --- a/src/proto_alpha/lib_delegate/baking_state_types.ml +++ b/src/proto_alpha/lib_delegate/baking_state_types.ml @@ -11,6 +11,8 @@ open Alpha_context module Key_id = struct type t = Signature.Public_key_hash.t + let of_pkh pkh = pkh + let to_pkh pkh = pkh let compare = Signature.Public_key_hash.compare diff --git a/src/proto_alpha/lib_delegate/baking_state_types.mli b/src/proto_alpha/lib_delegate/baking_state_types.mli index 7e5492a9c879..7ecc41bb7326 100644 --- a/src/proto_alpha/lib_delegate/baking_state_types.mli +++ b/src/proto_alpha/lib_delegate/baking_state_types.mli @@ -12,6 +12,9 @@ open Alpha_context module Key_id : sig type t + (** Only use at library frontiers *) + val of_pkh : Signature.public_key_hash -> t + (** Only use at library frontiers *) val to_pkh : t -> Signature.public_key_hash -- GitLab From 4696a3a1c442e9c0174010cfa113eab1623d972a Mon Sep 17 00:00:00 2001 From: Gabriel Moise Date: Tue, 18 Nov 2025 16:47:30 +0000 Subject: [PATCH 2/4] Alpha/Baker: Update DAL attestable slots streams when preparing next level --- .../lib_delegate/baking_actions.ml | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/proto_alpha/lib_delegate/baking_actions.ml b/src/proto_alpha/lib_delegate/baking_actions.ml index 09ea9be15015..215791718e03 100644 --- a/src/proto_alpha/lib_delegate/baking_actions.ml +++ b/src/proto_alpha/lib_delegate/baking_actions.ml @@ -1148,7 +1148,7 @@ let update_to_level state level_update = in Lwt.return (dal_attestable_slots, next_level_dal_attestable_slots)) in - let*! new_state = + let*! new_state, new_action = (compute_new_state ~current_round ~delegate_infos @@ -1157,7 +1157,24 @@ let update_to_level state level_update = ~next_level_dal_attestable_slots [@profiler.record_s {verbosity = Debug} "compute new state"]) in - return new_state + let _promise = + only_if_dal_feature_enabled + new_state + ~default_value:() + (fun dal_node_rpc_ctxt -> + let next_level_delegate_ids = + Baking_state.Delegate_infos.own_delegate_ids next_level_delegate_infos + in + (* Refresh per-delegate subscriptions for the next level. Rights/committee + can change at level boundaries (e.g. migrations, reorganisations, key/profile + updates). Doing it here makes the streams ready before we build next-level + attestations. *) + Dal_attestable_slots_worker.update_streams_subscriptions + state.global_state.dal_attestable_slots_worker + dal_node_rpc_ctxt + ~delegate_ids:next_level_delegate_ids) + in + return (new_state, new_action) let synchronize_round state {new_round_proposal; handle_proposal} = let open Lwt_result_syntax in -- GitLab From 4fc5a3cc98eaaec52322902447ef2d77c269d0c9 Mon Sep 17 00:00:00 2001 From: Gabriel Moise Date: Tue, 18 Nov 2025 16:50:52 +0000 Subject: [PATCH 3/4] Alpha/Baker: Update DAL attestable slots streams in bake command --- .../lib_delegate/baking_actions.mli | 6 +++ src/proto_alpha/lib_delegate/baking_lib.ml | 47 ++++++++++++++----- .../lib_delegate/baking_scheduling.mli | 3 ++ 3 files changed, 44 insertions(+), 12 deletions(-) diff --git a/src/proto_alpha/lib_delegate/baking_actions.mli b/src/proto_alpha/lib_delegate/baking_actions.mli index ef90615a0ba6..67d32b2d79e2 100644 --- a/src/proto_alpha/lib_delegate/baking_actions.mli +++ b/src/proto_alpha/lib_delegate/baking_actions.mli @@ -141,6 +141,12 @@ val inject_consensus_votes : and updates the state accordingly. *) val update_to_level : state -> level_update -> (state * t) tzresult Lwt.t +val only_if_dal_feature_enabled : + state -> + default_value:'a -> + (Tezos_rpc.Context.generic -> 'a Lwt.t) -> + 'a Lwt.t + (** [may_get_dal_content state unsigned_consensus_vote], if the DAL feature is enabled, recovers the attestable slots by calling [Node_rpc.get_attestable_slots] and computes the corresponding [dal_content]. *) diff --git a/src/proto_alpha/lib_delegate/baking_lib.ml b/src/proto_alpha/lib_delegate/baking_lib.ml index ef3cbd809b42..47283cc4a62c 100644 --- a/src/proto_alpha/lib_delegate/baking_lib.ml +++ b/src/proto_alpha/lib_delegate/baking_lib.ml @@ -72,18 +72,41 @@ let create_state cctxt ?dal_node_rpc_ctxt ?synchronize ?monitor_node_mempool ~attestation_lag:constants.parametric.dal.attestation_lag ~number_of_slots:constants.parametric.dal.number_of_slots in - Baking_scheduling.create_initial_state - cctxt - ?dal_node_rpc_ctxt - ?synchronize - ~chain - config - operation_worker - dal_attestable_slots_worker - round_durations - ~current_proposal - ~constants - delegates + let* state = + Baking_scheduling.create_initial_state + cctxt + ?dal_node_rpc_ctxt + ?synchronize + ~chain + config + operation_worker + dal_attestable_slots_worker + round_durations + ~current_proposal + ~constants + delegates + in + let*! () = + Baking_actions.only_if_dal_feature_enabled + state + ~default_value:() + (fun dal_node_rpc_ctxt -> + let*! delegates = + List.map_s + (Baking_scheduling.try_resolve_consensus_keys cctxt) + delegates + in + let delegate_ids = + List.map Baking_state_types.Delegate_id.of_pkh delegates + in + (* Ensures the DAL attestable slots cache is populated in time for the + first block’s attestation. *) + Dal_attestable_slots_worker.update_streams_subscriptions + state.global_state.dal_attestable_slots_worker + dal_node_rpc_ctxt + ~delegate_ids) + in + return state let get_current_proposal cctxt ?cache () = let open Lwt_result_syntax in diff --git a/src/proto_alpha/lib_delegate/baking_scheduling.mli b/src/proto_alpha/lib_delegate/baking_scheduling.mli index 81d2ace576c2..1004594f620c 100644 --- a/src/proto_alpha/lib_delegate/baking_scheduling.mli +++ b/src/proto_alpha/lib_delegate/baking_scheduling.mli @@ -52,6 +52,9 @@ val retry : 'a -> 'b tzresult Lwt.t +val try_resolve_consensus_keys : + #Protocol_client_context.rpc_context -> Key.t -> public_key_hash Lwt.t + (** [run context ?canceler ?stop_on_event ?on_error ?constants chain baking_configuration consensus_keys] is the entry point of the baker automaton. This function performs the following tasks: -- GitLab From 850442e0099c50ea1955919805faa19ca5e39006 Mon Sep 17 00:00:00 2001 From: Gabriel Moise Date: Tue, 18 Nov 2025 17:00:26 +0000 Subject: [PATCH 4/4] Alpha/Baker: Fix migration test consensus key assertion With the consensus keys resolution in the baking_lib create state function, the assertion no longer holds. --- src/proto_alpha/lib_delegate/baking_scheduling.ml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/proto_alpha/lib_delegate/baking_scheduling.ml b/src/proto_alpha/lib_delegate/baking_scheduling.ml index 8237d06f491e..d943c919259b 100644 --- a/src/proto_alpha/lib_delegate/baking_scheduling.ml +++ b/src/proto_alpha/lib_delegate/baking_scheduling.ml @@ -932,7 +932,7 @@ let try_resolve_consensus_keys cctxt key = () in match attesting_rights with - | Error _ | Ok [Plugin.RPC.Validators.{delegates = []; _}] -> + | Error _ | Ok [Plugin.RPC.Validators.{delegates = []; _}] | Ok [] -> try_find_delegate_key (head_offset - 1) | Ok Plugin.RPC.Validators. @@ -953,7 +953,7 @@ let try_resolve_consensus_keys cctxt key = ] -> (* The primary registered key as delegate found. Return it. *) return delegate - | Ok (_ :: _ :: _) | Ok [] -> assert false + | Ok (_ :: _ :: _) -> assert false (* we query only one level, the returned list length must be 1 *) in try_find_delegate_key levels_to_inspect -- GitLab