2016-09-08 21:13:10 +04:00
|
|
|
(**************************************************************************)
|
|
|
|
(* *)
|
2018-02-06 00:17:03 +04:00
|
|
|
(* Copyright (c) 2014 - 2018. *)
|
2016-09-08 21:13:10 +04:00
|
|
|
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
|
|
|
|
(* *)
|
|
|
|
(* All rights reserved. No warranty, explicit or implicit, provided. *)
|
|
|
|
(* *)
|
|
|
|
(**************************************************************************)
|
|
|
|
|
2018-02-20 21:28:05 +04:00
|
|
|
(** Tezos Shell Net - Low level API for the Gossip network
|
|
|
|
|
|
|
|
This is the entry point of the peer-to-peer layer.
|
|
|
|
|
|
|
|
It is used by the Shell as the API to communicate with other
|
|
|
|
nodes.
|
|
|
|
*)
|
2017-11-13 22:06:30 +04:00
|
|
|
|
2017-01-16 21:38:40 +04:00
|
|
|
type 'meta meta_config = {
|
|
|
|
encoding : 'meta Data_encoding.t;
|
|
|
|
initial : 'meta;
|
2017-01-24 17:36:42 +04:00
|
|
|
score : 'meta -> float
|
2017-01-16 21:38:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
type 'msg app_message_encoding = Encoding : {
|
|
|
|
tag: int ;
|
|
|
|
encoding: 'a Data_encoding.t ;
|
|
|
|
wrap: 'a -> 'msg ;
|
|
|
|
unwrap: 'msg -> 'a option ;
|
|
|
|
max_length: int option ;
|
|
|
|
} -> 'msg app_message_encoding
|
|
|
|
|
|
|
|
type 'msg message_config = {
|
|
|
|
encoding : 'msg app_message_encoding list ;
|
2018-01-24 15:48:25 +04:00
|
|
|
versions : P2p_version.t list;
|
2017-01-16 21:38:40 +04:00
|
|
|
}
|
|
|
|
|
2016-09-08 21:13:10 +04:00
|
|
|
(** Network configuration *)
|
|
|
|
type config = {
|
2017-01-14 16:14:17 +04:00
|
|
|
|
2018-01-24 15:48:25 +04:00
|
|
|
listening_port : P2p_addr.port option;
|
2016-09-08 21:13:10 +04:00
|
|
|
(** Tells if incoming connections accepted, precising the TCP port
|
2016-11-07 17:32:10 +04:00
|
|
|
on which the peer can be reached *)
|
2017-01-14 16:14:17 +04:00
|
|
|
|
2018-01-24 15:48:25 +04:00
|
|
|
listening_addr : P2p_addr.t option;
|
2017-01-14 16:14:17 +04:00
|
|
|
(** When incoming connections are accepted, precising on which
|
|
|
|
IP adddress the node listen (default: [[::]]). *)
|
|
|
|
|
2018-01-24 15:48:25 +04:00
|
|
|
trusted_points : P2p_point.Id.t list ;
|
2017-01-14 16:14:17 +04:00
|
|
|
(** List of hard-coded known peers to bootstrap the network from. *)
|
|
|
|
|
2016-09-08 21:13:10 +04:00
|
|
|
peers_file : string ;
|
2017-01-14 16:14:17 +04:00
|
|
|
(** The path to the JSON file where the metadata associated to
|
2017-02-24 06:50:33 +04:00
|
|
|
peer_ids are loaded / stored. *)
|
2017-01-14 16:14:17 +04:00
|
|
|
|
2016-09-08 21:13:10 +04:00
|
|
|
closed_network : bool ;
|
2017-01-14 16:14:17 +04:00
|
|
|
(** If [true], the only accepted connections are from peers whose
|
2018-04-25 16:37:33 +04:00
|
|
|
addresses are in [trusted_points]. *)
|
2017-01-14 16:14:17 +04:00
|
|
|
|
2018-01-24 15:48:25 +04:00
|
|
|
identity : P2p_identity.t ;
|
2017-01-14 16:14:17 +04:00
|
|
|
(** Cryptographic identity of the peer. *)
|
|
|
|
|
|
|
|
proof_of_work_target : Crypto_box.target ;
|
|
|
|
(** Expected level of proof of work of peers' identity. *)
|
|
|
|
|
2016-09-08 21:13:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
(** Network capacities *)
|
|
|
|
type limits = {
|
2017-01-14 16:14:17 +04:00
|
|
|
|
2018-02-20 21:28:05 +04:00
|
|
|
connection_timeout : float ;
|
|
|
|
(** Maximum time allowed to the establishment of a connection. *)
|
|
|
|
|
|
|
|
authentication_timeout : float ;
|
2017-01-14 16:14:17 +04:00
|
|
|
(** Delay granted to a peer to perform authentication, in seconds. *)
|
|
|
|
|
2018-04-25 16:37:33 +04:00
|
|
|
greylist_timeout : int ;
|
2018-02-22 18:35:50 +04:00
|
|
|
(** GC delay for the grelists tables, in seconds. *)
|
|
|
|
|
2016-09-08 21:13:10 +04:00
|
|
|
min_connections : int ;
|
2017-01-14 16:14:17 +04:00
|
|
|
(** Strict minimum number of connections (triggers an urgent maintenance) *)
|
|
|
|
|
|
|
|
expected_connections : int ;
|
2017-01-30 22:10:16 +04:00
|
|
|
(** Targeted number of connections to reach when bootstraping / maintaining *)
|
2017-01-14 16:14:17 +04:00
|
|
|
|
2016-09-08 21:13:10 +04:00
|
|
|
max_connections : int ;
|
2017-01-14 16:14:17 +04:00
|
|
|
(** Maximum number of connections (exceeding peers are disconnected) *)
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2017-01-14 16:14:17 +04:00
|
|
|
backlog : int ;
|
|
|
|
(** Argument of [Lwt_unix.accept].*)
|
|
|
|
|
|
|
|
max_incoming_connections : int ;
|
2017-01-30 22:10:16 +04:00
|
|
|
(** Maximum not-yet-authenticated incoming connections. *)
|
2016-11-15 04:33:12 +04:00
|
|
|
|
2017-01-14 16:14:17 +04:00
|
|
|
max_download_speed : int option ;
|
|
|
|
(** Hard-limit in the number of bytes received per second. *)
|
2016-11-07 17:32:10 +04:00
|
|
|
|
2017-01-14 16:14:17 +04:00
|
|
|
max_upload_speed : int option ;
|
|
|
|
(** Hard-limit in the number of bytes sent per second. *)
|
2016-11-07 17:32:10 +04:00
|
|
|
|
2017-01-14 16:14:17 +04:00
|
|
|
read_buffer_size : int ;
|
|
|
|
(** Size in bytes of the buffer passed to [Lwt_unix.read]. *)
|
2016-11-07 17:32:10 +04:00
|
|
|
|
2017-01-14 16:14:17 +04:00
|
|
|
read_queue_size : int option ;
|
|
|
|
write_queue_size : int option ;
|
|
|
|
incoming_app_message_queue_size : int option ;
|
|
|
|
incoming_message_queue_size : int option ;
|
|
|
|
outgoing_message_queue_size : int option ;
|
|
|
|
(** Various bounds for internal queues. *)
|
2016-11-15 04:52:39 +04:00
|
|
|
|
2017-02-24 06:50:33 +04:00
|
|
|
known_peer_ids_history_size : int ;
|
2017-01-24 17:36:42 +04:00
|
|
|
known_points_history_size : int ;
|
|
|
|
(** Size of circular log buffers, in number of events recorded. *)
|
|
|
|
|
2017-02-24 06:50:33 +04:00
|
|
|
max_known_peer_ids : (int * int) option ;
|
2017-01-24 17:36:42 +04:00
|
|
|
max_known_points : (int * int) option ;
|
|
|
|
(** Optional limitation of internal hashtables (max, target) *)
|
2017-03-14 13:51:44 +04:00
|
|
|
|
|
|
|
swap_linger : float ;
|
|
|
|
(** Peer swapping does not occur more than once during a timespan of
|
|
|
|
[swap_linger] seconds. *)
|
|
|
|
|
2017-04-18 20:32:31 +04:00
|
|
|
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. *)
|
|
|
|
|
2017-01-14 16:14:17 +04:00
|
|
|
}
|
2016-11-15 04:52:39 +04:00
|
|
|
|
2018-02-20 21:28:05 +04:00
|
|
|
(** Type of a P2P layer instance, parametrized by:
|
|
|
|
['msg]: type of messages exchanged between peers
|
|
|
|
['meta]: type of the metadata associated with peers (score, etc.)
|
|
|
|
*)
|
2017-01-16 21:38:40 +04:00
|
|
|
type ('msg, 'meta) t
|
|
|
|
type ('msg, 'meta) net = ('msg, 'meta) t
|
2016-11-07 17:32:10 +04:00
|
|
|
|
2017-01-16 21:38:40 +04:00
|
|
|
(** A faked p2p layer, which do not initiate any connection
|
|
|
|
nor open any listening socket *)
|
2017-03-14 13:51:44 +04:00
|
|
|
val faked_network : 'meta meta_config -> ('msg, 'meta) net
|
2016-11-15 04:52:39 +04:00
|
|
|
|
2017-01-16 21:38:40 +04:00
|
|
|
(** Main network initialisation function *)
|
2017-01-23 14:10:02 +04:00
|
|
|
val create :
|
2017-01-16 21:38:40 +04:00
|
|
|
config:config -> limits:limits ->
|
2017-04-18 20:13:46 +04:00
|
|
|
'meta meta_config -> 'msg message_config -> ('msg, 'meta) net tzresult Lwt.t
|
2016-11-07 17:32:10 +04:00
|
|
|
|
2017-02-24 06:50:33 +04:00
|
|
|
(** Return one's peer_id *)
|
2018-01-24 15:48:25 +04:00
|
|
|
val peer_id : ('msg, 'meta) net -> P2p_peer.Id.t
|
2016-11-15 04:52:39 +04:00
|
|
|
|
2017-01-16 21:38:40 +04:00
|
|
|
(** A maintenance operation : try and reach the ideal number of peers *)
|
|
|
|
val maintain : ('msg, 'meta) net -> unit Lwt.t
|
2016-11-07 17:32:10 +04:00
|
|
|
|
2017-01-16 21:38:40 +04:00
|
|
|
(** Voluntarily drop some peers and replace them by new buddies *)
|
|
|
|
val roll : ('msg, 'meta) net -> unit Lwt.t
|
2016-11-07 17:32:10 +04:00
|
|
|
|
2017-01-16 21:38:40 +04:00
|
|
|
(** Close all connections properly *)
|
|
|
|
val shutdown : ('msg, 'meta) net -> unit Lwt.t
|
2016-11-07 17:32:10 +04:00
|
|
|
|
2017-01-16 21:38:40 +04:00
|
|
|
(** A connection to a peer *)
|
|
|
|
type ('msg, 'meta) connection
|
2016-11-29 02:01:37 +04:00
|
|
|
|
2017-01-16 21:38:40 +04:00
|
|
|
(** Access the domain of active peers *)
|
|
|
|
val connections : ('msg, 'meta) net -> ('msg, 'meta) connection list
|
2016-11-07 17:32:10 +04:00
|
|
|
|
2017-02-24 06:50:33 +04:00
|
|
|
(** Return the active peer with identity [peer_id] *)
|
2018-01-24 15:48:25 +04:00
|
|
|
val find_connection : ('msg, 'meta) net -> P2p_peer.Id.t -> ('msg, 'meta) connection option
|
2016-11-07 17:32:10 +04:00
|
|
|
|
2017-01-16 21:38:40 +04:00
|
|
|
(** Access the info of an active peer, if available *)
|
|
|
|
val connection_info :
|
2018-01-24 15:48:25 +04:00
|
|
|
('msg, 'meta) net -> ('msg, 'meta) connection -> P2p_connection.Info.t
|
2017-01-16 21:38:40 +04:00
|
|
|
val connection_stat :
|
2018-01-24 15:48:25 +04:00
|
|
|
('msg, 'meta) net -> ('msg, 'meta) connection -> P2p_stat.t
|
2017-09-29 20:43:13 +04:00
|
|
|
|
|
|
|
(** Cleanly closes a connection. *)
|
|
|
|
val disconnect :
|
|
|
|
('msg, 'meta) net -> ?wait:bool -> ('msg, 'meta) connection -> unit Lwt.t
|
|
|
|
|
2018-01-24 15:48:25 +04:00
|
|
|
val global_stat : ('msg, 'meta) net -> P2p_stat.t
|
2016-11-07 17:32:10 +04:00
|
|
|
|
2017-01-16 21:38:40 +04:00
|
|
|
(** Accessors for meta information about a global identifier *)
|
2018-01-24 15:48:25 +04:00
|
|
|
val get_metadata : ('msg, 'meta) net -> P2p_peer.Id.t -> 'meta
|
|
|
|
val set_metadata : ('msg, 'meta) net -> P2p_peer.Id.t -> 'meta -> unit
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2017-01-16 21:38:40 +04:00
|
|
|
(** Wait for a message from a given connection. *)
|
|
|
|
val recv :
|
|
|
|
('msg, 'meta) net -> ('msg, 'meta) connection -> 'msg tzresult Lwt.t
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2017-01-16 21:38:40 +04:00
|
|
|
(** Wait for a message from any active connections. *)
|
|
|
|
val recv_any :
|
|
|
|
('msg, 'meta) net -> (('msg, 'meta) connection * 'msg) Lwt.t
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2017-01-16 21:38:40 +04:00
|
|
|
(** [send net peer msg] is a thread that returns when [msg] has been
|
|
|
|
successfully enqueued in the send queue. *)
|
|
|
|
val send :
|
2017-02-24 20:17:53 +04:00
|
|
|
('msg, 'meta) net -> ('msg, 'meta) connection -> 'msg -> unit tzresult Lwt.t
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2017-01-16 21:38:40 +04:00
|
|
|
(** [try_send net peer msg] is [true] if [msg] has been added to the
|
|
|
|
send queue for [peer], [false] otherwise *)
|
|
|
|
val try_send :
|
|
|
|
('msg, 'meta) net -> ('msg, 'meta) connection -> 'msg -> bool
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2017-01-16 21:38:40 +04:00
|
|
|
(** Send a message to all peers *)
|
|
|
|
val broadcast : ('msg, 'meta) net -> 'msg -> unit
|
2016-11-15 04:52:39 +04:00
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
val fold_connections :
|
|
|
|
('msg, 'meta) net ->
|
2018-01-24 15:48:25 +04:00
|
|
|
init:'a -> f:(P2p_peer.Id.t -> ('msg, 'meta) connection -> 'a -> 'a) -> 'a
|
2017-02-24 20:17:53 +04:00
|
|
|
|
|
|
|
val iter_connections :
|
|
|
|
('msg, 'meta) net ->
|
2018-01-24 15:48:25 +04:00
|
|
|
(P2p_peer.Id.t -> ('msg, 'meta) connection -> unit) -> unit
|
2017-02-24 20:17:53 +04:00
|
|
|
|
|
|
|
val on_new_connection :
|
|
|
|
('msg, 'meta) net ->
|
2018-01-24 15:48:25 +04:00
|
|
|
(P2p_peer.Id.t -> ('msg, 'meta) connection -> unit) -> unit
|
2017-02-24 20:17:53 +04:00
|
|
|
|
2018-02-12 00:10:25 +04:00
|
|
|
val build_rpc_directory : _ t -> unit RPC_directory.t
|
|
|
|
|
2018-03-11 18:02:59 +04:00
|
|
|
val greylist_addr : ('msg, 'meta) net -> P2p_addr.t -> unit
|
|
|
|
val greylist_peer : ('msg, 'meta) net -> P2p_peer.Id.t -> unit
|
2018-02-22 18:35:50 +04:00
|
|
|
|
2017-01-16 21:38:40 +04:00
|
|
|
(**/**)
|
2017-11-27 09:13:12 +04:00
|
|
|
|
2017-01-16 21:38:40 +04:00
|
|
|
module Raw : sig
|
|
|
|
type 'a t =
|
|
|
|
| Bootstrap
|
2018-01-24 15:48:25 +04:00
|
|
|
| Advertise of P2p_point.Id.t list
|
|
|
|
| Swap_request of P2p_point.Id.t * P2p_peer.Id.t
|
|
|
|
| Swap_ack of P2p_point.Id.t * P2p_peer.Id.t
|
2017-01-16 21:38:40 +04:00
|
|
|
| Message of 'a
|
|
|
|
| Disconnect
|
|
|
|
val encoding: 'msg app_message_encoding list -> 'msg t Data_encoding.t
|
2016-11-07 17:32:10 +04:00
|
|
|
end
|
2017-01-16 21:38:40 +04:00
|
|
|
|