P2p: store connection_local_metadata in P2p_connection.Info.t

This commit is contained in:
Grégoire Henry 2018-06-05 11:41:23 +02:00
parent e610fcaade
commit 95a56753df
11 changed files with 37 additions and 19 deletions

View File

@ -60,6 +60,7 @@ module Info = struct
remote_socket_port : P2p_addr.port ; remote_socket_port : P2p_addr.port ;
versions : P2p_version.t list ; versions : P2p_version.t list ;
private_node : bool ; private_node : bool ;
local_metadata : 'meta ;
remote_metadata : 'meta ; remote_metadata : 'meta ;
} }
@ -67,25 +68,27 @@ module Info = struct
let open Data_encoding in let open Data_encoding in
conv conv
(fun { incoming ; peer_id ; id_point ; remote_socket_port ; (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, (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, (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 ; { incoming ; peer_id ; id_point ; remote_socket_port ;
versions ; private_node ; remote_metadata }) versions ; private_node ; local_metadata ; remote_metadata })
(obj7 (obj8
(req "incoming" bool) (req "incoming" bool)
(req "peer_id" P2p_peer_id.encoding) (req "peer_id" P2p_peer_id.encoding)
(req "id_point" Id.encoding) (req "id_point" Id.encoding)
(req "remote_socket_port" uint16) (req "remote_socket_port" uint16)
(req "versions" (list P2p_version.encoding)) (req "versions" (list P2p_version.encoding))
(req "private" bool) (req "private" bool)
(req "local_metadata" metadata_encoding)
(req "remote_metadata" metadata_encoding)) (req "remote_metadata" metadata_encoding))
let pp pp_meta ppf let pp pp_meta ppf
{ incoming ; id_point = (remote_addr, remote_port) ; { 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 version = List.hd versions in
let point = match remote_port with let point = match remote_port with
| None -> remote_addr, remote_socket_port | None -> remote_addr, remote_socket_port

View File

@ -38,6 +38,7 @@ module Info : sig
remote_socket_port : P2p_addr.port; remote_socket_port : P2p_addr.port;
versions : P2p_version.t list ; versions : P2p_version.t list ;
private_node : bool ; private_node : bool ;
local_metadata : 'meta ;
remote_metadata : 'meta ; remote_metadata : 'meta ;
} }

View File

@ -206,6 +206,8 @@ module Real = struct
P2p_pool.disconnect ?wait conn P2p_pool.disconnect ?wait conn
let connection_info _net conn = let connection_info _net conn =
P2p_pool.Connection.info conn P2p_pool.Connection.info conn
let connection_local_metadata _net conn =
P2p_pool.Connection.local_metadata conn
let connection_remote_metadata _net conn = let connection_remote_metadata _net conn =
P2p_pool.Connection.remote_metadata conn P2p_pool.Connection.remote_metadata conn
let connection_stat _net conn = let connection_stat _net conn =
@ -313,6 +315,7 @@ module Fake = struct
id_point = (Ipaddr.V6.unspecified, None) ; id_point = (Ipaddr.V6.unspecified, None) ;
remote_socket_port = 0 ; remote_socket_port = 0 ;
versions = [] ; versions = [] ;
local_metadata = faked_metadata ;
remote_metadata = faked_metadata ; remote_metadata = faked_metadata ;
private_node = false ; 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 ; ?wait:bool -> ('msg, 'peer_meta, 'conn_meta) connection -> unit Lwt.t ;
connection_info : connection_info :
('msg, 'peer_meta, 'conn_meta) connection -> 'conn_meta P2p_connection.Info.t ; ('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 : connection_remote_metadata :
('msg, 'peer_meta, 'conn_meta) connection -> 'conn_meta ; ('msg, 'peer_meta, 'conn_meta) connection -> 'conn_meta ;
connection_stat : ('msg, 'peer_meta, 'conn_meta) connection -> P2p_stat.t ; 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 ; find_connection = Real.find_connection net ;
disconnect = Real.disconnect ; disconnect = Real.disconnect ;
connection_info = Real.connection_info net ; connection_info = Real.connection_info net ;
connection_local_metadata = Real.connection_local_metadata net ;
connection_remote_metadata = Real.connection_remote_metadata net ; connection_remote_metadata = Real.connection_remote_metadata net ;
connection_stat = Real.connection_stat net ; connection_stat = Real.connection_stat net ;
global_stat = Real.global_stat net ; global_stat = Real.global_stat net ;
@ -435,6 +441,7 @@ let faked_network peer_cfg faked_metadata = {
find_connection = (fun _ -> None) ; find_connection = (fun _ -> None) ;
disconnect = (fun ?wait:_ _ -> Lwt.return_unit) ; disconnect = (fun ?wait:_ _ -> Lwt.return_unit) ;
connection_info = (fun _ -> Fake.connection_info faked_metadata) ; connection_info = (fun _ -> Fake.connection_info faked_metadata) ;
connection_local_metadata = (fun _ -> faked_metadata) ;
connection_remote_metadata = (fun _ -> faked_metadata) ; connection_remote_metadata = (fun _ -> faked_metadata) ;
connection_stat = (fun _ -> Fake.empty_stat) ; connection_stat = (fun _ -> Fake.empty_stat) ;
global_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 disconnect net = net.disconnect
let find_connection net = net.find_connection let find_connection net = net.find_connection
let connection_info net = net.connection_info 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_remote_metadata net = net.connection_remote_metadata
let connection_stat net = net.connection_stat let connection_stat net = net.connection_stat
let global_stat net = net.global_stat () let global_stat net = net.global_stat ()

View File

@ -187,6 +187,10 @@ val connection_info :
('msg, 'peer_meta, 'conn_meta) net -> ('msg, 'peer_meta, 'conn_meta) net ->
('msg, 'peer_meta, 'conn_meta) connection -> ('msg, 'peer_meta, 'conn_meta) connection ->
'conn_meta P2p_connection.Info.t '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 : val connection_remote_metadata :
('msg, 'peer_meta, 'conn_meta) net -> ('msg, 'peer_meta, 'conn_meta) net ->
('msg, 'peer_meta, 'conn_meta) connection -> ('msg, 'peer_meta, 'conn_meta) connection ->

View File

@ -596,6 +596,9 @@ module Connection = struct
let info { conn } = let info { conn } =
P2p_socket.info conn P2p_socket.info conn
let local_metadata { conn } =
P2p_socket.local_metadata conn
let remote_metadata { conn } = let remote_metadata { conn } =
P2p_socket.remote_metadata conn P2p_socket.remote_metadata conn

View File

@ -219,6 +219,7 @@ val disconnect:
module Connection : sig module Connection : sig
val info: ('msg, 'peer_meta,'conn_meta) connection -> 'conn_meta P2p_connection.Info.t 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 remote_metadata: ('msg, 'peer_meta,'conn_meta) connection -> 'conn_meta
val stat: ('msg, 'peer_meta,'conn_meta) connection -> P2p_stat.t val stat: ('msg, 'peer_meta,'conn_meta) connection -> P2p_stat.t

View File

@ -285,6 +285,7 @@ let authenticate
versions = msg.versions ; incoming ; versions = msg.versions ; incoming ;
id_point ; remote_socket_port ; id_point ; remote_socket_port ;
private_node = metadata_config.private_node remote_metadata ; private_node = metadata_config.private_node remote_metadata ;
local_metadata ;
remote_metadata ; remote_metadata ;
} in } in
return (info, { fd ; info ; cryptobox_data }) 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 pp ppf { conn } = P2p_connection.Info.pp (fun _ _ -> ()) ppf conn.info
let info { conn } = conn.info let info { conn } = conn.info
let local_metadata { conn } = conn.info.local_metadata
let remote_metadata { conn } = conn.info.remote_metadata let remote_metadata { conn } = conn.info.remote_metadata
let private_node { conn } = conn.info.private_node let private_node { conn } = conn.info.private_node

View File

@ -39,6 +39,7 @@ val equal: ('mst, 'meta) t -> ('msg, 'meta) t -> bool
val pp: Format.formatter -> ('msg, 'meta) t -> unit val pp: Format.formatter -> ('msg, 'meta) t -> unit
val info: ('msg, 'meta) t -> 'meta P2p_connection.Info.t 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 remote_metadata: ('msg, 'meta) t -> 'meta
val private_node: ('msg, 'meta) t -> bool val private_node: ('msg, 'meta) t -> bool

View File

@ -305,7 +305,6 @@ type db = {
protocol_db: Raw_protocol.t ; protocol_db: Raw_protocol.t ;
block_input: (Block_hash.t * Block_header.t) Lwt_watcher.input ; block_input: (Block_hash.t * Block_header.t) Lwt_watcher.input ;
operation_input: (Operation_hash.t * Operation.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 = { and chain_db = {
@ -518,7 +517,7 @@ module P2p_reader = struct
let head = Block_header.hash header in let head = Block_header.hash header in
State.Block.known_invalid chain_db.chain_state head >>= fun known_invalid -> State.Block.known_invalid chain_db.chain_state head >>= fun known_invalid ->
let { Connection_metadata.disable_mempool } = 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 = let known_invalid =
known_invalid || known_invalid ||
(disable_mempool && mempool <> Mempool.empty) (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) | Some conn -> ignore (P2p.try_send p2p conn msg : bool)
let create disk p2p connection_metadata_value = let create disk p2p =
let global_request = let global_request =
{ data = () ; { data = () ;
active = active_peer_ids p2p ; active = active_peer_ids p2p ;
@ -723,7 +722,7 @@ let create disk p2p connection_metadata_value =
{ p2p ; p2p_readers ; disk ; { p2p ; p2p_readers ; disk ;
active_chains ; protocol_db ; active_chains ; protocol_db ;
block_input ; operation_input ; block_input ; operation_input ;
connection_metadata_value } in } in
P2p.on_new_connection p2p (P2p_reader.run db) ; P2p.on_new_connection p2p (P2p_reader.run db) ;
P2p.iter_connections p2p (P2p_reader.run db) ; P2p.iter_connections p2p (P2p_reader.run db) ;
db db

View File

@ -18,7 +18,7 @@ module Message = Distributed_db_message
type p2p = (Message.t, Peer_metadata.t, Connection_metadata.t) P2p.net 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 state: db -> State.t
val shutdown: t -> unit Lwt.t val shutdown: t -> unit Lwt.t

View File

@ -84,7 +84,6 @@ let init_connection_metadata opt =
| None -> | None ->
{ disable_mempool = false ; { disable_mempool = false ;
private_node = false } private_node = false }
| Some c -> | Some c ->
{ disable_mempool = c.P2p.disable_mempool ; { disable_mempool = c.P2p.disable_mempool ;
private_node = c.P2p.private_mode } private_node = c.P2p.private_mode }
@ -93,10 +92,8 @@ let init_p2p p2p_params =
match p2p_params with match p2p_params with
| None -> | None ->
let c_meta = init_connection_metadata None in 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 () -> lwt_log_notice "P2P layer is disabled" >>= fun () ->
return return (P2p.faked_network peer_metadata_cfg c_meta)
(P2p.faked_network peer_metadata_cfg c_meta, conn_metadata_cfg)
| Some (config, limits) -> | Some (config, limits) ->
let c_meta = init_connection_metadata (Some config) in let c_meta = init_connection_metadata (Some config) in
let conn_metadata_cfg = connection_metadata_cfg c_meta in let conn_metadata_cfg = connection_metadata_cfg c_meta in
@ -107,7 +104,7 @@ let init_p2p p2p_params =
conn_metadata_cfg conn_metadata_cfg
Distributed_db_message.cfg >>=? fun p2p -> Distributed_db_message.cfg >>=? fun p2p ->
Lwt.async (fun () -> P2p.maintain p2p) ; Lwt.async (fun () -> P2p.maintain p2p) ;
return (p2p, conn_metadata_cfg) return p2p
type config = { type config = {
genesis: State.Chain.genesis ; genesis: State.Chain.genesis ;
@ -159,11 +156,10 @@ let create { genesis ; store_root ; context_root ;
match p2p_params with match p2p_params with
| Some (config, _limits) -> not config.P2p.disable_mempool | Some (config, _limits) -> not config.P2p.disable_mempool
| None -> true in | None -> true in
init_p2p p2p_params >>=? fun (p2p, conn_metadata_cfg) -> init_p2p p2p_params >>=? fun p2p ->
State.read State.read
~store_root ~context_root ?patch_context genesis >>=? fun (state, mainchain_state) -> ~store_root ~context_root ?patch_context genesis >>=? fun (state, mainchain_state) ->
let distributed_db = let distributed_db = Distributed_db.create state p2p in
Distributed_db.create state p2p conn_metadata_cfg.conn_meta_value in
Validator.create state distributed_db Validator.create state distributed_db
peer_validator_limits peer_validator_limits
block_validator_limits block_validator_limits