From e0a2570988138a0b0441a489681233777bbc924e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Henry?= Date: Mon, 22 Jan 2018 18:58:43 +0100 Subject: [PATCH] Refactor: Rename `Node_rpc_services.Network` into `P2p_services` --- src/lib_client_base/client_node_rpcs.ml | 10 +- src/lib_node_p2p_base/p2p_services.ml | 167 +++++++++++++ src/lib_node_p2p_base/p2p_services.mli | 83 +++++++ src/lib_node_services/node_rpc_services.ml | 235 ++---------------- src/lib_node_services/node_rpc_services.mli | 84 ------- src/lib_node_shell/node_rpc.ml | 31 ++- src/lib_rpc/RPC_error.ml | 46 ++++ src/lib_rpc/RPC_error.mli | 15 ++ src/lib_rpc/jbuild | 4 +- .../lib_client_alpha/client_baking_forge.ml | 2 +- 10 files changed, 353 insertions(+), 324 deletions(-) create mode 100644 src/lib_node_p2p_base/p2p_services.ml create mode 100644 src/lib_node_p2p_base/p2p_services.mli create mode 100644 src/lib_rpc/RPC_error.ml create mode 100644 src/lib_rpc/RPC_error.mli diff --git a/src/lib_client_base/client_node_rpcs.ml b/src/lib_client_base/client_node_rpcs.ml index fd25d3f4e..39439e239 100644 --- a/src/lib_client_base/client_node_rpcs.ml +++ b/src/lib_client_base/client_node_rpcs.ml @@ -13,7 +13,7 @@ open Client_rpcs module Services = Node_rpc_services let errors (rpc : #Client_rpcs.ctxt) = - call_service0 rpc Services.Error.service () + call_service0 rpc RPC_error.service () let forge_block_header rpc header = call_service0 rpc Services.forge_block_header header @@ -147,15 +147,15 @@ end module Network = struct let stat cctxt = - call_service0 cctxt Services.Network.stat () + call_service0 cctxt P2p_services.stat () let connections cctxt = - call_service0 cctxt Services.Network.Connection.list () + call_service0 cctxt P2p_services.Connection.list () let peers cctxt = - call_service0 cctxt Services.Network.Peer_id.list [] + call_service0 cctxt P2p_services.Peer_id.list [] let points cctxt = - call_service0 cctxt Services.Network.Point.list [] + call_service0 cctxt P2p_services.Point.list [] end diff --git a/src/lib_node_p2p_base/p2p_services.ml b/src/lib_node_p2p_base/p2p_services.ml new file mode 100644 index 000000000..08907c4df --- /dev/null +++ b/src/lib_node_p2p_base/p2p_services.ml @@ -0,0 +1,167 @@ +(**************************************************************************) +(* *) +(* Copyright (c) 2014 - 2017. *) +(* Dynamic Ledger Solutions, Inc. *) +(* *) +(* All rights reserved. No warranty, explicit or implicit, provided. *) +(* *) +(**************************************************************************) + +open P2p_types + +let (peer_id_arg : P2p_types.Peer_id.t RPC_arg.arg) = + Crypto_box.Public_key_hash.rpc_arg + +let point_arg = + RPC_arg.make + ~name:"point" + ~descr:"A network point (ipv4:port or [ipv6]:port)." + ~destruct:Point.of_string + ~construct:Point.to_string + () + +let versions = + RPC_service.post_service + ~description:"Supported network layer versions." + ~query: RPC_query.empty + ~input: Data_encoding.empty + ~output: (Data_encoding.list P2p_types.Version.encoding) + ~error: Data_encoding.empty + RPC_path.(root / "network" / "versions") + +let stat = + RPC_service.post_service + ~description:"Global network bandwidth statistics in B/s." + ~query: RPC_query.empty + ~input: Data_encoding.empty + ~output: P2p_types.Stat.encoding + ~error: Data_encoding.empty + RPC_path.(root / "network" / "stat") + +let events = + RPC_service.post_service + ~description:"Stream of all network events" + ~query: RPC_query.empty + ~input: Data_encoding.empty + ~output: P2p_types.Connection_pool_log_event.encoding + ~error: Data_encoding.empty + RPC_path.(root / "network" / "log") + +let connect = + RPC_service.post_service + ~description:"Connect to a peer" + ~query: RPC_query.empty + ~input: Data_encoding.(obj1 (dft "timeout" float 5.)) + ~output: (RPC_error.wrap Data_encoding.empty) + ~error: Data_encoding.empty + RPC_path.(root / "network" / "connect" /: point_arg) + +let monitor_encoding = Data_encoding.(obj1 (dft "monitor" bool false)) + +module Connection = struct + + let list = + RPC_service.post_service + ~description:"List the running P2P connection." + ~query: RPC_query.empty + ~input: Data_encoding.empty + ~output: (Data_encoding.list P2p_types.Connection_info.encoding) + ~error: Data_encoding.empty + RPC_path.(root / "network" / "connection") + + let info = + RPC_service.post_service + ~query: RPC_query.empty + ~input: Data_encoding.empty + ~output: (Data_encoding.option P2p_types.Connection_info.encoding) + ~error: Data_encoding.empty + ~description:"Details about the current P2P connection to the given peer." + RPC_path.(root / "network" / "connection" /: peer_id_arg) + + let kick = + RPC_service.post_service + ~query: RPC_query.empty + ~input: Data_encoding.(obj1 (req "wait" bool)) + ~output: Data_encoding.empty + ~error: Data_encoding.empty + ~description:"Forced close of the current P2P connection to the given peer." + RPC_path.(root / "network" / "connection" /: peer_id_arg / "kick") + +end + +module Point = struct + + let info = + RPC_service.post_service + ~query: RPC_query.empty + ~input: Data_encoding.empty + ~output: (Data_encoding.option P2p_types.Point_info.encoding) + ~error: Data_encoding.empty + ~description: "Details about a given `IP:addr`." + RPC_path.(root / "network" / "point" /: point_arg) + + let events = + RPC_service.post_service + ~query: RPC_query.empty + ~input: monitor_encoding + ~output: (Data_encoding.list + P2p_connection_pool_types.Point_info.Event.encoding) + ~error: Data_encoding.empty + ~description: "Monitor network events related to an `IP:addr`." + RPC_path.(root / "network" / "point" /: point_arg / "log") + + let list = + let filter = + let open Data_encoding in + obj1 (dft "filter" (list P2p_types.Point_state.encoding) []) in + RPC_service.post_service + ~query: RPC_query.empty + ~input: filter + ~output: + Data_encoding.(list (tup2 + P2p_types.Point.encoding + P2p_types.Point_info.encoding)) + ~error: Data_encoding.empty + ~description:"List the pool of known `IP:port` \ + used for establishing P2P connections ." + RPC_path.(root / "network" / "point") + +end + +module Peer_id = struct + + let info = + RPC_service.post_service + ~query: RPC_query.empty + ~input: Data_encoding.empty + ~output: (Data_encoding.option P2p_types.Peer_info.encoding) + ~error: Data_encoding.empty + ~description:"Details about a given peer." + RPC_path.(root / "network" / "peer_id" /: peer_id_arg) + + let events = + RPC_service.post_service + ~query: RPC_query.empty + ~input: monitor_encoding + ~output: (Data_encoding.list + P2p_connection_pool_types.Peer_info.Event.encoding) + ~error: Data_encoding.empty + ~description:"Monitor network events related to a given peer." + RPC_path.(root / "network" / "peer_id" /: peer_id_arg / "log") + + let list = + let filter = + let open Data_encoding in + obj1 (dft "filter" (list P2p_types.Peer_state.encoding) []) in + RPC_service.post_service + ~query: RPC_query.empty + ~input: filter + ~output: + Data_encoding.(list (tup2 + P2p_types.Peer_id.encoding + P2p_types.Peer_info.encoding)) + ~error: Data_encoding.empty + ~description:"List the peers the node ever met." + RPC_path.(root / "network" / "peer_id") + +end diff --git a/src/lib_node_p2p_base/p2p_services.mli b/src/lib_node_p2p_base/p2p_services.mli new file mode 100644 index 000000000..719640c47 --- /dev/null +++ b/src/lib_node_p2p_base/p2p_services.mli @@ -0,0 +1,83 @@ +(**************************************************************************) +(* *) +(* Copyright (c) 2014 - 2017. *) +(* Dynamic Ledger Solutions, Inc. *) +(* *) +(* All rights reserved. No warranty, explicit or implicit, provided. *) +(* *) +(**************************************************************************) + +open P2p_types + +val stat : + ([ `POST ], unit, + unit, unit, unit, + Stat.t, unit) RPC_service.t + +val versions : + ([ `POST ], unit, + unit, unit, unit, + Version.t list, unit) RPC_service.t + +val events : + ([ `POST ], unit, + unit, unit, unit, + Connection_pool_log_event.t, unit) RPC_service.t + +val connect : + ([ `POST ], unit, + unit * Point.t, unit, float, + unit tzresult, unit) RPC_service.t + +module Connection : sig + + val list : + ([ `POST ], unit, + unit, unit, unit, + Connection_info.t list, unit) RPC_service.t + + val info : + ([ `POST ], unit, + unit * Peer_id.t, unit, unit, + Connection_info.t option, unit) RPC_service.t + + val kick : + ([ `POST ], unit, + unit * Peer_id.t, unit, bool, + unit, unit) RPC_service.t + +end + +module Point : sig + val list : + ([ `POST ], unit, + unit, unit, Point_state.t list, + (Point.t * Point_info.t) list, unit) RPC_service.t + val info : + ([ `POST ], unit, + unit * Point.t, unit, unit, + Point_info.t option, unit) RPC_service.t + val events : + ([ `POST ], unit, + unit * Point.t, unit, bool, + P2p_connection_pool_types.Point_info.Event.t list, unit) RPC_service.t +end + +module Peer_id : sig + + val list : + ([ `POST ], unit, + unit, unit, Peer_state.t list, + (Peer_id.t * Peer_info.t) list, unit) RPC_service.t + + val info : + ([ `POST ], unit, + unit * Peer_id.t, unit, unit, + Peer_info.t option, unit) RPC_service.t + + val events : + ([ `POST ], unit, + unit * Peer_id.t, unit, bool, + P2p_connection_pool_types.Peer_info.Event.t list, unit) RPC_service.t + +end diff --git a/src/lib_node_services/node_rpc_services.ml b/src/lib_node_services/node_rpc_services.ml index 1ce69227f..9189de1e2 100644 --- a/src/lib_node_services/node_rpc_services.ml +++ b/src/lib_node_services/node_rpc_services.ml @@ -9,46 +9,6 @@ open Data_encoding -module Error = struct - - let service = - RPC_service.post_service - ~description: "Schema for all the RPC errors from the shell" - ~query: RPC_query.empty - ~input: Data_encoding.empty - ~output: Data_encoding.json_schema - ~error: Data_encoding.empty - RPC_path.(root / "errors") - - let encoding = - let { RPC_service.meth ; uri ; _ } = - RPC_service.forge_request service () () in - describe - ~description: - (Printf.sprintf - "The full list of error is available with \ - the global RPC `%s %s`" - (RPC_service.string_of_meth meth) (Uri.path_and_query uri)) - (conv - ~schema:Json_schema.any - (fun exn -> `A (List.map json_of_error exn)) - (function `A exns -> List.map error_of_json exns | _ -> []) - json) - - let wrap param_encoding = - union [ - case (Tag 0) - (obj1 (req "ok" param_encoding)) - (function Ok x -> Some x | _ -> None) - (fun x -> Ok x) ; - case (Tag 1) - (obj1 (req "error" encoding)) - (function Error x -> Some x | _ -> None) - (fun x -> Error x) ; - ] - -end - module Blocks = struct type block = [ @@ -306,7 +266,7 @@ module Blocks = struct unprocessed Operation_hash.Map.empty)) (merge_objs (dynamic_size - (Preapply_result.encoding Error.encoding)) + (Preapply_result.encoding RPC_error.encoding)) (obj1 (req "unprocessed" (list (dynamic_size operation_encoding)))))) ~error: Data_encoding.empty RPC_path.(block_path / "pending_operations") @@ -347,7 +307,7 @@ module Blocks = struct (obj2 (req "shell_header" Block_header.shell_header_encoding) (req "operations" - (list (Preapply_result.encoding Error.encoding))))) + (list (Preapply_result.encoding RPC_error.encoding))))) let preapply = RPC_service.post_service @@ -356,7 +316,7 @@ module Blocks = struct the given operations and return the resulting fitness." ~query: RPC_query.empty ~input: preapply_param_encoding - ~output: (Error.wrap preapply_result_encoding) + ~output: (RPC_error.wrap preapply_result_encoding) ~error: Data_encoding.empty RPC_path.(block_path / "preapply") @@ -463,7 +423,7 @@ module Blocks = struct (obj3 (req "block" Block_hash.encoding) (req "level" int32) - (req "errors" Error.encoding))) + (req "errors" RPC_error.encoding))) ~error: Data_encoding.empty RPC_path.(root / "invalid_blocks") @@ -549,7 +509,7 @@ module Workers = struct (list (obj2 (req "net_id" Net_id.encoding) - (req "status" (Worker_types.worker_status_encoding Error.encoding)))) + (req "status" (Worker_types.worker_status_encoding RPC_error.encoding)))) RPC_path.(root / "workers" / "prevalidators") let state = @@ -562,8 +522,8 @@ module Workers = struct ~output: (Worker_types.full_status_encoding Prevalidator_worker_state.Request.encoding - (Prevalidator_worker_state.Event.encoding Error.encoding) - Error.encoding) + (Prevalidator_worker_state.Event.encoding RPC_error.encoding) + RPC_error.encoding) RPC_path.(root / "workers" / "prevalidators" /: net_id_arg ) end @@ -580,8 +540,8 @@ module Workers = struct ~output: (Worker_types.full_status_encoding Block_validator_worker_state.Request.encoding - (Block_validator_worker_state.Event.encoding Error.encoding) - Error.encoding) + (Block_validator_worker_state.Event.encoding RPC_error.encoding) + RPC_error.encoding) RPC_path.(root / "workers" / "block_validator") end @@ -618,7 +578,7 @@ module Workers = struct (list (obj2 (req "peer_id" P2p_types.Peer_id.encoding) - (req "status" (Worker_types.worker_status_encoding Error.encoding)))) + (req "status" (Worker_types.worker_status_encoding RPC_error.encoding)))) RPC_path.(root / "workers" / "peer_validators" /: net_id_arg) let state = @@ -631,8 +591,8 @@ module Workers = struct ~output: (Worker_types.full_status_encoding Peer_validator_worker_state.Request.encoding - (Peer_validator_worker_state.Event.encoding Error.encoding) - Error.encoding) + (Peer_validator_worker_state.Event.encoding RPC_error.encoding) + RPC_error.encoding) RPC_path.(root / "workers" / "peer_validators" /: net_id_arg /: peer_id_arg) end @@ -659,7 +619,7 @@ module Workers = struct (list (obj2 (req "net_id" Net_id.encoding) - (req "status" (Worker_types.worker_status_encoding Error.encoding)))) + (req "status" (Worker_types.worker_status_encoding RPC_error.encoding)))) RPC_path.(root / "workers" / "net_validators") let state = @@ -672,8 +632,8 @@ module Workers = struct ~output: (Worker_types.full_status_encoding Net_validator_worker_state.Request.encoding - (Net_validator_worker_state.Event.encoding Error.encoding) - Error.encoding) + (Net_validator_worker_state.Event.encoding RPC_error.encoding) + RPC_error.encoding) RPC_path.(root / "workers" / "net_validators" /: net_id_arg ) end @@ -681,165 +641,6 @@ module Workers = struct end -module Network = struct - - open P2p_types - - let (peer_id_arg : P2p_types.Peer_id.t RPC_arg.arg) = - Crypto_box.Public_key_hash.rpc_arg - - let point_arg = - RPC_arg.make - ~name:"point" - ~descr:"A network point (ipv4:port or [ipv6]:port)." - ~destruct:Point.of_string - ~construct:Point.to_string - () - - let versions = - RPC_service.post_service - ~description:"Supported network layer versions." - ~query: RPC_query.empty - ~input: empty - ~output: (list P2p_types.Version.encoding) - ~error: Data_encoding.empty - RPC_path.(root / "network" / "versions") - - let stat = - RPC_service.post_service - ~description:"Global network bandwidth statistics in B/s." - ~query: RPC_query.empty - ~input: empty - ~output: P2p_types.Stat.encoding - ~error: Data_encoding.empty - RPC_path.(root / "network" / "stat") - - let events = - RPC_service.post_service - ~description:"Stream of all network events" - ~query: RPC_query.empty - ~input: empty - ~output: P2p_types.Connection_pool_log_event.encoding - ~error: Data_encoding.empty - RPC_path.(root / "network" / "log") - - let connect = - RPC_service.post_service - ~description:"Connect to a peer" - ~query: RPC_query.empty - ~input: (obj1 (dft "timeout" float 5.)) - ~output: (Error.wrap @@ empty) - ~error: Data_encoding.empty - RPC_path.(root / "network" / "connect" /: point_arg) - - let monitor_encoding = obj1 (dft "monitor" bool false) - - module Connection = struct - - let list = - RPC_service.post_service - ~description:"List the running P2P connection." - ~query: RPC_query.empty - ~input: empty - ~output: (list P2p_types.Connection_info.encoding) - ~error: Data_encoding.empty - RPC_path.(root / "network" / "connection") - - let info = - RPC_service.post_service - ~query: RPC_query.empty - ~input: empty - ~output: (option P2p_types.Connection_info.encoding) - ~error: Data_encoding.empty - ~description:"Details about the current P2P connection to the given peer." - RPC_path.(root / "network" / "connection" /: peer_id_arg) - - let kick = - RPC_service.post_service - ~query: RPC_query.empty - ~input: (obj1 (req "wait" bool)) - ~output: empty - ~error: Data_encoding.empty - ~description:"Forced close of the current P2P connection to the given peer." - RPC_path.(root / "network" / "connection" /: peer_id_arg / "kick") - - end - - module Point = struct - - let info = - RPC_service.post_service - ~query: RPC_query.empty - ~input: empty - ~output: (option P2p_types.Point_info.encoding) - ~error: Data_encoding.empty - ~description: "Details about a given `IP:addr`." - RPC_path.(root / "network" / "point" /: point_arg) - - let events = - RPC_service.post_service - ~query: RPC_query.empty - ~input: monitor_encoding - ~output: (list P2p_connection_pool_types.Point_info.Event.encoding) - ~error: Data_encoding.empty - ~description: "Monitor network events related to an `IP:addr`." - RPC_path.(root / "network" / "point" /: point_arg / "log") - - let list = - let filter = - obj1 (dft "filter" (list P2p_types.Point_state.encoding) []) in - RPC_service.post_service - ~query: RPC_query.empty - ~input: filter - ~output: - (list (tup2 - P2p_types.Point.encoding - P2p_types.Point_info.encoding)) - ~error: Data_encoding.empty - ~description:"List the pool of known `IP:port` \ - used for establishing P2P connections ." - RPC_path.(root / "network" / "point") - - end - - module Peer_id = struct - - let info = - RPC_service.post_service - ~query: RPC_query.empty - ~input: empty - ~output: (option P2p_types.Peer_info.encoding) - ~error: Data_encoding.empty - ~description:"Details about a given peer." - RPC_path.(root / "network" / "peer_id" /: peer_id_arg) - - let events = - RPC_service.post_service - ~query: RPC_query.empty - ~input: monitor_encoding - ~output: (list P2p_connection_pool_types.Peer_info.Event.encoding) - ~error: Data_encoding.empty - ~description:"Monitor network events related to a given peer." - RPC_path.(root / "network" / "peer_id" /: peer_id_arg / "log") - - let list = - let filter = - obj1 (dft "filter" (list P2p_types.Peer_state.encoding) []) in - RPC_service.post_service - ~query: RPC_query.empty - ~input: filter - ~output: - (list (tup2 - P2p_types.Peer_id.encoding - P2p_types.Peer_info.encoding)) - ~error: Data_encoding.empty - ~description:"List the peers the node ever met." - RPC_path.(root / "network" / "peer_id") - - end - -end - let forge_block_header = RPC_service.post_service ~description: "Forge a block header" @@ -897,7 +698,7 @@ let inject_block = ~query: RPC_query.empty ~input: inject_block_param ~output: - (Error.wrap @@ + (RPC_error.wrap @@ (obj1 (req "block_hash" Block_hash.encoding))) ~error: Data_encoding.empty RPC_path.(root / "inject_block") @@ -927,7 +728,7 @@ let inject_operation = true) (opt "net_id" Net_id.encoding)) ~output: - (Error.wrap @@ + (RPC_error.wrap @@ describe ~title: "Hash of the injected operation" @@ (obj1 (req "injectedOperation" Operation_hash.encoding))) @@ -956,7 +757,7 @@ let inject_protocol = "Should we inject protocol that is invalid. (default: false)" bool))) ~output: - (Error.wrap @@ + (RPC_error.wrap @@ describe ~title: "Hash of the injected protocol" @@ (obj1 (req "injectedProtocol" Protocol_hash.encoding))) diff --git a/src/lib_node_services/node_rpc_services.mli b/src/lib_node_services/node_rpc_services.mli index c6c86fb10..1b553f194 100644 --- a/src/lib_node_services/node_rpc_services.mli +++ b/src/lib_node_services/node_rpc_services.mli @@ -7,13 +7,6 @@ (* *) (**************************************************************************) -module Error : sig - val service: - ([ `POST ], unit, unit, unit, unit, Json_schema.schema, unit) RPC_service.t - val encoding: error list Data_encoding.t - val wrap: 'a Data_encoding.t -> 'a tzresult Data_encoding.encoding -end - module Blocks : sig type block = [ @@ -237,83 +230,6 @@ module Workers : sig end -module Network : sig - - val stat : - ([ `POST ], unit, - unit, unit, unit, - P2p_types.Stat.t, unit) RPC_service.t - - val versions : - ([ `POST ], unit, - unit, unit, unit, - P2p_types.Version.t list, unit) RPC_service.t - - val events : - ([ `POST ], unit, - unit, unit, unit, - P2p_types.Connection_pool_log_event.t, unit) RPC_service.t - - val connect : - ([ `POST ], unit, - unit * P2p_types.Point.t, unit, float, - unit tzresult, unit) RPC_service.t - - module Connection : sig - - val list : - ([ `POST ], unit, - unit, unit, unit, - P2p_types.Connection_info.t list, unit) RPC_service.t - - val info : - ([ `POST ], unit, - unit * P2p_types.Peer_id.t, unit, unit, - P2p_types.Connection_info.t option, unit) RPC_service.t - - val kick : - ([ `POST ], unit, - unit * P2p_types.Peer_id.t, unit, bool, - unit, unit) RPC_service.t - - end - - module Point : sig - val list : - ([ `POST ], unit, - unit, unit, P2p_types.Point_state.t list, - (P2p_types.Point.t * P2p_types.Point_info.t) list, unit) RPC_service.t - val info : - ([ `POST ], unit, - unit * P2p_types.Point.t, unit, unit, - P2p_types.Point_info.t option, unit) RPC_service.t - val events : - ([ `POST ], unit, - unit * P2p_types.Point.t, unit, bool, - P2p_connection_pool_types.Point_info.Event.t list, unit) RPC_service.t - end - - module Peer_id : sig - - val list : - ([ `POST ], unit, - unit, unit, P2p_types.Peer_state.t list, - (P2p_types.Peer_id.t * P2p_types.Peer_info.t) list, unit) RPC_service.t - - val info : - ([ `POST ], unit, - unit * P2p_types.Peer_id.t, unit, unit, - P2p_types.Peer_info.t option, unit) RPC_service.t - - val events : - ([ `POST ], unit, - unit * P2p_types.Peer_id.t, unit, bool, - P2p_connection_pool_types.Peer_info.Event.t list, unit) RPC_service.t - - end - -end - val forge_block_header: ([ `POST ], unit, unit, unit, Block_header.t, diff --git a/src/lib_node_shell/node_rpc.ml b/src/lib_node_shell/node_rpc.ml index 9745eaa3d..8739f1e1c 100644 --- a/src/lib_node_shell/node_rpc.ml +++ b/src/lib_node_shell/node_rpc.ml @@ -442,7 +442,7 @@ let build_rpc_directory node = let implementation () () = RPC_answer.return Data_encoding.Json.(schema Error_monad.error_encoding) in - RPC_directory.register0 dir Services.Error.service implementation in + RPC_directory.register0 dir RPC_error.service implementation in let dir = RPC_directory.register1 dir Services.complete (fun s () () -> @@ -527,50 +527,49 @@ let build_rpc_directory node = (* Network : Global *) let dir = - let implementation () () = - Node.RPC.Network.stat node |> RPC_answer.return in - RPC_directory.register0 dir Services.Network.stat implementation in + let implementation () () = Node.RPC.Network.stat node |> RPC_answer.return in + RPC_directory.register0 dir P2p_services.stat implementation in let dir = let implementation () () = RPC_answer.return Distributed_db.Raw.supported_versions in - RPC_directory.register0 dir Services.Network.versions implementation in + RPC_directory.register0 dir P2p_services.versions implementation in let dir = let implementation () () = let stream, stopper = Node.RPC.Network.watch node in let shutdown () = Lwt_watcher.shutdown stopper in let next () = Lwt_stream.get stream in RPC_answer.return_stream { next ; shutdown } in - RPC_directory.register0 dir Services.Network.events implementation in + RPC_directory.register0 dir P2p_services.events implementation in let dir = let implementation point () timeout = Node.RPC.Network.connect node point timeout >>= RPC_answer.return in - RPC_directory.register1 dir Services.Network.connect implementation in + RPC_directory.register1 dir P2p_services.connect implementation in (* Network : Connection *) let dir = let implementation peer_id () () = Node.RPC.Network.Connection.info node peer_id |> RPC_answer.return in - RPC_directory.register1 dir Services.Network.Connection.info implementation in + RPC_directory.register1 dir P2p_services.Connection.info implementation in let dir = let implementation peer_id () wait = Node.RPC.Network.Connection.kick node peer_id wait >>= RPC_answer.return in - RPC_directory.register1 dir Services.Network.Connection.kick implementation in + RPC_directory.register1 dir P2p_services.Connection.kick implementation in let dir = let implementation () () = Node.RPC.Network.Connection.list node |> RPC_answer.return in - RPC_directory.register0 dir Services.Network.Connection.list implementation in + RPC_directory.register0 dir P2p_services.Connection.list implementation in (* Network : Peer_id *) let dir = let implementation () state = Node.RPC.Network.Peer_id.list node ~restrict:state |> RPC_answer.return in - RPC_directory.register0 dir Services.Network.Peer_id.list implementation in + RPC_directory.register0 dir P2p_services.Peer_id.list implementation in let dir = let implementation peer_id () () = Node.RPC.Network.Peer_id.info node peer_id |> RPC_answer.return in - RPC_directory.register1 dir Services.Network.Peer_id.info implementation in + RPC_directory.register1 dir P2p_services.Peer_id.info implementation in let dir = let implementation peer_id () monitor = if monitor then @@ -587,18 +586,18 @@ let build_rpc_directory node = RPC_answer.return_stream { next ; shutdown } else Node.RPC.Network.Peer_id.events node peer_id |> RPC_answer.return in - RPC_directory.register1 dir Services.Network.Peer_id.events implementation in + RPC_directory.register1 dir P2p_services.Peer_id.events implementation in (* Network : Point *) let dir = let implementation () state = Node.RPC.Network.Point.list node ~restrict:state |> RPC_answer.return in - RPC_directory.register0 dir Services.Network.Point.list implementation in + RPC_directory.register0 dir P2p_services.Point.list implementation in let dir = let implementation point () () = Node.RPC.Network.Point.info node point |> RPC_answer.return in - RPC_directory.register1 dir Services.Network.Point.info implementation in + RPC_directory.register1 dir P2p_services.Point.info implementation in let dir = let implementation point () monitor = if monitor then @@ -615,7 +614,7 @@ let build_rpc_directory node = RPC_answer.return_stream { next ; shutdown } else Node.RPC.Network.Point.events node point |> RPC_answer.return in - RPC_directory.register1 dir Services.Network.Point.events implementation in + RPC_directory.register1 dir P2p_services.Point.events implementation in let dir = RPC_directory.register_describe_directory_service dir Services.describe in dir diff --git a/src/lib_rpc/RPC_error.ml b/src/lib_rpc/RPC_error.ml new file mode 100644 index 000000000..eb3b86339 --- /dev/null +++ b/src/lib_rpc/RPC_error.ml @@ -0,0 +1,46 @@ +(**************************************************************************) +(* *) +(* Copyright (c) 2014 - 2017. *) +(* Dynamic Ledger Solutions, Inc. *) +(* *) +(* All rights reserved. No warranty, explicit or implicit, provided. *) +(* *) +(**************************************************************************) + +let service = + RPC_service.post_service + ~description: "Schema for all the RPC errors from the shell" + ~query: RPC_query.empty + ~input: Data_encoding.empty + ~output: Data_encoding.json_schema + ~error: Data_encoding.empty + RPC_path.(root / "errors") + +let encoding = + let { RPC_service.meth ; uri ; _ } = + RPC_service.forge_request service () () in + let open Data_encoding in + describe + ~description: + (Printf.sprintf + "The full list of error is available with \ + the global RPC `%s %s`" + (RPC_service.string_of_meth meth) (Uri.path_and_query uri)) + (conv + ~schema:Json_schema.any + (fun exn -> `A (List.map Error_monad.json_of_error exn)) + (function `A exns -> List.map Error_monad.error_of_json exns | _ -> []) + json) + +let wrap param_encoding = + let open Data_encoding in + union [ + case (Tag 0) + (obj1 (req "ok" param_encoding)) + (function Ok x -> Some x | _ -> None) + (fun x -> Ok x) ; + case (Tag 1) + (obj1 (req "error" encoding)) + (function Error x -> Some x | _ -> None) + (fun x -> Error x) ; + ] diff --git a/src/lib_rpc/RPC_error.mli b/src/lib_rpc/RPC_error.mli new file mode 100644 index 000000000..60acdac91 --- /dev/null +++ b/src/lib_rpc/RPC_error.mli @@ -0,0 +1,15 @@ +(**************************************************************************) +(* *) +(* Copyright (c) 2014 - 2017. *) +(* Dynamic Ledger Solutions, Inc. *) +(* *) +(* All rights reserved. No warranty, explicit or implicit, provided. *) +(* *) +(**************************************************************************) + +open Error_monad + +val service: + ([ `POST ], unit, unit, unit, unit, Json_schema.schema, unit) RPC_service.t +val encoding: error list Data_encoding.t +val wrap: 'a Data_encoding.t -> 'a tzresult Data_encoding.encoding diff --git a/src/lib_rpc/jbuild b/src/lib_rpc/jbuild index 83680e92c..497f3795c 100644 --- a/src/lib_rpc/jbuild +++ b/src/lib_rpc/jbuild @@ -4,10 +4,12 @@ ((name tezos_rpc) (public_name tezos-rpc) (libraries (tezos-data-encoding + tezos-error-monad ocplib-resto)) (flags (:standard -w -9+27-30-32-40@8 -safe-string - -open Tezos_data_encoding)))) + -open Tezos_data_encoding + -open Tezos_error_monad)))) (alias ((name runtest_indent) diff --git a/src/proto_alpha/lib_client_alpha/client_baking_forge.ml b/src/proto_alpha/lib_client_alpha/client_baking_forge.ml index 3ad8ebbbe..9c9a32c81 100644 --- a/src/proto_alpha/lib_client_alpha/client_baking_forge.ml +++ b/src/proto_alpha/lib_client_alpha/client_baking_forge.ml @@ -86,7 +86,7 @@ let () = Data_encoding. (obj2 (req "operation" (dynamic_size Tezos_base.Operation.encoding)) - (req "error" Node_rpc_services.Error.encoding)) + (req "error" RPC_error.encoding)) (function | Failed_to_preapply (hash, err) -> Some (hash, err) | _ -> None)