Legend:
Library
Module
Module type
Parameter
Class
Class type
Tezos P2p layer - Dynamic overlay network of authenticated peers.
The P2P layer implements several mechanisms, notably:
It maintains pools of known points (P2P servers), peers (authenticated P2P servers), connections,
it implements an "administrative" protocol for maintaining the network topology,
it regulates bandwidth usage between connections,
it implements an authentication / session agreement protocol,
it can ban or greylist peers or IP addresses who don't behave well,
it offers the ability to the upper-layer to send, broadcast, or receive messages.
The protocol sends/receives messages to maintain the network topology, and also "generic" application messages that can be sent and received by the upper-layer. See P2p_message.
The protocol may operate in *private* mode, in which only user-provided points (a.k.a. *trusted* ) are used. In particular, points advertisements and swap requests messages are ignored.
The module P2p_pool maintains pools of points, peers and connections.
Several workers are used:
P2p_maintenance tries to regulate the number of connections
P2p_welcome waits for incoming connections
P2p_discovery looks for points on the local network via UDP messages
A protocol worker implements the messaging protocol
Points can be trusted. This is relevant in private mode (see above), but generally peers shouldn't advertise trusted points.
Addresses and peers can be *banned* (a.k.a. blacklisted). In which case, connections to and from them should be ignored.
Addresses or peers can be *greylisted*. As for banning, greylisting can be enforced via the API, but also dynamically when the peer isn't able to authenticate. Eventually greylisted peers are whitelisted again.
Many types used in the P2p layer are parameterized by three type parameters:
'msg: type of messages exchanged between peers
'peer_meta: type of the metadata associated with peers (score, etc.)
'conn_meta: type of the metadata associated with connections
The concrete types, and functions operating on them, are defined by the calling layer, and passed to P2p.create. See module P2p_params.
If incoming connections accepted, specifying the TCP port other peers should use to connect to this peer (default: listening_port). Can be used when this peer is behind NAT.
List of hard-coded known peers to bootstrap the network from.
*)
peers_file : string;
(*
The path to the JSON file where the metadata associated to peer_ids are loaded / stored.
*)
private_mode : bool;
(*
If true, only open outgoing/accept incoming connections to/from peers whose addresses are in trusted_peers, and inform these peers that the identity of this node should not be revealed to the rest of the network.
If set to true, the p2p layer will not participate to the peer discovery mechanism. The p2p layer will not be able to find new peers to connect with. For more details, refers to field disable_peer_discovery of P2p_connect_handler.config.
*)
}
Network configuration
type('msg, 'peer_meta, 'conn_meta) t
Type of a P2P layer instance
type('msg, 'peer_meta, 'conn_meta) net = ('msg, 'peer_meta, 'conn_meta)t
received_msg_hook is a function that is called every time a message 'msg is received. sent_msg_hook is a function that is called every time a message 'msg is sent. broadcasted_msg_hook is a function that is called every time a message 'msg is broadcasted.
val activate : ('msg, 'peer_meta, 'conn_meta)net-> unit
Returns the network version that will be used for this connection. This network version is the best version compatible with the versions supported by ours and the remote peer.
connect net ?trusted ?expected_peer_id ?timeout point attempts to establish a connection to point within an optional duration timeout. The optional arguments trusted and expected_peer_id can be used to specify whether the connection should be trusted and to provide the expected remote peer's id, respectively.
broadcast net connections ~except ~(alt:if_conn,then_msg) msg will send messages to all connections that do not satisfy the predicate except. alt can be used to send an alternative message then_msg to connections that do satisfy the if_conn predicate. ?except and ?alt can be used to selectively transfer messages.
Broadcasting is best effort and nothing ensures that messages are actually received (https://gitlab.com/tezos/tezos/-/issues/4205).
on_new_connection p2p f registers a function f that is called everytime we were successfully connected to a peer (after having initialised a secured channel).
This function will not be called if we started to connect to a peer and the connection was rejected during the initialisation of the connection. For example, if the peer is using a different network version.
on_disconnection p2p f registers a function f that is called everytime we disconnect from a peer after we were successfully connected to a peer (this call may happen at any moment during the disconnection). The library ensures that anytime f peer is called, then for any callback registered with on_new_connection, those callbacks would have been called with peer in the first place (if the callbacks were registered at that time).
A direct implication of this is that any callback registered with this function will not be called if we started to connect to a peer and we disconnect from it because the connection was rejected somehow.