From fe559a1f73d7ae7f44cbae70df4622fdc6a9fbb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Henry?= Date: Fri, 8 Dec 2017 19:10:08 +0100 Subject: [PATCH] RPC: share some `RPC_arg` definitions in `lib_base` --- lib_base/time.ml | 13 +++++++++++++ lib_base/time.mli | 2 ++ lib_crypto/blake2B.ml | 15 +++++++++++++++ lib_crypto/jbuild | 2 ++ lib_crypto/net_id.ml | 13 +++++++++++++ lib_crypto/s.ml | 2 ++ lib_crypto/tezos-crypto.opam | 1 + lib_embedded_protocol_alpha/src/services.ml | 13 +------------ lib_node_services/node_rpc_services.ml | 19 ++----------------- lib_protocol_environment_sigs/jbuild | 18 ++++++++++-------- lib_protocol_environment_sigs/v1/hash.mli | 2 ++ 11 files changed, 63 insertions(+), 37 deletions(-) diff --git a/lib_base/time.ml b/lib_base/time.ml index f5439e1cd..5a92deb65 100644 --- a/lib_base/time.ml +++ b/lib_base/time.ml @@ -108,6 +108,19 @@ module T = struct (fun i -> i) ; ]) + let rpc_arg = + RPC_arg.make + ~name:(Format.asprintf "date") + ~descr:(Format.asprintf "A date in seconds from epoch") + ~destruct: + (fun s -> + match Int64.of_string s with + | exception _ -> + Error (Format.asprintf "failed to parse time (epoch): %S" s) + | v -> Ok v) + ~construct:Int64.to_string + () + type 'a timed_data = { data: 'a ; time: t ; diff --git a/lib_base/time.mli b/lib_base/time.mli index 97a0498bd..faab7adfe 100644 --- a/lib_base/time.mli +++ b/lib_base/time.mli @@ -40,6 +40,8 @@ val now : unit -> t val encoding : t Data_encoding.t val rfc_encoding : t Data_encoding.t +val rpc_arg : t RPC_arg.t + val pp_hum : Format.formatter -> t -> unit type 'a timed_data = { diff --git a/lib_crypto/blake2B.ml b/lib_crypto/blake2B.ml index c96c5d217..1418def36 100644 --- a/lib_crypto/blake2B.ml +++ b/lib_crypto/blake2B.ml @@ -161,6 +161,21 @@ module Make (R : sig let to_short_b58check s = String.sub (to_b58check s) 0 (10 + 2 * String.length K.b58check_prefix) + let rpc_arg = + RPC_arg.make + ~name:(Format.asprintf "hash.%s" K.name) + ~descr:(Format.asprintf "A b58check-encoded hash (%s)" K.name) + ~destruct: + (fun s -> + match of_b58check_opt s with + | None -> + Error (Format.asprintf + "failed to decode b58check-encoded hash (%s): %S" + K.name s) + | Some v -> Ok v) + ~construct:to_b58check + () + let encoding = let open Data_encoding in splitted diff --git a/lib_crypto/jbuild b/lib_crypto/jbuild index a7c57c548..778422b61 100644 --- a/lib_crypto/jbuild +++ b/lib_crypto/jbuild @@ -6,11 +6,13 @@ (flags (:standard -open Tezos_stdlib -open Tezos_data_encoding -open Tezos_stdlib_lwt + -open Tezos_rpc_base -open Tezos_error_monad__Error_monad)) (libraries (tezos-stdlib tezos-stdlib-lwt tezos-data-encoding tezos-error-monad + tezos-rpc-base nocrypto sodium zarith)))) diff --git a/lib_crypto/net_id.ml b/lib_crypto/net_id.ml index e3fe29aca..fa8a4ebc7 100644 --- a/lib_crypto/net_id.ml +++ b/lib_crypto/net_id.ml @@ -153,3 +153,16 @@ let zero = match of_hex (String.make (size * 2) '0') with | Some c -> c | None -> assert false + + +let rpc_arg = + RPC_arg.make + ~name:(Format.asprintf "hash.%s" name) + ~descr:(Format.asprintf "A b58check-encoded hash (%s)" name) + ~destruct: + (fun s -> + match of_b58check_opt s with + | None -> Error "" + | Some v -> Ok v) + ~construct:to_b58check + () diff --git a/lib_crypto/s.ml b/lib_crypto/s.ml index 2bc08ff01..4375077fa 100644 --- a/lib_crypto/s.ml +++ b/lib_crypto/s.ml @@ -72,6 +72,8 @@ module type HASH = sig type Base58.data += Hash of t val b58check_encoding: t Base58.encoding + val rpc_arg: t RPC_arg.t + module Set : sig include Set.S with type elt = t val encoding: t Data_encoding.t diff --git a/lib_crypto/tezos-crypto.opam b/lib_crypto/tezos-crypto.opam index 1f6188483..ba2039a05 100644 --- a/lib_crypto/tezos-crypto.opam +++ b/lib_crypto/tezos-crypto.opam @@ -13,6 +13,7 @@ depends: [ "tezos-stdlib-lwt" "tezos-data-encoding" "tezos-error-monad" + "tezos-rpc-base" "nocrypto" "sodium" "zarith" diff --git a/lib_embedded_protocol_alpha/src/services.ml b/lib_embedded_protocol_alpha/src/services.ml index 78ed962eb..f7f9209f4 100644 --- a/lib_embedded_protocol_alpha/src/services.ml +++ b/lib_embedded_protocol_alpha/src/services.ml @@ -266,18 +266,7 @@ module Context = struct module Key = struct - let public_key_hash_arg = - let construct = Ed25519.Public_key_hash.to_b58check in - let destruct hash = - match Ed25519.Public_key_hash.of_b58check_opt hash with - | None -> Error "Cannot parse public key hash" - | Some public_key_hash -> Ok public_key_hash in - RPC_arg.make - ~descr:"A public key hash" - ~name: "public_key_hash" - ~construct - ~destruct - () + let public_key_hash_arg = Ed25519.Public_key_hash.rpc_arg let pk_encoding = (obj2 diff --git a/lib_node_services/node_rpc_services.ml b/lib_node_services/node_rpc_services.ml index 3c89deada..510efc88a 100644 --- a/lib_node_services/node_rpc_services.ml +++ b/lib_node_services/node_rpc_services.ml @@ -459,15 +459,7 @@ end module Protocols = struct - let protocols_arg = - let name = "protocol_id" in - let descr = - "A protocol identifier in hexadecimal." in - let construct = Protocol_hash.to_b58check in - let destruct h = - try Ok (Protocol_hash.of_b58check_exn h) - with _ -> Error "Can't parse hash" in - RPC_arg.make ~name ~descr ~construct ~destruct () + let protocols_arg = Protocol_hash.rpc_arg let contents = RPC_service.post_service @@ -516,14 +508,7 @@ module Network = struct open P2p_types let (peer_id_arg : P2p_types.Peer_id.t RPC_arg.arg) = - RPC_arg.make - ~name:"peer_id" - ~descr:"A network global identifier, also known as an identity." - ~destruct:(fun s -> try - Ok (Crypto_box.Public_key_hash.of_b58check_exn s) - with Failure msg -> Error msg) - ~construct:Crypto_box.Public_key_hash.to_b58check - () + Crypto_box.Public_key_hash.rpc_arg let point_arg = RPC_arg.make diff --git a/lib_protocol_environment_sigs/jbuild b/lib_protocol_environment_sigs/jbuild index 2dda4d5cf..44cb698e5 100644 --- a/lib_protocol_environment_sigs/jbuild +++ b/lib_protocol_environment_sigs/jbuild @@ -29,23 +29,25 @@ v1/compare.mli v1/data_encoding.mli v1/error_monad.mli - v1/micheline.mli v1/logging.mli v1/time.mli - v1/base58.mli - v1/hash.mli - v1/blake2B.mli - v1/ed25519.mli - ;; Tezos specifics - v1/tezos_data.mli - v1/context.mli v1/RPC_arg.mli v1/RPC_path.mli v1/RPC_query.mli v1/RPC_service.mli v1/RPC_answer.mli v1/RPC_directory.mli + + v1/base58.mli + v1/hash.mli + v1/blake2B.mli + v1/ed25519.mli + + ;; Tezos specifics + v1/micheline.mli + v1/tezos_data.mli + v1/context.mli v1/updater.mli )) diff --git a/lib_protocol_environment_sigs/v1/hash.mli b/lib_protocol_environment_sigs/v1/hash.mli index bf08c72b1..b8306049e 100644 --- a/lib_protocol_environment_sigs/v1/hash.mli +++ b/lib_protocol_environment_sigs/v1/hash.mli @@ -68,6 +68,8 @@ module type HASH = sig type Base58.data += Hash of t val b58check_encoding: t Base58.encoding + val rpc_arg: t RPC_arg.t + module Set : sig include Set.S with type elt = t val encoding: t Data_encoding.t