2016-09-08 21:13:10 +04:00
|
|
|
(**************************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* Copyright (c) 2014 - 2016. *)
|
|
|
|
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
|
|
|
|
(* *)
|
|
|
|
(* All rights reserved. No warranty, explicit or implicit, provided. *)
|
|
|
|
(* *)
|
|
|
|
(**************************************************************************)
|
|
|
|
|
|
|
|
(** A peer connection address *)
|
2017-01-14 16:14:17 +04:00
|
|
|
type addr = Ipaddr.V6.t
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
(** A peer connection port *)
|
|
|
|
type port = int
|
|
|
|
|
2016-11-07 17:32:10 +04:00
|
|
|
(** A p2p protocol version *)
|
2017-01-14 16:14:17 +04:00
|
|
|
module Version = P2p_types.Version
|
|
|
|
|
|
|
|
(** A global identifier for a peer, a.k.a. an identity *)
|
|
|
|
module Gid = P2p_types.Gid
|
|
|
|
|
|
|
|
module Identity = P2p_types.Identity
|
|
|
|
|
|
|
|
module Point = P2p_types.Point
|
|
|
|
|
|
|
|
module Id_point = P2p_types.Id_point
|
|
|
|
|
|
|
|
module Connection_info = P2p_types.Connection_info
|
|
|
|
|
|
|
|
module Stat = P2p_types.Stat
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
(** Network configuration *)
|
|
|
|
type config = {
|
2017-01-14 16:14:17 +04:00
|
|
|
|
|
|
|
listening_port : 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
|
|
|
|
|
|
|
listening_addr : addr option;
|
|
|
|
(** When incoming connections are accepted, precising on which
|
|
|
|
IP adddress the node listen (default: [[::]]). *)
|
|
|
|
|
|
|
|
trusted_points : Point.t list ;
|
|
|
|
(** 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
|
|
|
|
gids are loaded / stored. *)
|
|
|
|
|
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
|
|
|
|
addresses are in [trusted_peers]. *)
|
|
|
|
|
|
|
|
identity : Identity.t ;
|
|
|
|
(** 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
|
|
|
|
|
|
|
authentification_timeout : float ;
|
|
|
|
(** Delay granted to a peer to perform authentication, 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 ;
|
|
|
|
(** Targeted number of connections to reach when bootstraping / maitening *)
|
|
|
|
|
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 ;
|
|
|
|
(** Maximum not-yet-authentified 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-01-14 16:14:17 +04:00
|
|
|
}
|
2016-11-15 04:52:39 +04:00
|
|
|
|
2016-11-07 17:32:10 +04:00
|
|
|
|
2017-01-14 16:14:17 +04:00
|
|
|
(** Type of message used by higher layers *)
|
|
|
|
module type MESSAGE = sig
|
|
|
|
type t
|
|
|
|
val encoding : t P2p_connection_pool.encoding list
|
2016-11-07 17:32:10 +04:00
|
|
|
(** High level protocol(s) talked by the peer. When two peers
|
|
|
|
initiate a connection, they exchange their list of supported
|
|
|
|
versions. The chosen one, if any, is the maximum common one (in
|
|
|
|
lexicographic order) *)
|
2017-01-14 16:14:17 +04:00
|
|
|
val supported_versions : Version.t list
|
|
|
|
end
|
2016-11-15 04:52:39 +04:00
|
|
|
|
2017-01-14 16:14:17 +04:00
|
|
|
(** Type of metadata associated to an identity *)
|
|
|
|
module type METADATA = sig
|
|
|
|
type t
|
|
|
|
val initial : t
|
|
|
|
val encoding : t Data_encoding.t
|
|
|
|
val score : t -> float
|
2016-11-07 17:32:10 +04:00
|
|
|
end
|
|
|
|
|
2017-01-14 16:14:17 +04:00
|
|
|
module Make (Message : MESSAGE) (Metadata : METADATA) : sig
|
2016-11-15 04:52:39 +04:00
|
|
|
|
2016-11-07 17:32:10 +04:00
|
|
|
type net
|
|
|
|
|
|
|
|
(** A faked p2p layer, which do not initiate any connection
|
2016-11-15 04:52:39 +04:00
|
|
|
nor open any listening socket *)
|
2016-11-07 17:32:10 +04:00
|
|
|
val faked_network : net
|
|
|
|
|
|
|
|
(** Main network initialisation function *)
|
|
|
|
val bootstrap : config:config -> limits:limits -> net Lwt.t
|
|
|
|
|
2016-11-29 02:01:37 +04:00
|
|
|
(** Return one's gid *)
|
2017-01-14 16:14:17 +04:00
|
|
|
val gid : net -> Gid.t
|
2016-11-29 02:01:37 +04:00
|
|
|
|
2016-11-07 17:32:10 +04:00
|
|
|
(** A maintenance operation : try and reach the ideal number of peers *)
|
|
|
|
val maintain : net -> unit Lwt.t
|
|
|
|
|
|
|
|
(** Voluntarily drop some peers and replace them by new buddies *)
|
|
|
|
val roll : net -> unit Lwt.t
|
|
|
|
|
|
|
|
(** Close all connections properly *)
|
|
|
|
val shutdown : net -> unit Lwt.t
|
|
|
|
|
|
|
|
(** A connection to a peer *)
|
2017-01-14 16:14:17 +04:00
|
|
|
type connection
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2016-11-07 17:32:10 +04:00
|
|
|
(** Access the domain of active peers *)
|
2017-01-14 16:14:17 +04:00
|
|
|
val connections : net -> connection list
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2016-11-07 17:32:10 +04:00
|
|
|
(** Return the active peer with identity [gid] *)
|
2017-01-14 16:14:17 +04:00
|
|
|
val find_connection : net -> Gid.t -> connection option
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2016-11-07 17:32:10 +04:00
|
|
|
(** Access the info of an active peer, if available *)
|
2017-01-14 16:14:17 +04:00
|
|
|
val connection_info : net -> connection -> Connection_info.t
|
|
|
|
val connection_stat : net -> connection -> Stat.t
|
|
|
|
val global_stat : net -> Stat.t
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2016-11-15 04:52:39 +04:00
|
|
|
(** Accessors for meta information about a global identifier *)
|
2017-01-14 16:14:17 +04:00
|
|
|
val get_metadata : net -> Gid.t -> Metadata.t option
|
|
|
|
val set_metadata : net -> Gid.t -> Metadata.t -> unit
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2016-11-29 01:18:00 +04:00
|
|
|
(** Wait for a message from any peer in the network *)
|
2017-01-14 16:14:17 +04:00
|
|
|
val recv : net -> (connection * Message.t) Lwt.t
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2016-11-29 01:18:00 +04:00
|
|
|
(** [send net peer msg] is a thread that returns when [msg] has been
|
|
|
|
successfully enqueued in the send queue. *)
|
2017-01-14 16:14:17 +04:00
|
|
|
val send : net -> connection -> Message.t -> unit Lwt.t
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2016-11-29 01:18:00 +04:00
|
|
|
(** [try_send net peer msg] is [true] if [msg] has been added to the
|
|
|
|
send queue for [peer], [false] otherwise *)
|
2017-01-14 16:14:17 +04:00
|
|
|
val try_send : net -> connection -> Message.t -> bool
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2016-11-29 01:18:00 +04:00
|
|
|
(** Send a message to all peers *)
|
2017-01-14 16:14:17 +04:00
|
|
|
val broadcast : net -> Message.t -> unit
|
|
|
|
|
|
|
|
(**/**)
|
|
|
|
module Raw : sig
|
|
|
|
type 'a t =
|
|
|
|
| Bootstrap
|
|
|
|
| Advertise of P2p_types.Point.t list
|
|
|
|
| Message of 'a
|
|
|
|
| Disconnect
|
|
|
|
type message = Message.t t
|
|
|
|
val encoding: message Data_encoding.t
|
|
|
|
val supported_versions: P2p_types.Version.t list
|
|
|
|
end
|
2016-11-15 04:52:39 +04:00
|
|
|
|
2016-11-07 17:32:10 +04:00
|
|
|
end
|