Source file sc_rollup_storage.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
type error +=
| Sc_rollup_already_staked
| Sc_rollup_disputed
| Sc_rollup_does_not_exist of Sc_rollup_repr.t
| Sc_rollup_no_conflict
| Sc_rollup_no_stakers
| Sc_rollup_not_staked
| Sc_rollup_not_staked_on_lcc
| Sc_rollup_parent_not_lcc
| Sc_rollup_remove_lcc
| Sc_rollup_staker_backtracked
| Sc_rollup_too_far_ahead
| Sc_rollup_too_recent
|
Sc_rollup_unknown_commitment of
Sc_rollup_repr.Commitment_hash.t
| Sc_rollup_bad_inbox_level
| Sc_rollup_max_number_of_available_messages_reached
let () =
register_error_kind
`Temporary
~id:"Sc_rollup_max_number_of_available_messages_reached"
~title:"Maximum number of available messages reached"
~description:"Maximum number of available messages reached"
Data_encoding.unit
(function
| Sc_rollup_max_number_of_available_messages_reached -> Some ()
| _ -> None)
(fun () -> Sc_rollup_max_number_of_available_messages_reached) ;
let description = "Already staked." in
register_error_kind
`Temporary
~id:"Sc_rollup_already_staked"
~title:"Already staked"
~description
~pp:(fun ppf () -> Format.fprintf ppf "%s" description)
Data_encoding.empty
(function Sc_rollup_already_staked -> Some () | _ -> None)
(fun () -> Sc_rollup_already_staked) ;
let description = "Attempted to cement a disputed commitment." in
register_error_kind
`Temporary
~id:"Sc_rollup_disputed"
~title:"Commitment disputed"
~description
~pp:(fun ppf () -> Format.fprintf ppf "%s" description)
Data_encoding.empty
(function Sc_rollup_disputed -> Some () | _ -> None)
(fun () -> Sc_rollup_disputed) ;
let description = "Attempted to use a rollup that has not been originated." in
register_error_kind
`Temporary
~id:"Sc_rollup_does_not_exist"
~title:"Rollup does not exist"
~description
~pp:(fun ppf x ->
Format.fprintf ppf "Rollup %a does not exist" Sc_rollup_repr.pp x)
Data_encoding.(obj1 (req "rollup" Sc_rollup_repr.encoding))
(function Sc_rollup_does_not_exist x -> Some x | _ -> None)
(fun x -> Sc_rollup_does_not_exist x) ;
let description = "No conflict." in
register_error_kind
`Temporary
~id:"Sc_rollup_no_conflict"
~title:"No conflict"
~description
~pp:(fun ppf () -> Format.fprintf ppf "%s" description)
Data_encoding.empty
(function Sc_rollup_no_conflict -> Some () | _ -> None)
(fun () -> Sc_rollup_no_conflict) ;
let description = "No stakers." in
register_error_kind
`Temporary
~id:"Sc_rollup_no_stakers"
~title:"No stakers"
~description
~pp:(fun ppf () -> Format.fprintf ppf "%s" description)
Data_encoding.empty
(function Sc_rollup_no_stakers -> Some () | _ -> None)
(fun () -> Sc_rollup_no_stakers) ;
let description = "Unknown staker." in
register_error_kind
`Temporary
~id:"Sc_rollup_not_staked"
~title:"Unknown staker"
~description
~pp:(fun ppf () -> Format.fprintf ppf "%s" description)
Data_encoding.empty
(function Sc_rollup_not_staked -> Some () | _ -> None)
(fun () -> Sc_rollup_not_staked) ;
let description =
"Attempted to withdraw while not staked on the last cemented commitment."
in
register_error_kind
`Temporary
~id:"Sc_rollup_not_staked_on_lcc"
~title:"Rollup not staked on LCC"
~description
~pp:(fun ppf () -> Format.fprintf ppf "%s" description)
Data_encoding.empty
(function Sc_rollup_not_staked_on_lcc -> Some () | _ -> None)
(fun () -> Sc_rollup_not_staked_on_lcc) ;
let description = "Parent is not cemented." in
register_error_kind
`Temporary
~id:"Sc_rollup_parent_not_lcc"
~title:"Parent not cemented"
~description
~pp:(fun ppf () -> Format.fprintf ppf "%s" description)
Data_encoding.empty
(function Sc_rollup_parent_not_lcc -> Some () | _ -> None)
(fun () -> Sc_rollup_parent_not_lcc) ;
let description = "Can not remove a cemented commitment." in
register_error_kind
`Temporary
~id:"Sc_rollup_remove_lcc"
~title:"Can not remove cemented"
~description
~pp:(fun ppf () -> Format.fprintf ppf "%s" description)
Data_encoding.empty
(function Sc_rollup_remove_lcc -> Some () | _ -> None)
(fun () -> Sc_rollup_remove_lcc) ;
let description = "Staker backtracked." in
register_error_kind
`Temporary
~id:"Sc_rollup_staker_backtracked"
~title:"Staker backtracked"
~description
~pp:(fun ppf () -> Format.fprintf ppf "%s" description)
Data_encoding.empty
(function Sc_rollup_staker_backtracked -> Some () | _ -> None)
(fun () -> Sc_rollup_staker_backtracked) ;
let description =
"Commitment is too far ahead of the last cemented commitment."
in
register_error_kind
`Temporary
~id:"Sc_rollup_too_far_ahead"
~title:"Commitment too far ahead"
~description
~pp:(fun ppf () -> Format.fprintf ppf "%s" description)
Data_encoding.empty
(function Sc_rollup_too_far_ahead -> Some () | _ -> None)
(fun () -> Sc_rollup_too_far_ahead) ;
let description =
"Attempted to cement a commitment before its refutation deadline."
in
register_error_kind
`Temporary
~id:"Sc_rollup_too_recent"
~title:"Commitment too recent"
~description
~pp:(fun ppf () -> Format.fprintf ppf "%s" description)
Data_encoding.empty
(function Sc_rollup_too_recent -> Some () | _ -> None)
(fun () -> Sc_rollup_too_recent) ;
let description = "Unknown commitment." in
register_error_kind
`Temporary
~id:"Sc_rollup_unknown_commitment"
~title:"Rollup does not exist"
~description
~pp:(fun ppf x ->
Format.fprintf
ppf
"Commitment %a does not exist"
Sc_rollup_repr.Commitment_hash.pp
x)
Data_encoding.(
obj1 (req "commitment" Sc_rollup_repr.Commitment_hash.encoding))
(function Sc_rollup_unknown_commitment x -> Some x | _ -> None)
(fun x -> Sc_rollup_unknown_commitment x) ;
let description = "Attempted to commit to a bad inbox level." in
register_error_kind
`Temporary
~id:"Sc_rollup_bad_inbox_level"
~title:"Committed too soon"
~description
~pp:(fun ppf () -> Format.fprintf ppf "%s" description)
Data_encoding.empty
(function Sc_rollup_bad_inbox_level -> Some () | _ -> None)
(fun () -> Sc_rollup_bad_inbox_level) ;
()
(** To be removed once [Lwt_tzresult_syntax] is in the environment. *)
module Lwt_tzresult_syntax = struct
let ( let* ) = ( >>=? )
let return = return
end
module Store = Storage.Sc_rollup
module Commitment = Sc_rollup_repr.Commitment
module Commitment_hash = Sc_rollup_repr.Commitment_hash
let originate ctxt ~kind ~boot_sector =
Raw_context.increment_origination_nonce ctxt >>?= fun (ctxt, nonce) ->
let level = Raw_context.current_level ctxt in
Sc_rollup_repr.Address.from_nonce nonce >>?= fun address ->
Storage.Sc_rollup.PVM_kind.add ctxt address kind >>= fun ctxt ->
Storage.Sc_rollup.Initial_level.add
ctxt
address
(Level_storage.current ctxt).level
>>= fun ctxt ->
Storage.Sc_rollup.Boot_sector.add ctxt address boot_sector >>= fun ctxt ->
let inbox = Sc_rollup_inbox_repr.empty address level.level in
Storage.Sc_rollup.Inbox.init ctxt address inbox >>=? fun (ctxt, size_diff) ->
Store.Last_cemented_commitment.init ctxt address Commitment_hash.zero
>>=? fun (ctxt, lcc_size_diff) ->
Store.Staker_count.init ctxt address 0l >>=? fun (ctxt, stakers_size_diff) ->
let addresses_size = 2 * Sc_rollup_repr.Address.size in
let stored_kind_size = 2 in
let boot_sector_size =
Data_encoding.Binary.length Data_encoding.string boot_sector
in
let origination_size = Constants_storage.sc_rollup_origination_size ctxt in
let size =
Z.of_int
(origination_size + stored_kind_size + boot_sector_size + addresses_size
+ size_diff + lcc_size_diff + stakers_size_diff)
in
return (address, size, ctxt)
let kind ctxt address = Storage.Sc_rollup.PVM_kind.find ctxt address
let last_cemented_commitment ctxt rollup =
let open Lwt_tzresult_syntax in
let* (ctxt, res) = Store.Last_cemented_commitment.find ctxt rollup in
match res with
| None -> fail (Sc_rollup_does_not_exist rollup)
| Some lcc -> return (lcc, ctxt)
(** Try to consume n messages. *)
let consume_n_messages ctxt rollup n =
let open Lwt_tzresult_syntax in
let* (ctxt, inbox) = Storage.Sc_rollup.Inbox.get ctxt rollup in
Sc_rollup_inbox_repr.consume_n_messages n inbox >>?= function
| None -> return ctxt
| Some inbox ->
let* (ctxt, size) = Storage.Sc_rollup.Inbox.update ctxt rollup inbox in
assert (Compare.Int.(size <= 0)) ;
return ctxt
let inbox ctxt rollup =
let open Lwt_tzresult_syntax in
let* (ctxt, res) = Storage.Sc_rollup.Inbox.find ctxt rollup in
match res with
| None -> fail (Sc_rollup_does_not_exist rollup)
| Some inbox -> return (inbox, ctxt)
let assert_inbox_size_ok ctxt next_size =
let max_size = Constants_storage.sc_rollup_max_available_messages ctxt in
fail_unless
Compare.Z.(next_size <= Z.of_int max_size)
Sc_rollup_max_number_of_available_messages_reached
let add_messages ctxt rollup messages =
let open Raw_context in
inbox ctxt rollup >>=? fun (inbox, ctxt) ->
let next_size =
Z.add
(Sc_rollup_inbox_repr.number_of_available_messages inbox)
(Z.of_int (List.length messages))
in
assert_inbox_size_ok ctxt next_size >>=? fun () ->
Sc_rollup_in_memory_inbox.current_messages ctxt rollup
|> fun current_messages ->
let {Level_repr.level; _} = Raw_context.current_level ctxt in
Sc_rollup_inbox_repr.(
add_messages_no_history inbox level messages current_messages)
>>=? fun (current_messages, inbox) ->
Sc_rollup_in_memory_inbox.set_current_messages ctxt rollup current_messages
|> fun ctxt ->
Storage.Sc_rollup.Inbox.update ctxt rollup inbox >>=? fun (ctxt, size) ->
return (inbox, Z.of_int size, ctxt)
let get_commitment_internal ctxt rollup commitment =
let open Lwt_tzresult_syntax in
let* (ctxt, res) = Store.Commitments.find (ctxt, rollup) commitment in
match res with
| None -> fail (Sc_rollup_unknown_commitment commitment)
| Some commitment -> return (commitment, ctxt)
let get_commitment ctxt rollup commitment =
let open Lwt_tzresult_syntax in
let* (_lcc, ctxt) = last_cemented_commitment ctxt rollup in
get_commitment_internal ctxt rollup commitment
let get_predecessor ctxt rollup node =
let open Lwt_tzresult_syntax in
let* (commitment, ctxt) = get_commitment_internal ctxt rollup node in
return (commitment.predecessor, ctxt)
let find_staker ctxt rollup staker =
let open Lwt_tzresult_syntax in
let* (ctxt, res) = Store.Stakers.find (ctxt, rollup) staker in
match res with
| None -> fail Sc_rollup_not_staked
| Some branch -> return (branch, ctxt)
let modify_staker_count ctxt rollup f =
let open Lwt_tzresult_syntax in
let* (ctxt, maybe_count) = Store.Staker_count.find ctxt rollup in
let count = Option.value ~default:0l maybe_count in
let* (ctxt, size_diff, _was_bound) =
Store.Staker_count.add ctxt rollup (f count)
in
assert (Compare.Int.(size_diff = 0)) ;
return ctxt
let get_commitment_stake_count ctxt rollup node =
let open Lwt_tzresult_syntax in
let* (ctxt, maybe_staked_on_commitment) =
Store.Commitment_stake_count.find (ctxt, rollup) node
in
return (Option.value ~default:0l maybe_staked_on_commitment, ctxt)
(** [set_commitment_added ctxt rollup node current] sets the commitment
addition time of [node] to [current] iff the commitment time was
not previously set, and leaves it unchanged otherwise.
*)
let set_commitment_added ctxt rollup node new_value =
let open Lwt_tzresult_syntax in
let* (ctxt, res) = Store.Commitment_added.find (ctxt, rollup) node in
let new_value =
match res with None -> new_value | Some old_value -> old_value
in
let* (ctxt, size_diff, _was_bound) =
Store.Commitment_added.add (ctxt, rollup) node new_value
in
return (size_diff, ctxt)
let deallocate ctxt rollup node =
let open Lwt_tzresult_syntax in
if Commitment_hash.(node = zero) then return ctxt
else
let* (ctxt, _size_freed) =
Store.Commitments.remove_existing (ctxt, rollup) node
in
let* (ctxt, _size_freed) =
Store.Commitment_added.remove_existing (ctxt, rollup) node
in
let* (ctxt, _size_freed) =
Store.Commitment_stake_count.remove_existing (ctxt, rollup) node
in
return ctxt
let modify_commitment_stake_count ctxt rollup node f =
let open Lwt_tzresult_syntax in
let* (count, ctxt) = get_commitment_stake_count ctxt rollup node in
let new_count = f count in
let* (ctxt, size_diff, _was_bound) =
Store.Commitment_stake_count.add (ctxt, rollup) node new_count
in
return (new_count, size_diff, ctxt)
let increase_commitment_stake_count ctxt rollup node =
let open Lwt_tzresult_syntax in
let* (_new_count, size_diff, ctxt) =
modify_commitment_stake_count ctxt rollup node Int32.succ
in
return (size_diff, ctxt)
let decrease_commitment_stake_count ctxt rollup node =
let open Lwt_tzresult_syntax in
let* (new_count, _size_diff, ctxt) =
modify_commitment_stake_count ctxt rollup node Int32.pred
in
if Compare.Int32.(new_count <= 0l) then deallocate ctxt rollup node
else return ctxt
let deposit_stake ctxt rollup staker =
let open Lwt_tzresult_syntax in
let* (lcc, ctxt) = last_cemented_commitment ctxt rollup in
let* (ctxt, res) = Store.Stakers.find (ctxt, rollup) staker in
match res with
| None ->
let* (ctxt, _size) = Store.Stakers.init (ctxt, rollup) staker lcc in
let* ctxt = modify_staker_count ctxt rollup Int32.succ in
return ctxt
| Some _ -> fail Sc_rollup_already_staked
let withdraw_stake ctxt rollup staker =
let open Lwt_tzresult_syntax in
let* (lcc, ctxt) = last_cemented_commitment ctxt rollup in
let* (ctxt, res) = Store.Stakers.find (ctxt, rollup) staker in
match res with
| None -> fail Sc_rollup_not_staked
| Some staked_on_commitment ->
if Commitment_hash.(staked_on_commitment = lcc) then
let* (ctxt, _size_freed) =
Store.Stakers.remove_existing (ctxt, rollup) staker
in
let* ctxt = modify_staker_count ctxt rollup Int32.pred in
modify_staker_count ctxt rollup Int32.pred
else fail Sc_rollup_not_staked_on_lcc
let sc_rollup_max_lookahead = 30_000l
let sc_rollup_commitment_frequency =
20
let sc_rollup_commitment_storage_size_in_bytes = 84
let assert_commitment_not_too_far_ahead ctxt rollup lcc commitment =
let open Lwt_tzresult_syntax in
let* (ctxt, min_level) =
if Commitment_hash.(lcc = zero) then
let* level = Store.Initial_level.get ctxt rollup in
return (ctxt, level)
else
let* (lcc, ctxt) = get_commitment_internal ctxt rollup lcc in
return (ctxt, Commitment.(lcc.inbox_level))
in
let max_level = Commitment.(commitment.inbox_level) in
if
Compare.Int32.(
sc_rollup_max_lookahead < Raw_level_repr.diff max_level min_level)
then fail Sc_rollup_too_far_ahead
else return ctxt
(** Enfore that a commitment's inbox level increases by an exact fixed amount over its predecessor.
This property is used in several places - not obeying it causes severe breakage.
*)
let assert_commitment_frequency ctxt rollup commitment =
let open Lwt_tzresult_syntax in
let pred = Commitment.(commitment.predecessor) in
let* (ctxt, pred_level) =
if Commitment_hash.(pred = zero) then
let* level = Store.Initial_level.get ctxt rollup in
return (ctxt, level)
else
let* (pred, ctxt) =
get_commitment_internal ctxt rollup commitment.predecessor
in
return (ctxt, Commitment.(pred.inbox_level))
in
if
Raw_level_repr.(
commitment.inbox_level = add pred_level sc_rollup_commitment_frequency)
then return ctxt
else fail Sc_rollup_bad_inbox_level
(** Check invariants on [inbox_level], enforcing overallocation of storage and
regularity of block prorudction.
The constants used by [assert_refine_conditions_met] must be chosen such
that the maximum cost of storage allocated by each staker at most the size
of their deposit.
*)
let assert_refine_conditions_met ctxt rollup lcc commitment =
let open Lwt_tzresult_syntax in
let* ctxt = assert_commitment_not_too_far_ahead ctxt rollup lcc commitment in
assert_commitment_frequency ctxt rollup commitment
let refine_stake ctxt rollup staker commitment =
let open Lwt_tzresult_syntax in
let* (lcc, ctxt) = last_cemented_commitment ctxt rollup in
let* (staked_on, ctxt) = find_staker ctxt rollup staker in
let* ctxt = assert_refine_conditions_met ctxt rollup lcc commitment in
let new_hash = Commitment.hash commitment in
let rec go node ctxt =
if Commitment_hash.(node = staked_on) then (
let* (ctxt, commitment_size_diff, _was_bound) =
Store.Commitments.add (ctxt, rollup) new_hash commitment
in
let level = (Raw_context.current_level ctxt).level in
let* (commitment_added_size_diff, ctxt) =
set_commitment_added ctxt rollup new_hash level
in
let* (ctxt, staker_count_diff) =
Store.Stakers.update (ctxt, rollup) staker new_hash
in
let* (stake_count_size_diff, ctxt) =
increase_commitment_stake_count ctxt rollup new_hash
in
let size_diff =
commitment_size_diff + commitment_added_size_diff
+ stake_count_size_diff + staker_count_diff
in
assert (
Compare.Int.(
size_diff = 0
|| size_diff = sc_rollup_commitment_storage_size_in_bytes)) ;
return (new_hash, ctxt) )
else if Commitment_hash.(node = lcc) then
fail Sc_rollup_staker_backtracked
else
let* (pred, ctxt) = get_predecessor ctxt rollup node in
let* (_size, ctxt) = increase_commitment_stake_count ctxt rollup node in
(go [@ocaml.tailcall]) pred ctxt
in
go Commitment.(commitment.predecessor) ctxt
let publish_commitment ctxt rollup staker commitment =
let open Lwt_tzresult_syntax in
let* (ctxt, res) = Store.Stakers.find (ctxt, rollup) staker in
match res with
| None ->
let* ctxt = deposit_stake ctxt rollup staker in
refine_stake ctxt rollup staker commitment
| Some _ -> refine_stake ctxt rollup staker commitment
let cement_commitment ctxt rollup new_lcc =
let open Lwt_tzresult_syntax in
let refutation_deadline_blocks =
Constants_storage.sc_rollup_challenge_window_in_blocks ctxt
in
let* (old_lcc, ctxt) = last_cemented_commitment ctxt rollup in
let* (ctxt, total_staker_count) = Store.Staker_count.get ctxt rollup in
if Compare.Int32.(total_staker_count <= 0l) then fail Sc_rollup_no_stakers
else
let* (new_lcc_commitment, ctxt) =
get_commitment_internal ctxt rollup new_lcc
in
let* (ctxt, new_lcc_added) =
Store.Commitment_added.get (ctxt, rollup) new_lcc
in
if Commitment_hash.(new_lcc_commitment.predecessor <> old_lcc) then
fail Sc_rollup_parent_not_lcc
else
let* (new_lcc_stake_count, ctxt) =
get_commitment_stake_count ctxt rollup new_lcc
in
if Compare.Int32.(total_staker_count <> new_lcc_stake_count) then
fail Sc_rollup_disputed
else if
let level = (Raw_context.current_level ctxt).level in
Raw_level_repr.(level < add new_lcc_added refutation_deadline_blocks)
then fail Sc_rollup_too_recent
else
let* (ctxt, lcc_size_diff) =
Store.Last_cemented_commitment.update ctxt rollup new_lcc
in
assert (Compare.Int.(lcc_size_diff = 0)) ;
let* ctxt = deallocate ctxt rollup old_lcc in
consume_n_messages
ctxt
rollup
(Int32.to_int
@@ Sc_rollup_repr.Number_of_messages.to_int32
new_lcc_commitment.number_of_messages)
type conflict_point = Commitment_hash.t * Commitment_hash.t
(** [goto_inbox_level ctxt rollup inbox_level commit] Follows the predecessors of [commit] until it
arrives at the exact [inbox_level]. The result is the commit hash at the given inbox level. *)
let goto_inbox_level ctxt rollup inbox_level commit =
let open Lwt_tzresult_syntax in
let rec go ctxt commit =
let* (info, ctxt) = get_commitment_internal ctxt rollup commit in
if Raw_level_repr.(info.Commitment.inbox_level <= inbox_level) then (
assert (Raw_level_repr.(info.inbox_level = inbox_level)) ;
return (commit, ctxt))
else (go [@ocaml.tailcall]) ctxt info.predecessor
in
go ctxt commit
let get_conflict_point ctxt rollup staker1 staker2 =
let open Lwt_tzresult_syntax in
let* (lcc, ctxt) = last_cemented_commitment ctxt rollup in
let* (commit1, ctxt) = find_staker ctxt rollup staker1 in
let* (commit2, ctxt) = find_staker ctxt rollup staker2 in
let* () =
fail_when
Commitment_hash.(
commit1 = zero || commit2 = zero
|| commit1 = lcc
|| commit2 = lcc)
Sc_rollup_no_conflict
in
let* (commit1_info, ctxt) = get_commitment_internal ctxt rollup commit1 in
let* (commit2_info, ctxt) = get_commitment_internal ctxt rollup commit2 in
let target_inbox_level =
Raw_level_repr.min commit1_info.inbox_level commit2_info.inbox_level
in
let* (commit1, ctxt) =
goto_inbox_level ctxt rollup target_inbox_level commit1
in
let* (commit2, ctxt) =
goto_inbox_level ctxt rollup target_inbox_level commit2
in
let rec traverse_in_parallel ctxt commit1 commit2 =
if Commitment_hash.(commit1 = commit2) then
fail Sc_rollup_no_conflict
else
let* (commit1_info, ctxt) = get_commitment_internal ctxt rollup commit1 in
let* (commit2_info, ctxt) = get_commitment_internal ctxt rollup commit2 in
assert (
Raw_level_repr.(commit1_info.inbox_level = commit2_info.inbox_level)) ;
if Commitment_hash.(commit1_info.predecessor = commit2_info.predecessor)
then
return ((commit1, commit2), ctxt)
else
(traverse_in_parallel [@ocaml.tailcall])
ctxt
commit1_info.predecessor
commit2_info.predecessor
in
traverse_in_parallel ctxt commit1 commit2
let remove_staker ctxt rollup staker =
let open Lwt_tzresult_syntax in
let* (lcc, ctxt) = last_cemented_commitment ctxt rollup in
let* (ctxt, res) = Store.Stakers.find (ctxt, rollup) staker in
match res with
| None -> fail Sc_rollup_not_staked
| Some staked_on ->
if Commitment_hash.(staked_on = lcc) then fail Sc_rollup_remove_lcc
else
let* (ctxt, _size_diff) =
Store.Stakers.remove_existing (ctxt, rollup) staker
in
let* ctxt = modify_staker_count ctxt rollup Int32.pred in
let rec go node ctxt =
if Commitment_hash.(node = lcc) then return ctxt
else
let* (pred, ctxt) = get_predecessor ctxt rollup node in
let* ctxt = decrease_commitment_stake_count ctxt rollup node in
(go [@ocaml.tailcall]) pred ctxt
in
go staked_on ctxt
let list ctxt = Storage.Sc_rollup.PVM_kind.keys ctxt >|= Result.return
let initial_level ctxt rollup =
let open Lwt_tzresult_syntax in
let* level = Storage.Sc_rollup.Initial_level.find ctxt rollup in
match level with
| None -> fail (Sc_rollup_does_not_exist rollup)
| Some level -> return level