Source file michelson_v1_stack.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
open Tezos_micheline
open Protocol
open Alpha_context
type localized_node = {
parser_loc : Micheline_parser.location option;
canonical_loc : Micheline.canonical_location;
node : string Micheline.canonical;
}
let print_localized_node_location fmt localized_node =
match localized_node.parser_loc with
| Some parser_loc ->
Format.fprintf
fmt
"%s"
(Format.kasprintf
String.capitalize_ascii
"%a"
Micheline_parser.print_location
parser_loc)
| None -> Format.fprintf fmt "At position %d" localized_node.canonical_loc
let print_localized_node fmt localized_node =
Micheline_printer.print_expr_unwrapped
fmt
(Micheline_printer.printable Fun.id localized_node.node)
let localize_node ~(parsed : string Michelson_v1_parser.parser_result)
(n : (Micheline.canonical_location, string) Micheline.node) : localized_node
=
let canonical_loc = Micheline.location n in
let parser_loc =
let open Option_syntax in
let* oloc =
List.assoc ~equal:Int.equal canonical_loc parsed.unexpansion_table
in
let+ ploc, _ = List.assoc ~equal:Int.equal oloc parsed.expansion_table in
ploc
in
{parser_loc; canonical_loc; node = Micheline.strip_locations n}
let localized_node_encoding : localized_node Data_encoding.t =
Data_encoding.(
conv
(fun {parser_loc; canonical_loc; node} ->
(parser_loc, canonical_loc, node))
(fun (parser_loc, canonical_loc, node) ->
{parser_loc; canonical_loc; node})
(obj3
(req "parser_location" (option Micheline_parser.location_encoding))
(req
"canonical_location"
Micheline_encoding.canonical_location_encoding)
(req
"node"
(Micheline_encoding.canonical_encoding
~variant:"alpha_client"
Data_encoding.string))))
type error +=
| Wrong_stack_item of localized_node
| Wrong_stack of localized_node
| Wrong_other_contracts_item of localized_node
| Wrong_other_contracts of localized_node
| Invalid_address_for_smart_contract of string
| Duplicated_tzt_top_prim of string * localized_node
| Wrong_tzt_top_prim_arity of string * localized_node * int
| Unknown_tzt_top_prim of string * localized_node
| Missing_mandatory_tzt_top_prim of string
| Invalid_format_for_tzt_top_prim of string * localized_node
| Invalid_tzt_toplevel of localized_node
let () =
Protocol_client_context.register_error_kind
`Permanent
~id:"michelson.stack.wrong_stack_item"
~title:"Wrong stack item"
~description:"Failed to parse an item in a typed stack."
~pp:(fun ppf node ->
Format.fprintf
ppf
"%a,@ Unexpected format for an item in a typed stack. Expected: \
Stack_elt <ty> <value>; got %a."
print_localized_node_location
node
print_localized_node
node)
localized_node_encoding
(function Wrong_stack_item node -> Some node | _ -> None)
(fun node -> Wrong_stack_item node) ;
Protocol_client_context.register_error_kind
`Permanent
~id:"michelson.stack.wrong_stack"
~title:"Wrong stack"
~description:"Failed to parse a typed stack."
~pp:(fun ppf node ->
Format.fprintf
ppf
"%a,@ Unexpected format for a typed stack. Expected a sequence of \
Stack_elt <ty> <value>; got %a."
print_localized_node_location
node
print_localized_node
node)
localized_node_encoding
(function Wrong_stack node -> Some node | _ -> None)
(fun node -> Wrong_stack node) ;
Protocol_client_context.register_error_kind
`Permanent
~id:"michelson.wrong_other_contracts_item"
~title:"Wrong description of an other contract"
~description:"Failed to parse an item in a description of other contracts."
~pp:(fun ppf node ->
Format.fprintf
ppf
"%a,@ Unexpected format for an item in a description of other \
contracts. Expected: Contract <address> <ty>; got %a."
print_localized_node_location
node
print_localized_node
node)
localized_node_encoding
(function Wrong_other_contracts_item node -> Some node | _ -> None)
(fun node -> Wrong_other_contracts_item node) ;
Protocol_client_context.register_error_kind
`Permanent
~id:"michelson.wrong_other_contracts"
~title:"Wrong description of a list of other contracts"
~description:"Failed to parse a description of other contracts."
~pp:(fun ppf node ->
Format.fprintf
ppf
"%a,@ Unexpected format for a description of other contracts. Expected \
a sequence of Contract <address> <ty>; got %a."
print_localized_node_location
node
print_localized_node
node)
localized_node_encoding
(function Wrong_other_contracts node -> Some node | _ -> None)
(fun node -> Wrong_other_contracts node) ;
Protocol_client_context.register_error_kind
`Permanent
~id:"michelson.wrong_extra_big_maps_item"
~title:"Wrong description of an extra big map"
~description:"Failed to parse an item in a description of extra big maps."
~pp:(fun ppf node ->
Format.fprintf
ppf
"%a,@ Unexpected format for an item in a description of extra big \
maps. Expected: Big_map <index> <key_type> <value_type> <content>; \
got %a."
print_localized_node_location
node
print_localized_node
node)
localized_node_encoding
(function Wrong_extra_big_maps_item node -> Some node | _ -> None)
(fun node -> Wrong_extra_big_maps_item node) ;
Protocol_client_context.register_error_kind
`Permanent
~id:"michelson.wrong_extra_big_maps"
~title:"Wrong description of a list of extra big maps"
~description:"Failed to parse a description of extra big maps."
~pp:(fun ppf node ->
Format.fprintf
ppf
"%a,@ Unexpected format for a description of extra big maps. Expected \
a sequence of Big_map <index> <key_type> <value_type> <content>; got \
%a."
print_localized_node_location
node
print_localized_node
node)
localized_node_encoding
(function Wrong_extra_big_maps node -> Some node | _ -> None)
(fun node -> Wrong_extra_big_maps node) ;
Protocol_client_context.register_error_kind
`Permanent
~id:"InvalidAddressForSmartContract"
~title:"Invalid address for smart contract"
~description:
"Invalid input, expected a smart contract address in base58 check \
notation (KT1...)"
Data_encoding.(obj1 (req "invalid_address" string))
~pp:(fun ppf literal ->
Format.fprintf
ppf
"Bad argument value for a smart contract address. Expected an address \
in base58 checked notation starting with 'KT1', but given '%s'"
literal)
(function Invalid_address_for_smart_contract str -> Some str | _ -> None)
(fun str -> Invalid_address_for_smart_contract str) ;
Protocol_client_context.register_error_kind
`Permanent
~id:"tzt.wrong_toplevel_arity"
~title:"Wrong arity for a TZT toplevel primitive"
~description:"A known toplevel TZT primitive was used with a bad arity."
~pp:(fun ppf (prim, node, arity) ->
Format.fprintf
ppf
"%a,@ Wrong arity for TZT toplevel primitive %s, expected %d \
arguments, got %a"
print_localized_node_location
node
prim
arity
print_localized_node
node)
Data_encoding.(
obj3
(req "prim" string)
(req "node" localized_node_encoding)
(req "arity" int16))
(function
| Wrong_tzt_top_prim_arity (prim, node, arity) -> Some (prim, node, arity)
| _ -> None)
(fun (prim, node, arity) -> Wrong_tzt_top_prim_arity (prim, node, arity)) ;
Protocol_client_context.register_error_kind
`Permanent
~id:"tzt.duplicated_toplevel"
~title:"Duplicated TZT toplevel primitive"
~description:"A toplevel TZT primitive was used several times."
~pp:(fun ppf (prim, node) ->
Format.fprintf
ppf
"%a,@ The TZT toplevel primitive %s, cannot be used because it has \
already been used. A TZT toplevel primitive can only be used once per \
unit test."
print_localized_node_location
node
prim)
Data_encoding.(
obj2 (req "prim" string) (req "node" localized_node_encoding))
(function
| Duplicated_tzt_top_prim (prim, node) -> Some (prim, node) | _ -> None)
(fun (prim, node) -> Duplicated_tzt_top_prim (prim, node)) ;
Protocol_client_context.register_error_kind
`Permanent
~id:"tzt.unknown_toplevel"
~title:"Unknown TZT toplevel primitive"
~description:"A toplevel TZT primitive was unknown."
~pp:(fun ppf (prim, node) ->
Format.fprintf
ppf
"%a,@ The TZT toplevel primitive %s is unknown."
print_localized_node_location
node
prim)
Data_encoding.(
obj2 (req "prim" string) (req "node" localized_node_encoding))
(function
| Unknown_tzt_top_prim (prim, node) -> Some (prim, node) | _ -> None)
(fun (prim, node) -> Unknown_tzt_top_prim (prim, node)) ;
Protocol_client_context.register_error_kind
`Permanent
~id:"tzt.missing_mandatory"
~title:"Missing TZT mandatory toplevel primitive"
~description:"A mandatory toplevel TZT primitive was missing."
~pp:(fun ppf prim ->
Format.fprintf
ppf
"The mandatory TZT toplevel primitive %s is missing."
prim)
Data_encoding.(obj1 (req "prim" string))
(function Missing_mandatory_tzt_top_prim prim -> Some prim | _ -> None)
(fun prim -> Missing_mandatory_tzt_top_prim prim) ;
Protocol_client_context.register_error_kind
`Permanent
~id:"tzt.invalid_format"
~title:"Invalid format for a TZT toplevel primitive"
~description:"Invalid format for a TZT toplevel primitive"
~pp:(fun ppf (prim, node) ->
Format.fprintf
ppf
"%a,@ Invalid format for TZT toplevel primitive %s."
print_localized_node_location
node
prim)
Data_encoding.(
obj2 (req "prim" string) (req "node" localized_node_encoding))
(function
| Invalid_format_for_tzt_top_prim (prim, node) -> Some (prim, node)
| _ -> None)
(fun (prim, node) -> Invalid_format_for_tzt_top_prim (prim, node)) ;
Protocol_client_context.register_error_kind
`Permanent
~id:"tzt.invalid_toplevel"
~title:"Invalid format for TZT toplevel entry"
~description:"Invalid format for a TZT toplevel entry"
~pp:(fun ppf node ->
Format.fprintf
ppf
"%a,@ Invalid format for TZT toplevel entry, expected a sequence of \
primitive applications, got %a."
print_localized_node_location
node
print_localized_node
node)
localized_node_encoding
(function Invalid_tzt_toplevel node -> Some node | _ -> None)
(fun node -> Invalid_tzt_toplevel node)
let parse_expression (node : (_, string) Micheline.node) =
Environment.wrap_tzresult
@@ Michelson_v1_primitives.prims_of_strings (Micheline.strip_locations node)
let parse_stack_item ~parsed =
let open Result_syntax in
function
| Micheline.Prim (_loc, "Stack_elt", [ty; v], _annot) ->
let* ty = parse_expression ty in
let* v = parse_expression v in
return (ty, v)
| e -> tzfail (Wrong_stack_item (localize_node ~parsed e))
let parse_other_contract_item ~parsed =
let open Result_syntax in
function
| Micheline.Prim (_loc, "Contract", [address; ty], _annot) as e ->
let* address = parse_expression address in
let* address =
match Micheline.root address with
| Micheline.String (_loc, s) -> (
match Environment.Base58.decode s with
| Some (Contract_hash.Data h) -> return h
| Some _ | None -> tzfail (Invalid_address_for_smart_contract s))
| _ -> tzfail (Wrong_other_contracts_item (localize_node ~parsed e))
in
let* ty = parse_expression ty in
return RPC.Scripts.S.{address; ty}
| e -> tzfail (Wrong_other_contracts_item (localize_node ~parsed e))
let ~parsed =
let open Result_syntax in
function
| Micheline.Prim (_loc, "Big_map", [id; kty; vty; items], _annot) as e ->
let* id = parse_expression id in
let* id =
match Micheline.root id with
| Micheline.Int (_loc, id) -> return (Big_map.Id.parse_z id)
| _ -> tzfail (Wrong_other_contracts_item (localize_node ~parsed e))
in
let* kty = parse_expression kty in
let* vty = parse_expression vty in
let* items = parse_expression items in
return RPC.Scripts.S.{id; kty; vty; items}
| e -> tzfail (Wrong_extra_big_maps_item (localize_node ~parsed e))
let parse_sequence ?node ~(parsed : string Michelson_v1_parser.parser_result)
~error parse_item =
let node = Option.value ~default:(Micheline.root parsed.expanded) node in
let error () = error (localize_node ~parsed node) in
match node with
| Micheline.Seq (_loc, l) ->
record_trace_eval error @@ List.map_e (parse_item ~parsed) l
| _ -> Result_syntax.tzfail (error ())
let parse_stack ?node parsed =
parse_sequence
?node
~parsed
~error:(fun node -> Wrong_stack node)
parse_stack_item
let parse_other_contracts ?node parsed =
parse_sequence
?node
~parsed
~error:(fun node -> Wrong_other_contracts node)
parse_other_contract_item
let ?node parsed =
parse_sequence
?node
~parsed
~error:(fun node -> Wrong_extra_big_maps node)
parse_extra_big_map_item
type unit_test_optional = {
now : Script_timestamp.t option;
level : Script_int.n Script_int.num option;
sender : Contract.t option;
source : Signature.public_key_hash option;
chain_id : Chain_id.t option;
self : Contract_hash.t option;
parameter : Script.expr option;
amount : Tez.t option;
balance : Tez.t option;
other_contracts : RPC.Scripts.S.other_contract_description list option;
extra_big_maps : RPC.Scripts.S.extra_big_map_description list option;
}
type unit_test = {
input : (Script.expr * Script.expr) list;
code : Script.expr;
output : (Micheline.canonical_location, string) Micheline.node;
optional : unit_test_optional;
}
type temp_unit_test = {
temp_input : (Script.expr * Script.expr) list option;
temp_code : Script.expr option;
temp_output : (Micheline.canonical_location, string) Micheline.node option;
temp_optional : unit_test_optional;
}
let value_fe_err opt ~error =
Option.value_fe opt ~error:(fun () -> TzTrace.make (error ()))
let parse_mutez node ~error =
let mutez_opt =
match node with
| Micheline.Int (_loc, z) -> Tez.of_mutez (Z.to_int64 z)
| _ -> None
in
value_fe_err mutez_opt ~error
let parse_chain_id node ~error =
match node with
| Micheline.String (_loc, s) ->
record_trace_eval error @@ Chain_id.of_b58check s
| Bytes (_loc, b) ->
value_fe_err ~error
@@ Data_encoding.Binary.of_bytes_opt Chain_id.encoding b
| _ -> Result_syntax.tzfail @@ error ()
let parse_timestamp node ~error =
value_fe_err ~error
@@
match node with
| Micheline.String (_loc, s) -> Script_timestamp.of_string s
| Int (_loc, z) -> Some (Script_timestamp.of_zint z)
| _ -> None
let parse_nat node ~error =
value_fe_err ~error
@@
match node with
| Micheline.Int (_loc, z) -> Script_int.(is_nat (of_zint z))
| _ -> None
let parse_key_hash node ~error =
match node with
| Micheline.String (_loc, s) ->
record_trace_eval error @@ Signature.Public_key_hash.of_b58check s
| Bytes (_loc, b) ->
value_fe_err ~error
@@ Data_encoding.Binary.of_bytes_opt Signature.Public_key_hash.encoding b
| _ -> Result_syntax.tzfail @@ error ()
let parse_address node ~error =
match node with
| Micheline.String (_loc, s) ->
record_trace_eval error @@ Environment.wrap_tzresult
@@ Contract.of_b58check s
| Bytes (_loc, b) ->
value_fe_err ~error
@@ Data_encoding.Binary.of_bytes_opt Contract.encoding b
| _ -> Result_syntax.tzfail @@ error ()
let parse_contract_hash node ~error =
value_fe_err ~error
@@
match node with
| Micheline.String (_loc, s) -> Contract_hash.of_b58check_opt s
| Bytes (_loc, b) ->
Data_encoding.Binary.of_bytes_opt Contract_hash.encoding b
| _ -> None
let parse_unit_test (parsed : string Michelson_v1_parser.parser_result) =
let open Result_syntax in
let open Micheline in
let rec parse ut = function
| [] -> return ut
| (Prim (_loc, prim, [arg], _annots) as e) :: l -> (
let check_duplicated = function
| None -> return_unit
| Some _ ->
tzfail (Duplicated_tzt_top_prim (prim, localize_node ~parsed e))
in
let invalid_format () =
Invalid_format_for_tzt_top_prim (prim, localize_node ~parsed e)
in
let trace_invalid_format res = record_trace_eval invalid_format res in
match prim with
| "input" ->
let* () = check_duplicated ut.temp_input in
let* items = trace_invalid_format @@ parse_stack ~node:arg parsed in
parse {ut with temp_input = Some items} l
| "output" ->
let* () = check_duplicated ut.temp_output in
parse {ut with temp_output = Some arg} l
| "code" ->
let* () = check_duplicated ut.temp_code in
let* c = trace_invalid_format @@ parse_expression arg in
parse {ut with temp_code = Some c} l
| "amount" ->
let* () = check_duplicated ut.temp_optional.amount in
let* t = parse_mutez arg ~error:invalid_format in
parse
{ut with temp_optional = {ut.temp_optional with amount = Some t}}
l
| "balance" ->
let* () = check_duplicated ut.temp_optional.balance in
let* t = parse_mutez arg ~error:invalid_format in
parse
{ut with temp_optional = {ut.temp_optional with balance = Some t}}
l
| "chain_id" ->
let* () = check_duplicated ut.temp_optional.chain_id in
let* chain_id = parse_chain_id arg ~error:invalid_format in
parse
{
ut with
temp_optional = {ut.temp_optional with chain_id = Some chain_id};
}
l
| "now" ->
let* () = check_duplicated ut.temp_optional.now in
let* time = parse_timestamp arg ~error:invalid_format in
parse
{ut with temp_optional = {ut.temp_optional with now = Some time}}
l
| "level" ->
let* () = check_duplicated ut.temp_optional.level in
let* level = parse_nat arg ~error:invalid_format in
parse
{
ut with
temp_optional = {ut.temp_optional with level = Some level};
}
l
| "sender" ->
let* () = check_duplicated ut.temp_optional.sender in
let* addr = parse_address arg ~error:invalid_format in
parse
{
ut with
temp_optional = {ut.temp_optional with sender = Some addr};
}
l
| "source" ->
let* () = check_duplicated ut.temp_optional.source in
let* addr = parse_key_hash arg ~error:invalid_format in
parse
{
ut with
temp_optional = {ut.temp_optional with source = Some addr};
}
l
| "self" ->
let* () = check_duplicated ut.temp_optional.self in
let* addr = parse_contract_hash arg ~error:invalid_format in
parse
{ut with temp_optional = {ut.temp_optional with self = Some addr}}
l
| "parameter" ->
let* () = check_duplicated ut.temp_optional.parameter in
let* ty = parse_expression arg in
parse
{
ut with
temp_optional = {ut.temp_optional with parameter = Some ty};
}
l
| "other_contracts" ->
let* () = check_duplicated ut.temp_optional.other_contracts in
let* items = parse_other_contracts ~node:arg parsed in
parse
{
ut with
temp_optional =
{ut.temp_optional with other_contracts = Some items};
}
l
| "big_maps" ->
let* () = check_duplicated ut.temp_optional.extra_big_maps in
let* items = parse_extra_big_maps ~node:arg parsed in
parse
{
ut with
temp_optional =
{ut.temp_optional with extra_big_maps = Some items};
}
l
| _ -> tzfail @@ Unknown_tzt_top_prim (prim, localize_node ~parsed e))
| (Prim (_loc, prim, ([] | _ :: _ :: _), _annots) as e) :: _ ->
tzfail @@ Wrong_tzt_top_prim_arity (prim, localize_node ~parsed e, 1)
| ((Seq _ | Int _ | String _ | Bytes _) as e) :: _ ->
tzfail @@ Invalid_tzt_toplevel (localize_node ~parsed e)
in
let nodes =
match Micheline.root parsed.expanded with
| Seq (_, nodes) -> nodes
| node -> [node]
in
let* ut =
parse
{
temp_input = None;
temp_code = None;
temp_output = None;
temp_optional =
{
now = None;
level = None;
sender = None;
source = None;
chain_id = None;
self = None;
parameter = None;
amount = None;
balance = None;
other_contracts = None;
extra_big_maps = None;
};
}
nodes
in
let check_mandatory opt prim =
Option.value_e
opt
~error:(TzTrace.make @@ Missing_mandatory_tzt_top_prim prim)
in
let* input = check_mandatory ut.temp_input "input" in
let* code = check_mandatory ut.temp_code "code" in
let* output = check_mandatory ut.temp_output "output" in
return {input; code; output; optional = ut.temp_optional}