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
include Store_sigs
include Store_utils
include Store_v1
let version = Store_version.V2
module Make_hash_index_key (H : Tezos_crypto.Intfs.HASH) =
Indexed_store.Make_index_key (struct
include Indexed_store.Make_fixed_encodable (H)
let equal = H.equal
end)
(** Unaggregated messages per block *)
module Messages =
Indexed_store.Make_indexed_file
(struct
let name = "messages"
end)
(Make_hash_index_key (Merkelized_payload_hashes_hash))
(struct
type t = string list
let name = "messages_list"
let encoding = Data_encoding.(list @@ dynamic_size (Variable.string' Hex))
end)
(** Versioned inboxes *)
module Inboxes =
Indexed_store.Make_simple_indexed_file
(struct
let name = "inboxes"
end)
(Make_hash_index_key (Octez_smart_rollup.Inbox.Hash))
(struct
type t = Octez_smart_rollup.Inbox.t
let encoding =
Data_encoding.conv
Octez_smart_rollup.Inbox.to_versioned
Octez_smart_rollup.Inbox.of_versioned
Octez_smart_rollup.Inbox.versioned_encoding
let name = "inbox"
include Add_empty_header
end)
(** Versioned commitments *)
module Commitments =
Indexed_store.Make_simple_indexed_file
(struct
let name = "commitments"
end)
(Make_hash_index_key (Octez_smart_rollup.Commitment.Hash))
(struct
type t = Octez_smart_rollup.Commitment.t
let encoding =
Data_encoding.conv
Octez_smart_rollup.Commitment.to_versioned
Octez_smart_rollup.Commitment.of_versioned
Octez_smart_rollup.Commitment.versioned_encoding
let name = "commitment"
include Add_empty_header
end)
(** Single commitment for LCC. *)
module Lcc = struct
type lcc = {commitment : Commitment.Hash.t; level : int32}
include Indexed_store.Make_singleton (struct
type t = lcc
let name = "lcc"
let encoding =
let open Data_encoding in
conv
(fun {commitment; level} -> (commitment, level))
(fun (commitment, level) -> {commitment; level})
@@ obj2
(req "commitment" Octez_smart_rollup.Commitment.Hash.encoding)
(req "level" int32)
end)
end
(** Single commitment for LPC. *)
module Lpc = Indexed_store.Make_singleton (struct
type t = Octez_smart_rollup.Commitment.t
let encoding =
Data_encoding.conv
Octez_smart_rollup.Commitment.to_versioned
Octez_smart_rollup.Commitment.of_versioned
Octez_smart_rollup.Commitment.versioned_encoding
let name = "lpc"
end)
(** Versioned slot headers *)
(** Versioned Confirmed DAL slots history *)
module Dal_confirmed_slots_history =
Irmin_store.Make_append_only_map
(struct
let path = ["dal"; "confirmed_slots_history"]
end)
(struct
type key = Block_hash.t
let to_path_representation = Block_hash.to_b58check
end)
(struct
type value = Octez_smart_rollup.Dal.Slot_history.t
let name = "dal_slot_histories"
let encoding =
Data_encoding.conv
Octez_smart_rollup.Dal.Slot_history.to_versioned
Octez_smart_rollup.Dal.Slot_history.of_versioned
Octez_smart_rollup.Dal.Slot_history.versioned_encoding
end)
(** Versioned Confirmed DAL slots histories cache. *)
module Dal_confirmed_slots_histories =
Irmin_store.Make_append_only_map
(struct
let path = ["dal"; "confirmed_slots_histories_cache"]
end)
(struct
type key = Block_hash.t
let to_path_representation = Block_hash.to_b58check
end)
(struct
type value = Octez_smart_rollup.Dal.Slot_history_cache.t
let name = "dal_slot_histories"
let encoding =
Data_encoding.conv
Octez_smart_rollup.Dal.Slot_history_cache.to_versioned
Octez_smart_rollup.Dal.Slot_history_cache.of_versioned
Octez_smart_rollup.Dal.Slot_history_cache.versioned_encoding
end)
module Protocols = struct
type level = First_known of int32 | Activation_level of int32
type proto_info = {
level : level;
proto_level : int;
protocol : Protocol_hash.t;
}
type value = proto_info list
let level_encoding =
let open Data_encoding in
conv
(function First_known l -> (l, false) | Activation_level l -> (l, true))
(function l, false -> First_known l | l, true -> Activation_level l)
@@ obj2 (req "level" int32) (req "activates" bool)
let proto_info_encoding =
let open Data_encoding in
conv
(fun {level; proto_level; protocol} -> (level, proto_level, protocol))
(fun (level, proto_level, protocol) -> {level; proto_level; protocol})
@@ obj3
(req "level" level_encoding)
(req "proto_level" int31)
(req "protocol" Protocol_hash.encoding)
include Indexed_store.Make_singleton (struct
type t = value
let name = "protocols"
let level_encoding =
let open Data_encoding in
conv
(function
| First_known l -> (l, false) | Activation_level l -> (l, true))
(function l, false -> First_known l | l, true -> Activation_level l)
@@ obj2 (req "level" int32) (req "activates" bool)
let proto_info_encoding =
let open Data_encoding in
conv
(fun {level; proto_level; protocol} -> (level, proto_level, protocol))
(fun (level, proto_level, protocol) -> {level; proto_level; protocol})
@@ obj3
(req "level" level_encoding)
(req "proto_level" int31)
(req "protocol" Protocol_hash.encoding)
let encoding = Data_encoding.list proto_info_encoding
end)
end
module Gc_levels = struct
type levels = {last_gc_level : int32; first_available_level : int32}
type value = levels
include Indexed_store.Make_singleton (struct
type t = levels
let name = "gc_levels"
let encoding : t Data_encoding.t =
let open Data_encoding in
conv
(fun {last_gc_level; first_available_level} ->
(last_gc_level, first_available_level))
(fun (last_gc_level, first_available_level) ->
{last_gc_level; first_available_level})
@@ obj2 (req "last_gc_level" int32) (req "first_available_level" int32)
end)
end
module History_mode = Indexed_store.Make_singleton (struct
type t = Configuration.history_mode
let name = "history_mode"
let encoding = Configuration.history_mode_encoding
end)
type 'a store = {
l2_blocks : 'a L2_blocks.t;
messages : 'a Messages.t;
inboxes : 'a Inboxes.t;
commitments : 'a Commitments.t;
commitments_published_at_level : 'a Commitments_published_at_level.t;
l2_head : 'a L2_head.t;
last_finalized_level : 'a Last_finalized_level.t;
lcc : 'a Lcc.t;
lpc : 'a Lpc.t;
levels_to_hashes : 'a Levels_to_hashes.t;
protocols : 'a Protocols.t;
irmin_store : 'a Irmin_store.t;
gc_levels : 'a Gc_levels.t;
history_mode : 'a History_mode.t;
}
type 'a t = ([< `Read | `Write > `Read] as 'a) store
type rw = Store_sigs.rw t
type ro = Store_sigs.ro t
let readonly
({
l2_blocks;
messages;
inboxes;
commitments;
commitments_published_at_level;
l2_head;
last_finalized_level;
lcc;
lpc;
levels_to_hashes;
protocols;
irmin_store;
gc_levels;
history_mode;
} :
_ t) : ro =
{
l2_blocks = L2_blocks.readonly l2_blocks;
messages = Messages.readonly messages;
inboxes = Inboxes.readonly inboxes;
commitments = Commitments.readonly commitments;
commitments_published_at_level =
Commitments_published_at_level.readonly commitments_published_at_level;
l2_head = L2_head.readonly l2_head;
last_finalized_level = Last_finalized_level.readonly last_finalized_level;
lcc = Lcc.readonly lcc;
lpc = Lpc.readonly lpc;
levels_to_hashes = Levels_to_hashes.readonly levels_to_hashes;
protocols = Protocols.readonly protocols;
irmin_store = Irmin_store.readonly irmin_store;
gc_levels = Gc_levels.readonly gc_levels;
history_mode = History_mode.readonly history_mode;
}
let close
({
l2_blocks;
messages;
inboxes;
commitments;
commitments_published_at_level;
l2_head = _;
last_finalized_level = _;
lcc = _;
lpc = _;
levels_to_hashes;
protocols = _;
irmin_store;
gc_levels = _;
history_mode = _;
} :
_ t) =
let open Lwt_result_syntax in
let+ () = L2_blocks.close l2_blocks
and+ () = Messages.close messages
and+ () = Inboxes.close inboxes
and+ () = Commitments.close commitments
and+ () = Commitments_published_at_level.close commitments_published_at_level
and+ () = Levels_to_hashes.close levels_to_hashes
and+ () = Irmin_store.close irmin_store in
()
let load (type a) (mode : a mode) ~index_buffer_size ~l2_blocks_cache_size
data_dir : a store tzresult Lwt.t =
let open Lwt_result_syntax in
let path name = Filename.concat data_dir name in
let cache_size = l2_blocks_cache_size in
let* l2_blocks =
L2_blocks.load mode ~index_buffer_size ~path:(path "l2_blocks") ~cache_size
in
let* messages =
Messages.load mode ~index_buffer_size ~path:(path "messages") ~cache_size
in
let* inboxes =
Inboxes.load mode ~index_buffer_size ~path:(path "inboxes") ~cache_size
in
let* commitments =
Commitments.load
mode
~index_buffer_size
~path:(path "commitments")
~cache_size
in
let* commitments_published_at_level =
Commitments_published_at_level.load
~index_buffer_size
mode
~path:(path "commitments_published_at_level")
in
let* l2_head = L2_head.load mode ~path:(path "l2_head") in
let* last_finalized_level =
Last_finalized_level.load mode ~path:(path "last_finalized_level")
in
let* lcc = Lcc.load mode ~path:(path "lcc") in
let* lpc = Lpc.load mode ~path:(path "lpc") in
let* levels_to_hashes =
Levels_to_hashes.load
mode
~index_buffer_size
~path:(path "levels_to_hashes")
in
let* protocols = Protocols.load mode ~path:(path "protocols") in
let* gc_levels = Gc_levels.load mode ~path:(path "gc_levels") in
let* history_mode = History_mode.load mode ~path:(path "history_mode") in
let+ irmin_store = Irmin_store.load mode (path "irmin_store") in
{
l2_blocks;
messages;
inboxes;
commitments;
commitments_published_at_level;
l2_head;
last_finalized_level;
lcc;
lpc;
levels_to_hashes;
protocols;
irmin_store;
gc_levels;
history_mode;
}
let first_available_level metadata store =
let open Lwt_result_syntax in
let* gc_levels = Gc_levels.read store.gc_levels in
match gc_levels with
| Some {first_available_level; _} -> return first_available_level
| None -> return metadata.Metadata.genesis_info.level
let iter_l2_blocks ?progress metadata ({l2_blocks; l2_head; _} as store) f =
let open Lwt_result_syntax in
let* head = L2_head.read l2_head in
match head with
| None ->
return_unit
| Some head ->
let* track_progress =
match progress with
| None -> return (fun f -> f (fun _ -> Lwt.return_unit))
| Some message ->
let+ first_level = first_available_level metadata store in
let progress_bar =
let total =
Int32.sub head.header.level first_level |> Int32.to_int
in
Progress_bar.progress_bar ~counter:`Int ~message total
in
fun f -> Progress_bar.Lwt.with_reporter progress_bar f
in
track_progress @@ fun count_progress ->
let rec loop hash =
let* block = L2_blocks.read l2_blocks hash in
match block with
| None ->
return_unit
| Some (block, ) ->
let* () = f {block with header} in
let*! () = count_progress 1 in
loop header.predecessor
in
loop head.header.block_hash
let gc_l2_blocks l2_blocks ~(head : Sc_rollup_block.t) ~level =
L2_blocks.gc
l2_blocks
(Indexed_store.Iterator
{
first = head.header.block_hash;
next =
(fun _hash (_content, ) ->
if header.Sc_rollup_block.level <= level then Lwt.return_none
else Lwt.return_some header.predecessor);
})
let gc_commitments commitments ~last_commitment ~level =
Commitments.gc
commitments
(Indexed_store.Iterator
{
first = last_commitment;
next =
(fun _hash (commitment, ()) ->
if commitment.Commitment.inbox_level <= level then Lwt.return_none
else Lwt.return_some commitment.predecessor);
})
let gc_levels_to_hashes levels_to_hashes ~(head : Sc_rollup_block.t) ~level =
Levels_to_hashes.gc
levels_to_hashes
(Indexed_store.Iterator
{
first = head.header.level;
next =
(fun blevel _bhash ->
if blevel <= level then Lwt.return_none
else Lwt.return_some (Int32.pred blevel));
})
let gc_messages messages l2_blocks ~(head : Sc_rollup_block.t) ~level =
Messages.gc
messages
(Indexed_store.Iterator
{
first = head.header.inbox_witness;
next =
(fun _witness (_msgs, predecessor) ->
let open Lwt_syntax in
let* pred_inbox_witness =
let open Lwt_result_syntax in
let+ pred = L2_blocks.header l2_blocks predecessor in
match pred with
| Some {level = pred_level; inbox_witness; _}
when pred_level >= level ->
Some inbox_witness
| _ -> None
in
match pred_inbox_witness with
| Error e ->
Fmt.failwith
"Could not compute messages witness for GC: %a"
pp_print_trace
e
| Ok witness -> return witness);
})
let gc_commitments_published_at_level commitments_published_at_level commitments
lpc ~level =
let open Lwt_result_syntax in
let* lpc = Lpc.read lpc in
match lpc with
| None -> return_unit
| Some lpc ->
Commitments_published_at_level.gc
commitments_published_at_level
(Indexed_store.Iterator
{
first = Commitment.hash lpc;
next =
(fun commitment_hash _ ->
let open Lwt_syntax in
let* commitment =
Commitments.read commitments commitment_hash
in
match commitment with
| Error e ->
Fmt.failwith
"Could not compute commitment published at level for \
GC: %a"
pp_print_trace
e
| Ok None -> return_none
| Ok (Some (commitment, ())) ->
if commitment.Commitment.inbox_level <= level then
return_none
else return_some commitment.predecessor);
})
let gc_inboxes inboxes ~(head : Sc_rollup_block.t) ~level =
Inboxes.gc
inboxes
(Indexed_store.Iterator
{
first = head.header.inbox_hash;
next =
(fun _inbox_hash (inbox, ()) ->
let open Lwt_syntax in
if inbox.level <= level then return_none
else
return (Inbox.Skip_list.back_pointer inbox.old_levels_messages 0));
})
let gc
({
l2_blocks;
messages;
inboxes;
commitments;
commitments_published_at_level;
l2_head;
last_finalized_level = _;
lcc = _;
lpc;
levels_to_hashes;
irmin_store = _;
protocols = _;
gc_levels = _;
history_mode = _;
} :
_ t) ~level =
let open Lwt_result_syntax in
let* head = L2_head.read l2_head in
match head with
| None -> return_unit
| Some head ->
let last_commitment =
Sc_rollup_block.most_recent_commitment head.header
in
let* () =
tzjoin
[
gc_l2_blocks l2_blocks ~head ~level;
gc_commitments commitments ~last_commitment ~level;
gc_levels_to_hashes levels_to_hashes ~head ~level;
gc_messages messages l2_blocks ~head ~level;
gc_commitments_published_at_level
commitments_published_at_level
commitments
lpc
~level;
gc_inboxes inboxes ~head ~level;
]
in
return_unit
let wait_gc_completion
({
l2_blocks;
messages;
inboxes;
commitments;
commitments_published_at_level;
l2_head = _;
last_finalized_level = _;
lcc = _;
lpc = _;
levels_to_hashes;
irmin_store = _;
protocols = _;
gc_levels = _;
history_mode = _;
} :
_ t) =
let open Lwt_syntax in
let* () = L2_blocks.wait_gc_completion l2_blocks
and* () = Messages.wait_gc_completion messages
and* () = Inboxes.wait_gc_completion inboxes
and* () = Commitments.wait_gc_completion commitments
and* () =
Commitments_published_at_level.wait_gc_completion
commitments_published_at_level
and* () = Levels_to_hashes.wait_gc_completion levels_to_hashes in
return_unit
let is_gc_finished
({
l2_blocks;
messages;
inboxes;
commitments;
commitments_published_at_level;
l2_head = _;
last_finalized_level = _;
lcc = _;
lpc = _;
levels_to_hashes;
irmin_store = _;
protocols = _;
gc_levels = _;
history_mode = _;
} :
_ t) =
L2_blocks.is_gc_finished l2_blocks
&& Messages.is_gc_finished messages
&& Inboxes.is_gc_finished inboxes
&& Commitments.is_gc_finished commitments
&& Commitments_published_at_level.is_gc_finished
commitments_published_at_level
&& Levels_to_hashes.is_gc_finished levels_to_hashes