diff --git a/manifest/main.ml b/manifest/main.ml index 1f53fbe8bcf611c61b9836d4689963489b1dee6f..0d5956dc7735b9b45f67c5636968464014467cbf 100644 --- a/manifest/main.ml +++ b/manifest/main.ml @@ -1931,7 +1931,7 @@ let octez_shell_context = let _octez_protocol_environment_tests = tests - ["test"; "test_mem_context_array_theory"; "test_cache"] + ["test"; "test_mem_context_array_theory"] ~path:"src/lib_protocol_environment/test" ~opam:"tezos-protocol-environment" ~deps: diff --git a/src/lib_protocol_environment/environment_V5.ml b/src/lib_protocol_environment/environment_V5.ml index 9425e50a5fa70e8d900117744be99cbb75d2303e..e8b289daaf72328127398d2efe5f675b8acebbeb 100644 --- a/src/lib_protocol_environment/environment_V5.ml +++ b/src/lib_protocol_environment/environment_V5.ml @@ -241,7 +241,7 @@ struct module Lwt = Lwt module Data_encoding = struct - include Data_encoding + include Tezos_protocol_environment_structs.V5.Data_encoding type tag_size = [`Uint8 | `Uint16] diff --git a/src/lib_protocol_environment/environment_V6.ml b/src/lib_protocol_environment/environment_V6.ml index 35cc9e792c29b599c15040ca88c9ae711af220e3..8418e886045c6bb059bae24d27d4c355619cb402 100644 --- a/src/lib_protocol_environment/environment_V6.ml +++ b/src/lib_protocol_environment/environment_V6.ml @@ -247,7 +247,7 @@ struct module Lwt = Lwt module Data_encoding = struct - include Data_encoding + include Tezos_protocol_environment_structs.V6.Data_encoding type tag_size = [`Uint8 | `Uint16] diff --git a/src/lib_protocol_environment/environment_V7.ml b/src/lib_protocol_environment/environment_V7.ml index 888cb15cadf8c26714959e875e8be8d515e9689f..77607a11bea7072858d6cf218755aff00486f2b1 100644 --- a/src/lib_protocol_environment/environment_V7.ml +++ b/src/lib_protocol_environment/environment_V7.ml @@ -256,7 +256,7 @@ struct module Lwt = Lwt module Data_encoding = struct - include Data_encoding + include Tezos_protocol_environment_structs.V7.Data_encoding type tag_size = [`Uint8 | `Uint16] diff --git a/src/lib_protocol_environment/environment_V8.ml b/src/lib_protocol_environment/environment_V8.ml index 6a6b32df82d2d9486c81fc5c398d3ee201a7e945..0588c8f171a2b5999c89e4ac1a6166cb307aa805 100644 --- a/src/lib_protocol_environment/environment_V8.ml +++ b/src/lib_protocol_environment/environment_V8.ml @@ -261,7 +261,7 @@ struct module Lwt = Lwt module Data_encoding = struct - include Data_encoding + include Tezos_protocol_environment_structs.V8.Data_encoding type tag_size = [`Uint8 | `Uint16] diff --git a/src/lib_protocol_environment/structs/tezos_protocol_environment_structs.ml b/src/lib_protocol_environment/structs/tezos_protocol_environment_structs.ml index 42dab242a816dc39ba26aa825028362cef55f498..b3fb8569fbc52380bb0f4d054295ee2e99cba492 100644 --- a/src/lib_protocol_environment/structs/tezos_protocol_environment_structs.ml +++ b/src/lib_protocol_environment/structs/tezos_protocol_environment_structs.ml @@ -91,12 +91,14 @@ module V4 = struct end module V5 = struct + module Data_encoding = V5_data_encoding module Error_monad_infix_globals = V0_error_monad_infix_globals module Bounded = V5_bounded module RPC_directory = V0_RPC_directory end module V6 = struct + module Data_encoding = V5_data_encoding module Error_monad_infix_globals = V0_error_monad_infix_globals module Plonk = V6_plonk module Bounded = V5_bounded @@ -104,6 +106,7 @@ module V6 = struct end module V7 = struct + module Data_encoding = V5_data_encoding module Error_monad_infix_globals = V0_error_monad_infix_globals module Array = V7_array module Plonk = V7_plonk diff --git a/src/lib_protocol_environment/structs/v0_data_encoding.ml b/src/lib_protocol_environment/structs/v0_data_encoding.ml index add46e3eb5709352e1079c27a062dbb1ad104ee1..1988704c5d60898fddbd287d23280bc6731752e6 100644 --- a/src/lib_protocol_environment/structs/v0_data_encoding.ml +++ b/src/lib_protocol_environment/structs/v0_data_encoding.ml @@ -25,6 +25,72 @@ include Data_encoding +module Encoding = struct + include Encoding + + let lazy_encoding encoding = + let binary = lazy_encoding encoding in + let json = + let open Json_encoding in + let write (type value) + (module Repr : Json_repr.Repr with type value = value) le = + match force_decode le with + | Some r -> + Json_repr.convert + (module Json_repr.Ezjsonm) + (module Repr) + (Json.construct encoding r) + | None -> + apply_lazy + ~fun_value:(fun _ -> assert false) + ~fun_bytes:(fun b -> + let (`Hex h) = Hex.of_bytes b in + Repr.repr (`O [("unparsed-binary", Repr.repr (`String h))])) + ~fun_combine:(fun _ _ -> assert false) + le + in + let read (type value) + (module Repr : Json_repr.Repr with type value = value) j = + let j = Json_repr.convert (module Repr) (module Json_repr.Ezjsonm) j in + make_lazy encoding (Json.destruct encoding j) + in + repr_agnostic_custom {write; read} ~schema:Json_schema.any + in + Data_encoding__Encoding.raw_splitted ~json ~binary +end + +(* We have to define this twice bc in data-encoding<0.7 the type equality for + [lazy_t] is not propagated. *) +let lazy_encoding encoding = + let binary = lazy_encoding encoding in + let json = + let open Json_encoding in + let write (type value) + (module Repr : Json_repr.Repr with type value = value) le = + match force_decode le with + | Some r -> + Json_repr.convert + (module Json_repr.Ezjsonm) + (module Repr) + (Json.construct encoding r) + | None -> + apply_lazy + ~fun_value:(fun _ -> assert false) + ~fun_bytes:(fun b -> + let (`Hex h) = Hex.of_bytes b in + Repr.repr (`O [("unparsed-binary", Repr.repr (`String h))])) + ~fun_combine:(fun _ _ -> assert false) + le + in + let read (type value) (module Repr : Json_repr.Repr with type value = value) + j = + let j = Json_repr.convert (module Repr) (module Json_repr.Ezjsonm) j in + make_lazy encoding (Json.destruct encoding j) + in + repr_agnostic_custom {write; read} ~schema:Json_schema.any + in + Data_encoding__Encoding.raw_splitted ~json ~binary + module Json = struct include Data_encoding.Json diff --git a/src/lib_protocol_environment/structs/v3_data_encoding.ml b/src/lib_protocol_environment/structs/v3_data_encoding.ml index 9f35ff48880c7c3e97e4cfa511669cceeb280183..7b19599ddbca5927841b6e429c06f1b27dbc6797 100644 --- a/src/lib_protocol_environment/structs/v3_data_encoding.ml +++ b/src/lib_protocol_environment/structs/v3_data_encoding.ml @@ -25,6 +25,72 @@ include Data_encoding +module Encoding = struct + include Encoding + + let lazy_encoding encoding = + let binary = lazy_encoding encoding in + let json = + let open Json_encoding in + let write (type value) + (module Repr : Json_repr.Repr with type value = value) le = + match force_decode le with + | Some r -> + Json_repr.convert + (module Json_repr.Ezjsonm) + (module Repr) + (Json.construct encoding r) + | None -> + apply_lazy + ~fun_value:(fun _ -> assert false) + ~fun_bytes:(fun b -> + let (`Hex h) = Hex.of_bytes b in + Repr.repr (`O [("unparsed-binary", Repr.repr (`String h))])) + ~fun_combine:(fun _ _ -> assert false) + le + in + let read (type value) + (module Repr : Json_repr.Repr with type value = value) j = + let j = Json_repr.convert (module Repr) (module Json_repr.Ezjsonm) j in + make_lazy encoding (Json.destruct encoding j) + in + repr_agnostic_custom {write; read} ~schema:Json_schema.any + in + Data_encoding__Encoding.raw_splitted ~json ~binary +end + +(* We have to define this twice bc in data-encoding<0.7 the type equality for + [lazy_t] is not propagated. *) +let lazy_encoding encoding = + let binary = lazy_encoding encoding in + let json = + let open Json_encoding in + let write (type value) + (module Repr : Json_repr.Repr with type value = value) le = + match force_decode le with + | Some r -> + Json_repr.convert + (module Json_repr.Ezjsonm) + (module Repr) + (Json.construct encoding r) + | None -> + apply_lazy + ~fun_value:(fun _ -> assert false) + ~fun_bytes:(fun b -> + let (`Hex h) = Hex.of_bytes b in + Repr.repr (`O [("unparsed-binary", Repr.repr (`String h))])) + ~fun_combine:(fun _ _ -> assert false) + le + in + let read (type value) (module Repr : Json_repr.Repr with type value = value) + j = + let j = Json_repr.convert (module Repr) (module Json_repr.Ezjsonm) j in + make_lazy encoding (Json.destruct encoding j) + in + repr_agnostic_custom {write; read} ~schema:Json_schema.any + in + Data_encoding__Encoding.raw_splitted ~json ~binary + module Json = struct include Data_encoding.Json diff --git a/src/lib_protocol_environment/structs/v5_data_encoding.ml b/src/lib_protocol_environment/structs/v5_data_encoding.ml new file mode 100644 index 0000000000000000000000000000000000000000..0dc7fdcb656ec9b0f4794d2e642139091a9b81c9 --- /dev/null +++ b/src/lib_protocol_environment/structs/v5_data_encoding.ml @@ -0,0 +1,92 @@ +(*****************************************************************************) +(* *) +(* Open Source License *) +(* Copyright (c) 2022 Nomadic Labs. *) +(* *) +(* Permission is hereby granted, free of charge, to any person obtaining a *) +(* copy of this software and associated documentation files (the "Software"),*) +(* to deal in the Software without restriction, including without limitation *) +(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *) +(* and/or sell copies of the Software, and to permit persons to whom the *) +(* Software is furnished to do so, subject to the following conditions: *) +(* *) +(* The above copyright notice and this permission notice shall be included *) +(* in all copies or substantial portions of the Software. *) +(* *) +(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*) +(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *) +(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *) +(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*) +(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *) +(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *) +(* DEALINGS IN THE SOFTWARE. *) +(* *) +(*****************************************************************************) + +include Data_encoding + +module Encoding = struct + include Encoding + + let lazy_encoding encoding = + let binary = lazy_encoding encoding in + let json = + let open Json_encoding in + let write (type value) + (module Repr : Json_repr.Repr with type value = value) le = + match force_decode le with + | Some r -> + Json_repr.convert + (module Json_repr.Ezjsonm) + (module Repr) + (Json.construct encoding r) + | None -> + apply_lazy + ~fun_value:(fun _ -> assert false) + ~fun_bytes:(fun b -> + let (`Hex h) = Hex.of_bytes b in + Repr.repr (`O [("unparsed-binary", Repr.repr (`String h))])) + ~fun_combine:(fun _ _ -> assert false) + le + in + let read (type value) + (module Repr : Json_repr.Repr with type value = value) j = + let j = Json_repr.convert (module Repr) (module Json_repr.Ezjsonm) j in + make_lazy encoding (Json.destruct encoding j) + in + repr_agnostic_custom {write; read} ~schema:Json_schema.any + in + Data_encoding__Encoding.raw_splitted ~json ~binary +end + +(* We have to define this twice bc in data-encoding<0.7 the type equality for + [lazy_t] is not propagated. *) +let lazy_encoding encoding = + let binary = lazy_encoding encoding in + let json = + let open Json_encoding in + let write (type value) + (module Repr : Json_repr.Repr with type value = value) le = + match force_decode le with + | Some r -> + Json_repr.convert + (module Json_repr.Ezjsonm) + (module Repr) + (Json.construct encoding r) + | None -> + apply_lazy + ~fun_value:(fun _ -> assert false) + ~fun_bytes:(fun b -> + let (`Hex h) = Hex.of_bytes b in + Repr.repr (`O [("unparsed-binary", Repr.repr (`String h))])) + ~fun_combine:(fun _ _ -> assert false) + le + in + let read (type value) (module Repr : Json_repr.Repr with type value = value) + j = + let j = Json_repr.convert (module Repr) (module Json_repr.Ezjsonm) j in + make_lazy encoding (Json.destruct encoding j) + in + repr_agnostic_custom {write; read} ~schema:Json_schema.any + in + Data_encoding__Encoding.raw_splitted ~json ~binary diff --git a/src/lib_protocol_environment/test/dune b/src/lib_protocol_environment/test/dune index c5123f6aeaa38f752269b3c8f97c959cf8f42e2b..6e3bb3b09727696c7d7482642c128323224300cb 100644 --- a/src/lib_protocol_environment/test/dune +++ b/src/lib_protocol_environment/test/dune @@ -2,7 +2,7 @@ ; Edit file manifest/main.ml instead. (executables - (names test test_mem_context_array_theory test_cache) + (names test test_mem_context_array_theory) (libraries tezos-base tezos-base.unix @@ -25,8 +25,3 @@ (alias runtest) (package tezos-protocol-environment) (action (run %{dep:./test_mem_context_array_theory.exe}))) - -(rule - (alias runtest) - (package tezos-protocol-environment) - (action (run %{dep:./test_cache.exe}))) diff --git a/src/lib_protocol_environment/test/test.ml b/src/lib_protocol_environment/test/test.ml index f121a0846057d3246baf462fcaac2712e830239d..0af3b0a88b9c588954cbe6f40900dca916f2d514 100644 --- a/src/lib_protocol_environment/test/test.ml +++ b/src/lib_protocol_environment/test/test.ml @@ -23,8 +23,18 @@ (* *) (*****************************************************************************) +(* Invocation: + dune exec src/lib_protocol_environment/test/test.exe + or for a superset of these tests: + dune build @src/lib_protocol_environment/runtest +*) + let () = Alcotest_lwt.run "tezos-shell-context" - [("mem_context", Test_mem_context.tests); ("cache", Test_cache.tests)] + [ + ("mem_context", Test_mem_context.tests); + ("cache", Test_cache.tests); + ("data_encoding", Test_data_encoding.tests); + ] |> Lwt_main.run diff --git a/src/lib_protocol_environment/test/test_cache.ml b/src/lib_protocol_environment/test/test_cache.ml index 5b13c7ba5d0bffae638ddb464d6824fcde631af9..8bb936526adedd7908e0b0e4c41079773c0bcfe3 100644 --- a/src/lib_protocol_environment/test/test_cache.ml +++ b/src/lib_protocol_environment/test/test_cache.ml @@ -27,7 +27,7 @@ (** Testing ------- Component: Protocol Cache - Invocation: dune build @src/lib_protocol_environment/runtest + Invocation: dune exec src/lib_protocol_environment/test/test.exe -- test "^cache$" Dependencies: src/lib_protocol_environment/test/assert.ml Subject: Low-level operations on protocol cache *) diff --git a/src/lib_protocol_environment/test/test_cache.mli b/src/lib_protocol_environment/test/test_data_encoding.ml similarity index 56% rename from src/lib_protocol_environment/test/test_cache.mli rename to src/lib_protocol_environment/test/test_data_encoding.ml index ed614e82efdd51f101d6d6b63d4767ba034f16f4..6ece1c48ad8ecb3da5b7fa20a2c0098edb6445ce 100644 --- a/src/lib_protocol_environment/test/test_cache.mli +++ b/src/lib_protocol_environment/test/test_data_encoding.ml @@ -1,8 +1,7 @@ (*****************************************************************************) (* *) (* Open Source License *) -(* Copyright (c) 2021 Nomadic Labs *) -(* Copyright (c) 2021 Marigold *) +(* Copyright (c) 2022 Nomadic Labs *) (* *) (* Permission is hereby granted, free of charge, to any person obtaining a *) (* copy of this software and associated documentation files (the "Software"),*) @@ -26,10 +25,52 @@ (** Testing ------- - Component: Protocol Cache - Invocation: dune build @src/lib_protocol_environment/runtest - Dependencies: src/lib_protocol_environment/test/assert.ml - Subject: Low-level operations on protocol cache + Component: Environment structs + Invocation: dune exec src/lib_protocol_environment/test/test.exe -- test "^data_encoding$" + Subject: Environment structs modifications to Data_encoding + e.g. in src/lib_protocol_environment/structs/v5_data_encoding.ml *) -val tests : unit Alcotest_lwt.test_case trace +open Tezos_protocol_environment_structs.V8 + +type t = {x : int; y : string Data_encoding.lazy_t} + +let test_unparsable_lazyexpr () = + let parsed_encoding = Data_encoding.(lazy_encoding (Fixed.string 3)) in + let enc = + let open Data_encoding in + conv + (fun {x; y} -> (x, y)) + (fun (x, y) -> {x; y}) + (obj2 (req "x" int8) (req "y" parsed_encoding)) + in + let bytes_v = Hex.(to_bytes_exn (`Hex "030000000401020304")) in + let v = Data_encoding.Binary.of_bytes_exn enc bytes_v in + let json = + try Data_encoding.Json.construct enc v + with exn -> + Alcotest.failf + "Unexpected exception in %s:@\n%s" + __LOC__ + (Printexc.to_string exn) + in + let expected_json = + `O + [ + ("x", `Float 3.000000); + ("y", `O [("unparsed-binary", `String "01020304")]); + ] + in + Lib_test.Assert.equal + ~pp:Data_encoding.Json.pp + ~msg:"Constructed json is incorrect" + ~loc:__LOC__ + expected_json + json ; + Lwt.return_unit + +let tests = + [ + Alcotest_lwt.test_case "unparsable_lazyexpr" `Quick (fun _ -> + test_unparsable_lazyexpr); + ] diff --git a/src/lib_protocol_environment/test/test_mem_context.ml b/src/lib_protocol_environment/test/test_mem_context.ml index 49bc72320c45a691e810eb78ddaa920510c08444..3a5851539006cdaa39ee71aa216df4dea5a95475 100644 --- a/src/lib_protocol_environment/test/test_mem_context.ml +++ b/src/lib_protocol_environment/test/test_mem_context.ml @@ -26,7 +26,7 @@ (** Testing ------- Component: Protocol Environment - Invocation: dune build @src/lib_protocol_environment/runtest + Invocation: dune exec src/lib_protocol_environment/test/test.exe -- test "^mem_context$" Dependencies: src/lib_protocol_environment/test/assert.ml Subject: Low-level operations on memory contexts. *) diff --git a/src/lib_protocol_environment/test/test_mem_context.mli b/src/lib_protocol_environment/test/test_mem_context.mli deleted file mode 100644 index 7604b41118b4ec060cafa6fa51912b0ef59a8a59..0000000000000000000000000000000000000000 --- a/src/lib_protocol_environment/test/test_mem_context.mli +++ /dev/null @@ -1,36 +0,0 @@ -(*****************************************************************************) -(* *) -(* Open Source License *) -(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. *) -(* *) -(* 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: Protocol Environment - Invocation: dune build @src/lib_protocol_environment/runtest - Dependencies: src/lib_protocol_environment/test/assert.ml - Subject: Low-level operations on memory contexts. -*) - -val tests : unit Alcotest_lwt.test_case trace - -val domain : Context.t -> string list list Lwt.t diff --git a/src/lib_protocol_environment/test/test_mem_context_array_theory.ml b/src/lib_protocol_environment/test/test_mem_context_array_theory.ml index 09e889497d96163efc6112daa355030efe62db92..a2afb1b3172e80bdd1101eba8f9e0d714737d9fd 100644 --- a/src/lib_protocol_environment/test/test_mem_context_array_theory.ml +++ b/src/lib_protocol_environment/test/test_mem_context_array_theory.ml @@ -26,7 +26,7 @@ (** Testing ------- Component: Protocol Environment - Invocation: dune build @src/lib_protocol_environment/runtest + Invocation: dune exec src/lib_protocol_environment/test/test_mem_context_array_theory.exe Dependencies: src/lib_protocol_environment/test/test_mem_context.ml Subject: get/set operations on memory contexts. *)