2018-01-24 15:48:25 +04:00
|
|
|
(**************************************************************************)
|
|
|
|
(* *)
|
2018-02-06 00:17:03 +04:00
|
|
|
(* Copyright (c) 2014 - 2018. *)
|
2018-01-24 15:48:25 +04:00
|
|
|
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
|
|
|
|
(* *)
|
|
|
|
(* All rights reserved. No warranty, explicit or implicit, provided. *)
|
|
|
|
(* *)
|
|
|
|
(**************************************************************************)
|
|
|
|
|
|
|
|
module Id = struct
|
|
|
|
|
|
|
|
(* A net point (address x port). *)
|
|
|
|
type t = P2p_addr.t * P2p_addr.port option
|
|
|
|
let compare (a1, p1) (a2, p2) =
|
|
|
|
match Ipaddr.V6.compare a1 a2 with
|
|
|
|
| 0 -> Pervasives.compare p1 p2
|
|
|
|
| x -> x
|
|
|
|
let equal p1 p2 = compare p1 p2 = 0
|
|
|
|
let hash = Hashtbl.hash
|
|
|
|
let pp ppf (addr, port) =
|
|
|
|
match port with
|
|
|
|
| None ->
|
|
|
|
Format.fprintf ppf "[%a]:??" Ipaddr.V6.pp_hum addr
|
|
|
|
| Some port ->
|
|
|
|
Format.fprintf ppf "[%a]:%d" Ipaddr.V6.pp_hum addr port
|
|
|
|
let pp_opt ppf = function
|
|
|
|
| None -> Format.pp_print_string ppf "none"
|
|
|
|
| Some point -> pp ppf point
|
|
|
|
let to_string t = Format.asprintf "%a" pp t
|
|
|
|
|
|
|
|
let is_local (addr, _) = Ipaddr.V6.is_private addr
|
|
|
|
let is_global (addr, _) = not @@ Ipaddr.V6.is_private addr
|
|
|
|
|
|
|
|
let of_point (addr, port) = addr, Some port
|
|
|
|
let to_point = function
|
|
|
|
| _, None -> None
|
|
|
|
| addr, Some port -> Some (addr, port)
|
|
|
|
let to_point_exn = function
|
|
|
|
| _, None -> invalid_arg "to_point_exn"
|
|
|
|
| addr, Some port -> addr, port
|
|
|
|
|
|
|
|
let encoding =
|
|
|
|
let open Data_encoding in
|
|
|
|
(obj2
|
|
|
|
(req "addr" P2p_addr.encoding)
|
|
|
|
(opt "port" uint16))
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
module Map = Map.Make (Id)
|
|
|
|
module Set = Set.Make (Id)
|
|
|
|
module Table = Hashtbl.Make (Id)
|
|
|
|
|
|
|
|
module Info = struct
|
|
|
|
|
2018-06-05 03:45:43 +04:00
|
|
|
type 'meta t = {
|
2018-02-08 13:51:00 +04:00
|
|
|
incoming : bool ;
|
|
|
|
peer_id : P2p_peer_id.t ;
|
|
|
|
id_point : Id.t ;
|
|
|
|
remote_socket_port : P2p_addr.port ;
|
2018-01-24 15:48:25 +04:00
|
|
|
versions : P2p_version.t list ;
|
2018-06-05 03:45:43 +04:00
|
|
|
private_node : bool ;
|
2018-06-05 13:41:23 +04:00
|
|
|
local_metadata : 'meta ;
|
2018-06-05 03:45:43 +04:00
|
|
|
remote_metadata : 'meta ;
|
2018-01-24 15:48:25 +04:00
|
|
|
}
|
|
|
|
|
2018-06-05 03:45:43 +04:00
|
|
|
let encoding metadata_encoding =
|
2018-01-24 15:48:25 +04:00
|
|
|
let open Data_encoding in
|
|
|
|
conv
|
2018-06-05 03:45:43 +04:00
|
|
|
(fun { incoming ; peer_id ; id_point ; remote_socket_port ;
|
2018-06-05 13:41:23 +04:00
|
|
|
versions ; private_node ; local_metadata ; remote_metadata } ->
|
2018-06-05 03:45:43 +04:00
|
|
|
(incoming, peer_id, id_point, remote_socket_port,
|
2018-06-05 13:41:23 +04:00
|
|
|
versions, private_node, local_metadata, remote_metadata))
|
2018-06-05 03:45:43 +04:00
|
|
|
(fun (incoming, peer_id, id_point, remote_socket_port,
|
2018-06-05 13:41:23 +04:00
|
|
|
versions, private_node, local_metadata, remote_metadata) ->
|
2018-06-05 03:45:43 +04:00
|
|
|
{ incoming ; peer_id ; id_point ; remote_socket_port ;
|
2018-06-05 13:41:23 +04:00
|
|
|
versions ; private_node ; local_metadata ; remote_metadata })
|
|
|
|
(obj8
|
2018-01-24 15:48:25 +04:00
|
|
|
(req "incoming" bool)
|
2018-02-08 13:51:00 +04:00
|
|
|
(req "peer_id" P2p_peer_id.encoding)
|
2018-01-24 15:48:25 +04:00
|
|
|
(req "id_point" Id.encoding)
|
|
|
|
(req "remote_socket_port" uint16)
|
2018-06-05 03:45:43 +04:00
|
|
|
(req "versions" (list P2p_version.encoding))
|
|
|
|
(req "private" bool)
|
2018-06-05 13:41:23 +04:00
|
|
|
(req "local_metadata" metadata_encoding)
|
2018-06-05 03:45:43 +04:00
|
|
|
(req "remote_metadata" metadata_encoding))
|
2018-01-24 15:48:25 +04:00
|
|
|
|
2018-06-05 03:45:43 +04:00
|
|
|
let pp pp_meta ppf
|
2018-01-24 15:48:25 +04:00
|
|
|
{ incoming ; id_point = (remote_addr, remote_port) ;
|
2018-06-05 13:41:23 +04:00
|
|
|
remote_socket_port ; peer_id ; versions ; private_node ;
|
|
|
|
local_metadata = _ ; remote_metadata } =
|
2018-01-24 15:48:25 +04:00
|
|
|
let version = List.hd versions in
|
|
|
|
let point = match remote_port with
|
|
|
|
| None -> remote_addr, remote_socket_port
|
|
|
|
| Some port -> remote_addr, port in
|
2018-06-05 03:45:43 +04:00
|
|
|
Format.fprintf ppf "%s %a %a (%a)%s%a"
|
2018-01-24 15:48:25 +04:00
|
|
|
(if incoming then "↘" else "↗")
|
2018-02-08 13:51:00 +04:00
|
|
|
P2p_peer_id.pp peer_id
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_point.Id.pp point
|
|
|
|
P2p_version.pp version
|
2018-06-05 03:45:43 +04:00
|
|
|
(if private_node then " private" else "")
|
|
|
|
pp_meta remote_metadata
|
2018-01-24 15:48:25 +04:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
module Pool_event = struct
|
|
|
|
|
|
|
|
(** Pool-level events *)
|
|
|
|
|
|
|
|
type t =
|
|
|
|
|
|
|
|
| Too_few_connections
|
|
|
|
| Too_many_connections
|
|
|
|
|
|
|
|
| New_point of P2p_point.Id.t
|
2018-02-08 13:51:00 +04:00
|
|
|
| New_peer of P2p_peer_id.t
|
2018-01-24 15:48:25 +04:00
|
|
|
|
|
|
|
| Gc_points
|
|
|
|
| Gc_peer_ids
|
|
|
|
|
|
|
|
| Incoming_connection of P2p_point.Id.t
|
|
|
|
| Outgoing_connection of P2p_point.Id.t
|
|
|
|
| Authentication_failed of P2p_point.Id.t
|
2018-02-08 13:51:00 +04:00
|
|
|
| Accepting_request of P2p_point.Id.t * Id.t * P2p_peer_id.t
|
|
|
|
| Rejecting_request of P2p_point.Id.t * Id.t * P2p_peer_id.t
|
|
|
|
| Request_rejected of P2p_point.Id.t * (Id.t * P2p_peer_id.t) option
|
|
|
|
| Connection_established of Id.t * P2p_peer_id.t
|
|
|
|
|
|
|
|
| Swap_request_received of { source : P2p_peer_id.t }
|
|
|
|
| Swap_ack_received of { source : P2p_peer_id.t }
|
|
|
|
| Swap_request_sent of { source : P2p_peer_id.t }
|
|
|
|
| Swap_ack_sent of { source : P2p_peer_id.t }
|
|
|
|
| Swap_request_ignored of { source : P2p_peer_id.t }
|
|
|
|
| Swap_success of { source : P2p_peer_id.t }
|
|
|
|
| Swap_failure of { source : P2p_peer_id.t }
|
|
|
|
|
|
|
|
| Disconnection of P2p_peer_id.t
|
|
|
|
| External_disconnection of P2p_peer_id.t
|
2018-01-24 15:48:25 +04:00
|
|
|
|
|
|
|
let encoding =
|
|
|
|
let open Data_encoding in
|
|
|
|
let branch_encoding name obj =
|
|
|
|
conv (fun x -> (), x) (fun ((), x) -> x)
|
|
|
|
(merge_objs
|
|
|
|
(obj1 (req "event" (constant name))) obj) in
|
|
|
|
union ~tag_size:`Uint8 [
|
2018-03-27 17:15:03 +04:00
|
|
|
case (Tag 0)
|
2018-06-01 01:19:43 +04:00
|
|
|
~title:"Too_few_connections"
|
2018-03-27 17:15:03 +04:00
|
|
|
(branch_encoding "too_few_connections" empty)
|
2018-01-24 15:48:25 +04:00
|
|
|
(function Too_few_connections -> Some () | _ -> None)
|
|
|
|
(fun () -> Too_few_connections) ;
|
2018-03-27 17:15:03 +04:00
|
|
|
case (Tag 1)
|
2018-06-01 01:19:43 +04:00
|
|
|
~title:"Too_many_connections"
|
2018-03-27 17:15:03 +04:00
|
|
|
(branch_encoding "too_many_connections" empty)
|
2018-01-24 15:48:25 +04:00
|
|
|
(function Too_many_connections -> Some () | _ -> None)
|
|
|
|
(fun () -> Too_many_connections) ;
|
2018-03-27 17:15:03 +04:00
|
|
|
case (Tag 2)
|
2018-06-01 01:19:43 +04:00
|
|
|
~title:"New_point"
|
2018-03-27 17:15:03 +04:00
|
|
|
(branch_encoding "new_point"
|
|
|
|
(obj1 (req "point" P2p_point.Id.encoding)))
|
2018-01-24 15:48:25 +04:00
|
|
|
(function New_point p -> Some p | _ -> None)
|
|
|
|
(fun p -> New_point p) ;
|
2018-03-27 17:15:03 +04:00
|
|
|
case (Tag 3)
|
2018-06-01 01:19:43 +04:00
|
|
|
~title:"New_peer"
|
2018-03-27 17:15:03 +04:00
|
|
|
(branch_encoding "new_peer"
|
|
|
|
(obj1 (req "peer_id" P2p_peer_id.encoding)))
|
2018-01-24 15:48:25 +04:00
|
|
|
(function New_peer p -> Some p | _ -> None)
|
|
|
|
(fun p -> New_peer p) ;
|
2018-03-27 17:15:03 +04:00
|
|
|
case (Tag 4)
|
2018-06-01 01:19:43 +04:00
|
|
|
~title:"Incoming_connection"
|
2018-03-27 17:15:03 +04:00
|
|
|
(branch_encoding "incoming_connection"
|
|
|
|
(obj1 (req "point" P2p_point.Id.encoding)))
|
2018-01-24 15:48:25 +04:00
|
|
|
(function Incoming_connection p -> Some p | _ -> None)
|
|
|
|
(fun p -> Incoming_connection p) ;
|
2018-03-27 17:15:03 +04:00
|
|
|
case (Tag 5)
|
2018-06-01 01:19:43 +04:00
|
|
|
~title:"Outgoing_connection"
|
2018-03-27 17:15:03 +04:00
|
|
|
(branch_encoding "outgoing_connection"
|
|
|
|
(obj1 (req "point" P2p_point.Id.encoding)))
|
2018-01-24 15:48:25 +04:00
|
|
|
(function Outgoing_connection p -> Some p | _ -> None)
|
|
|
|
(fun p -> Outgoing_connection p) ;
|
2018-03-27 17:15:03 +04:00
|
|
|
case (Tag 6)
|
2018-06-01 01:19:43 +04:00
|
|
|
~title:"Authentication_failed"
|
2018-03-27 17:15:03 +04:00
|
|
|
(branch_encoding "authentication_failed"
|
|
|
|
(obj1 (req "point" P2p_point.Id.encoding)))
|
2018-01-24 15:48:25 +04:00
|
|
|
(function Authentication_failed p -> Some p | _ -> None)
|
|
|
|
(fun p -> Authentication_failed p) ;
|
2018-03-27 17:15:03 +04:00
|
|
|
case (Tag 7)
|
2018-06-01 01:19:43 +04:00
|
|
|
~title:"Accepting_request"
|
2018-03-27 17:15:03 +04:00
|
|
|
(branch_encoding "accepting_request"
|
|
|
|
(obj3
|
|
|
|
(req "point" P2p_point.Id.encoding)
|
|
|
|
(req "id_point" Id.encoding)
|
|
|
|
(req "peer_id" P2p_peer_id.encoding)))
|
2018-01-24 15:48:25 +04:00
|
|
|
(function Accepting_request (p, id_p, g) ->
|
|
|
|
Some (p, id_p, g) | _ -> None)
|
|
|
|
(fun (p, id_p, g) -> Accepting_request (p, id_p, g)) ;
|
2018-03-27 17:15:03 +04:00
|
|
|
case (Tag 8)
|
2018-06-01 01:19:43 +04:00
|
|
|
~title:"Rejecting_request"
|
2018-03-27 17:15:03 +04:00
|
|
|
(branch_encoding "rejecting_request"
|
|
|
|
(obj3
|
|
|
|
(req "point" P2p_point.Id.encoding)
|
|
|
|
(req "id_point" Id.encoding)
|
|
|
|
(req "peer_id" P2p_peer_id.encoding)))
|
2018-01-24 15:48:25 +04:00
|
|
|
(function Rejecting_request (p, id_p, g) ->
|
|
|
|
Some (p, id_p, g) | _ -> None)
|
|
|
|
(fun (p, id_p, g) -> Rejecting_request (p, id_p, g)) ;
|
2018-03-27 17:15:03 +04:00
|
|
|
case (Tag 9)
|
2018-06-01 01:19:43 +04:00
|
|
|
~title:"Request_rejected"
|
2018-03-27 17:15:03 +04:00
|
|
|
(branch_encoding "request_rejected"
|
|
|
|
(obj2
|
|
|
|
(req "point" P2p_point.Id.encoding)
|
|
|
|
(opt "identity"
|
|
|
|
(tup2 Id.encoding P2p_peer_id.encoding))))
|
2018-01-24 15:48:25 +04:00
|
|
|
(function Request_rejected (p, id) -> Some (p, id) | _ -> None)
|
|
|
|
(fun (p, id) -> Request_rejected (p, id)) ;
|
2018-03-27 17:15:03 +04:00
|
|
|
case (Tag 10)
|
2018-06-01 01:19:43 +04:00
|
|
|
~title:"Connection_established"
|
2018-03-27 17:15:03 +04:00
|
|
|
(branch_encoding "connection_established"
|
|
|
|
(obj2
|
|
|
|
(req "id_point" Id.encoding)
|
|
|
|
(req "peer_id" P2p_peer_id.encoding)))
|
2018-01-24 15:48:25 +04:00
|
|
|
(function Connection_established (id_p, g) ->
|
|
|
|
Some (id_p, g) | _ -> None)
|
|
|
|
(fun (id_p, g) -> Connection_established (id_p, g)) ;
|
2018-03-27 17:15:03 +04:00
|
|
|
case (Tag 11)
|
2018-06-01 01:19:43 +04:00
|
|
|
~title:"Disconnection"
|
2018-03-27 17:15:03 +04:00
|
|
|
(branch_encoding "disconnection"
|
|
|
|
(obj1 (req "peer_id" P2p_peer_id.encoding)))
|
2018-01-24 15:48:25 +04:00
|
|
|
(function Disconnection g -> Some g | _ -> None)
|
|
|
|
(fun g -> Disconnection g) ;
|
2018-03-27 17:15:03 +04:00
|
|
|
case (Tag 12)
|
2018-06-01 01:19:43 +04:00
|
|
|
~title:"External_disconnection"
|
2018-03-27 17:15:03 +04:00
|
|
|
(branch_encoding "external_disconnection"
|
|
|
|
(obj1 (req "peer_id" P2p_peer_id.encoding)))
|
2018-01-24 15:48:25 +04:00
|
|
|
(function External_disconnection g -> Some g | _ -> None)
|
|
|
|
(fun g -> External_disconnection g) ;
|
2018-03-27 17:15:03 +04:00
|
|
|
case (Tag 13)
|
2018-06-01 01:19:43 +04:00
|
|
|
~title:"Gc_points"
|
2018-03-27 17:15:03 +04:00
|
|
|
(branch_encoding "gc_points" empty)
|
2018-01-24 15:48:25 +04:00
|
|
|
(function Gc_points -> Some () | _ -> None)
|
|
|
|
(fun () -> Gc_points) ;
|
2018-03-27 17:15:03 +04:00
|
|
|
case (Tag 14)
|
2018-06-01 01:19:43 +04:00
|
|
|
~title:"Gc_peer_ids"
|
2018-03-27 17:15:03 +04:00
|
|
|
(branch_encoding "gc_peer_ids" empty)
|
2018-01-24 15:48:25 +04:00
|
|
|
(function Gc_peer_ids -> Some () | _ -> None)
|
|
|
|
(fun () -> Gc_peer_ids) ;
|
2018-03-27 17:15:03 +04:00
|
|
|
case (Tag 15)
|
2018-06-01 01:19:43 +04:00
|
|
|
~title:"Swap_request_received"
|
2018-03-27 17:15:03 +04:00
|
|
|
(branch_encoding "swap_request_received"
|
|
|
|
(obj1 (req "source" P2p_peer_id.encoding)))
|
2018-01-24 15:48:25 +04:00
|
|
|
(function
|
|
|
|
| Swap_request_received { source } -> Some source
|
|
|
|
| _ -> None)
|
|
|
|
(fun source -> Swap_request_received { source }) ;
|
2018-03-27 17:15:03 +04:00
|
|
|
case (Tag 16)
|
2018-06-01 01:19:43 +04:00
|
|
|
~title:"Swap_ack_received"
|
2018-03-27 17:15:03 +04:00
|
|
|
(branch_encoding "swap_ack_received"
|
|
|
|
(obj1 (req "source" P2p_peer_id.encoding)))
|
2018-01-24 15:48:25 +04:00
|
|
|
(function
|
|
|
|
| Swap_ack_received { source } -> Some source
|
|
|
|
| _ -> None)
|
|
|
|
(fun source -> Swap_ack_received { source }) ;
|
2018-03-27 17:15:03 +04:00
|
|
|
case (Tag 17)
|
2018-06-01 01:19:43 +04:00
|
|
|
~title:"Swap_request_sent"
|
2018-03-27 17:15:03 +04:00
|
|
|
(branch_encoding "swap_request_sent"
|
|
|
|
(obj1 (req "source" P2p_peer_id.encoding)))
|
2018-01-24 15:48:25 +04:00
|
|
|
(function
|
|
|
|
| Swap_request_sent { source } -> Some source
|
|
|
|
| _ -> None)
|
|
|
|
(fun source -> Swap_request_sent { source }) ;
|
2018-03-27 17:15:03 +04:00
|
|
|
case (Tag 18)
|
2018-06-01 01:19:43 +04:00
|
|
|
~title:"Swap_ack_sent"
|
2018-03-27 17:15:03 +04:00
|
|
|
(branch_encoding "swap_ack_sent"
|
|
|
|
(obj1 (req "source" P2p_peer_id.encoding)))
|
2018-01-24 15:48:25 +04:00
|
|
|
(function
|
|
|
|
| Swap_ack_sent { source } -> Some source
|
|
|
|
| _ -> None)
|
|
|
|
(fun source -> Swap_ack_sent { source }) ;
|
2018-03-27 17:15:03 +04:00
|
|
|
case (Tag 19)
|
2018-06-01 01:19:43 +04:00
|
|
|
~title:"Swap_request_ignored"
|
2018-03-27 17:15:03 +04:00
|
|
|
(branch_encoding "swap_request_ignored"
|
|
|
|
(obj1 (req "source" P2p_peer_id.encoding)))
|
2018-01-24 15:48:25 +04:00
|
|
|
(function
|
|
|
|
| Swap_request_ignored { source } -> Some source
|
|
|
|
| _ -> None)
|
|
|
|
(fun source -> Swap_request_ignored { source }) ;
|
2018-03-27 17:15:03 +04:00
|
|
|
case (Tag 20)
|
2018-06-01 01:19:43 +04:00
|
|
|
~title:"Swap_success"
|
2018-03-27 17:15:03 +04:00
|
|
|
(branch_encoding "swap_success"
|
|
|
|
(obj1 (req "source" P2p_peer_id.encoding)))
|
2018-01-24 15:48:25 +04:00
|
|
|
(function
|
|
|
|
| Swap_success { source } -> Some source
|
|
|
|
| _ -> None)
|
|
|
|
(fun source -> Swap_success { source }) ;
|
2018-03-27 17:15:03 +04:00
|
|
|
case (Tag 21)
|
2018-06-01 01:19:43 +04:00
|
|
|
~title:"Swap_failure"
|
2018-03-27 17:15:03 +04:00
|
|
|
(branch_encoding "swap_failure"
|
|
|
|
(obj1 (req "source" P2p_peer_id.encoding)))
|
2018-01-24 15:48:25 +04:00
|
|
|
(function
|
|
|
|
| Swap_failure { source } -> Some source
|
|
|
|
| _ -> None)
|
|
|
|
(fun source -> Swap_failure { source }) ;
|
|
|
|
]
|
|
|
|
|
|
|
|
end
|