From 0f4986bd29ac7f48918a1a4f1f685827d257f8df Mon Sep 17 00:00:00 2001 From: Lucas Randazzo Date: Mon, 4 Aug 2025 16:12:50 +0200 Subject: [PATCH] Proto/abaab: abtract attestation power This also allows us to also hide the flag check behind the alpha_context abstraction --- .../lib_client/operation_result.ml | 10 ++-- src/proto_alpha/lib_plugin/RPC.ml | 3 +- src/proto_alpha/lib_protocol/alpha_context.ml | 15 ++++- .../lib_protocol/alpha_context.mli | 26 +++++++-- src/proto_alpha/lib_protocol/apply.ml | 23 +++----- src/proto_alpha/lib_protocol/apply_results.ml | 18 +++--- .../lib_protocol/apply_results.mli | 12 ++-- .../lib_protocol/attestation_power_repr.ml | 6 ++ .../lib_protocol/attestation_power_repr.mli | 9 +-- src/proto_alpha/lib_protocol/baking.ml | 47 ++++++++-------- src/proto_alpha/lib_protocol/baking.mli | 6 +- .../delegate_missed_attestations_storage.ml | 6 +- .../delegate_missed_attestations_storage.mli | 2 +- src/proto_alpha/lib_protocol/raw_context.ml | 15 +++-- src/proto_alpha/lib_protocol/raw_context.mli | 15 +++-- .../integration/consensus/test_aggregate.ml | 9 ++- src/proto_alpha/lib_protocol/validate.ml | 56 +++++++++---------- .../lib_protocol/validate_errors.ml | 18 +++--- .../lib_protocol/validate_errors.mli | 6 +- .../alpha_machine.real.ml | 10 +++- 20 files changed, 170 insertions(+), 142 deletions(-) diff --git a/src/proto_alpha/lib_client/operation_result.ml b/src/proto_alpha/lib_client/operation_result.ml index 70205b0ed8c7..71d784a484a0 100644 --- a/src/proto_alpha/lib_client/operation_result.ml +++ b/src/proto_alpha/lib_client/operation_result.ml @@ -987,7 +987,7 @@ let pp_contents_and_result : "(%a, %a)" Consensus_key.pp delegate - Attestation_power_repr.pp + Attestation_power.pp voting_power in fun ppf -> function @@ -1040,7 +1040,7 @@ let pp_contents_and_result : balance_updates Consensus_key.pp {delegate; consensus_pkh = consensus_key} - Attestation_power_repr.pp + Attestation_power.pp consensus_power | ( Attestation {consensus_content = {level; _}; dal_content = _}, Attestation_result @@ -1058,7 +1058,7 @@ let pp_contents_and_result : balance_updates Consensus_key.pp {delegate; consensus_pkh = consensus_key} - Attestation_power_repr.pp + Attestation_power.pp consensus_power | ( Preattestations_aggregate {consensus_content = {level; _}; _}, Preattestations_aggregate_result @@ -1076,7 +1076,7 @@ let pp_contents_and_result : balance_updates (Format.pp_print_list pp_committee) committee - Attestation_power_repr.pp + Attestation_power.pp total_consensus_power | ( Attestations_aggregate {consensus_content = {level; _}; _}, Attestations_aggregate_result @@ -1094,7 +1094,7 @@ let pp_contents_and_result : balance_updates (Format.pp_print_list pp_committee) committee - Attestation_power_repr.pp + Attestation_power.pp total_consensus_power | ( Double_consensus_operation_evidence {slot; op1; op2}, Double_consensus_operation_evidence_result double_signing_result ) -> diff --git a/src/proto_alpha/lib_plugin/RPC.ml b/src/proto_alpha/lib_plugin/RPC.ml index 34ddecbd92bf..a23f4b4ea8af 100644 --- a/src/proto_alpha/lib_plugin/RPC.ml +++ b/src/proto_alpha/lib_plugin/RPC.ml @@ -3916,7 +3916,8 @@ module Attestation_rights = struct delegate; consensus_key; first_slot; - attestation_power = attestation_power.slots; + (* TODO ABAAB (this RPC is only used for tests) *) + attestation_power = Attestation_power.get_slots attestation_power; } :: acc) rights diff --git a/src/proto_alpha/lib_protocol/alpha_context.ml b/src/proto_alpha/lib_protocol/alpha_context.ml index f5b44941083e..a2259dd4359b 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.ml +++ b/src/proto_alpha/lib_protocol/alpha_context.ml @@ -626,9 +626,6 @@ module Delegate = struct end module Stake_distribution = struct - let check_all_bakers_attest_at_level = - Delegate_sampler.check_all_bakers_attest_at_level - let baking_rights_owner = Delegate_sampler.baking_rights_owner let slot_owner = Delegate_sampler.slot_owner @@ -674,6 +671,18 @@ end module Migration = Migration_repr +module Attestation_power = struct + include Attestation_power_repr + + let get ctxt level {slots; stake} = + let all_bakers_attest_enabled = + Delegate_sampler.check_all_bakers_attest_at_level ctxt level + in + if all_bakers_attest_enabled then stake else Int64.of_int slots + + let get_slots {slots; _} = slots +end + module Consensus = struct include Raw_context.Consensus diff --git a/src/proto_alpha/lib_protocol/alpha_context.mli b/src/proto_alpha/lib_protocol/alpha_context.mli index c4463d78d25d..54aa682a5b10 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/alpha_context.mli @@ -2242,6 +2242,25 @@ module Receipt : sig val balance_updates_encoding : balance_updates Data_encoding.t end +(** This module re-exports definitions from {!Attestation_power_repr}. *) +module Attestation_power : sig + type t + + val encoding : t Data_encoding.t + + val pp : Format.formatter -> t -> unit + + val zero : t + + val make : slots:int -> stake:int64 -> t + + val add : t -> t -> t + + val get : context -> Level.t -> t -> int64 + + val get_slots : t -> int +end + (** This module re-exports definitions from {!Delegate_consensus_key}. *) module Consensus_key : sig type pk = { @@ -2259,7 +2278,7 @@ module Consensus_key : sig type power = { consensus_key : pk; - attestation_power : Attestation_power_repr.t; + attestation_power : Attestation_power.t; dal_power : int; } @@ -2364,7 +2383,7 @@ module Delegate : sig context -> delegate:public_key_hash -> participation:level_participation -> - attesting_power:int -> + attesting_slots:int -> context tzresult Lwt.t val record_dal_participation : @@ -5234,8 +5253,6 @@ end (** This module re-exports definitions from {!Stake_storage}, {!Delegate_storage} and {!Delegate}. *) module Stake_distribution : sig - val check_all_bakers_attest_at_level : context -> Level.t -> bool - val baking_rights_owner : context -> Level.t -> @@ -5532,6 +5549,7 @@ module Consensus : sig and type 'a level_map := 'a Level.Map.t and type slot_set := Slot.Set.t and type round := Round.t + and type attestation_power := Attestation_power.t and type consensus_power := Consensus_key.power (** [store_attestation_branch context branch] sets the "attestation branch" diff --git a/src/proto_alpha/lib_protocol/apply.ml b/src/proto_alpha/lib_protocol/apply.ml index 6b82310a1ff3..fe3cea2ff0d9 100644 --- a/src/proto_alpha/lib_protocol/apply.ml +++ b/src/proto_alpha/lib_protocol/apply.ml @@ -2346,7 +2346,7 @@ let record_preattestation ctxt (mode : mode) (content : consensus_content) : ( ctxt, mk_preattestation_result consensus_key - Attestation_power_repr.zero (* Fake power. *) ) + Attestation_power.zero (* Fake power. *) ) let record_dal_content ctxt slot ~dal_power = function | None -> Result.return ctxt @@ -2401,7 +2401,7 @@ let record_attestation ctxt (mode : mode) (consensus : consensus_content) ( ctxt, mk_attestation_result consensus_key - Attestation_power_repr.zero (* Fake power. *) ) + Attestation_power.zero (* Fake power. *) ) let record_attestations_aggregate ctxt (mode : mode) committee : (context * Kind.attestations_aggregate contents_result_list) tzresult Lwt.t @@ -2432,8 +2432,8 @@ let record_attestations_aggregate ctxt (mode : mode) committee : return ( ctxt, (key, attestation_power) :: consensus_keys, - Attestation_power_repr.add attestation_power consensus_power )) - (ctxt, [], Attestation_power_repr.zero) + Attestation_power.add attestation_power consensus_power )) + (ctxt, [], Attestation_power.zero) committee in let result = @@ -2490,8 +2490,8 @@ let record_preattestations_aggregate ctxt (mode : mode) return ( ctxt, (key, attestation_power) :: consensus_keys, - Attestation_power_repr.add attestation_power consensus_power )) - (ctxt, [], Attestation_power_repr.zero) + Attestation_power.add attestation_power consensus_power )) + (ctxt, [], Attestation_power.zero) committee in let result = @@ -3012,7 +3012,7 @@ let record_attesting_participation ctxt dal_attestation = ctxt ~delegate:consensus_key.delegate ~participation - ~attesting_power:attestation_power.slots + ~attesting_slots:(Attestation_power.get_slots attestation_power) in Dal_apply.record_participation ctxt @@ -3246,14 +3246,9 @@ let finalize_application ctxt block_data_contents ~round ~predecessor_hash let* ctxt, reward_bonus = if required_attestations then let* ctxt = record_attesting_participation ctxt dal_attestation in - let all_bakers_attest_enabled = - Stake_distribution.check_all_bakers_attest_at_level ctxt level - in let*? rewards_bonus = - if all_bakers_attest_enabled then - error Validate_errors.Consensus.All_bakers_attest_not_implemented - (* TODO ABAAB *) - else Baking.bonus_baking_reward ctxt ~attestation_power + (* TODO ABAAB : only works if flag is false *) + Baking.bonus_baking_reward ctxt level ~attestation_power in return (ctxt, Some rewards_bonus) else return (ctxt, None) diff --git a/src/proto_alpha/lib_protocol/apply_results.ml b/src/proto_alpha/lib_protocol/apply_results.ml index b2c614b5a838..96d8839dc5a8 100644 --- a/src/proto_alpha/lib_protocol/apply_results.ml +++ b/src/proto_alpha/lib_protocol/apply_results.ml @@ -888,26 +888,26 @@ type 'kind contents_result = balance_updates : Receipt.balance_updates; delegate : Signature.public_key_hash; consensus_key : Signature.public_key_hash; - consensus_power : Attestation_power_repr.t; + consensus_power : Attestation_power.t; } -> Kind.preattestation contents_result | Attestation_result : { balance_updates : Receipt.balance_updates; delegate : Signature.public_key_hash; consensus_key : Signature.public_key_hash; - consensus_power : Attestation_power_repr.t; + consensus_power : Attestation_power.t; } -> Kind.attestation contents_result | Preattestations_aggregate_result : { balance_updates : Receipt.balance_updates; - committee : (Consensus_key.t * Attestation_power_repr.t) list; - total_consensus_power : Attestation_power_repr.t; + committee : (Consensus_key.t * Attestation_power.t) list; + total_consensus_power : Attestation_power.t; } -> Kind.preattestations_aggregate contents_result | Attestations_aggregate_result : { balance_updates : Receipt.balance_updates; - committee : (Consensus_key.t * Attestation_power_repr.t) list; - total_consensus_power : Attestation_power_repr.t; + committee : (Consensus_key.t * Attestation_power.t) list; + total_consensus_power : Attestation_power.t; } -> Kind.attestations_aggregate contents_result | Seed_nonce_revelation_result : @@ -1035,7 +1035,7 @@ module Encoding = struct obj4 (dft "balance_updates" Receipt.balance_updates_encoding []) (req "delegate" Signature.Public_key_hash.encoding) - (req "consensus_power" Attestation_power_repr.encoding) + (req "consensus_power" Attestation_power.encoding) (req "consensus_key" Signature.Public_key_hash.encoding) let consensus_aggregate_result_encoding = @@ -1047,8 +1047,8 @@ module Encoding = struct (list (merge_objs Consensus_key.encoding - (obj1 (req "consensus_power" Attestation_power_repr.encoding))))) - (req "total_consensus_power" Attestation_power_repr.encoding) + (obj1 (req "consensus_power" Attestation_power.encoding))))) + (req "total_consensus_power" Attestation_power.encoding) type case = | Case : { diff --git a/src/proto_alpha/lib_protocol/apply_results.mli b/src/proto_alpha/lib_protocol/apply_results.mli index e9f7ec3088df..4bdfaa4012db 100644 --- a/src/proto_alpha/lib_protocol/apply_results.mli +++ b/src/proto_alpha/lib_protocol/apply_results.mli @@ -69,26 +69,26 @@ and 'kind contents_result = balance_updates : Receipt.balance_updates; delegate : Signature.public_key_hash; consensus_key : Signature.public_key_hash; - consensus_power : Attestation_power_repr.t; + consensus_power : Attestation_power.t; } -> Kind.preattestation contents_result | Attestation_result : { balance_updates : Receipt.balance_updates; delegate : Signature.public_key_hash; consensus_key : Signature.public_key_hash; - consensus_power : Attestation_power_repr.t; + consensus_power : Attestation_power.t; } -> Kind.attestation contents_result | Preattestations_aggregate_result : { balance_updates : Receipt.balance_updates; - committee : (Consensus_key.t * Attestation_power_repr.t) list; - total_consensus_power : Attestation_power_repr.t; + committee : (Consensus_key.t * Attestation_power.t) list; + total_consensus_power : Attestation_power.t; } -> Kind.preattestations_aggregate contents_result | Attestations_aggregate_result : { balance_updates : Receipt.balance_updates; - committee : (Consensus_key.t * Attestation_power_repr.t) list; - total_consensus_power : Attestation_power_repr.t; + committee : (Consensus_key.t * Attestation_power.t) list; + total_consensus_power : Attestation_power.t; } -> Kind.attestations_aggregate contents_result | Seed_nonce_revelation_result : diff --git a/src/proto_alpha/lib_protocol/attestation_power_repr.ml b/src/proto_alpha/lib_protocol/attestation_power_repr.ml index 33a4d1c4268a..994c4d867d92 100644 --- a/src/proto_alpha/lib_protocol/attestation_power_repr.ml +++ b/src/proto_alpha/lib_protocol/attestation_power_repr.ml @@ -5,6 +5,10 @@ (* *) (*****************************************************************************) +(** If the consensus is distributed randomly for 7000 slots, + the (relative) power is the number of slots of the given delegate, + noted in [slots]. Otherwise, if all bakers attest, then its power is its stake + for the cycle in which the rights were distributed, noted in [stake]. *) type t = {slots : int; stake : int64} let encoding = @@ -19,4 +23,6 @@ let pp ppf {slots; stake} = let zero = {slots = 0; stake = 0L} +let make ~slots ~stake = {slots; stake} + let add a b = {slots = a.slots + b.slots; stake = Int64.add a.stake b.stake} diff --git a/src/proto_alpha/lib_protocol/attestation_power_repr.mli b/src/proto_alpha/lib_protocol/attestation_power_repr.mli index 7684e35d30b9..46b555a75205 100644 --- a/src/proto_alpha/lib_protocol/attestation_power_repr.mli +++ b/src/proto_alpha/lib_protocol/attestation_power_repr.mli @@ -5,12 +5,7 @@ (* *) (*****************************************************************************) -(** Internal representation of attestation power. - - If the consensus is distributed randomly for 7000 slots, - the (relative) power is the number of slots of the given delegate, - noted in [slots]. Otherwise, if all bakers attest, then its power is its stake - for the cycle in which the rights were distributed, noted in [stake]. *) +(** Internal representation of attestation power. *) type t = {slots : int; stake : int64} @@ -20,4 +15,6 @@ val pp : Format.formatter -> t -> unit val zero : t +val make : slots:int -> stake:int64 -> t + val add : t -> t -> t diff --git a/src/proto_alpha/lib_protocol/baking.ml b/src/proto_alpha/lib_protocol/baking.ml index fd49c03e5bb4..540bd7d8941f 100644 --- a/src/proto_alpha/lib_protocol/baking.ml +++ b/src/proto_alpha/lib_protocol/baking.ml @@ -29,8 +29,8 @@ open Alpha_context type error += | (* `Permanent *) Insufficient_attestation_power of { - attestation_power : int; - consensus_threshold : int; + attestation_power : int64; + consensus_threshold : int64; } let () = @@ -44,12 +44,12 @@ let () = ~pp:(fun ppf (attestation_power, consensus_threshold) -> Format.fprintf ppf - "The attestation power (%d) is insufficient to satisfy the consensus \ - threshold (%d)." + "The attestation power (%Ld) is insufficient to satisfy the consensus \ + threshold (%Ld)." attestation_power consensus_threshold) Data_encoding.( - obj2 (req "attestation_power" int31) (req "consensus_threshold" int31)) + obj2 (req "attestation_power" int64) (req "consensus_threshold" int64)) (function | Insufficient_attestation_power {attestation_power; consensus_threshold} -> @@ -58,22 +58,26 @@ let () = (fun (attestation_power, consensus_threshold) -> Insufficient_attestation_power {attestation_power; consensus_threshold}) -let bonus_baking_reward ctxt ~attestation_power = +let bonus_baking_reward ctxt level ~attestation_power = let open Result_syntax in - (* TODO ABAAB *) - let attestation_power = attestation_power.Attestation_power_repr.slots in - let consensus_threshold_size = Constants.consensus_threshold_size ctxt in + (* TODO ABAAB : only works if flag is false *) + let attestation_power = Attestation_power.get ctxt level attestation_power in + let consensus_threshold_size = + Constants.consensus_threshold_size ctxt |> Int64.of_int + in let* baking_reward_bonus_per_slot = Delegate.Rewards.baking_reward_bonus_per_slot ctxt in - let extra_attestation_power = attestation_power - consensus_threshold_size in + let extra_attestation_power = + Int64.sub attestation_power consensus_threshold_size + in let* () = error_when - Compare.Int.(extra_attestation_power < 0) + Compare.Int64.(extra_attestation_power < 0L) (Insufficient_attestation_power {attestation_power; consensus_threshold = consensus_threshold_size}) in - Tez.(baking_reward_bonus_per_slot *? Int64.of_int extra_attestation_power) + Tez.(baking_reward_bonus_per_slot *? extra_attestation_power) type ordered_slots = { delegate : Signature.public_key_hash; @@ -110,6 +114,10 @@ let attesting_rights (ctxt : t) level = (ctxt, Signature.Public_key_hash.Map.empty) slots +let incr_slot att_rights = + let one = Attestation_power.make ~slots:1 ~stake:0L in + Attestation_power.add one att_rights + let attesting_rights_by_first_slot ctxt level : (t * Consensus_key.power Slot.Map.t) tzresult Lwt.t = let open Lwt_result_syntax in @@ -151,10 +159,8 @@ let attesting_rights_by_first_slot ctxt level : { consensus_key; attestation_power = - { - Attestation_power_repr.slots = 1; - (* built at a later step *) stake = 0L; - }; + incr_slot Attestation_power.zero + (* stake added in the next step *); dal_power = in_dal_committee; } | Some @@ -163,11 +169,7 @@ let attesting_rights_by_first_slot ctxt level : Some { consensus_key; - attestation_power = - { - attestation_power with - slots = attestation_power.slots + 1; - }; + attestation_power = incr_slot attestation_power; dal_power = dal_power + in_dal_committee; }) slots_map @@ -193,7 +195,8 @@ let attesting_rights_by_first_slot ctxt level : { v with attestation_power = - {v.attestation_power with stake = weight}; + Attestation_power.( + add (make ~slots:0 ~stake:weight) v.attestation_power); } acc)) Slot.Map.empty diff --git a/src/proto_alpha/lib_protocol/baking.mli b/src/proto_alpha/lib_protocol/baking.mli index 20cee99b3835..9ccc224fbae8 100644 --- a/src/proto_alpha/lib_protocol/baking.mli +++ b/src/proto_alpha/lib_protocol/baking.mli @@ -29,8 +29,8 @@ open Alpha_context type error += | (* `Permanent *) Insufficient_attestation_power of { - attestation_power : int; - consensus_threshold : int; + attestation_power : int64; + consensus_threshold : int64; } type ordered_slots = private { @@ -63,4 +63,4 @@ val attesting_rights_by_first_slot : (** Computes the bonus baking reward depending on the attestation power. *) val bonus_baking_reward : - context -> attestation_power:Attestation_power_repr.t -> Tez.t tzresult + context -> Level.t -> attestation_power:Attestation_power.t -> Tez.t tzresult diff --git a/src/proto_alpha/lib_protocol/delegate_missed_attestations_storage.ml b/src/proto_alpha/lib_protocol/delegate_missed_attestations_storage.ml index 6de36bb5fe2f..a8f7b770ec59 100644 --- a/src/proto_alpha/lib_protocol/delegate_missed_attestations_storage.ml +++ b/src/proto_alpha/lib_protocol/delegate_missed_attestations_storage.ml @@ -62,7 +62,7 @@ type level_participation = Participated | Didn't_participate (* Note that the participation for the last block of a cycle is recorded in the next cycle. *) let record_attesting_participation ctxt ~delegate ~participation - ~attesting_power = + ~attesting_slots = let open Lwt_result_syntax in match participation with | Participated -> Stake_storage.set_active ctxt delegate @@ -71,7 +71,7 @@ let record_attesting_participation ctxt ~delegate ~participation let* result = Storage.Contract.Missed_attestations.find ctxt contract in match result with | Some {remaining_slots; missed_levels} -> - let remaining_slots = remaining_slots - attesting_power in + let remaining_slots = remaining_slots - attesting_slots in Storage.Contract.Missed_attestations.update ctxt contract @@ -108,7 +108,7 @@ let record_attesting_participation ctxt ~delegate ~participation in let minimal_activity = expected_slots * numerator / denominator in let maximal_inactivity = expected_slots - minimal_activity in - let remaining_slots = maximal_inactivity - attesting_power in + let remaining_slots = maximal_inactivity - attesting_slots in Storage.Contract.Missed_attestations.init ctxt contract diff --git a/src/proto_alpha/lib_protocol/delegate_missed_attestations_storage.mli b/src/proto_alpha/lib_protocol/delegate_missed_attestations_storage.mli index ef8366b62c10..bfe77f20079d 100644 --- a/src/proto_alpha/lib_protocol/delegate_missed_attestations_storage.mli +++ b/src/proto_alpha/lib_protocol/delegate_missed_attestations_storage.mli @@ -56,7 +56,7 @@ val record_attesting_participation : Raw_context.t -> delegate:Signature.Public_key_hash.t -> participation:level_participation -> - attesting_power:int -> + attesting_slots:int -> Raw_context.t tzresult Lwt.t (** Update the participation of a delegate as a DAL attester in the current diff --git a/src/proto_alpha/lib_protocol/raw_context.ml b/src/proto_alpha/lib_protocol/raw_context.ml index e9421c6a9728..5871bf83c404 100644 --- a/src/proto_alpha/lib_protocol/raw_context.ml +++ b/src/proto_alpha/lib_protocol/raw_context.ml @@ -2156,6 +2156,8 @@ module type CONSENSUS = sig type round + type attestation_power + type consensus_power val allowed_attestations : t -> consensus_power slot_map option @@ -2168,7 +2170,7 @@ module type CONSENSUS = sig type error += Slot_map_not_found of {loc : string} - val current_attestation_power : t -> Attestation_power_repr.t + val current_attestation_power : t -> attestation_power val initialize_consensus_operation : t -> @@ -2178,14 +2180,10 @@ module type CONSENSUS = sig t val record_attestation : - t -> initial_slot:slot -> power:Attestation_power_repr.t -> t tzresult + t -> initial_slot:slot -> power:attestation_power -> t tzresult val record_preattestation : - t -> - initial_slot:slot -> - power:Attestation_power_repr.t -> - round -> - t tzresult + t -> initial_slot:slot -> power:attestation_power -> round -> t tzresult val forbid_delegate : t -> Signature.Public_key_hash.t -> t @@ -2197,7 +2195,7 @@ module type CONSENSUS = sig val set_preattestations_quorum_round : t -> round -> t - val locked_round_evidence : t -> (round * Attestation_power_repr.t) option + val locked_round_evidence : t -> (round * attestation_power) option val set_attestation_branch : t -> Block_hash.t * Block_payload_hash.t -> t @@ -2212,6 +2210,7 @@ module Consensus : and type 'a level_map := 'a Level_repr.Map.t and type slot_set := Slot_repr.Set.t and type round := Round_repr.t + and type attestation_power := Attestation_power_repr.t and type consensus_power := consensus_power = struct let[@inline] update_consensus_with ctxt f = {ctxt with back = {ctxt.back with consensus = f ctxt.back.consensus}} diff --git a/src/proto_alpha/lib_protocol/raw_context.mli b/src/proto_alpha/lib_protocol/raw_context.mli index 14d52d2ac6a7..cdd628773924 100644 --- a/src/proto_alpha/lib_protocol/raw_context.mli +++ b/src/proto_alpha/lib_protocol/raw_context.mli @@ -374,6 +374,8 @@ module type CONSENSUS = sig type round + type attestation_power + (** Info on the power of a given member of the consensus committee. Contains a consensus_pk, its attesting power and its DAL power. *) type consensus_power @@ -401,7 +403,7 @@ module type CONSENSUS = sig (** [attestation power ctx] returns the attestation power and stake of the current block. *) - val current_attestation_power : t -> Attestation_power_repr.t + val current_attestation_power : t -> attestation_power (** Initializes the map of allowed attestations and preattestations, this function must be called only once and before applying any consensus @@ -420,7 +422,7 @@ module type CONSENSUS = sig [Int_map.find_opt initial_slot allowed_attestation ctx = Some (pkh, power)]. *) val record_attestation : - t -> initial_slot:slot -> power:Attestation_power_repr.t -> t tzresult + t -> initial_slot:slot -> power:attestation_power -> t tzresult (** [record_preattestation ctx ~initial_slot ~power round payload_hash power] records a preattestation for a proposal at @@ -430,11 +432,7 @@ module type CONSENSUS = sig [Int_map.find_opt initial_slot allowed_preattestation ctx = Some (pkh, power)]. *) val record_preattestation : - t -> - initial_slot:slot -> - power:Attestation_power_repr.t -> - round -> - t tzresult + t -> initial_slot:slot -> power:attestation_power -> round -> t tzresult (** [forbid_delegate ctx delegate] adds [delegate] to the set of forbidden delegates, which prevents this delegate from baking or @@ -462,7 +460,7 @@ module type CONSENSUS = sig (** [locked_round_evidence ctx] returns the round of the recorded preattestations as well as their power. *) - val locked_round_evidence : t -> (round * Attestation_power_repr.t) option + val locked_round_evidence : t -> (round * attestation_power) option val set_attestation_branch : t -> Block_hash.t * Block_payload_hash.t -> t @@ -477,6 +475,7 @@ module Consensus : and type 'a level_map := 'a Level_repr.Map.t and type slot_set := Slot_repr.Set.t and type round := Round_repr.t + and type attestation_power := Attestation_power_repr.t and type consensus_power := consensus_power module Sc_rollup_in_memory_inbox : sig 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 0cebaf6a384f..1e0e14232e75 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 @@ -108,12 +108,15 @@ let check_aggregate_result (type kind) (kind : kind aggregate) ~attesters 0 attesters in - if voting_power = total_consensus_power.slots then return_unit + let total_consensus_power = + Alpha_context.Attestation_power.get_slots total_consensus_power + in + if voting_power = total_consensus_power then return_unit else Test.fail "Wrong voting power : expected %d, found %d" voting_power - total_consensus_power.slots + total_consensus_power in (* Check committee *) let expected_committee = @@ -125,7 +128,7 @@ let check_aggregate_result (type kind) (kind : kind aggregate) ~attesters in let resulting_committee = List.map - (fun (a, b) -> (a, b.Attestation_power_repr.slots)) + (fun (a, b) -> (a, Alpha_context.Attestation_power.get_slots b)) resulting_committee in if diff --git a/src/proto_alpha/lib_protocol/validate.ml b/src/proto_alpha/lib_protocol/validate.ml index deae3b3cd691..3b008427eb56 100644 --- a/src/proto_alpha/lib_protocol/validate.ml +++ b/src/proto_alpha/lib_protocol/validate.ml @@ -431,8 +431,8 @@ type block_state = { remaining_block_gas : Gas.Arith.fp; recorded_operations_rev : Operation_hash.t list; last_op_validation_pass : int option; - locked_round_evidence : (Round.t * Attestation_power_repr.t) option; - attestation_power : Attestation_power_repr.t; + locked_round_evidence : (Round.t * Attestation_power.t) option; + attestation_power : Attestation_power.t; } type validation_state = { @@ -480,7 +480,7 @@ let init_block_state vi = recorded_operations_rev = []; last_op_validation_pass = None; locked_round_evidence = None; - attestation_power = Attestation_power_repr.zero; + attestation_power = Attestation_power.zero; } let get_initial_ctxt {info; _} = info.ctxt @@ -751,9 +751,7 @@ module Consensus = struct construction mode. *) Some ( consensus_content.round, - Attestation_power_repr.add - total_attesting_power - attesting_power )) + Attestation_power.add total_attesting_power attesting_power )) in {block_state with locked_round_evidence} @@ -960,9 +958,7 @@ module Consensus = struct { block_state with attestation_power = - Attestation_power_repr.add - block_state.attestation_power - attesting_power; + Attestation_power.add block_state.attestation_power attesting_power; } (* Hypothesis: this function will only be called in mempool mode *) @@ -1335,11 +1331,11 @@ module Consensus = struct | Bls pk -> return ( pk :: public_keys, - Attestation_power_repr.add + Attestation_power.add attestation_power total_attestation_power ) | _ -> tzfail Validate_errors.Consensus.Non_bls_key_in_aggregate) - ([], Attestation_power_repr.zero) + ([], Attestation_power.zero) committee in (* Fail on empty committee *) @@ -1441,7 +1437,7 @@ module Consensus = struct in let* () = check_dal_content info level slot consensus_key dal in let total_power = - Attestation_power_repr.add attestation_power total_power + Attestation_power.add attestation_power total_power in match consensus_key.consensus_pk with | Bls consensus_pk -> ( @@ -1468,7 +1464,7 @@ module Consensus = struct (Missing_companion_key_for_bls_dal (Consensus_key.pkh consensus_key))) | _ -> tzfail Validate_errors.Consensus.Non_bls_key_in_aggregate) - ([], [], Attestation_power_repr.zero) + ([], [], Attestation_power.zero) committee in (* Fail on empty committee *) @@ -4028,11 +4024,13 @@ let check_attestation_power vi bs = return Compare.Int32.(level_position_in_protocol > 1l) in if are_attestations_required then - let required = Constants.consensus_threshold_size vi.ctxt in - (* TODO ABAÆB *) - let provided = bs.attestation_power.slots in + let required = Constants.consensus_threshold_size vi.ctxt |> Int64.of_int in + (* TODO ABAAB : required should depend on the flag *) + let provided = + Attestation_power.get vi.ctxt vi.current_level bs.attestation_power + in fail_unless - Compare.Int.(provided >= required) + Compare.Int64.(provided >= required) (Not_enough_attestations {required; provided}) else return_unit @@ -4076,21 +4074,17 @@ let check_preattestation_round_and_power vi vs round = (Locked_round_after_block_round {locked_round = preattestation_round; round}) in - let consensus_threshold = Constants.consensus_threshold_size vi.ctxt in - let all_bakers_attest_enabled = - Stake_distribution.check_all_bakers_attest_at_level - vi.ctxt - vi.current_level + (* TODO ABAAB : threshold should depend on abaab flag *) + let consensus_threshold = + Constants.consensus_threshold_size vi.ctxt |> Int64.of_int in - if all_bakers_attest_enabled then - error Validate_errors.Consensus.All_bakers_attest_not_implemented - (* TODO ABAAB *) - else - let total_attesting_power = total_attesting_power.slots in - error_when - Compare.Int.(total_attesting_power < consensus_threshold) - (Insufficient_locked_round_evidence - {total_attesting_power; consensus_threshold}) + let total_attesting_power = + Attestation_power.get vi.ctxt vi.current_level total_attesting_power + in + error_when + Compare.Int64.(total_attesting_power < consensus_threshold) + (Insufficient_locked_round_evidence + {total_attesting_power; consensus_threshold}) let check_payload_hash block_state ~predecessor_hash (block_header_contents : Block_header.contents) = diff --git a/src/proto_alpha/lib_protocol/validate_errors.ml b/src/proto_alpha/lib_protocol/validate_errors.ml index a89f60292688..36857f8a5248 100644 --- a/src/proto_alpha/lib_protocol/validate_errors.ml +++ b/src/proto_alpha/lib_protocol/validate_errors.ml @@ -1968,7 +1968,7 @@ let () = module Block = struct (* All block errors are permanent. *) type error += - | Not_enough_attestations of {required : int; provided : int} + | Not_enough_attestations of {required : int64; provided : int64} | Inconsistent_validation_passes_in_block of { expected : int; provided : int; @@ -1982,8 +1982,8 @@ module Block = struct round : Round.t; } | Insufficient_locked_round_evidence of { - total_attesting_power : int; - consensus_threshold : int; + total_attesting_power : int64; + consensus_threshold : int64; } let () = @@ -1997,10 +1997,10 @@ module Block = struct ~pp:(fun ppf (required, provided) -> Format.fprintf ppf - "Wrong number of attestations (%i), at least %i are expected" + "Wrong number of attestations (%Li), at least %Li are expected" provided required) - Data_encoding.(obj2 (req "required" int31) (req "provided" int31)) + Data_encoding.(obj2 (req "required" int64) (req "provided" int64)) (function | Not_enough_attestations {required; provided} -> Some (required, provided) @@ -2080,14 +2080,14 @@ module Block = struct ~pp:(fun ppf (total_attesting_power, consensus_threshold) -> Format.fprintf ppf - "The provided locked round evidence is not sufficient: provided %d \ - total attesting power but was expecting at least %d." + "The provided locked round evidence is not sufficient: provided %Ld \ + total attesting power but was expecting at least %Ld." total_attesting_power consensus_threshold) Data_encoding.( obj2 - (req "total_attesting_power" int31) - (req "consensus_threshold" int31)) + (req "total_attesting_power" int64) + (req "consensus_threshold" int64)) (function | Insufficient_locked_round_evidence {total_attesting_power; consensus_threshold} -> diff --git a/src/proto_alpha/lib_protocol/validate_errors.mli b/src/proto_alpha/lib_protocol/validate_errors.mli index 1fd06f503ed2..c89c5329b1d2 100644 --- a/src/proto_alpha/lib_protocol/validate_errors.mli +++ b/src/proto_alpha/lib_protocol/validate_errors.mli @@ -290,7 +290,7 @@ type error += Failing_noop_error module Block : sig type error += - | Not_enough_attestations of {required : int; provided : int} + | Not_enough_attestations of {required : int64; provided : int64} | Inconsistent_validation_passes_in_block of { expected : int; provided : int; @@ -304,7 +304,7 @@ module Block : sig round : Round.t; } | Insufficient_locked_round_evidence of { - total_attesting_power : int; - consensus_threshold : int; + total_attesting_power : int64; + consensus_threshold : int64; } end diff --git a/teztale/bin_teztale_archiver/alpha_machine.real.ml b/teztale/bin_teztale_archiver/alpha_machine.real.ml index 9271b7d4db54..f76e4e42bee8 100644 --- a/teztale/bin_teztale_archiver/alpha_machine.real.ml +++ b/teztale/bin_teztale_archiver/alpha_machine.real.ml @@ -240,7 +240,7 @@ module Services : Protocol_machinery.PROTOCOL_SERVICES = struct kind; }; delegate = Tezos_crypto.Signature.Of_V2.public_key_hash ck.delegate; - power = power.Protocol.Attestation_power_repr.slots; + power = Protocol.Alpha_context.Attestation_power.get_slots power; } :: acc) acc @@ -276,7 +276,9 @@ module Services : Protocol_machinery.PROTOCOL_SERVICES = struct }; delegate = Tezos_crypto.Signature.Of_V2.public_key_hash delegate; - power = consensus_power.slots; + power = + Protocol.Alpha_context.Attestation_power.get_slots + consensus_power; } :: acc | Receipt @@ -298,7 +300,9 @@ module Services : Protocol_machinery.PROTOCOL_SERVICES = struct }; delegate = Tezos_crypto.Signature.Of_V2.public_key_hash delegate; - power = consensus_power.slots; + power = + Protocol.Alpha_context.Attestation_power.get_slots + consensus_power; } :: acc | Receipt -- GitLab