package tezt-tezos

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file dac_node.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
(*****************************************************************************)
(*                                                                           *)
(* Open Source License                                                       *)
(* Copyright (c) 2022 Nomadic Labs, <contact@nomadic-labs.com>               *)
(* Copyright (c) 2023 Marigold, <contact@marigold.dev>                       *)
(*                                                                           *)
(* Permission is hereby granted, free of charge, to any person obtaining a   *)
(* copy of this software and associated documentation files (the "Software"),*)
(* to deal in the Software without restriction, including without limitation *)
(* the rights to use, copy, modify, merge, publish, distribute, sublicense,  *)
(* and/or sell copies of the Software, and to permit persons to whom the     *)
(* Software is furnished to do so, subject to the following conditions:      *)
(*                                                                           *)
(* The above copyright notice and this permission notice shall be included   *)
(* in all copies or substantial portions of the Software.                    *)
(*                                                                           *)
(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)
(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  *)
(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL   *)
(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)
(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING   *)
(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER       *)
(* DEALINGS IN THE SOFTWARE.                                                 *)
(*                                                                           *)
(*****************************************************************************)

module Parameters = struct
  type coordinator_mode_settings = {committee_members : string list}

  type committee_member_mode_settings = {
    address : string;
    coordinator_rpc_host : string;
    coordinator_rpc_port : int;
  }

  type observer_mode_settings = {
    coordinator_rpc_host : string;
    coordinator_rpc_port : int;
    committee_member_rpcs : (string * int) list;
    timeout : int option;
  }

  type mode_settings =
    | Coordinator of coordinator_mode_settings
    | Committee_member of committee_member_mode_settings
    | Observer of observer_mode_settings

  type persistent_state = {
    data_dir : string;
    reveal_data_dir : string;
    rpc_host : string;
    rpc_port : int;
    mode : mode_settings;
    endpoint : Client.endpoint;
    client : Client.t;
    mutable pending_ready : unit option Lwt.u list;
    allow_v1_api : bool;
  }

  type session_state = {mutable ready : bool}

  let base_default_name = "octez-dac-node"

  let default_colors = Log.Color.[|FG.gray; FG.magenta; FG.yellow; FG.green|]
end

open Parameters
include Daemon.Make (Parameters)

let wait dac_node =
  match dac_node.status with
  | Not_running ->
      Test.fail
        "DAC node %s is not running, cannot wait for it to terminate"
        (name dac_node)
  | Running {process; _} -> Process.wait process

let is_running_not_ready dac_node =
  match dac_node.status with
  | Running {session_state = {ready}; _} when not ready -> true
  | _ -> false

let name dac_node = dac_node.name

let mode dac_node =
  match dac_node.persistent_state.mode with
  | Coordinator _ -> "Coordinator"
  | Committee_member _ -> "Committee_member"
  | Observer _ -> "Observer"

let rpc_host dac_node = dac_node.persistent_state.rpc_host

let rpc_port dac_node = dac_node.persistent_state.rpc_port

let layer1_scheme dac_node = Client.scheme dac_node.persistent_state.endpoint

let layer1_addr dac_node = Client.address dac_node.persistent_state.endpoint

let layer1_port dac_node = Client.rpc_port dac_node.persistent_state.endpoint

let endpoint dac_node =
  Printf.sprintf "http://%s:%d" (rpc_host dac_node) (rpc_port dac_node)

let data_dir dac_node = dac_node.persistent_state.data_dir

let reveal_data_dir dac_node = dac_node.persistent_state.reveal_data_dir

let allow_v1_api dac_node = dac_node.persistent_state.allow_v1_api

let spawn_command dac_node =
  Process.spawn ~name:dac_node.name ~color:dac_node.color dac_node.path

let raw_rpc (host, port) = Printf.sprintf "%s:%d" host port

let localhost = "127.0.0.1"

