From 95a56753dfbd8881763b517f4bc7a0f8bcb6dcf5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Henry?= Date: Tue, 5 Jun 2018 11:41:23 +0200 Subject: [PATCH] P2p: store `connection_local_metadata` in `P2p_connection.Info.t` --- src/lib_base/p2p_connection.ml | 15 +++++++++------ src/lib_base/p2p_connection.mli | 1 + src/lib_p2p/p2p.ml | 8 ++++++++ src/lib_p2p/p2p.mli | 4 ++++ src/lib_p2p/p2p_pool.ml | 3 +++ src/lib_p2p/p2p_pool.mli | 1 + src/lib_p2p/p2p_socket.ml | 2 ++ src/lib_p2p/p2p_socket.mli | 1 + src/lib_shell/distributed_db.ml | 7 +++---- src/lib_shell/distributed_db.mli | 2 +- src/lib_shell/node.ml | 12 ++++-------- 11 files changed, 37 insertions(+), 19 deletions(-) diff --git a/src/lib_base/p2p_connection.ml b/src/lib_base/p2p_connection.ml index 070e1c460..9724fe47c 100644 --- a/src/lib_base/p2p_connection.ml +++ b/src/lib_base/p2p_connection.ml @@ -60,6 +60,7 @@ module Info = struct remote_socket_port : P2p_addr.port ; versions : P2p_version.t list ; private_node : bool ; + local_metadata : 'meta ; remote_metadata : 'meta ; } @@ -67,25 +68,27 @@ module Info = struct let open Data_encoding in conv (fun { incoming ; peer_id ; id_point ; remote_socket_port ; - versions ; private_node ; remote_metadata } -> + versions ; private_node ; local_metadata ; remote_metadata } -> (incoming, peer_id, id_point, remote_socket_port, - versions, private_node, remote_metadata)) + versions, private_node, local_metadata, remote_metadata)) (fun (incoming, peer_id, id_point, remote_socket_port, - versions, private_node, remote_metadata) -> + versions, private_node, local_metadata, remote_metadata) -> { incoming ; peer_id ; id_point ; remote_socket_port ; - versions ; private_node ; remote_metadata }) - (obj7 + versions ; private_node ; local_metadata ; remote_metadata }) + (obj8 (req "incoming" bool) (req "peer_id" P2p_peer_id.encoding) (req "id_point" Id.encoding) (req "remote_socket_port" uint16) (req "versions" (list P2p_version.encoding)) (req "private" bool) + (req "local_metadata" metadata_encoding) (req "remote_metadata" metadata_encoding)) let pp pp_meta ppf { incoming ; id_point = (remote_addr, remote_port) ; - remote_socket_port ; peer_id ; versions ; private_node ; remote_metadata } = + remote_socket_port ; peer_id ; versions ; private_node ; + local_metadata = _ ; remote_metadata } = let version = List.hd versions in let point = match remote_port with | None -> remote_addr, remote_socket_port diff --git a/src/lib_base/p2p_connection.mli b/src/lib_base/p2p_connection.mli index 756d82763..519679460 100644 --- a/src/lib_base/p2p_connection.mli +++ b/src/lib_base/p2p_connection.mli @@ -38,6 +38,7 @@ module Info : sig remote_socket_port : P2p_addr.port; versions : P2p_version.t list ; private_node : bool ; + local_metadata : 'meta ; remote_metadata : 'meta ; } diff --git a/src/lib_p2p/p2p.ml b/src/lib_p2p/p2p.ml index bf985b86e..fff7f0682 100644 --- a/src/lib_p2p/p2p.ml +++ b/src/lib_p2p/p2p.ml @@ -206,6 +206,8 @@ module Real = struct P2p_pool.disconnect ?wait conn let connection_info _net conn = P2p_pool.Connection.info conn + let connection_local_metadata _net conn = + P2p_pool.Connection.local_metadata conn let connection_remote_metadata _net conn = P2p_pool.Connection.remote_metadata conn let connection_stat _net conn = @@ -313,6 +315,7 @@ module Fake = struct id_point = (Ipaddr.V6.unspecified, None) ; remote_socket_port = 0 ; versions = [] ; + local_metadata = faked_metadata ; remote_metadata = faked_metadata ; private_node = false ; } @@ -332,6 +335,8 @@ type ('msg, 'peer_meta, 'conn_meta) t = { ?wait:bool -> ('msg, 'peer_meta, 'conn_meta) connection -> unit Lwt.t ; connection_info : ('msg, 'peer_meta, 'conn_meta) connection -> 'conn_meta P2p_connection.Info.t ; + connection_local_metadata : + ('msg, 'peer_meta, 'conn_meta) connection -> 'conn_meta ; connection_remote_metadata : ('msg, 'peer_meta, 'conn_meta) connection -> 'conn_meta ; connection_stat : ('msg, 'peer_meta, 'conn_meta) connection -> P2p_stat.t ; @@ -409,6 +414,7 @@ let create ~config ~limits peer_cfg conn_cfg msg_cfg = find_connection = Real.find_connection net ; disconnect = Real.disconnect ; connection_info = Real.connection_info net ; + connection_local_metadata = Real.connection_local_metadata net ; connection_remote_metadata = Real.connection_remote_metadata net ; connection_stat = Real.connection_stat net ; global_stat = Real.global_stat net ; @@ -435,6 +441,7 @@ let faked_network peer_cfg faked_metadata = { find_connection = (fun _ -> None) ; disconnect = (fun ?wait:_ _ -> Lwt.return_unit) ; connection_info = (fun _ -> Fake.connection_info faked_metadata) ; + connection_local_metadata = (fun _ -> faked_metadata) ; connection_remote_metadata = (fun _ -> faked_metadata) ; connection_stat = (fun _ -> Fake.empty_stat) ; global_stat = (fun () -> Fake.empty_stat) ; @@ -459,6 +466,7 @@ let connections net = net.connections () let disconnect net = net.disconnect let find_connection net = net.find_connection let connection_info net = net.connection_info +let connection_local_metadata net = net.connection_local_metadata let connection_remote_metadata net = net.connection_remote_metadata let connection_stat net = net.connection_stat let global_stat net = net.global_stat () diff --git a/src/lib_p2p/p2p.mli b/src/lib_p2p/p2p.mli index 9c21ccfb0..593392df2 100644 --- a/src/lib_p2p/p2p.mli +++ b/src/lib_p2p/p2p.mli @@ -187,6 +187,10 @@ val connection_info : ('msg, 'peer_meta, 'conn_meta) net -> ('msg, 'peer_meta, 'conn_meta) connection -> 'conn_meta P2p_connection.Info.t +val connection_local_metadata : + ('msg, 'peer_meta, 'conn_meta) net -> + ('msg, 'peer_meta, 'conn_meta) connection -> + 'conn_meta val connection_remote_metadata : ('msg, 'peer_meta, 'conn_meta) net -> ('msg, 'peer_meta, 'conn_meta) connection -> diff --git a/src/lib_p2p/p2p_pool.ml b/src/lib_p2p/p2p_pool.ml index 6520e5601..b41f7e614 100644 --- a/src/lib_p2p/p2p_pool.ml +++ b/src/lib_p2p/p2p_pool.ml @@ -596,6 +596,9 @@ module Connection = struct let info { conn } = P2p_socket.info conn + let local_metadata { conn } = + P2p_socket.local_metadata conn + let remote_metadata { conn } = P2p_socket.remote_metadata conn diff --git a/src/lib_p2p/p2p_pool.mli b/src/lib_p2p/p2p_pool.mli index a4673e3c0..8a6a095d6 100644 --- a/src/lib_p2p/p2p_pool.mli +++ b/src/lib_p2p/p2p_pool.mli @@ -219,6 +219,7 @@ val disconnect: module Connection : sig val info: ('msg, 'peer_meta,'conn_meta) connection -> 'conn_meta P2p_connection.Info.t + val local_metadata: ('msg, 'peer_meta,'conn_meta) connection -> 'conn_meta val remote_metadata: ('msg, 'peer_meta,'conn_meta) connection -> 'conn_meta val stat: ('msg, 'peer_meta,'conn_meta) connection -> P2p_stat.t diff --git a/src/lib_p2p/p2p_socket.ml b/src/lib_p2p/p2p_socket.ml index bcea2af46..e78dfe07a 100644 --- a/src/lib_p2p/p2p_socket.ml +++ b/src/lib_p2p/p2p_socket.ml @@ -285,6 +285,7 @@ let authenticate versions = msg.versions ; incoming ; id_point ; remote_socket_port ; private_node = metadata_config.private_node remote_metadata ; + local_metadata ; remote_metadata ; } in return (info, { fd ; info ; cryptobox_data }) @@ -523,6 +524,7 @@ let equal { conn = { id = id1 } } { conn = { id = id2 } } = id1 = id2 let pp ppf { conn } = P2p_connection.Info.pp (fun _ _ -> ()) ppf conn.info let info { conn } = conn.info +let local_metadata { conn } = conn.info.local_metadata let remote_metadata { conn } = conn.info.remote_metadata let private_node { conn } = conn.info.private_node diff --git a/src/lib_p2p/p2p_socket.mli b/src/lib_p2p/p2p_socket.mli index 56457254e..219177c58 100644 --- a/src/lib_p2p/p2p_socket.mli +++ b/src/lib_p2p/p2p_socket.mli @@ -39,6 +39,7 @@ val equal: ('mst, 'meta) t -> ('msg, 'meta) t -> bool val pp: Format.formatter -> ('msg, 'meta) t -> unit val info: ('msg, 'meta) t -> 'meta P2p_connection.Info.t +val local_metadata: ('msg, 'meta) t -> 'meta val remote_metadata: ('msg, 'meta) t -> 'meta val private_node: ('msg, 'meta) t -> bool diff --git a/src/lib_shell/distributed_db.ml b/src/lib_shell/distributed_db.ml index d2ea4f8c5..1385ba664 100644 --- a/src/lib_shell/distributed_db.ml +++ b/src/lib_shell/distributed_db.ml @@ -305,7 +305,6 @@ type db = { protocol_db: Raw_protocol.t ; block_input: (Block_hash.t * Block_header.t) Lwt_watcher.input ; operation_input: (Operation_hash.t * Operation.t) Lwt_watcher.input ; - connection_metadata_value: (P2p_peer.Id.t -> Connection_metadata.t) } and chain_db = { @@ -518,7 +517,7 @@ module P2p_reader = struct let head = Block_header.hash header in State.Block.known_invalid chain_db.chain_state head >>= fun known_invalid -> let { Connection_metadata.disable_mempool } = - chain_db.global_db.connection_metadata_value state.gid in + P2p.connection_local_metadata chain_db.global_db.p2p state.conn in let known_invalid = known_invalid || (disable_mempool && mempool <> Mempool.empty) @@ -708,7 +707,7 @@ let raw_try_send p2p peer_id msg = | Some conn -> ignore (P2p.try_send p2p conn msg : bool) -let create disk p2p connection_metadata_value = +let create disk p2p = let global_request = { data = () ; active = active_peer_ids p2p ; @@ -723,7 +722,7 @@ let create disk p2p connection_metadata_value = { p2p ; p2p_readers ; disk ; active_chains ; protocol_db ; block_input ; operation_input ; - connection_metadata_value } in + } in P2p.on_new_connection p2p (P2p_reader.run db) ; P2p.iter_connections p2p (P2p_reader.run db) ; db diff --git a/src/lib_shell/distributed_db.mli b/src/lib_shell/distributed_db.mli index 44a5a6e39..429579344 100644 --- a/src/lib_shell/distributed_db.mli +++ b/src/lib_shell/distributed_db.mli @@ -18,7 +18,7 @@ module Message = Distributed_db_message type p2p = (Message.t, Peer_metadata.t, Connection_metadata.t) P2p.net -val create: State.t -> p2p -> (P2p_peer.Id.t -> Connection_metadata.t) -> t +val create: State.t -> p2p -> t val state: db -> State.t val shutdown: t -> unit Lwt.t diff --git a/src/lib_shell/node.ml b/src/lib_shell/node.ml index 57dfb4886..71fc0f115 100644 --- a/src/lib_shell/node.ml +++ b/src/lib_shell/node.ml @@ -84,7 +84,6 @@ let init_connection_metadata opt = | None -> { disable_mempool = false ; private_node = false } - | Some c -> { disable_mempool = c.P2p.disable_mempool ; private_node = c.P2p.private_mode } @@ -93,10 +92,8 @@ let init_p2p p2p_params = match p2p_params with | None -> let c_meta = init_connection_metadata None in - let conn_metadata_cfg = connection_metadata_cfg c_meta in lwt_log_notice "P2P layer is disabled" >>= fun () -> - return - (P2p.faked_network peer_metadata_cfg c_meta, conn_metadata_cfg) + return (P2p.faked_network peer_metadata_cfg c_meta) | Some (config, limits) -> let c_meta = init_connection_metadata (Some config) in let conn_metadata_cfg = connection_metadata_cfg c_meta in @@ -107,7 +104,7 @@ let init_p2p p2p_params = conn_metadata_cfg Distributed_db_message.cfg >>=? fun p2p -> Lwt.async (fun () -> P2p.maintain p2p) ; - return (p2p, conn_metadata_cfg) + return p2p type config = { genesis: State.Chain.genesis ; @@ -159,11 +156,10 @@ let create { genesis ; store_root ; context_root ; match p2p_params with | Some (config, _limits) -> not config.P2p.disable_mempool | None -> true in - init_p2p p2p_params >>=? fun (p2p, conn_metadata_cfg) -> + init_p2p p2p_params >>=? fun p2p -> State.read ~store_root ~context_root ?patch_context genesis >>=? fun (state, mainchain_state) -> - let distributed_db = - Distributed_db.create state p2p conn_metadata_cfg.conn_meta_value in + let distributed_db = Distributed_db.create state p2p in Validator.create state distributed_db peer_validator_limits block_validator_limits