diff --git a/manifest/main.ml b/manifest/main.ml index 4cafc00c47e937ffa366764a2125daa34bf588a7..09385642140604efe15cb432d995a10559499be4 100644 --- a/manifest/main.ml +++ b/manifest/main.ml @@ -5472,7 +5472,7 @@ let _octez_dal_node = octez_stdlib |> open_; octez_dal_node_lib |> open_; octez_dal_node_services |> open_; - octez_crypto_dal; + octez_crypto_dal |> open_; irmin_pack; irmin_pack_unix; irmin; diff --git a/src/bin_dal_node/configuration.ml b/src/bin_dal_node/configuration.ml index 5361e1ad744ab917081dfd2971f61f87c3facc7f..e08bb5a9cc5bf112936377753ae06866de525001 100644 --- a/src/bin_dal_node/configuration.ml +++ b/src/bin_dal_node/configuration.ml @@ -24,7 +24,7 @@ (*****************************************************************************) type t = { - unsafe_srs : bool; + use_unsafe_srs : bool; data_dir : string; rpc_addr : string; rpc_port : int; @@ -42,15 +42,21 @@ let default_rpc_addr = "127.0.0.1" let default_rpc_port = 10732 +let default_use_unsafe_srs = false + let encoding : t Data_encoding.t = let open Data_encoding in conv - (fun {unsafe_srs; data_dir; rpc_addr; rpc_port} -> - (unsafe_srs, data_dir, rpc_addr, rpc_port)) - (fun (unsafe_srs, data_dir, rpc_addr, rpc_port) -> - {unsafe_srs; data_dir; rpc_addr; rpc_port}) + (fun {use_unsafe_srs; data_dir; rpc_addr; rpc_port} -> + (use_unsafe_srs, data_dir, rpc_addr, rpc_port)) + (fun (use_unsafe_srs, data_dir, rpc_addr, rpc_port) -> + {use_unsafe_srs; data_dir; rpc_addr; rpc_port}) (obj4 - (dft "unsafe_srs" ~description:"use unsafe srs for tests" bool false) + (dft + "use_unsafe_srs" + ~description:"use unsafe srs for tests" + bool + default_use_unsafe_srs) (dft "data-dir" ~description:"Location of the data dir" diff --git a/src/bin_dal_node/cryptobox.ml b/src/bin_dal_node/configuration.mli similarity index 51% rename from src/bin_dal_node/cryptobox.ml rename to src/bin_dal_node/configuration.mli index b84bd3465a941d2d9d4cb6424595e24a983c8d57..fcfcb7c054c23fb13f7e40ac66bcf09dee31ad6b 100644 --- a/src/bin_dal_node/cryptobox.ml +++ b/src/bin_dal_node/configuration.mli @@ -22,61 +22,29 @@ (* DEALINGS IN THE SOFTWARE. *) (* *) (*****************************************************************************) -open Tezos_crypto_dal -include Dal_cryptobox -type slot = bytes +type t = { + use_unsafe_srs : bool; + (** Run dal-node in test mode with an unsafe SRS (Trusted setup) *) + data_dir : string; (** The path to the DAL node data directory *) + rpc_addr : string; (** The address the DAL node listens to *) + rpc_port : int; (** The port the DAL node listens to *) +} -type slot_header = commitment +(** [filename config] gets the path to config file *) +val filename : t -> string -let slot_header_encoding = Commitment.encoding +(** [data_dir_path config subpath] builds a subpath relatively to the + [config] *) +val data_dir_path : t -> string -> string -type parameters = { - redundancy_factor : int; - segment_size : int; - slot_size : int; - number_of_shards : int; -} +val default_data_dir : string + +val default_rpc_addr : string -type error += Cryptobox_initialisation_failed of string +val default_rpc_port : int -let () = - register_error_kind - `Permanent - ~id:"dal.node.cryptobox.initialisation_failed" - ~title:"Cryptobox initialisation failed" - ~description:"Unable to initialise the cryptobox parameters" - ~pp:(fun ppf msg -> - Format.fprintf - ppf - "Unable to initialise the cryptobox parameters. Reason: %s" - msg) - Data_encoding.(obj1 (req "error" string)) - (function Cryptobox_initialisation_failed str -> Some str | _ -> None) - (fun str -> Cryptobox_initialisation_failed str) +(** [save config] writes config file in [config.data_dir] *) +val save : t -> unit tzresult Lwt.t -let init unsafe_srs cctxt (module Plugin : Dal_constants_plugin.T) = - let open Lwt_result_syntax in - let* Plugin.{redundancy_factor; segment_size; slot_size; number_of_shards} = - Plugin.get_constants cctxt#chain cctxt#block cctxt - in - let parameters = - {redundancy_factor; segment_size; slot_size; number_of_shards} - in - let* initialisation_parameters = - if unsafe_srs then - return - @@ Internal_for_tests.initialisation_parameters_from_slot_size ~slot_size - else - let*? g1_path, g2_path = Tezos_base.Dal_srs.find_trusted_setup_files () in - initialisation_parameters_from_files ~g1_path ~g2_path - in - let*? () = load_parameters initialisation_parameters in - let* dal_constants = - match - make ~redundancy_factor ~segment_size ~slot_size ~number_of_shards - with - | Ok cryptobox -> return cryptobox - | Error (`Fail msg) -> fail [Cryptobox_initialisation_failed msg] - in - return @@ (dal_constants, parameters) +val load : data_dir:string -> (t, Error_monad.tztrace) result Lwt.t diff --git a/src/bin_dal_node/daemon.ml b/src/bin_dal_node/daemon.ml index 3e8b550ed43dcd9d6289a2fc420992a9781a26ed..b1d42131a46c9656212283b9338d1db969e7243e 100644 --- a/src/bin_dal_node/daemon.ml +++ b/src/bin_dal_node/daemon.ml @@ -23,8 +23,6 @@ (* *) (*****************************************************************************) -let daemonize cctxt handle = Lwt.no_cancel @@ Layer1.iter_events cctxt handle - let resolve_plugin cctxt = let open Lwt_result_syntax in let* protocols = @@ -35,6 +33,59 @@ let resolve_plugin cctxt = (Dal_constants_plugin.get protocols.current_protocol) (Dal_constants_plugin.get protocols.next_protocol) +type error += Cryptobox_initialisation_failed of string + +let () = + register_error_kind + `Permanent + ~id:"dal.node.cryptobox.initialisation_failed" + ~title:"Cryptobox initialisation failed" + ~description:"Unable to initialise the cryptobox parameters" + ~pp:(fun ppf msg -> + Format.fprintf + ppf + "Unable to initialise the cryptobox parameters. Reason: %s" + msg) + Data_encoding.(obj1 (req "error" string)) + (function Cryptobox_initialisation_failed str -> Some str | _ -> None) + (fun str -> Cryptobox_initialisation_failed str) + +let init_cryptobox unsafe_srs cctxt (module Plugin : Dal_constants_plugin.T) = + let open Cryptobox in + let open Lwt_result_syntax in + let* parameters = Plugin.get_constants cctxt#chain cctxt#block cctxt in + let* initialisation_parameters = + if unsafe_srs then + return + @@ Cryptobox.Internal_for_tests.initialisation_parameters_from_slot_size + ~slot_size:parameters.slot_size + else + let*? g1_path, g2_path = Tezos_base.Dal_srs.find_trusted_setup_files () in + Cryptobox.initialisation_parameters_from_files ~g1_path ~g2_path + in + let*? () = Cryptobox.load_parameters initialisation_parameters in + match Cryptobox.make parameters with + | Ok cryptobox -> return (cryptobox, parameters) + | Error (`Fail msg) -> fail [Cryptobox_initialisation_failed msg] + +let daemonize cctxt handle = + let open Lwt_result_syntax in + let* t, stopper = Layer1.on_new_head cctxt handle in + let (_ : Lwt_exit.clean_up_callback_id) = + (* close the stream when an exit signal is received *) + Lwt_exit.register_clean_up_callback ~loc:__LOC__ (fun _exit_status -> + stopper () ; + Lwt.return_unit) + in + (* The no_cancel might not be necessary. Lwt_exit cancels the + main promise upon receiving a signal or other form of interruption. The + no_cancel renders this cancelation into a no-op.*) + Lwt.no_cancel t + +(* FIXME: https://gitlab.com/tezos/tezos/-/issues/3605 + + Improve general architecture, handle L1 disconnection etc +*) let run ~data_dir cctxt = let open Lwt_result_syntax in let*! () = Event.(emit starting_node) () in @@ -43,29 +94,31 @@ let run ~data_dir cctxt = let*! store = Store.init config in let ready = ref false in let*! () = Event.(emit layer1_node_tracking_started ()) in - daemonize cctxt @@ fun (_hash, (_block_header : Tezos_base.Block_header.t)) -> - if not !ready then - let* plugin = resolve_plugin cctxt in - match plugin with - | Some plugin -> - let (module Plugin : Dal_constants_plugin.T) = plugin in - let*! () = - Event.( - emit - protocol_plugin_resolved - (Format.asprintf "%a" Protocol_hash.pp_short Plugin.Proto.hash)) - in - let* dal_constants, dal_parameters = - Cryptobox.init config.unsafe_srs cctxt plugin - in - let ctxt = Node_context.make config dal_constants dal_parameters in - let* rpc_server = RPC_server.(start config (register ctxt store)) in - let _ = RPC_server.install_finalizer rpc_server in - let*! () = - Event.(emit rpc_server_is_ready (config.rpc_addr, config.rpc_port)) - in - let*! () = Event.(emit node_is_ready ()) in - ready := true ; - return_unit - | None -> return_unit - else return_unit + let new_head_handler (_hash, (_block_header : Tezos_base.Block_header.t)) = + (* Try to resolve the protocol plugin corresponding to the protocol of the + targeted node. *) + if not !ready then + let* plugin = resolve_plugin cctxt in + match plugin with + | Some plugin -> + let (module Plugin : Dal_constants_plugin.T) = plugin in + let*! () = Event.emit_protocol_plugin_resolved Plugin.Proto.hash in + let* dal_constants, dal_parameters = + init_cryptobox config.use_unsafe_srs cctxt plugin + in + let ctxt = Node_context.make config dal_constants dal_parameters in + let* rpc_server = RPC_server.(start config (register ctxt store)) in + let _ = RPC_server.install_finalizer rpc_server in + let*! () = + Event.(emit rpc_server_is_ready (config.rpc_addr, config.rpc_port)) + in + let*! () = Event.(emit node_is_ready ()) in + ready := true ; + return_unit + | None -> return_unit + else + (* If rpc and plugin are ready, there is nothing else to do. + Future work will update this part of the code *) + return_unit + in + daemonize cctxt new_head_handler diff --git a/src/bin_dal_node/dune b/src/bin_dal_node/dune index 66b66bee44c34bdbe96cf8e9af21d32d66ff2ecf..a0fa01a5e84890d0aa6f175b66f94ba4d8f0ba27 100644 --- a/src/bin_dal_node/dune +++ b/src/bin_dal_node/dune @@ -40,6 +40,7 @@ -open Tezos_stdlib_unix -open Tezos_stdlib -open Tezos_dal_node_lib - -open Tezos_dal_node_services)) + -open Tezos_dal_node_services + -open Tezos_crypto_dal)) (rule (action (progn (write-file void_for_linking-tezos-dal-alpha.empty "")))) diff --git a/src/bin_dal_node/event.ml b/src/bin_dal_node/event.ml index d3b99aa2f03678e40c2bf719e2fa7c9cee895f8d..e1301dd9e0c8b40460a5a46ab8ff95ba358fb609 100644 --- a/src/bin_dal_node/event.ml +++ b/src/bin_dal_node/event.ml @@ -120,3 +120,9 @@ let protocol_plugin_resolved = ~msg:"Resolved plugin on protocol {proto_hash}" ~level:Notice ("proto_hash", Data_encoding.string) + +let proto_short_hash_string hash = + Format.asprintf "%a" Protocol_hash.pp_short hash + +let emit_protocol_plugin_resolved hash = + emit protocol_plugin_resolved (proto_short_hash_string hash) diff --git a/src/bin_dal_node/layer1.ml b/src/bin_dal_node/layer1.ml index 1362f41c6a8a5c6c7cb7c8c69199032b62467b95..a269c7fc2286bcda999bf6eaebb347ceb42dac1a 100644 --- a/src/bin_dal_node/layer1.ml +++ b/src/bin_dal_node/layer1.ml @@ -23,24 +23,14 @@ (* *) (*****************************************************************************) -(** FIXME: https://gitlab.com/tezos/tezos/-/issues/3517 - +(* FIXME: https://gitlab.com/tezos/tezos/-/issues/3517 If the layer1 node reboots, the rpc stream breaks. *) -let chain_events cctxt = - let open Lwt_result_syntax in - let* heads, _ = Tezos_shell_services.Monitor_services.heads cctxt `Main in - return heads - -let handle_event (hash, (block_header : Tezos_base.Block_header.t)) = +let on_new_head cctxt handle = let open Lwt_result_syntax in - let level = block_header.shell.level in - let*! () = Event.(emit layer1_node_new_head (hash, level)) in - return_true - -let iter_events cctxt handle = - let open Lwt_result_syntax in - let* stream = chain_events cctxt in + let* stream, stopper = + Tezos_shell_services.Monitor_services.heads cctxt `Main + in let rec go () = Lwt.bind (Lwt_stream.get stream) @@ fun tok -> match tok with @@ -49,4 +39,4 @@ let iter_events cctxt handle = let* () = handle element in go () in - go () + return (go (), stopper) diff --git a/src/bin_dal_node/layer1.mli b/src/bin_dal_node/layer1.mli new file mode 100644 index 0000000000000000000000000000000000000000..fec865062cf545c6177c075c325086248e36537d --- /dev/null +++ b/src/bin_dal_node/layer1.mli @@ -0,0 +1,35 @@ +(*****************************************************************************) +(* *) +(* Open Source License *) +(* Copyright (c) 2022 Nomadic Labs, *) +(* *) +(* Permission is hereby granted, free of charge, to any person obtaining a *) +(* copy of this software and associated documentation files (the "Software"),*) +(* to deal in the Software without restriction, including without limitation *) +(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *) +(* and/or sell copies of the Software, and to permit persons to whom the *) +(* Software is furnished to do so, subject to the following conditions: *) +(* *) +(* The above copyright notice and this permission notice shall be included *) +(* in all copies or substantial portions of the Software. *) +(* *) +(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*) +(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *) +(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *) +(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*) +(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *) +(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *) +(* DEALINGS IN THE SOFTWARE. *) +(* *) +(*****************************************************************************) + +(** [on_new_head cctxt handler] opens a [Monitor_services.heads] stream on the + targeted layer 1 node and calls [handler] each time a new head is received. + + It returns a couple [(p, stopper)] where [p] is a promise resolving when the + stream closes and [stopper] a function closing the stream. +*) +val on_new_head : + #RPC_context.streamed -> + (Block_hash.t * Block_header.t -> unit tzresult Lwt.t) -> + (unit tzresult Lwt.t * (unit -> unit)) tzresult Lwt.t diff --git a/src/bin_dal_node/main_dal.ml b/src/bin_dal_node/main_dal.ml index c0760575d3186d942d32401686c20a56f43fe13e..fe93429fb1bea9d509015768251e6ccf6b147c7f 100644 --- a/src/bin_dal_node/main_dal.ml +++ b/src/bin_dal_node/main_dal.ml @@ -23,8 +23,7 @@ (* *) (*****************************************************************************) -let group = - {Clic.name = "dal-daemon"; title = "Commands related to the DAL daemon"} +let group = {Clic.name = "dal-node"; title = "Commands related to the DAL node"} let data_dir_arg = let default = Configuration.default_data_dir in @@ -33,7 +32,7 @@ let data_dir_arg = ~placeholder:"data-dir" ~doc: (Format.sprintf - "The path to the DAL daemon data directory. Default value is %s" + "The path to the DAL node data directory. Default value is %s" default) ~default (Client_config.string_parameter ()) @@ -45,8 +44,7 @@ let rpc_addr_arg = ~placeholder:"rpc-address|ip" ~doc: (Format.sprintf - "The address the smart-contract rollup node listens to. Default value \ - is %s" + "The address the DAL node listens to. Default value is %s" default) ~default (Client_config.string_parameter ()) @@ -63,15 +61,14 @@ let rpc_port_arg = ~placeholder:"rpc-port" ~doc: (Format.sprintf - "The port the smart-contract rollup node listens to. Default value is \ - %s" + "The port the DAL node listens to. Default value is %s" default) ~default int_parameter -let unsafe_srs_for_tests_arg = +let use_unsafe_srs_for_tests_arg = Clic.switch - ~long:"unsafe-srs-for-tests" + ~long:"use-unsafe-srs-for-tests" ~doc: (Format.sprintf "Run dal-node in test mode with an unsafe SRS (Trusted setup)") @@ -83,11 +80,11 @@ let config_init_command = command ~group ~desc:"Configure DAL node." - (args4 data_dir_arg rpc_addr_arg rpc_port_arg unsafe_srs_for_tests_arg) + (args4 data_dir_arg rpc_addr_arg rpc_port_arg use_unsafe_srs_for_tests_arg) (prefixes ["init-config"] stop) - (fun (data_dir, rpc_addr, rpc_port, unsafe_srs) cctxt -> + (fun (data_dir, rpc_addr, rpc_port, use_unsafe_srs) cctxt -> let open Configuration in - let config = {data_dir; rpc_addr; rpc_port; unsafe_srs} in + let config = {data_dir; rpc_addr; rpc_port; use_unsafe_srs} in let* () = save config in let*! _ = cctxt#message "DAL node configuration written in %s" (filename config) @@ -98,7 +95,7 @@ let run_command = let open Clic in command ~group - ~desc:"Run the DAL daemon." + ~desc:"Run the DAL node." (args1 data_dir_arg) (prefixes ["run"] @@ stop) (fun data_dir cctxt -> Daemon.run ~data_dir cctxt) diff --git a/src/bin_dal_node/slot_manager.ml b/src/bin_dal_node/slot_manager.ml index b281c7127fddd390412c736c34632af40ca164de..b05757ae53aa8edfbbbdd715500bd9685dee8b87 100644 --- a/src/bin_dal_node/slot_manager.ml +++ b/src/bin_dal_node/slot_manager.ml @@ -88,6 +88,8 @@ let () = (function Illformed_shard -> Some () | _ -> None) (fun () -> Illformed_shard) +type slot = bytes + let wrap_encoding_error = Result.map_error (fun e -> [Tezos_base.Data_encoding_wrapper.Encoding_error e]) @@ -133,7 +135,7 @@ let split_and_store cb_constants store slot = let get_shard store slot_header shard_id = let open Lwt_result_syntax in - let*? slot_header = encode Cryptobox.slot_header_encoding slot_header in + let*? slot_header = encode Cryptobox.Commitment.encoding slot_header in let* share = Lwt.catch (fun () -> @@ -189,7 +191,7 @@ let get_slot initial_constants dal_constants store slot_header = in return slot -(* FIXME https://gitlab.com/tezos/tezos/-/issues/3405 +(* FIXME: https://gitlab.com/tezos/tezos/-/issues/3405 This can work only if a slot never ends with a `\000`. But I am not sure in general such thing is required. *) diff --git a/src/bin_dal_node/slot_manager.mli b/src/bin_dal_node/slot_manager.mli index c338b0932e0d30bb307ed47b49499ab595f4d42a..e4e6bd1d3c9e18543dad6fb9afcd66eba812380f 100644 --- a/src/bin_dal_node/slot_manager.mli +++ b/src/bin_dal_node/slot_manager.mli @@ -30,19 +30,18 @@ - reading a slot means rebuild it from the shards *) +type slot = bytes + (** [split_and_store dal_constants ts store slot] splits [slot] in shards, stores it onto the disk and returns the corresponding [slot_header], using [dal_constants] and trusted setup [ts] *) val split_and_store : - Cryptobox.t -> - Store.t -> - Cryptobox.slot -> - Cryptobox.slot_header tzresult Lwt.t + Cryptobox.t -> Store.t -> slot -> Cryptobox.Commitment.t tzresult Lwt.t (** [get_shard store slot_header shard_id] gets the shard associated to [slot_header] at the range [shard_id] *) val get_shard : - Store.t -> Cryptobox.slot_header -> int -> Cryptobox.shard tzresult Lwt.t + Store.t -> Cryptobox.commitment -> int -> Cryptobox.shard tzresult Lwt.t (** [get_slot dal_parameters dal_constants store slot_header] fetches from disk the shards associated to [slot_header], gathers them, rebuilds and @@ -51,15 +50,15 @@ val get_slot : Cryptobox.parameters -> Cryptobox.t -> Store.t -> - Cryptobox.slot_header -> - Cryptobox.slot tzresult Lwt.t + Cryptobox.commitment -> + slot tzresult Lwt.t module Utils : sig - (** [trim_x00 b] removes trailing '\000' at the end of a [b] and returns a new - [bytes]. This function in needed to debug the fetching a slot and remove - spurious uneeded data form it. *) - val trim_x00 : bytes -> bytes + (** [trim_x00 s] removes trailing '\000' at the end of [s] and returns + a new [slot]. This function is needed to debug the fetched slot and + remove spurious uneeded data form it. *) + val trim_x00 : slot -> slot - (** [fill_x00 slot_size b] fills a bytes with '\000' to match [slot_size] *) - val fill_x00 : int -> bytes -> bytes + (** [fill_x00 slot_size s] fills [s] with '\000' to match [slot_size] *) + val fill_x00 : int -> slot -> slot end diff --git a/src/bin_node/node_config_file.ml b/src/bin_node/node_config_file.ml index 2b6231a3fb575d049756acae258f4fb77a50c0fe..c7d77d8a0848f9966eac7633106d034e6db7dde2 100644 --- a/src/bin_node/node_config_file.ml +++ b/src/bin_node/node_config_file.ml @@ -1649,7 +1649,7 @@ let bootstrap_peers config = let init_dal dal_config = let open Lwt_result_syntax in if dal_config.activated then - let open Tezos_crypto_dal.Dal_cryptobox in + let open Tezos_crypto_dal.Cryptobox in let* initialisation_parameters = match dal_config.srs_size with | None -> diff --git a/src/lib_base/dal_srs.mli b/src/lib_base/dal_srs.mli index 4942d10e074cce7d138ded181ab00121896255bf..ef9f37c14e44dda50cd102292f7e5513c3b249c6 100644 --- a/src/lib_base/dal_srs.mli +++ b/src/lib_base/dal_srs.mli @@ -25,7 +25,7 @@ (** [find_trusted_setup_files] returns the path of the two files necessary to initialize cryptographic primitives used by the - DAL. See {!module:Tezos-crypto_dal.Dal_cryptobox}. *) + DAL. See {!module:Tezos-crypto_dal.Cryptobox}. *) val find_trusted_setup_files : ?getenv_opt:(string -> string option) -> ?getcwd:(unit -> string) -> diff --git a/src/lib_crypto_dal/dal_cryptobox.ml b/src/lib_crypto_dal/cryptobox.ml similarity index 99% rename from src/lib_crypto_dal/dal_cryptobox.ml rename to src/lib_crypto_dal/cryptobox.ml index 02986daaa48afc2b7a00cdf0b091ab689eb5212b..352393adbc274d8f5231cde722777d3103ffbb75 100644 --- a/src/lib_crypto_dal/dal_cryptobox.ml +++ b/src/lib_crypto_dal/cryptobox.ml @@ -24,7 +24,7 @@ (*****************************************************************************) open Error_monad -include Dal_cryptobox_intf +include Cryptobox_intf module Base58 = Tezos_crypto.Base58 module Srs_g1 = Bls12_381_polynomial.Polynomial.Srs_g1 module Srs_g2 = Bls12_381_polynomial.Polynomial.Srs_g2 @@ -325,9 +325,16 @@ module Inner = struct let slot_as_polynomial_length ~slot_size = 1 lsl Z.(log2up (of_int slot_size / of_int scalar_bytes_amount)) + type parameters = { + redundancy_factor : int; + segment_size : int; + slot_size : int; + number_of_shards : int; + } + (* Error cases of this functions are not encapsulated into `tzresult` for modularity reasons. *) - let make ~redundancy_factor ~slot_size ~segment_size ~number_of_shards = + let make {redundancy_factor; slot_size; segment_size; number_of_shards} = let open Result_syntax in let k = slot_as_polynomial_length ~slot_size in let n = redundancy_factor * k in @@ -383,7 +390,7 @@ module Inner = struct (* We encode by segments of [segment_size] bytes each. The segments are arranged in cosets to evaluate in batch with Kate amortized. *) - let polynomial_from_bytes' t slot = + let polynomial_from_bytes' (t : t) slot = if Bytes.length slot <> t.slot_size then Error (`Slot_wrong_size diff --git a/src/lib_crypto_dal/dal_cryptobox.mli b/src/lib_crypto_dal/cryptobox.mli similarity index 93% rename from src/lib_crypto_dal/dal_cryptobox.mli rename to src/lib_crypto_dal/cryptobox.mli index 48ce065de76a0869435f59a065393bfbbfc70fc3..90fdcb513fa5123f26e6a3bb5761853e82e0ca7d 100644 --- a/src/lib_crypto_dal/dal_cryptobox.mli +++ b/src/lib_crypto_dal/cryptobox.mli @@ -23,10 +23,20 @@ (* *) (*****************************************************************************) -open Dal_cryptobox_intf - -(** Encapsulates parameters required to use the cryptographic primitives exported - by this module. *) +open Cryptobox_intf + +(** Initial values to parametrize dal cryptographic primitives. It used to build + a value of type [t] *) +type parameters = { + redundancy_factor : int; + segment_size : int; + slot_size : int; + number_of_shards : int; +} + +(** Encapsulates parameters required to use the cryptographic primitives + exported by this module. A value of type [t] contains both initial + [parameters] and computed values depending on it. *) type t (** Because of the shell/protocol separation, cryptographic primitives @@ -49,9 +59,9 @@ type t that both the [Verifier] and the [Builder] are instantiated with the same parameters and use the same trusted setup. *) -module Verifier : VERIFIER +module Verifier : VERIFIER with type parameters = parameters -include VERIFIER with type t := t +include VERIFIER with type t := t and type parameters := parameters (** The primitives exposed in this modules require some preprocessing. This preprocessing generates data from an unknown @@ -74,7 +84,7 @@ val initialisation_parameters_from_files : val load_parameters : initialisation_parameters -> unit Error_monad.tzresult module Commitment : sig - include Dal_cryptobox_intf.COMMITMENT with type t = commitment + include COMMITMENT with type t = commitment val rpc_arg : commitment Resto.Arg.t end diff --git a/src/lib_crypto_dal/dal_cryptobox_intf.ml b/src/lib_crypto_dal/cryptobox_intf.ml similarity index 94% rename from src/lib_crypto_dal/dal_cryptobox_intf.ml rename to src/lib_crypto_dal/cryptobox_intf.ml index 6937559ec4f1df1f4765a27a853a4a0cbd34b7c7..6504a0cfc0d279e870f45183df1c90df3bad1b7b 100644 --- a/src/lib_crypto_dal/dal_cryptobox_intf.ml +++ b/src/lib_crypto_dal/cryptobox_intf.ml @@ -48,14 +48,17 @@ module type VERIFIER = sig (** A precomputed set of constants *) type t + (** Parameters to build a value of type [t] *) + type parameters = { + redundancy_factor : int; + segment_size : int; + slot_size : int; + number_of_shards : int; + } + (** [make] precomputes the set of values needed by the cryptographic primitives defined in this module and stores them in a value of type [t] *) - val make : - redundancy_factor:int -> - slot_size:int -> - segment_size:int -> - number_of_shards:int -> - (t, [> `Fail of string]) result + val make : parameters -> (t, [> `Fail of string]) result (** Commitment to a polynomial. *) type commitment diff --git a/src/lib_crypto_dal/test/test_dal_cryptobox.ml b/src/lib_crypto_dal/test/test_dal_cryptobox.ml index 8e0286b7c4c5d30e1f9d166655fec40a764fea3b..eb77c32ffe51c25182d753787cda306fb84739af 100644 --- a/src/lib_crypto_dal/test/test_dal_cryptobox.ml +++ b/src/lib_crypto_dal/test/test_dal_cryptobox.ml @@ -35,29 +35,26 @@ module Test = struct Bytes.set_int64_le msg (i * 8) (Random.int64 Int64.max_int) done ; let parameters = - Dal_cryptobox.Internal_for_tests.initialisation_parameters_from_slot_size + Cryptobox.Internal_for_tests.initialisation_parameters_from_slot_size ~slot_size in - let () = Dal_cryptobox.Internal_for_tests.load_parameters parameters in + let () = Cryptobox.Internal_for_tests.load_parameters parameters in Tezos_lwt_result_stdlib.Lwtreslib.Bare.List.iter_e (fun redundancy_factor -> let* t = - Dal_cryptobox.make - ~redundancy_factor - ~slot_size - ~segment_size - ~number_of_shards + Cryptobox.make + {redundancy_factor; slot_size; segment_size; number_of_shards} in - let* p = Dal_cryptobox.polynomial_from_slot t msg in - let cm = Dal_cryptobox.commit t p in - let* pi = Dal_cryptobox.prove_segment t p 1 in + let* p = Cryptobox.polynomial_from_slot t msg in + let cm = Cryptobox.commit t p in + let* pi = Cryptobox.prove_segment t p 1 in let segment = Bytes.sub msg segment_size segment_size in let* check = - Dal_cryptobox.verify_segment t cm {index = 1; content = segment} pi + Cryptobox.verify_segment t cm {index = 1; content = segment} pi in assert check ; - let enc_shards = Dal_cryptobox.shards_from_polynomial t p in + let enc_shards = Cryptobox.shards_from_polynomial t p in let c_indices = random_indices (number_of_shards - 1) @@ -65,45 +62,43 @@ module Test = struct |> Array.of_list in let c = - Dal_cryptobox.IntMap.filter - (fun i _ -> Array.mem i c_indices) - enc_shards + Cryptobox.IntMap.filter (fun i _ -> Array.mem i c_indices) enc_shards in - let* dec = Dal_cryptobox.polynomial_from_shards t c in + let* dec = Cryptobox.polynomial_from_shards t c in assert ( Bytes.compare msg (Bytes.sub - (Dal_cryptobox.polynomial_to_bytes t dec) + (Cryptobox.polynomial_to_bytes t dec) 0 (min slot_size msg_size)) = 0) ; - let comm = Dal_cryptobox.commit t p in - let shard_proofs = Dal_cryptobox.prove_shards t p in - match Dal_cryptobox.IntMap.find 0 enc_shards with + let comm = Cryptobox.commit t p in + let shard_proofs = Cryptobox.prove_shards t p in + match Cryptobox.IntMap.find 0 enc_shards with | None -> Ok () | Some eval -> let check = - Dal_cryptobox.verify_shard + Cryptobox.verify_shard t comm {index = 0; share = eval} shard_proofs.(0) in assert check ; - let pi = Dal_cryptobox.prove_commitment t p in - let check = Dal_cryptobox.verify_commitment t comm pi in + let pi = Cryptobox.prove_commitment t p in + let check = Cryptobox.verify_commitment t comm pi in assert check ; Ok () (* let point = Scalar.random () in *) - (* let+ pi_slot = Dal_cryptobox.prove_single trusted_setup p point in + (* let+ pi_slot = Cryptobox.prove_single trusted_setup p point in * * assert ( - * Dal_cryptobox.verify_single + * Cryptobox.verify_single * trusted_setup * comm * ~point - * ~evaluation:(Dal_cryptobox.polynomial_evaluate p point) + * ~evaluation:(Cryptobox.polynomial_evaluate p point) * pi_slot) *)) [2] |> fun x -> match x with Ok () -> () | Error _ -> assert false diff --git a/src/lib_dal_node/dal_constants_plugin.ml b/src/lib_dal_node/dal_constants_plugin.ml index 1e5553f51eed4e3b7cf7038f0d907214fa85d5a0..c607bc7ecbefe2b1d15ee0e508d60c751a7e1a73 100644 --- a/src/lib_dal_node/dal_constants_plugin.ml +++ b/src/lib_dal_node/dal_constants_plugin.ml @@ -26,22 +26,11 @@ module type T = sig module Proto : Registered_protocol.T - (* FIXME: https://gitlab.com/tezos/tezos/-/issues/3497 - - Move those constants into lib_crypto_dal and environment ? *) - - type constants = { - redundancy_factor : int; - segment_size : int; - slot_size : int; - number_of_shards : int; - } - val get_constants : Tezos_shell_services.Chain_services.chain -> Tezos_shell_services.Block_services.block -> Client_context.full -> - constants tzresult Lwt.t + Tezos_crypto_dal.Cryptobox.Verifier.parameters tzresult Lwt.t end let table : (module T) Protocol_hash.Table.t = Protocol_hash.Table.create 5 diff --git a/src/lib_dal_node/dal_constants_plugin.mli b/src/lib_dal_node/dal_constants_plugin.mli index 681828468a5d7361b314163596a7e06e3820f4b2..38c2fb4ce10d38bac7c9d0d77cc20bfd33e004dc 100644 --- a/src/lib_dal_node/dal_constants_plugin.mli +++ b/src/lib_dal_node/dal_constants_plugin.mli @@ -26,18 +26,11 @@ module type T = sig module Proto : Registered_protocol.T - type constants = { - redundancy_factor : int; - segment_size : int; - slot_size : int; - number_of_shards : int; - } - val get_constants : Tezos_shell_services.Chain_services.chain -> Tezos_shell_services.Block_services.block -> Client_context.full -> - constants tzresult Lwt.t + Tezos_crypto_dal.Cryptobox.Verifier.parameters tzresult Lwt.t end val register : (module T) -> unit diff --git a/src/lib_dal_node_services/services.ml b/src/lib_dal_node_services/services.ml index 09c5ced7ef34bb2e5335701f22bbde1d2c181670..2ac54048f2d84c0fb3504bb51fea96a44a6db0a1 100644 --- a/src/lib_dal_node_services/services.ml +++ b/src/lib_dal_node_services/services.ml @@ -50,14 +50,12 @@ let slot () = ~description:"Show content of a slot" ~query:slot_query ~output:Data_encoding.string - RPC_path.( - open_root / "slot" / "content" /: Dal_cryptobox.Commitment.rpc_arg) + RPC_path.(open_root / "slot" / "content" /: Cryptobox.Commitment.rpc_arg) let shard () = let shard_arg = RPC_arg.int in RPC_service.get_service ~description:"Fetch shard as bytes" ~query:RPC_query.empty - ~output:Dal_cryptobox.shard_encoding - RPC_path.( - open_root / "shard" /: Dal_cryptobox.Commitment.rpc_arg /: shard_arg) + ~output:Cryptobox.shard_encoding + RPC_path.(open_root / "shard" /: Cryptobox.Commitment.rpc_arg /: shard_arg) diff --git a/src/lib_protocol_environment/environment_V7.ml b/src/lib_protocol_environment/environment_V7.ml index beb388e79fe0d0f97d4116aacf86ea5757a436bd..f3285c370d788444940487a8b4961b9b77f32649 100644 --- a/src/lib_protocol_environment/environment_V7.ml +++ b/src/lib_protocol_environment/environment_V7.ml @@ -104,6 +104,7 @@ module type T = sig and type Plonk.public_parameters = Plonk.Main_protocol.verifier_public_parameters * Plonk.Main_protocol.transcript + and type Dal.parameters = Tezos_crypto_dal.Cryptobox.Verifier.parameters type error += Ecoproto_error of Error_monad.error @@ -1473,5 +1474,5 @@ struct module Equality_witness = Environment_context.Equality_witness module Plonk = Tezos_protocol_environment_structs.V7.Plonk - module Dal = Tezos_crypto_dal.Dal_cryptobox.Verifier + module Dal = Tezos_crypto_dal.Cryptobox.Verifier end diff --git a/src/lib_protocol_environment/environment_V7.mli b/src/lib_protocol_environment/environment_V7.mli index 0cd0c7cdeaf8f1b7febd31847cc90907cdf98fcf..c4d676636cbfbcec66637aff8ff4253cf042f073 100644 --- a/src/lib_protocol_environment/environment_V7.mli +++ b/src/lib_protocol_environment/environment_V7.mli @@ -104,6 +104,7 @@ module type T = sig and type Plonk.public_parameters = Plonk.Main_protocol.verifier_public_parameters * Plonk.Main_protocol.transcript + and type Dal.parameters = Tezos_crypto_dal.Cryptobox.Verifier.parameters (** An [Ecoproto_error e] is a shell error that carry a protocol error. diff --git a/src/lib_protocol_environment/sigs/v7.ml b/src/lib_protocol_environment/sigs/v7.ml index 2204b3e433aca770c200de47a5926a95d63f6165..4d6725b31caae89c5512c76c56047fb1d7b01799 100644 --- a/src/lib_protocol_environment/sigs/v7.ml +++ b/src/lib_protocol_environment/sigs/v7.ml @@ -11549,14 +11549,17 @@ end (** A precomputed set of constants *) type t +(** Parameters to build a value of type [t] *) +type parameters = { + redundancy_factor : int; + segment_size : int; + slot_size : int; + number_of_shards : int; +} + (** [make] precomputes the set of values needed by cryptographic primitives defined in this module and store them in a value of type [t] *) -val make : - redundancy_factor:int -> - slot_size:int -> - segment_size:int -> - number_of_shards:int -> - (t, [> `Fail of string]) result +val make : parameters -> (t, [> `Fail of string]) result (** Commitment to a polynomial. *) type commitment diff --git a/src/lib_protocol_environment/sigs/v7/dal.mli b/src/lib_protocol_environment/sigs/v7/dal.mli index 23d561e164eaacd922a03eb9ea9905327ffa17b0..3435c92f28f749e43a735eea7650eb1407e06d7d 100644 --- a/src/lib_protocol_environment/sigs/v7/dal.mli +++ b/src/lib_protocol_environment/sigs/v7/dal.mli @@ -26,14 +26,17 @@ (** A precomputed set of constants *) type t +(** Parameters to build a value of type [t] *) +type parameters = { + redundancy_factor : int; + segment_size : int; + slot_size : int; + number_of_shards : int; +} + (** [make] precomputes the set of values needed by cryptographic primitives defined in this module and store them in a value of type [t] *) -val make : - redundancy_factor:int -> - slot_size:int -> - segment_size:int -> - number_of_shards:int -> - (t, [> `Fail of string]) result +val make : parameters -> (t, [> `Fail of string]) result (** Commitment to a polynomial. *) type commitment diff --git a/src/proto_alpha/lib_dal/dal_plugin_registration.ml b/src/proto_alpha/lib_dal/dal_plugin_registration.ml index 60fbca2b1cc8a310cd3d093ad913284220a40564..17490442a541dd13d86b4fbffe36b93749ff6281 100644 --- a/src/proto_alpha/lib_dal/dal_plugin_registration.ml +++ b/src/proto_alpha/lib_dal/dal_plugin_registration.ml @@ -26,13 +26,6 @@ module Plugin = struct module Proto = Registerer.Registered - type constants = { - redundancy_factor : int; - segment_size : int; - slot_size : int; - number_of_shards : int; - } - let get_constants chain block ctxt = let cpctxt = new Protocol_client_context.wrap_full ctxt in let open Lwt_result_syntax in @@ -41,7 +34,9 @@ module Plugin = struct {redundancy_factor; segment_size; slot_size; number_of_shards; _} = constants.parametric.dal in - return {redundancy_factor; segment_size; slot_size; number_of_shards} + return + Environment.Dal. + {redundancy_factor; segment_size; slot_size; number_of_shards} end let () = Dal_constants_plugin.register (module Plugin) diff --git a/tezt/lib_tezos/dal_node.ml b/tezt/lib_tezos/dal_node.ml index e035651aa8bf8d1303c0e293e53fedf84a8014e9..373c8680bec35ff2026f59853f1467f3e8025a6c 100644 --- a/tezt/lib_tezos/dal_node.ml +++ b/tezt/lib_tezos/dal_node.ml @@ -73,7 +73,7 @@ let data_dir dal_node = dal_node.persistent_state.data_dir let spawn_command dal_node = Process.spawn ~name:dal_node.name ~color:dal_node.color dal_node.path -let spawn_config_init ?(unsafe_srs = true) dal_node = +let spawn_config_init ?(use_unsafe_srs = true) dal_node = spawn_command dal_node @@ List.filter_map Fun.id @@ -85,11 +85,11 @@ let spawn_config_init ?(unsafe_srs = true) dal_node = Some (string_of_int (rpc_port dal_node)); Some "--rpc-addr"; Some (rpc_host dal_node); - (if unsafe_srs then Some "--unsafe-srs-for-tests" else None); + (if use_unsafe_srs then Some "--use-unsafe-srs-for-tests" else None); ] -let init_config ?unsafe_srs dal_node = - let process = spawn_config_init ?unsafe_srs dal_node in +let init_config ?use_unsafe_srs dal_node = + let process = spawn_config_init ?use_unsafe_srs dal_node in let* output = Process.check_and_read_stdout process in match output =~* rex "DAL node configuration written in ([^\n]*)" with | None -> failwith "DAL node configuration initialization failed" diff --git a/tezt/lib_tezos/dal_node.mli b/tezt/lib_tezos/dal_node.mli index 55eaf6526dd20b69457f03968b013d78e1c1179e..aecde5dda198a10807922889fd1c6dec06f8c56c 100644 --- a/tezt/lib_tezos/dal_node.mli +++ b/tezt/lib_tezos/dal_node.mli @@ -85,11 +85,11 @@ val wait : t -> Unix.process_status Lwt.t (** Run [tezos-dal-node init-config]. Returns the name of the resulting configuration file. - If [unsafe_srs] is [true], the dal node runs with unsafe computed SRS + If [use_unsafe_srs] is [true], the dal node runs with unsafe computed SRS allowing tests to run faster, without the need of large file. Default is [true] in tezt. *) -val init_config : ?unsafe_srs:bool -> t -> string Lwt.t +val init_config : ?use_unsafe_srs:bool -> t -> string Lwt.t (** [split_slot_rpc dal_node data] spawns curl process on slot/split on [data] *) val split_slot_rpc : t -> string -> string Lwt.t diff --git a/tezt/lib_tezos/operation_core.ml b/tezt/lib_tezos/operation_core.ml index 13a17cfa2f3a810bf98e7253f232ec0c46f4c004..1973da05194da93e2bffc71fd099545067fdb19d 100644 --- a/tezt/lib_tezos/operation_core.ml +++ b/tezt/lib_tezos/operation_core.ml @@ -267,7 +267,7 @@ module Manager = struct let json_of_commitment commitment = Data_encoding.Json.construct - Tezos_crypto_dal.Dal_cryptobox.Commitment.encoding + Tezos_crypto_dal.Cryptobox.Commitment.encoding commitment let get_next_counter ?(source = Constant.bootstrap1) client = @@ -334,7 +334,7 @@ module Manager = struct | Dal_publish_slot_header of { level : int; index : int; - header : Tezos_crypto_dal.Dal_cryptobox.commitment; + header : Tezos_crypto_dal.Cryptobox.commitment; } | Sc_rollup_dal_slot_subscribe of {rollup : string; slot_index : int} | Delegation of {delegate : Account.key} diff --git a/tezt/lib_tezos/operation_core.mli b/tezt/lib_tezos/operation_core.mli index 3db2246e0e846a43b8aca0d27177bb931998bcb7..2e10e8fb9eabbca08dc451f576a031aa95c4a0d6 100644 --- a/tezt/lib_tezos/operation_core.mli +++ b/tezt/lib_tezos/operation_core.mli @@ -270,7 +270,7 @@ module Manager : sig val dal_publish_slot_header : level:int -> index:int -> - header:Tezos_crypto_dal.Dal_cryptobox.commitment -> + header:Tezos_crypto_dal.Cryptobox.commitment -> payload (** [sc_rollup_dal_slot_subscribe ~rollup ~slot_index] builds an diff --git a/tezt/lib_tezos/rollup.ml b/tezt/lib_tezos/rollup.ml index 580a877fbccc457e1aab8e146120de8375b3f6ac..75805b056e0495e986079d84ae9afa60f85c1c42 100644 --- a/tezt/lib_tezos/rollup.ml +++ b/tezt/lib_tezos/rollup.ml @@ -529,7 +529,7 @@ module Dal = struct return {number_of_shards; redundancy_factor; slot_size; segment_size} end - module Cryptobox = Tezos_crypto_dal.Dal_cryptobox + module Cryptobox = Tezos_crypto_dal.Cryptobox let make ?(on_error = @@ -543,10 +543,7 @@ module Dal = struct Cryptobox.Internal_for_tests.load_parameters initialisation_parameters ; match Cryptobox.make - ~redundancy_factor - ~slot_size - ~segment_size - ~number_of_shards + {redundancy_factor; slot_size; segment_size; number_of_shards} with | Ok cryptobox -> cryptobox | Error (`Fail msg) -> on_error msg diff --git a/tezt/lib_tezos/rollup.mli b/tezt/lib_tezos/rollup.mli index 9e67f42924dd719365af7e4988edc97270b154ac..7ab9a02d9c4fade8ea012a19b2358436d9802bad 100644 --- a/tezt/lib_tezos/rollup.mli +++ b/tezt/lib_tezos/rollup.mli @@ -228,7 +228,7 @@ module Dal : sig val from_client : Client.t -> t Lwt.t end - module Cryptobox = Tezos_crypto_dal.Dal_cryptobox + module Cryptobox = Tezos_crypto_dal.Cryptobox val make : ?on_error:(string -> Cryptobox.t) -> Parameters.t -> Cryptobox.t