Source file configuration.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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
(*****************************************************************************)
(*                                                                           *)
(* Open Source License                                                       *)
(* Copyright (c) 2021 Nomadic Labs, <contact@nomadic-labs.com>               *)
(* Copyright (c) 2022 Trili Tech, <contact@trili.tech>                       *)
(* 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.                                                 *)
(*                                                                           *)
(*****************************************************************************)

type mode =
  | Observer
  | Accuser
  | Bailout
  | Batcher
  | Maintenance
  | Operator
  | Custom of Operation_kind.t list

type batcher = {
  min_batch_elements : int;
  min_batch_size : int;
  max_batch_elements : int;
  max_batch_size : int option;
}

type injector = {retention_period : int; attempts : int; injection_ttl : int}

type fee_parameters = Injector_common.fee_parameter Operation_kind.Map.t

type gc_parameters = {
  frequency_in_blocks : int32;
  context_splitting_period : int option;
}

type history_mode = Archive | Full

type t = {
  sc_rollup_address : Tezos_crypto.Hashed.Smart_rollup_address.t;
  boot_sector_file : string option;
  operators : Purpose.operators;
  rpc_addr : string;
  rpc_port : int;
  acl : Tezos_rpc_http_server.RPC_server.Acl.policy;
  metrics_addr : string option;
  reconnection_delay : float;
  fee_parameters : fee_parameters;
  mode : mode;
  loser_mode : Loser_mode.t;
  apply_unsafe_patches : bool;
  unsafe_pvm_patches : Pvm_patches.unsafe_patch list;
  dal_node_endpoint : Uri.t option;
  dac_observer_endpoint : Uri.t option;
  dac_timeout : Z.t option;
  pre_images_endpoint : Uri.t option;
  batcher : batcher;
  injector : injector;
  l1_blocks_cache_size : int;
  l2_blocks_cache_size : int;
  prefetch_blocks : int option;
  l1_rpc_timeout : float;
  loop_retry_delay : float;
  index_buffer_size : int option;
  irmin_cache_size : int option;
  log_kernel_debug : bool;
  no_degraded : bool;
  gc_parameters : gc_parameters;
  history_mode : history_mode option;
  cors : Resto_cohttp.Cors.t;
}

type error += Empty_operation_kinds_for_custom_mode

