package tezos-p2p

  1. Overview
  2. Docs

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.

type config = {
  1. listening_port : Tezos_base.P2p_addr.port option;
    (*

    Tells if incoming connections accepted, specifying the TCP port on which the peer can be reached (default: 9732)

    *)
  2. listening_addr : Tezos_base.P2p_addr.t option;
    (*

    When incoming connections are accepted, precise on which IP address the node listen (default: [::]).

    *)
  3. advertised_port : Tezos_base.P2p_addr.port option;
    (*

    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.

    *)
  4. discovery_port : Tezos_base.P2p_addr.port option;
    (*

    Tells if local peer discovery is enabled, specifying the TCP port on which the peer can be reached (default: 10732)

    *)
  5. discovery_addr : Ipaddr.V4.t option;
    (*

    When local peer discovery is enabled, precise on which IP address messages are broadcast (default: 255.255.255.255).

    *)
  6. trusted_points : (Tezos_base.P2p_point.Id.t * Tezos_base.P2p_peer.Id.t option) list;
    (*

    List of hard-coded known peers to bootstrap the network from.

    *)
  7. peers_file : string;
    (*

    The path to the JSON file where the metadata associated to peer_ids are loaded / stored.

    *)
  8. 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.

    *)
  9. identity : Tezos_base.P2p_identity.t;
    (*

    Cryptographic identity of the peer.

    *)
  10. proof_of_work_target : Tezos_crypto.Crypto_box.pow_target;
    (*

    Expected level of proof of work of peers' identity.

    *)
  11. trust_discovered_peers : bool;
    (*

    If true, peers discovered on the local network will be trusted.

    *)
  12. reconnection_config : P2p_point_state.Info.reconnection_config;
    (*

    The reconnection delat configuration.

    *)
}

Network configuration

type limits = {
  1. connection_timeout : Tezos_base.Time.System.Span.t;
    (*

    Maximum time allowed to the establishment of a connection.

    *)
  2. authentication_timeout : Tezos_base.Time.System.Span.t;
    (*

    Delay granted to a peer to perform authentication.

    *)
  3. greylist_timeout : Tezos_base.Time.System.Span.t;
    (*

    GC delay for the greylists tables.

    *)
  4. maintenance_idle_time : Tezos_base.Time.System.Span.t;
    (*

    How long to wait at most before running a maintenance loop.

    *)
  5. min_connections : int;
    (*

    Strict minimum number of connections (triggers an urgent maintenance)

    *)
  6. expected_connections : int;
    (*

    Targeted number of connections to reach when bootstrapping / maintaining

    *)
  7. max_connections : int;
    (*

    Maximum number of connections (exceeding peers are disconnected)

    *)
  8. backlog : int;
    (*

    Argument of Lwt_unix.accept.

    *)
  9. max_incoming_connections : int;
    (*

    Maximum not-yet-authenticated incoming connections.

    *)
  10. max_download_speed : int option;
    (*

    Hard-limit in the number of bytes received per second.

    *)
  11. max_upload_speed : int option;
    (*

    Hard-limit in the number of bytes sent per second.

    *)
  12. read_buffer_size : int;
    (*

    Size in bytes of the buffer passed to Lwt_unix.read.

    *)
  13. read_queue_size : int option;
  14. write_queue_size : int option;
  15. incoming_app_message_queue_size : int option;
  16. incoming_message_queue_size : int option;
  17. outgoing_message_queue_size : int option;
    (*

    Various bounds for internal queues.

    *)
  18. max_known_peer_ids : (int * int) option;
  19. max_known_points : (int * int) option;
    (*

    Optional limitation of internal hashtables (max, target)

    *)
  20. peer_greylist_size : int;
    (*

    The number of peer_ids kept in the peer_id greylist.

    *)
  21. ip_greylist_size_in_kilobytes : int;
    (*

    The size of the IP address greylist in kilobytes.

    *)
  22. ip_greylist_cleanup_delay : Tezos_base.Time.System.Span.t;
    (*

    The time an IP address is kept in the greylist.

    *)
  23. swap_linger : Tezos_base.Time.System.Span.t;
    (*

    Peer swapping does not occur more than once during a timespan of swap_linger.

    *)
  24. binary_chunks_size : int option;
    (*

    Size (in bytes) of binary blocks that are sent to other peers. Default value is 64 kB. Max value is 64kB.

    *)
}

Network capacities

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
val announced_version : ('msg, 'peer_meta, 'conn_meta) net -> Tezos_base.Network_version.t
val pool : ('msg, 'peer_meta, 'conn_meta) net -> ('msg, 'peer_meta, 'conn_meta) P2p_pool.t option
val connect_handler : ('msg, 'peer_meta, 'conn_meta) net -> ('msg, 'peer_meta, 'conn_meta) P2p_connect_handler.t option
val faked_network : 'msg P2p_params.message_config -> 'peer_meta P2p_params.peer_meta_config -> 'conn_meta -> ('msg, 'peer_meta, 'conn_meta) net

A faked p2p layer, which do not initiate any connection nor open any listening socket

val create : config:config -> limits:limits -> 'peer_meta P2p_params.peer_meta_config -> 'conn_meta P2p_params.conn_meta_config -> 'msg P2p_params.message_config -> (('msg, 'peer_meta, 'conn_meta) net, Tezos_error_monad.TzCore.error list) Stdlib.result Lwt.t

