Source file p2p_test_utils.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
(** This module provides functions used for tests. *)
type error += Timeout_exceed of string
let () =
register_error_kind
`Permanent
~id:"test_p2p.utils.timeout_exceed"
~title:"Timeout exceed"
~description:"The timeout used to wait connections has been exceed."
~pp:(fun ppf msg ->
Format.fprintf ppf "The timeout has been exceed : %s." msg)
Data_encoding.(obj1 (req "msg" Data_encoding.string))
(function Timeout_exceed msg -> Some msg | _ -> None)
(fun msg -> Timeout_exceed msg)
let connect_all ?timeout connect_handler points =
List.map_ep
(fun point -> P2p_connect_handler.connect ?timeout connect_handler point)
points
type 'a timeout_t = {time : float; msg : 'a -> string}
let wait_pred ?timeout ~pred ~arg () =
let open Lwt_result_syntax in
let rec inner_wait_pred () =
let*! () = Lwt.pause () in
if not (pred arg) then inner_wait_pred () else return_unit
in
match timeout with
| None -> inner_wait_pred ()
| Some timeout ->
Lwt.pick
[
(let*! () = Lwt_unix.sleep timeout.time in
tzfail (Timeout_exceed (timeout.msg arg)));
inner_wait_pred ();
]
let wait_pred_s ?timeout ~pred ~arg () =
let open Lwt_result_syntax in
let rec inner_wait_pred () =
let*! () = Lwt.pause () in
let*! cond = pred arg in
if not cond then inner_wait_pred () else return_unit
in
match timeout with
| None -> inner_wait_pred ()
| Some timeout ->
Lwt.pick
[
(let*! () = Lwt_unix.sleep timeout.time in
tzfail (Timeout_exceed (timeout.msg arg)));
inner_wait_pred ();
]
let wait_conns ?timeout ~pool n =
let timeout =
Option.map
(fun timeout ->
{
time = timeout;
msg =
(fun (pool, n) ->
Format.asprintf
"in wait_conns with n = %d and conns = %d"
n
(P2p_pool.active_connections pool));
})
timeout
in
wait_pred
?timeout
~pred:(fun (pool, n) -> P2p_pool.active_connections pool = n)
~arg:(pool, n)
()
let close_active_conns pool =
P2p_pool.Connection.fold
~init:Lwt.return_unit
~f:(fun _ conn _ ->
P2p_conn.disconnect ~reason:(User "closed by test") ~wait:true conn)
pool
let canceler = Lwt_canceler.create ()
let proof_of_work_target = Tezos_crypto.Crypto_box.make_pow_target 1.
let id1 = P2p_identity.generate proof_of_work_target
let id2 = P2p_identity.generate proof_of_work_target
let version =
{
Network_version.chain_name =
Distributed_db_version.Name.of_string "SANDBOXED_TEZOS";
distributed_db_version = Distributed_db_version.one;
p2p_version = P2p_version.zero;
}
let conn_meta_config : unit P2p_params.conn_meta_config =
{
conn_meta_encoding = Data_encoding.empty;
conn_meta_value = (fun () -> ());
private_node = (fun _ -> false);
}
let glob_port = ref None
let listen ?port addr =
let open Lwt_result_syntax in
let port_or_gen = Option.value port ~default:0 in
let* sock = P2p_fd.create_listening_socket ~backlog:1 ~addr port_or_gen in
let port =
match port with
| Some port -> port
| None -> (
let addr = Lwt_unix.getsockname sock in
match addr with ADDR_INET (_, port) -> port | _ -> assert false)
in
return (sock, port)
let rec sync_nodes nodes =
let open Lwt_result_syntax in
let* () = List.iter_ep (fun p -> Process.receive p) nodes in
let* () = List.iter_ep (fun p -> Process.send p ()) nodes in
sync_nodes nodes
let sync_nodes nodes =
let open Lwt_result_syntax in
let*! r = sync_nodes nodes in
match r with
| Ok () | Error (Exn End_of_file :: _) -> return_unit
| Error _ as err -> Lwt.return err
let run_nodes ~addr ?port client server =
let open Lwt_result_syntax in
let p =
match port with
| None ->
glob_port := None ;
None
| Some p ->
glob_port := Some (p + 1) ;
Some p
in
let* main_socket, p = listen ?port:p addr in
let* server_node =
Process.detach ~prefix:"server: " (fun channel ->
let sched = P2p_io_scheduler.create ~read_buffer_size:(1 lsl 12) () in
Lwt.finalize
(fun () ->
let* () = server channel sched main_socket in
let*! () = P2p_io_scheduler.shutdown sched in
return_unit)
(fun () ->
let*! r = Lwt_utils_unix.safe_close main_socket in
match r with
| Error trace ->
Format.eprintf "Uncaught error: %a\n%!" pp_print_trace trace ;
Lwt.return_unit
| Ok () -> Lwt.return_unit))
in
let* client_node =
Process.detach ~prefix:"client: " (fun channel ->
let*! () =
let*! r = Lwt_utils_unix.safe_close main_socket in
match r with
| Error trace ->
Format.eprintf "Uncaught error: %a\n%!" pp_print_trace trace ;
Lwt.return_unit
| Ok () -> Lwt.return_unit
in
let sched = P2p_io_scheduler.create ~read_buffer_size:(1 lsl 12) () in
let* () = client channel sched addr p in
let*! () = P2p_io_scheduler.shutdown sched in
return_unit)
in
let nodes = [server_node; client_node] in
Lwt.ignore_result (sync_nodes nodes) ;
let* () = Process.wait_all nodes in
let*! _ = Lwt_utils_unix.safe_close main_socket in
return_unit
let run_nodes_fd ~addr ?port client server =
let open Lwt_result_syntax in
let p =
match port with
| None ->
glob_port := None ;
None
| Some p ->
glob_port := Some (p + 1) ;
Some p
in
let* main_socket, p = listen ?port:p addr in
let* server_node =
Process.detach ~prefix:"server: " (fun channel ->
Lwt.finalize
(fun () ->
let* () = server channel main_socket in
return_unit)
(fun () ->
let*! r = Lwt_utils_unix.safe_close main_socket in
match r with
| Error trace ->
Format.eprintf "Uncaught error: %a\n%!" pp_print_trace trace ;
Lwt.return_unit
| Ok () -> Lwt.return_unit))
in
let* client_node =
Process.detach ~prefix:"client: " (fun channel ->
let*! () =
let*! r = Lwt_utils_unix.safe_close main_socket in
match r with
| Error trace ->
Format.eprintf "Uncaught error: %a\n%!" pp_print_trace trace ;
Lwt.return_unit
| Ok () -> Lwt.return_unit
in
let* () = client channel addr p in
return_unit)
in
let nodes = [server_node; client_node] in
Lwt.ignore_result (sync_nodes nodes) ;
let* () = Process.wait_all nodes in
let*! _ = Lwt_utils_unix.safe_close main_socket in
return_unit
let raw_accept sched main_socket =
let open Lwt_syntax in
let* r = P2p_fd.accept main_socket in
match r with
| Error (`Socket_error ex | `System_error ex | `Unexpected_error ex) ->
Lwt.fail ex
| Ok (fd, sockaddr) ->
let fd = P2p_io_scheduler.register sched fd in
let point =
match sockaddr with
| Lwt_unix.ADDR_UNIX _ -> assert false
| Lwt_unix.ADDR_INET (addr, port) ->
(Ipaddr_unix.V6.of_inet_addr_exn addr, port)
in
Lwt.return_ok (fd, point)
(** [accept ?id ?proof_of_work_target sched main_socket] connect
and performs [P2p_socket.authenticate] with the given
[proof_of_work_target]. *)
let accept ?(id = id1) ?(proof_of_work_target = proof_of_work_target) sched
main_socket =
let open Lwt_syntax in
let* r = raw_accept sched main_socket in
let* id1 = id in
match r with
| Error (`Socket_error ex | `System_error ex | `Unexpected_error ex) ->
Lwt.fail ex
| Ok (fd, point) ->
P2p_socket.authenticate
~canceler
~proof_of_work_target
~incoming:true
fd
point
id1
version
conn_meta_config
let raw_connect sched addr port =
let open Lwt_result_syntax in
let*! fd = P2p_fd.socket () in
let uaddr = Lwt_unix.ADDR_INET (Ipaddr_unix.V6.to_inet_addr addr, port) in
let* () = P2p_fd.connect fd uaddr in
let fd = P2p_io_scheduler.register sched fd in
Lwt.return_ok fd
exception Cant_connect
(** [connect ?proof_of_work_target sched addr port] connect
and performs [P2p_socket.authenticate] with the given
[proof_of_work_target]. *)
let connect ?(proof_of_work_target = proof_of_work_target) sched addr port id =
let open Lwt_result_syntax in
let*! r = raw_connect sched addr port in
match r with
| Error (`Connection_failed | `Unexpected_error _) -> Lwt.fail Cant_connect
| Ok fd ->
P2p_socket.authenticate
~canceler
~proof_of_work_target
~incoming:false
fd
(addr, port)
id
version
conn_meta_config
let sync ch =
let open Lwt_result_syntax in
let* () = Process.Channel.push ch () in
let* () = Process.Channel.pop ch in
return_unit