Source file p2p_peer.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
module Id = P2p_peer_id
module Table = Id.Table
module Error_table = Id.Error_table
module Map = Id.Map
module Set = Id.Set
module Filter = struct
type t = Accepted | Running | Disconnected
let rpc_arg =
RPC_arg.make
~name:"p2p.point.state_filter"
~destruct:(function
| "accepted" -> Ok Accepted
| "running" -> Ok Running
| "disconnected" -> Ok Disconnected
| s -> Error (Format.asprintf "Invalid state: %s" s))
~construct:(function
| Accepted -> "accepted"
| Running -> "running"
| Disconnected -> "disconnected")
()
end
module State = struct
type t = Accepted | Running | Disconnected
let pp_digram ppf = function
| Accepted -> Format.fprintf ppf "⚎"
| Running -> Format.fprintf ppf "⚌"
| Disconnected -> Format.fprintf ppf "⚏"
let encoding =
let open Data_encoding in
def
"p2p_peer.state"
~description:
"The state a peer connection can be in: accepted (when the connection \
is being established), running (when the connection is already \
established), disconnected (otherwise)."
@@ string_enum
[
("accepted", Accepted);
("running", Running);
("disconnected", Disconnected);
]
let raw_filter (f : Filter.t) (s : t) =
match (f, s) with
| (Accepted, Accepted) -> true
| (Accepted, (Running | Disconnected)) | ((Running | Disconnected), Accepted)
->
false
| (Running, Running) -> true
| (Disconnected, Disconnected) -> true
| (Running, Disconnected) | (Disconnected, Running) -> false
let filter filters state = List.exists (fun f -> raw_filter f state) filters
end
module Info = struct
type ('peer_meta, 'conn_meta) t = {
score : float;
trusted : bool;
conn_metadata : 'conn_meta option;
peer_metadata : 'peer_meta;
state : State.t;
id_point : P2p_connection.Id.t option;
stat : P2p_stat.t;
last_failed_connection : (P2p_connection.Id.t * Time.System.t) option;
last_rejected_connection : (P2p_connection.Id.t * Time.System.t) option;
last_established_connection : (P2p_connection.Id.t * Time.System.t) option;
last_disconnection : (P2p_connection.Id.t * Time.System.t) option;
last_seen : (P2p_connection.Id.t * Time.System.t) option;
last_miss : (P2p_connection.Id.t * Time.System.t) option;
}
let encoding peer_metadata_encoding conn_metadata_encoding =
let open Data_encoding in
conv
(fun {
score;
trusted;
conn_metadata;
peer_metadata;
state;
id_point;
stat;
last_failed_connection;
last_rejected_connection;
last_established_connection;
last_disconnection;
last_seen;
last_miss;
} ->
( (score, trusted, conn_metadata, peer_metadata, state, id_point, stat),
( last_failed_connection,
last_rejected_connection,
last_established_connection,
last_disconnection,
last_seen,
last_miss ) ))
(fun ( ( score,
trusted,
conn_metadata,
peer_metadata,
state,
id_point,
stat ),
( last_failed_connection,
last_rejected_connection,
last_established_connection,
last_disconnection,
last_seen,
last_miss ) ) ->
{
score;
trusted;
conn_metadata;
peer_metadata;
state;
id_point;
stat;
last_failed_connection;
last_rejected_connection;
last_established_connection;
last_disconnection;
last_seen;
last_miss;
})
(merge_objs
(obj7
(req "score" float)
(req "trusted" bool)
(opt "conn_metadata" conn_metadata_encoding)
(req "peer_metadata" peer_metadata_encoding)
(req "state" State.encoding)
(opt "reachable_at" P2p_connection.Id.encoding)
(req "stat" P2p_stat.encoding))
(obj6
(opt
"last_failed_connection"
(tup2 P2p_connection.Id.encoding Time.System.encoding))
(opt
"last_rejected_connection"
(tup2 P2p_connection.Id.encoding Time.System.encoding))
(opt
"last_established_connection"
(tup2 P2p_connection.Id.encoding Time.System.encoding))
(opt
"last_disconnection"
(tup2 P2p_connection.Id.encoding Time.System.encoding))
(opt
"last_seen"
(tup2 P2p_connection.Id.encoding Time.System.encoding))
(opt
"last_miss"
(tup2 P2p_connection.Id.encoding Time.System.encoding))))
end
module Pool_event = struct
type kind =
| Accepting_request
| Rejecting_request
| Request_rejected
| Connection_established
| Disconnection
| External_disconnection
let kind_encoding =
Data_encoding.string_enum
[
("incoming_request", Accepting_request);
("rejecting_request", Rejecting_request);
("request_rejected", Request_rejected);
("connection_established", Connection_established);
("disconnection", Disconnection);
("external_disconnection", External_disconnection);
]
type t = {kind : kind; timestamp : Time.System.t; point : P2p_connection.Id.t}
let encoding =
let open Data_encoding in
def
"p2p_peer.pool_event"
~description:
"An event that may happen during maintenance of and other operations \
on the connection to a specific peer."
@@ conv
(fun {kind; timestamp; point = (addr, port)} ->
(kind, timestamp, addr, port))
(fun (kind, timestamp, addr, port) ->
{kind; timestamp; point = (addr, port)})
(obj4
(req "kind" kind_encoding)
(req "timestamp" Time.System.encoding)
(req "addr" P2p_addr.encoding)
(opt "port" uint16))
end
let () =
Data_encoding.Registration.register ~pp:State.pp_digram State.encoding ;
Data_encoding.Registration.register Pool_event.encoding