diff --git a/devtools/testnet_experiment_tools/tool_alpha.ml b/devtools/testnet_experiment_tools/tool_alpha.ml index 71d9ef85d41880db2df5b92518f1abba74bc4844..55833eaf0edbe46bba646e23ed2cf7da563642ad 100644 --- a/devtools/testnet_experiment_tools/tool_alpha.ml +++ b/devtools/testnet_experiment_tools/tool_alpha.ml @@ -207,12 +207,11 @@ let load_client_context (cctxt : ctxt_kind) = let get_delegates (cctxt : Protocol_client_context.full) = let proj_delegate (alias, public_key_hash, public_key, secret_key_uri) = - { - Baking_state.alias = Some alias; - public_key_hash; - public_key; - secret_key_uri; - } + Baking_state.Consensus_key.make + ~alias:(Some alias) + ~public_key_hash + ~public_key + ~secret_key_uri in let* keys = Client_keys.get_keys cctxt in let delegates = List.map proj_delegate keys in @@ -222,7 +221,8 @@ let get_delegates (cctxt : Protocol_client_context.full) = cctxt (List.filter_map (function - | {Baking_state.alias = Some alias; _} -> Some alias | _ -> None) + | {Baking_state.Consensus_key.alias = Some alias; _} -> Some alias + | _ -> None) delegates) in let delegates_no_duplicates = List.sort_uniq compare delegates in diff --git a/src/proto_alpha/lib_delegate/baking_actions.ml b/src/proto_alpha/lib_delegate/baking_actions.ml index 6cf02772ea6d39c41f8a24f390892d98c0f93758..71db50334edd66c3fa70b149478a14e34675abb7 100644 --- a/src/proto_alpha/lib_delegate/baking_actions.ml +++ b/src/proto_alpha/lib_delegate/baking_actions.ml @@ -247,7 +247,7 @@ let sign_block_header global_state proposer unsigned_block_header = (may_sign_block cctxt block_location - ~delegate:proposer.public_key_hash + ~delegate:proposer.Consensus_key.id ~level ~round [@profiler.record_s {verbosity = Debug} "may sign"]) in @@ -257,7 +257,7 @@ let sign_block_header global_state proposer unsigned_block_header = (record_block cctxt block_location - ~delegate:proposer.public_key_hash + ~delegate:proposer.id ~level ~round [@profiler.record_s {verbosity = Debug} "record block"]) in @@ -284,15 +284,7 @@ let sign_block_header global_state proposer unsigned_block_header = let prepare_block (global_state : global_state) (block_to_bake : block_to_bake) = let open Lwt_result_syntax in - let { - predecessor; - round; - delegate = (consensus_key, _) as delegate; - kind; - force_apply; - } = - block_to_bake - in + let {predecessor; round; delegate; kind; force_apply} = block_to_bake in let*! () = Events.( emit @@ -351,7 +343,7 @@ let prepare_block (global_state : global_state) (block_to_bake : block_to_bake) (generate_seed_nonce_hash ?timeout:global_state.config.remote_calls_timeout global_state.config.Baking_configuration.nonce - consensus_key + delegate.consensus_key injection_level [@profiler.record_s {verbosity = Info} "generate seed nonce hash"]) in @@ -423,7 +415,7 @@ let prepare_block (global_state : global_state) (block_to_bake : block_to_bake) let* signed_block_header = (sign_block_header global_state - consensus_key + delegate.consensus_key unsigned_block_header [@profiler.record_s {verbosity = Info} "sign block header"]) in @@ -521,17 +513,15 @@ let process_dal_rpc_result state delegate level round = let may_get_dal_content state consensus_vote = let open Lwt_result_syntax in - let {delegate = _consensus_key, pkh; vote_consensus_content; _} = - consensus_vote - in + let {delegate; vote_consensus_content; _} = consensus_vote in let level, round = ( Raw_level.to_int32 vote_consensus_content.level, vote_consensus_content.round ) in let promise_opt = List.assoc_opt - ~equal:Signature.Public_key_hash.equal - pkh + ~equal:Delegate_id.equal + delegate.delegate_id state.level_state.dal_attestable_slots in match promise_opt with @@ -551,12 +541,10 @@ let may_get_dal_content state consensus_vote = Lwt.return (`RPC_result tz_res)); ] in - process_dal_rpc_result state pkh level round res + process_dal_rpc_result state delegate.delegate_id level round res let is_authorized (global_state : global_state) highwatermarks consensus_vote = - let {delegate = consensus_key, _; vote_consensus_content; _} = - consensus_vote - in + let {delegate; vote_consensus_content; _} = consensus_vote in let level, round = ( Raw_level.to_int32 vote_consensus_content.level, vote_consensus_content.round ) @@ -566,13 +554,13 @@ let is_authorized (global_state : global_state) highwatermarks consensus_vote = | Preattestation -> Baking_highwatermarks.may_sign_preattestation highwatermarks - ~delegate:consensus_key.public_key_hash + ~delegate:delegate.consensus_key.id ~level ~round | Attestation -> Baking_highwatermarks.may_sign_attestation highwatermarks - ~delegate:consensus_key.public_key_hash + ~delegate:delegate.consensus_key.id ~level ~round in @@ -610,8 +598,8 @@ let authorized_consensus_votes global_state (* Record all consensus votes new highwatermarks as one batch *) let delegates = List.map - (fun ({delegate = ck, _; _} : unsigned_consensus_vote) -> - ck.public_key_hash) + (fun ({delegate; _} : unsigned_consensus_vote) -> + delegate.consensus_key.id) authorized_votes in let record_all_consensus_vote = @@ -654,7 +642,7 @@ let forge_and_sign_consensus_vote global_state ~branch unsigned_consensus_vote : let open Lwt_result_syntax in let cctxt = global_state.cctxt in let chain_id = global_state.chain_id in - let {vote_kind; vote_consensus_content; delegate = ck, _; dal_content} = + let {vote_kind; vote_consensus_content; delegate; dal_content} = unsigned_consensus_vote in let shell = {Tezos_base.Operation.branch} in @@ -684,7 +672,7 @@ let forge_and_sign_consensus_vote global_state ~branch unsigned_consensus_vote : Operation.unsigned_encoding unsigned_operation in - let sk_uri = ck.secret_key_uri in + let sk_uri = delegate.consensus_key.secret_key_uri in let* signature = sign ?timeout:global_state.config.remote_calls_timeout @@ -953,13 +941,11 @@ let compute_round (proposal : proposal) round_durations = let notice_delegates_without_slots all_delegates delegate_slots level = let delegates_without_slots = List.filter - (fun {Baking_state.public_key_hash; _} -> + (fun {Baking_state.Consensus_key.id; _} -> not @@ List.exists - (fun { - consensus_key_and_delegate = consensus_key, _pkh_with_rights; - _; - } -> public_key_hash = consensus_key.public_key_hash) + (fun ({delegate = {consensus_key; _}; _} : delegate_slot) -> + id = consensus_key.id) (Baking_state.Delegate_slots.own_delegates delegate_slots)) all_delegates in diff --git a/src/proto_alpha/lib_delegate/baking_commands.ml b/src/proto_alpha/lib_delegate/baking_commands.ml index 7247560dd706160ad5e4ee736911d02a525b5b4f..11b696e3602c8c7fe2ad61da3173c400ddf05156 100644 --- a/src/proto_alpha/lib_delegate/baking_commands.ml +++ b/src/proto_alpha/lib_delegate/baking_commands.ml @@ -305,12 +305,11 @@ let get_delegates (cctxt : Protocol_client_context.full) (pkhs : Signature.public_key_hash list) = let open Lwt_result_syntax in let proj_delegate (alias, public_key_hash, public_key, secret_key_uri) = - { - Baking_state.alias = Some alias; - public_key_hash; - public_key; - secret_key_uri; - } + Baking_state.Consensus_key.make + ~alias:(Some alias) + ~public_key_hash + ~public_key + ~secret_key_uri in let* delegates = if pkhs = [] then @@ -329,7 +328,8 @@ let get_delegates (cctxt : Protocol_client_context.full) cctxt (List.filter_map (function - | {Baking_state.alias = Some alias; _} -> Some alias | _ -> None) + | {Baking_state.Consensus_key.alias = Some alias; _} -> Some alias + | _ -> None) delegates) in let delegates_no_duplicates = List.sort_uniq compare delegates in diff --git a/src/proto_alpha/lib_delegate/baking_events.ml b/src/proto_alpha/lib_delegate/baking_events.ml index 17c641c7e60aa53a27ed1b0542f36e27f88849d9..b2c9ac81fc37cb23a3326585bb7701ec9348062e 100644 --- a/src/proto_alpha/lib_delegate/baking_events.ml +++ b/src/proto_alpha/lib_delegate/baking_events.ml @@ -189,8 +189,8 @@ module State_transitions = struct ("level", Data_encoding.int32) ~pp3:Round.pp ("next_round", Round.encoding) - ~pp4:Baking_state.pp_consensus_key_and_delegate - ("delegate", Baking_state.consensus_key_and_delegate_encoding) + ~pp4:Baking_state.Delegate.pp + ("delegate", Baking_state.Delegate.encoding) let new_head_while_waiting_for_qc = declare_0 @@ -385,8 +385,8 @@ module State_transitions = struct ~name:"preparing_fresh_block" ~level:Info ~msg:"preparing fresh block for {delegate} at round {round}" - ~pp1:Baking_state.pp_consensus_key_and_delegate - ("delegate", Baking_state.consensus_key_and_delegate_encoding) + ~pp1:Baking_state.Delegate.pp + ("delegate", Baking_state.Delegate.encoding) ~pp2:Round.pp ("round", Round.encoding) @@ -452,8 +452,8 @@ module State_transitions = struct ~msg: "discarding outdated preattestation for {delegate} at level {level}, \ round {round}" - ~pp1:Baking_state.pp_consensus_key_and_delegate - ("delegate", Baking_state.consensus_key_and_delegate_encoding) + ~pp1:Baking_state.Delegate.pp + ("delegate", Baking_state.Delegate.encoding) ~pp2:pp_int32 ("level", Data_encoding.int32) ~pp3:Round.pp @@ -467,8 +467,8 @@ module State_transitions = struct ~msg: "discarding outdated attestation for {delegate} at level {level}, \ round {round}" - ~pp1:Baking_state.pp_consensus_key_and_delegate - ("delegate", Baking_state.consensus_key_and_delegate_encoding) + ~pp1:Baking_state.Delegate.pp + ("delegate", Baking_state.Delegate.encoding) ~pp2:pp_int32 ("level", Data_encoding.int32) ~pp3:Round.pp @@ -483,8 +483,8 @@ module State_transitions = struct "discarding preattestation for {delegate} with payload {payload} at \ level {level}, round {round} where the prequorum was locked on a \ different payload {state_payload}." - ~pp1:Baking_state.pp_consensus_key_and_delegate - ("delegate", Baking_state.consensus_key_and_delegate_encoding) + ~pp1:Baking_state.Delegate.pp + ("delegate", Baking_state.Delegate.encoding) ~pp2:Block_payload_hash.pp ("payload", Block_payload_hash.encoding) ~pp3:pp_int32 @@ -502,8 +502,8 @@ module State_transitions = struct ~msg: "discarding attestation for {delegate} at level {level}, round {round} \ where no prequorum was reached." - ~pp1:Baking_state.pp_consensus_key_and_delegate - ("delegate", Baking_state.consensus_key_and_delegate_encoding) + ~pp1:Baking_state.Delegate.pp + ("delegate", Baking_state.Delegate.encoding) ~pp2:pp_int32 ("level", Data_encoding.int32) ~pp3:Round.pp @@ -518,8 +518,8 @@ module State_transitions = struct "discarding attestation for {delegate} with payload {payload} at level \ {level}, round {round} where the prequorum was on a different payload \ {state_payload}." - ~pp1:Baking_state.pp_consensus_key_and_delegate - ("delegate", Baking_state.consensus_key_and_delegate_encoding) + ~pp1:Baking_state.Delegate.pp + ("delegate", Baking_state.Delegate.encoding) ~pp2:Block_payload_hash.pp ("payload", Block_payload_hash.encoding) ~pp3:pp_int32 @@ -594,9 +594,9 @@ module Delegates = struct ~msg:"Baker will run with the following delegates:\n {delegates}" ~pp1: (Format.pp_print_list - (fun fmt (delegate : Baking_state.consensus_key) -> - Format.fprintf fmt "%a" Baking_state.pp_consensus_key delegate)) - ("delegates", Data_encoding.list Baking_state.consensus_key_encoding) + (fun fmt (delegate : Baking_state.Consensus_key.t) -> + Format.fprintf fmt "%a" Baking_state.Consensus_key.pp delegate)) + ("delegates", Data_encoding.list Baking_state.Consensus_key.encoding) end module Scheduling = struct @@ -657,8 +657,8 @@ module Scheduling = struct ("round", Round.encoding) ~pp3:Timestamp.pp ("timestamp", Timestamp.encoding) - ~pp4:Baking_state.pp_consensus_key_and_delegate - ("delegate", Baking_state.consensus_key_and_delegate_encoding) + ~pp4:Baking_state.Delegate.pp + ("delegate", Baking_state.Delegate.encoding) let waiting_end_of_round = declare_3 @@ -826,8 +826,8 @@ module Actions = struct {round} -- {trace}" ~pp1:Baking_state.pp_consensus_vote_kind ("vote_kind", Baking_state.consensus_vote_kind_encoding) - ~pp2:Baking_state.pp_consensus_key_and_delegate - ("delegate", Baking_state.consensus_key_and_delegate_encoding) + ~pp2:Baking_state.Delegate.pp + ("delegate", Baking_state.Delegate.encoding) ~pp3:pp_int32 ("level", Data_encoding.int32) ~pp4:Round.pp @@ -841,7 +841,7 @@ module Actions = struct ~name:"failed_to_get_attestations" ~level:Error ~msg:"unable to get DAL attestation for {delegate} -- {trace}" - ("delegate", Signature.Public_key_hash.encoding) + ("delegate", Baking_state.Delegate_id.encoding) ~pp2:Error_monad.pp_print_trace ("trace", Error_monad.trace_encoding) @@ -851,7 +851,7 @@ module Actions = struct ~name:"failed_to_get_attestations_in_time" ~level:Error ~msg:"unable to get DAL attestation for {delegate} in time" - ("delegate", Signature.Public_key_hash.encoding) + ("delegate", Baking_state.Delegate_id.encoding) let failed_to_inject_consensus_vote = declare_3 @@ -861,8 +861,8 @@ module Actions = struct ~msg:"failed to inject {vote_kind} for {delegate} -- {trace}" ~pp1:Baking_state.pp_consensus_vote_kind ("vote_kind", Baking_state.consensus_vote_kind_encoding) - ~pp2:Baking_state.pp_consensus_key_and_delegate - ("delegate", Baking_state.consensus_key_and_delegate_encoding) + ~pp2:Baking_state.Delegate.pp + ("delegate", Baking_state.Delegate.encoding) ~pp3:Error_monad.pp_print_trace ("trace", Error_monad.trace_encoding) @@ -872,8 +872,8 @@ module Actions = struct ~name:"failed_to_forge_block" ~level:Error ~msg:"failed to forge block for {delegate} -- {trace}" - ~pp1:Baking_state.pp_consensus_key_and_delegate - ("delegate", Baking_state.consensus_key_and_delegate_encoding) + ~pp1:Baking_state.Delegate.pp + ("delegate", Baking_state.Delegate.encoding) ~pp2:Error_monad.pp_print_trace ("trace", Error_monad.trace_encoding) @@ -900,8 +900,8 @@ module Actions = struct ("vote_kind", Baking_state.consensus_vote_kind_encoding) ~pp2:Operation_hash.pp ("ophash", Operation_hash.encoding) - ~pp3:Baking_state.pp_consensus_key_and_delegate - ("delegate", Baking_state.consensus_key_and_delegate_encoding) + ~pp3:Baking_state.Delegate.pp + ("delegate", Baking_state.Delegate.encoding) ~pp4:pp_int32 ("level", Data_encoding.int32) ~pp5:Round.pp @@ -916,7 +916,7 @@ module Actions = struct "ready to attach DAL attestation for level {attestation_level}, round \ {round}, with bitset {bitset} for {delegate} to attest slots \ published at level {published_level}" - ("delegate", Signature.Public_key_hash.encoding) + ("delegate", Baking_state.Delegate_id.encoding) ~pp2:Z.pp_print ("bitset", Data_encoding.n) ("published_level", Data_encoding.int32) @@ -929,7 +929,7 @@ module Actions = struct ~name:"not_in_dal_committee" ~level:Notice ~msg:"{delegate} has no assigned DAL shards at level {attestation_level}" - ("delegate", Signature.Public_key_hash.encoding) + ("delegate", Baking_state.Delegate_id.encoding) ("attestation_level", Data_encoding.int32) let synchronizing_round = @@ -950,10 +950,10 @@ module Actions = struct "prepare forging block at level {level}, round {round} for {delegate}" ~pp1:pp_int32 ~pp2:Round.pp - ~pp3:Baking_state.pp_consensus_key_and_delegate + ~pp3:Baking_state.Delegate.pp ("level", Data_encoding.int32) ("round", Round.encoding) - ("delegate", Baking_state.consensus_key_and_delegate_encoding) + ("delegate", Baking_state.Delegate.encoding) let forging_block = declare_4 @@ -965,10 +965,10 @@ module Actions = struct apply: {force_apply})" ~pp1:pp_int32 ~pp2:Round.pp - ~pp3:Baking_state.pp_consensus_key_and_delegate + ~pp3:Baking_state.Delegate.pp ("level", Data_encoding.int32) ("round", Round.encoding) - ("delegate", Baking_state.consensus_key_and_delegate_encoding) + ("delegate", Baking_state.Delegate.encoding) ("force_apply", Data_encoding.bool) let delayed_block_injection = @@ -985,8 +985,8 @@ module Actions = struct ~pp2:pp_int32 ("round", Round.encoding) ~pp3:Round.pp - ("delegate", Baking_state.consensus_key_and_delegate_encoding) - ~pp4:Baking_state.pp_consensus_key_and_delegate + ("delegate", Baking_state.Delegate.encoding) + ~pp4:Baking_state.Delegate.pp let injecting_block = declare_3 @@ -996,10 +996,10 @@ module Actions = struct ~msg:"injecting block at level {level}, round {round} for {delegate}" ~pp1:pp_int32 ~pp2:Round.pp - ~pp3:Baking_state.pp_consensus_key_and_delegate + ~pp3:Baking_state.Delegate.pp ("level", Data_encoding.int32) ("round", Round.encoding) - ("delegate", Baking_state.consensus_key_and_delegate_encoding) + ("delegate", Baking_state.Delegate.encoding) let block_injected = declare_5 @@ -1013,7 +1013,7 @@ module Actions = struct ~pp1:Block_hash.pp ~pp2:pp_int32 ~pp3:Round.pp - ~pp4:Baking_state.pp_consensus_key_and_delegate + ~pp4:Baking_state.Delegate.pp ~pp5: (Format.pp_print_option (fun fmt Baking_state.{manager_operation_number; total_fees} -> @@ -1026,7 +1026,7 @@ module Actions = struct ("block", Block_hash.encoding) ("level", Data_encoding.int32) ("round", Round.encoding) - ("delegate", Baking_state.consensus_key_and_delegate_encoding) + ("delegate", Baking_state.Delegate.encoding) ( "manager_operations_infos", Data_encoding.option Baking_state.manager_operations_infos_encoding ) @@ -1049,8 +1049,8 @@ module Actions = struct ~msg:"signing {vote_kind} for {delegate}" ~pp1:Baking_state.pp_consensus_vote_kind ("vote_kind", Baking_state.consensus_vote_kind_encoding) - ~pp2:Baking_state.pp_consensus_key_and_delegate - ("delegate", Baking_state.consensus_key_and_delegate_encoding) + ~pp2:Baking_state.Delegate.pp + ("delegate", Baking_state.Delegate.encoding) let invalid_json_file = declare_1 @@ -1120,8 +1120,8 @@ module Actions = struct ~msg: "The following delegates have no attesting rights at level {level}: \ {delegates}" - ~pp1:(Format.pp_print_list Baking_state.pp_consensus_key) - ("delegates", Data_encoding.list Baking_state.consensus_key_encoding) + ~pp1:(Format.pp_print_list Baking_state.Consensus_key.pp) + ("delegates", Data_encoding.list Baking_state.Consensus_key.encoding) ~pp2:pp_int32 ("level", Data_encoding.int32) diff --git a/src/proto_alpha/lib_delegate/baking_highwatermarks.ml b/src/proto_alpha/lib_delegate/baking_highwatermarks.ml index e9227a5d6f1adaff48e248d2546e7683b365c25b..4c9824b6ca9f947d8624cbfe56a28e2e1e3a16a6 100644 --- a/src/proto_alpha/lib_delegate/baking_highwatermarks.ml +++ b/src/proto_alpha/lib_delegate/baking_highwatermarks.ml @@ -99,9 +99,9 @@ let () = (fun highwatermark -> Block_previously_attested highwatermark) module DelegateMap = Map.Make (struct - type t = Signature.Public_key_hash.t + type t = Baking_state.Consensus_key_id.t - let compare = Signature.Public_key_hash.compare + let compare = Baking_state.Consensus_key_id.compare end) let highwatermark_delegate_map_encoding = @@ -112,7 +112,7 @@ let highwatermark_delegate_map_encoding = fun l -> List.fold_left (fun map (k, v) -> add k v map) empty l) (list (obj2 - (req "delegate" Signature.Public_key_hash.encoding) + (req "delegate" Baking_state.Consensus_key_id.encoding) (req "highwatermark" highwatermark_encoding))) type highwatermarks = { diff --git a/src/proto_alpha/lib_delegate/baking_highwatermarks.mli b/src/proto_alpha/lib_delegate/baking_highwatermarks.mli index 64b3bc50853d4fa493ff2e50db439d7cf4ca5326..cfc15b69accce94e45c692e499ac8a1feb8c531d 100644 --- a/src/proto_alpha/lib_delegate/baking_highwatermarks.mli +++ b/src/proto_alpha/lib_delegate/baking_highwatermarks.mli @@ -43,21 +43,21 @@ val load : val may_sign_block : #Protocol_client_context.full -> [`Highwatermarks] Baking_files.location -> - delegate:Signature.public_key_hash -> + delegate:Baking_state.Consensus_key_id.t -> level:int32 -> round:Round.t -> bool tzresult Lwt.t val may_sign_preattestation : t -> - delegate:Signature.public_key_hash -> + delegate:Baking_state.Consensus_key_id.t -> level:int32 -> round:Round.t -> bool val may_sign_attestation : t -> - delegate:Signature.public_key_hash -> + delegate:Baking_state.Consensus_key_id.t -> level:int32 -> round:Round.t -> bool @@ -65,7 +65,7 @@ val may_sign_attestation : val record_block : #Protocol_client_context.full -> [`Highwatermarks] Baking_files.location -> - delegate:Signature.public_key_hash -> + delegate:Baking_state.Consensus_key_id.t -> level:int32 -> round:Round.t -> unit tzresult Lwt.t @@ -73,7 +73,7 @@ val record_block : val record_preattestation : #Protocol_client_context.full -> [`Highwatermarks] Baking_files.location -> - delegate:Signature.public_key_hash -> + delegate:Baking_state.Consensus_key_id.t -> level:int32 -> round:Round.t -> unit tzresult Lwt.t @@ -81,7 +81,7 @@ val record_preattestation : val record_attestation : #Protocol_client_context.full -> [`Highwatermarks] Baking_files.location -> - delegate:Signature.public_key_hash -> + delegate:Baking_state.Consensus_key_id.t -> level:int32 -> round:Round.t -> unit tzresult Lwt.t @@ -90,7 +90,7 @@ val record_all_preattestations : t -> #Protocol_client_context.full -> [`Highwatermarks] Baking_files.location -> - delegates:public_key_hash list -> + delegates:Baking_state.Consensus_key_id.t list -> level:int32 -> round:Round.t -> unit tzresult Lwt.t @@ -99,7 +99,7 @@ val record_all_attestations : t -> #Protocol_client_context.full -> [`Highwatermarks] Baking_files.location -> - delegates:public_key_hash list -> + delegates:Baking_state.Consensus_key_id.t list -> level:int32 -> round:Round.t -> unit tzresult Lwt.t diff --git a/src/proto_alpha/lib_delegate/baking_lib.ml b/src/proto_alpha/lib_delegate/baking_lib.ml index fcc6f60c2a2f58969cbd19d421a3b5d5d36d2951..4b5117f32e7725513730b018e0c09764679254ed 100644 --- a/src/proto_alpha/lib_delegate/baking_lib.ml +++ b/src/proto_alpha/lib_delegate/baking_lib.ml @@ -114,10 +114,7 @@ let preattest (cctxt : Protocol_client_context.full) ?(force = false) delegates let*! () = cctxt#message "@[Preattesting for:@ %a@]" - Format.( - pp_print_list - ~pp_sep:pp_print_space - Baking_state.pp_consensus_key_and_delegate) + Format.(pp_print_list ~pp_sep:pp_print_space Baking_state.Delegate.pp) (List.map (fun ({delegate; _} : unsigned_consensus_vote) -> delegate) consensus_batch.unsigned_consensus_votes) @@ -157,10 +154,7 @@ let attest (cctxt : Protocol_client_context.full) ?(force = false) delegates = let*! () = cctxt#message "@[Attesting for:@ %a@]" - Format.( - pp_print_list - ~pp_sep:pp_print_space - Baking_state.pp_consensus_key_and_delegate) + Format.(pp_print_list ~pp_sep:pp_print_space Baking_state.Delegate.pp) (List.map (fun ({delegate; _} : unsigned_consensus_vote) -> delegate) consensus_batch.unsigned_consensus_votes) @@ -442,11 +436,11 @@ let propose (cctxt : Protocol_client_context.full) ?minimal_fees match proposal_acceptance with | Invalid | Outdated_proposal -> ( match round_proposer state ~level:`Current round with - | Some {consensus_key_and_delegate; _} -> + | Some {delegate; _} -> let*! action = State_transitions.propose_block_action state - consensus_key_and_delegate + delegate round ~last_proposal:state.level_state.latest_proposal in @@ -533,11 +527,11 @@ let repropose (cctxt : Protocol_client_context.full) ?(force = false) match proposal_validity with | Invalid | Outdated_proposal -> ( match Baking_state.round_proposer state ~level:`Current round with - | Some {consensus_key_and_delegate; _} -> + | Some {delegate; _} -> let*! action = State_transitions.propose_block_action state - consensus_key_and_delegate + delegate round ~last_proposal:state.level_state.latest_proposal in diff --git a/src/proto_alpha/lib_delegate/baking_lib.mli b/src/proto_alpha/lib_delegate/baking_lib.mli index 6e968e35931180331aed8bed36627c9da855580f..20e2fd4c72c6c1155bbdcd7db76f53a1b072390c 100644 --- a/src/proto_alpha/lib_delegate/baking_lib.mli +++ b/src/proto_alpha/lib_delegate/baking_lib.mli @@ -43,19 +43,19 @@ val bake : ?count:int -> ?votes:Baking_configuration.per_block_votes_config -> ?state_recorder:Baking_configuration.state_recorder_config -> - Baking_state.consensus_key list -> + Baking_state.Consensus_key.t list -> unit tzresult Lwt.t val preattest : Protocol_client_context.full -> ?force:bool -> - Baking_state.consensus_key list -> + Baking_state.Consensus_key.t list -> unit tzresult Lwt.t val attest : Protocol_client_context.full -> ?force:bool -> - Baking_state.consensus_key list -> + Baking_state.Consensus_key.t list -> unit tzresult Lwt.t val propose : @@ -69,7 +69,7 @@ val propose : ?extra_operations:Baking_configuration.Operations_source.t -> ?context_path:string -> ?state_recorder:Baking_configuration.state_recorder_config -> - Baking_state.consensus_key list -> + Baking_state.Consensus_key.t list -> unit tzresult Lwt.t (** [repropose] tries to bake a new block proposal on the same level @@ -80,5 +80,5 @@ val repropose : Protocol_client_context.full -> ?force:bool -> ?force_round:Round.t -> - Baking_state.consensus_key list -> + Baking_state.Consensus_key.t list -> unit tzresult Lwt.t diff --git a/src/proto_alpha/lib_delegate/baking_nonces.ml b/src/proto_alpha/lib_delegate/baking_nonces.ml index 8f00e12ffa22f1fde5176cc411702a65bf84f4e8..20fa148fcd01f0364d4a967d7d565794d4842583 100644 --- a/src/proto_alpha/lib_delegate/baking_nonces.ml +++ b/src/proto_alpha/lib_delegate/baking_nonces.ml @@ -409,7 +409,7 @@ let generate_deterministic_nonce ?timeout secret_key_uri data = let generate_seed_nonce ?timeout (nonce_config : Baking_configuration.nonce_config) - (delegate : Baking_state.consensus_key) level = + (delegate : Baking_state.Consensus_key.t) level = let open Lwt_result_syntax in let* nonce = match nonce_config with diff --git a/src/proto_alpha/lib_delegate/baking_nonces.mli b/src/proto_alpha/lib_delegate/baking_nonces.mli index a4c2a5f3bce61c4b083cbdfc165f3e69aaca9a8d..44c2ac915b9b45a24ebbebc0524710f02a5269b5 100644 --- a/src/proto_alpha/lib_delegate/baking_nonces.mli +++ b/src/proto_alpha/lib_delegate/baking_nonces.mli @@ -48,7 +48,7 @@ val load : val generate_seed_nonce : ?timeout:float -> Baking_configuration.nonce_config -> - Baking_state.consensus_key -> + Baking_state.Consensus_key.t -> Raw_level.t -> (Nonce_hash.t * Nonce.t) tzresult Lwt.t diff --git a/src/proto_alpha/lib_delegate/baking_scheduling.ml b/src/proto_alpha/lib_delegate/baking_scheduling.ml index 777222810915bf48c2167f6a15092a7558260ba3..b4761a9bb8496ad0423bc52ca51bfe1e39324156 100644 --- a/src/proto_alpha/lib_delegate/baking_scheduling.ml +++ b/src/proto_alpha/lib_delegate/baking_scheduling.ml @@ -310,8 +310,7 @@ let rec first_own_round_in_range delegate_slots ~committee_size ~included_min (* Impossible because [Round.of_int] builds a sound round. *) None | Ok slot -> ( match Delegate_slots.own_slot_owner delegate_slots ~slot with - | Some {consensus_key_and_delegate; _} -> - Some (round, consensus_key_and_delegate) + | Some {delegate; _} -> Some (round, delegate) | None -> first_own_round_in_range delegate_slots diff --git a/src/proto_alpha/lib_delegate/baking_scheduling.mli b/src/proto_alpha/lib_delegate/baking_scheduling.mli index 7e6054355c436f56b392f2ccf1df473678b342f3..b6f52af181ca3bdf1f0652d6420743cc283c2da9 100644 --- a/src/proto_alpha/lib_delegate/baking_scheduling.mli +++ b/src/proto_alpha/lib_delegate/baking_scheduling.mli @@ -81,7 +81,7 @@ val run : ?constants:Constants.t -> chain:Chain_services.chain -> Baking_configuration.t -> - consensus_key list -> + Baking_state.Consensus_key.t list -> unit tzresult Lwt.t (** {2 Functions only needed for the baking_lib} *) @@ -94,9 +94,7 @@ val sleep_until : Time.Protocol.t -> unit Lwt.t option own delegates; also returns the corresponding delegate. Or returns [None] if no such round exists. *) val first_potential_round_at_next_level : - state -> - earliest_round:Round.t -> - (Round.t * consensus_key_and_delegate) option + state -> earliest_round:Round.t -> (Round.t * Baking_state.Delegate.t) option (** [compute_next_potential_baking_time state] From the current [state], the function returns an optional association pair, which consists of the next @@ -147,7 +145,7 @@ val create_initial_state : Operation_worker.t -> current_proposal:proposal -> ?constants:Constants.t -> - consensus_key list -> + Baking_state.Consensus_key.t list -> state tzresult Lwt.t (** [automaton_loop ?stop_on_event baking_configuration on_error loop_state diff --git a/src/proto_alpha/lib_delegate/baking_state.ml b/src/proto_alpha/lib_delegate/baking_state.ml index fc9e2f0bea0d4bd1b18392e70f8c855f8621b7c3..22df71126b3ab579535e0e90224a99b1a013feb8 100644 --- a/src/proto_alpha/lib_delegate/baking_state.ml +++ b/src/proto_alpha/lib_delegate/baking_state.ml @@ -29,69 +29,99 @@ open Baking_errors module Profiler = (val Profiler.wrap Baking_profiler.baker_profiler) -(** A consensus key (aka, a validator) is identified by its alias name, its +module Consensus_key_id = struct + type t = Signature.Public_key_hash.t + + let to_pkh pkh = pkh + + let compare = Signature.Public_key_hash.compare + + let encoding = Signature.Public_key_hash.encoding + + let pp = Signature.Public_key_hash.pp + + module Table = Signature.Public_key_hash.Table +end + +module Consensus_key = struct + (** A consensus key (aka, a validator) is identified by its alias name, its public key, its public key hash, and its secret key. *) -type consensus_key = { - alias : string option; - public_key : Signature.Public_key.t; - public_key_hash : Signature.Public_key_hash.t; - secret_key_uri : Client_keys.sk_uri; -} + type t = { + alias : string option; + id : Consensus_key_id.t; + public_key : Signature.Public_key.t; + secret_key_uri : Client_keys.sk_uri; + } -let consensus_key_encoding = - let open Data_encoding in - conv - (fun {alias; public_key; public_key_hash; secret_key_uri} -> - ( alias, - public_key, - public_key_hash, - Uri.to_string (secret_key_uri :> Uri.t) )) - (fun (alias, public_key, public_key_hash, secret_key_uri) -> - { - alias; - public_key; - public_key_hash; - secret_key_uri = - (match Client_keys.make_sk_uri (Uri.of_string secret_key_uri) with - | Ok sk -> sk - | Error e -> Format.kasprintf Stdlib.failwith "%a" pp_print_trace e); - }) - (obj4 - (req "alias" (option string)) - (req "public_key" Signature.Public_key.encoding) - (req "public_key_hash" Signature.Public_key_hash.encoding) - (req "secret_key_uri" string)) - -let pp_consensus_key fmt {alias; public_key_hash; _} = - match alias with - | None -> Format.fprintf fmt "%a" Signature.Public_key_hash.pp public_key_hash - | Some alias -> - Format.fprintf - fmt - "%s (%a)" - alias - Signature.Public_key_hash.pp - public_key_hash + let make ~alias ~public_key ~public_key_hash ~secret_key_uri = + {alias; public_key; id = public_key_hash; secret_key_uri} + + let encoding = + let open Data_encoding in + conv + (fun {alias; public_key; id; secret_key_uri} -> + (alias, public_key, id, Uri.to_string (secret_key_uri :> Uri.t))) + (fun (alias, public_key, id, secret_key_uri) -> + { + alias; + public_key; + id; + secret_key_uri = + (match Client_keys.make_sk_uri (Uri.of_string secret_key_uri) with + | Ok sk -> sk + | Error e -> Format.kasprintf Stdlib.failwith "%a" pp_print_trace e); + }) + (obj4 + (req "alias" (option string)) + (req "public_key" Signature.Public_key.encoding) + (req "public_key_hash" Signature.Public_key_hash.encoding) + (req "secret_key_uri" string)) + + let pp fmt {alias; id; _} = + match alias with + | None -> Format.fprintf fmt "%a" Signature.Public_key_hash.pp id + | Some alias -> + Format.fprintf fmt "%s (%a)" alias Signature.Public_key_hash.pp id +end -type consensus_key_and_delegate = consensus_key * Signature.Public_key_hash.t +module Delegate_id = struct + type t = Signature.public_key_hash -let consensus_key_and_delegate_encoding = - let open Data_encoding in - merge_objs - consensus_key_encoding - (obj1 (req "delegate" Signature.Public_key_hash.encoding)) + let of_pkh pkh = pkh -let pp_consensus_key_and_delegate fmt (consensus_key, delegate) = - if Signature.Public_key_hash.equal consensus_key.public_key_hash delegate then - pp_consensus_key fmt consensus_key - else - Format.fprintf - fmt - "%a@,on behalf of %a" - pp_consensus_key - consensus_key - Signature.Public_key_hash.pp - delegate + let to_pkh pkh = pkh + + let equal = Signature.Public_key_hash.equal + + let encoding = Signature.Public_key_hash.encoding + + let pp = Signature.Public_key_hash.pp +end + +module Delegate = struct + type t = {consensus_key : Consensus_key.t; delegate_id : Delegate_id.t} + + let encoding = + let open Data_encoding in + conv + (fun {consensus_key; delegate_id} -> (consensus_key, delegate_id)) + (fun (consensus_key, delegate_id) -> {consensus_key; delegate_id}) + (merge_objs + Consensus_key.encoding + (obj1 (req "delegate" Delegate_id.encoding))) + + let pp fmt {consensus_key; delegate_id} = + if Signature.Public_key_hash.equal consensus_key.id delegate_id then + Consensus_key.pp fmt consensus_key + else + Format.fprintf + fmt + "%a@,on behalf of %a" + Consensus_key.pp + consensus_key + Delegate_id.pp + delegate_id +end type validation_mode = Node | Local of Abstract_context_index.t @@ -116,7 +146,7 @@ type block_info = { type cache = { known_timestamps : Timestamp.time Baking_cache.Timestamp_of_round_cache.t; round_timestamps : - (Timestamp.time * Round.t * consensus_key_and_delegate) + (Timestamp.time * Round.t * Delegate.t) Baking_cache.Round_timestamp_interval_cache.t; } @@ -191,7 +221,7 @@ let block_info_encoding = module SlotMap : Map.S with type key = Slot.t = Map.Make (Slot) type delegate_slot = { - consensus_key_and_delegate : consensus_key_and_delegate; + delegate : Delegate.t; first_slot : Slot.t; attesting_power : int; } @@ -228,7 +258,7 @@ end type delegate_slots = Delegate_slots.t type dal_attestable_slots = - (Signature.Public_key_hash.t + (Delegate_id.t * Tezos_dal_node_services.Types.attestable_slots tzresult Lwt.t) list @@ -281,7 +311,7 @@ type manager_operations_infos = { type prepared_block = { signed_block_header : block_header; round : Round.t; - delegate : consensus_key_and_delegate; + delegate : Delegate.t; operations : Tezos_base.Operation.t list list; manager_operations_infos : manager_operations_infos option; baking_votes : Per_block_votes_repr.per_block_votes; @@ -359,7 +389,7 @@ type block_kind = type block_to_bake = { predecessor : block_info; round : Round.t; - delegate : consensus_key_and_delegate; + delegate : Delegate.t; kind : block_kind; force_apply : bool; } @@ -391,7 +421,7 @@ let pp_consensus_vote_kind fmt = function type unsigned_consensus_vote = { vote_kind : consensus_vote_kind; vote_consensus_content : consensus_content; - delegate : consensus_key_and_delegate; + delegate : Delegate.t; dal_content : dal_content option; } @@ -593,7 +623,7 @@ type global_state = { (* the validation mode used by the baker*) validation_mode : validation_mode; (* the delegates on behalf of which the baker is running *) - delegates : consensus_key list; + delegates : Consensus_key.t list; cache : cache; dal_node_rpc_ctxt : Tezos_rpc.Context.generic option; } @@ -733,7 +763,7 @@ let unsigned_consensus_vote_encoding = (obj4 (req "vote_kind" vote_kind_encoding) (req "vote_consensus_content" consensus_content_encoding) - (req "delegate" consensus_key_and_delegate_encoding) + (req "delegate" Delegate.encoding) (opt "dal_content" dal_content_encoding)) let signed_consensus_vote_encoding = @@ -791,7 +821,7 @@ let forge_event_encoding = (obj6 (req "header" (dynamic_size Block_header.encoding)) (req "round" Round.encoding) - (req "delegate" consensus_key_and_delegate_encoding) + (req "delegate" Delegate.encoding) (req "operations" (list (list (dynamic_size Tezos_base.Operation.encoding)))) @@ -1004,9 +1034,9 @@ let may_load_attestable_data state = module DelegateSet = struct include Set.Make (struct - type t = consensus_key + type t = Consensus_key.t - let compare {public_key_hash = pkh; _} {public_key_hash = pkh'; _} = + let compare Consensus_key.{id = pkh; _} Consensus_key.{id = pkh'; _} = Signature.Public_key_hash.compare pkh pkh' end) @@ -1014,9 +1044,8 @@ module DelegateSet = struct let exception Found of elt in try iter - (fun ({public_key_hash; _} as delegate) -> - if Signature.Public_key_hash.equal pkh public_key_hash then - raise (Found delegate) + (fun ({id; _} as delegate) -> + if Signature.Public_key_hash.equal pkh id then raise (Found delegate) else ()) s ; None @@ -1037,7 +1066,7 @@ let delegate_slots attesting_rights delegates = | Some consensus_key -> let attesting_slot = { - consensus_key_and_delegate = (consensus_key, delegate); + delegate = {consensus_key; delegate_id = delegate}; first_slot; attesting_power; } @@ -1172,7 +1201,7 @@ let pp_global_state fmt {chain_id; config; validation_mode; delegates; _} = config pp_validation_mode validation_mode - Format.(pp_print_list pp_consensus_key) + Format.(pp_print_list Consensus_key.pp) delegates let pp_option pp fmt = function @@ -1248,32 +1277,25 @@ let pp_elected_block fmt {proposal; attestation_qc} = proposal.block (List.length attestation_qc) -let pp_delegate_slot fmt - {consensus_key_and_delegate; first_slot; attesting_power} = +let pp_delegate_slot fmt {delegate; first_slot; attesting_power} = Format.fprintf fmt "slots: @[first_slot: %a@],@ delegate: %a,@ attesting_power: %d" Slot.pp first_slot - pp_consensus_key_and_delegate - consensus_key_and_delegate + Delegate.pp + delegate attesting_power (* this type is only used below for pretty-printing *) -type delegate_slots_for_pp = { - attester : consensus_key_and_delegate; - all_slots : Slot.t list; -} +type delegate_slots_for_pp = {attester : Delegate.t; all_slots : Slot.t list} let delegate_slots_for_pp delegate_slot_map = SlotMap.fold - (fun slot {consensus_key_and_delegate; first_slot; attesting_power = _} acc -> + (fun slot {delegate; first_slot; attesting_power = _} acc -> match SlotMap.find first_slot acc with | None -> - SlotMap.add - first_slot - {attester = consensus_key_and_delegate; all_slots = [slot]} - acc + SlotMap.add first_slot {attester = delegate; all_slots = [slot]} acc | Some {attester; all_slots} -> SlotMap.add first_slot {attester; all_slots = slot :: all_slots} acc) delegate_slot_map @@ -1292,7 +1314,7 @@ let pp_delegate_slots fmt Delegate_slots.{own_delegate_slots; _} = Format.fprintf fmt "attester: %a, power: %d, first 10 slots: %a" - pp_consensus_key_and_delegate + Delegate.pp attester (List.length all_slots) (Format.pp_print_list @@ -1301,8 +1323,7 @@ let pp_delegate_slots fmt Delegate_slots.{own_delegate_slots; _} = (List.filteri (fun i _ -> i < 10) all_slots))) (SlotMap.bindings (delegate_slots_for_pp own_delegate_slots)) -let pp_prepared_block fmt - {signed_block_header; delegate = consensus_key_and_delegate; _} = +let pp_prepared_block fmt {signed_block_header; delegate; _} = Format.fprintf fmt "predecessor block hash: %a, payload hash: %a, level: %ld, delegate: %a" @@ -1311,8 +1332,8 @@ let pp_prepared_block fmt Block_payload_hash.pp_short signed_block_header.protocol_data.contents.payload_hash signed_block_header.shell.level - pp_consensus_key_and_delegate - consensus_key_and_delegate + Delegate.pp + delegate let pp_level_state fmt { @@ -1405,7 +1426,7 @@ let pp_forge_event fmt = fprintf fmt "for delegate %a at level %ld (round %a)" - pp_consensus_key_and_delegate + Delegate.pp unsigned_consensus_vote.delegate (Raw_level.to_int32 unsigned_consensus_vote.vote_consensus_content.level) Round.pp @@ -1416,7 +1437,7 @@ let pp_forge_event fmt = fprintf fmt "block ready for delegate: %a at level %ld (round: %a)" - pp_consensus_key_and_delegate + Delegate.pp delegate signed_block_header.shell.level Round.pp diff --git a/src/proto_alpha/lib_delegate/baking_state.mli b/src/proto_alpha/lib_delegate/baking_state.mli index 8da69e3b16874db223d0eb534476b9d660520ca8..32a003163fe6235a430a7e9fdd8e031f9f54846e 100644 --- a/src/proto_alpha/lib_delegate/baking_state.mli +++ b/src/proto_alpha/lib_delegate/baking_state.mli @@ -27,32 +27,74 @@ open Protocol open Alpha_context (** {2 Consensus key type and functions} *) +module Consensus_key_id : sig + type t -type consensus_key = { - alias : string option; - public_key : Signature.public_key; - public_key_hash : Signature.public_key_hash; - secret_key_uri : Client_keys.sk_uri; -} + (** Only use at library frontiers *) + val to_pkh : t -> Signature.public_key_hash + + val compare : t -> t -> int + + val encoding : t Data_encoding.t + + val pp : Format.formatter -> t -> unit -val consensus_key_encoding : consensus_key Data_encoding.t + module Table : sig + include Hashtbl.SeededS with type key = t -val pp_consensus_key : Format.formatter -> consensus_key -> unit + val encoding : 'a Data_encoding.t -> 'a t Data_encoding.t + end +end + +module Consensus_key : sig + type t = private { + alias : string option; + id : Consensus_key_id.t; + public_key : Signature.public_key; + secret_key_uri : Client_keys.sk_uri; + } -type consensus_key_and_delegate = consensus_key * Signature.Public_key_hash.t + val make : + alias:string option -> + public_key:Signature.public_key -> + public_key_hash:Signature.public_key_hash -> + secret_key_uri:Client_keys.sk_uri -> + t -val consensus_key_and_delegate_encoding : - consensus_key_and_delegate Data_encoding.t + val encoding : t Data_encoding.t -val pp_consensus_key_and_delegate : - Format.formatter -> consensus_key_and_delegate -> unit + val pp : Format.formatter -> t -> unit +end (** {2 Delegates slots type and functions} *) +module Delegate_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 + + val equal : t -> t -> bool + + val encoding : t Data_encoding.t + + val pp : Format.formatter -> t -> unit +end + +module Delegate : sig + type t = {consensus_key : Consensus_key.t; delegate_id : Delegate_id.t} + + val encoding : t Data_encoding.t + + val pp : Format.formatter -> t -> unit +end (** A delegate slot consists of the delegate's consensus key, its public key hash, its first slot, and its attesting power at some level. *) type delegate_slot = { - consensus_key_and_delegate : consensus_key_and_delegate; + delegate : Delegate.t; first_slot : Slot.t; attesting_power : int; } @@ -89,7 +131,7 @@ val compute_delegate_slots : ?block:Block_services.block -> level:int32 -> chain:Shell_services.chain -> - consensus_key list -> + Consensus_key.t list -> delegate_slots tzresult Lwt.t (** {2 Consensus operations types functions} *) @@ -98,7 +140,7 @@ val compute_delegate_slots : at some level (as obtained through the [get_attestable_slots] RPC). See usage in {!level_state}. *) type dal_attestable_slots = - (Signature.Public_key_hash.t + (Delegate_id.t * Tezos_dal_node_services.Types.attestable_slots tzresult Lwt.t) list @@ -114,7 +156,7 @@ val pp_consensus_vote_kind : Format.formatter -> consensus_vote_kind -> unit type unsigned_consensus_vote = { vote_kind : consensus_vote_kind; vote_consensus_content : consensus_content; - delegate : consensus_key_and_delegate; + delegate : Delegate.t; dal_content : dal_content option; } @@ -148,7 +190,7 @@ val make_unsigned_consensus_vote_batch : consensus_vote_kind -> batch_content -> batch_branch:Block_hash.t -> - (consensus_key_and_delegate * Slot.t) list -> + (Delegate.t * Slot.t) list -> unsigned_consensus_vote_batch (** [dal_content_map_p f unsigned_consensus_vote_batch] map each @@ -358,8 +400,7 @@ type block_kind = type block_to_bake = { predecessor : block_info; round : Round.t; - delegate : consensus_key_and_delegate; - (** Delegate that have the right to bake the block. *) + delegate : Delegate.t; (** Delegate that have the right to bake the block. *) kind : block_kind; (** Either a reproposal or a fresh proposal *) force_apply : bool; (** if true, while baking the block, try and apply the block and its @@ -393,7 +434,7 @@ val manager_operations_infos_encoding : manager_operations_infos Data_encoding.t type prepared_block = { signed_block_header : block_header; round : Round.t; - delegate : consensus_key_and_delegate; + delegate : Delegate.t; operations : Tezos_base.Operation.t list list; manager_operations_infos : manager_operations_infos option; baking_votes : Per_block_votes_repr.per_block_votes; @@ -433,7 +474,7 @@ val pp_validation_mode : Format.formatter -> validation_mode -> unit type cache = { known_timestamps : Timestamp.time Baking_cache.Timestamp_of_round_cache.t; round_timestamps : - (Timestamp.time * Round.t * consensus_key_and_delegate) + (Timestamp.time * Round.t * Delegate.t) Baking_cache.Round_timestamp_interval_cache.t; } @@ -450,7 +491,7 @@ type global_state = { operation_worker : Operation_worker.t; mutable forge_worker_hooks : forge_worker_hooks; validation_mode : validation_mode; - delegates : consensus_key list; + delegates : Consensus_key.t list; cache : cache; dal_node_rpc_ctxt : Tezos_rpc.Context.generic option; } diff --git a/src/proto_alpha/lib_delegate/client_daemon.mli b/src/proto_alpha/lib_delegate/client_daemon.mli index bd24247fda99c5201fdb1ff8a1fae0b3dffebfea..b0073a38c7892f4d57c8d215945334b02b87db16 100644 --- a/src/proto_alpha/lib_delegate/client_daemon.mli +++ b/src/proto_alpha/lib_delegate/client_daemon.mli @@ -42,7 +42,7 @@ module Baker : sig ?state_recorder:Baking_configuration.state_recorder_config -> chain:Shell_services.chain -> keep_alive:bool -> - Baking_state.consensus_key list -> + Baking_state.Consensus_key.t list -> unit tzresult Lwt.t end diff --git a/src/proto_alpha/lib_delegate/forge_worker.ml b/src/proto_alpha/lib_delegate/forge_worker.ml index 09f0374e315761dcddc7b22d2023a8a3498b578d..8f61f91647fc68a5272589ceef08dbab09b52c32 100644 --- a/src/proto_alpha/lib_delegate/forge_worker.ml +++ b/src/proto_alpha/lib_delegate/forge_worker.ml @@ -16,7 +16,7 @@ end module Delegate_signing_queue = struct type t = { - delegate : consensus_key_and_delegate; + delegate : Baking_state.Delegate.t; task_stream : (unit -> unit Lwt.t) Lwt_stream.t; push : (unit -> unit Lwt.t) option -> unit; worker : unit Lwt.t; @@ -70,8 +70,7 @@ type worker = { push_task : forge_request option -> unit; push_event : forge_event option -> unit; event_stream : forge_event Lwt_stream.t; - delegate_signing_queues : - Delegate_signing_queue.t Signature.Public_key_hash.Table.t; + delegate_signing_queues : Delegate_signing_queue.t Consensus_key_id.Table.t; } type t = worker @@ -83,7 +82,7 @@ let get_event_stream state = state.event_stream let cancel_all_pending_tasks {delegate_signing_queues; _} = Lwt.dont_wait (fun () -> - Signature.Public_key_hash.Table.iter_p + Consensus_key_id.Table.iter_p (fun _ queue -> Delegate_signing_queue.cancel_pending_tasks queue) delegate_signing_queues) (fun _exn -> ()) @@ -91,7 +90,7 @@ let cancel_all_pending_tasks {delegate_signing_queues; _} = let shutdown state = let open Lwt_syntax in let* () = - Signature.Public_key_hash.Table.iter_p + Consensus_key_id.Table.iter_p (fun _ queue -> Delegate_signing_queue.cancel_all_tasks_and_close queue) state.delegate_signing_queues in @@ -100,15 +99,15 @@ let shutdown state = let get_or_create_queue worker delegate = match - Signature.Public_key_hash.Table.find_opt + Consensus_key_id.Table.find_opt worker.delegate_signing_queues - (fst delegate).public_key_hash + delegate.Baking_state.Delegate.consensus_key.id with | None -> let queue = Delegate_signing_queue.create delegate in - Signature.Public_key_hash.Table.add + Consensus_key_id.Table.add worker.delegate_signing_queues - (fst delegate).public_key_hash + delegate.consensus_key.id queue ; queue | Some queue -> queue @@ -190,7 +189,7 @@ let start (baking_state : Baking_state.global_state) = let open Lwt_result_syntax in let task_stream, push_task = Lwt_stream.create () in let event_stream, push_event = Lwt_stream.create () in - let delegate_signing_queues = Signature.Public_key_hash.Table.create 13 in + let delegate_signing_queues = Consensus_key_id.Table.create 13 in let state : worker = {push_task; push_event; event_stream; delegate_signing_queues} in diff --git a/src/proto_alpha/lib_delegate/node_rpc.ml b/src/proto_alpha/lib_delegate/node_rpc.ml index 6c2d3d3e1376b0d07d1f630362d729b888d6fae9..059620950307d7c47fdbee362f39e0f928875c82 100644 --- a/src/proto_alpha/lib_delegate/node_rpc.ml +++ b/src/proto_alpha/lib_delegate/node_rpc.ml @@ -375,7 +375,8 @@ let fetch_dal_config cctxt = | Error e -> return_error e | Ok dal_config -> return_ok dal_config -let get_attestable_slots dal_node_rpc_ctxt pkh ~attested_level = +let get_attestable_slots dal_node_rpc_ctxt delegate_id ~attested_level = + let pkh = Delegate_id.to_pkh delegate_id in Tezos_rpc.Context.make_call Tezos_dal_node_services.Services.get_attestable_slots dal_node_rpc_ctxt @@ -387,9 +388,10 @@ let dal_attestable_slots (dal_node_rpc_ctxt : Tezos_rpc.Context.generic) ~attestation_level delegate_slots = let attested_level = Int32.succ attestation_level in List.map - (fun delegate_slot -> - let pkh = snd delegate_slot.consensus_key_and_delegate in - (pkh, get_attestable_slots dal_node_rpc_ctxt pkh ~attested_level)) + (fun (delegate_slot : delegate_slot) -> + let delegate_id = delegate_slot.delegate.delegate_id in + ( delegate_id, + get_attestable_slots dal_node_rpc_ctxt delegate_id ~attested_level )) delegate_slots let get_dal_profiles dal_node_rpc_ctxt = @@ -403,7 +405,10 @@ let get_dal_profiles dal_node_rpc_ctxt = let register_dal_profiles dal_node_rpc_ctxt delegates = let profiles = Tezos_dal_node_services.Operator_profile.make - ~attesters:(List.map (fun k -> k.public_key_hash) delegates) + ~attesters: + (List.map + (fun k -> Consensus_key_id.to_pkh k.Consensus_key.id) + delegates) () in Tezos_rpc.Context.make_call diff --git a/src/proto_alpha/lib_delegate/node_rpc.mli b/src/proto_alpha/lib_delegate/node_rpc.mli index a5409ed7b1b370ca6a3809842dda583a3879112c..5baf873e8ba40b2351b6c2e95ea27cc143f3420e 100644 --- a/src/proto_alpha/lib_delegate/node_rpc.mli +++ b/src/proto_alpha/lib_delegate/node_rpc.mli @@ -107,7 +107,7 @@ val get_dal_profiles : [delegates]. *) val register_dal_profiles : Tezos_rpc.Context.generic -> - Baking_state.consensus_key list -> + Baking_state.Consensus_key.t list -> unit tzresult Lwt.t (** [get_dal_health ctxt] calls the DAL node RPC 'GET /health' *) diff --git a/src/proto_alpha/lib_delegate/state_transitions.ml b/src/proto_alpha/lib_delegate/state_transitions.ml index 9492061385e2458253a4de838b33501a0a5664ba..4d776cd58dd0cf14a09a7705acde9f906bd6aeae 100644 --- a/src/proto_alpha/lib_delegate/state_transitions.ml +++ b/src/proto_alpha/lib_delegate/state_transitions.ml @@ -93,8 +93,8 @@ let make_consensus_vote_batch state proposal kind = let batch_content = {level; round; block_payload_hash} in let delegates_and_slots = List.map - (fun delegate_slot -> - (delegate_slot.consensus_key_and_delegate, delegate_slot.first_slot)) + (fun (delegate_slot : delegate_slot) -> + (delegate_slot.delegate, delegate_slot.first_slot)) (Delegate_slots.own_delegates state.level_state.delegate_slots) in (* The branch is the latest finalized block. *) @@ -724,7 +724,7 @@ let end_of_round state current_round = level block arrive. Meanwhile, we are idle *) let new_state = update_current_phase new_state Idle in do_nothing new_state - | Some {consensus_key_and_delegate; _} -> + | Some {delegate; _} -> let latest_proposal = state.level_state.latest_proposal in if Baking_state.is_first_block_in_protocol latest_proposal then (* Do not inject a block for the previous protocol! (Let the @@ -738,13 +738,13 @@ let end_of_round state current_round = ( current_round, state.level_state.current_level, new_round, - consensus_key_and_delegate )) + delegate )) in (* We have a delegate, we need to determine what to inject *) let* action = (propose_block_action new_state - consensus_key_and_delegate + delegate new_round ~last_proposal:state.level_state.latest_proposal [@profiler.record_s @@ -769,7 +769,7 @@ let time_to_prepare_next_level_block state at_round = (* Unreachable: the [Time_to_prepare_next_level_block] event can only be triggered when we have a slot and an elected block *) assert false - | Some elected_block, Some {consensus_key_and_delegate; _} -> + | Some elected_block, Some {delegate; _} -> let attestations = elected_block.attestation_qc in let new_level_state = {state.level_state with next_level_proposed_round = Some at_round} @@ -780,7 +780,7 @@ let time_to_prepare_next_level_block state at_round = ~attestations ~predecessor:elected_block.proposal.block new_state - consensus_key_and_delegate + delegate at_round [@profiler.record_s {verbosity = Debug} "create propose fresh block action"]) diff --git a/src/proto_alpha/lib_delegate/state_transitions.mli b/src/proto_alpha/lib_delegate/state_transitions.mli index bb2cf360fd020cd04cbaddd3bb6868f938d3aeef..91f4f76956835e1892f05301bad0583340abc07c 100644 --- a/src/proto_alpha/lib_delegate/state_transitions.mli +++ b/src/proto_alpha/lib_delegate/state_transitions.mli @@ -56,11 +56,7 @@ val handle_proposal : given that there was already a proposal at the current level, the last one being [last_proposal]. *) val propose_block_action : - state -> - consensus_key_and_delegate -> - Round.t -> - last_proposal:proposal -> - action Lwt.t + state -> Delegate.t -> Round.t -> last_proposal:proposal -> action Lwt.t (** Increase the current round and propose at the new round (same level), if the baker has a proposer slot. *) diff --git a/src/proto_alpha/lib_delegate/test/mockup_simulator/mockup_simulator.ml b/src/proto_alpha/lib_delegate/test/mockup_simulator/mockup_simulator.ml index 6db51018b53336a6cde956f142c92aa8c57f8d4c..1aade797bce2de018bdd6d0f7381d7f102619375 100644 --- a/src/proto_alpha/lib_delegate/test/mockup_simulator/mockup_simulator.ml +++ b/src/proto_alpha/lib_delegate/test/mockup_simulator/mockup_simulator.ml @@ -149,7 +149,7 @@ module type Hooks = sig val on_start_baker : baker_position:int -> - delegates:Baking_state.consensus_key list -> + delegates:Baking_state.Consensus_key.t list -> cctxt:Protocol_client_context.full -> unit Lwt.t @@ -935,7 +935,7 @@ class tezt_printer : Tezos_client_base.Client_context.printer = end (** Start baker process. *) -let baker_process ~(delegates : Baking_state.consensus_key list) ~base_dir +let baker_process ~(delegates : Baking_state.Consensus_key.t list) ~base_dir ~(genesis_block : Block_header.t * Tezos_protocol_environment.rpc_context) ~i ~global_chain_table ~broadcast_pipes ~(user_hooks : (module Hooks)) = let open Lwt_result_syntax in @@ -965,15 +965,16 @@ let baker_process ~(delegates : Baking_state.consensus_key list) ~base_dir let*! () = User_hooks.on_start_baker ~baker_position:i ~delegates ~cctxt in let* () = List.iter_es - (fun ({alias; public_key; public_key_hash; secret_key_uri} : - Baking_state.consensus_key) -> + (fun ({alias; public_key; id; secret_key_uri} : + Baking_state.Consensus_key.t) -> let open Tezos_client_base in let name = alias |> WithExceptions.Option.get ~loc:__LOC__ in let* public_key_uri = Client_keys.neuterize secret_key_uri in + let pkh = Baking_state.Consensus_key_id.to_pkh id in Client_keys.register_key wallet ~force:false - (public_key_hash, public_key_uri, secret_key_uri) + (pkh, public_key_uri, secret_key_uri) ~public_key name) delegates @@ -1288,14 +1289,12 @@ let default_config = let make_baking_delegate ( (account : Alpha_context.Parameters.bootstrap_account), (secret : Tezos_mockup_commands.Mockup_wallet.bootstrap_secret) ) : - Baking_state.consensus_key = - Baking_state. - { - alias = Some secret.name; - public_key = account.public_key |> WithExceptions.Option.get ~loc:__LOC__; - public_key_hash = account.public_key_hash; - secret_key_uri = secret.sk_uri; - } + Baking_state.Consensus_key.t = + Baking_state.Consensus_key.make + ~alias:(Some secret.name) + ~public_key_hash:account.public_key_hash + ~public_key:(account.public_key |> WithExceptions.Option.get ~loc:__LOC__) + ~secret_key_uri:secret.sk_uri let run ?(config = default_config) bakers_spec = let open Lwt_result_syntax in diff --git a/src/proto_alpha/lib_delegate/test/mockup_simulator/mockup_simulator.mli b/src/proto_alpha/lib_delegate/test/mockup_simulator/mockup_simulator.mli index 7f2b37fba612eb697e1148ab98fde90c7fd8d0b3..4ce76277e3ad609dd7832dada6cc9b86e1f48425 100644 --- a/src/proto_alpha/lib_delegate/test/mockup_simulator/mockup_simulator.mli +++ b/src/proto_alpha/lib_delegate/test/mockup_simulator/mockup_simulator.mli @@ -126,7 +126,7 @@ module type Hooks = sig bakers that were started for this run. *) val on_start_baker : baker_position:int -> - delegates:Baking_state.consensus_key list -> + delegates:Baking_state.Consensus_key.t list -> cctxt:Protocol_client_context.full -> unit Lwt.t