builtins.rs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. // SPDX-License-Identifier: Apache-2.0
  2. use parity_scale_codec::{Decode, Encode};
  3. use crate::build_solidity;
  4. #[test]
  5. fn abi_decode() {
  6. let mut runtime = build_solidity(
  7. r##"
  8. contract bar {
  9. function test() public {
  10. (int16 a, bool b) = abi.decode(hex"7f0001", (int16, bool));
  11. assert(a == 127);
  12. assert(b == true);
  13. }
  14. }"##,
  15. );
  16. runtime.function("test", Vec::new());
  17. let mut runtime = build_solidity(
  18. r##"
  19. contract bar {
  20. function test() public {
  21. uint8 a = abi.decode(hex"40", (uint8));
  22. assert(a == 64);
  23. }
  24. }"##,
  25. );
  26. runtime.function("test", Vec::new());
  27. }
  28. #[test]
  29. fn abi_encode() {
  30. let mut runtime = build_solidity(
  31. r##"
  32. struct s {
  33. int32 f1;
  34. uint8 f2;
  35. string f3;
  36. uint16[2] f4;
  37. }
  38. contract bar {
  39. function test() public {
  40. uint16 a = 0xfd01;
  41. assert(abi.encode(a) == hex"01fd");
  42. uint32 b = 0xaabbccdd;
  43. assert(abi.encode(true, b, false) == hex"01ddccbbaa00");
  44. }
  45. function test2() public {
  46. string b = "foobar";
  47. assert(abi.encode(b) == hex"18666f6f626172");
  48. assert(abi.encode("foobar") == hex"18666f6f626172");
  49. }
  50. function test3() public {
  51. s x = s({ f1: 511, f2: 0xf7, f3: "testie", f4: [ uint16(4), 5 ] });
  52. assert(abi.encode(x) == hex"ff010000f71874657374696504000500");
  53. }
  54. }"##,
  55. );
  56. runtime.function("test", Vec::new());
  57. runtime.heap_verify();
  58. runtime.function("test2", Vec::new());
  59. runtime.heap_verify();
  60. runtime.function("test3", Vec::new());
  61. runtime.heap_verify();
  62. }
  63. #[test]
  64. fn abi_encode_packed() {
  65. let mut runtime = build_solidity(
  66. r##"
  67. struct s {
  68. int32 f1;
  69. uint8 f2;
  70. string f3;
  71. uint16[2] f4;
  72. }
  73. contract bar {
  74. function test() public {
  75. uint16 a = 0xfd01;
  76. assert(abi.encodePacked(a) == hex"01fd");
  77. uint32 b = 0xaabbccdd;
  78. assert(abi.encodePacked(true, b, false) == hex"01ddccbbaa00");
  79. }
  80. function test2() public {
  81. string b = "foobar";
  82. assert(abi.encodePacked(b) == "foobar");
  83. assert(abi.encodePacked("foobar") == "foobar");
  84. assert(abi.encodePacked("foo", "bar") == "foobar");
  85. }
  86. function test3() public {
  87. s x = s({ f1: 511, f2: 0xf7, f3: "testie", f4: [ uint16(4), 5 ] });
  88. assert(abi.encodePacked(x) == hex"ff010000f774657374696504000500");
  89. }
  90. }"##,
  91. );
  92. runtime.function("test", Vec::new());
  93. runtime.function("test2", Vec::new());
  94. runtime.function("test3", Vec::new());
  95. }
  96. #[test]
  97. fn abi_encode_with_selector() {
  98. let mut runtime = build_solidity(
  99. r##"
  100. contract bar {
  101. function test1() public {
  102. uint16 a = 0xfd01;
  103. assert(abi.encodeWithSelector(hex"44332211", a) == hex"4433221101fd");
  104. uint32 b = 0xaabbccdd;
  105. assert(abi.encodeWithSelector(hex"aabbccdd", true, b, false) == hex"aabbccdd01ddccbbaa00");
  106. assert(abi.encodeWithSelector(hex"aabbccdd") == hex"aabbccdd");
  107. }
  108. function test2() public {
  109. uint8[] arr = new uint8[](3);
  110. arr[0] = 0xfe;
  111. arr[1] = 0xfc;
  112. arr[2] = 0xf8;
  113. assert(abi.encodeWithSelector(hex"01020304", arr) == hex"010203040cfefcf8");
  114. }
  115. }"##,
  116. );
  117. runtime.function("test1", Vec::new());
  118. runtime.function("test2", Vec::new());
  119. }
  120. #[test]
  121. fn abi_encode_with_signature() {
  122. let mut runtime = build_solidity(
  123. r##"
  124. contract bar {
  125. string bla = "Hello, World!";
  126. function test1() public {
  127. assert(keccak256("Hello, World!") == hex"acaf3289d7b601cbd114fb36c4d29c85bbfd5e133f14cb355c3fd8d99367964f");
  128. assert(abi.encodeWithSignature("Hello, World!") == hex"acaf3289");
  129. assert(abi.encodeWithSignature(bla) == hex"acaf3289");
  130. }
  131. function test2() public {
  132. uint8[] arr = new uint8[](3);
  133. arr[0] = 0xfe;
  134. arr[1] = 0xfc;
  135. arr[2] = 0xf8;
  136. assert(abi.encodeWithSelector(hex"01020304", arr) == hex"010203040cfefcf8");
  137. }
  138. }"##,
  139. );
  140. runtime.constructor(0, Vec::new());
  141. runtime.function("test1", Vec::new());
  142. runtime.function("test2", Vec::new());
  143. }
  144. #[test]
  145. fn call() {
  146. let mut runtime = build_solidity(
  147. r##"
  148. contract superior {
  149. function test1() public {
  150. inferior i = new inferior();
  151. i.test1();
  152. assert(keccak256("test1()") == hex"6b59084dfb7dcf1c687dd12ad5778be120c9121b21ef90a32ff73565a36c9cd3");
  153. bytes bs;
  154. bool success;
  155. (success, bs) = address(i).call(hex"6b59084d");
  156. assert(success == true);
  157. assert(bs == hex"");
  158. }
  159. function test2() public {
  160. inferior i = new inferior();
  161. assert(i.test2(257) == 256);
  162. assert(keccak256("test2(uint64)") == hex"296dacf0801def8823747fbd751fbc1444af573e88de40d29c4d01f6013bf095");
  163. bytes bs;
  164. bool success;
  165. (success, bs) = address(i).call(hex"296dacf0_0101_0000__0000_0000");
  166. assert(success == true);
  167. assert(bs == hex"0001_0000__0000_0000");
  168. }
  169. }
  170. contract inferior {
  171. function test1() public {
  172. print("Baa!");
  173. }
  174. function test2(uint64 x) public returns (uint64) {
  175. return x ^ 1;
  176. }
  177. }"##,
  178. );
  179. runtime.function("test1", Vec::new());
  180. runtime.function("test2", Vec::new());
  181. let mut runtime = build_solidity(
  182. r##"
  183. contract superior {
  184. function test1() public {
  185. inferior i = new inferior();
  186. assert(keccak256("test1()") == hex"6b59084dfb7dcf1c687dd12ad5778be120c9121b21ef90a32ff73565a36c9cd3");
  187. bytes bs;
  188. bool success;
  189. (success, bs) = address(i).call(abi.encodeWithSelector(hex"6b59084d"));
  190. assert(success == true);
  191. assert(bs == hex"");
  192. (success, bs) = address(i).call(abi.encodeWithSignature("test1()"));
  193. assert(success == true);
  194. assert(bs == hex"");
  195. }
  196. function test2() public {
  197. inferior i = new inferior();
  198. assert(keccak256("test2(uint64)") == hex"296dacf0801def8823747fbd751fbc1444af573e88de40d29c4d01f6013bf095");
  199. bytes bs;
  200. bool success;
  201. (success, bs) = address(i).call(abi.encodeWithSelector(hex"296dacf0", uint64(257)));
  202. assert(success == true);
  203. assert(abi.decode(bs, (uint64)) == 256);
  204. (success, bs) = address(i).call(abi.encodeWithSignature("test2(uint64)", uint64(0xfeec)));
  205. assert(success == true);
  206. assert(abi.decode(bs, (uint64)) == 0xfeed);
  207. }
  208. }
  209. contract inferior {
  210. function test1() public {
  211. print("Baa!");
  212. }
  213. function test2(uint64 x) public returns (uint64) {
  214. return x ^ 1;
  215. }
  216. }"##,
  217. );
  218. runtime.function("test1", Vec::new());
  219. runtime.function("test2", Vec::new());
  220. }
  221. #[test]
  222. fn block() {
  223. let mut runtime = build_solidity(
  224. r##"
  225. contract bar {
  226. function test() public {
  227. uint64 b = block.number;
  228. assert(b == 950_119_597);
  229. }
  230. }"##,
  231. );
  232. runtime.function("test", Vec::new());
  233. let mut runtime = build_solidity(
  234. r##"
  235. contract bar {
  236. function test() public {
  237. uint64 b = block.timestamp;
  238. assert(b == 1594035638);
  239. }
  240. }"##,
  241. );
  242. runtime.function("test", Vec::new());
  243. let mut runtime = build_solidity(
  244. r##"
  245. contract bar {
  246. function test() public {
  247. uint128 b = block.minimum_balance;
  248. assert(b == 500);
  249. }
  250. }"##,
  251. );
  252. runtime.function("test", Vec::new());
  253. }
  254. #[test]
  255. fn tx() {
  256. let mut runtime = build_solidity(
  257. r##"
  258. contract bar {
  259. function test() public {
  260. uint128 b = tx.gasprice(1);
  261. assert(b == 59_541_253_813_967);
  262. }
  263. }"##,
  264. );
  265. runtime.function("test", Vec::new());
  266. let mut runtime = build_solidity(
  267. r##"
  268. contract bar {
  269. function test() public {
  270. uint128 b = tx.gasprice(1000);
  271. assert(b == 59_541_253_813_967_000);
  272. }
  273. }"##,
  274. );
  275. runtime.function("test", Vec::new());
  276. }
  277. #[test]
  278. fn msg() {
  279. let mut runtime = build_solidity(
  280. r##"
  281. contract bar {
  282. function test() public payable {
  283. uint128 b = msg.value;
  284. assert(b == 145_594_775_678_703_046_797_448_357_509_034_994_219);
  285. }
  286. }"##,
  287. );
  288. runtime.vm.value = 145_594_775_678_703_046_797_448_357_509_034_994_219;
  289. runtime.function("test", Vec::new());
  290. let mut runtime = build_solidity(
  291. r##"
  292. contract c {
  293. function test() public {
  294. other o = new other();
  295. address foo = o.test();
  296. assert(foo == address(this));
  297. }
  298. }
  299. contract other {
  300. function test() public returns (address) {
  301. return msg.sender;
  302. }
  303. }
  304. "##,
  305. );
  306. runtime.function("test", Vec::new());
  307. }
  308. #[test]
  309. fn functions() {
  310. let mut runtime = build_solidity(
  311. r##"
  312. contract bar {
  313. function test() public {
  314. uint64 b = gasleft();
  315. assert(b == 2_224_097_461);
  316. }
  317. }"##,
  318. );
  319. runtime.function("test", Vec::new());
  320. }
  321. #[test]
  322. fn data() {
  323. #[derive(Debug, PartialEq, Eq, Encode, Decode)]
  324. struct Uint32(u32);
  325. #[derive(Debug, PartialEq, Eq, Encode, Decode)]
  326. struct String(Vec<u8>);
  327. let mut runtime = build_solidity(
  328. r##"
  329. contract bar {
  330. constructor(string memory s) public {
  331. assert(msg.data == hex"98dd1bb318666f6f626172");
  332. assert(msg.sig == hex"98dd_1bb3");
  333. }
  334. function test(uint32 x) public {
  335. assert(msg.data == hex"e3cff634addeadde");
  336. assert(msg.sig == hex"e3cf_f634");
  337. }
  338. }"##,
  339. );
  340. runtime.constructor(0, String(b"foobar".to_vec()).encode());
  341. runtime.function("test", Uint32(0xdeaddead).encode());
  342. }
  343. #[test]
  344. fn addmod() {
  345. // does it work with small numbers
  346. let mut runtime = build_solidity(
  347. r##"
  348. contract x {
  349. function test() public {
  350. assert(addmod(500, 100, 3) == 0);
  351. }
  352. }"##,
  353. );
  354. runtime.function("test", Vec::new());
  355. // divide by zero
  356. let mut runtime = build_solidity(
  357. r##"
  358. contract x {
  359. function test() public {
  360. assert(addmod(500, 100, 0) == 0);
  361. }
  362. }"##,
  363. );
  364. runtime.function("test", Vec::new());
  365. // bigger numbers (64 bit)
  366. let mut runtime = build_solidity(
  367. r##"
  368. contract x {
  369. function test() public {
  370. // 8_163_321_534_310_945_187 * 16_473_784_705_703_234_153 = 134_480_801_439_669_508_040_541_782_812_209_371_611
  371. assert(addmod(
  372. 0,
  373. 134_480_801_439_669_508_040_541_782_812_209_371_611,
  374. 16_473_784_705_703_234_153) == 0);
  375. }
  376. }"##,
  377. );
  378. runtime.function("test", Vec::new());
  379. // bigger numbers (128 bit)
  380. let mut runtime = build_solidity(
  381. r##"
  382. contract x {
  383. function test() public {
  384. // 254_765_928_331_839_140_628_748_569_208_536_440_801 * 148_872_967_607_295_528_830_315_866_466_318_446_379 = 37_927_759_795_988_462_606_362_647_643_228_779_300_269_446_446_871_437_380_583_919_404_728_626_309_579
  385. assert(addmod(
  386. 0,
  387. 37_927_759_795_988_462_606_362_647_643_228_779_300_269_446_446_871_437_380_583_919_404_728_626_309_579,
  388. 148_872_967_607_295_528_830_315_866_466_318_446_379) == 0);
  389. }
  390. }"##,
  391. );
  392. runtime.function("test", Vec::new());
  393. // bigger numbers (256 bit)
  394. let mut runtime = build_solidity(
  395. r##"
  396. contract x {
  397. function test() public {
  398. assert(addmod(
  399. 109802613191917590715814365746623394364442484359636492253827647701845853490667,
  400. 49050800785888222684575674817707208319566972397745729319314900174750088808217,
  401. 233) == 204);
  402. }
  403. }"##,
  404. );
  405. runtime.function("test", Vec::new());
  406. let mut runtime = build_solidity(
  407. r##"
  408. contract x {
  409. function test() public {
  410. assert(addmod(
  411. 109802613191917590715814365746623394364442484359636492253827647701845853490667,
  412. 109802613191917590715814365746623394364442484359636492253827647701845853490667,
  413. 2) == 0);
  414. }
  415. }"##,
  416. );
  417. runtime.function("test", Vec::new());
  418. }
  419. #[test]
  420. fn mulmod() {
  421. // does it work with small numbers
  422. let mut runtime = build_solidity(
  423. r##"
  424. contract x {
  425. function test() public {
  426. assert(mulmod(500, 100, 5) == 0);
  427. }
  428. }"##,
  429. );
  430. runtime.function("test", Vec::new());
  431. // divide by zero
  432. let mut runtime = build_solidity(
  433. r##"
  434. contract x {
  435. function test() public {
  436. assert(mulmod(500, 100, 0) == 0);
  437. }
  438. }"##,
  439. );
  440. runtime.function("test", Vec::new());
  441. // bigger numbers
  442. let mut runtime = build_solidity(
  443. r##"
  444. contract x {
  445. function test() public {
  446. assert(mulmod(50000, 10000, 5) == 0);
  447. }
  448. }"##,
  449. );
  450. runtime.function("test", Vec::new());
  451. let mut runtime = build_solidity(
  452. r##"
  453. contract x {
  454. function test() public {
  455. assert(mulmod(18446744073709551616, 18446744073709550403, 1024) == 0);
  456. }
  457. }"##,
  458. );
  459. runtime.function("test", Vec::new());
  460. // 2^127 = 170141183460469231731687303715884105728
  461. let mut runtime = build_solidity(
  462. r##"
  463. contract x {
  464. function test() public {
  465. assert(mulmod(170141183460469231731687303715884105728, 170141183460469231731687303715884105728, 170141183460469231731687303715884105728) == 0);
  466. }
  467. }"##,
  468. );
  469. runtime.function("test", Vec::new());
  470. // 2^128 = 340282366920938463463374607431768211456
  471. let mut runtime = build_solidity(
  472. r##"
  473. contract x {
  474. function test() public {
  475. assert(mulmod(340282366920938463463374607431768211456, 340282366920938463463374607431768211456, 340282366920938463463374607431768211456) == 0);
  476. }
  477. }"##,
  478. );
  479. runtime.function("test", Vec::new());
  480. // 2^240 = 1766847064778384329583297500742918515827483896875618958121606201292619776
  481. let mut runtime = build_solidity(
  482. r##"
  483. contract x {
  484. function test() public {
  485. assert(mulmod(1766847064778384329583297500742918515827483896875618958121606201292619776,
  486. 1766847064778384329583297500742918515827483896875618958121606201292619776,
  487. 1766847064778384329583297500742918515827483896875618958121606201292619776)
  488. == 0);
  489. }
  490. }"##,
  491. );
  492. runtime.function("test", Vec::new());
  493. // 240 bit prime: 824364134751099588297822369420176791913922347811791536817152126684405253
  494. let mut runtime = build_solidity(
  495. r##"
  496. contract x {
  497. function test() public {
  498. assert(mulmod(824364134751099588297822369420176791913922347811791536817152126684405253,
  499. 824364134751099588297822369420176791913922347811791536817152126684405253,
  500. 824364134751099588297822369420176791913922347811791536817152126684405253)
  501. == 0);
  502. }
  503. }"##,
  504. );
  505. runtime.function("test", Vec::new());
  506. // 256 bit prime: 113477814626329405513123655892059150026234290706112418221315641434319827527851
  507. let mut runtime = build_solidity(
  508. r##"
  509. contract x {
  510. function test() public {
  511. assert(mulmod(113477814626329405513123655892059150026234290706112418221315641434319827527851,
  512. 113477814626329405513123655892059150026234290706112418221315641434319827527851,
  513. 113477814626329405513123655892059150026234290706112418221315641434319827527851)
  514. == 0);
  515. }
  516. }"##,
  517. );
  518. runtime.function("test", Vec::new());
  519. let mut runtime = build_solidity(
  520. r##"
  521. contract x {
  522. function test() public {
  523. assert(mulmod(113477814626329405513123655892059150026234290706112418221315641434319827527851,
  524. 113477814626329405513123655892059150026234290706112418221315641434319827527841,
  525. 233)
  526. == 12);
  527. }
  528. }"##,
  529. );
  530. runtime.function("test", Vec::new());
  531. }
  532. #[test]
  533. fn my_token() {
  534. #[derive(Debug, PartialEq, Eq, Encode, Decode)]
  535. struct TokenTest([u8; 32], bool);
  536. let mut runtime = build_solidity(
  537. "
  538. contract mytoken {
  539. function test(address account, bool sender) public view returns (address) {
  540. if (sender) {
  541. return msg.sender;
  542. }
  543. return account;
  544. }
  545. }
  546. ",
  547. );
  548. let addr: [u8; 32] = [
  549. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
  550. 0x16, 0x17, 0x18, 0x19, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30,
  551. 0x31, 0x32,
  552. ];
  553. runtime.function("test", TokenTest(addr, true).encode());
  554. assert_eq!(&runtime.vm.caller[..], &runtime.vm.output[..]);
  555. runtime.function("test", TokenTest(addr, false).encode());
  556. assert_eq!(&runtime.vm.output[..], &addr[..]);
  557. runtime.function(
  558. "test",
  559. TokenTest(<[u8; 32]>::try_from(&runtime.vm.caller[..]).unwrap(), true).encode(),
  560. );
  561. assert_eq!(&runtime.vm.caller[..], &runtime.vm.output[..]);
  562. runtime.function(
  563. "test",
  564. TokenTest(<[u8; 32]>::try_from(&runtime.vm.caller[..]).unwrap(), false).encode(),
  565. );
  566. assert_eq!(&runtime.vm.caller[..], &runtime.vm.output[..]);
  567. }
  568. #[test]
  569. fn hash() {
  570. let mut runtime = build_solidity(
  571. r##"
  572. import "substrate";
  573. contract Foo {
  574. Hash current;
  575. bytes32 current2;
  576. function set(Hash h) public returns (bytes32) {
  577. current = h;
  578. current2 = Hash.unwrap(h);
  579. return current2;
  580. }
  581. function get() public view returns (Hash) {
  582. return Hash.wrap(current2);
  583. }
  584. }
  585. "##,
  586. );
  587. #[derive(Encode)]
  588. struct Hash([u8; 32]);
  589. let h = Hash([
  590. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
  591. 0x16, 0x17, 0x18, 0x19, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30,
  592. 0x31, 0x32,
  593. ]);
  594. runtime.function("set", h.encode());
  595. assert_eq!(&runtime.vm.output[..], &h.0[..]);
  596. runtime.function("get", vec![]);
  597. assert_eq!(&runtime.vm.output[..], &h.0[..]);
  598. }