From aedf867e4a3445af03a287f67e58797ef4b5320e Mon Sep 17 00:00:00 2001 From: michael Date: Tue, 22 May 2018 19:00:20 +0200 Subject: [PATCH] RPC: Add conn_metadata to peer_info rpcs --- src/lib_base/p2p_peer.ml | 16 +++++++++------- src/lib_base/p2p_peer.mli | 5 +++-- src/lib_p2p/p2p.ml | 8 +++++++- src/lib_p2p/p2p.mli | 3 ++- src/lib_shell/node.ml | 3 ++- src/lib_shell_services/p2p_services.ml | 4 ++-- src/lib_shell_services/p2p_services.mli | 10 ++++++---- 7 files changed, 31 insertions(+), 18 deletions(-) diff --git a/src/lib_base/p2p_peer.ml b/src/lib_base/p2p_peer.ml index 230b22f19..1f81aa0ba 100644 --- a/src/lib_base/p2p_peer.ml +++ b/src/lib_base/p2p_peer.ml @@ -37,9 +37,10 @@ end module Info = struct - type t = { + type 'conn_meta t = { score : float ; trusted : bool ; + conn_metadata : 'conn_meta option; state : State.t ; id_point : P2p_connection.Id.t option ; stat : P2p_stat.t ; @@ -51,30 +52,31 @@ module Info = struct last_miss : (P2p_connection.Id.t * Time.t) option ; } - let encoding = + let encoding conn_metadata_encoding = let open Data_encoding in conv (fun ( - { score ; trusted ; state ; id_point ; stat ; + { score ; trusted ; conn_metadata ; state ; id_point ; stat ; last_failed_connection ; last_rejected_connection ; last_established_connection ; last_disconnection ; last_seen ; last_miss }) -> - ((score, trusted, state, id_point, stat), + ((score, trusted, conn_metadata, state, id_point, stat), (last_failed_connection, last_rejected_connection, last_established_connection, last_disconnection, last_seen, last_miss))) - (fun ((score, trusted, state, id_point, stat), + (fun ((score, trusted, conn_metadata, state, id_point, stat), (last_failed_connection, last_rejected_connection, last_established_connection, last_disconnection, last_seen, last_miss)) -> - { score ; trusted ; state ; id_point ; stat ; + { score ; trusted ; conn_metadata ; state ; id_point ; stat ; last_failed_connection ; last_rejected_connection ; last_established_connection ; last_disconnection ; last_seen ; last_miss }) (merge_objs - (obj5 + (obj6 (req "score" float) (req "trusted" bool) + (opt "conn_metadata" conn_metadata_encoding) (req "state" State.encoding) (opt "reachable_at" P2p_connection.Id.encoding) (req "stat" P2p_stat.encoding)) diff --git a/src/lib_base/p2p_peer.mli b/src/lib_base/p2p_peer.mli index d6d86f05f..08db0f1d2 100644 --- a/src/lib_base/p2p_peer.mli +++ b/src/lib_base/p2p_peer.mli @@ -27,9 +27,10 @@ end module Info : sig - type t = { + type 'conn_meta t = { score : float ; trusted : bool ; + conn_metadata : 'conn_meta option ; state : State.t ; id_point : P2p_connection.Id.t option ; stat : P2p_stat.t ; @@ -41,7 +42,7 @@ module Info : sig last_miss : (P2p_connection.Id.t * Time.t) option ; } - val encoding : t Data_encoding.t + val encoding : 'conn_meta Data_encoding.t -> 'conn_meta t Data_encoding.t end diff --git a/src/lib_p2p/p2p.ml b/src/lib_p2p/p2p.ml index c44362046..f5f43c525 100644 --- a/src/lib_p2p/p2p.ml +++ b/src/lib_p2p/p2p.ml @@ -515,13 +515,19 @@ let info_of_peer_info pool i = | Disconnected -> Disconnected, None in let peer_id = P2p_peer_state.Info.peer_id i in let score = P2p_pool.Peers.get_score pool peer_id in + let conn_opt = P2p_pool.Connection.find_by_peer_id pool peer_id in let stat = - match P2p_pool.Connection.find_by_peer_id pool peer_id with + match conn_opt with | None -> P2p_stat.empty | Some conn -> P2p_pool.Connection.stat conn in + let meta_opt = + match conn_opt with + | None -> None + | Some conn -> Some (P2p_pool.Connection.meta conn) in P2p_peer_state.Info.{ score ; trusted = trusted i ; + conn_metadata = meta_opt ; state ; id_point ; stat ; diff --git a/src/lib_p2p/p2p.mli b/src/lib_p2p/p2p.mli index 2313fdcdf..5b9f4b8ea 100644 --- a/src/lib_p2p/p2p.mli +++ b/src/lib_p2p/p2p.mli @@ -251,7 +251,8 @@ val on_new_connection : ('msg, 'peer_meta, 'conn_meta) net -> (P2p_peer.Id.t -> ('msg, 'peer_meta, 'conn_meta) connection -> unit) -> unit -val build_rpc_directory : _ t -> unit RPC_directory.t +val build_rpc_directory : + (_, _, Connection_metadata.t) t -> unit RPC_directory.t val greylist_addr : ('msg, 'peer_meta, 'conn_meta) net -> P2p_addr.t -> unit val greylist_peer : ('msg, 'peer_meta, 'conn_meta) net -> P2p_peer.Id.t -> unit diff --git a/src/lib_shell/node.ml b/src/lib_shell/node.ml index 6c2a7a548..fdf57d688 100644 --- a/src/lib_shell/node.ml +++ b/src/lib_shell/node.ml @@ -86,7 +86,8 @@ let init_p2p p2p_params = private_node = false ; } in lwt_log_notice "P2P layer is disabled" >>= fun () -> - return (P2p.faked_network peer_metadata_cfg, conn_metadata_cfg) + return + (P2p.faked_network peer_metadata_cfg, conn_metadata_cfg) | Some (config, limits) -> let conn_metadata_cfg = connection_metadata_cfg { diff --git a/src/lib_shell_services/p2p_services.ml b/src/lib_shell_services/p2p_services.ml index bc22a3a7a..00a8fdab5 100644 --- a/src/lib_shell_services/p2p_services.ml +++ b/src/lib_shell_services/p2p_services.ml @@ -181,7 +181,7 @@ module Peers = struct RPC_service.post_service ~query: RPC_query.empty ~input: Data_encoding.empty - ~output: P2p_peer.Info.encoding + ~output: (P2p_peer.Info.encoding Connection_metadata.encoding) ~description:"Details about a given peer." RPC_path.(root / "network" / "peers" /: P2p_peer.Id.rpc_arg) @@ -204,7 +204,7 @@ module Peers = struct ~output: Data_encoding.(list (tup2 P2p_peer.Id.encoding - P2p_peer.Info.encoding)) + (P2p_peer.Info.encoding Connection_metadata.encoding))) ~description:"List the peers the node ever met." RPC_path.(root / "network" / "peers") diff --git a/src/lib_shell_services/p2p_services.mli b/src/lib_shell_services/p2p_services.mli index 1d938bd25..df444438c 100644 --- a/src/lib_shell_services/p2p_services.mli +++ b/src/lib_shell_services/p2p_services.mli @@ -141,9 +141,11 @@ module Peers : sig val list: ?filter:(P2p_peer.State.t list) -> #simple -> - (P2p_peer.Id.t * P2p_peer.Info.t) list tzresult Lwt.t + (P2p_peer.Id.t * Connection_metadata.t P2p_peer.Info.t) list tzresult Lwt.t - val info: #simple -> P2p_peer.Id.t -> P2p_peer.Info.t tzresult Lwt.t + val info: + #simple -> P2p_peer.Id.t -> + Connection_metadata.t P2p_peer.Info.t tzresult Lwt.t val events: #streamed -> P2p_peer.Id.t -> @@ -162,12 +164,12 @@ module Peers : sig val list : ([ `POST ], unit, unit, unit, P2p_peer.State.t list, - (P2p_peer.Id.t * P2p_peer.Info.t) list) RPC_service.t + (P2p_peer.Id.t * Connection_metadata.t P2p_peer.Info.t) list) RPC_service.t val info : ([ `POST ], unit, unit * P2p_peer.Id.t, unit, unit, - P2p_peer.Info.t) RPC_service.t + Connection_metadata.t P2p_peer.Info.t) RPC_service.t val events : ([ `POST ], unit,