Source file ticket_scanner.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
open Alpha_context
type error +=
| Unsupported_non_empty_overlay
| Unsupported_type_operation
let () =
register_error_kind
`Branch
~id:"Unsupported_non_empty_overlay"
~title:"Unsupported non empty overlay"
~description:"Unsupported big-map value with non-empty overlay"
~pp:(fun ppf () ->
Format.fprintf ppf "Unsupported big-map value with non-empty overlay")
Data_encoding.empty
(function Unsupported_non_empty_overlay -> Some () | _ -> None)
(fun () -> Unsupported_non_empty_overlay) ;
register_error_kind
`Branch
~id:"Unsupported_type_operation"
~title:"Unsupported type operation"
~description:"Types embedding operations are not supported"
~pp:(fun ppf () ->
Format.fprintf ppf "Types embedding operations are not supported")
Data_encoding.empty
(function Unsupported_type_operation -> Some () | _ -> None)
(fun () -> Unsupported_type_operation)
type ex_ticket =
| Ex_ticket :
'a Script_typed_ir.comparable_ty * 'a Script_typed_ir.ticket
-> ex_ticket
module Ticket_inspection = struct
(**
Witness flag for whether a type can be populated by a value containing a
ticket. [False_ht] must be used only when a value of the type cannot
contain a ticket.
This flag is necessary for avoiding ticket collection (see below) to have
quadratic complexity in the order of: size-of-the-type * size-of-value.
This type is local to the [Ticket_scanner] module and should not be
exported.
*)
type 'a has_tickets =
| True_ht : _ Script_typed_ir.ticket has_tickets
| False_ht : _ has_tickets
| Pair_ht :
'a has_tickets * 'b has_tickets
-> ('a, 'b) Script_typed_ir.pair has_tickets
| Or_ht :
'a has_tickets * 'b has_tickets
-> ('a, 'b) Script_typed_ir.or_ has_tickets
| Option_ht : 'a has_tickets -> 'a option has_tickets
| List_ht : 'a has_tickets -> 'a Script_list.t has_tickets
| Set_ht : 'k has_tickets -> 'k Script_typed_ir.set has_tickets
| Map_ht :
'k has_tickets * 'v has_tickets
-> ('k, 'v) Script_typed_ir.map has_tickets
| Big_map_ht :
'k has_tickets * 'v has_tickets
-> ('k, 'v) Script_typed_ir.big_map has_tickets
let has_tickets_of_comparable :
type a ret.
a Script_typed_ir.comparable_ty -> (a has_tickets -> ret) -> ret =
fun key_ty k ->
let open Script_typed_ir in
match key_ty with
| Unit_t -> (k [@ocaml.tailcall]) False_ht
| Never_t -> (k [@ocaml.tailcall]) False_ht
| Int_t -> (k [@ocaml.tailcall]) False_ht
| Nat_t -> (k [@ocaml.tailcall]) False_ht
| Signature_t -> (k [@ocaml.tailcall]) False_ht
| String_t -> (k [@ocaml.tailcall]) False_ht
| Bytes_t -> (k [@ocaml.tailcall]) False_ht
| Mutez_t -> (k [@ocaml.tailcall]) False_ht
| Bool_t -> (k [@ocaml.tailcall]) False_ht
| Key_hash_t -> (k [@ocaml.tailcall]) False_ht
| Key_t -> (k [@ocaml.tailcall]) False_ht
| Timestamp_t -> (k [@ocaml.tailcall]) False_ht
| Chain_id_t -> (k [@ocaml.tailcall]) False_ht
| Address_t -> (k [@ocaml.tailcall]) False_ht
| Pair_t (_, _, _, YesYes) -> (k [@ocaml.tailcall]) False_ht
| Or_t (_, _, _, YesYes) -> (k [@ocaml.tailcall]) False_ht
| Option_t (_, _, Yes) -> (k [@ocaml.tailcall]) False_ht
let pair_has_tickets pair ht1 ht2 =
match (ht1, ht2) with False_ht, False_ht -> False_ht | _ -> pair ht1 ht2
let map_has_tickets map ht =
match ht with False_ht -> False_ht | _ -> map ht
type ('a, 'r) continuation = 'a has_tickets -> 'r tzresult
let rec has_tickets_of_ty :
type a ac ret.
(a, ac) Script_typed_ir.ty -> (a, ret) continuation -> ret tzresult =
fun ty k ->
let open Script_typed_ir in
match ty with
| Ticket_t _ -> (k [@ocaml.tailcall]) True_ht
| Unit_t -> (k [@ocaml.tailcall]) False_ht
| Int_t -> (k [@ocaml.tailcall]) False_ht
| Nat_t -> (k [@ocaml.tailcall]) False_ht
| Signature_t -> (k [@ocaml.tailcall]) False_ht
| String_t -> (k [@ocaml.tailcall]) False_ht
| Bytes_t -> (k [@ocaml.tailcall]) False_ht
| Mutez_t -> (k [@ocaml.tailcall]) False_ht
| Key_hash_t -> (k [@ocaml.tailcall]) False_ht
| Key_t -> (k [@ocaml.tailcall]) False_ht
| Timestamp_t -> (k [@ocaml.tailcall]) False_ht
| Address_t -> (k [@ocaml.tailcall]) False_ht
| Bool_t -> (k [@ocaml.tailcall]) False_ht
| Pair_t (ty1, ty2, _, _) ->
(has_tickets_of_pair [@ocaml.tailcall])
ty1
ty2
~pair:(fun ht1 ht2 -> Pair_ht (ht1, ht2))
k
| Or_t (ty1, ty2, _, _) ->
(has_tickets_of_pair [@ocaml.tailcall])
ty1
ty2
~pair:(fun ht1 ht2 -> Or_ht (ht1, ht2))
k
| Lambda_t (_, _, _) ->
(k [@ocaml.tailcall]) False_ht
| Option_t (ty, _, _) ->
(has_tickets_of_ty [@ocaml.tailcall]) ty (fun ht ->
let opt_hty = map_has_tickets (fun ht -> Option_ht ht) ht in
(k [@ocaml.tailcall]) opt_hty)
| List_t (ty, _) ->
(has_tickets_of_ty [@ocaml.tailcall]) ty (fun ht ->
let list_hty = map_has_tickets (fun ht -> List_ht ht) ht in
(k [@ocaml.tailcall]) list_hty)
| Set_t (key_ty, _) ->
(has_tickets_of_comparable [@ocaml.tailcall]) key_ty (fun ht ->
let set_hty = map_has_tickets (fun ht -> Set_ht ht) ht in
(k [@ocaml.tailcall]) set_hty)
| Map_t (key_ty, val_ty, _) ->
(has_tickets_of_key_and_value [@ocaml.tailcall])
key_ty
val_ty
~pair:(fun ht1 ht2 -> Map_ht (ht1, ht2))
k
| Big_map_t (key_ty, val_ty, _) ->
(has_tickets_of_key_and_value [@ocaml.tailcall])
key_ty
val_ty
~pair:(fun ht1 ht2 -> Big_map_ht (ht1, ht2))
k
| Contract_t _ -> (k [@ocaml.tailcall]) False_ht
| Sapling_transaction_t _ -> (k [@ocaml.tailcall]) False_ht
| Sapling_transaction_deprecated_t _ -> (k [@ocaml.tailcall]) False_ht
| Sapling_state_t _ -> (k [@ocaml.tailcall]) False_ht
| Operation_t ->
Result_syntax.tzfail Unsupported_type_operation
| Chain_id_t -> (k [@ocaml.tailcall]) False_ht
| Never_t -> (k [@ocaml.tailcall]) False_ht
| Bls12_381_g1_t -> (k [@ocaml.tailcall]) False_ht
| Bls12_381_g2_t -> (k [@ocaml.tailcall]) False_ht
| Bls12_381_fr_t -> (k [@ocaml.tailcall]) False_ht
| Chest_t -> (k [@ocaml.tailcall]) False_ht
| Chest_key_t -> (k [@ocaml.tailcall]) False_ht
and has_tickets_of_pair :
type a ac b bc c ret.
(a, ac) Script_typed_ir.ty ->
(b, bc) Script_typed_ir.ty ->
pair:(a has_tickets -> b has_tickets -> c has_tickets) ->
(c, ret) continuation ->
ret tzresult =
fun ty1 ty2 ~pair k ->
(has_tickets_of_ty [@ocaml.tailcall]) ty1 (fun ht1 ->
(has_tickets_of_ty [@ocaml.tailcall]) ty2 (fun ht2 ->
(k [@ocaml.tailcall]) (pair_has_tickets pair ht1 ht2)))
and has_tickets_of_key_and_value :
type k v vc t ret.
k Script_typed_ir.comparable_ty ->
(v, vc) Script_typed_ir.ty ->
pair:(k has_tickets -> v has_tickets -> t has_tickets) ->
(t, ret) continuation ->
ret tzresult =
fun key_ty val_ty ~pair k ->
(has_tickets_of_comparable [@ocaml.tailcall]) key_ty (fun ht1 ->
(has_tickets_of_ty [@ocaml.tailcall]) val_ty (fun ht2 ->
(k [@ocaml.tailcall]) (pair_has_tickets pair ht1 ht2)))
let has_tickets_of_ty ctxt ty =
let open Result_syntax in
let* ctxt = Gas.consume ctxt (Ticket_costs.has_tickets_of_ty_cost ty) in
let+ ht = has_tickets_of_ty ty return in
(ht, ctxt)
end
module Ticket_collection = struct
type accumulator = ex_ticket list
type 'a continuation = context -> accumulator -> 'a tzresult Lwt.t
let tickets_of_comparable :
type a ret.
context ->
a Script_typed_ir.comparable_ty ->
accumulator ->
ret continuation ->
ret tzresult Lwt.t =
fun ctxt comp_ty acc k ->
let open Script_typed_ir in
match comp_ty with
| Unit_t -> (k [@ocaml.tailcall]) ctxt acc
| Never_t -> (k [@ocaml.tailcall]) ctxt acc
| Int_t -> (k [@ocaml.tailcall]) ctxt acc
| Nat_t -> (k [@ocaml.tailcall]) ctxt acc
| Signature_t -> (k [@ocaml.tailcall]) ctxt acc
| String_t -> (k [@ocaml.tailcall]) ctxt acc
| Bytes_t -> (k [@ocaml.tailcall]) ctxt acc
| Mutez_t -> (k [@ocaml.tailcall]) ctxt acc
| Bool_t -> (k [@ocaml.tailcall]) ctxt acc
| Key_hash_t -> (k [@ocaml.tailcall]) ctxt acc
| Key_t -> (k [@ocaml.tailcall]) ctxt acc
| Timestamp_t -> (k [@ocaml.tailcall]) ctxt acc
| Chain_id_t -> (k [@ocaml.tailcall]) ctxt acc
| Address_t -> (k [@ocaml.tailcall]) ctxt acc
| Pair_t (_, _, _, YesYes) -> (k [@ocaml.tailcall]) ctxt acc
| Or_t (_, _, _, YesYes) -> (k [@ocaml.tailcall]) ctxt acc
| Option_t (_, _, Yes) -> (k [@ocaml.tailcall]) ctxt acc
let tickets_of_set :
type a ret.
context ->
a Script_typed_ir.comparable_ty ->
a Script_typed_ir.set ->
accumulator ->
ret continuation ->
ret tzresult Lwt.t =
let open Lwt_result_syntax in
fun ctxt key_ty _set acc k ->
let*? ctxt = Ticket_costs.consume_gas_steps ctxt ~num_steps:1 in
(tickets_of_comparable [@ocaml.tailcall]) ctxt key_ty acc k
let rec tickets_of_value :
type a ac ret.
include_lazy:bool ->
context ->
a Ticket_inspection.has_tickets ->
(a, ac) Script_typed_ir.ty ->
a ->
accumulator ->
ret continuation ->
ret tzresult Lwt.t =
let open Lwt_result_syntax in
fun ~include_lazy ctxt hty ty x acc k ->
let open Script_typed_ir in
let*? ctxt = Ticket_costs.consume_gas_steps ctxt ~num_steps:1 in
match (hty, ty) with
| False_ht, _ -> (k [@ocaml.tailcall]) ctxt acc
| Pair_ht (hty1, hty2), Pair_t (ty1, ty2, _, _) ->
let l, r = x in
(tickets_of_value [@ocaml.tailcall])
~include_lazy
ctxt
hty1
ty1
l
acc
(fun ctxt acc ->
(tickets_of_value [@ocaml.tailcall])
~include_lazy
ctxt
hty2
ty2
r
acc
k)
| Or_ht (htyl, htyr), Or_t (tyl, tyr, _, _) -> (
match x with
| L v ->
(tickets_of_value [@ocaml.tailcall])
~include_lazy
ctxt
htyl
tyl
v
acc
k
| R v ->
(tickets_of_value [@ocaml.tailcall])
~include_lazy
ctxt
htyr
tyr
v
acc
k)
| Option_ht el_hty, Option_t (el_ty, _, _) -> (
match x with
| Some x ->
(tickets_of_value [@ocaml.tailcall])
~include_lazy
ctxt
el_hty
el_ty
x
acc
k
| None -> (k [@ocaml.tailcall]) ctxt acc)
| List_ht el_hty, List_t (el_ty, _) ->
let elements = Script_list.to_list x in
(tickets_of_list [@ocaml.tailcall])
ctxt
~include_lazy
el_hty
el_ty
elements
acc
k
| Set_ht _, Set_t (key_ty, _) ->
(tickets_of_set [@ocaml.tailcall]) ctxt key_ty x acc k
| Map_ht (_, val_hty), Map_t (key_ty, val_ty, _) ->
(tickets_of_comparable [@ocaml.tailcall])
ctxt
key_ty
acc
(fun ctxt acc ->
(tickets_of_map [@ocaml.tailcall])
ctxt
~include_lazy
val_hty
val_ty
x
acc
k)
| Big_map_ht (_, val_hty), Big_map_t (key_ty, _, _) ->
if include_lazy then
(tickets_of_big_map [@ocaml.tailcall]) ctxt val_hty key_ty x acc k
else (k [@ocaml.tailcall]) ctxt acc
| True_ht, Ticket_t (comp_ty, _) ->
(k [@ocaml.tailcall]) ctxt (Ex_ticket (comp_ty, x) :: acc)
and tickets_of_list :
type a ac ret.
context ->
include_lazy:bool ->
a Ticket_inspection.has_tickets ->
(a, ac) Script_typed_ir.ty ->
a list ->
accumulator ->
ret continuation ->
ret tzresult Lwt.t =
let open Lwt_result_syntax in
fun ctxt ~include_lazy el_hty el_ty elements acc k ->
let*? ctxt = Ticket_costs.consume_gas_steps ctxt ~num_steps:1 in
match elements with
| elem :: elems ->
(tickets_of_value [@ocaml.tailcall])
~include_lazy
ctxt
el_hty
el_ty
elem
acc
(fun ctxt acc ->
(tickets_of_list [@ocaml.tailcall])
~include_lazy
ctxt
el_hty
el_ty
elems
acc
k)
| [] -> (k [@ocaml.tailcall]) ctxt acc
and tickets_of_map :
type k v vc ret.
include_lazy:bool ->
context ->
v Ticket_inspection.has_tickets ->
(v, vc) Script_typed_ir.ty ->
(k, v) Script_typed_ir.map ->
accumulator ->
ret continuation ->
ret tzresult Lwt.t =
let open Lwt_result_syntax in
fun ~include_lazy ctxt val_hty val_ty map acc k ->
let (module M) = Script_map.get_module map in
let*? ctxt = Ticket_costs.consume_gas_steps ctxt ~num_steps:1 in
let*? ctxt = Ticket_costs.consume_gas_steps ctxt ~num_steps:M.size in
let values = M.OPS.fold (fun _ v vs -> v :: vs) M.boxed [] in
(tickets_of_list [@ocaml.tailcall])
~include_lazy
ctxt
val_hty
val_ty
values
acc
k
and tickets_of_big_map :
type k v ret.
context ->
v Ticket_inspection.has_tickets ->
k Script_typed_ir.comparable_ty ->
(k, v) Script_typed_ir.big_map ->
accumulator ->
ret continuation ->
ret tzresult Lwt.t =
let open Lwt_result_syntax in
fun ctxt
val_hty
key_ty
(Big_map {id; diff = {map = _; size}; key_type = _; value_type})
acc
k ->
let*? ctxt = Ticket_costs.consume_gas_steps ctxt ~num_steps:1 in
if Compare.Int.(size > 0) then tzfail Unsupported_non_empty_overlay
else
(tickets_of_comparable [@ocaml.tailcall])
ctxt
key_ty
acc
(fun ctxt acc ->
match id with
| Some id ->
let accum (values, ctxt) (_key_hash, exp) =
let+ v, ctxt =
Script_ir_translator.parse_data
~elab_conf:
Script_ir_translator_config.(make ~legacy:true ())
ctxt
~allow_forged:true
value_type
(Micheline.root exp)
in
(v :: values, ctxt)
in
let* ctxt, exps = Big_map.list_key_values ctxt id in
let* values, ctxt = List.fold_left_es accum ([], ctxt) exps in
(tickets_of_list [@ocaml.tailcall])
~include_lazy:true
ctxt
val_hty
value_type
values
acc
k
| None -> (k [@ocaml.tailcall]) ctxt acc)
let tickets_of_value ctxt ~include_lazy ht ty x =
tickets_of_value ctxt ~include_lazy ht ty x [] (fun ctxt ex_tickets ->
return (ex_tickets, ctxt))
end
type 'a has_tickets =
| Has_tickets :
'a Ticket_inspection.has_tickets * ('a, _) Script_typed_ir.ty
-> 'a has_tickets
let type_has_tickets ctxt ty =
let open Result_syntax in
let+ has_tickets, ctxt = Ticket_inspection.has_tickets_of_ty ctxt ty in
(Has_tickets (has_tickets, ty), ctxt)
let tickets_of_value ctxt ~include_lazy (Has_tickets (ht, ty)) =
Ticket_collection.tickets_of_value ctxt ~include_lazy ht ty
let has_tickets (Has_tickets (ht, _)) =
match ht with Ticket_inspection.False_ht -> false | _ -> true
let tickets_of_node ctxt ~include_lazy has_tickets expr =
let (Has_tickets (ht, ty)) = has_tickets in
let open Lwt_result_syntax in
match ht with
| Ticket_inspection.False_ht -> return ([], ctxt)
| _ ->
let* value, ctxt =
Script_ir_translator.parse_data
ctxt
~elab_conf:Script_ir_translator_config.(make ~legacy:true ())
~allow_forged:true
ty
expr
in
tickets_of_value ctxt ~include_lazy has_tickets value
let ex_ticket_size ctxt (Ex_ticket (ty, ticket)) =
let open Lwt_result_syntax in
let*? ty = Script_typed_ir.ticket_t Micheline.dummy_location ty in
let*? ty', ctxt = Script_ir_unparser.unparse_ty ~loc:() ctxt ty in
let ty_nodes, ty_size = Script_typed_ir_size.node_size ty' in
let ty_size_cost = Script_typed_ir_size_costs.nodes_cost ~nodes:ty_nodes in
let*? ctxt = Gas.consume ctxt ty_size_cost in
let val_nodes, val_size = Script_typed_ir_size.value_size ty ticket in
let val_size_cost = Script_typed_ir_size_costs.nodes_cost ~nodes:val_nodes in
let*? ctxt = Gas.consume ctxt val_size_cost in
return (Saturation_repr.add ty_size val_size, ctxt)
let ex_token_and_amount_of_ex_ticket
(Ex_ticket (contents_type, {Script_typed_ir.ticketer; contents; amount})) =
(Ticket_token.Ex_token {ticketer; contents_type; contents}, amount)