let spawn_config_init dac_node =
  let arg_command =
    List.append
      [
        "--data-dir";
        data_dir dac_node;
        "--rpc-port";
        string_of_int (rpc_port dac_node);
        "--rpc-addr";
        rpc_host dac_node;
        "--reveal-data-dir";
        reveal_data_dir dac_node;
      ]
    @@ if allow_v1_api dac_node then ["--allow-v1-api"] else []
  in
  let mode_command =
    match dac_node.persistent_state.mode with
    | Coordinator coordinator_params ->
        [
          "configure";
          "as";
          "coordinator";
          "with";
          "data";
          "availability";
          "committee";
          "members";
        ]
        @ coordinator_params.committee_members
    | Committee_member committee_member_params ->
        let coordinator_host =
          committee_member_params.coordinator_rpc_host ^ ":"
          ^ Int.to_string committee_member_params.coordinator_rpc_port
        in
        [
          "configure";
          "as";
          "committee";
          "member";
          "with";
          "coordinator";
          coordinator_host;
          "and";
          "signer";
          committee_member_params.address;
        ]
    | Observer
        {
          coordinator_rpc_host;
          coordinator_rpc_port;
          committee_member_rpcs;
          timeout;
        } ->
        let with_timeout =
          match timeout with
          | Some t -> ["--timeout"; Int.to_string t]
          | None -> []
        in
        let coordinator_host =
          coordinator_rpc_host ^ ":" ^ Int.to_string coordinator_rpc_port
        in
        let committee_member_rpcs = List.map raw_rpc committee_member_rpcs in
        [
          "configure";
          "as";
          "observer";
          "with";
          "coordinator";
          coordinator_host;
          "and";
          "committee";
          "member";
          "rpc";
          "addresses";
        ]
        @ committee_member_rpcs @ with_timeout
  in
  spawn_command dac_node (mode_command @ arg_command)

let ls = "ls"

let ls_reveal_data_dir dac_node =
  let reveal_data_dir = dac_node.persistent_state.reveal_data_dir in
  let commands = [reveal_data_dir] in
  let process =
    Process.spawn ~name:dac_node.name ~color:dac_node.color ls commands
  in
  let* filenames = Process.check_and_read_stdout process in
  return @@ String.split_on_char '\n' filenames

let init_config dac_node =
  let process = spawn_config_init dac_node in
  let* output = Process.check_and_read_stdout process in
  match output =~* rex "DAC node configuration written in ([^\n]*)" with
  | None -> failwith "DAC node configuration initialization failed"
  | Some filename -> return filename

module Config_file = struct
  let filename dac_node = sf "%s/config.json" @@ data_dir dac_node

  let read dac_node = JSON.parse_file (filename dac_node)

  let write dac_node config = JSON.encode_to_file (filename dac_node) config

  let update dac_node update = read dac_node |> update |> write dac_node
end

let check_event ?timeout ?where dac_node name promise =
  let* result =
    match timeout with
    | None -> promise
    | Some timeout ->
        Lwt.pick
          [
            promise;
            (let* () = Lwt_unix.sleep timeout in
             Lwt.return None);
          ]
  in
  match result with
  | None ->
      raise
        (Terminated_before_event {daemon = dac_node.name; event = name; where})
  | Some x -> return x

let trigger_ready dac_node value =
  let pending = dac_node.persistent_state.pending_ready in
  dac_node.persistent_state.pending_ready <- [] ;
  List.iter (fun pending -> Lwt.wakeup_later pending value) pending

let set_ready dac_node =
  (match dac_node.status with
  | Not_running -> ()
  | Running status -> status.session_state.ready <- true) ;
  trigger_ready dac_node (Some ())

let wait_for_ready dac_node =
  match dac_node.status with
  | Running {session_state = {ready = true; _}; _} -> unit
  | Not_running | Running {session_state = {ready = false; _}; _} ->
      let promise, resolver = Lwt.task () in
      dac_node.persistent_state.pending_ready <-
        resolver :: dac_node.persistent_state.pending_ready ;
      check_event dac_node "dac_node_is_ready.v0" promise

