diff --git a/src/lib_protocol_environment/environment_V5.ml b/src/lib_protocol_environment/environment_V5.ml index 171f408868262dcdd66b7a677f56c1cf2ec31ccb..2701115884596da4cbcac619150d741f65b69851 100644 --- a/src/lib_protocol_environment/environment_V5.ml +++ b/src/lib_protocol_environment/environment_V5.ml @@ -250,7 +250,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 6e55e2df035b0b7e38a4684e62c757d93419f5dd..a2b54755f245842ef9bf3146382d28fc50774a3f 100644 --- a/src/lib_protocol_environment/environment_V6.ml +++ b/src/lib_protocol_environment/environment_V6.ml @@ -255,7 +255,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/structs/tezos_protocol_environment_structs.ml b/src/lib_protocol_environment/structs/tezos_protocol_environment_structs.ml index 0a4c8a98291d13c1b8682ca181e1af07b9135435..4bfdb77718371d20bb54ae496e5cc49c09d50d10 100644 --- a/src/lib_protocol_environment/structs/tezos_protocol_environment_structs.ml +++ b/src/lib_protocol_environment/structs/tezos_protocol_environment_structs.ml @@ -86,10 +86,12 @@ module V4 = struct end module V5 = struct + module Data_encoding = V5_data_encoding module Error_monad_infix_globals = V0_error_monad_infix_globals end module V6 = struct + module Data_encoding = V5_data_encoding module Error_monad_infix_globals = V0_error_monad_infix_globals module Plonk = V6_plonk end 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