From 481217f48b6e6c9124ff97334f5d1510359e67ff Mon Sep 17 00:00:00 2001 From: Eugen Zalinescu Date: Tue, 6 Feb 2024 13:25:13 +0100 Subject: [PATCH] Alpha/DAL: enhance validation of attestations in mempool --- src/proto_alpha/lib_protocol/dal_apply.ml | 13 ++++++++++++- src/proto_alpha/lib_protocol/dal_apply.mli | 20 +++++++++++++++----- src/proto_alpha/lib_protocol/validate.ml | 9 ++++++++- 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/src/proto_alpha/lib_protocol/dal_apply.ml b/src/proto_alpha/lib_protocol/dal_apply.ml index 08aa72783d70..d9387a71ba31 100644 --- a/src/proto_alpha/lib_protocol/dal_apply.ml +++ b/src/proto_alpha/lib_protocol/dal_apply.ml @@ -53,7 +53,7 @@ let slot_of_int_e ~number_of_slots n = let pkh_of_consensus_key (consensus_key : Consensus_key.pk) = consensus_key.delegate -let validate_attestation ctxt level consensus_key attestation = +let validate_block_attestation ctxt level consensus_key attestation = let open Lwt_result_syntax in let*? () = assert_dal_feature_enabled ctxt in let number_of_slots = Dal.number_of_slots ctxt in @@ -70,6 +70,17 @@ let validate_attestation ctxt level consensus_key attestation = (Option.is_none @@ Dal.Attestation.shards_of_attester ctxt ~attester) (Dal_data_availibility_attester_not_in_committee {attester; level}) +let validate_mempool_attestation ctxt attestation = + let open Lwt_result_syntax in + let*? () = assert_dal_feature_enabled ctxt in + let number_of_slots = Dal.number_of_slots ctxt in + let*? max_index = number_of_slots - 1 |> slot_of_int_e ~number_of_slots in + let maximum_size = Dal.Attestation.expected_size_in_bits ~max_index in + let size = Dal.Attestation.occupied_size_in_bits attestation in + fail_unless + Compare.Int.(size <= maximum_size) + (Dal_attestation_size_limit_exceeded {maximum_size; got = size}) + let validate_dal_attestation ctxt get_consensus_key_and_round_opt op = let open Lwt_result_syntax in let*? () = assert_dal_feature_enabled ctxt in diff --git a/src/proto_alpha/lib_protocol/dal_apply.mli b/src/proto_alpha/lib_protocol/dal_apply.mli index 7ea682789ed9..3b0ecec076da 100644 --- a/src/proto_alpha/lib_protocol/dal_apply.mli +++ b/src/proto_alpha/lib_protocol/dal_apply.mli @@ -28,22 +28,32 @@ open Alpha_context -(** [validate_attestation ctxt level consensus_key attestation] checks whether - the DAL attestation [attestation] emitted at given [level] by the attester - with the given [consensus_key] is valid. If an [Error _] is returned, the - [op] is not valid. The checks made are: +(** [validate_block_attestation ctxt level consensus_key attestation] checks + whether the DAL attestation [attestation] emitted at given [level] by the + attester with the given [consensus_key] is valid for block inclusion. If an + [Error _] is returned, the [op] is not valid. The checks made are: * the attestation size does not exceed the maximum; * the delegate is in the DAL committee. These are checks done for the DAL part alone, checks on other fields of an attestation (like level, round, slot) are done by the caller. *) -val validate_attestation : +val validate_block_attestation : t -> Raw_level.t -> Consensus_key.pk -> Dal.Attestation.t -> unit tzresult Lwt.t +(** [validate_mempool_attestation ctxt level consensus_key attestation] checks + whether the DAL attestation [attestation] is valid for the mempool. It is + similar to [check_block_attestion], but it performs only the check on the + size, as [consensus_key] is not available. If an [Error _] is returned, the + [op] is not valid. + + These are checks done for the DAL part alone, checks on other fields of an + attestation (like level, round, slot) are done by the caller. *) +val validate_mempool_attestation : t -> Dal.Attestation.t -> unit tzresult Lwt.t + (** [validate_dal_attestation ctxt get_consensus_key_and_round op] checks whether the DAL attestation [op] is valid. If an [Error _] is returned, the [op] is not valid. The checks made are: diff --git a/src/proto_alpha/lib_protocol/validate.ml b/src/proto_alpha/lib_protocol/validate.ml index d0231f2afc09..ee449695c475 100644 --- a/src/proto_alpha/lib_protocol/validate.ml +++ b/src/proto_alpha/lib_protocol/validate.ml @@ -724,7 +724,7 @@ module Consensus = struct Option.fold ~none:return_unit ~some:(fun dal -> - Dal_apply.validate_attestation + Dal_apply.validate_block_attestation vi.ctxt level consensus_key @@ -753,6 +753,13 @@ module Consensus = struct consensus_content dal_content | Mempool -> + let* () = + Option.fold + ~none:return_unit + ~some:(fun dal -> + Dal_apply.validate_mempool_attestation vi.ctxt dal.attestation) + dal_content + in check_mempool_consensus vi consensus_info -- GitLab