package tezos-protocol-010-PtGRANAD
Tezos protocol 010-PtGRANAD package
Install
Dune Dependency
Authors
Maintainers
Sources
tezos-octez-v20.1.tag.bz2
sha256=ddfb5076eeb0b32ac21c1eed44e8fc86a6743ef18ab23fff02d36e365bb73d61
sha512=d22a827df5146e0aa274df48bc2150b098177ff7e5eab52c6109e867eb0a1f0ec63e6bfbb0e3645a6c2112de3877c91a17df32ccbff301891ce4ba630c997a65
doc/src/tezos_raw_protocol_010_PtGRANAD/script_interpreter.ml.html
Source file script_interpreter.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 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685
(*****************************************************************************) (* *) (* Open Source License *) (* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.com> *) (* Copyright (c) 2020 Metastate AG <hello@metastate.dev> *) (* Copyright (c) 2021 Nomadic Labs, <contact@nomadic-labs.com> *) (* *) (* Permission is hereby granted, free of charge, to any person obtaining a *) (* copy of this software and associated documentation files (the "Software"),*) (* to deal in the Software without restriction, including without limitation *) (* the rights to use, copy, modify, merge, publish, distribute, sublicense, *) (* and/or sell copies of the Software, and to permit persons to whom the *) (* Software is furnished to do so, subject to the following conditions: *) (* *) (* The above copyright notice and this permission notice shall be included *) (* in all copies or substantial portions of the Software. *) (* *) (* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*) (* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *) (* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *) (* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*) (* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *) (* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *) (* DEALINGS IN THE SOFTWARE. *) (* *) (*****************************************************************************) (* This module implements an interpreter for Michelson. It takes the form of a [step] function that interprets script instructions in a dedicated abstract machine. The interpreter is written in a small-step style: an execution [step] only interprets a single instruction by updating the configuration of a dedicated abstract machine. This abstract machine has two components: - a stack to control which instructions must be executed ; and - a stack of values where instructions get their inputs and put their outputs. In addition, the machine has access to effectful primitives to interact with the execution environment (e.g. the Tezos node). These primitives live in the [Lwt+State+Error] monad. Hence, this interpreter produces a computation in the [Lwt+State+Error] monad. This interpreter enjoys the following properties: - The interpreter is tail-recursive, hence it is robust to stack overflow. This property is checked by the compiler thanks to the [@ocaml.tailcall] annotation of each recursive call. - The interpreter is type-preserving. Thanks to GADTs, the typing rules of Michelson are statically checked by the OCaml typechecker: a Michelson program cannot go wrong. - The interpreter is tagless. Thanks to GADTs, the exact shape of the stack is known statically so the interpreter does not have to check that the input stack has the shape expected by the instruction to be executed. Outline ======= This file is organized as follows: 1. Definition of runtime errors. 2. Interpretation loop: This is the main functionality of this module, aka the [step] function. 3. Interface functions: This part of the module builds high-level functions on top of the more basic [step] function. Auxiliary definitions can be found in {!Script_interpreter_defs}. Implementation details are explained along the file. *) open Alpha_context open Script open Script_typed_ir open Script_ir_translator open Script_interpreter_defs module S = Saturation_repr type step_constants = Script_interpreter_defs.step_constants = { source : Contract.t; payer : Contract.t; self : Contract.t; amount : Tez.t; chain_id : Chain_id.t; } (* ---- Run-time errors -----------------------------------------------------*) type error += | Reject of Script.location * Script.expr * execution_trace option type error += Overflow of Script.location * execution_trace option type error += Runtime_contract_error : Contract.t * Script.expr -> error type error += Bad_contract_parameter of Contract.t (* `Permanent *) type error += Cannot_serialize_failure type error += Cannot_serialize_storage type error += Michelson_too_many_recursive_calls let () = let open Data_encoding in let trace_encoding = list @@ obj3 (req "location" Script.location_encoding) (req "gas" Gas.encoding) (req "stack" (list (obj2 (req "item" Script.expr_encoding) (opt "annot" string)))) in (* Reject *) register_error_kind `Temporary ~id:"michelson_v1.script_rejected" ~title:"Script failed" ~description:"A FAILWITH instruction was reached" (obj3 (req "location" Script.location_encoding) (req "with" Script.expr_encoding) (opt "trace" trace_encoding)) (function Reject (loc, v, trace) -> Some (loc, v, trace) | _ -> None) (fun (loc, v, trace) -> Reject (loc, v, trace)) ; (* Overflow *) register_error_kind `Temporary ~id:"michelson_v1.script_overflow" ~title:"Script failed (overflow error)" ~description: "A FAIL instruction was reached due to the detection of an overflow" (obj2 (req "location" Script.location_encoding) (opt "trace" trace_encoding)) (function Overflow (loc, trace) -> Some (loc, trace) | _ -> None) (fun (loc, trace) -> Overflow (loc, trace)) ; (* Runtime contract error *) register_error_kind `Temporary ~id:"michelson_v1.runtime_error" ~title:"Script runtime error" ~description:"Toplevel error for all runtime script errors" (obj2 (req "contract_handle" Contract.encoding) (req "contract_code" Script.expr_encoding)) (function | Runtime_contract_error (contract, expr) -> Some (contract, expr) | _ -> None) (fun (contract, expr) -> Runtime_contract_error (contract, expr)) ; (* Bad contract parameter *) register_error_kind `Permanent ~id:"michelson_v1.bad_contract_parameter" ~title:"Contract supplied an invalid parameter" ~description: "Either no parameter was supplied to a contract with a non-unit \ parameter type, a non-unit parameter was passed to an account, or a \ parameter was supplied of the wrong type" Data_encoding.(obj1 (req "contract" Contract.encoding)) (function Bad_contract_parameter c -> Some c | _ -> None) (fun c -> Bad_contract_parameter c) ; (* Cannot serialize failure *) register_error_kind `Temporary ~id:"michelson_v1.cannot_serialize_failure" ~title:"Not enough gas to serialize argument of FAILWITH" ~description: "Argument of FAILWITH was too big to be serialized with the provided gas" Data_encoding.empty (function Cannot_serialize_failure -> Some () | _ -> None) (fun () -> Cannot_serialize_failure) ; (* Cannot serialize storage *) register_error_kind `Temporary ~id:"michelson_v1.cannot_serialize_storage" ~title:"Not enough gas to serialize execution storage" ~description: "The returned storage was too big to be serialized with the provided gas" Data_encoding.empty (function Cannot_serialize_storage -> Some () | _ -> None) (fun () -> Cannot_serialize_storage) (* Interpretation loop =================== *) (* As announced earlier, the [step] function produces a computation in the [Lwt+State+Error] monad. The [State] monad is implemented by having the [context] passed as input and returned updated as output. The [Error] monad is represented by the [tzresult] type constructor. The [step] function is actually defined as an internal tail-recursive routine of the toplevel [step]. It monitors the gas level before executing the instruction under focus, once this is done, it recursively calls itself on the continuation held by the current instruction. For each pure instruction (i.e. that is not monadic), the interpretation simply updates the input arguments of the [step] function. Since these arguments are (most likely) stored in hardware registers and since the tail-recursive calls are compiled into direct jumps, this interpretation technique offers good performances while saving safety thanks to a rich typing. For each impure instruction, the interpreter makes use of monadic bindings to compose monadic primitives with the [step] function. Again, we make sure that the recursive calls to [step] are tail calls by annotating them with [@ocaml.tailcall]. The [step] function is actually based on several mutually recursive functions that can be separated in two groups: the first group focuses on the evaluation of continuations while the second group is about evaluating the instructions. *) (* Evaluation of continuations =========================== As explained in [Script_typed_ir], there are several kinds of continuations, each having a specific evaluation rules. The following group of functions starts with a list of evaluation rules for continuations that generate fresh continuations. This group ends with the definition of [next], which dispatches evaluation rules depending on the continuation at stake. *) let rec kmap_exit : type a b c d e f g h m n o. (a, b, c, d, e, f, g, h, m, n, o) kmap_exit_type = fun mk g gas (body, xs, ys, yk) ks accu stack -> let ys = map_update yk (Some accu) ys in let ks = mk (KMap_enter_body (body, xs, ys, ks)) in let (accu, stack) = stack in (next [@ocaml.tailcall]) g gas ks accu stack [@@inline] and kmap_enter : type a b c d i j k. (a, b, c, d, i, j, k) kmap_enter_type = fun mk g gas (body, xs, ys) ks accu stack -> match xs with | [] -> (next [@ocaml.tailcall]) g gas ks ys (accu, stack) | (xk, xv) :: xs -> let ks = mk (KMap_exit_body (body, xs, ys, xk, ks)) in let res = (xk, xv) in let stack = (accu, stack) in (step [@ocaml.tailcall]) g gas body ks res stack [@@inline] and klist_exit : type a b c d i j. (a, b, c, d, i, j) klist_exit_type = fun mk g gas (body, xs, ys, len) ks accu stack -> let ks = mk (KList_enter_body (body, xs, accu :: ys, len, ks)) in let (accu, stack) = stack in (next [@ocaml.tailcall]) g gas ks accu stack [@@inline] and klist_enter : type a b c d e j. (a, b, c, d, e, j) klist_enter_type = fun mk g gas (body, xs, ys, len) ks' accu stack -> match xs with | [] -> let ys = {elements = List.rev ys; length = len} in (next [@ocaml.tailcall]) g gas ks' ys (accu, stack) | x :: xs -> let ks = mk (KList_exit_body (body, xs, ys, len, ks')) in (step [@ocaml.tailcall]) g gas body ks x (accu, stack) [@@inline] and kloop_in_left : type a b c d e f g. (a, b, c, d, e, f, g) kloop_in_left_type = fun g gas ks0 ki ks' accu stack -> match accu with | L v -> (step [@ocaml.tailcall]) g gas ki ks0 v stack | R v -> (next [@ocaml.tailcall]) g gas ks' v stack [@@inline] and kloop_in : type a b c r f s. (a, b, c, r, f, s) kloop_in_type = fun g gas ks0 ki ks' accu stack -> let (accu', stack') = stack in if accu then (step [@ocaml.tailcall]) g gas ki ks0 accu' stack' else (next [@ocaml.tailcall]) g gas ks' accu' stack' [@@inline] and kiter : type a b s r f. (a, b, s, r, f) kiter_type = fun mk g gas (body, xs) ks accu stack -> match xs with | [] -> (next [@ocaml.tailcall]) g gas ks accu stack | x :: xs -> let ks = mk (KIter (body, xs, ks)) in (step [@ocaml.tailcall]) g gas body ks x (accu, stack) [@@inline] and next : type a s r f. outdated_context * step_constants -> local_gas_counter -> (a, s, r, f) continuation -> a -> s -> (r * f * outdated_context * local_gas_counter) tzresult Lwt.t = fun ((ctxt, _) as g) gas ks0 accu stack -> match consume_control gas ks0 with | None -> Lwt.return (Gas.gas_exhausted_error (update_context gas ctxt)) | Some gas -> ( match ks0 with | KLog (ks, logger) -> (klog [@ocaml.tailcall]) logger g gas ks0 ks accu stack | KNil -> Lwt.return (Ok (accu, stack, ctxt, gas)) | KCons (k, ks) -> (step [@ocaml.tailcall]) g gas k ks accu stack | KLoop_in (ki, ks') -> (kloop_in [@ocaml.tailcall]) g gas ks0 ki ks' accu stack | KReturn (stack', ks) -> (next [@ocaml.tailcall]) g gas ks accu stack' | KLoop_in_left (ki, ks') -> (kloop_in_left [@ocaml.tailcall]) g gas ks0 ki ks' accu stack | KUndip (x, ks) -> (next [@ocaml.tailcall]) g gas ks x (accu, stack) | KIter (body, xs, ks) -> let extra = (body, xs) in (kiter [@ocaml.tailcall]) id g gas extra ks accu stack | KList_enter_body (body, xs, ys, len, ks) -> let extra = (body, xs, ys, len) in (klist_enter [@ocaml.tailcall]) id g gas extra ks accu stack | KList_exit_body (body, xs, ys, len, ks) -> let extra = (body, xs, ys, len) in (klist_exit [@ocaml.tailcall]) id g gas extra ks accu stack | KMap_enter_body (body, xs, ys, ks) -> let extra = (body, xs, ys) in (kmap_enter [@ocaml.tailcall]) id g gas extra ks accu stack | KMap_exit_body (body, xs, ys, yk, ks) -> let extra = (body, xs, ys, yk) in (kmap_exit [@ocaml.tailcall]) id g gas extra ks accu stack ) (* Evaluation of instructions ========================== The following functions define evaluation rules for instructions that generate fresh continuations. As such, they expect a constructor [log_if_needed] which inserts a [KLog] if the evaluation is logged. The [step] function is taking care of the evaluation of the other instructions. *) and ilist_map : type a b c d e f g h. (a, b, c, d, e, f, g, h) ilist_map_type = fun log_if_needed g gas (body, k) ks accu stack -> let xs = accu.elements in let ys = [] in let len = accu.length in let ks = log_if_needed (KList_enter_body (body, xs, ys, len, KCons (k, ks))) in let (accu, stack) = stack in (next [@ocaml.tailcall]) g gas ks accu stack [@@inline] and ilist_iter : type a b c d e f g. (a, b, c, d, e, f, g) ilist_iter_type = fun log_if_needed g gas (body, k) ks accu stack -> let xs = accu.elements in let ks = log_if_needed (KIter (body, xs, KCons (k, ks))) in let (accu, stack) = stack in (next [@ocaml.tailcall]) g gas ks accu stack [@@inline] and iset_iter : type a b c d e f g. (a, b, c, d, e, f, g) iset_iter_type = fun log_if_needed g gas (body, k) ks accu stack -> let set = accu in let l = List.rev (set_fold (fun e acc -> e :: acc) set []) in let ks = log_if_needed (KIter (body, l, KCons (k, ks))) in let (accu, stack) = stack in (next [@ocaml.tailcall]) g gas ks accu stack [@@inline] and imap_map : type a b c d e f g h i. (a, b, c, d, e, f, g, h, i) imap_map_type = fun log_if_needed g gas (body, k) ks accu stack -> let map = accu in let xs = List.rev (map_fold (fun k v a -> (k, v) :: a) map []) in let ys = empty_map (map_key_ty map) in let ks = log_if_needed (KMap_enter_body (body, xs, ys, KCons (k, ks))) in let (accu, stack) = stack in (next [@ocaml.tailcall]) g gas ks accu stack [@@inline] and imap_iter : type a b c d e f g h. (a, b, c, d, e, f, g, h) imap_iter_type = fun log_if_needed g gas (body, k) ks accu stack -> let map = accu in let l = List.rev (map_fold (fun k v a -> (k, v) :: a) map []) in let ks = log_if_needed (KIter (body, l, KCons (k, ks))) in let (accu, stack) = stack in (next [@ocaml.tailcall]) g gas ks accu stack [@@inline] and imul_teznat : type a b c d e f. (a, b, c, d, e, f) imul_teznat_type = fun logger g gas (kinfo, k) ks accu stack -> let x = accu in let (y, stack) = stack in match Script_int.to_int64 y with | None -> get_log logger >>=? fun log -> fail (Overflow (kinfo.iloc, log)) | Some y -> Tez.(x *? y) >>?= fun res -> (step [@ocaml.tailcall]) g gas k ks res stack and imul_nattez : type a b c d e f. (a, b, c, d, e, f) imul_nattez_type = fun logger g gas (kinfo, k) ks accu stack -> let y = accu in let (x, stack) = stack in match Script_int.to_int64 y with | None -> get_log logger >>=? fun log -> fail (Overflow (kinfo.iloc, log)) | Some y -> Tez.(x *? y) >>?= fun res -> (step [@ocaml.tailcall]) g gas k ks res stack and ilsl_nat : type a b c d e f. (a, b, c, d, e, f) ilsl_nat_type = fun logger g gas (kinfo, k) ks accu stack -> let x = accu and (y, stack) = stack in match Script_int.shift_left_n x y with | None -> get_log logger >>=? fun log -> fail (Overflow (kinfo.iloc, log)) | Some x -> (step [@ocaml.tailcall]) g gas k ks x stack and ilsr_nat : type a b c d e f. (a, b, c, d, e, f) ilsr_nat_type = fun logger g gas (kinfo, k) ks accu stack -> let x = accu and (y, stack) = stack in match Script_int.shift_right_n x y with | None -> get_log logger >>=? fun log -> fail (Overflow (kinfo.iloc, log)) | Some r -> (step [@ocaml.tailcall]) g gas k ks r stack and ifailwith : type a b. (a, b) ifailwith_type = fun logger (ctxt, _) gas kloc tv accu -> let v = accu in let ctxt = update_context gas ctxt in trace Cannot_serialize_failure (unparse_data ctxt Optimized tv v) >>=? fun (v, _ctxt) -> let v = Micheline.strip_locations v in get_log logger >>=? fun log -> fail (Reject (kloc, v, log)) and iexec : type a b c d e f g. (a, b, c, d, e, f, g) iexec_type = fun logger g gas k ks accu stack -> let arg = accu and (code, stack) = stack in let (Lam (code, _)) = code in let code = match logger with | None -> code.kinstr | Some logger -> log_kinstr logger code.kinstr in let ks = KReturn (stack, KCons (k, ks)) in (step [@ocaml.tailcall]) g gas code ks arg (EmptyCell, EmptyCell) and step : type a s b t r f. (a, s, b, t, r, f) step_type = fun ((ctxt, sc) as g) gas i ks accu stack -> match consume gas i accu stack with | None -> Lwt.return (Gas.gas_exhausted_error (update_context gas ctxt)) | Some gas -> ( match i with | ILog (_, event, logger, k) -> (log [@ocaml.tailcall]) (logger, event) g gas k ks accu stack | IHalt _ -> (next [@ocaml.tailcall]) g gas ks accu stack (* stack ops *) | IDrop (_, k) -> let (accu, stack) = stack in (step [@ocaml.tailcall]) g gas k ks accu stack | IDup (_, k) -> (step [@ocaml.tailcall]) g gas k ks accu (accu, stack) | ISwap (_, k) -> let (top, stack) = stack in (step [@ocaml.tailcall]) g gas k ks top (accu, stack) | IConst (_, v, k) -> (step [@ocaml.tailcall]) g gas k ks v (accu, stack) (* options *) | ICons_some (_, k) -> (step [@ocaml.tailcall]) g gas k ks (Some accu) stack | ICons_none (_, _, k) -> (step [@ocaml.tailcall]) g gas k ks None (accu, stack) | IIf_none {branch_if_none; branch_if_some} -> ( match accu with | None -> let (accu, stack) = stack in (step [@ocaml.tailcall]) g gas branch_if_none ks accu stack | Some v -> (step [@ocaml.tailcall]) g gas branch_if_some ks v stack ) (* pairs *) | ICons_pair (_, k) -> let (b, stack) = stack in (step [@ocaml.tailcall]) g gas k ks (accu, b) stack | IUnpair (_, k) -> let (a, b) = accu in (step [@ocaml.tailcall]) g gas k ks a (b, stack) | ICar (_, k) -> let (a, _) = accu in (step [@ocaml.tailcall]) g gas k ks a stack | ICdr (_, k) -> let (_, b) = accu in (step [@ocaml.tailcall]) g gas k ks b stack (* unions *) | ICons_left (_, k) -> (step [@ocaml.tailcall]) g gas k ks (L accu) stack | ICons_right (_, k) -> (step [@ocaml.tailcall]) g gas k ks (R accu) stack | IIf_left {branch_if_left; branch_if_right} -> ( match accu with | L v -> (step [@ocaml.tailcall]) g gas branch_if_left ks v stack | R v -> (step [@ocaml.tailcall]) g gas branch_if_right ks v stack ) (* lists *) | ICons_list (_, k) -> let (tl, stack) = stack in let accu = list_cons accu tl in (step [@ocaml.tailcall]) g gas k ks accu stack | INil (_, k) -> let stack = (accu, stack) in let accu = list_empty in (step [@ocaml.tailcall]) g gas k ks accu stack | IIf_cons {branch_if_cons; branch_if_nil} -> ( match accu.elements with | [] -> let (accu, stack) = stack in (step [@ocaml.tailcall]) g gas branch_if_nil ks accu stack | hd :: tl -> let tl = {elements = tl; length = accu.length - 1} in (step [@ocaml.tailcall]) g gas branch_if_cons ks hd (tl, stack) ) | IList_map (_, body, k) -> (ilist_map [@ocaml.tailcall]) id g gas (body, k) ks accu stack | IList_size (_, k) -> let list = accu in let len = Script_int.(abs (of_int list.length)) in (step [@ocaml.tailcall]) g gas k ks len stack | IList_iter (_, body, k) -> (ilist_iter [@ocaml.tailcall]) id g gas (body, k) ks accu stack (* sets *) | IEmpty_set (_, ty, k) -> let res = empty_set ty in let stack = (accu, stack) in (step [@ocaml.tailcall]) g gas k ks res stack | ISet_iter (_, body, k) -> (iset_iter [@ocaml.tailcall]) id g gas (body, k) ks accu stack | ISet_mem (_, k) -> let (set, stack) = stack in let res = set_mem accu set in (step [@ocaml.tailcall]) g gas k ks res stack | ISet_update (_, k) -> let (presence, (set, stack)) = stack in let res = set_update accu presence set in (step [@ocaml.tailcall]) g gas k ks res stack | ISet_size (_, k) -> let res = set_size accu in (step [@ocaml.tailcall]) g gas k ks res stack (* maps *) | IEmpty_map (_, ty, _, k) -> let res = empty_map ty and stack = (accu, stack) in (step [@ocaml.tailcall]) g gas k ks res stack | IMap_map (_, body, k) -> (imap_map [@ocaml.tailcall]) id g gas (body, k) ks accu stack | IMap_iter (_, body, k) -> (imap_iter [@ocaml.tailcall]) id g gas (body, k) ks accu stack | IMap_mem (_, k) -> let (map, stack) = stack in let res = map_mem accu map in (step [@ocaml.tailcall]) g gas k ks res stack | IMap_get (_, k) -> let (map, stack) = stack in let res = map_get accu map in (step [@ocaml.tailcall]) g gas k ks res stack | IMap_update (_, k) -> let (v, (map, stack)) = stack in let key = accu in let res = map_update key v map in (step [@ocaml.tailcall]) g gas k ks res stack | IMap_get_and_update (_, k) -> let key = accu in let (v, (map, rest)) = stack in let map' = map_update key v map in let v' = map_get key map in (step [@ocaml.tailcall]) g gas k ks v' (map', rest) | IMap_size (_, k) -> let res = map_size accu in (step [@ocaml.tailcall]) g gas k ks res stack (* Big map operations *) | IEmpty_big_map (_, tk, tv, k) -> let ebm = Script_ir_translator.empty_big_map tk tv in (step [@ocaml.tailcall]) g gas k ks ebm (accu, stack) | IBig_map_mem (_, k) -> let (map, stack) = stack in let key = accu in ( use_gas_counter_in_ctxt ctxt gas @@ fun ctxt -> Script_ir_translator.big_map_mem ctxt key map ) >>=? fun (res, ctxt, gas) -> (step [@ocaml.tailcall]) (ctxt, sc) gas k ks res stack | IBig_map_get (_, k) -> let (map, stack) = stack in let key = accu in ( use_gas_counter_in_ctxt ctxt gas @@ fun ctxt -> Script_ir_translator.big_map_get ctxt key map ) >>=? fun (res, ctxt, gas) -> (step [@ocaml.tailcall]) (ctxt, sc) gas k ks res stack | IBig_map_update (_, k) -> let key = accu in let (maybe_value, (map, stack)) = stack in ( use_gas_counter_in_ctxt ctxt gas @@ fun ctxt -> Script_ir_translator.big_map_update ctxt key maybe_value map ) >>=? fun (big_map, ctxt, gas) -> (step [@ocaml.tailcall]) (ctxt, sc) gas k ks big_map stack | IBig_map_get_and_update (_, k) -> let key = accu in let (v, (map, stack)) = stack in ( use_gas_counter_in_ctxt ctxt gas @@ fun ctxt -> Script_ir_translator.big_map_get_and_update ctxt key v map ) >>=? fun ((v', map'), ctxt, gas) -> (step [@ocaml.tailcall]) (ctxt, sc) gas k ks v' (map', stack) (* timestamp operations *) | IAdd_seconds_to_timestamp (_, k) -> let n = accu in let (t, stack) = stack in let result = Script_timestamp.add_delta t n in (step [@ocaml.tailcall]) g gas k ks result stack | IAdd_timestamp_to_seconds (_, k) -> let t = accu in let (n, stack) = stack in let result = Script_timestamp.add_delta t n in (step [@ocaml.tailcall]) g gas k ks result stack | ISub_timestamp_seconds (_, k) -> let t = accu in let (s, stack) = stack in let result = Script_timestamp.sub_delta t s in (step [@ocaml.tailcall]) g gas k ks result stack | IDiff_timestamps (_, k) -> let t1 = accu in let (t2, stack) = stack in let result = Script_timestamp.diff t1 t2 in (step [@ocaml.tailcall]) g gas k ks result stack (* string operations *) | IConcat_string_pair (_, k) -> let x = accu in let (y, stack) = stack in let s = String.concat "" [x; y] in (step [@ocaml.tailcall]) g gas k ks s stack | IConcat_string (_, k) -> let ss = accu in (* The cost for this fold_left has been paid upfront *) let total_length = List.fold_left (fun acc s -> S.add acc (S.safe_int (String.length s))) S.zero ss.elements in consume' ctxt gas (Interp_costs.concat_string total_length) >>?= fun gas -> let s = String.concat "" ss.elements in (step [@ocaml.tailcall]) g gas k ks s stack | ISlice_string (_, k) -> let offset = accu and (length, (s, stack)) = stack in let s_length = Z.of_int (String.length s) in let offset = Script_int.to_zint offset in let length = Script_int.to_zint length in if Compare.Z.(offset < s_length && Z.add offset length <= s_length) then let s = String.sub s (Z.to_int offset) (Z.to_int length) in (step [@ocaml.tailcall]) g gas k ks (Some s) stack else (step [@ocaml.tailcall]) g gas k ks None stack | IString_size (_, k) -> let s = accu in let result = Script_int.(abs (of_int (String.length s))) in (step [@ocaml.tailcall]) g gas k ks result stack (* bytes operations *) | IConcat_bytes_pair (_, k) -> let x = accu in let (y, stack) = stack in let s = Bytes.cat x y in (step [@ocaml.tailcall]) g gas k ks s stack | IConcat_bytes (_, k) -> let ss = accu in (* The cost for this fold_left has been paid upfront *) let total_length = List.fold_left (fun acc s -> S.add acc (S.safe_int (Bytes.length s))) S.zero ss.elements in consume' ctxt gas (Interp_costs.concat_string total_length) >>?= fun gas -> let s = Bytes.concat Bytes.empty ss.elements in (step [@ocaml.tailcall]) g gas k ks s stack | ISlice_bytes (_, k) -> let offset = accu and (length, (s, stack)) = stack in let s_length = Z.of_int (Bytes.length s) in let offset = Script_int.to_zint offset in let length = Script_int.to_zint length in if Compare.Z.(offset < s_length && Z.add offset length <= s_length) then let s = Bytes.sub s (Z.to_int offset) (Z.to_int length) in (step [@ocaml.tailcall]) g gas k ks (Some s) stack else (step [@ocaml.tailcall]) g gas k ks None stack | IBytes_size (_, k) -> let s = accu in let result = Script_int.(abs (of_int (Bytes.length s))) in (step [@ocaml.tailcall]) g gas k ks result stack (* currency operations *) | IAdd_tez (_, k) -> let x = accu in let (y, stack) = stack in Tez.(x +? y) >>?= fun res -> (step [@ocaml.tailcall]) g gas k ks res stack | ISub_tez (_, k) -> let x = accu in let (y, stack) = stack in Tez.(x -? y) >>?= fun res -> (step [@ocaml.tailcall]) g gas k ks res stack | IMul_teznat (kinfo, k) -> imul_teznat None g gas (kinfo, k) ks accu stack | IMul_nattez (kinfo, k) -> imul_nattez None g gas (kinfo, k) ks accu stack (* boolean operations *) | IOr (_, k) -> let x = accu in let (y, stack) = stack in (step [@ocaml.tailcall]) g gas k ks (x || y) stack | IAnd (_, k) -> let x = accu in let (y, stack) = stack in (step [@ocaml.tailcall]) g gas k ks (x && y) stack | IXor (_, k) -> let x = accu in let (y, stack) = stack in let res = Compare.Bool.(x <> y) in (step [@ocaml.tailcall]) g gas k ks res stack | INot (_, k) -> let x = accu in (step [@ocaml.tailcall]) g gas k ks (not x) stack (* integer operations *) | IIs_nat (_, k) -> let x = accu in let res = Script_int.is_nat x in (step [@ocaml.tailcall]) g gas k ks res stack | IAbs_int (_, k) -> let x = accu in let res = Script_int.abs x in (step [@ocaml.tailcall]) g gas k ks res stack | IInt_nat (_, k) -> let x = accu in let res = Script_int.int x in (step [@ocaml.tailcall]) g gas k ks res stack | INeg_int (_, k) -> let x = accu in let res = Script_int.neg x in (step [@ocaml.tailcall]) g gas k ks res stack | INeg_nat (_, k) -> let x = accu in let res = Script_int.neg x in (step [@ocaml.tailcall]) g gas k ks res stack | IAdd_intint (_, k) -> let x = accu and (y, stack) = stack in let res = Script_int.add x y in (step [@ocaml.tailcall]) g gas k ks res stack | IAdd_intnat (_, k) -> let x = accu and (y, stack) = stack in let res = Script_int.add x y in (step [@ocaml.tailcall]) g gas k ks res stack | IAdd_natint (_, k) -> let x = accu and (y, stack) = stack in let res = Script_int.add x y in (step [@ocaml.tailcall]) g gas k ks res stack | IAdd_natnat (_, k) -> let x = accu and (y, stack) = stack in let res = Script_int.add_n x y in (step [@ocaml.tailcall]) g gas k ks res stack | ISub_int (_, k) -> let x = accu and (y, stack) = stack in let res = Script_int.sub x y in (step [@ocaml.tailcall]) g gas k ks res stack | IMul_intint (_, k) -> let x = accu and (y, stack) = stack in let res = Script_int.mul x y in (step [@ocaml.tailcall]) g gas k ks res stack | IMul_intnat (_, k) -> let x = accu and (y, stack) = stack in let res = Script_int.mul x y in (step [@ocaml.tailcall]) g gas k ks res stack | IMul_natint (_, k) -> let x = accu and (y, stack) = stack in let res = Script_int.mul x y in (step [@ocaml.tailcall]) g gas k ks res stack | IMul_natnat (_, k) -> let x = accu and (y, stack) = stack in let res = Script_int.mul_n x y in (step [@ocaml.tailcall]) g gas k ks res stack | IEdiv_teznat (_, k) -> let x = accu and (y, stack) = stack in let x = Script_int.of_int64 (Tez.to_mutez x) in let result = match Script_int.ediv x y with | None -> None | Some (q, r) -> ( match (Script_int.to_int64 q, Script_int.to_int64 r) with | (Some q, Some r) -> ( match (Tez.of_mutez q, Tez.of_mutez r) with | (Some q, Some r) -> Some (q, r) (* Cannot overflow *) | _ -> assert false ) (* Cannot overflow *) | _ -> assert false ) in (step [@ocaml.tailcall]) g gas k ks result stack | IEdiv_tez (_, k) -> let x = accu and (y, stack) = stack in let x = Script_int.abs (Script_int.of_int64 (Tez.to_mutez x)) in let y = Script_int.abs (Script_int.of_int64 (Tez.to_mutez y)) in let result = match Script_int.ediv_n x y with | None -> None | Some (q, r) -> ( match Script_int.to_int64 r with | None -> assert false (* Cannot overflow *) | Some r -> ( match Tez.of_mutez r with | None -> assert false (* Cannot overflow *) | Some r -> Some (q, r) ) ) in (step [@ocaml.tailcall]) g gas k ks result stack | IEdiv_intint (_, k) -> let x = accu and (y, stack) = stack in let res = Script_int.ediv x y in (step [@ocaml.tailcall]) g gas k ks res stack | IEdiv_intnat (_, k) -> let x = accu and (y, stack) = stack in let res = Script_int.ediv x y in (step [@ocaml.tailcall]) g gas k ks res stack | IEdiv_natint (_, k) -> let x = accu and (y, stack) = stack in let res = Script_int.ediv x y in (step [@ocaml.tailcall]) g gas k ks res stack | IEdiv_natnat (_, k) -> let x = accu and (y, stack) = stack in let res = Script_int.ediv_n x y in (step [@ocaml.tailcall]) g gas k ks res stack | ILsl_nat (kinfo, k) -> ilsl_nat None g gas (kinfo, k) ks accu stack | ILsr_nat (kinfo, k) -> ilsr_nat None g gas (kinfo, k) ks accu stack | IOr_nat (_, k) -> let x = accu and (y, stack) = stack in let res = Script_int.logor x y in (step [@ocaml.tailcall]) g gas k ks res stack | IAnd_nat (_, k) -> let x = accu and (y, stack) = stack in let res = Script_int.logand x y in (step [@ocaml.tailcall]) g gas k ks res stack | IAnd_int_nat (_, k) -> let x = accu and (y, stack) = stack in let res = Script_int.logand x y in (step [@ocaml.tailcall]) g gas k ks res stack | IXor_nat (_, k) -> let x = accu and (y, stack) = stack in let res = Script_int.logxor x y in (step [@ocaml.tailcall]) g gas k ks res stack | INot_int (_, k) -> let x = accu in let res = Script_int.lognot x in (step [@ocaml.tailcall]) g gas k ks res stack | INot_nat (_, k) -> let x = accu in let res = Script_int.lognot x in (step [@ocaml.tailcall]) g gas k ks res stack (* control *) | IIf {branch_if_true; branch_if_false} -> let (res, stack) = stack in if accu then (step [@ocaml.tailcall]) g gas branch_if_true ks res stack else (step [@ocaml.tailcall]) g gas branch_if_false ks res stack | ILoop (_, body, k) -> let ks = KLoop_in (body, KCons (k, ks)) in (next [@ocaml.tailcall]) g gas ks accu stack | ILoop_left (_, bl, br) -> let ks = KLoop_in_left (bl, KCons (br, ks)) in (next [@ocaml.tailcall]) g gas ks accu stack | IDip (_, b, k) -> let ign = accu in let ks = KUndip (ign, KCons (k, ks)) in let (accu, stack) = stack in (step [@ocaml.tailcall]) g gas b ks accu stack | IExec (_, k) -> iexec None g gas k ks accu stack | IApply (_, capture_ty, k) -> let capture = accu in let (lam, stack) = stack in apply ctxt gas capture_ty capture lam >>=? fun (lam', ctxt, gas) -> (step [@ocaml.tailcall]) (ctxt, sc) gas k ks lam' stack | ILambda (_, lam, k) -> (step [@ocaml.tailcall]) g gas k ks lam (accu, stack) | IFailwith (_, kloc, tv, _) -> ifailwith None g gas kloc tv accu (* comparison *) | ICompare (_, ty, k) -> let a = accu in let (b, stack) = stack in let r = Script_int.of_int @@ Script_ir_translator.compare_comparable ty a b in (step [@ocaml.tailcall]) g gas k ks r stack (* comparators *) | IEq (_, k) -> let a = accu in let a = Script_int.compare a Script_int.zero in let a = Compare.Int.(a = 0) in (step [@ocaml.tailcall]) g gas k ks a stack | INeq (_, k) -> let a = accu in let a = Script_int.compare a Script_int.zero in let a = Compare.Int.(a <> 0) in (step [@ocaml.tailcall]) g gas k ks a stack | ILt (_, k) -> let a = accu in let a = Script_int.compare a Script_int.zero in let a = Compare.Int.(a < 0) in (step [@ocaml.tailcall]) g gas k ks a stack | ILe (_, k) -> let a = accu in let a = Script_int.compare a Script_int.zero in let a = Compare.Int.(a <= 0) in (step [@ocaml.tailcall]) g gas k ks a stack | IGt (_, k) -> let a = accu in let a = Script_int.compare a Script_int.zero in let a = Compare.Int.(a > 0) in (step [@ocaml.tailcall]) g gas k ks a stack | IGe (_, k) -> let a = accu in let a = Script_int.compare a Script_int.zero in let a = Compare.Int.(a >= 0) in (step [@ocaml.tailcall]) g gas k ks a stack (* packing *) | IPack (_, ty, k) -> let value = accu in ( use_gas_counter_in_ctxt ctxt gas @@ fun ctxt -> Script_ir_translator.pack_data ctxt ty value ) >>=? fun (bytes, ctxt, gas) -> (step [@ocaml.tailcall]) (ctxt, sc) gas k ks bytes stack | IUnpack (_, ty, k) -> let bytes = accu in (use_gas_counter_in_ctxt ctxt gas @@ fun ctxt -> unpack ctxt ~ty ~bytes) >>=? fun (opt, ctxt, gas) -> (step [@ocaml.tailcall]) (ctxt, sc) gas k ks opt stack | IAddress (_, k) -> let (_, address) = accu in (step [@ocaml.tailcall]) g gas k ks address stack | IContract (kinfo, t, entrypoint, k) -> ( let contract = accu in match (contract, entrypoint) with | ((contract, "default"), entrypoint) | ((contract, entrypoint), "default") -> let ctxt = update_context gas ctxt in Script_ir_translator.parse_contract_for_script ctxt kinfo.iloc t contract ~entrypoint >>=? fun (ctxt, maybe_contract) -> let gas = update_local_gas_counter ctxt in let ctxt = outdated ctxt in let accu = maybe_contract in (step [@ocaml.tailcall]) (ctxt, sc) gas k ks accu stack | _ -> (step [@ocaml.tailcall]) (ctxt, sc) gas k ks None stack ) | ITransfer_tokens (_, k) -> let p = accu in let (amount, ((tp, (destination, entrypoint)), stack)) = stack in transfer (ctxt, sc) gas amount tp p destination entrypoint >>=? fun (accu, ctxt, gas) -> (step [@ocaml.tailcall]) (ctxt, sc) gas k ks accu stack | IImplicit_account (_, k) -> let key = accu in let contract = Contract.implicit_contract key in let res = (Unit_t None, (contract, "default")) in (step [@ocaml.tailcall]) g gas k ks res stack | ICreate_contract {storage_type; arg_type; lambda = Lam (_, code); root_name; k} -> (* Removed the instruction's arguments manager, spendable and delegatable *) let delegate = accu in let (credit, (init, stack)) = stack in create_contract g gas storage_type arg_type code root_name delegate credit init >>=? fun (res, contract, ctxt, gas) -> let stack = ((contract, "default"), stack) in (step [@ocaml.tailcall]) (ctxt, sc) gas k ks res stack | ISet_delegate (_, k) -> let delegate = accu in let operation = Delegation delegate in let ctxt = update_context gas ctxt in fresh_internal_nonce ctxt >>?= fun (ctxt, nonce) -> let res = (Internal_operation {source = sc.self; operation; nonce}, None) in let gas = update_local_gas_counter ctxt in let ctxt = outdated ctxt in (step [@ocaml.tailcall]) (ctxt, sc) gas k ks res stack | IBalance (_, k) -> let ctxt = update_context gas ctxt in Contract.get_balance_carbonated ctxt sc.self >>=? fun (ctxt, balance) -> let gas = update_local_gas_counter ctxt in let ctxt = outdated ctxt in let g = (ctxt, sc) in (step [@ocaml.tailcall]) g gas k ks balance (accu, stack) | ILevel (_, k) -> let level = (Level.current (context_from_outdated_context ctxt)).level |> Raw_level.to_int32 |> Script_int.of_int32 |> Script_int.abs in (step [@ocaml.tailcall]) g gas k ks level (accu, stack) | INow (_, k) -> let now = Script_timestamp.now (context_from_outdated_context ctxt) in (step [@ocaml.tailcall]) g gas k ks now (accu, stack) | ICheck_signature (_, k) -> let key = accu and (signature, (message, stack)) = stack in let res = Signature.check key signature message in (step [@ocaml.tailcall]) g gas k ks res stack | IHash_key (_, k) -> let key = accu in let res = Signature.Public_key.hash key in (step [@ocaml.tailcall]) g gas k ks res stack | IBlake2b (_, k) -> let bytes = accu in let hash = Raw_hashes.blake2b bytes in (step [@ocaml.tailcall]) g gas k ks hash stack | ISha256 (_, k) -> let bytes = accu in let hash = Raw_hashes.sha256 bytes in (step [@ocaml.tailcall]) g gas k ks hash stack | ISha512 (_, k) -> let bytes = accu in let hash = Raw_hashes.sha512 bytes in (step [@ocaml.tailcall]) g gas k ks hash stack | ISource (_, k) -> let res = (sc.payer, "default") in (step [@ocaml.tailcall]) g gas k ks res (accu, stack) | ISender (_, k) -> let res = (sc.source, "default") in (step [@ocaml.tailcall]) g gas k ks res (accu, stack) | ISelf (_, ty, entrypoint, k) -> let res = (ty, (sc.self, entrypoint)) in (step [@ocaml.tailcall]) g gas k ks res (accu, stack) | ISelf_address (_, k) -> let res = (sc.self, "default") in (step [@ocaml.tailcall]) g gas k ks res (accu, stack) | IAmount (_, k) -> let accu = sc.amount and stack = (accu, stack) in (step [@ocaml.tailcall]) g gas k ks accu stack | IDig (_, _n, n', k) -> let ((accu, stack), x) = interp_stack_prefix_preserving_operation (fun v stack -> (stack, v)) n' accu stack in let accu = x and stack = (accu, stack) in (step [@ocaml.tailcall]) g gas k ks accu stack | IDug (_, _n, n', k) -> let v = accu in let (accu, stack) = stack in let ((accu, stack), ()) = interp_stack_prefix_preserving_operation (fun accu stack -> ((v, (accu, stack)), ())) n' accu stack in (step [@ocaml.tailcall]) g gas k ks accu stack | IDipn (_, _n, n', b, k) -> let (accu, stack, restore_prefix) = kundip n' accu stack k in let ks = KCons (restore_prefix, ks) in (step [@ocaml.tailcall]) g gas b ks accu stack | IDropn (_, _n, n', k) -> let stack = let rec aux : type a s b t. (b, t, b, t, a, s, a, s) stack_prefix_preservation_witness -> a -> s -> b * t = fun w accu stack -> match w with | KRest -> (accu, stack) | KPrefix (_, w) -> let (accu, stack) = stack in aux w accu stack in aux n' accu stack in let (accu, stack) = stack in (step [@ocaml.tailcall]) g gas k ks accu stack | ISapling_empty_state (_, memo_size, k) -> let state = Sapling.empty_state ~memo_size () in (step [@ocaml.tailcall]) g gas k ks state (accu, stack) | ISapling_verify_update (_, k) -> ( let transaction = accu in let (state, stack) = stack in let address = Contract.to_b58check sc.self in let chain_id = Chain_id.to_b58check sc.chain_id in let anti_replay = address ^ chain_id in let ctxt = update_context gas ctxt in Sapling.verify_update ctxt state transaction anti_replay >>=? fun (ctxt, balance_state_opt) -> let gas = update_local_gas_counter ctxt in let ctxt = outdated ctxt in match balance_state_opt with | Some (balance, state) -> let state = Some (Script_int.of_int64 balance, state) in (step [@ocaml.tailcall]) (ctxt, sc) gas k ks state stack | None -> (step [@ocaml.tailcall]) (ctxt, sc) gas k ks None stack ) | IChainId (_, k) -> let accu = sc.chain_id and stack = (accu, stack) in (step [@ocaml.tailcall]) g gas k ks accu stack | INever _ -> ( match accu with _ -> . ) | IVoting_power (_, k) -> let key_hash = accu in let ctxt = update_context gas ctxt in Vote.get_voting_power ctxt key_hash >>=? fun (ctxt, rolls) -> let power = Script_int.(abs (of_int32 rolls)) in let gas = update_local_gas_counter ctxt in let ctxt = outdated ctxt in (step [@ocaml.tailcall]) (ctxt, sc) gas k ks power stack | ITotal_voting_power (_, k) -> let ctxt = update_context gas ctxt in Vote.get_total_voting_power ctxt >>=? fun (ctxt, rolls) -> let power = Script_int.(abs (of_int32 rolls)) in let gas = update_local_gas_counter ctxt in let ctxt = outdated ctxt in let g = (ctxt, sc) in (step [@ocaml.tailcall]) g gas k ks power (accu, stack) | IKeccak (_, k) -> let bytes = accu in let hash = Raw_hashes.keccak256 bytes in (step [@ocaml.tailcall]) g gas k ks hash stack | ISha3 (_, k) -> let bytes = accu in let hash = Raw_hashes.sha3_256 bytes in (step [@ocaml.tailcall]) g gas k ks hash stack | IAdd_bls12_381_g1 (_, k) -> let x = accu and (y, stack) = stack in let accu = Bls12_381.G1.add x y in (step [@ocaml.tailcall]) g gas k ks accu stack | IAdd_bls12_381_g2 (_, k) -> let x = accu and (y, stack) = stack in let accu = Bls12_381.G2.add x y in (step [@ocaml.tailcall]) g gas k ks accu stack | IAdd_bls12_381_fr (_, k) -> let x = accu and (y, stack) = stack in let accu = Bls12_381.Fr.add x y in (step [@ocaml.tailcall]) g gas k ks accu stack | IMul_bls12_381_g1 (_, k) -> let x = accu and (y, stack) = stack in let accu = Bls12_381.G1.mul x y in (step [@ocaml.tailcall]) g gas k ks accu stack | IMul_bls12_381_g2 (_, k) -> let x = accu and (y, stack) = stack in let accu = Bls12_381.G2.mul x y in (step [@ocaml.tailcall]) g gas k ks accu stack | IMul_bls12_381_fr (_, k) -> let x = accu and (y, stack) = stack in let accu = Bls12_381.Fr.mul x y in (step [@ocaml.tailcall]) g gas k ks accu stack | IMul_bls12_381_fr_z (_, k) -> let x = accu and (y, stack) = stack in let x = Bls12_381.Fr.of_z (Script_int.to_zint x) in let res = Bls12_381.Fr.mul x y in (step [@ocaml.tailcall]) g gas k ks res stack | IMul_bls12_381_z_fr (_, k) -> let y = accu and (x, stack) = stack in let x = Bls12_381.Fr.of_z (Script_int.to_zint x) in let res = Bls12_381.Fr.mul x y in (step [@ocaml.tailcall]) g gas k ks res stack | IInt_bls12_381_fr (_, k) -> let x = accu in let res = Script_int.of_zint (Bls12_381.Fr.to_z x) in (step [@ocaml.tailcall]) g gas k ks res stack | INeg_bls12_381_g1 (_, k) -> let x = accu in let accu = Bls12_381.G1.negate x in (step [@ocaml.tailcall]) g gas k ks accu stack | INeg_bls12_381_g2 (_, k) -> let x = accu in let accu = Bls12_381.G2.negate x in (step [@ocaml.tailcall]) g gas k ks accu stack | INeg_bls12_381_fr (_, k) -> let x = accu in let accu = Bls12_381.Fr.negate x in (step [@ocaml.tailcall]) g gas k ks accu stack | IPairing_check_bls12_381 (_, k) -> let pairs = accu in let check = match pairs.elements with | [] -> true | pairs -> Bls12_381.( miller_loop pairs |> final_exponentiation_opt |> Option.map Gt.(eq one)) |> Option.value ~default:false in (step [@ocaml.tailcall]) g gas k ks check stack | IComb (_, _, witness, k) -> let rec aux : type before after. (before, after) comb_gadt_witness -> before -> after = fun witness stack -> match (witness, stack) with | (Comb_one, stack) -> stack | (Comb_succ witness', (a, tl)) -> let (b, tl') = aux witness' tl in ((a, b), tl') in let stack = aux witness (accu, stack) in let (accu, stack) = stack in (step [@ocaml.tailcall]) g gas k ks accu stack | IUncomb (_, _, witness, k) -> let rec aux : type before after. (before, after) uncomb_gadt_witness -> before -> after = fun witness stack -> match (witness, stack) with | (Uncomb_one, stack) -> stack | (Uncomb_succ witness', ((a, b), tl)) -> (a, aux witness' (b, tl)) in let stack = aux witness (accu, stack) in let (accu, stack) = stack in (step [@ocaml.tailcall]) g gas k ks accu stack | IComb_get (_, _, witness, k) -> let comb = accu in let rec aux : type before after. (before, after) comb_get_gadt_witness -> before -> after = fun witness comb -> match (witness, comb) with | (Comb_get_zero, v) -> v | (Comb_get_one, (a, _)) -> a | (Comb_get_plus_two witness', (_, b)) -> aux witness' b in let accu = aux witness comb in (step [@ocaml.tailcall]) g gas k ks accu stack | IComb_set (_, _, witness, k) -> let value = accu and (comb, stack) = stack in let rec aux : type value before after. (value, before, after) comb_set_gadt_witness -> value -> before -> after = fun witness value item -> match (witness, item) with | (Comb_set_zero, _) -> value | (Comb_set_one, (_hd, tl)) -> (value, tl) | (Comb_set_plus_two witness', (hd, tl)) -> (hd, aux witness' value tl) in let accu = aux witness value comb in (step [@ocaml.tailcall]) g gas k ks accu stack | IDup_n (_, _, witness, k) -> let rec aux : type before after. (before, after) dup_n_gadt_witness -> before -> after = fun witness stack -> match (witness, stack) with | (Dup_n_zero, (a, _)) -> a | (Dup_n_succ witness', (_, tl)) -> aux witness' tl in let stack = (accu, stack) in let accu = aux witness stack in (step [@ocaml.tailcall]) g gas k ks accu stack (* Tickets *) | ITicket (_, k) -> let contents = accu and (amount, stack) = stack in let ticketer = (sc.self, "default") in let accu = {ticketer; contents; amount} in (step [@ocaml.tailcall]) g gas k ks accu stack | IRead_ticket (_, k) -> let {ticketer; contents; amount} = accu in let stack = (accu, stack) in let accu = (ticketer, (contents, amount)) in (step [@ocaml.tailcall]) g gas k ks accu stack | ISplit_ticket (_, k) -> let ticket = accu and ((amount_a, amount_b), stack) = stack in let result = if Compare.Int.( Script_int.(compare (add_n amount_a amount_b) ticket.amount) = 0) then Some ({ticket with amount = amount_a}, {ticket with amount = amount_b}) else None in (step [@ocaml.tailcall]) g gas k ks result stack | IJoin_tickets (_, contents_ty, k) -> let (ticket_a, ticket_b) = accu in let result = if Compare.Int.( compare_address ticket_a.ticketer ticket_b.ticketer = 0 && compare_comparable contents_ty ticket_a.contents ticket_b.contents = 0) then Some { ticketer = ticket_a.ticketer; contents = ticket_a.contents; amount = Script_int.add_n ticket_a.amount ticket_b.amount; } else None in (step [@ocaml.tailcall]) g gas k ks result stack ) (* Zero-cost logging ================= *) (* The following functions insert a logging instruction and modify the continuation to continue the logging process in the next execution steps. There is a special treatment of instructions that generate fresh continuations: we pass a constructor as argument to their evaluation rules so that they can instrument these fresh continuations by themselves. This on-the-fly instrumentation of the execution allows zero-cost logging since logging instructions are only introduced if an initial logging continuation is pushed in the initial continuation that starts the evaluation. *) and log : type a s b t r f. logger * logging_event -> (a, s, b, t, r, f) step_type = fun (logger, event) ((ctxt, _) as g) gas k ks accu stack -> ( match (k, event) with | (ILog _, LogEntry) -> () | (_, LogEntry) -> log_entry logger ctxt gas k accu stack | (_, LogExit prev_kinfo) -> log_exit logger ctxt gas prev_kinfo k accu stack ) ; let k = log_next_kinstr logger k in let with_log k = match k with KLog _ -> k | _ -> KLog (k, logger) in match k with | IList_map (_, body, k) -> (ilist_map [@ocaml.tailcall]) with_log g gas (body, k) ks accu stack | IList_iter (_, body, k) -> (ilist_iter [@ocaml.tailcall]) with_log g gas (body, k) ks accu stack | ISet_iter (_, body, k) -> (iset_iter [@ocaml.tailcall]) with_log g gas (body, k) ks accu stack | IMap_map (_, body, k) -> (imap_map [@ocaml.tailcall]) with_log g gas (body, k) ks accu stack | IMap_iter (_, body, k) -> (imap_iter [@ocaml.tailcall]) with_log g gas (body, k) ks accu stack | ILoop (_, body, k) -> let ks = with_log (KLoop_in (body, KCons (k, ks))) in (next [@ocaml.tailcall]) g gas ks accu stack | ILoop_left (_, bl, br) -> let ks = with_log (KLoop_in_left (bl, KCons (br, ks))) in (next [@ocaml.tailcall]) g gas ks accu stack | IMul_teznat (kinfo, k) -> let extra = (kinfo, k) in (imul_teznat [@ocaml.tailcall]) (Some logger) g gas extra ks accu stack | IMul_nattez (kinfo, k) -> let extra = (kinfo, k) in (imul_nattez [@ocaml.tailcall]) (Some logger) g gas extra ks accu stack | ILsl_nat (kinfo, k) -> let extra = (kinfo, k) in (ilsl_nat [@ocaml.tailcall]) (Some logger) g gas extra ks accu stack | ILsr_nat (kinfo, k) -> let extra = (kinfo, k) in (ilsr_nat [@ocaml.tailcall]) (Some logger) g gas extra ks accu stack | IFailwith (_, kloc, tv, _) -> (ifailwith [@ocaml.tailcall]) (Some logger) g gas kloc tv accu | IExec (_, k) -> (iexec [@ocaml.tailcall]) (Some logger) g gas k ks accu stack | _ -> (step [@ocaml.tailcall]) g gas k (with_log ks) accu stack [@@inline] and klog : type a s r f. logger -> outdated_context * step_constants -> local_gas_counter -> (a, s, r, f) continuation -> (a, s, r, f) continuation -> a -> s -> (r * f * outdated_context * local_gas_counter) tzresult Lwt.t = fun logger g gas ks0 ks accu stack -> (match ks with KLog _ -> () | _ -> log_control logger ks) ; let enable_log ki = log_kinstr logger ki in let mk k = match k with KLog _ -> k | _ -> KLog (k, logger) in match ks with | KCons (ki, ks') -> let log = enable_log ki in let ks = mk ks' in (step [@ocaml.tailcall]) g gas log ks accu stack | KNil -> (next [@ocaml.tailcall]) g gas ks accu stack | KLoop_in (ki, ks') -> let ks' = mk ks' in let ki = enable_log ki in (kloop_in [@ocaml.tailcall]) g gas ks0 ki ks' accu stack | KReturn (stack', ks') -> let ks' = mk ks' in let ks = KReturn (stack', ks') in (next [@ocaml.tailcall]) g gas ks accu stack | KLoop_in_left (ki, ks') -> let ks' = mk ks' in let ki = enable_log ki in (kloop_in_left [@ocaml.tailcall]) g gas ks0 ki ks' accu stack | KUndip (x, ks') -> let ks' = mk ks' in let ks = KUndip (x, ks') in (next [@ocaml.tailcall]) g gas ks accu stack | KIter (body, xs, ks') -> let ks' = mk ks' in let body = enable_log body in (kiter [@ocaml.tailcall]) mk g gas (body, xs) ks' accu stack | KList_enter_body (body, xs, ys, len, ks') -> let ks' = mk ks' in let extra = (body, xs, ys, len) in (klist_enter [@ocaml.tailcall]) mk g gas extra ks' accu stack | KList_exit_body (body, xs, ys, len, ks') -> let ks' = mk ks' in let extra = (body, xs, ys, len) in (klist_exit [@ocaml.tailcall]) mk g gas extra ks' accu stack | KMap_enter_body (body, xs, ys, ks') -> let ks' = mk ks' in (kmap_enter [@ocaml.tailcall]) mk g gas (body, xs, ys) ks' accu stack | KMap_exit_body (body, xs, ys, yk, ks') -> let ks' = mk ks' in (kmap_exit [@ocaml.tailcall]) mk g gas (body, xs, ys, yk) ks' accu stack | KLog (_, _) -> (* This case should never happen. *) (next [@ocaml.tailcall]) g gas ks accu stack [@@inline] (* Entrypoints =========== *) let step_descr ~log_now logger (ctxt, sc) descr accu stack = let gas = (Gas.remaining_operation_gas ctxt :> int) in ( match logger with | None -> step (outdated ctxt, sc) gas descr.kinstr KNil accu stack | Some logger -> ( if log_now then let kinfo = kinfo_of_kinstr descr.kinstr in logger.log_interp descr.kinstr ctxt kinfo.iloc descr.kbef (accu, stack) ) ; let log = ILog (kinfo_of_kinstr descr.kinstr, LogEntry, logger, descr.kinstr) in step (outdated ctxt, sc) gas log KNil accu stack ) >>=? fun (accu, stack, ctxt, gas) -> return (accu, stack, update_context gas ctxt) let interp logger g (Lam (code, _)) arg = step_descr ~log_now:true logger g code arg (EmptyCell, EmptyCell) >|=? fun (ret, (EmptyCell, EmptyCell), ctxt) -> (ret, ctxt) let kstep logger ctxt step_constants kinstr accu stack = let gas = (Gas.remaining_operation_gas ctxt :> int) in let kinstr = match logger with | None -> kinstr | Some logger -> ILog (kinfo_of_kinstr kinstr, LogEntry, logger, kinstr) in step (outdated ctxt, step_constants) gas kinstr KNil accu stack >>=? fun (accu, stack, ctxt, gas) -> return (accu, stack, update_context gas ctxt) let internal_step ctxt step_constants gas kinstr accu stack = step (ctxt, step_constants) gas kinstr KNil accu stack let step logger ctxt step_constants descr stack = step_descr ~log_now:false logger (ctxt, step_constants) descr stack (* High-level functions ==================== *) let execute logger ctxt mode step_constants ~entrypoint ~internal unparsed_script arg : ( Script.expr * packed_internal_operation list * context * Lazy_storage.diffs option ) tzresult Lwt.t = parse_script ctxt unparsed_script ~legacy:true ~allow_forged_in_storage:true >>=? fun (Ex_script {code; arg_type; storage; storage_type; root_name}, ctxt) -> record_trace (Bad_contract_parameter step_constants.self) (find_entrypoint arg_type ~root_name entrypoint) >>?= fun (box, _) -> trace (Bad_contract_parameter step_constants.self) (parse_data ctxt ~legacy:false ~allow_forged:internal arg_type (box arg)) >>=? fun (arg, ctxt) -> Script.force_decode_in_context ctxt unparsed_script.code >>?= fun (script_code, ctxt) -> Script_ir_translator.collect_lazy_storage ctxt arg_type arg >>?= fun (to_duplicate, ctxt) -> Script_ir_translator.collect_lazy_storage ctxt storage_type storage >>?= fun (to_update, ctxt) -> trace (Runtime_contract_error (step_constants.self, script_code)) (interp logger (ctxt, step_constants) code (arg, storage)) >>=? fun ((ops, storage), ctxt) -> Script_ir_translator.extract_lazy_storage_diff ctxt mode ~temporary:false ~to_duplicate ~to_update storage_type storage >>=? fun (storage, lazy_storage_diff, ctxt) -> trace Cannot_serialize_storage ( unparse_data ctxt mode storage_type storage >>=? fun (storage, ctxt) -> Lwt.return ( Gas.consume ctxt (Script.strip_locations_cost storage) >>? fun ctxt -> ok (Micheline.strip_locations storage, ctxt) ) ) >|=? fun (storage, ctxt) -> let (ops, op_diffs) = List.split ops.elements in let lazy_storage_diff = match List.flatten (List.map (Option.value ~default:[]) (op_diffs @ [lazy_storage_diff])) with | [] -> None | diff -> Some diff in (storage, ops, ctxt, lazy_storage_diff) type execution_result = { ctxt : context; storage : Script.expr; lazy_storage_diff : Lazy_storage.diffs option; operations : packed_internal_operation list; } let execute ?logger ctxt mode step_constants ~script ~entrypoint ~parameter ~internal = execute logger ctxt mode step_constants ~entrypoint ~internal script (Micheline.root parameter) >|=? fun (storage, operations, ctxt, lazy_storage_diff) -> {ctxt; storage; lazy_storage_diff; operations} (* Internals ========= *) (* We export the internals definitions for tool that requires a white-box view on the interpreter, typically snoop, the gas model inference engine. *) module Internals = struct type nonrec local_gas_counter = local_gas_counter type nonrec outdated_context = outdated_context = | OutDatedContext of Alpha_context.t [@@unboxed] let next logger g gas ks accu stack = let ks = match logger with None -> ks | Some logger -> KLog (ks, logger) in next g gas ks accu stack let step (ctxt, step_constants) gas ks accu stack = internal_step ctxt step_constants gas ks accu stack end
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>