Main network initialization function

val activate : ('msg, 'peer_meta, 'conn_meta) net -> unit
val peer_id : ('msg, 'peer_meta, 'conn_meta) net -> Tezos_base.P2p_peer.Id.t

Return one's peer_id

val maintain : ('msg, 'peer_meta, 'conn_meta) net -> unit Lwt.t

A maintenance operation : try and reach the ideal number of peers

val roll : ('msg, 'peer_meta, 'conn_meta) net -> unit Lwt.t

Voluntarily drop some peers and replace them by new buddies

val shutdown : ('msg, 'peer_meta, 'conn_meta) net -> unit Lwt.t

Close all connections properly

type ('msg, 'peer_meta, 'conn_meta) connection

A connection to a peer

val connections : ('msg, 'peer_meta, 'conn_meta) net -> ('msg, 'peer_meta, 'conn_meta) connection list

Access the domain of active peers

val find_connection_by_peer_id : ('msg, 'peer_meta, 'conn_meta) net -> Tezos_base.P2p_peer.Id.t -> ('msg, 'peer_meta, 'conn_meta) connection option

Return the active peer with identity peer_id

val find_connection_by_point : ('msg, 'peer_meta, 'conn_meta) net -> Tezos_base.P2p_point.Id.t -> ('msg, 'peer_meta, 'conn_meta) connection option

Return the active peer corresponding to point

val connection_info : ('msg, 'peer_meta, 'conn_meta) net -> ('msg, 'peer_meta, 'conn_meta) connection -> 'conn_meta Tezos_base.P2p_connection.Info.t

Access the info of an active peer, if available

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 -> 'conn_meta
val connection_stat : ('msg, 'peer_meta, 'conn_meta) net -> ('msg, 'peer_meta, 'conn_meta) connection -> Tezos_base.P2p_stat.t
val negotiated_version : ('msg, 'peer_meta, 'conn_meta) net -> ('msg, 'peer_meta, 'conn_meta) connection -> Tezos_base.Network_version.t

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.

val disconnect : ('msg, 'peer_meta, 'conn_meta) net -> ?wait:bool -> ('msg, 'peer_meta, 'conn_meta) connection -> unit Lwt.t

Cleanly closes a connection.

val global_stat : ('msg, 'peer_meta, 'conn_meta) net -> Tezos_base.P2p_stat.t
val get_peer_metadata : ('msg, 'peer_meta, 'conn_meta) net -> Tezos_base.P2p_peer.Id.t -> 'peer_meta

Accessors for meta information about a global identifier

val set_peer_metadata : ('msg, 'peer_meta, 'conn_meta) net -> Tezos_base.P2p_peer.Id.t -> 'peer_meta -> unit
val connect : ('msg, 'peer_meta, 'conn_meta) net -> ?timeout:Ptime.span -> Tezos_base.P2p_point.Id.t -> (('msg, 'peer_meta, 'conn_meta) connection, Tezos_error_monad.TzCore.error list) Stdlib.result Lwt.t

connect net ?timeout point attempts to establish a connection to point within an optional duration timeout.

val recv : ('msg, 'peer_meta, 'conn_meta) net -> ('msg, 'peer_meta, 'conn_meta) connection -> ('msg, Tezos_error_monad.TzCore.error list) Stdlib.result Lwt.t

Wait for a message from a given connection.

val recv_any : ('msg, 'peer_meta, 'conn_meta) net -> (('msg, 'peer_meta, 'conn_meta) connection * 'msg) Lwt.t

Wait for a message from any active connections.

val send : ('msg, 'peer_meta, 'conn_meta) net -> ('msg, 'peer_meta, 'conn_meta) connection -> 'msg -> (unit, Tezos_error_monad.TzCore.error list) Stdlib.result Lwt.t

send net peer msg is a thread that returns when msg has been successfully enqueued in the send queue.

val try_send : ('msg, 'peer_meta, 'conn_meta) net -> ('msg, 'peer_meta, 'conn_meta) connection -> 'msg -> bool

try_send net peer msg is true if msg has been added to the send queue for peer, false otherwise

val broadcast : ('msg, 'peer_meta, 'conn_meta) net -> 'msg -> unit

Send a message to all peers

val fold_connections : ('msg, 'peer_meta, 'conn_meta) net -> init:'a -> f: (Tezos_base.P2p_peer.Id.t -> ('msg, 'peer_meta, 'conn_meta) connection -> 'a -> 'a) -> 'a
val iter_connections : ('msg, 'peer_meta, 'conn_meta) net -> (Tezos_base.P2p_peer.Id.t -> ('msg, 'peer_meta, 'conn_meta) connection -> unit) -> unit
val on_new_connection : ('msg, 'peer_meta, 'conn_meta) net -> (Tezos_base.P2p_peer.Id.t -> ('msg, 'peer_meta, 'conn_meta) connection -> unit) -> unit
val greylist_addr : ('msg, 'peer_meta, 'conn_meta) net -> Tezos_base.P2p_addr.t -> unit
val greylist_peer : ('msg, 'peer_meta, 'conn_meta) net -> Tezos_base.P2p_peer.Id.t -> unit
val watcher : ('msg, 'peer_meta, 'conn_meta) net -> Tezos_base.P2p_connection.P2p_event.t Lwt_stream.t * Lwt_watcher.stopper
OCaml

Innovation. Community. Security.