Source file node_config.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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
(*****************************************************************************)
(*                                                                           *)
(* Open Source License                                                       *)
(* Copyright (c) 2022 Nomadic Labs, <contact@nomadic-labs.com>               *)
(* Copyright (c) 2022 Marigold, <contact@marigold.dev>                       *)
(* Copyright (c) 2022 Oxhead Alpha <info@oxhead-alpha.com>                   *)
(*                                                                           *)
(* 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.                                                 *)
(*                                                                           *)
(*****************************************************************************)

open Protocol.Alpha_context

type mode = Observer | Accuser | Batcher | Maintenance | Operator | Custom

type 'a purposed = {
  operator : 'a;
  submit_batch : 'a;
  finalize_commitment : 'a;
  remove_commitment : 'a;
  rejection : 'a;
  dispatch_withdrawals : 'a;
}

type signers = Tezos_crypto.Signature.V0.public_key_hash option purposed

type cost_caps = {
  fee_cap : Protocol.Alpha_context.Tez.t;
  burn_cap : Protocol.Alpha_context.Tez.t;
}

type caps = cost_caps purposed

type t = {
  data_dir : string;
  rollup_id : Protocol.Alpha_context.Tx_rollup.t;
  origination_level : int32 option;
  rpc_addr : P2p_point.Id.t;
  cors_origins : string list;
  cors_headers : string list;
  reconnection_delay : float;
  mode : mode;
  signers : signers;
  allow_deposit : bool;
  l2_blocks_cache_size : int;
  caps : caps;
  batch_burn_limit : Protocol.Alpha_context.Tez.t option;
}

let default_data_dir rollup_id =
  let open Lwt_syntax in
  let home = Sys.getenv "HOME" in
  let dir =
    ".tezos-tx-rollup-node-"
    ^ Protocol.Alpha_context.Tx_rollup.to_b58check rollup_id
  in
  let dir = Filename.concat home dir in
  let+ () = Lwt_utils_unix.create_dir dir in
  dir

let default_rpc_addr = (Ipaddr.V6.localhost, 9999)

let default_reconnection_delay = 2.0

let default_l2_blocks_cache_size = 64

let modes = [Observer; Accuser; Batcher; Maintenance; Operator; Custom]

let string_of_mode = function
  | Observer -> "observer"
  | Accuser -> "accuser"
  | Batcher -> "batcher"
  | Maintenance -> "maintenance"
  | Operator -> "operator"
  | Custom -> "custom"

let mode_of_string = function
  | "observer" -> Ok Observer
  | "accuser" -> Ok Accuser
  | "batcher" -> Ok Batcher
  | "maintenance" -> Ok Maintenance
  | "operator" -> Ok Operator
  | "custom" -> Ok Custom
  | _ -> Error [Exn (Failure "Invalid mode")]

let mode_encoding =
  Data_encoding.string_enum
    [
      ("observer", Observer);
      ("accuser", Accuser);
      ("batcher", Batcher);
      ("maintenance", Maintenance);
      ("operator", Operator);
      ("custom", Custom);
    ]

let tez t = Tez.of_mutez_exn Int64.(mul (of_int t) 1_000_000L)

let default_cost_caps = {fee_cap = Tez.one; burn_cap = tez 2}

let default_commitment_caps = {fee_cap = tez 2; burn_cap = tez 3}

let default_submit_batch_caps = {fee_cap = tez 5; burn_cap = tez 5}

let default_finalize_commitment_caps = default_cost_caps

let default_remove_commitment_caps = default_cost_caps

let default_rejection_caps = {fee_cap = tez 10; burn_cap = tez 10}

let default_dispatch_withdrawals_caps = {fee_cap = tez 2; burn_cap = tez 3}

let default_caps =
  {
    operator = default_commitment_caps;
    submit_batch = default_submit_batch_caps;
    finalize_commitment = default_finalize_commitment_caps;
    remove_commitment = default_remove_commitment_caps;
    rejection = default_rejection_caps;
    dispatch_withdrawals = default_dispatch_withdrawals_caps;
  }

let signers_encoding =
  let open Data_encoding in
  conv
    (fun {
           operator;
           submit_batch;
           finalize_commitment;
           remove_commitment;
           rejection;
           dispatch_withdrawals;
         } ->
      ( operator,
        submit_batch,
        finalize_commitment,
        remove_commitment,
        rejection,
        dispatch_withdrawals ))
    (fun ( operator,
           submit_batch,
           finalize_commitment,
           remove_commitment,
           rejection,
           dispatch_withdrawals ) ->
      {
        operator;
        submit_batch;
        finalize_commitment;
        remove_commitment;
        rejection;
        dispatch_withdrawals;
      })
  @@ obj6
       (opt
          ~description:"The operator of the rollup (public key hash) if any"
          "operator"
          Tezos_crypto.Signature.V0.Public_key_hash.encoding)
       (opt
          "submit_batch"
          Tezos_crypto.Signature.V0.Public_key_hash.encoding
          ~description:"The public key hash of the signer for batch submission")
       (opt
          "finalize_commitment"
          Tezos_crypto.Signature.V0.Public_key_hash.encoding
          ~description:
            "The public key hash of the signer for finalization of commitments")
       (opt
          "remove_commitment"
          Tezos_crypto.Signature.V0.Public_key_hash.encoding
          ~description:
            "The public key hash of the signer for removals of commitments")
       (opt
          "rejection"
          Tezos_crypto.Signature.V0.Public_key_hash.encoding
          ~description:"The public key hash of the signer for rejections")
       (opt
          "dispatch_withdrawals"
          Tezos_crypto.Signature.V0.Public_key_hash.encoding
          ~description:
            "The public key hash of the signer for the dispatch of withdrawals")

