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
(** This module is a copy of
[src/proto_016_PtMumbai/lib_sc_rollup_node/store.ml], which contains the
store for the Mumbai rollup node. *)
include Store_sigs
include Store_utils
let version = Store_version.V0
module Irmin_store = struct
module IStore = Irmin_store.Make (struct
let name = "Tezos smart rollup node"
end)
include IStore
include Store_utils.Make (IStore)
end
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)
(** L2 blocks *)
module L2_blocks =
Indexed_store.Make_indexed_file
(struct
let name = "l2_blocks"
end)
(struct
include Tezos_store_shared.Block_key
let pp = Block_hash.pp
end)
(struct
type t = (unit, unit) Sc_rollup_block.block
let name = "sc_rollup_block_info"
let encoding =
Sc_rollup_block.block_encoding Data_encoding.unit Data_encoding.unit
end)
(** Unaggregated messages per block *)
module Messages =
Indexed_store.Make_indexed_file
(struct
let name = "messages"
end)
(Make_hash_index_key (Octez_smart_rollup.Merkelized_payload_hashes_hash))
(struct
type t = string list
let name = "messages_list"
let encoding = Data_encoding.(list @@ dynamic_size (Variable.string' Hex))
end)
(** Inbox state for each block *)
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.V1.t
let name = "inbox"
let encoding = Octez_smart_rollup.Inbox.V1.encoding
include Add_empty_header
end)
module Commitments =
Indexed_store.Make_indexable
(struct
let name = "commitments"
end)
(Make_hash_index_key (Octez_smart_rollup.Commitment.Hash))
(Indexed_store.Make_index_value (Indexed_store.Make_fixed_encodable (struct
include Octez_smart_rollup.Commitment.V1
let name = "commitment"
end)))
module Commitments_published_at_level = struct
type element = {
first_published_at_level : int32;
published_at_level : int32 option;
}
let element_encoding =
let open Data_encoding in
let opt_level_encoding =
conv
(function None -> -1l | Some l -> l)
(fun l -> if l = -1l then None else Some l)
int32
in
conv
(fun {first_published_at_level; published_at_level} ->
(first_published_at_level, published_at_level))
(fun (first_published_at_level, published_at_level) ->
{first_published_at_level; published_at_level})
@@ obj2
(req "first_published_at_level" int32)
(req "published_at_level" opt_level_encoding)
include
Indexed_store.Make_indexable
(struct
let name = "commitments_published_at_level"
end)
(Make_hash_index_key (Octez_smart_rollup.Commitment.Hash))
(Indexed_store.Make_index_value (Indexed_store.Make_fixed_encodable (struct
type t = element
let name = "published_levels"
let encoding = element_encoding
end)))
end
module L2_head = Indexed_store.Make_singleton (struct
type t = Sc_rollup_block.t
let name = "l2_head"
let encoding = Sc_rollup_block.encoding
end)
module Last_finalized_level = Indexed_store.Make_singleton (struct
type t = int32
let name = "finalized_level"
let encoding = Data_encoding.int32
end)
(** Table from L1 levels to blocks hashes. *)
module Levels_to_hashes =
Indexed_store.Make_indexable
(struct
let name = "tezos_levels"
end)
(Indexed_store.Make_index_key (struct
type t = int32
let encoding = Data_encoding.int32
let name = "level"
let fixed_size = 4
let equal = Int32.equal
end))
(Tezos_store_shared.Block_key)
(** stores slots whose data have been considered and pages stored to disk (if
they are confirmed). *)
module Dal_processed_slots =
Irmin_store.Make_nested_map
(struct
let path = ["dal"; "processed_slots"]
end)
(struct
type key = Block_hash.t
let to_path_representation = Block_hash.to_b58check
end)
(struct
type key = Octez_smart_rollup.Dal.Slot_index.t
let encoding = Octez_smart_rollup.Dal.Slot_index.encoding
let compare = Compare.Int.compare
let name = "slot_index"
end)
(struct
type value = [`Confirmed | `Unconfirmed]
let name = "slot_processing_status"
let encoding =
let open Data_encoding in
let mk_case constr ~tag ~title =
case
~title
(Tag tag)
(obj1 (req "kind" (constant title)))
(fun x -> if x = constr then Some () else None)
(fun () -> constr)
in
union
~tag_size:`Uint8
[
mk_case `Confirmed ~tag:0 ~title:"Confirmed";
mk_case `Unconfirmed ~tag:1 ~title:"Unconfirmed";
]
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;
levels_to_hashes : 'a Levels_to_hashes.t;
irmin_store : 'a Irmin_store.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;
levels_to_hashes;
irmin_store;
} :
_ 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;
levels_to_hashes = Levels_to_hashes.readonly levels_to_hashes;
irmin_store = Irmin_store.readonly irmin_store;
}
let close
({
l2_blocks;
messages;
inboxes;
commitments;
commitments_published_at_level;
l2_head = _;
last_finalized_level = _;
levels_to_hashes;
irmin_store;
} :
_ 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")
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* levels_to_hashes =
Levels_to_hashes.load
mode
~index_buffer_size
~path:(path "levels_to_hashes")
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;
levels_to_hashes;
irmin_store;
}
let iter_l2_blocks ?progress _ {l2_blocks; l2_head; _} 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 -> fun f -> f (fun _ -> Lwt.return_unit)
| Some message ->
let progress_bar = Progress_bar.spinner ~message in
Progress_bar.Lwt.with_reporter progress_bar
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 (_ : _ t) ~level:_ = failwith "GC is not implemented for store V0"
let wait_gc_completion (_ : _ t) =
Lwt.fail_with "GC is not implemented for store V0"