From 858fa7c013754178067444b88b46fbfe0dc6c518 Mon Sep 17 00:00:00 2001 From: Thomas Letan Date: Mon, 1 Aug 2022 13:17:06 +0200 Subject: [PATCH] Tree_encoding: Reorganize the dependencie tree of 'lib_scoru_wasm' MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Before to this patch, the dependency tree of lib_scoru_wasm is straightforward. lib_webassembly <- lib_scoru_wasm (provide lazy containers) (provide tree encoding) The caveat is that the encoding of the types introduced in lib_webassembly are defined in lib_scoru_wasm… and therefore cannot be easily tested in the official WASM test-suite (in a decode, eval, encode loop). This patch is the first step towards being able to test the tree encoders developed for the PVM in the WASM test-suite. We introduce two libraries, - 'lazy-containers' introduces laz maps, lazy vectors, and chunked byte vectors - 'tree-encoding' introduces the tree encoders (including for the lazy containers) The rest of the code base is unchanged, in particular the tree encoding are still defined in 'lib_scoru_wasm'. --- .gitlab/ci/opam-ci.yml | 18 ++- dune-project | 3 + manifest/main.ml | 121 +++++++++++++----- opam/lazy-containers.opam | 23 ++++ opam/tezos-scoru-wasm-test.opam | 1 + opam/tezos-scoru-wasm.opam | 2 + opam/tezos-webassembly-interpreter.opam | 3 +- opam/tree-encoding-test.opam | 26 ++++ opam/tree-encoding.opam | 22 ++++ .../chunked_byte_vector.ml | 47 ++++++- .../chunked_byte_vector.mli | 25 ++++ src/lib_lazy_containers/dune | 10 ++ src/lib_lazy_containers/exn.ml | 31 +++++ .../util => lib_lazy_containers}/lazy_map.ml | 25 ++++ .../util => lib_lazy_containers}/lazy_map.mli | 29 ++++- .../lazy_vector.ml | 31 ++++- .../lazy_vector.mli | 29 ++++- .../test}/chunked_byte_vector_tests.ml | 25 ++++ src/lib_lazy_containers/test/dune | 20 +++ .../test}/lazy_vector_tests.ml | 25 ++++ src/lib_lazy_containers/test/main.ml | 32 +++++ src/lib_scoru_wasm/dune | 2 + src/lib_scoru_wasm/gather_floppies.ml | 36 +++--- src/lib_scoru_wasm/gather_floppies.mli | 5 +- src/lib_scoru_wasm/test/ast_generators.ml | 1 + src/lib_scoru_wasm/test/ast_printer.ml | 1 + src/lib_scoru_wasm/test/dune | 1 + .../test/test_ast_generators.ml | 2 +- src/lib_scoru_wasm/test/test_input.ml | 66 ++++++---- src/lib_scoru_wasm/test/test_scoru_wasm.ml | 1 - src/lib_scoru_wasm/test/test_wasm_encoding.ml | 16 +-- src/lib_scoru_wasm/wasm_encoding.ml | 15 ++- src/lib_scoru_wasm/wasm_encoding.mli | 2 +- src/lib_scoru_wasm/wasm_pvm.ml | 39 +++--- src/lib_scoru_wasm/wasm_pvm.mli | 2 +- .../decoding.ml} | 0 .../decoding.mli} | 0 src/lib_tree_encoding/dune | 15 +++ .../encoding.ml} | 0 .../encoding.mli} | 0 src/lib_tree_encoding/test/dune | 24 ++++ .../test/test_encoding.ml | 85 ++++++------ .../test/test_tree_encoding.ml | 35 +++++ .../tree.ml | 17 ++- .../tree.mli | 17 ++- .../tree_encoding.ml} | 31 ++--- .../tree_encoding.mli} | 54 ++++---- src/lib_webassembly/bin/dune | 8 +- src/lib_webassembly/dune | 6 +- src/lib_webassembly/runtime/memory_exn.ml | 4 +- src/lib_webassembly/tests/dune | 2 - src/lib_webassembly/tests/main.ml | 7 +- 52 files changed, 796 insertions(+), 246 deletions(-) create mode 100644 opam/lazy-containers.opam create mode 100644 opam/tree-encoding-test.opam create mode 100644 opam/tree-encoding.opam rename src/{lib_webassembly/util => lib_lazy_containers}/chunked_byte_vector.ml (75%) rename src/{lib_webassembly/util => lib_lazy_containers}/chunked_byte_vector.mli (57%) create mode 100644 src/lib_lazy_containers/dune create mode 100644 src/lib_lazy_containers/exn.ml rename src/{lib_webassembly/util => lib_lazy_containers}/lazy_map.ml (71%) rename src/{lib_webassembly/util => lib_lazy_containers}/lazy_map.mli (69%) rename src/{lib_webassembly/util => lib_lazy_containers}/lazy_vector.ml (80%) rename src/{lib_webassembly/util => lib_lazy_containers}/lazy_vector.mli (77%) rename src/{lib_webassembly/tests => lib_lazy_containers/test}/chunked_byte_vector_tests.ml (61%) create mode 100644 src/lib_lazy_containers/test/dune rename src/{lib_webassembly/tests => lib_lazy_containers/test}/lazy_vector_tests.ml (63%) create mode 100644 src/lib_lazy_containers/test/main.ml rename src/{lib_scoru_wasm/tree_decoding.ml => lib_tree_encoding/decoding.ml} (100%) rename src/{lib_scoru_wasm/tree_decoding.mli => lib_tree_encoding/decoding.mli} (100%) create mode 100644 src/lib_tree_encoding/dune rename src/{lib_scoru_wasm/tree_encoding.ml => lib_tree_encoding/encoding.ml} (100%) rename src/{lib_scoru_wasm/tree_encoding.mli => lib_tree_encoding/encoding.mli} (100%) create mode 100644 src/lib_tree_encoding/test/dune rename src/{lib_scoru_wasm => lib_tree_encoding}/test/test_encoding.ml (87%) create mode 100644 src/lib_tree_encoding/test/test_tree_encoding.ml rename src/{lib_scoru_wasm => lib_tree_encoding}/tree.ml (89%) rename src/{lib_scoru_wasm => lib_tree_encoding}/tree.mli (89%) rename src/{lib_scoru_wasm/tree_encoding_decoding.ml => lib_tree_encoding/tree_encoding.ml} (94%) rename src/{lib_scoru_wasm/tree_encoding_decoding.mli => lib_tree_encoding/tree_encoding.mli} (88%) diff --git a/.gitlab/ci/opam-ci.yml b/.gitlab/ci/opam-ci.yml index 27805a34a782..f31ac02b6dcb 100644 --- a/.gitlab/ci/opam-ci.yml +++ b/.gitlab/ci/opam-ci.yml @@ -35,6 +35,8 @@ # Ignoring package tezt-self-tests, it only contains tests or private targets +# Ignoring package tree-encoding-test, it only contains tests or private targets + .rules_template__trigger_opam_batch_1: rules: # Run on scheduled builds. @@ -252,6 +254,13 @@ start_in: 7 minutes - when: never # default +opam:lazy-containers: + extends: + - .opam_template + - .rules_template__trigger_opam_batch_7 + variables: + package: lazy-containers + opam:tezos-012-Psithaca-test-helpers: extends: - .opam_template @@ -696,7 +705,7 @@ opam:tezos-crypto: opam:tezos-crypto-dal: extends: - .opam_template - - .rules_template__trigger_opam_batch_6 + - .rules_template__trigger_opam_batch_7 variables: package: tezos-crypto-dal @@ -1630,3 +1639,10 @@ opam:tezt-tezos: - .rules_template__trigger_opam_batch_6 variables: package: tezt-tezos + +opam:tree-encoding: + extends: + - .opam_template + - .rules_template__trigger_opam_batch_6 + variables: + package: tree-encoding diff --git a/dune-project b/dune-project index 48a349b63568..e486fa460f75 100644 --- a/dune-project +++ b/dune-project @@ -2,6 +2,7 @@ (formatting (enabled_for ocaml)) (cram enable) (package (name internal-devtools)) +(package (name lazy-containers)) (package (name tezos-012-Psithaca-test-helpers)) (package (name tezos-013-PtJakart-test-helpers)) (package (name tezos-014-PtKathma-test-helpers)) @@ -215,5 +216,7 @@ (package (name tezt-performance-regression)) (package (name tezt-self-tests)) (package (name tezt-tezos)) +(package (name tree-encoding)) +(package (name tree-encoding-test)(allow_empty)) ; This file was automatically generated, do not edit. ; Edit file manifest/manifest.ml instead. diff --git a/manifest/main.ml b/manifest/main.ml index a38f70621f62..9fe83fb6e9c3 100644 --- a/manifest/main.ml +++ b/manifest/main.ml @@ -579,40 +579,6 @@ let octez_error_monad = ] ~js_compatible:true -let octez_webassembly_interpreter = - public_lib - "tezos-webassembly-interpreter" - ~path:"src/lib_webassembly" - ~license:"Apache License 2.0" - ~extra_authors:["WebAssembly Authors"] - ~synopsis:"WebAssembly reference interpreter with tweaks for Tezos" - ~flags:(Flags.standard ~disable_warnings:[27] ()) - ~dune:Dune.[[S "include_subdirs"; S "unqualified"]] - ~deps:[octez_lwt_result_stdlib; zarith] - -let _octez_webassembly_repl = - private_exe - "main" - ~path:"src/lib_webassembly/bin" - ~opam:"" - ~flags:(Flags.standard ~disable_warnings:[27] ()) - ~dune:Dune.[[S "include"; S "dune.inc"]] - ~deps:[octez_webassembly_interpreter |> open_; lwt_unix] - -let _octez_webassembly_test = - test - "main" - ~path:"src/lib_webassembly/tests" - ~opam:"tezos-webassembly-interpreter" - ~dune:Dune.[[S "include_subdirs"; S "no"]] - ~deps: - [ - octez_webassembly_interpreter |> open_; - qcheck_core; - qcheck_alcotest; - alcotest; - ] - let octez_hacl = let js_stubs = ["random.js"; "evercrypt.js"] in let js_generated = "runtime-generated.js" in @@ -1113,6 +1079,71 @@ let octez_base_test_helpers = ~linkall:true ~bisect_ppx:false +let lazy_containers = + public_lib + "lazy-containers" + ~path:"src/lib_lazy_containers" + ~synopsis: + "A collection of lazy containers whose contents is fetched from \ + arbitrary backend on-demand" + ~deps:[octez_lwt_result_stdlib; zarith] + +let _lazy_containers_tests = + test + "main" + ~path:"src/lib_lazy_containers/test" + ~opam:"lazy-containers" + ~dune:Dune.[[S "include_subdirs"; S "no"]] + ~deps:[lazy_containers |> open_; qcheck_core; qcheck_alcotest; alcotest] + +let tree_encoding = + public_lib + "tree-encoding" + ~path:"src/lib_tree_encoding" + ~synopsis: + "A general-purpose library to encode arbitrary data in Merkle trees" + ~deps: + [ + octez_base |> open_ ~m:"TzPervasives"; + lazy_containers; + octez_lwt_result_stdlib; + data_encoding; + ] + +let octez_webassembly_interpreter = + public_lib + "tezos-webassembly-interpreter" + ~path:"src/lib_webassembly" + ~license:"Apache License 2.0" + ~extra_authors:["WebAssembly Authors"] + ~synopsis:"WebAssembly reference interpreter with tweaks for Tezos" + ~flags:(Flags.standard ~disable_warnings:[27] ()) + ~dune:Dune.[[S "include_subdirs"; S "unqualified"]] + ~deps:[octez_lwt_result_stdlib; zarith; lazy_containers |> open_] + +let _octez_webassembly_repl = + private_exe + "main" + ~path:"src/lib_webassembly/bin" + ~opam:"" + ~flags:(Flags.standard ~disable_warnings:[27] ()) + ~dune:Dune.[[S "include"; S "dune.inc"]] + ~deps: + [ + octez_webassembly_interpreter |> open_; + lwt_unix; + tree_encoding |> open_; + lazy_containers |> open_; + ] + +let _octez_webassembly_test = + test + "main" + ~path:"src/lib_webassembly/tests" + ~opam:"tezos-webassembly-interpreter" + ~dune:Dune.[[S "include_subdirs"; S "no"]] + ~deps:[octez_webassembly_interpreter |> open_; alcotest] + let octez_version_parser = public_lib "tezos-version.parser" @@ -1494,6 +1525,8 @@ let octez_scoru_wasm = ~deps: [ octez_base |> open_ ~m:"TzPervasives"; + tree_encoding; + lazy_containers; octez_webassembly_interpreter; octez_context_sigs; octez_lwt_result_stdlib; @@ -1573,6 +1606,25 @@ let octez_context_disk = octez_context_dump; ] +let _tree_encoding_tests = + test + "test_tree_encoding" + ~path:"src/lib_tree_encoding/test" + ~opam:"tree-encoding-test" + ~synopsis:"Tests for the tree encoding library" + ~deps: + [ + octez_base |> open_ ~m:"TzPervasives"; + tree_encoding; + octez_base_unix; + octez_context_disk; + octez_base_test_helpers |> open_; + octez_test_helpers; + octez_webassembly_interpreter; + qcheck_alcotest; + alcotest_lwt; + ] + let _octez_scoru_wasm_tests = test "test_scoru_wasm" @@ -1582,6 +1634,7 @@ let _octez_scoru_wasm_tests = ~deps: [ octez_base |> open_ ~m:"TzPervasives"; + tree_encoding; octez_base_unix; octez_context_disk; octez_base_test_helpers |> open_; diff --git a/opam/lazy-containers.opam b/opam/lazy-containers.opam new file mode 100644 index 000000000000..d181469e176e --- /dev/null +++ b/opam/lazy-containers.opam @@ -0,0 +1,23 @@ +# 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" } + "tezos-lwt-result-stdlib" + "zarith" { >= "1.12" & < "1.13" } + "qcheck-core" {with-test} + "qcheck-alcotest" { with-test & >= "0.18" } + "alcotest" { with-test & >= "1.5.0" } +] +build: [ + ["rm" "-r" "vendors"] + ["dune" "build" "-p" name "-j" jobs] + ["dune" "runtest" "-p" name "-j" jobs] {with-test} +] +synopsis: "A collection of lazy containers whose contents is fetched from arbitrary backend on-demand" diff --git a/opam/tezos-scoru-wasm-test.opam b/opam/tezos-scoru-wasm-test.opam index a886e2b47843..17a3893ca1cc 100644 --- a/opam/tezos-scoru-wasm-test.opam +++ b/opam/tezos-scoru-wasm-test.opam @@ -10,6 +10,7 @@ license: "MIT" depends: [ "dune" { >= "3.0" } "tezos-base" {with-test} + "tree-encoding" {with-test} "tezos-context" {with-test} "tezos-base-test-helpers" {with-test} "tezos-test-helpers" {with-test} diff --git a/opam/tezos-scoru-wasm.opam b/opam/tezos-scoru-wasm.opam index 230cf3b22ef5..012480eedb03 100644 --- a/opam/tezos-scoru-wasm.opam +++ b/opam/tezos-scoru-wasm.opam @@ -10,6 +10,8 @@ license: "MIT" depends: [ "dune" { >= "3.0" } "tezos-base" + "tree-encoding" + "lazy-containers" "tezos-webassembly-interpreter" "tezos-context" "tezos-lwt-result-stdlib" diff --git a/opam/tezos-webassembly-interpreter.opam b/opam/tezos-webassembly-interpreter.opam index 432c9d7f71cf..6d57a03d9c2f 100644 --- a/opam/tezos-webassembly-interpreter.opam +++ b/opam/tezos-webassembly-interpreter.opam @@ -11,8 +11,7 @@ depends: [ "dune" { >= "3.0" } "tezos-lwt-result-stdlib" "zarith" { >= "1.12" & < "1.13" } - "qcheck-core" {with-test} - "qcheck-alcotest" { with-test & >= "0.18" } + "lazy-containers" "alcotest" { with-test & >= "1.5.0" } ] build: [ diff --git a/opam/tree-encoding-test.opam b/opam/tree-encoding-test.opam new file mode 100644 index 000000000000..a2587afc86f8 --- /dev/null +++ b/opam/tree-encoding-test.opam @@ -0,0 +1,26 @@ +# 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" } + "tezos-base" {with-test} + "tree-encoding" {with-test} + "tezos-context" {with-test} + "tezos-base-test-helpers" {with-test} + "tezos-test-helpers" {with-test} + "tezos-webassembly-interpreter" {with-test} + "qcheck-alcotest" { with-test & >= "0.18" } + "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: "Tests for the tree encoding library" diff --git a/opam/tree-encoding.opam b/opam/tree-encoding.opam new file mode 100644 index 000000000000..59fce304e39a --- /dev/null +++ b/opam/tree-encoding.opam @@ -0,0 +1,22 @@ +# 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" } + "tezos-base" + "lazy-containers" + "tezos-lwt-result-stdlib" + "data-encoding" { >= "0.6" & < "0.7" } +] +build: [ + ["rm" "-r" "vendors"] + ["dune" "build" "-p" name "-j" jobs] + ["dune" "runtest" "-p" name "-j" jobs] {with-test} +] +synopsis: "A general-purpose library to encode arbitrary data in Merkle trees" diff --git a/src/lib_webassembly/util/chunked_byte_vector.ml b/src/lib_lazy_containers/chunked_byte_vector.ml similarity index 75% rename from src/lib_webassembly/util/chunked_byte_vector.ml rename to src/lib_lazy_containers/chunked_byte_vector.ml index 8a6e66153dd4..759712c57340 100644 --- a/src/lib_webassembly/util/chunked_byte_vector.ml +++ b/src/lib_lazy_containers/chunked_byte_vector.ml @@ -1,5 +1,43 @@ +(*****************************************************************************) +(* *) +(* 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. *) +(* *) +(*****************************************************************************) + open Bigarray -open Lib.Bigarray + +module Array1_64 = struct + let create kind layout n = + if n < 0L || n > Int64.of_int max_int then + raise (Invalid_argument "Bigarray.Array1_64.create") ; + Array1.create kind layout (Int64.to_int n) + + let index_of_int64 i = + if i < 0L || i > Int64.of_int max_int then -1 else Int64.to_int i + + let get a i = Array1.get a (index_of_int64 i) + + let set a i x = Array1.set a (index_of_int64 i) x +end module Chunk = struct type t = (int, int8_unsigned_elt, c_layout) Array1.t @@ -127,13 +165,13 @@ module Make (Effect : Effect.S) : S with type 'a effect = 'a Effect.t = struct let load_byte vector address = let open Effect in - if Int64.compare address vector.length >= 0 then raise Memory_exn.Bounds ; + if Int64.compare address vector.length >= 0 then raise Exn.Bounds ; let+ chunk = Vector.get (Chunk.index address) vector.chunks in Array1_64.get chunk (Chunk.offset address) let store_byte vector address byte = let open Effect in - if Int64.compare address vector.length >= 0 then raise Memory_exn.Bounds ; + if Int64.compare address vector.length >= 0 then raise Exn.Bounds ; let+ chunk = Vector.get (Chunk.index address) vector.chunks in Array1_64.set chunk (Chunk.offset address) byte @@ -199,8 +237,7 @@ module Make (Effect : Effect.S) : S with type 'a effect = 'a Effect.t = struct let to_bytes vector = let open Effect in let chunks_number = Vector.num_elements vector.chunks in - if vector.length > Int64.of_int Sys.max_string_length then - raise Memory_exn.Bounds ; + if vector.length > Int64.of_int Sys.max_string_length then raise Exn.Bounds ; (* Once we ensure the vector can be contained in a string, we can safely convert everything to int, since the size of the vector is contained in a `nativeint`. See {!of_string} comment. *) diff --git a/src/lib_webassembly/util/chunked_byte_vector.mli b/src/lib_lazy_containers/chunked_byte_vector.mli similarity index 57% rename from src/lib_webassembly/util/chunked_byte_vector.mli rename to src/lib_lazy_containers/chunked_byte_vector.mli index 9ec87bfcfb8b..077f756e923e 100644 --- a/src/lib_webassembly/util/chunked_byte_vector.mli +++ b/src/lib_lazy_containers/chunked_byte_vector.mli @@ -1,3 +1,28 @@ +(*****************************************************************************) +(* *) +(* 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 Chunk : sig (** Chunk within the byte vector *) type t diff --git a/src/lib_lazy_containers/dune b/src/lib_lazy_containers/dune new file mode 100644 index 000000000000..e5289b4d6b2c --- /dev/null +++ b/src/lib_lazy_containers/dune @@ -0,0 +1,10 @@ +; This file was automatically generated, do not edit. +; Edit file manifest/main.ml instead. + +(library + (name lazy_containers) + (public_name lazy-containers) + (instrumentation (backend bisect_ppx)) + (libraries + tezos-lwt-result-stdlib + zarith)) diff --git a/src/lib_lazy_containers/exn.ml b/src/lib_lazy_containers/exn.ml new file mode 100644 index 000000000000..560f4ea6c3e3 --- /dev/null +++ b/src/lib_lazy_containers/exn.ml @@ -0,0 +1,31 @@ +(*****************************************************************************) +(* *) +(* 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. *) +(* *) +(*****************************************************************************) + +(* This module is included as-is in a module of the WASM interpreter + ('Tezos_webassembly_interpreter.Memory_exn'). *) + +exception Bounds + +exception SizeOverflow diff --git a/src/lib_webassembly/util/lazy_map.ml b/src/lib_lazy_containers/lazy_map.ml similarity index 71% rename from src/lib_webassembly/util/lazy_map.ml rename to src/lib_lazy_containers/lazy_map.ml index c810cdc3b1ea..30c09ac83419 100644 --- a/src/lib_webassembly/util/lazy_map.ml +++ b/src/lib_lazy_containers/lazy_map.ml @@ -1,3 +1,28 @@ +(*****************************************************************************) +(* *) +(* 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 Effect = struct module type S = sig type 'a t diff --git a/src/lib_webassembly/util/lazy_map.mli b/src/lib_lazy_containers/lazy_map.mli similarity index 69% rename from src/lib_webassembly/util/lazy_map.mli rename to src/lib_lazy_containers/lazy_map.mli index 3c858fd1d921..321cf3e42362 100644 --- a/src/lib_webassembly/util/lazy_map.mli +++ b/src/lib_lazy_containers/lazy_map.mli @@ -1,3 +1,28 @@ +(*****************************************************************************) +(* *) +(* 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. *) +(* *) +(*****************************************************************************) + (** A lazy map is a key-value association where each value is created dynamically. *) @@ -58,12 +83,12 @@ module type S = sig (** [get key map] retrieves the element at [key]. - @raises Memory_exn.Bounds when trying to access an invalid key *) + @raises Exn.Bounds when trying to access an invalid key *) val get : key -> 'a t -> 'a effect (** [set key value map] sets the element at [key] to [value]. - @raises Memory_exn.Bounds when trying to set an invalid key *) + @raises Exn.Bounds when trying to set an invalid key *) val set : key -> 'a -> 'a t -> 'a t (** [merge_into ?choose_producer ?map_key source dest] produces a new lazy map diff --git a/src/lib_webassembly/util/lazy_vector.ml b/src/lib_lazy_containers/lazy_vector.ml similarity index 80% rename from src/lib_webassembly/util/lazy_vector.ml rename to src/lib_lazy_containers/lazy_vector.ml index f2e42ab79022..7dd732d175fd 100644 --- a/src/lib_webassembly/util/lazy_vector.ml +++ b/src/lib_lazy_containers/lazy_vector.ml @@ -1,3 +1,28 @@ +(*****************************************************************************) +(* *) +(* 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 Effect = struct module type S = sig include Lazy_map.Effect.S @@ -145,12 +170,12 @@ module Make (Effect : Effect.S) (Key : KeyS) : let invalid_key key map = Key.unsigned_compare key map.num_elements >= 0 let get key map = - if invalid_key key map then raise Memory_exn.Bounds ; + if invalid_key key map then raise Exn.Bounds ; let key = Key.add map.first key in Map.get key map.values let set key value map = - if invalid_key key map then raise Memory_exn.Bounds ; + if invalid_key key map then raise Exn.Bounds ; let key = Key.add map.first key in {map with values = Map.set key value map.values} @@ -165,7 +190,7 @@ module Make (Effect : Effect.S) (Key : KeyS) : let grow ?produce_value delta map = if Key.unsigned_compare (Key.add delta map.num_elements) map.num_elements < 0 - then raise Memory_exn.SizeOverflow ; + then raise Exn.SizeOverflow ; let map_produce_value old_produce_value = match produce_value with diff --git a/src/lib_webassembly/util/lazy_vector.mli b/src/lib_lazy_containers/lazy_vector.mli similarity index 77% rename from src/lib_webassembly/util/lazy_vector.mli rename to src/lib_lazy_containers/lazy_vector.mli index 6c109ea70e8a..2d15b5f33972 100644 --- a/src/lib_webassembly/util/lazy_vector.mli +++ b/src/lib_lazy_containers/lazy_vector.mli @@ -1,3 +1,28 @@ +(*****************************************************************************) +(* *) +(* 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. *) +(* *) +(*****************************************************************************) + (** A lazy vector is a fixed-size key-value association where each value is created dynamically. @@ -95,12 +120,12 @@ module type S = sig (** [get key vector] retrieves the element at [key]. - @raises Memory_exn.Bounds when trying to access an invalid key *) + @raises Exn.Bounds when trying to access an invalid key *) val get : key -> 'a t -> 'a effect (** [set key value vector] sets the element at [key] to [value]. - @raises Memory_exn.Bounds when trying to set an invalid key *) + @raises Exn.Bounds when trying to set an invalid key *) val set : key -> 'a -> 'a t -> 'a t (** [cons value vector] prepends a value to the front and grows the vector by diff --git a/src/lib_webassembly/tests/chunked_byte_vector_tests.ml b/src/lib_lazy_containers/test/chunked_byte_vector_tests.ml similarity index 61% rename from src/lib_webassembly/tests/chunked_byte_vector_tests.ml rename to src/lib_lazy_containers/test/chunked_byte_vector_tests.ml index a5b4d5c9b1c6..97a7b0363b7d 100644 --- a/src/lib_webassembly/tests/chunked_byte_vector_tests.ml +++ b/src/lib_lazy_containers/test/chunked_byte_vector_tests.ml @@ -1,3 +1,28 @@ +(*****************************************************************************) +(* *) +(* 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. *) +(* *) +(*****************************************************************************) + open QCheck_alcotest open QCheck2 open Chunked_byte_vector diff --git a/src/lib_lazy_containers/test/dune b/src/lib_lazy_containers/test/dune new file mode 100644 index 000000000000..e11a6354b394 --- /dev/null +++ b/src/lib_lazy_containers/test/dune @@ -0,0 +1,20 @@ +; This file was automatically generated, do not edit. +; Edit file manifest/main.ml instead. + +(executable + (name main) + (libraries + lazy-containers + qcheck-core + qcheck-alcotest + alcotest) + (flags + (:standard) + -open Lazy_containers)) + +(rule + (alias runtest) + (package lazy-containers) + (action (run %{dep:./main.exe}))) + +(include_subdirs no) diff --git a/src/lib_webassembly/tests/lazy_vector_tests.ml b/src/lib_lazy_containers/test/lazy_vector_tests.ml similarity index 63% rename from src/lib_webassembly/tests/lazy_vector_tests.ml rename to src/lib_lazy_containers/test/lazy_vector_tests.ml index abbb820db58a..00c908e74ae3 100644 --- a/src/lib_webassembly/tests/lazy_vector_tests.ml +++ b/src/lib_lazy_containers/test/lazy_vector_tests.ml @@ -1,3 +1,28 @@ +(*****************************************************************************) +(* *) +(* 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. *) +(* *) +(*****************************************************************************) + open QCheck_alcotest open QCheck2 open Lazy_vector diff --git a/src/lib_lazy_containers/test/main.ml b/src/lib_lazy_containers/test/main.ml new file mode 100644 index 000000000000..5ef4abf5c62b --- /dev/null +++ b/src/lib_lazy_containers/test/main.ml @@ -0,0 +1,32 @@ +(*****************************************************************************) +(* *) +(* 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. *) +(* *) +(*****************************************************************************) + +let tests = + [ + ("Lazy_vector", Lazy_vector_tests.tests); + ("Chunked_byte_vector", Chunked_byte_vector_tests.tests); + ] + +let () = Alcotest.run "Lazy structs tests" tests diff --git a/src/lib_scoru_wasm/dune b/src/lib_scoru_wasm/dune index fffb6605d85f..7c770bcb1d76 100644 --- a/src/lib_scoru_wasm/dune +++ b/src/lib_scoru_wasm/dune @@ -7,6 +7,8 @@ (instrumentation (backend bisect_ppx)) (libraries tezos-base + tree-encoding + lazy-containers tezos-webassembly-interpreter tezos-context.sigs tezos-lwt-result-stdlib diff --git a/src/lib_scoru_wasm/gather_floppies.ml b/src/lib_scoru_wasm/gather_floppies.ml index 81b649e03329..67ef18a4cafb 100644 --- a/src/lib_scoru_wasm/gather_floppies.ml +++ b/src/lib_scoru_wasm/gather_floppies.ml @@ -24,6 +24,7 @@ (*****************************************************************************) open Wasm_pvm_sig +open Lazy_containers (** FIXME: https://gitlab.com/tezos/tezos/-/issues/3361 Increase the SCORU message size limit, and bump value to be 4,096. *) @@ -109,12 +110,13 @@ module type S = sig end end -module Make (T : Tree.S) (Wasm : Wasm_pvm_sig.S with type tree = T.tree) : +module Make + (T : Tree_encoding.TREE) + (Wasm : Wasm_pvm_sig.S with type tree = T.tree) : S with type tree = T.tree = struct type tree = Wasm.tree - open Tezos_webassembly_interpreter - module Merklizer = Tree_encoding_decoding.Make (T) + module Tree_encoding = Tree_encoding.Make (T) (** The tick state of the [Gathering_floppies] instrumentation. *) type state = { @@ -131,11 +133,11 @@ module Make (T : Tree.S) (Wasm : Wasm_pvm_sig.S with type tree = T.tree) : PVM. *) } - let boot_sector_merklizer : string Merklizer.t = - Merklizer.(value ["boot-sector"] Data_encoding.string) + let boot_sector_merklizer : string Tree_encoding.t = + Tree_encoding.(value ["boot-sector"] Data_encoding.string) - let state_merklizer : state Merklizer.t = - let open Merklizer in + let state_merklizer : state Tree_encoding.t = + let open Tree_encoding in conv (fun (internal_status, last_input_info, internal_tick, kernel) -> {internal_status; last_input_info; internal_tick; kernel}) @@ -173,7 +175,7 @@ module Make (T : Tree.S) (Wasm : Wasm_pvm_sig.S with type tree = T.tree) : It only tries to fetch the current tick (with the same key as the one used in [state_merklizer]. *) let broken_merklizer = - Merklizer.value ["gather-floppies"; "internal-tick"] Data_encoding.n + Tree_encoding.value ["gather-floppies"; "internal-tick"] Data_encoding.n (** [read_state tree] fetches the current state of the PVM from [tree]. *) @@ -182,18 +184,20 @@ module Make (T : Tree.S) (Wasm : Wasm_pvm_sig.S with type tree = T.tree) : Lwt.catch (fun () -> (* First, we try to interpret [tree] as a [state]. *) - let+ state = Merklizer.decode state_merklizer tree in + let+ state = Tree_encoding.decode state_merklizer tree in Running state) (fun _exn -> Lwt.catch (fun () -> (* If it fails, it means the PVM may be stuck. *) - let+ current_tick = Merklizer.decode broken_merklizer tree in + let+ current_tick = Tree_encoding.decode broken_merklizer tree in Broken {current_tick}) (fun _exn -> (* In case both previous attempts have failed, it means this is probably the very first tick of the PVM. *) - let+ boot_sector = Merklizer.decode boot_sector_merklizer tree in + let+ boot_sector = + Tree_encoding.decode boot_sector_merklizer tree + in Halted boot_sector)) (* PROCESS MESSAGES *) @@ -320,14 +324,14 @@ module Make (T : Tree.S) (Wasm : Wasm_pvm_sig.S with type tree = T.tree) : let* state = read_state tree in match state with | Broken {current_tick} -> - Merklizer.encode broken_merklizer (Z.succ current_tick) tree + Tree_encoding.encode broken_merklizer (Z.succ current_tick) tree | Halted origination_message -> ( match origination_kernel_loading_step origination_message with - | Some state -> Merklizer.encode state_merklizer state tree + | Some state -> Tree_encoding.encode state_merklizer state tree | None -> (* We could not interpret [origination_message], meaning the PVM is stuck. *) - Merklizer.encode broken_merklizer Z.one tree) + Tree_encoding.encode broken_merklizer Z.one tree) | Running state -> ( let state = increment_ticks state in match state.internal_status with @@ -353,7 +357,7 @@ module Make (T : Tree.S) (Wasm : Wasm_pvm_sig.S with type tree = T.tree) : match state.internal_status with | Gathering_floppies _ -> let* state = process_input_step input message state in - Merklizer.encode state_merklizer state tree + Tree_encoding.encode state_merklizer state tree | Not_gathering_floppies -> Wasm.set_input_step input message tree) let get_output = Wasm.get_output @@ -408,7 +412,7 @@ module Make (T : Tree.S) (Wasm : Wasm_pvm_sig.S with type tree = T.tree) : module Internal_for_tests = struct let initial_tree_from_boot_sector ~empty_tree boot_sector = match origination_kernel_loading_step boot_sector with - | Some state -> Merklizer.encode state_merklizer state empty_tree + | Some state -> Tree_encoding.encode state_merklizer state empty_tree | None -> raise (Invalid_argument "initial_tree_from_boot_sector: wrong boot sector") diff --git a/src/lib_scoru_wasm/gather_floppies.mli b/src/lib_scoru_wasm/gather_floppies.mli index d78163ea7b23..8d6cb8d16b82 100644 --- a/src/lib_scoru_wasm/gather_floppies.mli +++ b/src/lib_scoru_wasm/gather_floppies.mli @@ -72,5 +72,6 @@ end image as either a complete kernel in the origination message or a kernel image divided into chunks and provided via both origination- and inbox- messages. *) -module Make (T : Tree.S) (Wasm : Wasm_pvm_sig.S with type tree = T.tree) : - S with type tree = T.tree +module Make + (T : Tree_encoding.TREE) + (Wasm : Wasm_pvm_sig.S with type tree = T.tree) : S with type tree = T.tree diff --git a/src/lib_scoru_wasm/test/ast_generators.ml b/src/lib_scoru_wasm/test/ast_generators.ml index fda221495983..b235f819eaf5 100644 --- a/src/lib_scoru_wasm/test/ast_generators.ml +++ b/src/lib_scoru_wasm/test/ast_generators.ml @@ -24,6 +24,7 @@ (*****************************************************************************) open Tezos_webassembly_interpreter +open Lazy_containers open QCheck2.Gen let no_region it = Source.{it; at = no_region} diff --git a/src/lib_scoru_wasm/test/ast_printer.ml b/src/lib_scoru_wasm/test/ast_printer.ml index 85b1b3817d3f..3dfd8ec80fd6 100644 --- a/src/lib_scoru_wasm/test/ast_printer.ml +++ b/src/lib_scoru_wasm/test/ast_printer.ml @@ -23,6 +23,7 @@ (* *) (*****************************************************************************) +open Lazy_containers open Tezos_webassembly_interpreter let pp_pos out {Source.file; line; column} = diff --git a/src/lib_scoru_wasm/test/dune b/src/lib_scoru_wasm/test/dune index eeadad2175c6..7b2769bae504 100644 --- a/src/lib_scoru_wasm/test/dune +++ b/src/lib_scoru_wasm/test/dune @@ -5,6 +5,7 @@ (name test_scoru_wasm) (libraries tezos-base + tree-encoding tezos-base.unix tezos-context.disk tezos-base-test-helpers diff --git a/src/lib_scoru_wasm/test/test_ast_generators.ml b/src/lib_scoru_wasm/test/test_ast_generators.ml index a3147271d06e..a406cf4a4d93 100644 --- a/src/lib_scoru_wasm/test/test_ast_generators.ml +++ b/src/lib_scoru_wasm/test/test_ast_generators.ml @@ -25,7 +25,7 @@ (** Testing ------- - Component: Tree_encoding_decoding + Component: Tree_encoding Invocation: dune exec src/lib_scoru_wasm/test/test_scoru_wasm.exe \ -- test "AST Generators" Subject: Encoding tests for the tezos-scoru-wasm library diff --git a/src/lib_scoru_wasm/test/test_input.ml b/src/lib_scoru_wasm/test/test_input.ml index 8699f52fd9ac..b2fe4eccd28e 100644 --- a/src/lib_scoru_wasm/test/test_input.ml +++ b/src/lib_scoru_wasm/test/test_input.ml @@ -32,6 +32,7 @@ *) open Tztest +open Lazy_containers open Tezos_webassembly_interpreter open Tezos_scoru_wasm @@ -140,7 +141,7 @@ let test_host_fun () = in let module_inst = Tezos_webassembly_interpreter.Instance.empty_module_inst in let memories = - Tezos_webassembly_interpreter.Lazy_vector.LwtInt32Vector.cons + Lazy_vector.LwtInt32Vector.cons (Memory.alloc (MemoryType Types.{min = 20l; max = Some 3600l})) module_inst.memories in @@ -168,11 +169,7 @@ let test_host_fun () = values in let* module_inst = Instance.resolve_module_ref module_ref in - let* memory = - Tezos_webassembly_interpreter.Lazy_vector.LwtInt32Vector.get - 0l - module_inst.memories - in + let* memory = Lazy_vector.LwtInt32Vector.get 0l module_inst.memories in assert (Input_buffer.num_elements input = Z.zero) ; let* m = Memory.load_bytes memory 0l 1 in assert (m = "\001") ; @@ -185,25 +182,41 @@ let test_host_fun () = assert (result = Values.[Num (I32 5l)]) ; Lwt.return @@ Result.return_unit -module Wasm = Wasm_pvm.Make (Test_encoding.Tree) -module EncDec = Tree_encoding_decoding.Make (Test_encoding.Tree) +(* Use context-binary for testing. *) +module Context = Tezos_context_memory.Context_binary + +let empty_tree () = + let open Lwt_syntax in + let* index = Context.init "/tmp" in + let empty_store = Context.empty index in + return @@ Context.Tree.empty empty_store + +module Tree : Tree_encoding.TREE with type tree = Context.tree = struct + type tree = Context.tree + + include Context.Tree +end + +module Wasm = Wasm_pvm.Make (Tree) +module Tree_encoding = Tree_encoding.Make (Tree) let current_tick_encoding = - EncDec.value ["wasm"; "current_tick"] Data_encoding.z + Tree_encoding.value ["wasm"; "current_tick"] Data_encoding.z -let status_encoding = EncDec.value ["input"; "consuming"] Data_encoding.bool +let status_encoding = + Tree_encoding.value ["input"; "consuming"] Data_encoding.bool let floppy_encoding = - EncDec.value + Tree_encoding.value ["gather-floppies"; "status"] Gather_floppies.internal_status_encoding let level_encoding = - EncDec.value ["input"; "level"] Bounded.Int32.NonNegative.encoding + Tree_encoding.value ["input"; "level"] Bounded.Int32.NonNegative.encoding -let id_encoding = EncDec.value ["input"; "id"] Data_encoding.z +let id_encoding = Tree_encoding.value ["input"; "id"] Data_encoding.z -let inp_encoding = EncDec.value ["input"; "0"; "1"] Data_encoding.string +let inp_encoding = Tree_encoding.value ["input"; "0"; "1"] Data_encoding.string let zero = WithExceptions.Option.get ~loc:__LOC__ (Bounded.Int32.NonNegative.of_int32 0l) @@ -215,7 +228,7 @@ let zero = [gather_floppies] *) let initialise_tree () = let open Lwt_syntax in - let* empty_tree = Test_encoding.empty_tree () in + let* empty_tree = empty_tree () in let boot_sector = Data_encoding.Binary.to_string_exn Gather_floppies.origination_message_encoding @@ -227,11 +240,14 @@ let initialise_tree () = boot_sector in - let* tree = EncDec.encode current_tick_encoding Z.zero tree in + let* tree = Tree_encoding.encode current_tick_encoding Z.zero tree in let* tree = - EncDec.encode floppy_encoding Gather_floppies.Not_gathering_floppies tree + Tree_encoding.encode + floppy_encoding + Gather_floppies.Not_gathering_floppies + tree in - let* tree = EncDec.encode status_encoding true tree in + let* tree = Tree_encoding.encode status_encoding true tree in Lwt.return tree (** Artificial initialization of the raw_level and message id. Again, in practice @@ -239,8 +255,8 @@ let initialise_tree () = subsequent read_input steps.*) let add_level_id tree = let open Lwt_syntax in - let* tree = EncDec.encode level_encoding zero tree in - let* tree = EncDec.encode id_encoding Z.zero tree in + let* tree = Tree_encoding.encode level_encoding zero tree in + let* tree = Tree_encoding.encode id_encoding Z.zero tree in Lwt.return tree (** Simple test checking get_info after the initialization. Note that we also @@ -267,17 +283,17 @@ let test_set_input () = let open Lwt_syntax in let* tree = initialise_tree () in let* tree = add_level_id tree in - let* tree = EncDec.encode status_encoding false tree in - let* tree = EncDec.encode status_encoding true tree in + let* tree = Tree_encoding.encode status_encoding false tree in + let* tree = Tree_encoding.encode status_encoding true tree in let* tree = Wasm.set_input_step {inbox_level = zero; message_counter = Z.of_int 1} "hello" tree in - let* result_input = EncDec.decode inp_encoding tree in - let* waiting_for_input = EncDec.decode status_encoding tree in - let* current_tick = EncDec.decode current_tick_encoding tree in + let* result_input = Tree_encoding.decode inp_encoding tree in + let* waiting_for_input = Tree_encoding.decode status_encoding tree in + let* current_tick = Tree_encoding.decode current_tick_encoding tree in let expected_info = let open Wasm_pvm_sig in let last_input_read = Some {inbox_level = zero; message_counter = Z.zero} in diff --git a/src/lib_scoru_wasm/test/test_scoru_wasm.ml b/src/lib_scoru_wasm/test/test_scoru_wasm.ml index 506c77631f21..a480daedef66 100644 --- a/src/lib_scoru_wasm/test/test_scoru_wasm.ml +++ b/src/lib_scoru_wasm/test/test_scoru_wasm.ml @@ -35,7 +35,6 @@ let () = "test lib scoru wasm" [ ("Input", Test_input.tests); - ("Encodings", Test_encoding.tests); ("AST Generators", Test_ast_generators.tests); ("WASM Encodings", Test_wasm_encoding.tests); ] diff --git a/src/lib_scoru_wasm/test/test_wasm_encoding.ml b/src/lib_scoru_wasm/test/test_wasm_encoding.ml index f5021ea2b5d2..7969172125c4 100644 --- a/src/lib_scoru_wasm/test/test_wasm_encoding.ml +++ b/src/lib_scoru_wasm/test/test_wasm_encoding.ml @@ -25,7 +25,7 @@ (** Testing ------- - Component: Tree_encoding_decoding + Component: Tree_encoding Invocation: dune exec src/lib_scoru_wasm/test/test_scoru_wasm.exe \ -- test "WASM Encodings" Subject: Encoding tests for the tezos-scoru-wasm library @@ -59,8 +59,8 @@ module Tree = struct include Context.Tree end -module Merklizer = Tree_encoding_decoding.Make (Tree) -module Wasm_encoding = Wasm_encoding.Make (Merklizer) +module Tree_encoding = Tree_encoding.Make (Tree) +module Wasm_encoding = Wasm_encoding.Make (Tree_encoding) let empty_tree () = let open Lwt_syntax in @@ -71,8 +71,8 @@ let empty_tree () = let encode_decode enc value = let open Lwt_syntax in let* empty_tree = empty_tree () in - let* tree = Merklizer.encode enc value empty_tree in - Merklizer.decode enc tree + let* tree = Tree_encoding.encode enc value empty_tree in + Tree_encoding.decode enc tree (** Test serialize/deserialize instructions. *) let test_instr_roundtrip () = @@ -139,14 +139,14 @@ let test_module_tree () = (* We need to print here in order to force lazy bindings to be evaluated. *) let _ = print module1 in let*! tree1 = - Merklizer.encode + Tree_encoding.encode (Wasm_encoding.module_instance_encoding ~module_reg:lazy_dummy_module_reg) module1 empty_tree in let*! module2 = - Merklizer.decode + Tree_encoding.decode (Wasm_encoding.module_instance_encoding ~module_reg:lazy_dummy_module_reg) tree1 @@ -154,7 +154,7 @@ let test_module_tree () = (* We need to print here in order to force lazy bindings to be evaluated. *) let _ = print module2 in let*! tree2 = - Merklizer.encode + Tree_encoding.encode (Wasm_encoding.module_instance_encoding ~module_reg:lazy_dummy_module_reg) module2 diff --git a/src/lib_scoru_wasm/wasm_encoding.ml b/src/lib_scoru_wasm/wasm_encoding.ml index d05e2379f3c6..af77b8be2820 100644 --- a/src/lib_scoru_wasm/wasm_encoding.ml +++ b/src/lib_scoru_wasm/wasm_encoding.ml @@ -24,16 +24,18 @@ (*****************************************************************************) open Tezos_webassembly_interpreter +open Lazy_containers exception Uninitialized_current_module -module Make (Tree_encoding_decoding : Tree_encoding_decoding.S) = struct +module Make (Tree_encoding : Tree_encoding.S) = struct module V = Instance.Vector module M = Instance.NameMap module C = Chunked_byte_vector.Lwt - include Tree_encoding_decoding - include Lazy_vector_encoding_decoding.Int32 - include Lazy_map_encoding_decoding.NameMap + include Tree_encoding + include Lazy_vector_encoding.Int32 + module NameMap = Lazy_map_encoding.Make (Instance.NameMap) + module ModuleMap = Lazy_map_encoding.Make (Instance.ModuleMap.Map) (** Utility function*) let string_tag = value [] Data_encoding.string @@ -604,7 +606,7 @@ module Make (Tree_encoding_decoding : Tree_encoding_decoding.S) = struct lazy_vector_encoding "refs" (value_ref_encoding ~module_reg) let extern_map_encoding ~module_reg = - lazy_map + NameMap.lazy_map (tagged_union string_tag [ @@ -716,6 +718,5 @@ module Make (Tree_encoding_decoding : Tree_encoding_decoding.S) = struct Instance.ModuleMap.snapshot (scope ["modules"] - (Lazy_map_encoding_decoding.ModuleMap.lazy_map - (module_instance_encoding ~module_reg)))) + (ModuleMap.lazy_map (module_instance_encoding ~module_reg)))) end diff --git a/src/lib_scoru_wasm/wasm_encoding.mli b/src/lib_scoru_wasm/wasm_encoding.mli index 69dbba3b6b6d..85aae716e443 100644 --- a/src/lib_scoru_wasm/wasm_encoding.mli +++ b/src/lib_scoru_wasm/wasm_encoding.mli @@ -27,7 +27,7 @@ open Tezos_webassembly_interpreter exception Uninitialized_current_module -module Make (M : Tree_encoding_decoding.S) : sig +module Make (M : Tree_encoding.S) : sig type tree = M.tree type 'a t = 'a M.t diff --git a/src/lib_scoru_wasm/wasm_pvm.ml b/src/lib_scoru_wasm/wasm_pvm.ml index ffddb241c36f..17ead23297fa 100644 --- a/src/lib_scoru_wasm/wasm_pvm.ml +++ b/src/lib_scoru_wasm/wasm_pvm.ml @@ -30,7 +30,8 @@ *) -module Make (T : Tree.S) : Gather_floppies.S with type tree = T.tree = struct +module Make (T : Tree_encoding.TREE) : + Gather_floppies.S with type tree = T.tree = struct include Gather_floppies.Make (T) @@ -38,8 +39,8 @@ module Make (T : Tree.S) : Gather_floppies.S with type tree = T.tree = struct type tree = T.tree module Wasm = Tezos_webassembly_interpreter - module EncDec = Tree_encoding_decoding.Make (T) - module Wasm_encoding = Wasm_encoding.Make (EncDec) + module Tree_encoding = Tree_encoding.Make (T) + module Wasm_encoding = Wasm_encoding.Make (Tree_encoding) let compute_step = Lwt.return @@ -57,26 +58,29 @@ module Make (T : Tree.S) : Gather_floppies.S with type tree = T.tree = struct *) let current_tick_encoding = - EncDec.value ["wasm"; "current_tick"] Data_encoding.z + Tree_encoding.value ["wasm"; "current_tick"] Data_encoding.z let level_encoding = - EncDec.value ["input"; "level"] Bounded.Int32.NonNegative.encoding + Tree_encoding.value + ["input"; "level"] + Bounded.Int32.NonNegative.encoding - let id_encoding = EncDec.value ["input"; "id"] Data_encoding.z + let id_encoding = Tree_encoding.value ["input"; "id"] Data_encoding.z let last_input_read_encoder = - EncDec.tup2 ~flatten:true level_encoding id_encoding + Tree_encoding.tup2 ~flatten:true level_encoding id_encoding let status_encoding = - EncDec.value ["input"; "consuming"] Data_encoding.bool + Tree_encoding.value ["input"; "consuming"] Data_encoding.bool let inp_encoding level id = - EncDec.value ["input"; level; id] Data_encoding.string + Tree_encoding.value ["input"; level; id] Data_encoding.string let get_info tree = let open Lwt_syntax in let* waiting = - try EncDec.decode status_encoding tree with _ -> Lwt.return false + try Tree_encoding.decode status_encoding tree + with _ -> Lwt.return false in let input_request = if waiting then Wasm_pvm_sig.Input_required @@ -84,7 +88,7 @@ module Make (T : Tree.S) : Gather_floppies.S with type tree = T.tree = struct in let* input = try - let* t = EncDec.decode last_input_read_encoder tree in + let* t = Tree_encoding.decode last_input_read_encoder tree in Lwt.return @@ Some t with _ -> Lwt.return_none in @@ -96,7 +100,7 @@ module Make (T : Tree.S) : Gather_floppies.S with type tree = T.tree = struct in let* current_tick = - try EncDec.decode current_tick_encoding tree + try Tree_encoding.decode current_tick_encoding tree with _ -> Lwt.return Z.zero in Lwt.return Wasm_pvm_sig.{current_tick; last_input_read; input_request} @@ -109,11 +113,14 @@ module Make (T : Tree.S) : Gather_floppies.S with type tree = T.tree = struct Int32.to_string @@ Bounded.Int32.NonNegative.to_int32 inbox_level in let id = Z.to_string message_counter in - let* current_tick = EncDec.decode current_tick_encoding tree in + let* current_tick = Tree_encoding.decode current_tick_encoding tree in let* tree = - EncDec.encode current_tick_encoding (Z.succ current_tick) tree + Tree_encoding.encode + current_tick_encoding + (Z.succ current_tick) + tree in - let* tree = EncDec.encode status_encoding false tree in - EncDec.encode (inp_encoding level id) message tree + let* tree = Tree_encoding.encode status_encoding false tree in + Tree_encoding.encode (inp_encoding level id) message tree end) end diff --git a/src/lib_scoru_wasm/wasm_pvm.mli b/src/lib_scoru_wasm/wasm_pvm.mli index 9fc5ddae55fc..aaaf439bbb7c 100644 --- a/src/lib_scoru_wasm/wasm_pvm.mli +++ b/src/lib_scoru_wasm/wasm_pvm.mli @@ -25,4 +25,4 @@ (** Builds a WASM VM given a concrete implementation of {!Tree.S}. *) -module Make (T : Tree.S) : Gather_floppies.S with type tree = T.tree +module Make (T : Tree_encoding.TREE) : Gather_floppies.S with type tree = T.tree diff --git a/src/lib_scoru_wasm/tree_decoding.ml b/src/lib_tree_encoding/decoding.ml similarity index 100% rename from src/lib_scoru_wasm/tree_decoding.ml rename to src/lib_tree_encoding/decoding.ml diff --git a/src/lib_scoru_wasm/tree_decoding.mli b/src/lib_tree_encoding/decoding.mli similarity index 100% rename from src/lib_scoru_wasm/tree_decoding.mli rename to src/lib_tree_encoding/decoding.mli diff --git a/src/lib_tree_encoding/dune b/src/lib_tree_encoding/dune new file mode 100644 index 000000000000..a666e6a18b16 --- /dev/null +++ b/src/lib_tree_encoding/dune @@ -0,0 +1,15 @@ +; This file was automatically generated, do not edit. +; Edit file manifest/main.ml instead. + +(library + (name tree_encoding) + (public_name tree-encoding) + (instrumentation (backend bisect_ppx)) + (libraries + tezos-base + lazy-containers + tezos-lwt-result-stdlib + data-encoding) + (flags + (:standard) + -open Tezos_base.TzPervasives)) diff --git a/src/lib_scoru_wasm/tree_encoding.ml b/src/lib_tree_encoding/encoding.ml similarity index 100% rename from src/lib_scoru_wasm/tree_encoding.ml rename to src/lib_tree_encoding/encoding.ml diff --git a/src/lib_scoru_wasm/tree_encoding.mli b/src/lib_tree_encoding/encoding.mli similarity index 100% rename from src/lib_scoru_wasm/tree_encoding.mli rename to src/lib_tree_encoding/encoding.mli diff --git a/src/lib_tree_encoding/test/dune b/src/lib_tree_encoding/test/dune new file mode 100644 index 000000000000..2599979911ea --- /dev/null +++ b/src/lib_tree_encoding/test/dune @@ -0,0 +1,24 @@ +; This file was automatically generated, do not edit. +; Edit file manifest/main.ml instead. + +(executable + (name test_tree_encoding) + (libraries + tezos-base + tree-encoding + tezos-base.unix + tezos-context.disk + tezos-base-test-helpers + tezos-test-helpers + tezos-webassembly-interpreter + qcheck-alcotest + alcotest-lwt) + (flags + (:standard) + -open Tezos_base.TzPervasives + -open Tezos_base_test_helpers)) + +(rule + (alias runtest) + (package tree-encoding-test) + (action (run %{dep:./test_tree_encoding.exe}))) diff --git a/src/lib_scoru_wasm/test/test_encoding.ml b/src/lib_tree_encoding/test/test_encoding.ml similarity index 87% rename from src/lib_scoru_wasm/test/test_encoding.ml rename to src/lib_tree_encoding/test/test_encoding.ml index 59e109baff35..56a85c07643b 100644 --- a/src/lib_scoru_wasm/test/test_encoding.ml +++ b/src/lib_tree_encoding/test/test_encoding.ml @@ -25,33 +25,21 @@ (** Testing ------- - Component: Tree_encoding_decoding - Invocation: dune exec src/lib_scoru_wasm/test/test_scoru_wasm.exe \ - -- test "^Encodings$" - Subject: Encoding tests for the tezos-scoru-wasm library + Component: Tree_encoding + Invocation: dune exec src/lib_tree_encoding/test/test_tree_encoding.exe \ + -- test "^Encodings$" + Subject: Encoding tests for the tree-encoding library *) open Tztest -open Tezos_webassembly_interpreter -open Tezos_scoru_wasm +open Lazy_containers (* Use context-binary for testing. *) module Context = Tezos_context_memory.Context_binary -module Tree : - Tezos_context_sigs.Context.TREE - with type t = Context.t - and type tree = Context.tree - and type key = string list - and type value = bytes = struct - type t = Context.t - +module Tree : Tree_encoding.TREE with type tree = Context.tree = struct type tree = Context.tree - type key = Context.key - - type value = Context.value - include Context.Tree end @@ -66,10 +54,10 @@ module Map = let to_string x = x end) -module Merklizer = struct - include Tree_encoding_decoding.Make (Tree) - include Lazy_vector_encoding_decoding.Int - include Lazy_map_encoding_decoding.Make (Map) +module Tree_encoding = struct + include Tree_encoding.Make (Tree) + include Lazy_vector_encoding.Int + include Lazy_map_encoding.Make (Map) end let empty_tree () = @@ -81,15 +69,15 @@ let empty_tree () = let test_encode_decode enc value f = let open Lwt_result_syntax in let*! empty_tree = empty_tree () in - let*! tree = Merklizer.encode enc value empty_tree in - let*! value' = Merklizer.decode enc tree in + let*! tree = Tree_encoding.encode enc value empty_tree in + let*! value' = Tree_encoding.decode enc tree in f value' let test_decode_encode_decode tree enc f = let open Lwt_syntax in - let* value = Merklizer.decode enc tree in - let* tree = Merklizer.encode enc value tree in - let* value' = Merklizer.decode enc tree in + let* value = Tree_encoding.decode enc tree in + let* tree = Tree_encoding.encode enc value tree in + let* value' = Tree_encoding.decode enc tree in f value value' let encode_decode enc value = test_encode_decode enc value Lwt.return @@ -99,7 +87,7 @@ let decode_encode_decode tree enc = let assert_value tree enc v = let open Lwt_result_syntax in - let*! v' = Merklizer.decode enc tree in + let*! v' = Tree_encoding.decode enc tree in assert (v = v') ; return_unit @@ -124,25 +112,26 @@ let assert_decode_round_trip tree enc equal = return_unit let test_string () = - let enc = Merklizer.value ["key"] Data_encoding.string in + let enc = Tree_encoding.value ["key"] Data_encoding.string in assert_round_trip enc "Hello" String.equal let test_int () = - let enc = Merklizer.value ["key"] Data_encoding.int32 in + let enc = Tree_encoding.value ["key"] Data_encoding.int32 in assert_round_trip enc 42l Int32.equal let test_tree () = let enc = - Merklizer.scope ["foo"] @@ Merklizer.value ["key"] Data_encoding.int32 + Tree_encoding.scope ["foo"] + @@ Tree_encoding.value ["key"] Data_encoding.int32 in assert_round_trip enc 42l Int32.equal let test_raw () = - let enc = Merklizer.raw ["key"] in + let enc = Tree_encoding.raw ["key"] in assert_round_trip enc (Bytes.of_string "CAFEBABE") Bytes.equal let test_conv () = - let open Merklizer in + let open Tree_encoding in let enc = conv int_of_string string_of_int (value ["key"] Data_encoding.string) in @@ -154,7 +143,7 @@ type contact = | No_address let contact_enc ?default () = - let open Merklizer in + let open Tree_encoding in tagged_union ?default (value [] Data_encoding.string) @@ -208,7 +197,7 @@ let test_tagged_union_default () = return_unit let test_lazy_mapping () = - let open Merklizer in + let open Tree_encoding in let open Lwt_result_syntax in let enc = lazy_map (value ["key"] Data_encoding.string) in let map = Map.create () in @@ -226,7 +215,7 @@ let test_lazy_mapping () = return_unit let test_add_to_decoded_empty_map () = - let open Merklizer in + let open Tree_encoding in let open Lwt_result_syntax in let enc = lazy_map (value ["key"] Data_encoding.string) in let map = Map.create () in @@ -238,7 +227,7 @@ let test_add_to_decoded_empty_map () = return_unit let test_lazy_vector () = - let open Merklizer in + let open Tree_encoding in let open Lwt_result_syntax in let enc = lazy_vector (value [] Data_encoding.int31) (value [] Data_encoding.string) @@ -258,7 +247,7 @@ let test_lazy_vector () = return_unit let test_chunked_byte_vector () = - let open Merklizer in + let open Tree_encoding in let open Lwt_result_syntax in let vector = Chunked_byte_vector.Lwt.of_string @@ -276,7 +265,7 @@ let test_chunked_byte_vector () = return_unit let test_tuples () = - let open Merklizer in + let open Tree_encoding in let open Lwt_result_syntax in let int = value ["my_int"] Data_encoding.int31 in let* () = @@ -345,7 +334,7 @@ let test_tuples () = return_unit let test_option () = - let open Merklizer in + let open Tree_encoding in let open Lwt_result_syntax in let int = value [] Data_encoding.int31 in let enc = option (tup2 ~flatten:false int int) in @@ -354,7 +343,7 @@ let test_option () = return_unit let test_value_option () = - let open Merklizer in + let open Tree_encoding in let open Lwt_result_syntax in let enc = value_option [] Data_encoding.int31 in let* () = assert_round_trip enc (Some 1) Stdlib.( = ) in @@ -362,28 +351,28 @@ let test_value_option () = return_unit let test_value_default () = - let open Merklizer in + let open Tree_encoding in let open Lwt_result_syntax in let*! tree = empty_tree () in let enc = value ~default:42 [] Data_encoding.int31 in assert_value tree enc 42 let test_optional () = - let open Merklizer in + let open Tree_encoding in let open Lwt_result_syntax in let key = [] in let enc = optional key Data_encoding.int31 in let*! tree = empty_tree () in - let*! tree = Merklizer.encode enc (Some 0) tree in + let*! tree = Tree_encoding.encode enc (Some 0) tree in let* () = assert_value tree enc (Some 0) in - let*! tree = Merklizer.encode enc None tree in + let*! tree = Tree_encoding.encode enc None tree in let* () = assert_missing_value tree key in return_unit type cyclic = {name : string; self : unit -> cyclic} let test_with_self_ref () = - let open Merklizer in + let open Tree_encoding in let open Lwt_result_syntax in let enc () = with_self_reference (fun cycle -> @@ -396,9 +385,9 @@ let test_with_self_ref () = let rec cycle = {name = "Cycle"; self = (fun () -> cycle)} in (* Encode using an encoder and an empty tree. *) let*! empty_tree = empty_tree () in - let*! tree = Merklizer.encode (enc ()) cycle empty_tree in + let*! tree = Tree_encoding.encode (enc ()) cycle empty_tree in (* Decode using a new encoder value and the tree from above. *) - let*! ({name; self} as cycle) = Merklizer.decode (enc ()) tree in + let*! ({name; self} as cycle) = Tree_encoding.decode (enc ()) tree in assert (name = "Cycle") ; assert (cycle == self ()) ; return_unit diff --git a/src/lib_tree_encoding/test/test_tree_encoding.ml b/src/lib_tree_encoding/test/test_tree_encoding.ml new file mode 100644 index 000000000000..d7757b1a80d8 --- /dev/null +++ b/src/lib_tree_encoding/test/test_tree_encoding.ml @@ -0,0 +1,35 @@ +(*****************************************************************************) +(* *) +(* 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. *) +(* *) +(*****************************************************************************) + +(** Testing + ------- + Component: Lib_tree_encoding + Invocation: dune runtest src/lib_tree_encoding/ + Subject: Tests for the tree-encoding library +*) + +let () = + Alcotest_lwt.run "test lib tree encoding" [("Encodings", Test_encoding.tests)] + |> Lwt_main.run diff --git a/src/lib_scoru_wasm/tree.ml b/src/lib_tree_encoding/tree.ml similarity index 89% rename from src/lib_scoru_wasm/tree.ml rename to src/lib_tree_encoding/tree.ml index 2f7860b44db7..7ee7a6d1e8cd 100644 --- a/src/lib_scoru_wasm/tree.ml +++ b/src/lib_tree_encoding/tree.ml @@ -23,7 +23,16 @@ (* *) (*****************************************************************************) -module type S = - Tezos_context_sigs.Context.TREE - with type key = string list - and type value = bytes +module type S = sig + type tree + + type key := string list + + type value := bytes + + val remove : tree -> key -> tree Lwt.t + + val add : tree -> key -> value -> tree Lwt.t + + val find : tree -> key -> value option Lwt.t +end diff --git a/src/lib_scoru_wasm/tree.mli b/src/lib_tree_encoding/tree.mli similarity index 89% rename from src/lib_scoru_wasm/tree.mli rename to src/lib_tree_encoding/tree.mli index bf28e85bb300..d449352c4fb0 100644 --- a/src/lib_scoru_wasm/tree.mli +++ b/src/lib_tree_encoding/tree.mli @@ -26,7 +26,16 @@ (** Exposes a module type {!S} representing trees. *) (** An immutable tree API. *) -module type S = - Tezos_context_sigs.Context.TREE - with type key = string list - and type value = bytes +module type S = sig + type tree + + type key := string list + + type value := bytes + + val remove : tree -> key -> tree Lwt.t + + val add : tree -> key -> value -> tree Lwt.t + + val find : tree -> key -> value option Lwt.t +end diff --git a/src/lib_scoru_wasm/tree_encoding_decoding.ml b/src/lib_tree_encoding/tree_encoding.ml similarity index 94% rename from src/lib_scoru_wasm/tree_encoding_decoding.ml rename to src/lib_tree_encoding/tree_encoding.ml index f2edf1acf39f..408f1339c085 100644 --- a/src/lib_scoru_wasm/tree_encoding_decoding.ml +++ b/src/lib_tree_encoding/tree_encoding.ml @@ -23,7 +23,9 @@ (* *) (*****************************************************************************) -open Tezos_webassembly_interpreter +open Lazy_containers + +module type TREE = Tree.S module type Lwt_vector = Lazy_vector.S with type 'a effect = 'a Lwt.t @@ -38,18 +40,12 @@ module type S = sig type ('tag, 'a) case - module Decoding : Tree_decoding.S with type tree = tree - - module Encoding : Tree_encoding.S with type tree = tree - type 'a t val encode : 'a t -> 'a -> tree -> tree Lwt.t val decode : 'a t -> tree -> 'a Lwt.t - val custom : 'a Encoding.t -> 'a Decoding.t -> 'a t - val conv : ('a -> 'b) -> ('b -> 'a) -> 'a t -> 'b t val conv_lwt : ('a -> 'b Lwt.t) -> ('b -> 'a Lwt.t) -> 'a t -> 'b t @@ -126,7 +122,7 @@ module type S = sig val scope : key -> 'a t -> 'a t - module Lazy_map_encoding_decoding : sig + module Lazy_map_encoding : sig module type S = sig type 'a map @@ -134,13 +130,9 @@ module type S = sig end module Make (Map : Lwt_map) : S with type 'a map := 'a Map.t - - module NameMap : S with type 'a map := 'a Instance.NameMap.t - - module ModuleMap : S with type 'a map := 'a Instance.ModuleMap.Map.t end - module Lazy_vector_encoding_decoding : sig + module Lazy_vector_encoding : sig module type S = sig type key @@ -185,8 +177,8 @@ module type S = sig end module Make (T : Tree.S) : S with type tree = T.tree = struct - module Encoding = Tree_encoding.Make (T) - module Decoding = Tree_decoding.Make (T) + module Encoding = Encoding.Make (T) + module Decoding = Decoding.Make (T) module E = Encoding module D = Decoding @@ -198,8 +190,6 @@ module Make (T : Tree.S) : S with type tree = T.tree = struct type 'a t = {encode : 'a encoding; decode : 'a decoding} - let custom encode decode = {encode; decode} - let conv d e {encode; decode} = {encode = E.contramap e encode; decode = D.map d decode} @@ -339,7 +329,7 @@ module Make (T : Tree.S) : S with type tree = T.tree = struct let value_option key de = value key (Data_encoding.option de) - module Lazy_map_encoding_decoding = struct + module Lazy_map_encoding = struct module type S = sig type 'a map @@ -359,12 +349,9 @@ module Make (T : Tree.S) : S with type tree = T.tree = struct in {encode; decode} end - - module NameMap = Make (Instance.NameMap) - module ModuleMap = Make (Instance.ModuleMap.Map) end - module Lazy_vector_encoding_decoding = struct + module Lazy_vector_encoding = struct module type S = sig type key diff --git a/src/lib_scoru_wasm/tree_encoding_decoding.mli b/src/lib_tree_encoding/tree_encoding.mli similarity index 88% rename from src/lib_scoru_wasm/tree_encoding_decoding.mli rename to src/lib_tree_encoding/tree_encoding.mli index 5a87e1645172..552b0672377b 100644 --- a/src/lib_scoru_wasm/tree_encoding_decoding.mli +++ b/src/lib_tree_encoding/tree_encoding.mli @@ -23,11 +23,24 @@ (* *) (*****************************************************************************) -open Tezos_webassembly_interpreter +module type TREE = sig + type tree + + type key := string list + + type value := bytes + + val remove : tree -> key -> tree Lwt.t + + val add : tree -> key -> value -> tree Lwt.t + + val find : tree -> key -> value option Lwt.t +end -module type Lwt_vector = Lazy_vector.S with type 'a effect = 'a Lwt.t +module type Lwt_vector = + Lazy_containers.Lazy_vector.S with type 'a effect = 'a Lwt.t -module type Lwt_map = Lazy_map.S with type 'a effect = 'a Lwt.t +module type Lwt_map = Lazy_containers.Lazy_map.S with type 'a effect = 'a Lwt.t exception Uninitialized_self_ref @@ -46,12 +59,6 @@ module type S = sig (** Represents an encoder and a decoder. *) type 'a t - (** A decoding module with the same tree type. *) - module Decoding : Tree_decoding.S with type tree = tree - - (** An encoding module with the same tree type. *) - module Encoding : Tree_encoding.S with type tree = tree - (** {2 Functions }*) (** [encode enc x tree] encodes a value x using the encoder [enc] into the @@ -62,11 +69,6 @@ module type S = sig provided [tree]. *) val decode : 'a t -> tree -> 'a Lwt.t - (** [custom enc dec] creates a custom encoder that uses [enc] and [dec]. It's - the users responsibility to provide matching encoder and decoder values. - **) - val custom : 'a Encoding.t -> 'a Decoding.t -> 'a t - (** [conv f g enc] transforms from one encoding to a different one using [f] for mapping the results decoded using [enc], and [g] for mapping from the input. *) @@ -205,7 +207,7 @@ module type S = sig branch [key]. *) val scope : key -> 'a t -> 'a t - module Lazy_map_encoding_decoding : sig + module Lazy_map_encoding : sig module type S = sig type 'a map @@ -215,13 +217,9 @@ module type S = sig (** [Make (YouMap)] creates a module with the [lazy_map] combinator which can be used to decode [YouMap] specifically. *) module Make (Map : Lwt_map) : S with type 'a map := 'a Map.t - - module NameMap : S with type 'a map := 'a Instance.NameMap.t - - module ModuleMap : S with type 'a map := 'a Instance.ModuleMap.Map.t end - module Lazy_vector_encoding_decoding : sig + module Lazy_vector_encoding : sig module type S = sig type key @@ -238,22 +236,26 @@ module type S = sig S with type key := Vector.key and type 'a vector := 'a Vector.t module Int : - S with type key := int and type 'a vector := 'a Lazy_vector.LwtIntVector.t + S + with type key := int + and type 'a vector := 'a Lazy_containers.Lazy_vector.LwtIntVector.t module Int32 : S with type key := int32 - and type 'a vector := 'a Lazy_vector.LwtInt32Vector.t + and type 'a vector := 'a Lazy_containers.Lazy_vector.LwtInt32Vector.t module Z : - S with type key := Z.t and type 'a vector := 'a Lazy_vector.LwtZVector.t + S + with type key := Z.t + and type 'a vector := 'a Lazy_containers.Lazy_vector.LwtZVector.t end (** [chunk] is an encoder for the chunks used by [chunked_by_vector]. *) - val chunk : Chunked_byte_vector.Chunk.t t + val chunk : Lazy_containers.Chunked_byte_vector.Chunk.t t (** [chunked_byte_vector] is an encoder for [chunked_byte_vector]. *) - val chunked_byte_vector : Chunked_byte_vector.Lwt.t t + val chunked_byte_vector : Lazy_containers.Chunked_byte_vector.Lwt.t t (** [case tag enc inj proj] returns a partial encoder that represents a case in a sum-type. The encoder hides the (existentially bound) type of the @@ -293,4 +295,4 @@ end (** Produces an encoder/decoder module with the provided map, vector and tree structures. *) -module Make (T : Tree.S) : S with type tree = T.tree +module Make (T : TREE) : S with type tree = T.tree diff --git a/src/lib_webassembly/bin/dune b/src/lib_webassembly/bin/dune index 8918023d252b..5ccdda36f51b 100644 --- a/src/lib_webassembly/bin/dune +++ b/src/lib_webassembly/bin/dune @@ -6,10 +6,14 @@ (instrumentation (backend bisect_ppx)) (libraries tezos-webassembly-interpreter - lwt.unix) + lwt.unix + tree-encoding + lazy-containers) (flags (:standard) -w -27 - -open Tezos_webassembly_interpreter)) + -open Tezos_webassembly_interpreter + -open Tree_encoding + -open Lazy_containers)) (include dune.inc) diff --git a/src/lib_webassembly/dune b/src/lib_webassembly/dune index aedc35e490bc..91a245ad29de 100644 --- a/src/lib_webassembly/dune +++ b/src/lib_webassembly/dune @@ -7,9 +7,11 @@ (instrumentation (backend bisect_ppx)) (libraries tezos-lwt-result-stdlib - zarith) + zarith + lazy-containers) (flags (:standard) - -w -27)) + -w -27 + -open Lazy_containers)) (include_subdirs unqualified) diff --git a/src/lib_webassembly/runtime/memory_exn.ml b/src/lib_webassembly/runtime/memory_exn.ml index 13a6dcfd5c2e..666c62f11997 100644 --- a/src/lib_webassembly/runtime/memory_exn.ml +++ b/src/lib_webassembly/runtime/memory_exn.ml @@ -1,9 +1,7 @@ exception Type -exception SizeOverflow - exception SizeLimit exception OutOfMemory -exception Bounds +include Lazy_containers.Exn diff --git a/src/lib_webassembly/tests/dune b/src/lib_webassembly/tests/dune index 4969a2c8b871..3baa8375c135 100644 --- a/src/lib_webassembly/tests/dune +++ b/src/lib_webassembly/tests/dune @@ -5,8 +5,6 @@ (name main) (libraries tezos-webassembly-interpreter - qcheck-core - qcheck-alcotest alcotest) (flags (:standard) diff --git a/src/lib_webassembly/tests/main.ml b/src/lib_webassembly/tests/main.ml index ef06412f3a26..3e187136e3a8 100644 --- a/src/lib_webassembly/tests/main.ml +++ b/src/lib_webassembly/tests/main.ml @@ -1,8 +1,3 @@ -let tests = - [ - ("Smallint", Smallint.tests); - ("Lazy_vector", Lazy_vector_tests.tests); - ("Chunked_byte_vector", Chunked_byte_vector_tests.tests); - ] +let tests = [("Smallint", Smallint.tests)] let () = Alcotest.run "WebAssembly reference interpreter tests" tests -- GitLab