diff --git a/docs/protocols/alpha.rst b/docs/protocols/alpha.rst index 634598caf311ff82001f41ea8d3f4bca21f10d6c..7731bf7581abcc5ac824b1c48c62823802a61662 100644 --- a/docs/protocols/alpha.rst +++ b/docs/protocols/alpha.rst @@ -32,6 +32,9 @@ Data Availability Layer (ongoing) - Introduced a ``round`` field in DAL attestations, with a similar meaning as for consensus attestations. (MR :gl:`!11285`) +- Optimize the slot header publication operation by memoizing the + cryptobox. (MR :gl:`!11594`) + Adaptive Issuance (ongoing) ---------------------------- diff --git a/src/proto_alpha/lib_benchmarks_proto/dal_benchmarks.ml b/src/proto_alpha/lib_benchmarks_proto/dal_benchmarks.ml index 26b8ddea158197cd96d733d7404e6cb717ba4e02..394bbc6c6af4b8f5aeca5dde713d199ae653d0fc 100644 --- a/src/proto_alpha/lib_benchmarks_proto/dal_benchmarks.ml +++ b/src/proto_alpha/lib_benchmarks_proto/dal_benchmarks.ml @@ -74,7 +74,7 @@ module Publish_slot_header : Benchmark.S = struct {slot_index; commitment; commitment_proof} let make_bench rng_state (config : config) () : workload Generator.benchmark = - let open Lwt_result_syntax in + let open Lwt_result_wrap_syntax in let bench_promise = let dal = { @@ -90,7 +90,8 @@ module Publish_slot_header : Benchmark.S = struct | Error (`Fail msg) -> failwith "Dal_benchmarks: failed to initialize cryptobox (%s)" msg in - + (* Memoize the cryptobox in the context *) + let*?@ ctxt, _abstract_cryptobox = Alpha_context.Dal.make ctxt in let* op = match operation_generator cryptobox rng_state with | Ok op -> return op diff --git a/src/proto_alpha/lib_protocol/alpha_context.mli b/src/proto_alpha/lib_protocol/alpha_context.mli index 9345ee7cc1b3ea5ee423d70824ae3a95e156e7b0..d3b015125674327e211eb3067c6be409fe6fcf06 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/alpha_context.mli @@ -2655,7 +2655,7 @@ module Dal : sig type cryptobox - val make : context -> cryptobox tzresult + val make : context -> (context * cryptobox) tzresult val number_of_slots : context -> int diff --git a/src/proto_alpha/lib_protocol/dal_apply.ml b/src/proto_alpha/lib_protocol/dal_apply.ml index c2716ade29cf15c52b4c55d73677abe646ab06a6..6e2f88994f7a646866b4206f2bdae7b1f3ef6874 100644 --- a/src/proto_alpha/lib_protocol/dal_apply.ml +++ b/src/proto_alpha/lib_protocol/dal_apply.ml @@ -129,7 +129,7 @@ let apply_publish_slot_header ctxt operation = let open Result_syntax in let* ctxt = Gas.consume ctxt Dal_costs.cost_Dal_publish_slot_header in let number_of_slots = Dal.number_of_slots ctxt in - let* cryptobox = Dal.make ctxt in + let* ctxt, cryptobox = Dal.make ctxt in let current_level = (Level.current ctxt).level in let* slot_header = Dal.Operations.Publish_slot_header.slot_header diff --git a/src/proto_alpha/lib_protocol/raw_context.ml b/src/proto_alpha/lib_protocol/raw_context.ml index 58fd927a9289964a50cf9c4c924c3149af435e4e..b81fd58726eceee948e9e328939fd77fe33eb1e0 100644 --- a/src/proto_alpha/lib_protocol/raw_context.ml +++ b/src/proto_alpha/lib_protocol/raw_context.ml @@ -273,6 +273,7 @@ type back = { dummy slot headers. *) dal_attestation_slot_accountability : Dal_attestation_repr.Accountability.t; dal_committee : dal_committee; + dal_cryptobox : Dal.t option; adaptive_issuance_enable : bool; } @@ -887,6 +888,7 @@ let prepare ~level ~predecessor_timestamp ~timestamp ~adaptive_issuance_enable Dal_attestation_repr.Accountability.init ~length:constants.Constants_parametric_repr.dal.number_of_slots; dal_committee = empty_dal_committee; + dal_cryptobox = None; adaptive_issuance_enable; }; } @@ -1776,13 +1778,20 @@ module Dal = struct let make ctxt = let open Result_syntax in - let Constants_parametric_repr.{dal = {cryptobox_parameters; _}; _} = - ctxt.back.constants - in - match Dal.make cryptobox_parameters with - | Ok cryptobox -> return cryptobox - | Error (`Fail explanation) -> - tzfail (Dal_errors_repr.Dal_cryptobox_error {explanation}) + (* Dal.make takes some time (on the order of 10ms) so we memoize + its result to avoid calling it more than once per block. *) + match ctxt.back.dal_cryptobox with + | Some cryptobox -> return (ctxt, cryptobox) + | None -> ( + let Constants_parametric_repr.{dal = {cryptobox_parameters; _}; _} = + ctxt.back.constants + in + match Dal.make cryptobox_parameters with + | Ok cryptobox -> + let back = {ctxt.back with dal_cryptobox = Some cryptobox} in + return ({ctxt with back}, cryptobox) + | Error (`Fail explanation) -> + tzfail (Dal_errors_repr.Dal_cryptobox_error {explanation})) let number_of_slots ctxt = ctxt.back.constants.dal.number_of_slots diff --git a/src/proto_alpha/lib_protocol/raw_context.mli b/src/proto_alpha/lib_protocol/raw_context.mli index 12f7c303fcefcf41dfeac58c7e32e23378c5e7c5..327b8ed2c0a28d5c4d98e235f88134517c015bb1 100644 --- a/src/proto_alpha/lib_protocol/raw_context.mli +++ b/src/proto_alpha/lib_protocol/raw_context.mli @@ -428,7 +428,7 @@ end module Dal : sig type cryptobox = Dal.t - val make : t -> cryptobox tzresult + val make : t -> (t * cryptobox) tzresult val number_of_slots : t -> int