let () =
  register_error_kind
    ~id:"sc_rollup_node.empty_operation_kinds_for_custom_mode"
    ~title:"Empty operation kinds for custom mode"
    ~description:
      "Empty operation kinds are not allowed for custom modes, just like in \
       observer mode"
    ~pp:(fun ppf _s ->
      Format.pp_print_string ppf "Operation kinds for custom mode are empty.")
    `Permanent
    Data_encoding.unit
    (function Empty_operation_kinds_for_custom_mode -> Some () | _ -> None)
    (fun () -> Empty_operation_kinds_for_custom_mode)

let default_data_dir =
  Filename.concat (Sys.getenv "HOME") ".tezos-smart-rollup-node"

let storage_dir = "storage"

let context_dir = "context"

let default_storage_dir data_dir = Filename.concat data_dir storage_dir

let default_context_dir data_dir = Filename.concat data_dir context_dir

let config_filename ~data_dir = Filename.concat data_dir "config.json"

let default_rpc_addr = "127.0.0.1"

let default_rpc_port = 8932

let default_metrics_port = 9933

let default_acl = Tezos_rpc_http_server.RPC_server.Acl.empty_policy

let default_reconnection_delay = 2.0 (* seconds *)

let mutez mutez = {Injector_common.mutez}

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

(* The below default fee and burn limits are computed by taking into account
   the worst fee found in the tests for the rollup node.

   We take as base the cost of commitment cementation, which is 719 mutez in fees:
   - Commitment publishing is 1.37 times more expensive.
   - Message submission is 0.7 times more expensive, so cheaper but it depends on
     the size of the message.
   - For refutation games:
     - Open is 1.55 times more expensive.
     - Dissection move is 2.31 times more expensive.
     - Proof move is 1.47 times more expensive but depends on the size of the proof.
     - Timeout move is 1.34 times more expensive.

   We set a fee limit of 1 tz for cementation (instead of 719 mutez) which
   should be plenty enough even if the gas price or gas consumption
   increases. We adjust the other limits in proportion.
*)
let default_fee : Operation_kind.t -> Injector_common.tez = function
  | Cement -> tez 1
  | Recover -> tez 1
  | Publish -> tez 2
  | Add_messages ->
      (* We keep this limit even though it depends on the size of the message
         because the rollup node pays the fees for messages submitted by the
         **users**. *)
      tez 1
  | Timeout -> tez 2
  | Refute ->
      (* Should be 3 based on comment above but we want to make sure we inject
         refutation moves even if the proof is large. The stake is high (we can
         lose the 10k deposit or we can get the reward). *)
      tez 5
  | Execute_outbox_message -> tez 1

let default_burn : Operation_kind.t -> Injector_common.tez = function
  | Publish ->
      (* The first commitment can store data. *)
      tez 1
  | Add_messages -> tez 0
  | Cement -> tez 0
  | Recover -> tez 0
  | Timeout -> tez 0
  | Refute ->
      (* A refutation move can store data, e.g. opening a game. *)
      tez 1
  | Execute_outbox_message -> tez 1

(* Copied from src/proto_alpha/lib_plugin/mempool.ml *)
let default_fee_parameter operation_kind =
  {
    Injector_common.minimal_fees = mutez 100L;
    minimal_nanotez_per_byte = Q.of_int 1000;
    minimal_nanotez_per_gas_unit = Q.of_int 100;
    force_low_fee = false;
    fee_cap = default_fee operation_kind;
    burn_cap = default_burn operation_kind;
  }

let default_fee_parameters =
  List.fold_left
    (fun acc operation_kind ->
      Operation_kind.Map.add
        operation_kind
        (default_fee_parameter operation_kind)
        acc)
    Operation_kind.Map.empty
    Operation_kind.all

let default_batcher_min_batch_elements = 10

let default_batcher_min_batch_size = 10

let default_batcher_max_batch_elements = (1 lsl 30) - 1

let default_batcher =
  {
    min_batch_elements = default_batcher_min_batch_elements;
    min_batch_size = default_batcher_min_batch_size;
    max_batch_elements = default_batcher_max_batch_elements;
    max_batch_size = None;
  }

let default_injector =
  {retention_period = 2048; attempts = 10; injection_ttl = 120}

let max_injector_retention_period =
  5 * 8192 (* Preserved cycles (5) for mainnet *)

let default_l1_blocks_cache_size = 64

let default_l2_blocks_cache_size = 64

let default_l1_rpc_timeout = 60. (* seconds *)

let default_loop_retry_delay = 10. (* seconds *)

let default_gc_parameters =
  {
    (* TODO: https://gitlab.com/tezos/tezos/-/issues/6415
     * Refine the default GC frequency parameter *)
    frequency_in_blocks = 100l;
    context_splitting_period = None;
  }

(* TODO: https://gitlab.com/tezos/tezos/-/issues/6576
   Set to Full after initial evaluation on testnets. *)
let default_history_mode = Archive

let string_of_history_mode = function Archive -> "archive" | Full -> "full"

let history_mode_of_string = function
  | "archive" -> Archive
  | "full" -> Full
  | s -> invalid_arg ("history_mode_of_string " ^ s)

let modes =
  [
    Observer;
    Accuser;
    Bailout;
    Batcher;
    Maintenance;
    Operator;
    Custom Operation_kind.all;
  ]

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

let mode_of_string s =
  match s with
  | "observer" -> Ok Observer
  | "accuser" -> Ok Accuser
  | "bailout" -> Ok Bailout
  | "batcher" -> Ok Batcher
  | "maintenance" -> Ok Maintenance
  | "operator" -> Ok Operator
  | "custom" -> Ok (Custom [])
  | s when String.starts_with ~prefix:"custom:" s ->
      let kinds = String.sub s 7 (String.length s - 7) in
      let operation_kinds_strs = String.split_on_char ',' kinds in
      let operation_kinds =
        List.map Operation_kind.of_string_exn operation_kinds_strs
      in
      Ok (Custom operation_kinds)
  | _ -> Error [Exn (Failure "Invalid mode")]

let description_of_mode = function
  | Observer -> "Only follows the chain, reconstructs and interprets inboxes"
  | Accuser ->
      "Only publishes commitments for conflicts and play refutation games"
  | Bailout -> "Only defends and cements, does not publish any new commitments"
  | Batcher -> "Accepts transactions in its queue and batches them on the L1"
  | Maintenance ->
      "Follows the chain and publishes commitments, cement and refute"
  | Operator -> "Equivalent to maintenance + batcher"
  | Custom op_kinds ->
      let op_kinds_desc =
        List.map Operation_kind.to_string op_kinds |> String.concat ", "
      in
      Printf.sprintf
        "In this mode, the system handles only the specific operation kinds: \
         [%s]. This allows for tailored control and flexibility."
        op_kinds_desc

let mode_encoding =
  let open Data_encoding in
  let operation_kinds_encoding = list Operation_kind.encoding in
  let constant_case mode =
    let title = string_of_mode mode in
    case
      ~title
      Json_only
      (constant title)
      (fun m -> if m = mode then Some () else None)
      (fun () -> mode)
  in
  let custom_case =
    case
      ~title:"custom"
      Json_only
      (obj1 (req "custom" operation_kinds_encoding))
      (function Custom operation_kinds -> Some operation_kinds | _ -> None)
      (fun operation_kinds -> Custom operation_kinds)
  in
  let all_cases =
    custom_case
    :: List.map
         constant_case
         [Observer; Accuser; Bailout; Batcher; Maintenance; Operator]
  in
  def "sc_rollup_node_mode" @@ union all_cases

let batcher_encoding =
  let open Data_encoding in
  conv_with_guard
    (fun {
           min_batch_elements;
           min_batch_size;
           max_batch_elements;
           max_batch_size;
         } ->
      (min_batch_elements, min_batch_size, max_batch_elements, max_batch_size))
    (fun (min_batch_elements, min_batch_size, max_batch_elements, max_batch_size)
         ->
      let open Result_syntax in
      let error_when c s = if c then Error s else return_unit in
      let* () =
        error_when (min_batch_size <= 0) "min_batch_size must be positive"
      in
      let* () =
        match max_batch_size with
        | Some m when m < min_batch_size ->
            Error "max_batch_size must be greater than min_batch_size"
        | _ -> return_unit
      in
      let* () =
        error_when (min_batch_elements <= 0) "min_batch_size must be positive"
      in
      let+ () =
        error_when
          (max_batch_elements < min_batch_elements)
          "max_batch_elements must be greater than min_batch_elements"
      in
      {min_batch_elements; min_batch_size; max_batch_elements; max_batch_size})
  @@ obj4
       (dft "min_batch_elements" int31 default_batcher_min_batch_elements)
       (dft "min_batch_size" int31 default_batcher_min_batch_size)
       (dft "max_batch_elements" int31 default_batcher_max_batch_elements)
       (opt "max_batch_size" int31)

let injector_encoding : injector Data_encoding.t =
  let open Data_encoding in
  conv
    (fun {retention_period; attempts; injection_ttl} ->
      (retention_period, attempts, injection_ttl))
    (fun (retention_period, attempts, injection_ttl) ->
      if retention_period > max_injector_retention_period then
        Format.ksprintf
          Stdlib.failwith
          "injector.retention_period should be smaller than %d"
          max_injector_retention_period ;
      if injection_ttl < 1 then
        Stdlib.failwith "injector.injection_ttl should be at least 1" ;
      {retention_period; attempts; injection_ttl})
  @@ obj3
       (dft "retention_period" uint16 default_injector.retention_period)
       (dft "attempts" uint16 default_injector.attempts)
       (dft "injection_ttl" uint16 default_injector.injection_ttl)

let fee_parameters_encoding =
  Operation_kind.map_encoding (fun operation_kind ->
      Injector_common.fee_parameter_encoding
        ~default_fee_parameter:(default_fee_parameter operation_kind))

let gc_parameters_encoding : gc_parameters Data_encoding.t =
  let open Data_encoding in
  conv
    (fun {frequency_in_blocks; context_splitting_period} ->
      (frequency_in_blocks, context_splitting_period))
    (fun (frequency_in_blocks, context_splitting_period) ->
      {frequency_in_blocks; context_splitting_period})
  @@ obj2
       (dft "frequency" int32 default_gc_parameters.frequency_in_blocks)
       (opt "context_splitting_period" int31)

let history_mode_encoding : history_mode Data_encoding.t =
  Data_encoding.string_enum [("archive", Archive); ("full", Full)]

let cors_encoding : Resto_cohttp.Cors.t Data_encoding.t =
  let open Resto_cohttp.Cors in
  let open Data_encoding in
  conv
    (fun {allowed_headers; allowed_origins} ->
      (allowed_headers, allowed_origins))
    (fun (allowed_headers, allowed_origins) ->
      {allowed_headers; allowed_origins})
  @@ obj2
       (req "allowed_headers" (list string))
       (req "allowed_origins" (list string))

let encoding default_display : t Data_encoding.t =
  let open Data_encoding in
  let dft =
    match default_display with
    | `Hide -> dft
    | `Show ->
        fun ?title ?description name enc _default ->
          req ?title ?description name enc
  in
  conv
    (fun {
           sc_rollup_address;
           boot_sector_file;
           operators;
           rpc_addr;
           rpc_port;
           acl;
           metrics_addr;
           reconnection_delay;
           fee_parameters;
           mode;
           loser_mode;
           apply_unsafe_patches = _;
           unsafe_pvm_patches;
           dal_node_endpoint;
           dac_observer_endpoint;
           dac_timeout;
           pre_images_endpoint;
           batcher;
           injector;
           l1_blocks_cache_size;
           l2_blocks_cache_size;
           prefetch_blocks;
           l1_rpc_timeout;
           loop_retry_delay;
           index_buffer_size;
           irmin_cache_size;
           log_kernel_debug;
           no_degraded;
           gc_parameters;
           history_mode;
           cors;
         } ->
      ( ( ( sc_rollup_address,
            boot_sector_file,
            operators,
            rpc_addr,
            rpc_port,
            acl ),
          ( metrics_addr,
            reconnection_delay,
            fee_parameters,
            mode,
            loser_mode,
            unsafe_pvm_patches ) ),
        ( ( dal_node_endpoint,
            dac_observer_endpoint,
            dac_timeout,
            pre_images_endpoint,
            batcher,
            injector,
            l1_blocks_cache_size,
            l2_blocks_cache_size,
            prefetch_blocks ),
          ( l1_rpc_timeout,
            loop_retry_delay,
            index_buffer_size,
            irmin_cache_size,
            log_kernel_debug,
            no_degraded,
            gc_parameters,
            history_mode,
            cors ) ) ))
    (fun ( ( ( sc_rollup_address,
               boot_sector_file,
               operators,
               rpc_addr,
               rpc_port,
               acl ),
             ( metrics_addr,
               reconnection_delay,
               fee_parameters,
               mode,
               loser_mode,
               unsafe_pvm_patches ) ),
           ( ( dal_node_endpoint,
               dac_observer_endpoint,
               dac_timeout,
               pre_images_endpoint,
               batcher,
               injector,
               l1_blocks_cache_size,
               l2_blocks_cache_size,
               prefetch_blocks ),
             ( l1_rpc_timeout,
               loop_retry_delay,
               index_buffer_size,
               irmin_cache_size,
               log_kernel_debug,
               no_degraded,
               gc_parameters,
               history_mode,
               cors ) ) ) ->
      {
        sc_rollup_address;
        boot_sector_file;
        operators;
        rpc_addr;
        rpc_port;
        acl;
        metrics_addr;
        reconnection_delay;
        fee_parameters;
        mode;
        loser_mode;
        apply_unsafe_patches =
          (* Flag --apply-unsafe-patches must always be given on command
             line. *)
          false;
        unsafe_pvm_patches;
        dal_node_endpoint;
        dac_observer_endpoint;
        dac_timeout;
        pre_images_endpoint;
        batcher;
        injector;
        l1_blocks_cache_size;
        l2_blocks_cache_size;
        prefetch_blocks;
        l1_rpc_timeout;
        loop_retry_delay;
        index_buffer_size;
        irmin_cache_size;
        log_kernel_debug;
        no_degraded;
        gc_parameters;
        history_mode;
        cors;
      })
    (merge_objs
       (merge_objs
          (obj6
             (req
                "smart-rollup-address"
                ~description:"Smart rollup address"
                Tezos_crypto.Hashed.Smart_rollup_address.encoding)
             (opt "boot-sector" ~description:"Boot sector" string)
             (req
                "smart-rollup-node-operator"
                ~description:
                  "Operators that sign operations of the smart rollup, by \
                   purpose"
                Purpose.operators_encoding)
             (dft "rpc-addr" ~description:"RPC address" string default_rpc_addr)
             (dft "rpc-port" ~description:"RPC port" uint16 default_rpc_port)
             (dft
                "acl"
                ~description:"Access control list"
                Tezos_rpc_http_server.RPC_server.Acl.policy_encoding
                default_acl))
          (obj6
             (opt "metrics-addr" ~description:"Metrics address" string)
             (dft
                "reconnection_delay"
                ~description:
                  "The reconnection (to the tezos node) delay in seconds"
                float
                default_reconnection_delay)
             (dft
                "fee-parameters"
                ~description:
                  "The fee parameters for each purpose used when injecting \
                   operations in L1"
                fee_parameters_encoding
                default_fee_parameters)
             (req
                ~description:"The mode for this rollup node"
                "mode"
                mode_encoding)
             (dft
                "loser-mode"
                ~description:
                  "If enabled, the rollup node will issue wrong commitments \
                   (for test only!)"
                Loser_mode.encoding
                Loser_mode.no_failures)
             (dft
                "unsafe-pvm-patches"
                ~description:
                  "Unsafe patches to apply to the PVM. For tests only, don't \
                   set this value in production."
                (list Pvm_patches.unsafe_patch_encoding)
                [])))
       (merge_objs
          (obj9
             (opt "DAL node endpoint" Tezos_rpc.Encoding.uri_encoding)
             (opt "dac-observer-client" Tezos_rpc.Encoding.uri_encoding)
             (opt "dac-timeout" Data_encoding.z)
             (opt "pre-images-endpoint" Tezos_rpc.Encoding.uri_encoding)
             (dft "batcher" batcher_encoding default_batcher)
             (dft "injector" injector_encoding default_injector)
             (dft "l1_blocks_cache_size" int31 default_l1_blocks_cache_size)
             (dft "l2_blocks_cache_size" int31 default_l2_blocks_cache_size)
             (opt "prefetch_blocks" int31))
          (obj9
             (dft "l1_rpc_timeout" Data_encoding.float default_l1_rpc_timeout)
             (dft
                "loop_retry_delay"
                Data_encoding.float
                default_loop_retry_delay)
             (opt "index_buffer_size" int31)
             (opt "irmin_cache_size" int31)
             (dft "log-kernel-debug" Data_encoding.bool false)
             (dft "no-degraded" Data_encoding.bool false)
             (dft "gc-parameters" gc_parameters_encoding default_gc_parameters)
             (opt "history-mode" history_mode_encoding)
             (dft "cors" cors_encoding Resto_cohttp.Cors.default))))

let encoding_no_default = encoding `Show