let cost_caps_encoding =
  let open Data_encoding in
  conv
    (fun {fee_cap; burn_cap} -> (fee_cap, burn_cap))
    (fun (fee_cap, burn_cap) -> {fee_cap; burn_cap})
  @@ obj2 (req "fee_cap" Tez.encoding) (req "burn_cap" Tez.encoding)

let caps_encoding =
  let open Data_encoding in
  conv
    (fun {
           operator;
           submit_batch;
           finalize_commitment;
           remove_commitment;
           rejection;
           dispatch_withdrawals;
         } ->
      ( operator,
        submit_batch,
        finalize_commitment,
        remove_commitment,
        rejection,
        dispatch_withdrawals ))
    (fun ( operator,
           submit_batch,
           finalize_commitment,
           remove_commitment,
           rejection,
           dispatch_withdrawals ) ->
      {
        operator;
        submit_batch;
        finalize_commitment;
        remove_commitment;
        rejection;
        dispatch_withdrawals;
      })
  @@ obj6
       (dft
          ~description:"The caps for the cost of commitment operations"
          "commitment"
          cost_caps_encoding
          default_commitment_caps)
       (dft
          ~description:"The caps for the cost of submit batch operations"
          "submit_batch"
          cost_caps_encoding
          default_submit_batch_caps)
       (dft
          ~description:"The caps for the cost of finalize commitment operations"
          "finalized_commitment"
          cost_caps_encoding
          default_finalize_commitment_caps)
       (dft
          ~description:"The caps for the cost of remove commitment operations"
          "remove_commitment"
          cost_caps_encoding
          default_remove_commitment_caps)
       (dft
          ~description:"The caps for the cost of rejection operations"
          "rejection"
          cost_caps_encoding
          default_rejection_caps)
       (dft
          ~description:
            "The caps for the cost of dispatch withdrawals operations"
          "dispatch_withdrawals"
          cost_caps_encoding
          default_dispatch_withdrawals_caps)

let encoding data_dir =
  let open Data_encoding in
  conv
    (fun {
           data_dir = _;
           rollup_id;
           origination_level;
           rpc_addr;
           cors_origins;
           cors_headers;
           reconnection_delay;
           mode;
           signers;
           allow_deposit;
           l2_blocks_cache_size;
           caps;
           batch_burn_limit;
         } ->
      ( ( (),
          rollup_id,
          origination_level,
          rpc_addr,
          reconnection_delay,
          mode,
          signers,
          allow_deposit,
          l2_blocks_cache_size,
          caps ),
        (batch_burn_limit, cors_origins, cors_headers) ))
    (fun ( ( (),
             rollup_id,
             origination_level,
             rpc_addr,
             reconnection_delay,
             mode,
             signers,
             allow_deposit,
             l2_blocks_cache_size,
             caps ),
           (batch_burn_limit, cors_origins, cors_headers) ) ->
      {
        data_dir;
        rollup_id;
        origination_level;
        rpc_addr;
        cors_origins;
        cors_headers;
        reconnection_delay;
        mode;
        signers;
        allow_deposit;
        l2_blocks_cache_size;
        caps;
        batch_burn_limit;
      })
  @@ merge_objs
       (obj10
          (dft
             ~description:
               "Location where the rollup node data (store, context, etc.) is \
                stored"
             "data_dir"
             (constant data_dir)
             ())
          (req
             ~description:"Rollup id of the rollup to target"
             "rollup_id"
             Protocol.Alpha_context.Tx_rollup.encoding)
          (opt
             ~description:"Level of the block where the rollup was originated"
             "origination_level"
             int32)
          (dft
             ~description:"RPC address on which the rollup node listens"
             "rpc_addr"
             P2p_point.Id.encoding
             default_rpc_addr)
          (dft
             ~description:
               "The reconnection (to the tezos node) delay in seconds"
             "reconnection_delay"
             float
             default_reconnection_delay)
          (req
             ~description:"The mode for this rollup node"
             "mode"
             mode_encoding)
          (req
             ~description:"The signers for the various tx rollup operations"
             "signers"
             signers_encoding)
          (dft
             ~description:
               "Allow the operator to make a first deposit for commitments"
             "allow_deposit"
             bool
             false)
          (dft
             ~description:"The size of the L2 block cache in number of blocks"
             "l2_blocks_cache_size"
             int31
             default_l2_blocks_cache_size)
          (dft
             ~description:"The cost caps for the injection of operations"
             "caps"
             caps_encoding
             default_caps))
       (obj3
          (opt
             ~description:
               "The burn limit in for a batch (to be paid for the submission \
                of messages in the protocol inbox)"
             "batch_burn_limit"
             Tez.encoding)
          (dft
             ~description:
               "CORS origin allowed by the RPC server via \
                Access-Control-Allow-Origin"
             "cors-origins"
             (list string)
             [])
          (dft
             ~description:
               "Headers reported by Access-Control-Allow-Headers reported \
                during CORS"
             "cors-headers"
             (list string)
             []))

