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 ;
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

View File

@ -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 ;
}

View File

@ -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 ()

View File

@ -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 ->

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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