From cdbff023b85759ea995642646d1041cde5bddffa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Cauderlier?= Date: Fri, 19 Jan 2024 11:44:56 +0100 Subject: [PATCH 1/4] Proto: Dal.make returns the context Dal.make is a computation-intensive function which loads the DAL SRS and returns a so-called cryptobox. In order to memoize its result in the context, this commit modifies its interface to return a context (currently untouched but this will change in next commit). --- src/proto_alpha/lib_protocol/alpha_context.mli | 2 +- src/proto_alpha/lib_protocol/dal_apply.ml | 2 +- src/proto_alpha/lib_protocol/raw_context.ml | 2 +- src/proto_alpha/lib_protocol/raw_context.mli | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/proto_alpha/lib_protocol/alpha_context.mli b/src/proto_alpha/lib_protocol/alpha_context.mli index 9345ee7cc1b3..d3b015125674 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 c2716ade29cf..6e2f88994f7a 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 58fd927a9289..40615869e427 100644 --- a/src/proto_alpha/lib_protocol/raw_context.ml +++ b/src/proto_alpha/lib_protocol/raw_context.ml @@ -1780,7 +1780,7 @@ module Dal = struct ctxt.back.constants in match Dal.make cryptobox_parameters with - | Ok cryptobox -> return cryptobox + | Ok cryptobox -> return (ctxt, cryptobox) | Error (`Fail explanation) -> tzfail (Dal_errors_repr.Dal_cryptobox_error {explanation}) diff --git a/src/proto_alpha/lib_protocol/raw_context.mli b/src/proto_alpha/lib_protocol/raw_context.mli index 12f7c303fcef..327b8ed2c0a2 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 -- GitLab From 3537df9d5dfd3df85ceba7a62f75fa32143ef033 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Cauderlier?= Date: Fri, 19 Jan 2024 11:48:52 +0100 Subject: [PATCH 2/4] Proto: memoize Dal.make --- src/proto_alpha/lib_protocol/raw_context.ml | 23 ++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/proto_alpha/lib_protocol/raw_context.ml b/src/proto_alpha/lib_protocol/raw_context.ml index 40615869e427..b81fd58726ec 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 (ctxt, 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 -- GitLab From 91c35514ea5d28470ca30675ceea0454bf3b8e9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Cauderlier?= Date: Fri, 19 Jan 2024 11:51:34 +0100 Subject: [PATCH 3/4] Proto/Changes: mention !11594 --- docs/protocols/alpha.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/protocols/alpha.rst b/docs/protocols/alpha.rst index 634598caf311..7731bf7581ab 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) ---------------------------- -- GitLab From 324fe80dc0c0b410861891f3fdb1564de3e7b2ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Cauderlier?= Date: Fri, 19 Jan 2024 12:02:01 +0100 Subject: [PATCH 4/4] Benchmark/Proto: Memoize the Dal cryptobox. This commit forces the loading of the DAL SRS before benchmarking the slot header publication operation. This ensures that the loading is not part of the computation being benchmarked. --- src/proto_alpha/lib_benchmarks_proto/dal_benchmarks.ml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/proto_alpha/lib_benchmarks_proto/dal_benchmarks.ml b/src/proto_alpha/lib_benchmarks_proto/dal_benchmarks.ml index 26b8ddea1581..394bbc6c6af4 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 -- GitLab