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
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;
operations : Script_typed_ir.packed_internal_operation list;
}
let () =
let description = "Invalid parameters type for rollup" in
register_error_kind
`Permanent
~id:"Sc_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:"Sc_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:"Sc_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:"Sc_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;
}
let origination_proof_of_string origination_proof kind =
let open Lwt_tzresult_syntax in
match kind with
| Sc_rollup.Kind.Example_arith ->
let* proof =
match
Data_encoding.Binary.of_string_opt
Sc_rollup.ArithPVM.Protocol_implementation.proof_encoding
origination_proof
with
| Some x -> return x
| None ->
fail
(Sc_rollup_proof_repr.Sc_rollup_proof_check
"invalid encoding for Arith origination proof")
in
let (module PVM : Sc_rollup.PVM_with_proof
with type proof = Sc_rollup.ArithPVM.Protocol_implementation.proof)
=
(module struct
include Sc_rollup.ArithPVM.Protocol_implementation
let proof = proof
end)
in
return @@ Sc_rollup.Arith_pvm_with_proof (module PVM)
| Sc_rollup.Kind.Wasm_2_0_0 ->
let* proof =
match
Data_encoding.Binary.of_string_opt
Sc_rollup.Wasm_2_0_0PVM.Protocol_implementation.proof_encoding
origination_proof
with
| Some x -> return x
| None ->
fail
(Sc_rollup_proof_repr.Sc_rollup_proof_check
"invalid encoding for Wasm_2_0_0 origination proof")
in
let (module PVM : Sc_rollup.PVM_with_proof
with type proof =
Sc_rollup.Wasm_2_0_0PVM.Protocol_implementation.proof) =
(module struct
include Sc_rollup.Wasm_2_0_0PVM.Protocol_implementation
let proof = proof
end)
in
return @@ Sc_rollup.Wasm_2_0_0_pvm_with_proof (module PVM)
type 'ret continuation = unit -> 'ret tzresult
let rec validate_ty :
type a ac ret.
(a, ac) Script_typed_ir.ty -> ret continuation -> ret tzresult =
fun ty k ->
let open Script_typed_ir in
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 k
| Set_t (ty, _) -> (validate_ty [@ocaml.tailcall]) ty k
| Option_t (ty, _, _) -> (validate_ty [@ocaml.tailcall]) ty k
| List_t (ty, _) -> (validate_ty [@ocaml.tailcall]) ty k
| Pair_t (ty1, ty2, _, _) -> (validate_two_tys [@ocaml.tailcall]) ty1 ty2 k
| Union_t (ty1, ty2, _, _) -> (validate_two_tys [@ocaml.tailcall]) ty1 ty2 k
| Map_t (key_ty, val_ty, _) ->
(validate_two_tys [@ocaml.tailcall]) key_ty val_ty 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 ->
ret continuation ->
ret tzresult =
fun ty1 ty2 k ->
(validate_ty [@ocaml.tailcall]) ty1 (fun () ->
(validate_ty [@ocaml.tailcall]) ty2 k)
let validate_parameters_ty ctxt parameters_ty =
let open Tzresult_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 ok in
ctxt
let validate_untyped_parameters_ty ctxt parameters_ty =
let open Tzresult_syntax in
let* Ex_parameter_ty_and_entrypoints {arg_type; entrypoints = _}, ctxt =
Script_ir_translator.parse_parameter_ty_and_entrypoints
ctxt
~legacy:false
(Micheline.root parameters_ty)
in
validate_parameters_ty ctxt arg_type
let check_origination_proof kind boot_sector origination_proof =
let open Lwt_tzresult_syntax in
let (module PVM) = Sc_rollup.wrapped_proof_module origination_proof in
let kind' = Sc_rollup.wrapped_proof_kind_exn origination_proof in
let* () =
fail_when
Compare.String.(
Sc_rollup.Kind.name_of kind <> Sc_rollup.Kind.name_of kind')
(Sc_rollup_proof_repr.Sc_rollup_proof_check "incorrect kind proof")
in
let*! is_valid = PVM.verify_origination_proof PVM.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 proof)
let originate ctxt ~kind ~boot_sector ~origination_proof ~parameters_ty =
let open Lwt_tzresult_syntax in
let*? ctxt =
let open Tzresult_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 = origination_proof_of_string origination_proof kind in
let* genesis_hash =
check_origination_proof kind 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
~boot_sector
~parameters_ty
~genesis_commitment
in
({address; size; genesis_commitment_hash}, ctxt)
let to_transaction_operation ctxt ~source
(Sc_rollup_management_protocol.Transaction
{destination; entrypoint; parameters_ty; parameters; unparsed_parameters})
=
let open Tzresult_syntax in
let* ctxt, nonce = fresh_internal_nonce ctxt in
let* ctxt = validate_parameters_ty ctxt parameters_ty 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 = Contract.Implicit source; operation; nonce},
ctxt )
let transfer_ticket_token ctxt ~source_destination ~target_destination ~amount
ticket_token =
let open Lwt_tzresult_syntax in
let* source_key_hash, ctxt =
Ticket_balance_key.of_ex_token ctxt ~owner:source_destination ticket_token
in
let* target_key_hash, ctxt =
Ticket_balance_key.of_ex_token ctxt ~owner:target_destination ticket_token
in
let* source_storage_diff, ctxt =
Ticket_balance.adjust_balance ctxt source_key_hash ~delta:(Z.neg amount)
in
let* target_storage_diff, ctxt =
Ticket_balance.adjust_balance ctxt target_key_hash ~delta:amount
in
let* storage_diff_to_pay, ctxt =
Ticket_balance.adjust_storage_space
ctxt
~storage_diff:(Z.add source_storage_diff target_storage_diff)
in
return (storage_diff_to_pay, ctxt)
let transfer_ticket_tokens ctxt ~source_destination ~acc_storage_diff
{Ticket_operations_diff.ticket_token; total_amount = _; destinations} =
let open Lwt_tzresult_syntax in
List.fold_left_es
(fun (acc_storage_diff, ctxt)
(target_destination, (amount : Script_typed_ir.ticket_amount)) ->
let* storage_diff, ctxt =
transfer_ticket_token
ctxt
~source_destination
~target_destination
~amount:Script_int.(to_zint (amount :> n num))
ticket_token
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_tzresult_syntax in
let* ctxt, (module PVM : Sc_rollup.PVM.S) =
let+ ctxt, kind = Sc_rollup.kind ctxt rollup in
(ctxt, Sc_rollup.Kind.pvm_of kind)
in
let*? ctxt =
Gas.consume
ctxt
(Sc_rollup_costs.cost_deserialize_output_proof
~bytes_len:(String.length output_proof))
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* () =
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 ~source ~output_proof =
let open Lwt_tzresult_syntax in
let* lcc_hash, lcc_level, ctxt =
Sc_rollup.Commitment.last_cemented_commitment_hash_with_level ctxt rollup
in
let* () =
fail_unless
Sc_rollup.Commitment.Hash.(lcc_hash = cemented_commitment)
Sc_rollup_invalid_last_cemented_commitment
in
let* Sc_rollup.{outbox_level; message_index; message}, ctxt =
validate_and_decode_output_proof
ctxt
~cemented_commitment:lcc_hash
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 Tzresult_syntax in
let+ op, ctxt = to_transaction_operation ctxt ~source 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
return ({paid_storage_size_diff; operations}, ctxt)
module Internal_for_tests = struct
let execute_outbox_message = execute_outbox_message
let origination_proof_of_string = origination_proof_of_string
end
let execute_outbox_message ctxt =
execute_outbox_message ctxt ~validate_and_decode_output_proof