Legend:
Library
Module
Module type
Parameter
Class
Class type
This module maintains several pools of points/peers needed by the P2P layer.
Recall a *point id* (or point) is a couple (addr, port). A *peer id* (or peer) is a hash uniquely identifying a peer. An address may host several peers.
Pool functions can trigger two types of events. They can *log* P2p_connection.P2p_event.t (for the upper layer), and they can trigger condition variables defined in P2p_trigger.t, for inter-modules synchronization.
Pool management
type('msg, 'peer, 'conn) t
The type of a pool of connections, parametrized by, resp., the type of messages and the meta-information associated to an identity and a connection.
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.
*)
max_known_points : (int * int) option;
(*
Parameters for the garbage collection of known points. If None, no garbage collection is performed. Otherwise, the first integer of the couple limits the size of the "known points" table. When this number is reached, the table is purged off of disconnected points, older first, to try to reach the amount of connections indicated by the second integer.
*)
max_known_peer_ids : (int * int) option;
(*
Like max_known_points, but for known peer_ids.
*)
peer_greylist_size : int;
(*
The number of peer_ids kept in the peer_id greylist.
If point doesn't belong to the table of known points, register_point ?expected_peer_id t point creates a P2p_point_state.Info.t, triggers a `New_point` event and signals the `new_point` condition. If table capacity is exceeded, a GC of the table is triggered.
If point is already known, the P2p_point_state.Info.t from the table is returned. In either case, the trusted status of the returned P2p_point_state.Info.t is set to trusted. If an expected_peer_id is given, it will be used during the next connection attempt. If the provided peer_id is different the connection will be refused. If we are already connected to a peer, we check whether the current peer_id is the expected one otherwise we disconnect from this point.
register_list_of_new_point ?trusted medium source pool point_list o registers all points of the list as new points. medium and source are for logging purpose. medium should indicate through which medium the points have been acquired ( advertisement, Nack, ..) and source is the id of the peer which sent the list.
If peer doesn't belong to the table of known peers, register_peer t peer creates a P2p_peer.Info.t, triggers a `New_peer` event, and signals a `new_peer` condition. If table capacity is exceeded, a GC of the table is triggered. If peer is already known, the P2p_peer.Info.t from the table is returned.
gc_greylist removes some addresses from the greylist (the oldest have a higher probability to be removed, yet due to the underlying probabilistic structure, recent greylistings can be dropped).
set_expected_peer_id t point peer sets peer as an expected peer_id to point. For any future connection with this point, there is a check that the peer_id announced is the expected one. As a side effect, if there is an active connection with point with another peer_id, we disconnect from this point and the point is greylisted. If the connection is not active but accepted, the point is greylisted.