let handle_event dac_node {name; value = _; timestamp = _} =
  match name with "dac_node_is_ready.v0" -> set_ready dac_node | _ -> ()

let create_with_endpoint ?(path = Constant.dac_node) ?name ?color ?data_dir
    ?event_pipe ?(rpc_host = "127.0.0.1") ?rpc_port ?reveal_data_dir ~mode
    ~endpoint ~client ?(allow_v1_api = false) () =
  let name = match name with None -> fresh_name () | Some name -> name in
  let data_dir =
    match data_dir with None -> Temp.dir name | Some dir -> dir
  in
  let reveal_data_dir =
    match reveal_data_dir with
    | None -> Temp.dir (name ^ "preimages")
    | Some dir -> dir
  in
  let rpc_port =
    match rpc_port with None -> Port.fresh () | Some port -> port
  in
  let dac_node =
    create
      ~path
      ~name
      ?color
      ?event_pipe
      {
        data_dir;
        reveal_data_dir;
        rpc_host;
        rpc_port;
        mode;
        pending_ready = [];
        endpoint;
        client;
        allow_v1_api;
      }
  in
  on_event dac_node (handle_event dac_node) ;
  dac_node

let create_coordinator_with_endpoint ?path ?name ?color ?data_dir ?event_pipe
    ?rpc_host ?rpc_port ?reveal_data_dir ?allow_v1_api ~committee_members
    ~endpoint ~client () =
  let mode = Coordinator {committee_members} in
  create_with_endpoint
    ?path
    ?name
    ?color
    ?data_dir
    ?event_pipe
    ?rpc_host
    ?rpc_port
    ?reveal_data_dir
    ~mode
    ~endpoint
    ~client
    ?allow_v1_api
    ()

let create_coordinator ?path ?name ?color ?data_dir ?event_pipe ?rpc_host
    ?rpc_port ?reveal_data_dir ?allow_v1_api ~committee_members ~node ~client ()
    =
  create_coordinator_with_endpoint
    ?path
    ?name
    ?color
    ?data_dir
    ?event_pipe
    ?rpc_host
    ?rpc_port
    ?reveal_data_dir
    ?allow_v1_api
    ~committee_members
    ~endpoint:(Node node)
    ~client
    ()

let create_committee_member_with_endpoint ?path ?name ?color ?data_dir
    ?event_pipe ?rpc_host ?rpc_port ?reveal_data_dir
    ?(coordinator_rpc_host = localhost) ?coordinator_rpc_port ?allow_v1_api
    ~address ~endpoint ~client () =
  let coordinator_rpc_port =
    match coordinator_rpc_port with None -> Port.fresh () | Some port -> port
  in
  let mode =
    Committee_member {address; coordinator_rpc_host; coordinator_rpc_port}
  in
  create_with_endpoint
    ?path
    ?name
    ?color
    ?data_dir
    ?event_pipe
    ?rpc_host
    ?rpc_port
    ?reveal_data_dir
    ~mode
    ~endpoint
    ~client
    ?allow_v1_api
    ()

let create_committee_member ?path ?name ?color ?data_dir ?event_pipe ?rpc_host
    ?rpc_port ?reveal_data_dir ?coordinator_rpc_host ?coordinator_rpc_port
    ?allow_v1_api ~address ~node ~client () =
  create_committee_member_with_endpoint
    ?path
    ?name
    ?color
    ?data_dir
    ?event_pipe
    ?rpc_host
    ?rpc_port
    ?reveal_data_dir
    ?coordinator_rpc_host
    ?coordinator_rpc_port
    ?allow_v1_api
    ~address
    ~endpoint:(Node node)
    ~client
    ()

