RPC: share some RPC_arg definitions in lib_base

This commit is contained in:
Grégoire Henry 2017-12-08 19:10:08 +01:00 committed by Benjamin Canou
parent 60503aa6fa
commit fe559a1f73
11 changed files with 63 additions and 37 deletions

View File

@ -108,6 +108,19 @@ module T = struct
(fun i -> i) ; (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 = { type 'a timed_data = {
data: 'a ; data: 'a ;
time: t ; time: t ;

View File

@ -40,6 +40,8 @@ val now : unit -> t
val encoding : t Data_encoding.t val encoding : t Data_encoding.t
val rfc_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 val pp_hum : Format.formatter -> t -> unit
type 'a timed_data = { type 'a timed_data = {

View File

@ -161,6 +161,21 @@ module Make (R : sig
let to_short_b58check s = let to_short_b58check s =
String.sub (to_b58check s) 0 (10 + 2 * String.length K.b58check_prefix) 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 encoding =
let open Data_encoding in let open Data_encoding in
splitted splitted

View File

@ -6,11 +6,13 @@
(flags (:standard -open Tezos_stdlib (flags (:standard -open Tezos_stdlib
-open Tezos_data_encoding -open Tezos_data_encoding
-open Tezos_stdlib_lwt -open Tezos_stdlib_lwt
-open Tezos_rpc_base
-open Tezos_error_monad__Error_monad)) -open Tezos_error_monad__Error_monad))
(libraries (tezos-stdlib (libraries (tezos-stdlib
tezos-stdlib-lwt tezos-stdlib-lwt
tezos-data-encoding tezos-data-encoding
tezos-error-monad tezos-error-monad
tezos-rpc-base
nocrypto nocrypto
sodium sodium
zarith)))) zarith))))

View File

@ -153,3 +153,16 @@ let zero =
match of_hex (String.make (size * 2) '0') with match of_hex (String.make (size * 2) '0') with
| Some c -> c | Some c -> c
| None -> assert false | 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
()

View File

@ -72,6 +72,8 @@ module type HASH = sig
type Base58.data += Hash of t type Base58.data += Hash of t
val b58check_encoding: t Base58.encoding val b58check_encoding: t Base58.encoding
val rpc_arg: t RPC_arg.t
module Set : sig module Set : sig
include Set.S with type elt = t include Set.S with type elt = t
val encoding: t Data_encoding.t val encoding: t Data_encoding.t

View File

@ -13,6 +13,7 @@ depends: [
"tezos-stdlib-lwt" "tezos-stdlib-lwt"
"tezos-data-encoding" "tezos-data-encoding"
"tezos-error-monad" "tezos-error-monad"
"tezos-rpc-base"
"nocrypto" "nocrypto"
"sodium" "sodium"
"zarith" "zarith"

View File

@ -266,18 +266,7 @@ module Context = struct
module Key = struct module Key = struct
let public_key_hash_arg = let public_key_hash_arg = Ed25519.Public_key_hash.rpc_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 pk_encoding = let pk_encoding =
(obj2 (obj2

View File

@ -459,15 +459,7 @@ end
module Protocols = struct module Protocols = struct
let protocols_arg = let protocols_arg = Protocol_hash.rpc_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 contents = let contents =
RPC_service.post_service RPC_service.post_service
@ -516,14 +508,7 @@ module Network = struct
open P2p_types open P2p_types
let (peer_id_arg : P2p_types.Peer_id.t RPC_arg.arg) = let (peer_id_arg : P2p_types.Peer_id.t RPC_arg.arg) =
RPC_arg.make Crypto_box.Public_key_hash.rpc_arg
~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
()
let point_arg = let point_arg =
RPC_arg.make RPC_arg.make

View File

@ -29,23 +29,25 @@
v1/compare.mli v1/compare.mli
v1/data_encoding.mli v1/data_encoding.mli
v1/error_monad.mli v1/error_monad.mli
v1/micheline.mli
v1/logging.mli v1/logging.mli
v1/time.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_arg.mli
v1/RPC_path.mli v1/RPC_path.mli
v1/RPC_query.mli v1/RPC_query.mli
v1/RPC_service.mli v1/RPC_service.mli
v1/RPC_answer.mli v1/RPC_answer.mli
v1/RPC_directory.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 v1/updater.mli
)) ))

View File

@ -68,6 +68,8 @@ module type HASH = sig
type Base58.data += Hash of t type Base58.data += Hash of t
val b58check_encoding: t Base58.encoding val b58check_encoding: t Base58.encoding
val rpc_arg: t RPC_arg.t
module Set : sig module Set : sig
include Set.S with type elt = t include Set.S with type elt = t
val encoding: t Data_encoding.t val encoding: t Data_encoding.t