From 03c4e28078710ced11ead1e8dc67324fd7805ce0 Mon Sep 17 00:00:00 2001 From: Adam Allombert-Goget Date: Mon, 23 Jun 2025 10:47:36 +0200 Subject: [PATCH 1/2] proto: update aggregate operations result format proto/dal_plugin_registration: support new aggregate attestations type teztale: support new attestations_aggregation result encoding proto/test_aggregate: handle new aggregate result type --- .../lib_client/operation_result.ml | 15 ++- .../lib_dal/dal_plugin_registration.ml | 2 +- src/proto_alpha/lib_protocol/apply.ml | 14 +- src/proto_alpha/lib_protocol/apply_results.ml | 33 +++-- .../lib_protocol/apply_results.mli | 8 +- .../integration/consensus/test_aggregate.ml | 21 ++- .../alpha_machine.real.ml | 126 ++++++------------ 7 files changed, 97 insertions(+), 122 deletions(-) diff --git a/src/proto_alpha/lib_client/operation_result.ml b/src/proto_alpha/lib_client/operation_result.ml index dba9ce7719ee..200fb656e5fe 100644 --- a/src/proto_alpha/lib_client/operation_result.ml +++ b/src/proto_alpha/lib_client/operation_result.ml @@ -981,6 +981,9 @@ let pp_contents_and_result : Signature.Public_key_hash.pp rewarded_delegate in + let pp_committee ppf (delegate, voting_power) = + Format.fprintf ppf "(%a, %d)" Consensus_key.pp delegate voting_power + in fun ppf -> function | Seed_nonce_revelation {level; nonce}, Seed_nonce_revelation_result bus -> Format.fprintf @@ -1051,7 +1054,7 @@ let pp_contents_and_result : consensus_power | ( Preattestations_aggregate {consensus_content = {level; _}; _}, Preattestations_aggregate_result - {balance_updates; committee; consensus_power} ) -> + {balance_updates; committee; total_consensus_power} ) -> Format.fprintf ppf "@[Preattestations aggregate:@,\ @@ -1063,12 +1066,12 @@ let pp_contents_and_result : level pp_balance_updates balance_updates - (Format.pp_print_list Consensus_key.pp) + (Format.pp_print_list pp_committee) committee - consensus_power + total_consensus_power | ( Attestations_aggregate {consensus_content = {level; _}; _}, Attestations_aggregate_result - {balance_updates; committee; consensus_power} ) -> + {balance_updates; committee; total_consensus_power} ) -> Format.fprintf ppf "@[Attestations aggregate:@,\ @@ -1080,9 +1083,9 @@ let pp_contents_and_result : level pp_balance_updates balance_updates - (Format.pp_print_list Consensus_key.pp) + (Format.pp_print_list pp_committee) committee - consensus_power + total_consensus_power | ( Double_consensus_operation_evidence {slot; op1; op2}, Double_consensus_operation_evidence_result double_signing_result ) -> Format.fprintf diff --git a/src/proto_alpha/lib_dal/dal_plugin_registration.ml b/src/proto_alpha/lib_dal/dal_plugin_registration.ml index 0b070e64cd97..8a1c1c52836c 100644 --- a/src/proto_alpha/lib_dal/dal_plugin_registration.ml +++ b/src/proto_alpha/lib_dal/dal_plugin_registration.ml @@ -270,7 +270,7 @@ module Plugin = struct (Attestations_aggregate_result {committee; _}) -> List.map2 ~when_different_lengths:[Aggregation_result_size_error] - (fun (tb_slot, dal_attestation) consensus_key -> + (fun (tb_slot, dal_attestation) (consensus_key, _) -> ( tb_slot, Some consensus_key.Consensus_key.delegate, packed_operation, diff --git a/src/proto_alpha/lib_protocol/apply.ml b/src/proto_alpha/lib_protocol/apply.ml index bbb6efddfb6f..b8e5d86d6220 100644 --- a/src/proto_alpha/lib_protocol/apply.ml +++ b/src/proto_alpha/lib_protocol/apply.ml @@ -2400,7 +2400,7 @@ let record_attestations_aggregate ctxt (mode : mode) committee : match mode with | Application _ | Full_construction _ -> let slot_map = Consensus.allowed_attestations ctxt in - let*? ctxt, rev_committee, consensus_power = + let*? ctxt, rev_committee, total_consensus_power = let open Result_syntax in List.fold_left_e (fun (ctxt, consensus_keys, consensus_power) (slot, dal) -> @@ -2412,7 +2412,8 @@ let record_attestations_aggregate ctxt (mode : mode) committee : in let* ctxt = record_dal_content ctxt slot ~dal_power dal in let key = ({delegate; consensus_pkh} : Consensus_key.t) in - return (ctxt, key :: consensus_keys, power + consensus_power)) + return + (ctxt, (key, power) :: consensus_keys, power + consensus_power)) (ctxt, [], 0) committee in @@ -2421,7 +2422,7 @@ let record_attestations_aggregate ctxt (mode : mode) committee : { balance_updates = []; committee = List.rev rev_committee; - consensus_power; + total_consensus_power; } in return (ctxt, Single_result result) @@ -2448,7 +2449,7 @@ let record_preattestations_aggregate ctxt (mode : mode) in let slot_map = Consensus.allowed_preattestations ctxt in (* Accumulate the list of delegates and the total attesting power *) - let*? ctxt, rev_committee, consensus_power = + let*? ctxt, rev_committee, total_consensus_power = let open Result_syntax in List.fold_left_e (fun (ctxt, consensus_keys, consensus_power) slot -> @@ -2463,7 +2464,8 @@ let record_preattestations_aggregate ctxt (mode : mode) round in let key = ({delegate; consensus_pkh} : Consensus_key.t) in - return (ctxt, key :: consensus_keys, power + consensus_power)) + return + (ctxt, (key, power) :: consensus_keys, power + consensus_power)) (ctxt, [], 0) committee in @@ -2472,7 +2474,7 @@ let record_preattestations_aggregate ctxt (mode : mode) { balance_updates = []; committee = List.rev rev_committee; - consensus_power; + total_consensus_power; } in return (ctxt, Single_result result) diff --git a/src/proto_alpha/lib_protocol/apply_results.ml b/src/proto_alpha/lib_protocol/apply_results.ml index 1a8a398ccf06..609d28bdb3c8 100644 --- a/src/proto_alpha/lib_protocol/apply_results.ml +++ b/src/proto_alpha/lib_protocol/apply_results.ml @@ -900,14 +900,14 @@ type 'kind contents_result = -> Kind.attestation contents_result | Preattestations_aggregate_result : { balance_updates : Receipt.balance_updates; - committee : Consensus_key.t list; - consensus_power : int; + committee : (Consensus_key.t * int) list; + total_consensus_power : int; } -> Kind.preattestations_aggregate contents_result | Attestations_aggregate_result : { balance_updates : Receipt.balance_updates; - committee : Consensus_key.t list; - consensus_power : int; + committee : (Consensus_key.t * int) list; + total_consensus_power : int; } -> Kind.attestations_aggregate contents_result | Seed_nonce_revelation_result : @@ -1042,8 +1042,13 @@ module Encoding = struct let open Data_encoding in obj3 (dft "balance_updates" Receipt.balance_updates_encoding []) - (req "committee" (list Consensus_key.encoding)) - (req "consensus_power" int31) + (req + "committee" + (list + (merge_objs + Consensus_key.encoding + (obj1 (req "consensus_power" int31))))) + (req "total_consensus_power" int31) type case = | Case : { @@ -1158,12 +1163,12 @@ module Encoding = struct proj = (function | Attestations_aggregate_result - {balance_updates; committee; consensus_power} -> - (balance_updates, committee, consensus_power)); + {balance_updates; committee; total_consensus_power} -> + (balance_updates, committee, total_consensus_power)); inj = - (fun (balance_updates, committee, consensus_power) -> + (fun (balance_updates, committee, total_consensus_power) -> Attestations_aggregate_result - {balance_updates; committee; consensus_power}); + {balance_updates; committee; total_consensus_power}); } let preattestations_aggregate_case = @@ -1184,12 +1189,12 @@ module Encoding = struct proj = (function | Preattestations_aggregate_result - {balance_updates; committee; consensus_power} -> - (balance_updates, committee, consensus_power)); + {balance_updates; committee; total_consensus_power} -> + (balance_updates, committee, total_consensus_power)); inj = - (fun (balance_updates, committee, consensus_power) -> + (fun (balance_updates, committee, total_consensus_power) -> Preattestations_aggregate_result - {balance_updates; committee; consensus_power}); + {balance_updates; committee; total_consensus_power}); } let seed_nonce_revelation_case = diff --git a/src/proto_alpha/lib_protocol/apply_results.mli b/src/proto_alpha/lib_protocol/apply_results.mli index 21434b291695..6fa6fab25c51 100644 --- a/src/proto_alpha/lib_protocol/apply_results.mli +++ b/src/proto_alpha/lib_protocol/apply_results.mli @@ -81,14 +81,14 @@ and 'kind contents_result = -> Kind.attestation contents_result | Preattestations_aggregate_result : { balance_updates : Receipt.balance_updates; - committee : Consensus_key.t list; - consensus_power : int; + committee : (Consensus_key.t * int) list; + total_consensus_power : int; } -> Kind.preattestations_aggregate contents_result | Attestations_aggregate_result : { balance_updates : Receipt.balance_updates; - committee : Consensus_key.t list; - consensus_power : int; + committee : (Consensus_key.t * int) list; + total_consensus_power : int; } -> Kind.attestations_aggregate contents_result | Seed_nonce_revelation_result : diff --git a/src/proto_alpha/lib_protocol/test/integration/consensus/test_aggregate.ml b/src/proto_alpha/lib_protocol/test/integration/consensus/test_aggregate.ml index e29278b5eca6..4fd506e1c658 100644 --- a/src/proto_alpha/lib_protocol/test/integration/consensus/test_aggregate.ml +++ b/src/proto_alpha/lib_protocol/test/integration/consensus/test_aggregate.ml @@ -117,10 +117,18 @@ let check_aggregate_result (type kind) (kind : kind aggregate) ~committee match (kind, result) with | ( Preattestation, Preattestations_aggregate_result - {balance_updates; committee = resulting_committee; consensus_power} ) + { + balance_updates; + committee = resulting_committee; + total_consensus_power; + } ) | ( Attestation, Attestations_aggregate_result - {balance_updates; committee = resulting_committee; consensus_power} ) -> + { + balance_updates; + committee = resulting_committee; + total_consensus_power; + } ) -> (* Check balance updates *) let* () = match balance_updates with @@ -136,12 +144,12 @@ let check_aggregate_result (type kind) (kind : kind aggregate) ~committee 0 committee in - if voting_power = consensus_power then return_unit + if voting_power = total_consensus_power then return_unit else Test.fail "Wrong voting power : expected %d, found %d" voting_power - consensus_power + total_consensus_power in (* Check committee *) let committee_pkhs = @@ -151,7 +159,8 @@ let check_aggregate_result (type kind) (kind : kind aggregate) ~committee in let resulting_committee_pkhs = List.map - (fun (attester : Alpha_context.Consensus_key.t) -> attester.delegate) + (fun ((attester : Alpha_context.Consensus_key.t), _) -> + attester.delegate) resulting_committee in if @@ -746,7 +755,7 @@ let test_metadata_committee_is_correctly_ordered () = let check_committees ~loc committee result_committee = let result_committee = List.map - (fun (key : Alpha_context.Consensus_key.t) -> key.consensus_pkh) + (fun ((key : Alpha_context.Consensus_key.t), _) -> key.consensus_pkh) result_committee in let* () = diff --git a/teztale/bin_teztale_archiver/alpha_machine.real.ml b/teztale/bin_teztale_archiver/alpha_machine.real.ml index 7d2f2b8b1a75..9b328bcd3f78 100644 --- a/teztale/bin_teztale_archiver/alpha_machine.real.ml +++ b/teztale/bin_teztale_archiver/alpha_machine.real.ml @@ -228,75 +228,23 @@ module Services : Protocol_machinery.PROTOCOL_SERVICES = struct | Consensus_ops.Preattestation -> get_preattestation_round protocol_data | Attestation -> get_attestation_round protocol_data - let get_consensus_power cctxt protocol_data delegates = - let level = - match protocol_data with - | Protocol.Alpha_context.Operation_data {contents; _} -> ( - match contents with - | Single - (Preattestations_aggregate - {consensus_content = {level; _}; committee = _}) -> - level - | Single - (Attestations_aggregate - {consensus_content = {level; _}; committee = _}) -> - level - | _ -> assert false) - in - let lvl = Protocol.Alpha_context.Raw_level.to_int32 level in - let* answers = - Plugin.RPC.Attestation_rights.get - cctxt - ~levels:[level] - ~delegates - (cctxt#chain, `Level lvl) - in - match answers with - | answer :: _ -> - return - (List.fold_left - (fun acc - Plugin.RPC.Attestation_rights. - {delegate; first_slot = _; attestation_power; _} -> - Signature.Public_key_hash.Map.add delegate attestation_power acc) - Signature.Public_key_hash.Map.empty - answer.Plugin.RPC.Attestation_rights.delegates_rights) - | [] -> return Signature.Public_key_hash.Map.empty - - let consensus_ops_from_aggregate cctxt acc hash protocol_data committee kind = - let* consensus_powers = - get_consensus_power - cctxt - protocol_data - (List.map - (fun (ck : Protocol.Alpha_context.Consensus_key.t) -> ck.delegate) - committee) - in - return - @@ List.fold_left - (fun acc (ck : Protocol.Alpha_context.Consensus_key.t) -> - let power = - match - Signature.Public_key_hash.Map.find ck.delegate consensus_powers - with - | None -> 0 - | Some power -> power - in - Consensus_ops. - { - op = - { - hash; - round = Some (get_consensus_round protocol_data kind); - kind; - }; - delegate = - Tezos_crypto.Signature.Of_V2.public_key_hash ck.delegate; - power; - } - :: acc) - acc - committee + let consensus_ops_from_aggregate acc hash protocol_data committee kind = + List.fold_left + (fun acc ((ck : Protocol.Alpha_context.Consensus_key.t), power) -> + Consensus_ops. + { + op = + { + hash; + round = Some (get_consensus_round protocol_data kind); + kind; + }; + delegate = Tezos_crypto.Signature.Of_V2.public_key_hash ck.delegate; + power; + } + :: acc) + acc + committee let consensus_ops_info_of_block cctxt hash = let* ops = @@ -359,30 +307,38 @@ module Services : Protocol_machinery.PROTOCOL_SERVICES = struct contents = Single_result (Protocol.Apply_results.Attestations_aggregate_result - {committee; consensus_power = _; balance_updates = _}); + { + committee; + total_consensus_power = _; + balance_updates = _; + }); }) -> - consensus_ops_from_aggregate - cctxt - acc - hash - protocol_data - committee - Consensus_ops.Attestation + return + @@ consensus_ops_from_aggregate + acc + hash + protocol_data + committee + Consensus_ops.Attestation | Receipt (Protocol.Apply_results.Operation_metadata { contents = Single_result (Protocol.Apply_results.Preattestations_aggregate_result - {committee; consensus_power = _; balance_updates = _}); + { + committee; + total_consensus_power = _; + balance_updates = _; + }); }) -> - consensus_ops_from_aggregate - cctxt - acc - hash - protocol_data - committee - Consensus_ops.Preattestation + return + @@ consensus_ops_from_aggregate + acc + hash + protocol_data + committee + Consensus_ops.Preattestation | _ -> return acc) [] ops -- GitLab From 5211487665f811287ec7f302e69a142090107094 Mon Sep 17 00:00:00 2001 From: Adam Allombert-Goget Date: Wed, 25 Jun 2025 09:23:37 +0200 Subject: [PATCH 2/2] proto/alpha.rst: add entry for aggregate operations receipts update --- docs/protocols/alpha.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/protocols/alpha.rst b/docs/protocols/alpha.rst index b710aab207a9..3bbb218cb0f4 100644 --- a/docs/protocols/alpha.rst +++ b/docs/protocols/alpha.rst @@ -37,6 +37,8 @@ RPC Changes Operation receipts ------------------ +- Aggregate operations receipts update. Return the consensus power for each delegate in the committee (MR :gl:`!18435`) + Errors ------ -- GitLab