Source file sc_rollup_operations.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
open Alpha_context
type error +=
| Sc_rollup_invalid_parameters_type
| Sc_rollup_invalid_last_cemented_commitment
| Sc_rollup_invalid_output_proof
| Sc_rollup_invalid_outbox_level
type execute_outbox_message_result = {
paid_storage_size_diff : Z.t;
ticket_receipt : Ticket_receipt.t;
operations : Script_typed_ir.packed_internal_operation list;
}
let () =
let description = "Invalid parameters type for smart rollup" in
register_error_kind
`Permanent
~id:"smart_rollup_invalid_parameters_type"
~title:"Invalid parameters type"
~description
~pp:(fun fmt () -> Format.fprintf fmt "%s" description)
Data_encoding.unit
(function Sc_rollup_invalid_parameters_type -> Some () | _ -> None)
(fun () -> Sc_rollup_invalid_parameters_type) ;
let description = "Invalid last-cemented-commitment" in
register_error_kind
`Permanent
~id:"smart_rollup_invalid_last_cemented_commitment"
~title:description
~description
~pp:(fun ppf () -> Format.fprintf ppf "%s" description)
Data_encoding.empty
(function
| Sc_rollup_invalid_last_cemented_commitment -> Some () | _ -> None)
(fun () -> Sc_rollup_invalid_last_cemented_commitment) ;
let description = "Invalid output proof" in
register_error_kind
`Permanent
~id:"smart_rollup_invalid_output_proof"
~title:description
~description
~pp:(fun ppf () -> Format.fprintf ppf "%s" description)
Data_encoding.empty
(function Sc_rollup_invalid_output_proof -> Some () | _ -> None)
(fun () -> Sc_rollup_invalid_output_proof) ;
let description = "Invalid outbox level" in
register_error_kind
`Permanent
~id:"smart_rollup_invalid_outbox_level"
~title:description
~description
~pp:(fun ppf () -> Format.fprintf ppf "%s" description)
Data_encoding.empty
(function Sc_rollup_invalid_outbox_level -> Some () | _ -> None)
(fun () -> Sc_rollup_invalid_outbox_level)
type origination_result = {
address : Sc_rollup.Address.t;
size : Z.t;
genesis_commitment_hash : Sc_rollup.Commitment.Hash.t;
}
type 'ret continuation = unit -> 'ret tzresult
let rec validate_ty :
type a ac ret.
(a, ac) Script_typed_ir.ty ->
a Script_typed_ir.entrypoints_node ->
ret continuation ->
ret tzresult =
fun ty {nested = nested_entrypoints; at_node} k ->
let open Script_typed_ir in
match at_node with
| Some {name = _; original_type_expr = _} ->
error Sc_rollup_invalid_parameters_type
| None -> (
match ty with
| Unit_t -> (k [@ocaml.tailcall]) ()
| Int_t -> (k [@ocaml.tailcall]) ()
| Nat_t -> (k [@ocaml.tailcall]) ()
| Signature_t -> (k [@ocaml.tailcall]) ()
| String_t -> (k [@ocaml.tailcall]) ()
| Bytes_t -> (k [@ocaml.tailcall]) ()
| Key_hash_t -> (k [@ocaml.tailcall]) ()
| Key_t -> (k [@ocaml.tailcall]) ()
| Timestamp_t -> (k [@ocaml.tailcall]) ()
| Address_t -> (k [@ocaml.tailcall]) ()
| Bls12_381_g1_t -> (k [@ocaml.tailcall]) ()
| Bls12_381_g2_t -> (k [@ocaml.tailcall]) ()
| Bls12_381_fr_t -> (k [@ocaml.tailcall]) ()
| Bool_t -> (k [@ocaml.tailcall]) ()
| Never_t -> (k [@ocaml.tailcall]) ()
| Tx_rollup_l2_address_t -> (k [@ocaml.tailcall]) ()
| Chain_id_t -> (k [@ocaml.tailcall]) ()
| Ticket_t (ty, _) -> (validate_ty [@ocaml.tailcall]) ty no_entrypoints k
| Set_t (ty, _) -> (validate_ty [@ocaml.tailcall]) ty no_entrypoints k
| Option_t (ty, _, _) ->
(validate_ty [@ocaml.tailcall]) ty no_entrypoints k
| List_t (ty, _) -> (validate_ty [@ocaml.tailcall]) ty no_entrypoints k
| Pair_t (ty1, ty2, _, _) ->
(validate_two_tys [@ocaml.tailcall])
ty1
ty2
no_entrypoints
no_entrypoints
k
| Or_t (ty1, ty2, _, _) ->
let entrypoints_l, entrypoints_r =
match nested_entrypoints with
| Entrypoints_None -> (no_entrypoints, no_entrypoints)
| Entrypoints_Or {left; right} -> (left, right)
in
(validate_two_tys [@ocaml.tailcall])
ty1
ty2
entrypoints_l
entrypoints_r
k
| Map_t (key_ty, val_ty, _) ->
(validate_two_tys [@ocaml.tailcall])
key_ty
val_ty
no_entrypoints
no_entrypoints
k
| Mutez_t -> error Sc_rollup_invalid_parameters_type
| Big_map_t (_key_ty, _val_ty, _) ->
error Sc_rollup_invalid_parameters_type
| Contract_t _ -> error Sc_rollup_invalid_parameters_type
| Sapling_transaction_t _ -> error Sc_rollup_invalid_parameters_type
| Sapling_transaction_deprecated_t _ ->
error Sc_rollup_invalid_parameters_type
| Sapling_state_t _ -> error Sc_rollup_invalid_parameters_type
| Operation_t -> error Sc_rollup_invalid_parameters_type
| Chest_t -> error Sc_rollup_invalid_parameters_type
| Chest_key_t -> error Sc_rollup_invalid_parameters_type
| Lambda_t (_, _, _) -> error Sc_rollup_invalid_parameters_type)
and validate_two_tys :
type a ac b bc ret.
(a, ac) Script_typed_ir.ty ->
(b, bc) Script_typed_ir.ty ->
a Script_typed_ir.entrypoints_node ->
b Script_typed_ir.entrypoints_node ->
ret continuation ->
ret tzresult =
fun ty1 ty2 entrypoints1 entrypoints2 k ->
(validate_ty [@ocaml.tailcall]) ty1 entrypoints1 (fun () ->
(validate_ty [@ocaml.tailcall]) ty2 entrypoints2 k)
let validate_parameters_ty :
type a ac.
context ->
(a, ac) Script_typed_ir.ty ->
a Script_typed_ir.entrypoints_node ->
context tzresult =
fun ctxt parameters_ty entrypoints ->
let open Result_syntax in
let* ctxt =
Gas.consume
ctxt
(Sc_rollup_costs.is_valid_parameters_ty_cost
~ty_size:Script_typed_ir.(ty_size parameters_ty |> Type_size.to_int))
in
let+ () = validate_ty parameters_ty entrypoints ok in
ctxt
let validate_untyped_parameters_ty ctxt parameters_ty =
let open Result_syntax in
let* ( Ex_parameter_ty_and_entrypoints
{
arg_type;
entrypoints =
{Script_typed_ir.root = entrypoint; original_type_expr = _};
},
ctxt ) =
Script_ir_translator.parse_parameter_ty_and_entrypoints
ctxt
~legacy:false
(Micheline.root parameters_ty)
in
validate_parameters_ty ctxt arg_type entrypoint
let check_origination_proof (type state proof output)
~(pvm : (state, proof, output) Sc_rollup.PVM.implementation) boot_sector
origination_proof =
let open Lwt_result_syntax in
let (module PVM) = pvm in
let*! is_valid = PVM.verify_origination_proof origination_proof boot_sector in
let* () =
fail_when
(not is_valid)
(Sc_rollup_proof_repr.Sc_rollup_proof_check "invalid origination proof")
in
return PVM.(proof_stop_state origination_proof)
let originate ctxt ~kind ~boot_sector ~origination_proof ~parameters_ty =
let open Lwt_result_syntax in
let (Packed ((module PVM) as pvm)) = Sc_rollup.Kind.pvm_of kind in
let*? ctxt =
let open Result_syntax in
let* parameters_ty, ctxt =
Script.force_decode_in_context
~consume_deserialization_gas:When_needed
ctxt
parameters_ty
in
validate_untyped_parameters_ty ctxt parameters_ty
in
let*? origination_proof =
Sc_rollup.Proof.unserialize_pvm_step ~pvm origination_proof
in
let* genesis_hash =
check_origination_proof ~pvm boot_sector origination_proof
in
let genesis_commitment =
Sc_rollup.Commitment.genesis_commitment
~genesis_state_hash:genesis_hash
~origination_level:(Level.current ctxt).level
in
let+ address, size, genesis_commitment_hash, ctxt =
Sc_rollup.originate ctxt ~kind ~parameters_ty ~genesis_commitment
in
({address; size; genesis_commitment_hash}, ctxt)
let to_transaction_operation ctxt rollup
(Sc_rollup_management_protocol.Transaction
{destination; entrypoint; parameters_ty; parameters; unparsed_parameters})
=
let open Result_syntax in
let* ctxt, nonce = fresh_internal_nonce ctxt in
let* ctxt =
validate_parameters_ty ctxt parameters_ty Script_typed_ir.no_entrypoints
in
let operation =
Script_typed_ir.Transaction_to_smart_contract
{
destination;
amount = Tez.zero;
entrypoint;
location = Micheline.dummy_location;
parameters_ty;
parameters;
unparsed_parameters;
}
in
return
( Script_typed_ir.Internal_operation
{source = Destination.Sc_rollup rollup; operation; nonce},
ctxt )
let transfer_ticket_tokens ctxt ~source_destination ~acc_storage_diff
{Ticket_operations_diff.ticket_token; total_amount = _; destinations} =
let open Lwt_result_syntax in
List.fold_left_es
(fun (acc_storage_diff, ctxt)
(target_destination, (amount : Script_typed_ir.ticket_amount)) ->
let* ctxt, storage_diff =
Ticket_transfer.transfer_ticket
ctxt
~src:source_destination
~dst:target_destination
ticket_token
Ticket_amount.((amount :> t))
in
return (Z.(add acc_storage_diff storage_diff), ctxt))
(acc_storage_diff, ctxt)
destinations
let validate_and_decode_output_proof ctxt ~cemented_commitment rollup
~output_proof =
let open Lwt_result_syntax in
let* ctxt, Packed (module PVM) =
let+ ctxt, kind = Sc_rollup.kind ctxt rollup in
(ctxt, Sc_rollup.Kind.pvm_of kind)
in
let output_proof_length = String.length output_proof in
let*? ctxt =
Gas.consume
ctxt
(Sc_rollup_costs.cost_deserialize_output_proof
~bytes_len:output_proof_length)
in
let*? output_proof =
match
Data_encoding.Binary.of_string_opt PVM.output_proof_encoding output_proof
with
| Some x -> ok x
| None -> error Sc_rollup_invalid_output_proof
in
let output = PVM.output_of_output_proof output_proof in
let* {Sc_rollup.Commitment.compressed_state; _}, ctxt =
Sc_rollup.Commitment.get_commitment ctxt rollup cemented_commitment
in
let* () =
let output_proof_state = PVM.state_of_output_proof output_proof in
fail_unless
Sc_rollup.State_hash.(output_proof_state = compressed_state)
Sc_rollup_invalid_output_proof
in
let*? ctxt =
Gas.consume
ctxt
(Sc_rollup_costs.cost_verify_output_proof ~bytes_len:output_proof_length)
in
let* () =
let*! proof_is_valid = PVM.verify_output_proof output_proof in
fail_unless proof_is_valid Sc_rollup_invalid_output_proof
in
return (output, ctxt)
let validate_outbox_level ctxt ~outbox_level ~lcc_level =
let max_active_levels =
Int32.to_int (Constants.sc_rollup_max_active_outbox_levels ctxt)
in
let outbox_level_is_active =
let min_allowed_level =
Int32.sub (Raw_level.to_int32 lcc_level) (Int32.of_int max_active_levels)
in
Compare.Int32.(min_allowed_level < Raw_level.to_int32 outbox_level)
in
fail_unless
(Raw_level.(outbox_level <= lcc_level) && outbox_level_is_active)
Sc_rollup_invalid_outbox_level
let execute_outbox_message ctxt ~validate_and_decode_output_proof rollup
~cemented_commitment ~output_proof =
let open Lwt_result_syntax in
let* lcc_hash, lcc_level, ctxt =
Sc_rollup.Commitment.last_cemented_commitment_hash_with_level ctxt rollup
in
let* is_cemented_commitment_in_context, ctxt =
Sc_rollup.Commitment.check_if_commitments_are_related
ctxt
rollup
~descendant:lcc_hash
~ancestor:cemented_commitment
in
let* () =
fail_unless
is_cemented_commitment_in_context
Sc_rollup_invalid_last_cemented_commitment
in
let* Sc_rollup.{outbox_level; message_index; message}, ctxt =
validate_and_decode_output_proof
ctxt
~cemented_commitment
rollup
~output_proof
in
let* () = validate_outbox_level ctxt ~outbox_level ~lcc_level in
let* ( Sc_rollup_management_protocol.Atomic_transaction_batch {transactions},
ctxt ) =
Sc_rollup_management_protocol.outbox_message_of_outbox_message_repr
ctxt
message
in
let*? ctxt, operations =
List.fold_left_map_e
(fun ctxt transaction ->
let open Result_syntax in
let+ op, ctxt = to_transaction_operation ctxt rollup transaction in
(ctxt, op))
ctxt
transactions
in
let* applied_msg_size_diff, ctxt =
Sc_rollup.Outbox.record_applied_message
ctxt
rollup
outbox_level
~message_index:(Z.to_int message_index)
in
let paid_storage_size_diff = Z.max Z.zero applied_msg_size_diff in
let* ticket_token_diffs, ctxt =
Ticket_operations_diff.ticket_diffs_of_operations ctxt operations
in
let* paid_storage_size_diff, ctxt =
let source_destination = Destination.Sc_rollup rollup in
List.fold_left_es
(fun (acc_storage_diff, ctxt) ticket_token_diff ->
transfer_ticket_tokens
ctxt
~source_destination
~acc_storage_diff
ticket_token_diff)
(paid_storage_size_diff, ctxt)
ticket_token_diffs
in
let* ctxt, ticket_receipt =
List.fold_left_map_es
(fun ctxt
Ticket_operations_diff.
{ticket_token = ex_token; total_amount; destinations = _} ->
let+ ticket_token, ctxt = Ticket_token_unparser.unparse ctxt ex_token in
let item =
Ticket_receipt.
{
ticket_token;
updates =
[
{
account = Destination.Sc_rollup rollup;
amount = Z.neg (Script_int.to_zint total_amount);
};
];
}
in
(ctxt, item))
ctxt
ticket_token_diffs
in
return ({paid_storage_size_diff; ticket_receipt; operations}, ctxt)
module Internal_for_tests = struct
let execute_outbox_message = execute_outbox_message
end
let execute_outbox_message ctxt =
execute_outbox_message ctxt ~validate_and_decode_output_proof