let encoding = encoding `Hide

(** Maps a mode to their corresponding purposes. The Custom mode
    returns each purposes where it has at least one operation kind
    from (i.e. {!purposes_of_operation_kinds}). *)
let purposes_of_mode mode : Purpose.ex_purpose list =
  match mode with
  | Observer -> []
  | Batcher -> [Purpose Batching]
  | Accuser -> [Purpose Operating]
  | Bailout -> [Purpose Operating; Purpose Cementing; Purpose Recovering]
  | Maintenance ->
      [Purpose Operating; Purpose Cementing; Purpose Executing_outbox]
  | Operator ->
      [
        Purpose Operating;
        Purpose Cementing;
        Purpose Executing_outbox;
        Purpose Batching;
      ]
  | Custom op_kinds -> Purpose.of_operation_kind op_kinds

let operation_kinds_of_mode mode =
  match mode with
  | Custom op_kinds -> op_kinds
  | _ ->
      let purposes = purposes_of_mode mode in
      List.map Purpose.operation_kind purposes |> List.flatten

let check_custom_mode mode =
  error_when (mode = Custom []) Empty_operation_kinds_for_custom_mode

let can_inject mode (op_kind : Operation_kind.t) =
  let allowed_operations = operation_kinds_of_mode mode in
  List.mem ~equal:Stdlib.( = ) op_kind allowed_operations

let purpose_matches_mode (type k) mode (purpose : k Purpose.t) =
  List.mem ~equal:Stdlib.( = ) (Purpose.Purpose purpose) (purposes_of_mode mode)

let refutation_player_buffer_levels = 5

let default_index_buffer_size = 10_000

let default_irmin_cache_size = 300_000

let loser_warning_message config =
  if config.loser_mode <> Loser_mode.no_failures then
    Format.printf
      {|
************ WARNING *************
This rollup node is in loser mode.
This should be used for test only!
************ WARNING *************
|}

let override_acl ~rpc_addr ~rpc_port acl = function
  | None -> acl
  | Some kind ->
      let new_acl =
        match kind with
        | `Secure -> Rpc_server.Acl.secure
        | `Allow_all -> Rpc_server.Acl.allow_all
      in
      let addr =
        P2p_point.Id.{addr = rpc_addr; port = Some rpc_port; peer_id = None}
      in
      Tezos_rpc_http_server.RPC_server.Acl.put_policy (addr, new_acl) acl

let save ~force ~data_dir config =
  loser_warning_message config ;
  let open Lwt_result_syntax in
  let json = Data_encoding.Json.construct encoding config in
  let config_file = config_filename ~data_dir in
  let*! exists = Lwt_unix.file_exists config_file in
  if exists && not force then
    failwith
      "Configuration file %S already exists. Use --force to overwrite."
      config_file
  else
    let*! () = Lwt_utils_unix.create_dir data_dir in
    Lwt_utils_unix.Json.write_file config_file json

let load ~data_dir =
  let open Lwt_result_syntax in
  let+ json = Lwt_utils_unix.Json.read_file (config_filename ~data_dir) in
  let config = Data_encoding.Json.destruct encoding json in
  loser_warning_message config ;
  config

module Cli = struct
  let get_purposed_and_default_operators operators =
    let open Result_syntax in
    List.fold_left_e
      (fun (purposed_operator, default_operator_opt) -> function
        | `Purpose p_operator ->
            return (p_operator :: purposed_operator, default_operator_opt)
        | `Default operator ->
            if Option.is_none default_operator_opt then
              return (purposed_operator, Some operator)
            else tzfail (error_of_fmt "Multiple default operators"))
      ([], None)
      operators

  let configuration_from_args ~rpc_addr ~rpc_port ~acl_override ~metrics_addr
      ~loser_mode ~reconnection_delay ~dal_node_endpoint ~dac_observer_endpoint
      ~dac_timeout ~pre_images_endpoint ~injector_retention_period
      ~injector_attempts ~injection_ttl ~mode ~sc_rollup_address
      ~boot_sector_file ~operators ~index_buffer_size ~irmin_cache_size
      ~log_kernel_debug ~no_degraded ~gc_frequency ~history_mode
      ~allowed_origins ~allowed_headers ~apply_unsafe_patches =
    let open Result_syntax in
    let* purposed_operator, default_operator =
      get_purposed_and_default_operators operators
    in
    let* operators =
      Purpose.make_operator
        ?default_operator
        ~needed_purposes:(purposes_of_mode mode)
        purposed_operator
    in
    let rpc_addr = Option.value ~default:default_rpc_addr rpc_addr in
    let rpc_port = Option.value ~default:default_rpc_port rpc_port in
    let acl = override_acl ~rpc_addr ~rpc_port default_acl acl_override in
    let+ () = check_custom_mode mode in
    {
      sc_rollup_address;
      boot_sector_file;
      operators;
      rpc_addr;
      rpc_port;
      acl;
      reconnection_delay =
        Option.value ~default:default_reconnection_delay reconnection_delay;
      dal_node_endpoint;
      dac_observer_endpoint;
      dac_timeout;
      pre_images_endpoint;
      metrics_addr;
      fee_parameters = Operation_kind.Map.empty;
      mode;
      loser_mode = Option.value ~default:Loser_mode.no_failures loser_mode;
      apply_unsafe_patches;
      unsafe_pvm_patches = [];
      batcher = default_batcher;
      injector =
        {
          retention_period =
            Option.value
              ~default:default_injector.retention_period
              injector_retention_period;
          attempts =
            Option.value ~default:default_injector.attempts injector_attempts;
          injection_ttl =
            Option.value ~default:default_injector.injection_ttl injection_ttl;
        };
      l1_blocks_cache_size = default_l1_blocks_cache_size;
      l2_blocks_cache_size = default_l2_blocks_cache_size;
      prefetch_blocks = None;
      l1_rpc_timeout = default_l1_rpc_timeout;
      loop_retry_delay = default_loop_retry_delay;
      index_buffer_size;
      irmin_cache_size;
      log_kernel_debug;
      no_degraded;
      gc_parameters =
        {
          frequency_in_blocks =
            Option.value
              ~default:default_gc_parameters.frequency_in_blocks
              gc_frequency;
          context_splitting_period = None;
        };
      history_mode;
      cors =
        Resto_cohttp.Cors.
          {
            allowed_headers =
              Option.value ~default:default.allowed_headers allowed_headers;
            allowed_origins =
              Option.value ~default:default.allowed_origins allowed_origins;
          };
    }

  let patch_configuration_from_args configuration ~rpc_addr ~rpc_port
      ~acl_override ~metrics_addr ~loser_mode ~reconnection_delay
      ~dal_node_endpoint ~dac_observer_endpoint ~dac_timeout
      ~pre_images_endpoint ~injector_retention_period ~injector_attempts
      ~injection_ttl ~mode ~sc_rollup_address ~boot_sector_file ~operators
      ~index_buffer_size ~irmin_cache_size ~log_kernel_debug ~no_degraded
      ~gc_frequency ~history_mode ~allowed_origins ~allowed_headers
      ~apply_unsafe_patches =
    let open Result_syntax in
    let mode = Option.value ~default:configuration.mode mode in
    let* () = check_custom_mode mode in
    let* purposed_operator, default_operator =
      get_purposed_and_default_operators operators
    in
    let* operators =
      Purpose.replace_operator
        ?default_operator
        ~needed_purposes:(purposes_of_mode mode)
        purposed_operator
        configuration.operators
    in
    let rpc_addr = Option.value ~default:configuration.rpc_addr rpc_addr in
    let rpc_port = Option.value ~default:configuration.rpc_port rpc_port in
    let acl = override_acl ~rpc_addr ~rpc_port configuration.acl acl_override in
    return
      {
        configuration with
        sc_rollup_address =
          Option.value
            ~default:configuration.sc_rollup_address
            sc_rollup_address;
        boot_sector_file =
          Option.either boot_sector_file configuration.boot_sector_file;
        operators;
        mode;
        rpc_addr;
        rpc_port;
        acl;
        dal_node_endpoint =
          Option.either dal_node_endpoint configuration.dal_node_endpoint;
        dac_observer_endpoint =
          Option.either
            dac_observer_endpoint
            configuration.dac_observer_endpoint;
        dac_timeout = Option.either dac_timeout configuration.dac_timeout;
        pre_images_endpoint =
          Option.either pre_images_endpoint configuration.pre_images_endpoint;
        reconnection_delay =
          Option.value
            ~default:configuration.reconnection_delay
            reconnection_delay;
        injector =
          {
            retention_period =
              Option.value
                ~default:default_injector.retention_period
                injector_retention_period;
            attempts =
              Option.value ~default:default_injector.attempts injector_attempts;
            injection_ttl =
              Option.value ~default:default_injector.injection_ttl injection_ttl;
          };
        loser_mode = Option.value ~default:configuration.loser_mode loser_mode;
        apply_unsafe_patches;
        metrics_addr = Option.either metrics_addr configuration.metrics_addr;
        index_buffer_size =
          Option.either index_buffer_size configuration.index_buffer_size;
        irmin_cache_size =
          Option.either irmin_cache_size configuration.irmin_cache_size;
        log_kernel_debug = log_kernel_debug || configuration.log_kernel_debug;
        no_degraded = no_degraded || configuration.no_degraded;
        gc_parameters =
          {
            frequency_in_blocks =
              Option.value
                ~default:configuration.gc_parameters.frequency_in_blocks
                gc_frequency;
            context_splitting_period =
              configuration.gc_parameters.context_splitting_period;
          };
        history_mode = Option.either history_mode configuration.history_mode;
        cors =
          Resto_cohttp.Cors.
            {
              allowed_headers =
                Option.value
                  ~default:configuration.cors.allowed_headers
                  allowed_headers;
              allowed_origins =
                Option.value
                  ~default:configuration.cors.allowed_origins
                  allowed_origins;
            };
      }

  let create_or_read_config ~data_dir ~rpc_addr ~rpc_port ~acl_override
      ~metrics_addr ~loser_mode ~reconnection_delay ~dal_node_endpoint
      ~dac_observer_endpoint ~dac_timeout ~pre_images_endpoint
      ~injector_retention_period ~injector_attempts ~injection_ttl ~mode
      ~sc_rollup_address ~boot_sector_file ~operators ~index_buffer_size
      ~irmin_cache_size ~log_kernel_debug ~no_degraded ~gc_frequency
      ~history_mode ~allowed_origins ~allowed_headers ~apply_unsafe_patches =
    let open Lwt_result_syntax in
    let open Filename.Infix in
    (* Check if the data directory of the smart rollup node is not the one of Octez node *)
    let* () =
      let*! identity_file_in_data_dir_exists =
        Lwt_unix.file_exists (data_dir // "identity.json")
      in
      if identity_file_in_data_dir_exists then
        failwith
          "Invalid data directory. This is a data directory for an Octez node, \
           please choose a different directory for the smart rollup node data."
      else return_unit
    in
    let config_file = config_filename ~data_dir in
    let*! exists_config = Lwt_unix.file_exists config_file in
    if exists_config then
      (* Read configuration from file and patch if user wanted to override
         some fields with values provided by arguments. *)
      let* configuration = load ~data_dir in
      let*? configuration =
        patch_configuration_from_args
          configuration
          ~rpc_addr
          ~rpc_port
          ~acl_override
          ~metrics_addr
          ~loser_mode
          ~reconnection_delay
          ~dal_node_endpoint
          ~dac_observer_endpoint
          ~dac_timeout
          ~pre_images_endpoint
          ~injector_retention_period
          ~injector_attempts
          ~injection_ttl
          ~mode
          ~sc_rollup_address
          ~boot_sector_file
          ~operators
          ~index_buffer_size
          ~irmin_cache_size
          ~log_kernel_debug
          ~no_degraded
          ~gc_frequency
          ~history_mode
          ~allowed_origins
          ~allowed_headers
          ~apply_unsafe_patches
      in
      return configuration
    else
      (* Build configuration from arguments only. *)
      let*? mode =
        Option.value_e
          mode
          ~error:
            (TzTrace.make
            @@ error_of_fmt
                 "Argument --mode is required when configuration file is not \
                  present.")
      in
      let*? sc_rollup_address =
        Option.value_e
          sc_rollup_address
          ~error:
            (TzTrace.make
            @@ error_of_fmt
                 "Argument --rollup is required when configuration file is not \
                  present.")
      in
      let*? config =
        configuration_from_args
          ~rpc_addr
          ~rpc_port
          ~acl_override
          ~metrics_addr
          ~loser_mode
          ~reconnection_delay
          ~dal_node_endpoint
          ~dac_observer_endpoint
          ~dac_timeout
          ~pre_images_endpoint
          ~injector_retention_period
          ~injector_attempts
          ~injection_ttl
          ~mode
          ~sc_rollup_address
          ~boot_sector_file
          ~operators
          ~index_buffer_size
          ~irmin_cache_size
          ~log_kernel_debug
          ~no_degraded
          ~gc_frequency
          ~history_mode
          ~allowed_headers
          ~allowed_origins
          ~apply_unsafe_patches
      in
      return config
end