let create_observer_with_endpoint ?path ?name ?color ?data_dir ?event_pipe
    ?rpc_host ?rpc_port ?reveal_data_dir ?(coordinator_rpc_host = localhost)
    ?coordinator_rpc_port ?timeout ?allow_v1_api ~committee_member_rpcs
    ~endpoint ~client () =
  let coordinator_rpc_port =
    match coordinator_rpc_port with None -> Port.fresh () | Some port -> port
  in
  let mode =
    Observer
      {
        coordinator_rpc_host;
        coordinator_rpc_port;
        committee_member_rpcs;
        timeout;
      }
  in
  create_with_endpoint
    ?path
    ?name
    ?color
    ?data_dir
    ?event_pipe
    ?rpc_host
    ?rpc_port
    ?reveal_data_dir
    ~mode
    ~endpoint
    ~client
    ?allow_v1_api
    ()

let create_observer ?path ?name ?color ?data_dir ?event_pipe ?rpc_host ?rpc_port
    ?reveal_data_dir ?coordinator_rpc_host ?coordinator_rpc_port ?timeout
    ?allow_v1_api ~committee_member_rpcs ~node ~client () =
  create_observer_with_endpoint
    ?path
    ?name
    ?color
    ?data_dir
    ?event_pipe
    ?rpc_host
    ?rpc_port
    ?reveal_data_dir
    ?coordinator_rpc_host
    ?coordinator_rpc_port
    ?timeout
    ?allow_v1_api
    ~committee_member_rpcs
    ~endpoint:(Node node)
    ~client
    ()

let make_arguments node =
  let base_dir_args =
    ["--base-dir"; Client.base_dir node.persistent_state.client]
  in
  let endpoint_args =
    [
      "--endpoint";
      Printf.sprintf
        "%s://%s:%d"
        (layer1_scheme node)
        (layer1_addr node)
        (layer1_port node);
    ]
  in
  base_dir_args @ endpoint_args

let do_runlike_command ?env node arguments =
  if node.status <> Not_running then
    Test.fail "DAC node %s is already running" node.name ;
  let on_terminate _status =
    trigger_ready node None ;
    unit
  in
  let arguments = make_arguments node @ arguments in
  run ?env node {ready = false} arguments ~on_terminate

let run ?env node =
  do_runlike_command
    ?env
    node
    ["run"; "--data-dir"; node.persistent_state.data_dir]

let run ?(wait_ready = true) ?env node =
  let* () = run ?env node in
  let* () = if wait_ready then wait_for_ready node else Lwt.return_unit in
  return ()

let with_sleeping_node ?rpc_port ?(rpc_address = localhost) ~timeout f =
  let make_host str =
    match Ipaddr.of_string str with
    | Ok (Ipaddr.V4 addr) -> Ipaddr.v6_of_v4 addr
    | Ok (V6 addr) -> addr
    | Error (`Msg _) ->
        raise
          (Invalid_argument
             "Invalid rpc_address when initializing sleeping_node.")
  in
  let open Cohttp_lwt_unix in
  let callback _conn _req _body =
    let* _ = Lwt_unix.sleep timeout in
    Server.respond_string ~status:`OK ~body:"ok" ()
  in
  let stop, resolver = Lwt.task () in
  let stopper () = Lwt.wakeup_later resolver () in
  let rpc_port = Option.value rpc_port ~default:(Port.fresh ()) in
  let port = `Port rpc_port in
  let host = Ipaddr.V6.to_string (make_host rpc_address) in
  let server = Server.make ~callback () in
  let _ =
    let* ctx = Conduit_lwt_unix.init ~src:host () in
    let ctx = Cohttp_lwt_unix.Net.init ~ctx () in
    Server.create ~ctx ~stop ~mode:(`TCP port) server
  in
  Lwt.finalize
    (fun () -> f (rpc_address, rpc_port))
    (fun () -> return (stopper ()))
OCaml

Innovation. Community. Security.