From bc6ec23c9fcf18a85df16ac750ea3837df6be326 Mon Sep 17 00:00:00 2001 From: Ryan Tan Date: Mon, 6 Feb 2023 16:22:28 +0000 Subject: [PATCH] Dac: Integrate reveal mapper into node context Dac: prevent reveal_hash from leaking of out the protocol --- .gitlab/ci/jobs/packaging/opam_package.yml | 2 - dune-project | 1 - manifest/main.ml | 18 +-- opam/tezos-dac-node-lib-test.opam | 26 ----- src/bin_dac_node/daemon.ml | 2 +- src/bin_dac_node/data_streamer.ml | 5 +- src/bin_dac_node/data_streamer.mli | 5 +- src/bin_dac_node/node_context.ml | 14 ++- src/bin_dac_node/node_context.mli | 11 +- src/lib_dac_node/dac_hash.ml | 48 -------- src/lib_dac_node/dac_hash.mli | 48 -------- src/lib_dac_node/dac_plugin.ml | 15 ++- src/lib_dac_node/dac_plugin.mli | 24 +++- src/lib_dac_node/test/dune | 27 ----- src/lib_dac_node/test/main.ml | 51 --------- .../lib_dac/dac_plugin_registration.ml | 35 +++++- src/proto_alpha/lib_dac/test/dune | 4 +- src/proto_alpha/lib_dac/test/main.ml | 7 +- .../test/test_dac_plugin_registration.ml} | 104 ++++++------------ 19 files changed, 129 insertions(+), 318 deletions(-) delete mode 100644 opam/tezos-dac-node-lib-test.opam delete mode 100644 src/lib_dac_node/dac_hash.ml delete mode 100644 src/lib_dac_node/dac_hash.mli delete mode 100644 src/lib_dac_node/test/dune delete mode 100644 src/lib_dac_node/test/main.ml rename src/{lib_dac_node/test/test_dac_hash.ml => proto_alpha/lib_dac/test/test_dac_plugin_registration.ml} (55%) diff --git a/.gitlab/ci/jobs/packaging/opam_package.yml b/.gitlab/ci/jobs/packaging/opam_package.yml index 7c4053d2bc47..f18421168fb0 100644 --- a/.gitlab/ci/jobs/packaging/opam_package.yml +++ b/.gitlab/ci/jobs/packaging/opam_package.yml @@ -611,8 +611,6 @@ opam:tezos-crypto-dal: # Ignoring unreleased package tezos-dac-node-lib. -# Ignoring unreleased package tezos-dac-node-lib-test. - # Ignoring unreleased package tezos-dal-016-PtMumbai. # Ignoring unreleased package tezos-dal-alpha. diff --git a/dune-project b/dune-project index 4565a0c8033c..86074daf942d 100644 --- a/dune-project +++ b/dune-project @@ -79,7 +79,6 @@ (package (name tezos-crypto-dal)) (package (name tezos-dac-alpha)) (package (name tezos-dac-node-lib)(allow_empty)) -(package (name tezos-dac-node-lib-test)(allow_empty)) (package (name tezos-dal-016-PtMumbai)) (package (name tezos-dal-alpha)) (package (name tezos-dal-node-lib)(allow_empty)) diff --git a/manifest/main.ml b/manifest/main.ml index 2fd22280e93c..ba3ce039ae85 100644 --- a/manifest/main.ml +++ b/manifest/main.ml @@ -3293,23 +3293,6 @@ let octez_dac_node_lib = octez_stdlib_unix |> open_; ] -let _octez_dac_node_lib_tests = - test - "main" - ~path:"src/lib_dac_node/test" - ~opam:"tezos-dac-node-lib-test" - ~synopsis:"Test for dac node lib" - ~deps: - [ - octez_stdlib |> open_; - octez_stdlib_unix |> open_; - octez_base |> open_ |> open_ ~m:"TzPervasives"; - octez_test_helpers |> open_; - octez_base_test_helpers |> open_; - octez_dac_node_lib |> open_; - alcotest_lwt; - ] - let octez_node_config = public_lib "octez-node-config" @@ -5202,6 +5185,7 @@ module Protocol = Protocol main |> open_; octez_base_test_helpers |> open_; test_helpers |> if_some |> open_; + octez_dac_node_lib |> open_; alcotest_lwt; ] in diff --git a/opam/tezos-dac-node-lib-test.opam b/opam/tezos-dac-node-lib-test.opam deleted file mode 100644 index f21b671a59dd..000000000000 --- a/opam/tezos-dac-node-lib-test.opam +++ /dev/null @@ -1,26 +0,0 @@ -# This file was automatically generated, do not edit. -# Edit file manifest/main.ml instead. -opam-version: "2.0" -maintainer: "contact@tezos.com" -authors: ["Tezos devteam"] -homepage: "https://www.tezos.com/" -bug-reports: "https://gitlab.com/tezos/tezos/issues" -dev-repo: "git+https://gitlab.com/tezos/tezos.git" -license: "MIT" -depends: [ - "dune" { >= "3.0" } - "ocaml" { >= "4.14" } - "tezos-stdlib" {with-test} - "tezos-stdlib-unix" {with-test} - "tezos-base" {with-test} - "tezos-test-helpers" {with-test} - "tezos-base-test-helpers" {with-test} - "tezos-dac-node-lib" {with-test} - "alcotest-lwt" { with-test & >= "1.5.0" } -] -build: [ - ["rm" "-r" "vendors"] - ["dune" "build" "-p" name "-j" jobs] - ["dune" "runtest" "-p" name "-j" jobs] {with-test} -] -synopsis: "Test for dac node lib" diff --git a/src/bin_dac_node/daemon.ml b/src/bin_dac_node/daemon.ml index 913dba738474..bff5471abf00 100644 --- a/src/bin_dac_node/daemon.ml +++ b/src/bin_dac_node/daemon.ml @@ -78,7 +78,7 @@ module Handler = struct let*! dac_plugin = Dac_manager.resolve_plugin protocols in match dac_plugin with | Some dac_plugin -> - Node_context.set_ready ctxt ~dac_plugin ; + Node_context.set_ready ctxt dac_plugin ; let*! () = Event.(emit node_is_ready ()) in stopper () ; return_unit diff --git a/src/bin_dac_node/data_streamer.ml b/src/bin_dac_node/data_streamer.ml index 85c07abcb560..642726795818 100644 --- a/src/bin_dac_node/data_streamer.ml +++ b/src/bin_dac_node/data_streamer.ml @@ -33,10 +33,11 @@ module Root_hash_streamer = struct let init (_configuration : configuration) = () - let publish (_streamer : t) (_hash : Dac_hash.t) = + let publish (_streamer : t) (_hash : Dac_plugin.Dac_hash.t) = Lwt_result_syntax.return_unit let make_subscription (_streamer : t) : - (Dac_hash.t Lwt_stream.t * Lwt_watcher.stopper) tzresult Lwt.t = + (Dac_plugin.Dac_hash.t Lwt_stream.t * Lwt_watcher.stopper) tzresult Lwt.t + = Lwt_result_syntax.return @@ Lwt_watcher.create_fake_stream () end diff --git a/src/bin_dac_node/data_streamer.mli b/src/bin_dac_node/data_streamer.mli index acd87a9764c3..76f920a159a3 100644 --- a/src/bin_dac_node/data_streamer.mli +++ b/src/bin_dac_node/data_streamer.mli @@ -39,12 +39,13 @@ module Root_hash_streamer : sig (** [publish streamer root_hash] publishes a [root_hash] to all attached subscribers in [streamer]. *) - val publish : t -> Dac_hash.t -> unit tzresult Lwt.t + val publish : t -> Dac_plugin.Dac_hash.t -> unit tzresult Lwt.t (** [make_subscription streamer] returns a new stream of hashes for the subscriber to consume. An [Lwt_watcher.stopper] function is also returned for the subscriber to close the stream. *) val make_subscription : - t -> (Dac_hash.t Lwt_stream.t * Lwt_watcher.stopper) tzresult Lwt.t + t -> + (Dac_plugin.Dac_hash.t Lwt_stream.t * Lwt_watcher.stopper) tzresult Lwt.t end diff --git a/src/bin_dac_node/node_context.ml b/src/bin_dac_node/node_context.ml index 0cf1a0ae6783..f306f4814d41 100644 --- a/src/bin_dac_node/node_context.ml +++ b/src/bin_dac_node/node_context.ml @@ -25,7 +25,9 @@ exception Status_already_ready -type ready_ctxt = {dac_plugin : (module Dac_plugin.T)} +type dac_plugin_module = (module Dac_plugin.T) + +type ready_ctxt = {dac_plugin : dac_plugin_module} type status = Ready of ready_ctxt | Starting @@ -37,11 +39,9 @@ type t = { let init config cctxt = {status = Starting; config; tezos_node_cctxt = cctxt} -let set_ready ctxt ~dac_plugin = +let set_ready ctxt dac_plugin = match ctxt.status with | Starting -> - let (module Dac_plugin : Dac_plugin.T) = dac_plugin in - (* FIXME: https://gitlab.com/tezos/tezos/-/issues/4681 Currently, Dac only supports coordinator functionalities but we might want to filter this capability out depending on the profile. @@ -76,3 +76,9 @@ let get_config ctxt = ctxt.config let get_status ctxt = ctxt.status let get_tezos_node_cctxt ctxt = ctxt.tezos_node_cctxt + +let get_dac_hash_encoding ctxt = + let open Result_syntax in + match ctxt.status with + | Ready {dac_plugin = (module Dac_plugin)} -> Ok Dac_plugin.Dac_hash.encoding + | Starting -> tzfail Node_not_ready diff --git a/src/bin_dac_node/node_context.mli b/src/bin_dac_node/node_context.mli index 8cefbcc2379b..278048961a9e 100644 --- a/src/bin_dac_node/node_context.mli +++ b/src/bin_dac_node/node_context.mli @@ -22,10 +22,11 @@ (* DEALINGS IN THE SOFTWARE. *) (* *) (*****************************************************************************) +type dac_plugin_module = (module Dac_plugin.T) (** A [ready_ctx] value contains globally needed informations for a running dac node. It is available when the DAC plugin has been loaded. *) -type ready_ctxt = {dac_plugin : (module Dac_plugin.T)} +type ready_ctxt = {dac_plugin : dac_plugin_module} (** The status of the dac node. *) type status = Ready of ready_ctxt | Starting @@ -42,12 +43,12 @@ val init : Configuration.t -> Client_context.full -> t (** Raised by [set_ready] when the status is already [Ready _] *) exception Status_already_ready -(** [set_ready ctxt ~dac_plugin] updates +(** [set_ready ctxt dac_plugin_module] 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 -> dac_plugin:(module Tezos_dac_node_lib.Dac_plugin.T) -> unit +val set_ready : t -> dac_plugin_module -> unit type error += Node_not_ready @@ -64,3 +65,7 @@ val get_status : t -> status (** [get_tezos_node_cctxt ctxt] returns the Tezos node's client context. *) val get_tezos_node_cctxt : t -> Client_context.full + +(** [get_dac_hash_encoding ctxt] returns the [Dac_hash.t] encoding derived from + [Dac_plugin.Protocol_reveal_hash.encoding] *) +val get_dac_hash_encoding : t -> Dac_plugin.Dac_hash.t Data_encoding.t tzresult diff --git a/src/lib_dac_node/dac_hash.ml b/src/lib_dac_node/dac_hash.ml deleted file mode 100644 index 0f8373ae96e1..000000000000 --- a/src/lib_dac_node/dac_hash.ml +++ /dev/null @@ -1,48 +0,0 @@ -(*****************************************************************************) -(* *) -(* Open Source License *) -(* Copyright (c) 2023 Trili Tech, *) -(* *) -(* 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. *) -(* *) -(*****************************************************************************) -type t = bytes - -module type Reveal_hash_mapper = sig - type reveal_hash - - val of_reveal_hash : reveal_hash -> t - - val to_reveal_hash : t -> reveal_hash - - val encoding : t Data_encoding.t -end - -module Make (H : Dac_plugin.Protocol_reveal_hash) = struct - (* NB: Safe since failure case only occurs if [buffer_size] is given. *) - let of_reveal_hash proto_reveal_hash = - Data_encoding.Binary.to_bytes_exn H.encoding proto_reveal_hash - - (** NB: Safe since the [Dac_hash.t] could only be constructed from a - [H.t]. *) - let to_reveal_hash dac_hash = - Data_encoding.Binary.of_bytes_exn H.encoding dac_hash - - let encoding = Data_encoding.conv to_reveal_hash of_reveal_hash H.encoding -end diff --git a/src/lib_dac_node/dac_hash.mli b/src/lib_dac_node/dac_hash.mli deleted file mode 100644 index a1f89ad5f236..000000000000 --- a/src/lib_dac_node/dac_hash.mli +++ /dev/null @@ -1,48 +0,0 @@ -(*****************************************************************************) -(* *) -(* Open Source License *) -(* Copyright (c) 2023 Trili Tech, *) -(* *) -(* 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. *) -(* *) -(*****************************************************************************) - -(** This module exposes a hash representation that abstracts over the protocol - reveal hash type. -*) -type t - -(** Operations of [Dac_hash.t] that relate to [Protocol.Sc_rollup_reveal_hash.t]. *) -module type Reveal_hash_mapper = sig - (** Protocol reveal hash type typically coming from - [Protocol.Sc_rollup_reveal_hash.t]. - *) - type reveal_hash - - (** Derives a [Dac_hash.t] from [reveal_hash]. *) - val of_reveal_hash : reveal_hash -> t - - (** Derives a [reveal_hash] from [Dac_hash.t]. *) - val to_reveal_hash : t -> reveal_hash - - val encoding : t Data_encoding.t -end - -module Make (P : Dac_plugin.Protocol_reveal_hash) : - Reveal_hash_mapper with type reveal_hash := P.t diff --git a/src/lib_dac_node/dac_plugin.ml b/src/lib_dac_node/dac_plugin.ml index 6085856466fd..262909b47118 100644 --- a/src/lib_dac_node/dac_plugin.ml +++ b/src/lib_dac_node/dac_plugin.ml @@ -23,15 +23,16 @@ (* *) (*****************************************************************************) -(* Protocol's reveal_hash type. Usually [Protocol.Sc_rollup_reveal_hash.t]. *) -module type Protocol_reveal_hash = sig - type t +module Dac_hash = struct + type t = bytes - val encoding : t Data_encoding.t + let to_bytes = Fun.id end module type T = sig - module Protocol_reveal_hash : Protocol_reveal_hash + module Dac_hash : sig + val encoding : Dac_hash.t Data_encoding.t + end module Proto : Registered_protocol.T @@ -48,7 +49,9 @@ end let table : (module T) Protocol_hash.Table.t = Protocol_hash.Table.create 5 -let register (module Plugin : T) = +let register (make_plugin : (bytes -> Dac_hash.t) -> (module T)) : unit = + let dac_plugin = make_plugin Fun.id in + let module Plugin = (val dac_plugin) in assert (not (Protocol_hash.Table.mem table Plugin.Proto.hash)) ; Protocol_hash.Table.add table Plugin.Proto.hash (module Plugin) diff --git a/src/lib_dac_node/dac_plugin.mli b/src/lib_dac_node/dac_plugin.mli index 4eb36d14b3c7..794aabba2f42 100644 --- a/src/lib_dac_node/dac_plugin.mli +++ b/src/lib_dac_node/dac_plugin.mli @@ -23,15 +23,23 @@ (* *) (*****************************************************************************) -(* Protocol's reveal_hash type. Usually [Protocol.Sc_rollup_reveal_hash.t]. *) -module type Protocol_reveal_hash = sig +(** [Protocol.Sc_protocol_reveal_hash.t] is unknown to modules outside the + protocol and only known at runtime. To avoid the proliferation of functors + in the dac node, [Dac_hash.t] hides the dynamic [Protocol.Sc_protocol_reveal_hash.t] + behind an abstract static type. An instance of [Dac_plugin.T] implement + the [Dac_plugin.Dac_hash] which are the functions that can operate on [Dac_hash.t]. + *) +module Dac_hash : sig type t - val encoding : t Data_encoding.t + val to_bytes : t -> bytes end module type T = sig - module Protocol_reveal_hash : Protocol_reveal_hash + (** [Dac_hash.t] operations that need to be derived from the protocol *) + module Dac_hash : sig + val encoding : Dac_hash.t Data_encoding.t + end module Proto : Registered_protocol.T @@ -46,6 +54,12 @@ module type T = sig end end -val register : (module T) -> unit +(** [register make_plugin] derives and registers a new [Dac_plugin.T] given an + [of_bytes]. Implementers of plugin are responsible for providing the + definition of this derivation. Functions that expose + [Protocol.Sc_protocol_reveal_hash.t] can be wrapped into [Dac_hash.t] via + [Dac_hash.to_bytes] and [of_bytes]. +*) +val register : ((bytes -> Dac_hash.t) -> (module T)) -> unit val get : Protocol_hash.Table.key -> (module T) option diff --git a/src/lib_dac_node/test/dune b/src/lib_dac_node/test/dune deleted file mode 100644 index 1205238ef9a2..000000000000 --- a/src/lib_dac_node/test/dune +++ /dev/null @@ -1,27 +0,0 @@ -; This file was automatically generated, do not edit. -; Edit file manifest/main.ml instead. - -(executable - (name main) - (libraries - tezos-stdlib - tezos-stdlib-unix - tezos-base - tezos-test-helpers - tezos-base-test-helpers - tezos_dac_node_lib - alcotest-lwt) - (flags - (:standard) - -open Tezos_stdlib - -open Tezos_stdlib_unix - -open Tezos_base - -open Tezos_base.TzPervasives - -open Tezos_test_helpers - -open Tezos_base_test_helpers - -open Tezos_dac_node_lib)) - -(rule - (alias runtest) - (package tezos-dac-node-lib-test) - (action (run %{dep:./main.exe}))) diff --git a/src/lib_dac_node/test/main.ml b/src/lib_dac_node/test/main.ml deleted file mode 100644 index e30255bc9649..000000000000 --- a/src/lib_dac_node/test/main.ml +++ /dev/null @@ -1,51 +0,0 @@ -(*****************************************************************************) -(* *) -(* Open Source License *) -(* Copyright (c) 2022 Trili Tech, *) -(* *) -(* 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. *) -(* *) -(*****************************************************************************) - -module Unit_test : sig - (** - * Example: [spec "Dac_hash.ml" Test_dac_hash.tests] - * Unit tests needs tag in log (like "[UNIT] some test description here...") - * This function handles such meta data *) - val spec : - string -> - unit Alcotest_lwt.test_case list -> - string * unit Alcotest_lwt.test_case list - - (** Tests with description string without [Unit] are skipped *) - val _skip : - string -> - unit Alcotest_lwt.test_case list -> - string * unit Alcotest_lwt.test_case list -end = struct - let spec unit_name test_cases = ("[Unit] " ^ unit_name, test_cases) - - let _skip unit_name test_cases = ("[SKIPPED] " ^ unit_name, test_cases) -end - -let () = - Alcotest_lwt.run - "protocol > unit" - [Unit_test.spec "Dac_hash.ml" Test_dac_hash.tests] - |> Lwt_main.run diff --git a/src/proto_alpha/lib_dac/dac_plugin_registration.ml b/src/proto_alpha/lib_dac/dac_plugin_registration.ml index 5f4763996b8a..afe80bd58bc8 100644 --- a/src/proto_alpha/lib_dac/dac_plugin_registration.ml +++ b/src/proto_alpha/lib_dac/dac_plugin_registration.ml @@ -24,10 +24,39 @@ (* *) (*****************************************************************************) -module Plugin = struct - module Protocol_reveal_hash = Protocol.Sc_rollup_reveal_hash +module Make (Mapper : sig + val of_bytes : bytes -> Dac_plugin.Dac_hash.t +end) : Dac_plugin.T = struct + module Dac_hash = struct + let to_bytes = Dac_plugin.Dac_hash.to_bytes + + let to_reveal_hash dac_hash = + dac_hash |> to_bytes + |> Data_encoding.Binary.of_bytes_exn + Protocol.Sc_rollup_reveal_hash.encoding + + let of_reveal_hash reveal_hash = + reveal_hash + |> Data_encoding.Binary.to_bytes_exn + Protocol.Sc_rollup_reveal_hash.encoding + |> Mapper.of_bytes + + let encoding = + Data_encoding.conv + to_reveal_hash + of_reveal_hash + Protocol.Sc_rollup_reveal_hash.encoding + end + module Proto = Registerer.Registered module RPC = RPC end -let () = Dac_plugin.register (module Plugin) +let make_plugin : (bytes -> Dac_plugin.Dac_hash.t) -> (module Dac_plugin.T) = + fun of_bytes -> + let module Plugin = Make (struct + let of_bytes = of_bytes + end) in + (module Plugin) + +let () = Dac_plugin.register make_plugin diff --git a/src/proto_alpha/lib_dac/test/dune b/src/proto_alpha/lib_dac/test/dune index 0b4377db3166..29ca0f9c90db 100644 --- a/src/proto_alpha/lib_dac/test/dune +++ b/src/proto_alpha/lib_dac/test/dune @@ -9,6 +9,7 @@ tezos-protocol-alpha tezos-base-test-helpers tezos-alpha-test-helpers + tezos_dac_node_lib alcotest-lwt) (flags (:standard) @@ -17,7 +18,8 @@ -open Tezos_dac_alpha -open Tezos_protocol_alpha -open Tezos_base_test_helpers - -open Tezos_alpha_test_helpers)) + -open Tezos_alpha_test_helpers + -open Tezos_dac_node_lib)) (rule (alias runtest) diff --git a/src/proto_alpha/lib_dac/test/main.ml b/src/proto_alpha/lib_dac/test/main.ml index 11bbabc8c5bb..b326558332d5 100644 --- a/src/proto_alpha/lib_dac/test/main.ml +++ b/src/proto_alpha/lib_dac/test/main.ml @@ -47,5 +47,10 @@ end let () = Alcotest_lwt.run "protocol > unit" - [Unit_test.spec "Dac_pages_encoding.ml" Test_dac_pages_encoding.tests] + [ + Unit_test.spec "Dac_pages_encoding.ml" Test_dac_pages_encoding.tests; + Unit_test.spec + "Dac_plugin_registration.ml" + Test_dac_plugin_registration.tests; + ] |> Lwt_main.run diff --git a/src/lib_dac_node/test/test_dac_hash.ml b/src/proto_alpha/lib_dac/test/test_dac_plugin_registration.ml similarity index 55% rename from src/lib_dac_node/test/test_dac_hash.ml rename to src/proto_alpha/lib_dac/test/test_dac_plugin_registration.ml index 14dc2e8fa025..76a57e3c7765 100644 --- a/src/lib_dac_node/test/test_dac_hash.ml +++ b/src/proto_alpha/lib_dac/test/test_dac_plugin_registration.ml @@ -26,93 +26,61 @@ (** Testing ------- Component: Lib_dac_node Dac_hash - Invocation: dune exec src/lib_dac_node/test/main.exe \ - -- test "^\[Unit\] Dac_hash.ml$" + Invocation: dune exec src/proto_alpha/lib_dac/test/main.exe \ + -- test "^\[Unit\] Dac_plugin_registration.ml$" Subject: Tests for the interoperability between Dac hash and given protocol hash *) -open Tezos_crypto - -module Mock_Reveal_Hash = struct - module Blake2B = struct - include - Blake2B.Make - (Base58) - (struct - let name = "Smart_rollup_reveal_data_blake2b_hash" - - let title = "A smart rollup reveal hash" - - let b58check_prefix = - "\230\206\128\200\196" (* "scrrh1(56)" decoded from Base58. *) - - let size = Some 32 - end) - - let () = Base58.check_encoded_prefix b58check_encoding "scrrh1" 56 - end - - type t = Blake2B of Blake2B.t - - let encoding = - let open Data_encoding in - union - ~tag_size:`Uint8 - [ - case - ~title:"Reveal_data_hash_v0" - (Tag 0) - Blake2B.encoding - (fun (Blake2B s) -> Some s) - (fun s -> Blake2B s); - ] - - let to_hex hash = - let (`Hex hash) = - Hex.of_string @@ Data_encoding.Binary.to_string_exn encoding hash - in - hash - - let of_hex hex = - let open Option_syntax in - let* hash = Hex.to_bytes (`Hex hex) in - Data_encoding.Binary.of_bytes_opt encoding hash - - let equal (Blake2B a) (Blake2B b) = Blake2B.equal a b -end +module Protocol_reveal_hash = Protocol.Sc_rollup_reveal_hash (* Hash copied from https://gitlab.com/tezos/tezos/-/blob/master/tezt/tests/dac.ml#L331 *) let reveal_hash = Stdlib.Option.get - @@ Mock_Reveal_Hash.of_hex + @@ Protocol_reveal_hash.of_hex "00a3703854279d2f377d689163d1ec911a840d84b56c4c6f6cafdf0610394df7c6" -module Reveal_hash_mapper = Dac_hash.Make (Mock_Reveal_Hash) +let assert_equal_bytes ~loc msg = + Assert.equal ~loc Bytes.equal msg String.pp_bytes_hex + +let make_plugin_and_save_hash (proto : (module Dac_plugin.T) option ref) + make_plugin : (bytes -> Dac_plugin.Dac_hash.t) -> (module Dac_plugin.T) = + fun f -> + let dac_plugin = make_plugin f in + proto := Option.some dac_plugin ; + dac_plugin let test_dac_hash_bin_encoding_roundtrips_with_reveal_hash () = + let open Lwt_result_syntax in + let dac_plugin = Stdlib.Option.get (Dac_plugin.get Protocol.hash) in + let module Plugin = (val dac_plugin) in let to_bytes e a = Stdlib.Result.get_ok @@ Data_encoding.Binary.to_bytes e a in let from_bytes e a = Stdlib.Result.get_ok @@ Data_encoding.Binary.of_bytes e a in - let reveal_hash_bytes = to_bytes Mock_Reveal_Hash.encoding reveal_hash in - let dac_hash = from_bytes Reveal_hash_mapper.encoding reveal_hash_bytes in - let dac_hash_bytes = to_bytes Reveal_hash_mapper.encoding dac_hash in + let reveal_hash_bytes = to_bytes Protocol_reveal_hash.encoding reveal_hash in + let dac_hash = from_bytes Plugin.Dac_hash.encoding reveal_hash_bytes in + let dac_hash_bytes = to_bytes Plugin.Dac_hash.encoding dac_hash in let reveal_hash_decoded = - from_bytes Mock_Reveal_Hash.encoding dac_hash_bytes + from_bytes Protocol_reveal_hash.encoding dac_hash_bytes in - Assert.equal ~loc:__LOC__ reveal_hash_bytes dac_hash_bytes ; - Assert.equal ~loc:__LOC__ reveal_hash reveal_hash_decoded ; - Lwt.return @@ Ok () - -let test_dac_conversion_roundtrips_with_reveal_hash () = - let dac_hash = Reveal_hash_mapper.of_reveal_hash reveal_hash in - let decoded_reveal_hash = Reveal_hash_mapper.to_reveal_hash dac_hash in - Assert.equal ~loc:__LOC__ reveal_hash decoded_reveal_hash ; - Lwt.return @@ Ok () + let* () = + assert_equal_bytes + ~loc:__LOC__ + "Encoded bytes are not equal" + reveal_hash_bytes + dac_hash_bytes + in + Assert.equal + ~loc:__LOC__ + Protocol_reveal_hash.equal + "Roundtrip hash is not equal" + Protocol_reveal_hash.pp + reveal_hash + reveal_hash_decoded let tests = [ @@ -120,8 +88,4 @@ let tests = "Binary encoding roundtrip test between Dac hash and reveal hash" `Quick test_dac_hash_bin_encoding_roundtrips_with_reveal_hash; - Tztest.tztest - "Conversion roundrip test between Dac hash and reveal hash" - `Quick - test_dac_conversion_roundtrips_with_reveal_hash; ] -- GitLab