From 84d8ae92224ef2c22de593bc2a01168320d13516 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Henry?= Date: Thu, 8 Feb 2018 10:51:01 +0100 Subject: [PATCH] Client refactor: Move `Data_encoding_ezjsonm.read_file` into `Lwt_utils_unix` --- src/bin_node/node_config_file.ml | 4 +-- src/bin_node/node_data_version.ml | 4 +-- src/bin_node/node_identity_file.ml | 4 +-- src/bin_node/node_run_command.ml | 2 +- src/lib_base/data_encoding_ezjsonm.ml | 34 ------------------- src/lib_base/data_encoding_ezjsonm.mli | 16 --------- src/lib_base/tzPervasives.ml | 1 - src/lib_base/tzPervasives.mli | 1 - src/lib_client_base/client_commands.ml | 4 +-- src/lib_client_base/client_config.ml | 2 +- .../test/test_data_encoding.ml | 4 +-- src/lib_p2p/p2p_peer_state.ml | 4 +-- src/lib_stdlib_lwt/lwt_utils_unix.ml | 31 ++++++++++++++++- src/lib_stdlib_lwt/lwt_utils_unix.mli | 12 +++++++ .../lib_protocol/test/helpers/helpers_init.ml | 4 +-- .../lib_protocol/test/helpers/jbuild | 2 ++ 16 files changed, 60 insertions(+), 69 deletions(-) delete mode 100644 src/lib_base/data_encoding_ezjsonm.ml delete mode 100644 src/lib_base/data_encoding_ezjsonm.mli diff --git a/src/bin_node/node_config_file.ml b/src/bin_node/node_config_file.ml index d77b53f58..cadf68296 100644 --- a/src/bin_node/node_config_file.ml +++ b/src/bin_node/node_config_file.ml @@ -440,7 +440,7 @@ let encoding = let read fp = if Sys.file_exists fp then begin - Data_encoding_ezjsonm.read_file fp >>=? fun json -> + Lwt_utils_unix.Json.read_file fp >>=? fun json -> try return (Data_encoding.Json.destruct encoding json) with exn -> fail (Exn exn) end else @@ -448,7 +448,7 @@ let read fp = let write fp cfg = Node_data_version.ensure_data_dir (Filename.dirname fp) >>=? fun () -> - Data_encoding_ezjsonm.write_file fp + Lwt_utils_unix.Json.write_file fp (Data_encoding.Json.construct encoding cfg) let to_string cfg = diff --git a/src/bin_node/node_data_version.ml b/src/bin_node/node_data_version.ml index 86da2c01e..548ff69bd 100644 --- a/src/bin_node/node_data_version.ml +++ b/src/bin_node/node_data_version.ml @@ -91,7 +91,7 @@ let check_data_dir_version data_dir = let version_file = version_file data_dir in fail_unless (Sys.file_exists version_file) (No_data_dir_version_file version_file) >>=? fun () -> - Data_encoding_ezjsonm.read_file version_file + Lwt_utils_unix.Json.read_file version_file |> trace (Could_not_read_data_dir_version version_file) >>=? fun json -> begin try return (Data_encoding.Json.destruct version_encoding json) @@ -104,7 +104,7 @@ let check_data_dir_version data_dir = let ensure_data_dir data_dir = let write_version () = - Data_encoding_ezjsonm.write_file + Lwt_utils_unix.Json.write_file (version_file data_dir) (Data_encoding.Json.construct version_encoding data_version) in try if Sys.file_exists data_dir then diff --git a/src/bin_node/node_identity_file.ml b/src/bin_node/node_identity_file.ml index 9dac8080a..462e24beb 100644 --- a/src/bin_node/node_identity_file.ml +++ b/src/bin_node/node_identity_file.ml @@ -46,7 +46,7 @@ let read ?expected_pow file = | false -> fail (No_identity_file file) | true -> - Data_encoding_ezjsonm.read_file file >>=? fun json -> + Lwt_utils_unix.Json.read_file file >>=? fun json -> let id = Data_encoding.Json.destruct P2p_identity.encoding json in match expected_pow with | None -> return id @@ -80,5 +80,5 @@ let write file identity = fail (Existent_identity_file file) else Node_data_version.ensure_data_dir (Filename.dirname file) >>=? fun () -> - Data_encoding_ezjsonm.write_file file + Lwt_utils_unix.Json.write_file file (Data_encoding.Json.construct P2p_identity.encoding identity) diff --git a/src/bin_node/node_run_command.ml b/src/bin_node/node_run_command.ml index 80f87c90d..28bf0b847 100644 --- a/src/bin_node/node_run_command.ml +++ b/src/bin_node/node_run_command.ml @@ -109,7 +109,7 @@ let init_node ?sandbox (config : Node_config_file.t) = match sandbox_param with | None -> Lwt.return (Some (patch_context None)) | Some file -> - Data_encoding_ezjsonm.read_file file >>= function + Lwt_utils_unix.Json.read_file file >>= function | Error err -> lwt_warn "Can't parse sandbox parameters: %s" file >>= fun () -> diff --git a/src/lib_base/data_encoding_ezjsonm.ml b/src/lib_base/data_encoding_ezjsonm.ml deleted file mode 100644 index 948cb1dc3..000000000 --- a/src/lib_base/data_encoding_ezjsonm.ml +++ /dev/null @@ -1,34 +0,0 @@ -(**************************************************************************) -(* *) -(* Copyright (c) 2014 - 2018. *) -(* Dynamic Ledger Solutions, Inc. *) -(* *) -(* All rights reserved. No warranty, explicit or implicit, provided. *) -(* *) -(**************************************************************************) - -open Error_monad - -let to_root = function - | `O ctns -> `O ctns - | `A ctns -> `A ctns - | `Null -> `O [] - | oth -> `A [ oth ] - -let write_file file json = - let json = to_root json in - protect begin fun () -> - Lwt_io.with_file ~mode:Output file begin fun chan -> - let str = Data_encoding.Json.to_string ~minify:false json in - Lwt_io.write chan str >>= fun _ -> - return () - end - end - -let read_file file = - protect begin fun () -> - Lwt_io.with_file ~mode:Input file begin fun chan -> - Lwt_io.read chan >>= fun str -> - return (Ezjsonm.from_string str :> Data_encoding.json) - end - end diff --git a/src/lib_base/data_encoding_ezjsonm.mli b/src/lib_base/data_encoding_ezjsonm.mli deleted file mode 100644 index 1e1d8678b..000000000 --- a/src/lib_base/data_encoding_ezjsonm.mli +++ /dev/null @@ -1,16 +0,0 @@ -(**************************************************************************) -(* *) -(* Copyright (c) 2014 - 2018. *) -(* Dynamic Ledger Solutions, Inc. *) -(* *) -(* All rights reserved. No warranty, explicit or implicit, provided. *) -(* *) -(**************************************************************************) - -open Error_monad - -(** Loads a JSON file in memory *) -val read_file : string -> Data_encoding.json tzresult Lwt.t - -(** (Over)write a JSON file from in memory data *) -val write_file : string -> Data_encoding.json -> unit tzresult Lwt.t diff --git a/src/lib_base/tzPervasives.ml b/src/lib_base/tzPervasives.ml index 1faa551bd..2e54eb13e 100644 --- a/src/lib_base/tzPervasives.ml +++ b/src/lib_base/tzPervasives.ml @@ -29,7 +29,6 @@ module String = struct end module Time = Time -module Data_encoding_ezjsonm = Data_encoding_ezjsonm module Fitness = Fitness module Block_header = Block_header module Operation = Operation diff --git a/src/lib_base/tzPervasives.mli b/src/lib_base/tzPervasives.mli index a99a5881c..fd66bf8c3 100644 --- a/src/lib_base/tzPervasives.mli +++ b/src/lib_base/tzPervasives.mli @@ -27,7 +27,6 @@ module String : sig end module Time = Time -module Data_encoding_ezjsonm = Data_encoding_ezjsonm module Fitness = Fitness module Block_header = Block_header module Operation = Operation diff --git a/src/lib_client_base/client_commands.ml b/src/lib_client_base/client_commands.ml index 725d21327..3d8c53e03 100644 --- a/src/lib_client_base/client_commands.ml +++ b/src/lib_client_base/client_commands.ml @@ -77,7 +77,7 @@ class file_wallet dir : wallet = object (self) if not (Sys.file_exists filename) then return default else - Data_encoding_ezjsonm.read_file filename + Lwt_utils_unix.Json.read_file filename |> generic_trace "couldn't to read the %s file" alias_name >>=? fun json -> match Data_encoding.Json.destruct encoding json with @@ -94,7 +94,7 @@ class file_wallet dir : wallet = object (self) Lwt_utils_unix.create_dir dir >>= fun () -> let filename = self#filename alias_name in let json = Data_encoding.Json.construct encoding list in - Data_encoding_ezjsonm.write_file filename json) + Lwt_utils_unix.Json.write_file filename json) (fun exn -> Lwt.return (error_exn exn)) |> generic_trace "could not write the %s alias file." alias_name end diff --git a/src/lib_client_base/client_config.ml b/src/lib_client_base/client_config.ml index 1708e1063..0ed340408 100644 --- a/src/lib_client_base/client_config.ml +++ b/src/lib_client_base/client_config.ml @@ -92,7 +92,7 @@ module Cfg_file = struct Data_encoding.Json.destruct encoding json let read fp = - Data_encoding_ezjsonm.read_file fp >>=? fun json -> + Lwt_utils_unix.Json.read_file fp >>=? fun json -> return (from_json json) let write out cfg = diff --git a/src/lib_data_encoding/test/test_data_encoding.ml b/src/lib_data_encoding/test/test_data_encoding.ml index 42ef98497..59b0d137c 100644 --- a/src/lib_data_encoding/test/test_data_encoding.ml +++ b/src/lib_data_encoding/test/test_data_encoding.ml @@ -299,7 +299,7 @@ let test_json_input testdir = } |} in - Data_encoding_ezjsonm.read_file file >>= function + Lwt_utils_unix.Json.read_file file >>= function | Error _ -> Assert.fail_msg "Cannot parse \"good.json\"." | Ok json -> let (id, value, popup) = Json.destruct enc json in @@ -325,7 +325,7 @@ let test_json_input testdir = } |} in - Data_encoding_ezjsonm.read_file file >>= function + Lwt_utils_unix.Json.read_file file >>= function | Error _ -> Assert.fail_msg "Cannot parse \"unknown.json\"." | Ok json -> Assert.test_fail ~msg:__LOC__ diff --git a/src/lib_p2p/p2p_peer_state.ml b/src/lib_p2p/p2p_peer_state.ml index ae21f270c..5439eb714 100644 --- a/src/lib_p2p/p2p_peer_state.ml +++ b/src/lib_p2p/p2p_peer_state.ml @@ -133,14 +133,14 @@ module Info = struct let load path metadata_encoding = let enc = Data_encoding.list (encoding metadata_encoding) in if path <> "/dev/null" && Sys.file_exists path then - Data_encoding_ezjsonm.read_file path >>=? fun json -> + Lwt_utils_unix.Json.read_file path >>=? fun json -> return (Data_encoding.Json.destruct enc json) else return [] let save path metadata_encoding peers = let open Data_encoding in - Data_encoding_ezjsonm.write_file path @@ + Lwt_utils_unix.Json.write_file path @@ Json.construct (list (encoding metadata_encoding)) peers end diff --git a/src/lib_stdlib_lwt/lwt_utils_unix.ml b/src/lib_stdlib_lwt/lwt_utils_unix.ml index 235c7f222..43cff41c5 100644 --- a/src/lib_stdlib_lwt/lwt_utils_unix.ml +++ b/src/lib_stdlib_lwt/lwt_utils_unix.ml @@ -7,7 +7,7 @@ (* *) (**************************************************************************) -open Lwt.Infix +open Error_monad let read_bytes ?(pos = 0) ?len fd buf = let len = match len with None -> Bytes.length buf - pos | Some l -> l in @@ -116,3 +116,32 @@ let getaddrinfo ~passive ~node ~service = (fun { ai_addr ; _ } -> of_sockaddr ai_addr) addr in Lwt.return points + + +module Json = struct + + let to_root = function + | `O ctns -> `O ctns + | `A ctns -> `A ctns + | `Null -> `O [] + | oth -> `A [ oth ] + + let write_file file json = + let json = to_root json in + protect begin fun () -> + Lwt_io.with_file ~mode:Output file begin fun chan -> + let str = Data_encoding.Json.to_string ~minify:false json in + Lwt_io.write chan str >>= fun _ -> + return () + end + end + + let read_file file = + protect begin fun () -> + Lwt_io.with_file ~mode:Input file begin fun chan -> + Lwt_io.read chan >>= fun str -> + return (Ezjsonm.from_string str :> Data_encoding.json) + end + end + +end diff --git a/src/lib_stdlib_lwt/lwt_utils_unix.mli b/src/lib_stdlib_lwt/lwt_utils_unix.mli index ec5143e4e..93235339d 100644 --- a/src/lib_stdlib_lwt/lwt_utils_unix.mli +++ b/src/lib_stdlib_lwt/lwt_utils_unix.mli @@ -7,6 +7,8 @@ (* *) (**************************************************************************) +open Error_monad + val read_bytes: ?pos:int -> ?len:int -> Lwt_unix.file_descr -> bytes -> unit Lwt.t @@ -28,3 +30,13 @@ val getaddrinfo: passive:bool -> node:string -> service:string -> (Ipaddr.V6.t * int) list Lwt.t + +module Json : sig + + (** Loads a JSON file in memory *) + val read_file : string -> Data_encoding.json tzresult Lwt.t + + (** (Over)write a JSON file from in memory data *) + val write_file : string -> Data_encoding.json -> unit tzresult Lwt.t + +end diff --git a/src/proto_alpha/lib_protocol/test/helpers/helpers_init.ml b/src/proto_alpha/lib_protocol/test/helpers/helpers_init.ml index 1fb792997..94aac7a98 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/helpers_init.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/helpers_init.ml @@ -11,11 +11,11 @@ open Proto_alpha open Error_monad let get_sandbox () = - Data_encoding_ezjsonm.read_file + Lwt_utils_unix.Json.read_file "src/proto_alpha/lib_protocol/test/sandbox.json" >>= function | Ok x -> Lwt.return x | Error _ -> - Data_encoding_ezjsonm.read_file "test/sandbox.json" >>= fun x -> + Lwt_utils_unix.Json.read_file "test/sandbox.json" >>= fun x -> Lwt.return @@ Helpers_assert.no_error ~msg:__LOC__ x let main () = diff --git a/src/proto_alpha/lib_protocol/test/helpers/jbuild b/src/proto_alpha/lib_protocol/test/helpers/jbuild index d1f9651fa..eac0b2a1a 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/jbuild +++ b/src/proto_alpha/lib_protocol/test/helpers/jbuild @@ -4,11 +4,13 @@ ((name tezos_proto_alpha_isolate_helpers) (libraries (tezos-test-helpers tezos-base + tezos-stdlib-lwt tezos-protocol-environment-client tezos-protocol-alpha)) (wrapped false) (flags (:standard -w -9-32 -safe-string -open Tezos_base__TzPervasives + -open Tezos_stdlib_lwt -open Tezos_test_helpers -open Tezos_protocol_environment_client))))