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
(** The rollup node stores and publishes commitments for the PVM every
[Constants.sc_rollup_commitment_period_in_blocks] levels.
Every time a finalized block is processed by the rollup node, the latter
determines whether the last commitment that the node has produced referred
to [sc_rollup.commitment_period_in_blocks] blocks earlier. For mainnet,
[sc_rollup.commitment_period_in_blocks = 30]. In this case, it computes and
stores a new commitment in a level-indexed map.
Stored commitments are signed by the rollup node operator
and published on the layer1 chain. To ensure that commitments
produced by the rollup node are eventually published,
storing and publishing commitments are decoupled. Every time
a new head is processed, the node tries to publish the oldest
commitment that was not published already.
*)
open Publisher_worker_types
module Lwt_result_option_syntax = struct
let ( let** ) a f =
let open Lwt_result_syntax in
let* a in
match a with None -> return_none | Some a -> f a
let fail = Lwt_result_syntax.return_none
let return x = Lwt_result_syntax.return_some x
end
module Lwt_result_option_list_syntax = struct
(** A small monadic combinator to return an empty list on None results. *)
let ( let*& ) x f =
let open Lwt_result_syntax in
let* x in
match x with None -> return_nil | Some x -> f x
end
let add_level level increment =
if increment < 0 then invalid_arg "Commitment.add_level negative increment" ;
Int32.add level (Int32.of_int increment)
let sub_level level decrement =
if decrement < 0 then invalid_arg "Commitment.sub_level negative decrement" ;
let r = Int32.sub level (Int32.of_int decrement) in
if r < 0l then None else Some r
let sc_rollup_commitment_period node_ctxt =
(Reference.get node_ctxt.Node_context.current_protocol).constants.sc_rollup
.commitment_period_in_blocks
let sc_rollup_challenge_window node_ctxt =
(Reference.get node_ctxt.Node_context.current_protocol).constants.sc_rollup
.challenge_window_in_blocks
let next_commitment_level node_ctxt last_commitment_level =
add_level last_commitment_level (sc_rollup_commitment_period node_ctxt)
type state = Node_context.ro
let tick_of_level (node_ctxt : _ Node_context.t) inbox_level =
let open Lwt_result_syntax in
let* block = Node_context.get_l2_block_by_level node_ctxt inbox_level in
return (Sc_rollup_block.final_tick block)
let build_commitment (module Plugin : Protocol_plugin_sig.S)
(node_ctxt : _ Node_context.t)
(prev_commitment : Octez_smart_rollup.Commitment.Hash.t)
~prev_commitment_level ~inbox_level ctxt =
let open Lwt_result_syntax in
let*! pvm_state = Context.PVMState.find ctxt in
let*? pvm_state =
match pvm_state with
| Some pvm_state -> Ok pvm_state
| None ->
error_with
"PVM state for commitment at level %ld is not available"
inbox_level
in
let*! compressed_state = Plugin.Pvm.state_hash node_ctxt.kind pvm_state in
let*! tick = Plugin.Pvm.get_tick node_ctxt.kind pvm_state in
let* prev_commitment_tick = tick_of_level node_ctxt prev_commitment_level in
let distance = Z.sub tick prev_commitment_tick in
let number_of_ticks = Z.to_int64 distance in
let*? () =
if number_of_ticks = 0L then error_with "A 0-tick commitment is impossible"
else if number_of_ticks < 0L then
error_with "Invalid number of ticks for commitment"
else Ok ()
in
return
Octez_smart_rollup.Commitment.
{
predecessor = prev_commitment;
inbox_level;
number_of_ticks;
compressed_state;
}
let genesis_pvm_state (module Plugin : Protocol_plugin_sig.S)
(node_ctxt : _ Node_context.t) ctxt =
let open Lwt_result_syntax in
match
(node_ctxt.unsafe_patches
:> (Pvm_patches.unsafe_patch * Pvm_patches.kind) list)
with
| [] -> (
let*! pvm_state = Context.PVMState.find ctxt in
match pvm_state with
| Some pvm_state -> return pvm_state
| None -> failwith "PVM state for genesis commitment is not available")
| _ ->
let+ _, Original state =
Interpreter.genesis_state (module Plugin) node_ctxt
in
state
let genesis_commitment (module Plugin : Protocol_plugin_sig.S)
(node_ctxt : _ Node_context.t) ctxt =
let open Lwt_result_syntax in
let* pvm_state = genesis_pvm_state (module Plugin) node_ctxt ctxt in
let*! compressed_state = Plugin.Pvm.state_hash node_ctxt.kind pvm_state in
let commitment =
Octez_smart_rollup.Commitment.
{
predecessor = Hash.zero;
inbox_level = node_ctxt.genesis_info.level;
number_of_ticks = 0L;
compressed_state;
}
in
let commitment_hash = Octez_smart_rollup.Commitment.hash commitment in
let+ () =
fail_unless
Octez_smart_rollup.Commitment.Hash.(
commitment_hash = node_ctxt.genesis_info.commitment_hash)
(Rollup_node_errors.Invalid_genesis_state
{
expected = node_ctxt.genesis_info.commitment_hash;
actual = commitment_hash;
})
in
commitment
let create_commitment_if_necessary plugin (node_ctxt : _ Node_context.t)
~predecessor current_level ctxt =
let open Lwt_result_syntax in
if current_level = node_ctxt.genesis_info.level then
let*! () = Commitment_event.compute_commitment current_level in
let+ genesis_commitment = genesis_commitment plugin node_ctxt ctxt in
Some genesis_commitment
else
let* last_commitment_hash =
let+ pred = Node_context.get_l2_block node_ctxt predecessor in
Sc_rollup_block.most_recent_commitment pred.header
in
let* last_commitment =
Node_context.get_commitment node_ctxt last_commitment_hash
in
let next_commitment_level =
next_commitment_level node_ctxt last_commitment.inbox_level
in
if current_level = next_commitment_level then
let*! () = Commitment_event.compute_commitment current_level in
let* commitment =
build_commitment
plugin
node_ctxt
last_commitment_hash
~prev_commitment_level:last_commitment.inbox_level
~inbox_level:current_level
ctxt
in
return_some commitment
else return_none
let process_head plugin (node_ctxt : _ Node_context.t) ~predecessor
Layer1.{level; header = _; _} ctxt =
let open Lwt_result_syntax in
let current_level = level in
let* commitment =
create_commitment_if_necessary
plugin
node_ctxt
~predecessor
current_level
ctxt
in
match commitment with
| None -> return_none
| Some commitment ->
let*! () =
Commitment_event.new_commitment
(Octez_smart_rollup.Commitment.hash commitment)
commitment.inbox_level
in
let* commitment_hash =
Node_context.save_commitment node_ctxt commitment
in
return_some commitment_hash
let missing_commitments (node_ctxt : _ Node_context.t) =
let open Lwt_result_syntax in
let lpc_level =
match Reference.get node_ctxt.lpc with
| None -> node_ctxt.genesis_info.level
| Some lpc -> lpc.inbox_level
in
let* head = Node_context.last_processed_head_opt node_ctxt
and* finalized_level = Node_context.get_finalized_level node_ctxt in
let next_head_level =
Option.map (fun (b : Sc_rollup_block.t) -> Int32.succ b.header.level) head
in
let sc_rollup_challenge_window_int32 =
sc_rollup_challenge_window node_ctxt |> Int32.of_int
in
let rec gather acc (commitment_hash : Commitment.Hash.t) =
let* commitment = Node_context.find_commitment node_ctxt commitment_hash in
let lcc = Reference.get node_ctxt.lcc in
match commitment with
| None -> return acc
| Some commitment when commitment.inbox_level <= lcc.level ->
return acc
| Some commitment when commitment.inbox_level <= lpc_level ->
return acc
| Some commitment ->
let* published_info =
Node_context.commitment_published_at_level node_ctxt commitment_hash
in
let past_curfew =
match (published_info, next_head_level) with
| None, _ | _, None -> false
| Some {first_published_at_level; _}, Some next_head_level ->
Int32.sub next_head_level first_published_at_level
> sc_rollup_challenge_window_int32
in
let is_finalized = commitment.inbox_level <= finalized_level in
let acc =
if is_finalized && not past_curfew then commitment :: acc else acc
in
gather acc commitment.predecessor
in
let* finalized_block = Node_context.get_finalized_head_opt node_ctxt in
match finalized_block with
| None -> return_nil
| Some finalized ->
let commitment =
Sc_rollup_block.most_recent_commitment finalized.header
in
gather [] commitment
let publish_commitment (node_ctxt : _ Node_context.t)
(commitment : Octez_smart_rollup.Commitment.t) =
let open Lwt_result_syntax in
let publish_operation =
L1_operation.Publish
{rollup = node_ctxt.config.sc_rollup_address; commitment}
in
let*! () =
Commitment_event.publish_commitment
(Octez_smart_rollup.Commitment.hash commitment)
commitment.inbox_level
in
let* _hash =
Injector.check_and_add_pending_operation
node_ctxt.config.mode
publish_operation
in
return_unit
let inject_recover_bond (node_ctxt : _ Node_context.t)
(staker : Signature.Public_key_hash.t) =
let open Lwt_result_syntax in
let recover_operation =
L1_operation.Recover_bond
{rollup = node_ctxt.config.sc_rollup_address; staker}
in
let*! () = Commitment_event.recover_bond staker in
let* _hash =
Injector.check_and_add_pending_operation
node_ctxt.config.mode
recover_operation
in
return_unit
let on_publish_commitments (node_ctxt : state) =
let open Lwt_result_syntax in
let* commitments = missing_commitments node_ctxt in
List.iter_es (publish_commitment node_ctxt) commitments
let publish_single_commitment (node_ctxt : _ Node_context.t)
(commitment : Octez_smart_rollup.Commitment.t) =
let lcc = Reference.get node_ctxt.lcc in
when_ (commitment.inbox_level > lcc.level) @@ fun () ->
publish_commitment node_ctxt commitment
let recover_bond node_ctxt =
let open Lwt_result_syntax in
let operator = Node_context.get_operator node_ctxt Recovering in
match operator with
| None ->
return_unit
| Some (Single committer) -> inject_recover_bond node_ctxt committer
let earliest_cementing_level node_ctxt commitment_hash =
let open Lwt_result_option_syntax in
let** {first_published_at_level; _} =
Node_context.commitment_published_at_level node_ctxt commitment_hash
in
return
@@ Int32.add
first_published_at_level
(sc_rollup_challenge_window node_ctxt |> Int32.of_int)
(** [latest_cementable_commitment node_ctxt head] is the most recent commitment
hash that could be cemented in [head]'s successor if:
- all its predecessors were cemented
- it would have been first published at the same level as its inbox
It does not need to be exact but it must be an upper bound on which we can
start the search for cementable commitments. *)
let latest_cementable_commitment (node_ctxt : _ Node_context.t)
(head : Sc_rollup_block.t) =
let open Lwt_result_option_syntax in
let commitment_hash = Sc_rollup_block.most_recent_commitment head.header in
let** commitment = Node_context.find_commitment node_ctxt commitment_hash in
let** cementable_level_bound =
Lwt_result.return
@@ sub_level commitment.inbox_level (sc_rollup_challenge_window node_ctxt)
in
let lcc = Reference.get node_ctxt.lcc in
if cementable_level_bound <= lcc.level then fail
else
let** cementable_bound_block =
Node_context.find_l2_block_by_level node_ctxt cementable_level_bound
in
let cementable_commitment =
Sc_rollup_block.most_recent_commitment cementable_bound_block.header
in
return cementable_commitment
let cementable_commitments (node_ctxt : _ Node_context.t) =
let open Lwt_result_syntax in
let open Lwt_result_option_list_syntax in
let*& head = Node_context.last_processed_head_opt node_ctxt in
let head_level = head.header.level in
let lcc = Reference.get node_ctxt.lcc in
let rec gather acc (commitment_hash : Commitment.Hash.t) =
let* commitment = Node_context.find_commitment node_ctxt commitment_hash in
match commitment with
| None -> return acc
| Some commitment when commitment.inbox_level <= lcc.level ->
return acc
| Some commitment ->
let* earliest_cementing_level =
earliest_cementing_level node_ctxt commitment_hash
in
let acc =
match earliest_cementing_level with
| None -> acc
| Some earliest_cementing_level ->
if earliest_cementing_level > head_level then
acc
else commitment_hash :: acc
in
gather acc commitment.predecessor
in
let*& latest_cementable_commitment =
latest_cementable_commitment node_ctxt head
in
gather [] latest_cementable_commitment
let cement_commitment (node_ctxt : _ Node_context.t) commitment =
let open Lwt_result_syntax in
let cement_operation =
L1_operation.Cement
{rollup = node_ctxt.config.sc_rollup_address; commitment}
in
let* _hash =
Injector.check_and_add_pending_operation
node_ctxt.config.mode
cement_operation
in
return_unit
let on_cement_commitments (node_ctxt : state) =
let open Lwt_result_syntax in
let* cementable_commitments = cementable_commitments node_ctxt in
List.iter_es (cement_commitment node_ctxt) cementable_commitments
module Types = struct
type nonrec state = state
type parameters = {node_ctxt : Node_context.ro}
end
module Name = struct
type t = unit
let encoding = Data_encoding.unit
let base = Commitment_event.section @ ["publisher"]
let pp _ _ = ()
let equal () () = true
end
module Worker = Worker.MakeSingle (Name) (Request) (Types)
type worker = Worker.infinite Worker.queue Worker.t
module Handlers = struct
type self = worker
let on_request :
type r request_error.
worker -> (r, request_error) Request.t -> (r, request_error) result Lwt.t
=
fun w request ->
let state = Worker.state w in
match request with
| Request.Publish -> protect @@ fun () -> on_publish_commitments state
| Request.Cement -> protect @@ fun () -> on_cement_commitments state
type launch_error = error trace
let on_launch _w () Types.{node_ctxt} = Lwt_result.return node_ctxt
let on_error (type a b) _w st (r : (a, b) Request.t) (errs : b) :
unit tzresult Lwt.t =
let open Lwt_result_syntax in
let request_view = Request.view r in
let emit_and_return_errors errs =
let*! () =
Commitment_event.Publisher.request_failed request_view st errs
in
return_unit
in
match r with
| Request.Publish -> emit_and_return_errors errs
| Request.Cement -> emit_and_return_errors errs
let on_completion _w r _ st =
Commitment_event.Publisher.request_completed (Request.view r) st
let on_no_request _ = Lwt.return_unit
let on_close _w = Lwt.return_unit
end
let table = Worker.create_table Queue
let worker_promise, worker_waker = Lwt.task ()
let start node_ctxt =
let open Lwt_result_syntax in
let*! () = Commitment_event.starting () in
let node_ctxt = Node_context.readonly node_ctxt in
let+ worker = Worker.launch table () {node_ctxt} (module Handlers) in
Lwt.wakeup worker_waker worker
let start_in_mode mode =
let open Configuration in
match mode with
| Maintenance | Operator | Bailout -> true
| Observer | Accuser | Batcher -> false
| Custom ops -> purpose_matches_mode (Custom ops) Operating
let init (node_ctxt : _ Node_context.t) =
let open Lwt_result_syntax in
match Lwt.state worker_promise with
| Lwt.Return _ ->
return_unit
| Lwt.Fail exn ->
fail [Rollup_node_errors.No_publisher; Exn exn]
| Lwt.Sleep ->
if start_in_mode node_ctxt.config.mode then start node_ctxt
else return_unit
let worker =
let open Result_syntax in
lazy
(match Lwt.state worker_promise with
| Lwt.Return worker -> return worker
| Lwt.Fail exn -> fail (Error_monad.error_of_exn exn)
| Lwt.Sleep -> Error Rollup_node_errors.No_publisher)
let worker_add_request ~request =
let open Lwt_result_syntax in
match Lazy.force worker with
| Ok w ->
let node_ctxt = Worker.state w in
unless (Node_context.is_bailout node_ctxt && request = Request.Publish)
@@ fun () ->
let*! (_pushed : bool) = Worker.Queue.push_request w request in
return_unit
| Error Rollup_node_errors.No_publisher -> return_unit
| Error e -> tzfail e
let publish_commitments () = worker_add_request ~request:Request.Publish
let cement_commitments () = worker_add_request ~request:Request.Cement
let shutdown () =
match Lazy.force worker with
| Error _ ->
Lwt.return_unit
| Ok w -> Worker.shutdown w