Source file apply_internal_results.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
open Alpha_context
open Data_encoding
open Apply_operation_result
type 'kind internal_operation_contents =
| Transaction : {
amount : Tez.tez;
parameters : Script.lazy_expr;
entrypoint : Entrypoint.t;
destination : Destination.t;
}
-> Kind.transaction internal_operation_contents
| Origination : {
delegate : Signature.Public_key_hash.t option;
script : Script.t;
credit : Tez.tez;
}
-> Kind.origination internal_operation_contents
| Delegation :
Signature.Public_key_hash.t option
-> Kind.delegation internal_operation_contents
| Event : {
ty : Script.expr;
tag : Entrypoint.t;
payload : Script.expr;
}
-> Kind.event internal_operation_contents
type packed_internal_operation_contents =
| Internal_operation_contents :
'kind internal_operation_contents
-> packed_internal_operation_contents
type 'kind internal_operation = {
source : Destination.t;
operation : 'kind internal_operation_contents;
nonce : int;
}
type packed_internal_operation =
| Internal_operation : 'kind internal_operation -> packed_internal_operation
let internal_operation (type kind)
({source; operation; nonce} : kind Script_typed_ir.internal_operation) :
kind internal_operation =
let operation : kind internal_operation_contents =
match operation with
| Transaction_to_implicit {destination; amount} ->
Transaction
{
destination = Contract (Implicit destination);
amount;
entrypoint = Entrypoint.default;
parameters = Script.unit_parameter;
}
| Transaction_to_implicit_with_ticket
{destination; unparsed_ticket; amount; ticket_ty = _; ticket = _} ->
Transaction
{
destination = Contract (Implicit destination);
amount;
entrypoint = Entrypoint.default;
parameters = unparsed_ticket;
}
| Transaction_to_smart_contract
{destination; amount; entrypoint; unparsed_parameters; _} ->
Transaction
{
destination = Contract (Originated destination);
amount;
entrypoint;
parameters = Script.lazy_expr unparsed_parameters;
}
| Transaction_to_tx_rollup {destination; unparsed_parameters; _} ->
Transaction
{
destination = Tx_rollup destination;
amount = Tez.zero;
entrypoint = Entrypoint.deposit;
parameters = Script.lazy_expr unparsed_parameters;
}
| Transaction_to_sc_rollup {destination; entrypoint; unparsed_parameters; _}
->
Transaction
{
destination = Sc_rollup destination;
amount = Tez.zero;
entrypoint;
parameters = Script.lazy_expr unparsed_parameters;
}
| Event {ty; tag; unparsed_data} -> Event {ty; tag; payload = unparsed_data}
| Transaction_to_zk_rollup {destination; unparsed_parameters; _} ->
Transaction
{
destination = Zk_rollup destination;
amount = Tez.zero;
entrypoint = Entrypoint.deposit;
parameters = Script.lazy_expr unparsed_parameters;
}
| Origination {delegate; code; unparsed_storage; credit; _} ->
let script =
{
Script.code = Script.lazy_expr code;
storage = Script.lazy_expr unparsed_storage;
}
in
Origination {delegate; script; credit}
| Delegation delegate -> Delegation delegate
in
{source; operation; nonce}
let packed_internal_operation (Script_typed_ir.Internal_operation op) =
Internal_operation (internal_operation op)
let packed_internal_operations = List.map packed_internal_operation
type successful_transaction_result =
| Transaction_to_contract_result of {
storage : Script.expr option;
lazy_storage_diff : Lazy_storage.diffs option;
balance_updates : Receipt.balance_updates;
ticket_receipt : Ticket_receipt.t;
originated_contracts : Contract_hash.t list;
consumed_gas : Gas.Arith.fp;
storage_size : Z.t;
paid_storage_size_diff : Z.t;
allocated_destination_contract : bool;
}
| Transaction_to_tx_rollup_result of {
ticket_hash : Ticket_hash.t;
balance_updates : Receipt.balance_updates;
consumed_gas : Gas.Arith.fp;
paid_storage_size_diff : Z.t;
}
| Transaction_to_sc_rollup_result of {
consumed_gas : Gas.Arith.fp;
ticket_receipt : Ticket_receipt.t;
}
| Transaction_to_zk_rollup_result of {
ticket_hash : Ticket_hash.t;
balance_updates : Receipt.balance_updates;
consumed_gas : Gas.Arith.fp;
paid_storage_size_diff : Z.t;
}
type successful_origination_result = {
lazy_storage_diff : Lazy_storage.diffs option;
balance_updates : Receipt.balance_updates;
originated_contracts : Contract_hash.t list;
consumed_gas : Gas.Arith.fp;
storage_size : Z.t;
paid_storage_size_diff : Z.t;
}
(** Result of applying an internal operation. *)
type _ successful_internal_operation_result =
| ITransaction_result :
successful_transaction_result
-> Kind.transaction successful_internal_operation_result
| IOrigination_result :
successful_origination_result
-> Kind.origination successful_internal_operation_result
| IDelegation_result : {
consumed_gas : Gas.Arith.fp;
}
-> Kind.delegation successful_internal_operation_result
| IEvent_result : {
consumed_gas : Gas.Arith.fp;
}
-> Kind.event successful_internal_operation_result
type packed_successful_internal_operation_result =
| Successful_internal_operation_result :
'kind successful_internal_operation_result
-> packed_successful_internal_operation_result
type 'kind internal_operation_result =
( 'kind,
'kind Kind.manager,
'kind successful_internal_operation_result )
operation_result
type packed_internal_operation_result =
| Internal_operation_result :
'kind internal_operation * 'kind internal_operation_result
-> packed_internal_operation_result
let pack_internal_operation_result (type kind)
(internal_op : kind Script_typed_ir.internal_operation)
(manager_op : kind internal_operation_result) =
let internal_op = internal_operation internal_op in
Internal_operation_result (internal_op, manager_op)
type 'kind iselect =
packed_internal_operation_result ->
('kind internal_operation * 'kind internal_operation_result) option
module Internal_operation = struct
open Data_encoding
type 'kind case =
| MCase : {
tag : int;
name : string;
encoding : 'a Data_encoding.t;
iselect : 'kind iselect;
select :
packed_internal_operation_contents ->
'kind internal_operation_contents option;
proj : 'kind internal_operation_contents -> 'a;
inj : 'a -> 'kind internal_operation_contents;
}
-> 'kind case
let transaction_contract_variant_cases =
union
[
case
~title:"To_contract"
(Tag 0)
(obj9
(opt "storage" Script.expr_encoding)
(dft "balance_updates" Receipt.balance_updates_encoding [])
(dft "ticket_receipt" Ticket_receipt.encoding [])
(dft "originated_contracts" (list Contract.originated_encoding) [])
(dft "consumed_milligas" Gas.Arith.n_fp_encoding Gas.Arith.zero)
(dft "storage_size" z Z.zero)
(dft "paid_storage_size_diff" z Z.zero)
(dft "allocated_destination_contract" bool false)
(opt "lazy_storage_diff" Lazy_storage.encoding))
(function
| Transaction_to_contract_result
{
storage;
lazy_storage_diff;
balance_updates;
ticket_receipt;
originated_contracts;
consumed_gas;
storage_size;
paid_storage_size_diff;
allocated_destination_contract;
} ->
Some
( storage,
balance_updates,
ticket_receipt,
originated_contracts,
consumed_gas,
storage_size,
paid_storage_size_diff,
allocated_destination_contract,
lazy_storage_diff )
| _ -> None)
(fun ( storage,
balance_updates,
ticket_receipt,
originated_contracts,
consumed_gas,
storage_size,
paid_storage_size_diff,
allocated_destination_contract,
lazy_storage_diff ) ->
Transaction_to_contract_result
{
storage;
lazy_storage_diff;
balance_updates;
ticket_receipt;
originated_contracts;
consumed_gas;
storage_size;
paid_storage_size_diff;
allocated_destination_contract;
});
case
~title:"To_tx_rollup"
(Tag 1)
(obj4
(dft "balance_updates" Receipt.balance_updates_encoding [])
(dft "consumed_milligas" Gas.Arith.n_fp_encoding Gas.Arith.zero)
(req "ticket_hash" Ticket_hash.encoding)
(req "paid_storage_size_diff" n))
(function
| Transaction_to_tx_rollup_result
{
balance_updates;
consumed_gas;
ticket_hash;
paid_storage_size_diff;
} ->
Some
( balance_updates,
consumed_gas,
ticket_hash,
paid_storage_size_diff )
| _ -> None)
(fun ( balance_updates,
consumed_gas,
ticket_hash,
paid_storage_size_diff ) ->
Transaction_to_tx_rollup_result
{
balance_updates;
consumed_gas;
ticket_hash;
paid_storage_size_diff;
});
case
~title:"To_smart_rollup"
(Tag 2)
(obj2
(dft "consumed_milligas" Gas.Arith.n_fp_encoding Gas.Arith.zero)
(req "ticket_receipt" Ticket_receipt.encoding))
(function
| Transaction_to_sc_rollup_result {consumed_gas; ticket_receipt} ->
Some (consumed_gas, ticket_receipt)
| _ -> None)
(function
| consumed_gas, ticket_receipt ->
Transaction_to_sc_rollup_result {consumed_gas; ticket_receipt});
]
let transaction_case =
MCase
{
tag = 1;
name = "transaction";
encoding =
obj3
(req "amount" Tez.encoding)
(req "destination" Destination.encoding)
(opt
"parameters"
(obj2
(req "entrypoint" Entrypoint.smart_encoding)
(req "value" Script.lazy_expr_encoding)));
iselect : Kind.transaction iselect =
(function
| Internal_operation_result
(({operation = Transaction _; _} as op), res) ->
Some (op, res)
| _ -> None);
select =
(function
| Internal_operation_contents (Transaction _ as op) -> Some op
| _ -> None);
proj =
(function
| Transaction {amount; destination; parameters; entrypoint} ->
let parameters =
if
Script_repr.is_unit_parameter parameters
&& Entrypoint.is_default entrypoint
then None
else Some (entrypoint, parameters)
in
(amount, destination, parameters));
inj =
(fun (amount, destination, parameters) ->
let entrypoint, parameters =
match parameters with
| None -> (Entrypoint.default, Script.unit_parameter)
| Some (entrypoint, value) -> (entrypoint, value)
in
Transaction {amount; destination; parameters; entrypoint});
}
let origination_case =
MCase
{
tag = 2;
name = "origination";
encoding =
obj3
(req "balance" Tez.encoding)
(opt "delegate" Signature.Public_key_hash.encoding)
(req "script" Script.encoding);
iselect : Kind.origination iselect =
(function
| Internal_operation_result
(({operation = Origination _; _} as op), res) ->
Some (op, res)
| _ -> None);
select =
(function
| Internal_operation_contents (Origination _ as op) -> Some op
| _ -> None);
proj =
(function
| Origination {credit; delegate; script} -> (credit, delegate, script));
inj =
(fun (credit, delegate, script) ->
Origination {credit; delegate; script});
}
let delegation_case =
MCase
{
tag = 3;
name = "delegation";
encoding = obj1 (opt "delegate" Signature.Public_key_hash.encoding);
iselect : Kind.delegation iselect =
(function
| Internal_operation_result
(({operation = Delegation _; _} as op), res) ->
Some (op, res)
| _ -> None);
select =
(function
| Internal_operation_contents (Delegation _ as op) -> Some op
| _ -> None);
proj = (function Delegation key -> key);
inj = (fun key -> Delegation key);
}
let event_case =
MCase
{
tag = 4;
name = "event";
encoding =
obj3
(req "type" Script.expr_encoding)
(opt "tag" Entrypoint.smart_encoding)
(opt "payload" Script.expr_encoding);
iselect : Kind.event iselect =
(function
| Internal_operation_result (({operation = Event _; _} as op), res) ->
Some (op, res)
| _ -> None);
select =
(function
| Internal_operation_contents (Event _ as op) -> Some op | _ -> None);
proj =
(function
| Event {ty; tag; payload} ->
let tag = if Entrypoint.is_default tag then None else Some tag in
let payload =
if Script_repr.is_unit payload then None else Some payload
in
(ty, tag, payload));
inj =
(fun (ty, tag, payload) ->
let tag = Option.value ~default:Entrypoint.default tag in
let payload = Option.value ~default:Script_repr.unit payload in
Event {ty; tag; payload});
}
let case tag name args proj inj =
case
tag
~title:(String.capitalize_ascii name)
(merge_objs (obj1 (req "kind" (constant name))) args)
(fun x -> match proj x with None -> None | Some x -> Some ((), x))
(fun ((), x) -> inj x)
let encoding =
let make (MCase {tag; name; encoding; iselect = _; select; proj; inj}) =
case
(Tag tag)
name
encoding
(fun o -> match select o with None -> None | Some o -> Some (proj o))
(fun x -> Internal_operation_contents (inj x))
in
union
~tag_size:`Uint8
[
make transaction_case;
make origination_case;
make delegation_case;
make event_case;
]
end
let internal_operation_encoding : packed_internal_operation Data_encoding.t =
def "apply_internal_results.alpha.operation_result"
@@ conv
(fun (Internal_operation {source; operation; nonce}) ->
((source, nonce), Internal_operation_contents operation))
(fun ((source, nonce), Internal_operation_contents operation) ->
Internal_operation {source; operation; nonce})
(merge_objs
(obj2 (req "source" Destination.encoding) (req "nonce" uint16))
Internal_operation.encoding)
module Internal_operation_result = struct
type 'kind case =
| MCase : {
op_case : 'kind Internal_operation.case;
encoding : 'a Data_encoding.t;
kind : 'kind Kind.manager;
select :
packed_successful_internal_operation_result ->
'kind successful_internal_operation_result option;
proj : 'kind successful_internal_operation_result -> 'a;
inj : 'a -> 'kind successful_internal_operation_result;
t : 'kind internal_operation_result Data_encoding.t;
}
-> 'kind case
let make ~op_case ~encoding ~kind ~select ~proj ~inj =
let (Internal_operation.MCase {name; _}) = op_case in
let t =
def (Format.asprintf "operation.alpha.internal_operation_result.%s" name)
@@ union
~tag_size:`Uint8
[
case
(Tag 0)
~title:"Applied"
(merge_objs (obj1 (req "status" (constant "applied"))) encoding)
(fun o ->
match o with
| Skipped _ | Failed _ | Backtracked _ -> None
| Applied o -> (
match select (Successful_internal_operation_result o) with
| None -> None
| Some o -> Some ((), proj o)))
(fun ((), x) -> Applied (inj x));
case
(Tag 1)
~title:"Failed"
(obj2
(req "status" (constant "failed"))
(req "errors" trace_encoding))
(function Failed (_, errs) -> Some ((), errs) | _ -> None)
(fun ((), errs) -> Failed (kind, errs));
case
(Tag 2)
~title:"Skipped"
(obj1 (req "status" (constant "skipped")))
(function Skipped _ -> Some () | _ -> None)
(fun () -> Skipped kind);
case
(Tag 3)
~title:"Backtracked"
(merge_objs
(obj2
(req "status" (constant "backtracked"))
(opt "errors" trace_encoding))
encoding)
(fun o ->
match o with
| Skipped _ | Failed _ | Applied _ -> None
| Backtracked (o, errs) -> (
match select (Successful_internal_operation_result o) with
| None -> None
| Some o -> Some (((), errs), proj o)))
(fun (((), errs), x) -> Backtracked (inj x, errs));
]
in
MCase {op_case; encoding; kind; select; proj; inj; t}
let transaction_case =
make
~op_case:Internal_operation.transaction_case
~encoding:Internal_operation.transaction_contract_variant_cases
~select:(function
| Successful_internal_operation_result (ITransaction_result _ as op) ->
Some op
| _ -> None)
~kind:Kind.Transaction_manager_kind
~proj:(function ITransaction_result x -> x)
~inj:(fun x -> ITransaction_result x)
let origination_case =
make
~op_case:Internal_operation.origination_case
~encoding:
(obj6
(dft "balance_updates" Receipt.balance_updates_encoding [])
(dft "originated_contracts" (list Contract.originated_encoding) [])
(dft "consumed_milligas" Gas.Arith.n_fp_encoding Gas.Arith.zero)
(dft "storage_size" z Z.zero)
(dft "paid_storage_size_diff" z Z.zero)
(opt "lazy_storage_diff" Lazy_storage.encoding))
~select:(function
| Successful_internal_operation_result (IOrigination_result _ as op) ->
Some op
| _ -> None)
~proj:(function
| IOrigination_result
{
lazy_storage_diff;
balance_updates;
originated_contracts;
consumed_gas;
storage_size;
paid_storage_size_diff;
} ->
( balance_updates,
originated_contracts,
consumed_gas,
storage_size,
paid_storage_size_diff,
lazy_storage_diff ))
~kind:Kind.Origination_manager_kind
~inj:
(fun ( balance_updates,
originated_contracts,
consumed_gas,
storage_size,
paid_storage_size_diff,
lazy_storage_diff ) ->
IOrigination_result
{
lazy_storage_diff;
balance_updates;
originated_contracts;
consumed_gas;
storage_size;
paid_storage_size_diff;
})
let delegation_case =
make
~op_case:Internal_operation.delegation_case
~encoding:
Data_encoding.(
obj1 (dft "consumed_milligas" Gas.Arith.n_fp_encoding Gas.Arith.zero))
~select:(function
| Successful_internal_operation_result (IDelegation_result _ as op) ->
Some op
| _ -> None)
~kind:Kind.Delegation_manager_kind
~proj:(function IDelegation_result {consumed_gas} -> consumed_gas)
~inj:(fun consumed_gas -> IDelegation_result {consumed_gas})
let event_case =
make
~op_case:Internal_operation.event_case
~encoding:
Data_encoding.(
obj1 (dft "consumed_milligas" Gas.Arith.n_fp_encoding Gas.Arith.zero))
~select:(function
| Successful_internal_operation_result (IEvent_result _ as op) ->
Some op
| _ -> None)
~kind:Kind.Event_manager_kind
~proj:(function IEvent_result {consumed_gas} -> consumed_gas)
~inj:(fun consumed_gas -> IEvent_result {consumed_gas})
end
let internal_operation_result_encoding :
packed_internal_operation_result Data_encoding.t =
let make (type kind)
(Internal_operation_result.MCase res_case :
kind Internal_operation_result.case)
(Internal_operation.MCase ires_case : kind Internal_operation.case) =
let (Internal_operation.MCase op_case) = res_case.op_case in
case
(Tag op_case.tag)
~title:op_case.name
(merge_objs
(obj3
(req "kind" (constant op_case.name))
(req "source" Destination.encoding)
(req "nonce" uint16))
(merge_objs ires_case.encoding (obj1 (req "result" res_case.t))))
(fun op ->
match ires_case.iselect op with
| Some (op, res) ->
Some (((), op.source, op.nonce), (ires_case.proj op.operation, res))
| None -> None)
(fun (((), source, nonce), (op, res)) ->
let op = {source; operation = ires_case.inj op; nonce} in
Internal_operation_result (op, res))
in
def "apply_internal_results.alpha.operation_result"
@@ union
[
make
Internal_operation_result.transaction_case
Internal_operation.transaction_case;
make
Internal_operation_result.origination_case
Internal_operation.origination_case;
make
Internal_operation_result.delegation_case
Internal_operation.delegation_case;
make Internal_operation_result.event_case Internal_operation.event_case;
]