From 24373d028ca9f86488f77e3e71dd2d49c5230854 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Thir=C3=A9?= Date: Tue, 29 Nov 2022 14:17:24 +0100 Subject: [PATCH 1/2] Dal: Simplify the interface --- src/bin_dal_node/RPC_server_legacy.ml | 9 ++++----- src/bin_dal_node/daemon.ml | 17 ++++++++--------- src/bin_dal_node/node_context.ml | 10 +++------- src/bin_dal_node/node_context.mli | 14 +++----------- src/bin_dal_node/slot_manager_legacy.ml | 11 ++++++----- src/bin_dal_node/slot_manager_legacy.mli | 12 ++---------- 6 files changed, 26 insertions(+), 47 deletions(-) diff --git a/src/bin_dal_node/RPC_server_legacy.ml b/src/bin_dal_node/RPC_server_legacy.ml index c8b1b7a71e2d..cfb93b91a20e 100644 --- a/src/bin_dal_node/RPC_server_legacy.ml +++ b/src/bin_dal_node/RPC_server_legacy.ml @@ -41,9 +41,8 @@ let handle_split_slot ctxt () slot = let handle_slot ctxt (_, commitment) () () = let open Lwt_result_syntax in - let*? {dal_parameters; dal_constants; _} = Node_context.get_ready ctxt in + let*? {dal_constants; _} = Node_context.get_ready ctxt in Slot_manager.get_slot - dal_parameters dal_constants (Node_context.get_store ctxt).slots_store commitment @@ -59,9 +58,8 @@ let handle_stored_slot_headers ctxt (_, block_hash) () () = let handle_slot_pages ctxt (_, commitment) () () = let open Lwt_result_syntax in - let*? {dal_parameters; dal_constants; _} = Node_context.get_ready ctxt in + let*? {dal_constants; _} = Node_context.get_ready ctxt in Slot_manager.get_slot_pages - dal_parameters dal_constants (Node_context.get_store ctxt).slots_store commitment @@ -74,7 +72,8 @@ let handle_shard ctxt ((_, commitment), shard) () () = let handle_shards ctxt (_, commitment) () shards = let open Lwt_result_syntax in - let*? {dal_parameters; _} = Node_context.get_ready ctxt in + let*? {dal_constants; _} = Node_context.get_ready ctxt in + let dal_parameters = Cryptobox.parameters dal_constants in let shards = let open Slot_manager in List.fold_left diff --git a/src/bin_dal_node/daemon.ml b/src/bin_dal_node/daemon.ml index 667ca9cae8b7..e37fa2a879da 100644 --- a/src/bin_dal_node/daemon.ml +++ b/src/bin_dal_node/daemon.ml @@ -68,7 +68,7 @@ let init_cryptobox unsafe_srs cctxt (module Plugin : Dal_plugin.T) = in let*? () = Cryptobox.load_parameters initialisation_parameters in match Cryptobox.make parameters with - | Ok cryptobox -> return (cryptobox, parameters) + | Ok cryptobox -> return cryptobox | Error (`Fail msg) -> fail [Cryptobox_initialisation_failed msg] module Handler = struct @@ -111,14 +111,10 @@ module Handler = struct | Some plugin -> let (module Plugin : Dal_plugin.T) = plugin in let*! () = Event.emit_protocol_plugin_resolved Plugin.Proto.hash in - let* dal_constants, dal_parameters = + let* dal_constants = init_cryptobox config.Configuration.use_unsafe_srs cctxt plugin in - Node_context.set_ready - ctxt - (module Plugin) - dal_constants - dal_parameters ; + Node_context.set_ready ctxt (module Plugin) dal_constants ; let*! () = Event.(emit node_is_ready ()) in stopper () ; return_unit @@ -164,9 +160,12 @@ module Handler = struct (* Monitor neighbor DAL nodes and download published slots as shards. *) let open Lwt_result_syntax in let handler n_cctxt ready_ctxt slot_header = - let params = ready_ctxt.Node_context.dal_parameters in + let dal_constants = ready_ctxt.Node_context.dal_constants in + let dal_parameters = Cryptobox.parameters dal_constants in let downloaded_shard_ids = - 0 -- ((params.number_of_shards / params.redundancy_factor) - 1) + 0 + -- ((dal_parameters.number_of_shards / dal_parameters.redundancy_factor) + - 1) in let* shards = RPC_server_legacy.shards_rpc n_cctxt slot_header downloaded_shard_ids diff --git a/src/bin_dal_node/node_context.ml b/src/bin_dal_node/node_context.ml index 8823cd8fb812..9983240aef57 100644 --- a/src/bin_dal_node/node_context.ml +++ b/src/bin_dal_node/node_context.ml @@ -25,11 +25,7 @@ exception Status_already_ready -type ready_ctxt = { - dal_constants : Cryptobox.t; - dal_parameters : Cryptobox.parameters; - plugin : (module Dal_plugin.T); -} +type ready_ctxt = {dal_constants : Cryptobox.t; plugin : (module Dal_plugin.T)} type status = Ready of ready_ctxt | Starting @@ -49,9 +45,9 @@ let init config store = in {status = Starting; config; store; neighbors_cctxts} -let set_ready ctxt plugin dal_constants dal_parameters = +let set_ready ctxt plugin dal_constants = match ctxt.status with - | Starting -> ctxt.status <- Ready {plugin; dal_constants; dal_parameters} + | Starting -> ctxt.status <- Ready {plugin; dal_constants} | Ready _ -> raise Status_already_ready type error += Node_not_ready diff --git a/src/bin_dal_node/node_context.mli b/src/bin_dal_node/node_context.mli index cb628eaafb23..fe4a1004ee86 100644 --- a/src/bin_dal_node/node_context.mli +++ b/src/bin_dal_node/node_context.mli @@ -26,11 +26,7 @@ (** A [ready_ctx] value contains globally needed informations for a running dal node. It is available when both cryptobox is initialized and dal plugin is loaded. *) -type ready_ctxt = { - dal_constants : Cryptobox.t; - dal_parameters : Cryptobox.parameters; - plugin : (module Dal_plugin.T); -} +type ready_ctxt = {dal_constants : Cryptobox.t; plugin : (module Dal_plugin.T)} (** The status of the dal node *) type status = Ready of ready_ctxt | Starting @@ -46,17 +42,13 @@ val init : Configuration.t -> Store.node_store -> t (** Raised by [set_ready] when the status is already [Ready _] *) exception Status_already_ready -(** [set_ready ctxt plugin dal_constants dal_params] updates in place the status +(** [set_ready ctxt plugin dal_constants] updates in place the status value to [Ready], and initializes the inner [ready_ctxt] value with the given parameters. @raise Status_already_ready when the status is already [Ready _] *) val set_ready : - t -> - (module Tezos_dal_node_lib.Dal_plugin.T) -> - Cryptobox.t -> - Cryptobox.parameters -> - unit + t -> (module Tezos_dal_node_lib.Dal_plugin.T) -> Cryptobox.t -> unit type error += Node_not_ready diff --git a/src/bin_dal_node/slot_manager_legacy.ml b/src/bin_dal_node/slot_manager_legacy.ml index f66afaaa06b4..8874b7f84d3b 100644 --- a/src/bin_dal_node/slot_manager_legacy.ml +++ b/src/bin_dal_node/slot_manager_legacy.ml @@ -253,8 +253,9 @@ let get_shards store dal_parameters slot_header shard_ids = [] slot_header -let get_slot dal_parameters dal_constants store slot_header = +let get_slot dal_constants store slot_header = let open Lwt_result_syntax in + let dal_parameters = Cryptobox.parameters dal_constants in let* shards = fold_stored_shards ~check_shards:true @@ -272,10 +273,10 @@ let get_slot dal_parameters dal_constants store slot_header = in return slot -let get_slot_pages ({Cryptobox.page_size; _} as initial_constants) dal_constants - store slot_header = +let get_slot_pages dal_constants store slot_header = let open Lwt_result_syntax in - let* slot = get_slot initial_constants dal_constants store slot_header in + let dal_parameters = Cryptobox.parameters dal_constants in + let* slot = get_slot dal_constants store slot_header in (* The slot size `Bytes.length slot` should be an exact multiple of `page_size`. If this is not the case, we throw an `Illformed_pages` error. *) @@ -283,7 +284,7 @@ let get_slot_pages ({Cryptobox.page_size; _} as initial_constants) dal_constants Implement `Bytes.chunk_bytes` which returns a list of bytes directly. *) let*? pages = String.chunk_bytes - page_size + dal_parameters.page_size slot ~error_on_partial_chunk:(TzTrace.make Illformed_pages) in diff --git a/src/bin_dal_node/slot_manager_legacy.mli b/src/bin_dal_node/slot_manager_legacy.mli index 00277ec5e096..f255013b1b32 100644 --- a/src/bin_dal_node/slot_manager_legacy.mli +++ b/src/bin_dal_node/slot_manager_legacy.mli @@ -69,11 +69,7 @@ val get_shards : disk the shards associated to [slot_header], gathers them, rebuilds and returns the [slot]. *) val get_slot : - Cryptobox.parameters -> - Cryptobox.t -> - Store.t -> - Cryptobox.commitment -> - slot tzresult Lwt.t + Cryptobox.t -> Store.t -> Cryptobox.commitment -> slot tzresult Lwt.t (** [get_slot_pages] behaves as [get_slot], except that it also splits the slot into pages before returning them. @@ -83,11 +79,7 @@ val get_slot : length is not a multiple of the page-size specified in the [Cryptobox.parameters] argument. *) val get_slot_pages : - Cryptobox.parameters -> - Cryptobox.t -> - Store.t -> - Cryptobox.commitment -> - bytes list tzresult Lwt.t + Cryptobox.t -> Store.t -> Cryptobox.commitment -> bytes list tzresult Lwt.t (** [save_shards store slot_header shards] stores [shards] onto the [store] associated to the given [slot_header] *) -- GitLab From d1b7683ec7d3087bca2c99b492993f8b28199f41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Thir=C3=A9?= Date: Tue, 29 Nov 2022 14:36:19 +0100 Subject: [PATCH 2/2] Dal/Node: Rename `dal_constants` into `cryptobox` --- src/bin_dal_node/RPC_server.ml | 2 +- src/bin_dal_node/RPC_server_legacy.ml | 16 ++++++------ src/bin_dal_node/daemon.ml | 10 ++++---- src/bin_dal_node/node_context.ml | 6 ++--- src/bin_dal_node/node_context.mli | 2 +- src/bin_dal_node/slot_manager_legacy.ml | 34 ++++++++++++------------- 6 files changed, 35 insertions(+), 35 deletions(-) diff --git a/src/bin_dal_node/RPC_server.ml b/src/bin_dal_node/RPC_server.ml index ea46f686faac..7010aca12d8b 100644 --- a/src/bin_dal_node/RPC_server.ml +++ b/src/bin_dal_node/RPC_server.ml @@ -30,7 +30,7 @@ open Tezos_rpc_http_server module Slots_handlers = struct let call_handler handler ctxt = let open Lwt_result_syntax in - let*? {dal_constants = cryptobox; _} = Node_context.get_ready ctxt in + let*? {cryptobox; _} = Node_context.get_ready ctxt in let store = Node_context.get_store ctxt in handler store cryptobox diff --git a/src/bin_dal_node/RPC_server_legacy.ml b/src/bin_dal_node/RPC_server_legacy.ml index cfb93b91a20e..6992170d8c6c 100644 --- a/src/bin_dal_node/RPC_server_legacy.ml +++ b/src/bin_dal_node/RPC_server_legacy.ml @@ -28,12 +28,12 @@ open Tezos_dal_node_services let handle_split_slot ctxt () slot = let open Lwt_result_syntax in - let*? {dal_constants; _} = Node_context.get_ready ctxt in + let*? {cryptobox; _} = Node_context.get_ready ctxt in let store = Node_context.get_store ctxt in let+ commitment, proof = Slot_manager.split_and_store store.slots_watcher - dal_constants + cryptobox store.slots_store slot in @@ -41,9 +41,9 @@ let handle_split_slot ctxt () slot = let handle_slot ctxt (_, commitment) () () = let open Lwt_result_syntax in - let*? {dal_constants; _} = Node_context.get_ready ctxt in + let*? {cryptobox; _} = Node_context.get_ready ctxt in Slot_manager.get_slot - dal_constants + cryptobox (Node_context.get_store ctxt).slots_store commitment @@ -58,9 +58,9 @@ let handle_stored_slot_headers ctxt (_, block_hash) () () = let handle_slot_pages ctxt (_, commitment) () () = let open Lwt_result_syntax in - let*? {dal_constants; _} = Node_context.get_ready ctxt in + let*? {cryptobox; _} = Node_context.get_ready ctxt in Slot_manager.get_slot_pages - dal_constants + cryptobox (Node_context.get_store ctxt).slots_store commitment @@ -72,8 +72,8 @@ let handle_shard ctxt ((_, commitment), shard) () () = let handle_shards ctxt (_, commitment) () shards = let open Lwt_result_syntax in - let*? {dal_constants; _} = Node_context.get_ready ctxt in - let dal_parameters = Cryptobox.parameters dal_constants in + let*? {cryptobox; _} = Node_context.get_ready ctxt in + let dal_parameters = Cryptobox.parameters cryptobox in let shards = let open Slot_manager in List.fold_left diff --git a/src/bin_dal_node/daemon.ml b/src/bin_dal_node/daemon.ml index e37fa2a879da..42c97b576815 100644 --- a/src/bin_dal_node/daemon.ml +++ b/src/bin_dal_node/daemon.ml @@ -111,10 +111,10 @@ module Handler = struct | Some plugin -> let (module Plugin : Dal_plugin.T) = plugin in let*! () = Event.emit_protocol_plugin_resolved Plugin.Proto.hash in - let* dal_constants = + let* cryptobox = init_cryptobox config.Configuration.use_unsafe_srs cctxt plugin in - Node_context.set_ready ctxt (module Plugin) dal_constants ; + Node_context.set_ready ctxt (module Plugin) cryptobox ; let*! () = Event.(emit node_is_ready ()) in stopper () ; return_unit @@ -160,8 +160,8 @@ module Handler = struct (* Monitor neighbor DAL nodes and download published slots as shards. *) let open Lwt_result_syntax in let handler n_cctxt ready_ctxt slot_header = - let dal_constants = ready_ctxt.Node_context.dal_constants in - let dal_parameters = Cryptobox.parameters dal_constants in + let cryptobox = ready_ctxt.Node_context.cryptobox in + let dal_parameters = Cryptobox.parameters cryptobox in let downloaded_shard_ids = 0 -- ((dal_parameters.number_of_shards / dal_parameters.redundancy_factor) @@ -181,7 +181,7 @@ module Handler = struct Slot_manager.save_shards (Node_context.get_store ctxt).slots_store (Node_context.get_store ctxt).slots_watcher - ready_ctxt.dal_constants + ready_ctxt.Node_context.cryptobox slot_header shards in diff --git a/src/bin_dal_node/node_context.ml b/src/bin_dal_node/node_context.ml index 9983240aef57..d9de1fe12869 100644 --- a/src/bin_dal_node/node_context.ml +++ b/src/bin_dal_node/node_context.ml @@ -25,7 +25,7 @@ exception Status_already_ready -type ready_ctxt = {dal_constants : Cryptobox.t; plugin : (module Dal_plugin.T)} +type ready_ctxt = {cryptobox : Cryptobox.t; plugin : (module Dal_plugin.T)} type status = Ready of ready_ctxt | Starting @@ -45,9 +45,9 @@ let init config store = in {status = Starting; config; store; neighbors_cctxts} -let set_ready ctxt plugin dal_constants = +let set_ready ctxt plugin cryptobox = match ctxt.status with - | Starting -> ctxt.status <- Ready {plugin; dal_constants} + | Starting -> ctxt.status <- Ready {plugin; cryptobox} | Ready _ -> raise Status_already_ready type error += Node_not_ready diff --git a/src/bin_dal_node/node_context.mli b/src/bin_dal_node/node_context.mli index fe4a1004ee86..3a88e2b0da7a 100644 --- a/src/bin_dal_node/node_context.mli +++ b/src/bin_dal_node/node_context.mli @@ -26,7 +26,7 @@ (** A [ready_ctx] value contains globally needed informations for a running dal node. It is available when both cryptobox is initialized and dal plugin is loaded. *) -type ready_ctxt = {dal_constants : Cryptobox.t; plugin : (module Dal_plugin.T)} +type ready_ctxt = {cryptobox : Cryptobox.t; plugin : (module Dal_plugin.T)} (** The status of the dal node *) type status = Ready of ready_ctxt | Starting diff --git a/src/bin_dal_node/slot_manager_legacy.ml b/src/bin_dal_node/slot_manager_legacy.ml index 8874b7f84d3b..05ce8607234d 100644 --- a/src/bin_dal_node/slot_manager_legacy.ml +++ b/src/bin_dal_node/slot_manager_legacy.ml @@ -153,18 +153,18 @@ let save store watcher slot_header shards = Lwt_watcher.notify watcher slot_header ; return_unit -let split_and_store watcher dal_constants store slot = +let split_and_store watcher cryptobox store slot = let r = let open Result_syntax in - let* polynomial = Cryptobox.polynomial_from_slot dal_constants slot in - let commitment = Cryptobox.commit dal_constants polynomial in - let proof = Cryptobox.prove_commitment dal_constants polynomial in + let* polynomial = Cryptobox.polynomial_from_slot cryptobox slot in + let commitment = Cryptobox.commit cryptobox polynomial in + let proof = Cryptobox.prove_commitment cryptobox polynomial in return (polynomial, commitment, proof) in let open Lwt_result_syntax in match r with | Ok (polynomial, commitment, commitment_proof) -> - let shards = Cryptobox.shards_from_polynomial dal_constants polynomial in + let shards = Cryptobox.shards_from_polynomial cryptobox polynomial in let* () = save store watcher commitment shards in Lwt.return_ok (commitment, commitment_proof) | Error (`Slot_wrong_size msg) -> Lwt.return_error [Splitting_failed msg] @@ -185,16 +185,16 @@ let check_slot_consistency dal_parameters shards = {provided = List.length shards; required = required_shards}; ] -let polynomial_from_shards dal_constants shards = - match Cryptobox.polynomial_from_shards dal_constants shards with +let polynomial_from_shards cryptobox shards = + match Cryptobox.polynomial_from_shards cryptobox shards with | Ok p -> Ok p | Error (`Invert_zero msg | `Not_enough_shards msg) -> Error [Merging_failed msg] -let save_shards store watcher dal_constants slot_header shards = +let save_shards store watcher cryptobox slot_header shards = let open Lwt_result_syntax in - let*? polynomial = polynomial_from_shards dal_constants shards in - let rebuilt_slot_header = Cryptobox.commit dal_constants polynomial in + let*? polynomial = polynomial_from_shards cryptobox shards in + let rebuilt_slot_header = Cryptobox.commit cryptobox polynomial in let*? () = if Cryptobox.Commitment.equal slot_header rebuilt_slot_header then Ok () else Result_syntax.fail [Invalid_shards_slot_header_association] @@ -253,9 +253,9 @@ let get_shards store dal_parameters slot_header shard_ids = [] slot_header -let get_slot dal_constants store slot_header = +let get_slot cryptobox store slot_header = let open Lwt_result_syntax in - let dal_parameters = Cryptobox.parameters dal_constants in + let dal_parameters = Cryptobox.parameters cryptobox in let* shards = fold_stored_shards ~check_shards:true @@ -265,18 +265,18 @@ let get_slot dal_constants store slot_header = Cryptobox.IntMap.empty slot_header in - let*? polynomial = polynomial_from_shards dal_constants shards in - let slot = Cryptobox.polynomial_to_bytes dal_constants polynomial in + let*? polynomial = polynomial_from_shards cryptobox shards in + let slot = Cryptobox.polynomial_to_bytes cryptobox polynomial in let*! () = Event.( emit fetched_slot (Bytes.length slot, Cryptobox.IntMap.cardinal shards)) in return slot -let get_slot_pages dal_constants store slot_header = +let get_slot_pages cryptobox store slot_header = let open Lwt_result_syntax in - let dal_parameters = Cryptobox.parameters dal_constants in - let* slot = get_slot dal_constants store slot_header in + let dal_parameters = Cryptobox.parameters cryptobox in + let* slot = get_slot cryptobox store slot_header in (* The slot size `Bytes.length slot` should be an exact multiple of `page_size`. If this is not the case, we throw an `Illformed_pages` error. *) -- GitLab