Source file rollup_node_services.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
(** This file defines the services of the rollup node that do not depend on the
protocol. A top-level directory is built for them in the rollup node
library.
Protocol specific services are for RPCs under [/global/block/] and are
defined in the protocol specific libraries in
[src/proto_*/lib_sc_rollup_layer2/sc_rollup_services.ml].
*)
(** We distinguish RPC endpoints served by the rollup node into [global] and
[local]. The difference between the two lies in whether the responses given
by different rollup nodes in the same state (see below for an exact
definition) must be the same (in the case of global endpoints) or can differ
(in the case of local endpoints).
More formally, two rollup nodes are in the same quiescent state if they are
subscribed to the same rollup address, and have processed the same set of
heads from the layer1. We only consider quiescent states, that is those
where rollup nodes are not actively processing a head received from layer1.
Examples of global endpoints are [current_tezos_head] and
[last_stored_commitment], as the responses returned by these endpoints is
expected to be consistent across rollup nodes in the same state.
An example of local endpoint is [last_published_commitment], as two rollup
nodes in the same state may either publish or not publish a commitment,
according to whether its inbox level is below the inbox level of the last
cemented commitment at the time they tried to publish the commitment. See
below for a more detailed explanation.
*)
type message_status =
| Unknown
| Pending_batch
| Pending_injection of L1_operation.t
| Injected of {op : L1_operation.t; oph : Operation_hash.t; op_index : int}
| Included of {
op : L1_operation.t;
oph : Operation_hash.t;
op_index : int;
l1_block : Block_hash.t;
l1_level : int32;
finalized : bool;
cemented : bool;
}
| Committed of {
op : L1_operation.t;
oph : Operation_hash.t;
op_index : int;
l1_block : Block_hash.t;
l1_level : int32;
finalized : bool;
cemented : bool;
commitment : Commitment.t;
commitment_hash : Commitment.Hash.t;
first_published_at_level : int32;
published_at_level : int32;
}
type gc_info = {
last_gc_level : int32;
first_available_level : int32;
last_context_split_level : int32 option;
}
type sync_result =
| Synchronized
| Synchronizing of {
processed_level : int32;
l1_head_level : int32;
percentage_done : float;
}
type version = {
version : string;
store_version : string;
context_version : string;
}
type injector_operation = {
op : L1_operation.t;
errors : int;
last_error : tztrace option;
}
module Encodings = struct
open Data_encoding
let version =
conv
(fun {version; store_version; context_version} ->
(version, store_version, context_version))
(fun (version, store_version, context_version) ->
{version; store_version; context_version})
@@ obj3
(req "version" string)
(req "store_version" string)
(req "context_version" string)
let commitment_with_hash =
obj2
(req "commitment" Commitment.encoding)
(req "hash" Commitment.Hash.encoding)
let commitment_with_hash_and_level_infos =
obj4
(req "commitment" Commitment.encoding)
(req "hash" Commitment.Hash.encoding)
(opt "first_published_at_level" int32)
(opt "published_at_level" int32)
let queued_message =
obj2 (req "id" L2_message.Id.encoding) (req "message" L2_message.encoding)
let batcher_queue = list queued_message
let message_status =
union
[
case
(Tag 0)
~title:"unknown"
~description:"The message is not known by the batcher."
(obj1 (req "status" (constant "unknown")))
(function Unknown -> Some () | _ -> None)
(fun () -> Unknown);
case
(Tag 1)
~title:"pending_batch"
~description:"The message is in the batcher queue."
(obj1 (req "status" (constant "pending_batch")))
(function Pending_batch -> Some () | _ -> None)
(fun () -> Pending_batch);
case
(Tag 2)
~title:"pending_injection"
~description:"The message is batched but not injected yet."
(obj2
(req "status" (constant "pending_injection"))
(req "operation" L1_operation.encoding))
(function Pending_injection op -> Some ((), op) | _ -> None)
(fun ((), op) -> Pending_injection op);
case
(Tag 3)
~title:"injected"
~description:
"The message is injected as part of an L1 operation but it is not \
included in a block."
(obj3
(req "status" (constant "injected"))
(req "operation" L1_operation.encoding)
(req
"layer1"
(obj2
(req "operation_hash" Operation_hash.encoding)
(req "operation_index" int31))))
(function
| Injected {op; oph; op_index} -> Some ((), op, (oph, op_index))
| _ -> None)
(fun ((), op, (oph, op_index)) -> Injected {op; oph; op_index});
case
(Tag 4)
~title:"included"
~description:"The message is included in an inbox in an L1 block."
(obj5
(req "status" (constant "included"))
(req "operation" L1_operation.encoding)
(req
"layer1"
(obj4
(req "operation_hash" Operation_hash.encoding)
(req "operation_index" int31)
(req "block_hash" Block_hash.encoding)
(req "level" int32)))
(req "finalized" bool)
(req "cemented" bool))
(function
| Included
{op; oph; op_index; l1_block; l1_level; finalized; cemented} ->
Some
( (),
op,
(oph, op_index, l1_block, l1_level),
finalized,
cemented )
| _ -> None)
(fun ((), op, (oph, op_index, l1_block, l1_level), finalized, cemented)
->
Included
{op; oph; op_index; l1_block; l1_level; finalized; cemented});
case
(Tag 5)
~title:"committed"
~description:"The message is included in a committed inbox on L1."
(obj9
(req "status" (constant "committed"))
(req "operation" L1_operation.encoding)
(req
"layer1"
(obj4
(req "operation_hash" Operation_hash.encoding)
(req "operation_index" int31)
(req "block_hash" Block_hash.encoding)
(req "level" int32)))
(req "finalized" bool)
(req "cemented" bool)
(req "commitment" Commitment.encoding)
(req "hash" Commitment.Hash.encoding)
(req "first_published_at_level" int32)
(req "published_at_level" int32))
(function
| Committed
{
op;
oph;
op_index;
l1_block;
l1_level;
finalized;
cemented;
commitment;
commitment_hash;
first_published_at_level;
published_at_level;
} ->
Some
( (),
op,
(oph, op_index, l1_block, l1_level),
finalized,
cemented,
commitment,
commitment_hash,
first_published_at_level,
published_at_level )
| _ -> None)
(fun ( (),
op,
(oph, op_index, l1_block, l1_level),
finalized,
cemented,
commitment,
commitment_hash,
first_published_at_level,
published_at_level ) ->
Committed
{
op;
oph;
op_index;
l1_block;
l1_level;
finalized;
cemented;
commitment;
commitment_hash;
first_published_at_level;
published_at_level;
});
]
let message_status_output =
merge_objs (obj1 (opt "content" (string' Hex))) message_status
let gc_info : gc_info Data_encoding.t =
conv
(fun {last_gc_level; first_available_level; last_context_split_level} ->
(last_gc_level, first_available_level, last_context_split_level))
(fun (last_gc_level, first_available_level, last_context_split_level) ->
{last_gc_level; first_available_level; last_context_split_level})
@@ obj3
(req "last_gc_level" int32)
(req "first_available_level" int32)
(opt "last_context_split_level" int32)
let ocaml_gc_stat_encoding =
let open Gc in
conv
(fun {
minor_words;
promoted_words;
major_words;
minor_collections;
major_collections;
forced_major_collections;
heap_words;
heap_chunks;
live_words;
live_blocks;
free_words;
free_blocks;
largest_free;
fragments;
compactions;
top_heap_words;
stack_size;
} ->
( ( minor_words,
promoted_words,
major_words,
minor_collections,
major_collections,
forced_major_collections ),
( (heap_words, heap_chunks, live_words, live_blocks, free_words),
( free_blocks,
largest_free,
fragments,
compactions,
top_heap_words,
stack_size ) ) ))
(fun ( ( minor_words,
promoted_words,
major_words,
minor_collections,
major_collections,
forced_major_collections ),
( (heap_words, heap_chunks, live_words, live_blocks, free_words),
( free_blocks,
largest_free,
fragments,
compactions,
top_heap_words,
stack_size ) ) ) ->
{
minor_words;
promoted_words;
major_words;
minor_collections;
major_collections;
forced_major_collections;
heap_words;
heap_chunks;
live_words;
live_blocks;
free_words;
free_blocks;
largest_free;
fragments;
compactions;
top_heap_words;
stack_size;
})
(merge_objs
(obj6
(req "minor_words" float)
(req "promoted_words" float)
(req "major_words" float)
(req "minor_collections" int31)
(req "major_collections" int31)
(req "forced_major_collections" int31))
(merge_objs
(obj5
(req "heap_words" int31)
(req "heap_chunks" int31)
(req "live_words" int31)
(req "live_blocks" int31)
(req "free_words" int31))
(obj6
(req "free_blocks" int31)
(req "largest_free" int31)
(req "fragments" int31)
(req "compactions" int31)
(req "top_heap_words" int31)
(req "stack_size" int31))))
let mem_stat_encoding =
let open Memory in
union
~tag_size:`Uint8
[
case
(Tag 0)
(conv
(fun {page_size; size; resident; shared; text; lib; data; dt} ->
(page_size, size, resident, shared, text, lib, data, dt))
(fun (page_size, size, resident, shared, text, lib, data, dt) ->
{page_size; size; resident; shared; text; lib; data; dt})
(obj8
(req "page_size" int31)
(req "size" int64)
(req "resident" int64)
(req "shared" int64)
(req "text" int64)
(req "lib" int64)
(req "data" int64)
(req "dt" int64)))
~title:"Linux_proc_statm"
(function Statm x -> Some x | _ -> None)
(function res -> Statm res);
case
(Tag 1)
(conv
(fun {page_size; mem; resident} -> (page_size, mem, resident))
(fun (page_size, mem, resident) -> {page_size; mem; resident})
(obj3
(req "page_size" int31)
(req "mem" float)
(req "resident" int64)))
~title:"Darwin_ps"
(function Ps x -> Some x | _ -> None)
(function res -> Ps res);
]
let synchronization_result =
union
[
case
~title:"synchronized"
(Tag 0)
(constant "synchronized")
(function Synchronized -> Some () | _ -> None)
(fun () -> Synchronized);
case
~title:"synchronizing"
(Tag 1)
(obj1
(req
"synchronizing"
(obj3
(req "processed_level" int32)
(req "l1_head_level" int32)
(req "percentage_done" float))))
(function
| Synchronizing {processed_level; l1_head_level; percentage_done} ->
Some (processed_level, l1_head_level, percentage_done)
| _ -> None)
(fun (processed_level, l1_head_level, percentage_done) ->
Synchronizing {processed_level; l1_head_level; percentage_done});
]
let injector_queues_total =
obj2
(req
"injectors"
(list
(obj2
(req "tags" (list Operation_kind.encoding))
(req "queue_size" int31))))
(req "total" int31)
let injector_operation =
conv
(fun {op; errors; last_error} -> (op, (errors, last_error)))
(fun (op, (errors, last_error)) -> {op; errors; last_error})
@@ merge_objs
L1_operation.encoding
(obj1
(dft
"errors"
(obj2 (req "count" int31) (opt "last" trace_encoding))
(0, None)))
let injector_queues =
list
(obj2
(req "tags" (list Operation_kind.encoding))
(req "queue" (list injector_operation)))
end
module Arg = struct
type block_id =
[`Head | `Hash of Block_hash.t | `Level of Int32.t | `Finalized | `Cemented]
let construct_block_id = function
| `Head -> "head"
| `Hash h -> Block_hash.to_b58check h
| `Level l -> Int32.to_string l
| `Finalized -> "finalized"
| `Cemented -> "cemented"
let destruct_block_id h =
match h with
| "head" -> Ok `Head
| "finalized" -> Ok `Finalized
| "cemented" -> Ok `Cemented
| _ -> (
match Int32.of_string_opt h with
| Some l -> Ok (`Level l)
| None -> (
match Block_hash.of_b58check_opt h with
| Some b -> Ok (`Hash b)
| None -> Error "Cannot parse block id"))
let block_id : block_id Tezos_rpc.Arg.t =
Tezos_rpc.Arg.make
~descr:"An L1 block identifier."
~name:"block_id"
~construct:construct_block_id
~destruct:destruct_block_id
()
let l2_message_id : L2_message.id Tezos_rpc.Arg.t =
Tezos_rpc.Arg.make
~descr:"A L2 message id."
~name:"l2_message_id"
~construct:L2_message.Id.to_b58check
~destruct:(fun s ->
L2_message.Id.of_b58check_opt s
|> Option.to_result ~none:"Invalid L2 message id")
()
let commitment_hash : Commitment.Hash.t Tezos_rpc.Arg.t =
Tezos_rpc.Arg.make
~descr:"A commitment hash."
~name:"commitment_hash"
~construct:Commitment.Hash.to_b58check
~destruct:(fun s ->
Commitment.Hash.of_b58check_opt s
|> Option.to_result ~none:"Invalid commitment hash")
()
let operation_kind : Operation_kind.t Tezos_rpc.Arg.t =
Tezos_rpc.Arg.make
~descr:"A kind of operation for the injector."
~name:"operation_kind"
~construct:Operation_kind.to_string
~destruct:(fun s ->
Operation_kind.of_string s
|> Option.to_result ~none:"Invalid operation kind")
()
end
module Query = struct
type proto_query = {protocol : Protocol_hash.t option}
let proto_query : proto_query Tezos_rpc.Query.t =
let open Tezos_rpc.Query in
query (fun protocol -> {protocol})
|+ opt_field "protocol" Protocol_hash.rpc_arg (fun p -> p.protocol)
|> seal
let operation_tag_query : Operation_kind.t option Tezos_rpc.Query.t =
let open Tezos_rpc.Query in
query (fun tag -> tag)
|+ opt_field "tag" Arg.operation_kind (fun k -> k)
|> seal
end
module type PREFIX = sig
type prefix
val prefix : (unit, prefix) Tezos_rpc.Path.t
end
module Make_services (P : PREFIX) = struct
include P
let path : prefix Tezos_rpc.Path.context = Tezos_rpc.Path.open_root
let make_call s =
Tezos_rpc.Context.make_call (Tezos_rpc.Service.prefix prefix s)
let make_call1 s =
Tezos_rpc.Context.make_call1 (Tezos_rpc.Service.prefix prefix s)
let make_call2 s =
Tezos_rpc.Context.make_call2 (Tezos_rpc.Service.prefix prefix s)
end
module Global = struct
open Tezos_rpc.Path
include Make_services (struct
type prefix = unit
let prefix = open_root / "global"
end)
let sc_rollup_address =
Tezos_rpc.Service.get_service
~description:"Smart rollup address"
~query:Tezos_rpc.Query.empty
~output:Address.encoding
(path / "smart_rollup_address")
let current_tezos_head =
Tezos_rpc.Service.get_service
~description:"Tezos head known to the smart rollup node"
~query:Tezos_rpc.Query.empty
~output:(Data_encoding.option Block_hash.encoding)
(path / "tezos_head")
let current_tezos_level =
Tezos_rpc.Service.get_service
~description:"Tezos level known to the smart rollup node"
~query:Tezos_rpc.Query.empty
~output:(Data_encoding.option Data_encoding.int32)
(path / "tezos_level")
let last_stored_commitment =
Tezos_rpc.Service.get_service
~description:"Last commitment computed by the node"
~query:Tezos_rpc.Query.empty
~output:(Data_encoding.option Encodings.commitment_with_hash)
(path / "last_stored_commitment")
let global_block_watcher =
Tezos_rpc.Service.get_service
~description:"Monitor and streaming the L2 blocks"
~query:Tezos_rpc.Query.empty
~output:Sc_rollup_block.encoding
(path / "monitor_blocks")
end
module Local = struct
open Tezos_rpc.Path
include Make_services (struct
type prefix = unit
let prefix = open_root / "local"
end)
let last_published_commitment =
Tezos_rpc.Service.get_service
~description:"Last commitment published by the node"
~query:Tezos_rpc.Query.empty
~output:
(Data_encoding.option Encodings.commitment_with_hash_and_level_infos)
(path / "last_published_commitment")
let commitment =
Tezos_rpc.Service.get_service
~description:"Commitment computed and published by the node"
~query:Tezos_rpc.Query.empty
~output:
(Data_encoding.option Encodings.commitment_with_hash_and_level_infos)
(path / "commitments" /: Arg.commitment_hash)
let gc_info =
Tezos_rpc.Service.get_service
~description:"Information about garbage collection"
~query:Tezos_rpc.Query.empty
~output:Encodings.gc_info
(path / "gc_info")
let injection =
Tezos_rpc.Service.post_service
~description:"Inject messages in the batcher's queue"
~query:Tezos_rpc.Query.empty
~input:
Data_encoding.(
def
"messages"
~description:"Messages to inject"
(list L2_message.content_encoding))
~output:
Data_encoding.(
def
"message_ids"
~description:"Ids of injected L2 messages"
(list L2_message.Id.encoding))
(path / "batcher" / "injection")
let batcher_queue =
Tezos_rpc.Service.get_service
~description:"List messages present in the batcher's queue"
~query:Tezos_rpc.Query.empty
~output:Encodings.batcher_queue
(path / "batcher" / "queue")
let batcher_message =
Tezos_rpc.Service.get_service
~description:"Retrieve an L2 message and its status"
~query:Tezos_rpc.Query.empty
~output:Encodings.message_status_output
(path / "batcher" / "queue" /: Arg.l2_message_id)
let synchronized =
Tezos_rpc.Service.get_service
~description:
"Wait for the node to have synchronized its L2 chain with the L1 \
chain, streaming its progress."
~query:Tezos_rpc.Query.empty
~output:Encodings.synchronization_result
(path / "synchronized")
end
module Root = struct
open Tezos_rpc.Path
include Make_services (struct
type prefix = unit
let prefix = root
end)
let health =
Tezos_rpc.Service.get_service
~description:
"Returns an empty response if the rollup node can answer requests"
~query:Tezos_rpc.Query.empty
~output:Data_encoding.unit
(path / "health")
let version =
Tezos_rpc.Service.get_service
~description:"Returns the version information of the rollup node"
~query:Tezos_rpc.Query.empty
~output:Encodings.version
(path / "version")
let ocaml_gc =
Tezos_rpc.Service.get_service
~description:"Gets stats from the OCaml Garbage Collector"
~query:Tezos_rpc.Query.empty
~output:Encodings.ocaml_gc_stat_encoding
(path / "stats" / "ocaml_gc")
let memory =
Tezos_rpc.Service.get_service
~description:"Gets memory usage stats"
~query:Tezos_rpc.Query.empty
~output:Encodings.mem_stat_encoding
(path / "stats" / "memory")
let openapi =
Tezos_rpc.Service.get_service
~description:"OpenAPI specification of RPCs for rollup node"
~query:Query.proto_query
~output:Data_encoding.json
(path / "openapi")
end
module Admin = struct
open Tezos_rpc.Path
include Make_services (struct
type prefix = unit
let prefix = open_root / "admin"
end)
let injector_queues_total =
Tezos_rpc.Service.get_service
~description:"Get total operations queued in injectors"
~query:Tezos_rpc.Query.empty
~output:Encodings.injector_queues_total
(path / "injector" / "queues" / "total")
let injector_queues =
Tezos_rpc.Service.get_service
~description:"Get operation queues of injectors"
~query:Query.operation_tag_query
~output:Encodings.injector_queues
(path / "injector" / "queues")
let clear_injector_queues =
Tezos_rpc.Service.delete_service
~description:"Clear operation queues of injectors"
~query:Query.operation_tag_query
~output:Data_encoding.unit
(path / "injector" / "queues")
end