let get_configuration_filename data_dir =
  let filename = "config.json" in
  Filename.concat data_dir filename

module SigKindSet = struct
  include Set.Make (struct
    type t =
      [ `operator
      | `submit_batch
      | `finalize_commitment
      | `remove_commitment
      | `rejection
      | `dispatch_withdrawals ]

    let compare = Stdlib.compare
  end)

  let elt_to_string = function
    | `operator -> "operator"
    | `submit_batch -> "submit_batch"
    | `finalize_commitment -> "finalize_commitment"
    | `remove_commitment -> "remove_commitment"
    | `rejection -> "rejection"
    | `dispatch_withdrawals -> "dispatch_withdrawals"

  let to_string_list s = elements s |> List.map elt_to_string
end

let check_mode config =
  let with_signers signers =
    let config_signers =
      let add signer name acc =
        Option.fold ~none:acc ~some:(fun _ -> SigKindSet.add name acc) signer
      in
      SigKindSet.empty
      |> add config.signers.operator `operator
      |> add config.signers.submit_batch `submit_batch
      |> add config.signers.finalize_commitment `finalize_commitment
      |> add config.signers.remove_commitment `remove_commitment
      |> add config.signers.rejection `rejection
      |> add config.signers.dispatch_withdrawals `dispatch_withdrawals
    in
    let signers = SigKindSet.of_list signers in
    if SigKindSet.equal config_signers signers then Ok config
    else
      let missing_signers =
        SigKindSet.(diff signers config_signers |> to_string_list)
      in
      if missing_signers <> [] then
        let mode = string_of_mode config.mode in
        Error [Error.Tx_rollup_missing_mode_signers {mode; missing_signers}]
      else
        let extra_signers = SigKindSet.(diff config_signers signers) in
        let remove kind signers =
          match kind with
          | `operator -> {signers with operator = None}
          | `submit_batch -> {signers with submit_batch = None}
          | `finalize_commitment -> {signers with finalize_commitment = None}
          | `remove_commitment -> {signers with remove_commitment = None}
          | `rejection -> {signers with rejection = None}
          | `dispatch_withdrawals -> {signers with dispatch_withdrawals = None}
        in
        let signers = SigKindSet.fold remove extra_signers config.signers in
        Ok {config with signers}
  in
  match config.mode with
  | Observer -> with_signers []
  | Accuser -> with_signers [`rejection]
  | Batcher -> with_signers [`submit_batch]
  | Maintenance ->
      with_signers
        [
          `operator;
          `finalize_commitment;
          `remove_commitment;
          `rejection;
          `dispatch_withdrawals;
        ]
  | Operator ->
      with_signers
        [
          `operator;
          `submit_batch;
          `finalize_commitment;
          `remove_commitment;
          `rejection;
          `dispatch_withdrawals;
        ]
  | Custom -> Ok config

let save ~force configuration =
  let open Lwt_result_syntax in
  let json =
    Data_encoding.Json.construct (encoding configuration.data_dir) configuration
  in
  let*! () = Lwt_utils_unix.create_dir configuration.data_dir in
  let file = get_configuration_filename configuration.data_dir in
  let*! exists = Lwt_unix.file_exists file in
  let* () =
    fail_when
      (exists && not force)
      (Error.Tx_rollup_configuration_file_already_exists file)
  in
  let*! v =
    Lwt_utils_unix.with_atomic_open_out file (fun chan ->
        let content = Data_encoding.Json.to_string json in
        Lwt_utils_unix.write_string chan content)
  in
  let* () =
    Lwt.return
      (Result.map_error
         (fun _ -> [Error.Tx_rollup_unable_to_write_configuration_file file])
         v)
  in
  return file

let load ~data_dir =
  let open Lwt_result_syntax in
  let file = get_configuration_filename data_dir in
  let*! exists = Lwt_unix.file_exists file in
  let* () =
    fail_unless exists (Error.Tx_rollup_configuration_file_does_not_exists file)
  in
  let* json = Lwt_utils_unix.Json.read_file file in
  let config = Data_encoding.Json.destruct (encoding data_dir) json in
  return config