builtins.rs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  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.constructor(0, Vec::new());
  180. runtime.function("test1", Vec::new());
  181. runtime.function("test2", Vec::new());
  182. let mut runtime = build_solidity(
  183. r##"
  184. contract superior {
  185. function test1() public {
  186. inferior i = new inferior();
  187. assert(keccak256("test1()") == hex"6b59084dfb7dcf1c687dd12ad5778be120c9121b21ef90a32ff73565a36c9cd3");
  188. bytes bs;
  189. bool success;
  190. (success, bs) = address(i).call(abi.encodeWithSelector(hex"6b59084d"));
  191. assert(success == true);
  192. assert(bs == hex"");
  193. (success, bs) = address(i).call(abi.encodeWithSignature("test1()"));
  194. assert(success == true);
  195. assert(bs == hex"");
  196. }
  197. function test2() public {
  198. inferior i = new inferior();
  199. assert(keccak256("test2(uint64)") == hex"296dacf0801def8823747fbd751fbc1444af573e88de40d29c4d01f6013bf095");
  200. bytes bs;
  201. bool success;
  202. (success, bs) = address(i).call(abi.encodeWithSelector(hex"296dacf0", uint64(257)));
  203. assert(success == true);
  204. assert(abi.decode(bs, (uint64)) == 256);
  205. (success, bs) = address(i).call(abi.encodeWithSignature("test2(uint64)", uint64(0xfeec)));
  206. assert(success == true);
  207. assert(abi.decode(bs, (uint64)) == 0xfeed);
  208. }
  209. }
  210. contract inferior {
  211. function test1() public {
  212. print("Baa!");
  213. }
  214. function test2(uint64 x) public returns (uint64) {
  215. return x ^ 1;
  216. }
  217. }"##,
  218. );
  219. runtime.constructor(0, Vec::new());
  220. runtime.function("test1", Vec::new());
  221. runtime.function("test2", Vec::new());
  222. }
  223. #[test]
  224. fn block() {
  225. let mut runtime = build_solidity(
  226. r##"
  227. contract bar {
  228. function test() public {
  229. uint64 b = block.number;
  230. assert(b == 950_119_597);
  231. }
  232. }"##,
  233. );
  234. runtime.function("test", Vec::new());
  235. let mut runtime = build_solidity(
  236. r##"
  237. contract bar {
  238. function test() public {
  239. uint64 b = block.timestamp;
  240. assert(b == 1594035638);
  241. }
  242. }"##,
  243. );
  244. runtime.function("test", Vec::new());
  245. let mut runtime = build_solidity(
  246. r##"
  247. contract bar {
  248. function test() public {
  249. uint128 b = block.minimum_balance;
  250. assert(b == 500);
  251. }
  252. }"##,
  253. );
  254. runtime.function("test", Vec::new());
  255. }
  256. #[test]
  257. fn tx() {
  258. let mut runtime = build_solidity(
  259. r##"
  260. contract bar {
  261. function test() public {
  262. uint128 b = tx.gasprice(1);
  263. assert(b == 59_541_253_813_967);
  264. }
  265. }"##,
  266. );
  267. runtime.function("test", Vec::new());
  268. let mut runtime = build_solidity(
  269. r##"
  270. contract bar {
  271. function test() public {
  272. uint128 b = tx.gasprice(1000);
  273. assert(b == 59_541_253_813_967_000);
  274. }
  275. }"##,
  276. );
  277. runtime.function("test", Vec::new());
  278. }
  279. #[test]
  280. fn msg() {
  281. let mut runtime = build_solidity(
  282. r##"
  283. contract bar {
  284. function test() public payable {
  285. uint128 b = msg.value;
  286. assert(b == 145_594_775_678_703_046_797_448_357_509_034_994_219);
  287. }
  288. }"##,
  289. );
  290. let value = 145_594_775_678_703_046_797_448_357_509_034_994_219;
  291. runtime.set_transferred_value(value);
  292. runtime.raw_function(runtime.contracts()[0].code.messages["test"].to_vec());
  293. let mut runtime = build_solidity(
  294. r##"
  295. contract c {
  296. function test() public {
  297. other o = new other();
  298. address foo = o.test();
  299. assert(foo == address(this));
  300. }
  301. }
  302. contract other {
  303. function test() public returns (address) {
  304. return msg.sender;
  305. }
  306. }
  307. "##,
  308. );
  309. runtime.constructor(0, Vec::new());
  310. runtime.function("test", Vec::new());
  311. }
  312. #[test]
  313. fn functions() {
  314. let mut runtime = build_solidity(
  315. r##"
  316. contract bar {
  317. function test() public {
  318. uint64 b = gasleft();
  319. assert(b == 2_224_097_461);
  320. }
  321. }"##,
  322. );
  323. runtime.function("test", Vec::new());
  324. }
  325. #[test]
  326. fn data() {
  327. #[derive(Debug, PartialEq, Eq, Encode, Decode)]
  328. struct Uint32(u32);
  329. #[derive(Debug, PartialEq, Eq, Encode, Decode)]
  330. struct String(Vec<u8>);
  331. let mut runtime = build_solidity(
  332. r##"
  333. contract bar {
  334. constructor(string memory s) public {
  335. assert(msg.data == hex"98dd1bb318666f6f626172");
  336. assert(msg.sig == hex"98dd_1bb3");
  337. }
  338. function test(uint32 x) public {
  339. assert(msg.data == hex"e3cff634addeadde");
  340. assert(msg.sig == hex"e3cf_f634");
  341. }
  342. }"##,
  343. );
  344. runtime.constructor(0, String(b"foobar".to_vec()).encode());
  345. runtime.function("test", Uint32(0xdeaddead).encode());
  346. }
  347. #[test]
  348. fn addmod() {
  349. // does it work with small numbers
  350. let mut runtime = build_solidity(
  351. r##"
  352. contract x {
  353. function test() public {
  354. assert(addmod(500, 100, 3) == 0);
  355. }
  356. }"##,
  357. );
  358. runtime.function("test", Vec::new());
  359. // divide by zero
  360. let mut runtime = build_solidity(
  361. r##"
  362. contract x {
  363. function test() public {
  364. assert(addmod(500, 100, 0) == 0);
  365. }
  366. }"##,
  367. );
  368. runtime.function("test", Vec::new());
  369. // bigger numbers (64 bit)
  370. let mut runtime = build_solidity(
  371. r##"
  372. contract x {
  373. function test() public {
  374. // 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
  375. assert(addmod(
  376. 0,
  377. 134_480_801_439_669_508_040_541_782_812_209_371_611,
  378. 16_473_784_705_703_234_153) == 0);
  379. }
  380. }"##,
  381. );
  382. runtime.function("test", Vec::new());
  383. // bigger numbers (128 bit)
  384. let mut runtime = build_solidity(
  385. r##"
  386. contract x {
  387. function test() public {
  388. // 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
  389. assert(addmod(
  390. 0,
  391. 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,
  392. 148_872_967_607_295_528_830_315_866_466_318_446_379) == 0);
  393. }
  394. }"##,
  395. );
  396. runtime.function("test", Vec::new());
  397. // bigger numbers (256 bit)
  398. let mut runtime = build_solidity(
  399. r##"
  400. contract x {
  401. function test() public {
  402. assert(addmod(
  403. 109802613191917590715814365746623394364442484359636492253827647701845853490667,
  404. 49050800785888222684575674817707208319566972397745729319314900174750088808217,
  405. 233) == 204);
  406. }
  407. }"##,
  408. );
  409. runtime.function("test", Vec::new());
  410. let mut runtime = build_solidity(
  411. r##"
  412. contract x {
  413. function test() public {
  414. assert(addmod(
  415. 109802613191917590715814365746623394364442484359636492253827647701845853490667,
  416. 109802613191917590715814365746623394364442484359636492253827647701845853490667,
  417. 2) == 0);
  418. }
  419. }"##,
  420. );
  421. runtime.function("test", Vec::new());
  422. }
  423. #[test]
  424. fn mulmod() {
  425. // does it work with small numbers
  426. let mut runtime = build_solidity(
  427. r##"
  428. contract x {
  429. function test() public {
  430. assert(mulmod(500, 100, 5) == 0);
  431. }
  432. }"##,
  433. );
  434. runtime.function("test", Vec::new());
  435. // divide by zero
  436. let mut runtime = build_solidity(
  437. r##"
  438. contract x {
  439. function test() public {
  440. assert(mulmod(500, 100, 0) == 0);
  441. }
  442. }"##,
  443. );
  444. runtime.function("test", Vec::new());
  445. // bigger numbers
  446. let mut runtime = build_solidity(
  447. r##"
  448. contract x {
  449. function test() public {
  450. assert(mulmod(50000, 10000, 5) == 0);
  451. }
  452. }"##,
  453. );
  454. runtime.function("test", Vec::new());
  455. let mut runtime = build_solidity(
  456. r##"
  457. contract x {
  458. function test() public {
  459. assert(mulmod(18446744073709551616, 18446744073709550403, 1024) == 0);
  460. }
  461. }"##,
  462. );
  463. runtime.function("test", Vec::new());
  464. // 2^127 = 170141183460469231731687303715884105728
  465. let mut runtime = build_solidity(
  466. r##"
  467. contract x {
  468. function test() public {
  469. assert(mulmod(170141183460469231731687303715884105728, 170141183460469231731687303715884105728, 170141183460469231731687303715884105728) == 0);
  470. }
  471. }"##,
  472. );
  473. runtime.function("test", Vec::new());
  474. // 2^128 = 340282366920938463463374607431768211456
  475. let mut runtime = build_solidity(
  476. r##"
  477. contract x {
  478. function test() public {
  479. assert(mulmod(340282366920938463463374607431768211456, 340282366920938463463374607431768211456, 340282366920938463463374607431768211456) == 0);
  480. }
  481. }"##,
  482. );
  483. runtime.function("test", Vec::new());
  484. // 2^240 = 1766847064778384329583297500742918515827483896875618958121606201292619776
  485. let mut runtime = build_solidity(
  486. r##"
  487. contract x {
  488. function test() public {
  489. assert(mulmod(1766847064778384329583297500742918515827483896875618958121606201292619776,
  490. 1766847064778384329583297500742918515827483896875618958121606201292619776,
  491. 1766847064778384329583297500742918515827483896875618958121606201292619776)
  492. == 0);
  493. }
  494. }"##,
  495. );
  496. runtime.function("test", Vec::new());
  497. // 240 bit prime: 824364134751099588297822369420176791913922347811791536817152126684405253
  498. let mut runtime = build_solidity(
  499. r##"
  500. contract x {
  501. function test() public {
  502. assert(mulmod(824364134751099588297822369420176791913922347811791536817152126684405253,
  503. 824364134751099588297822369420176791913922347811791536817152126684405253,
  504. 824364134751099588297822369420176791913922347811791536817152126684405253)
  505. == 0);
  506. }
  507. }"##,
  508. );
  509. runtime.function("test", Vec::new());
  510. // 256 bit prime: 113477814626329405513123655892059150026234290706112418221315641434319827527851
  511. let mut runtime = build_solidity(
  512. r##"
  513. contract x {
  514. function test() public {
  515. assert(mulmod(113477814626329405513123655892059150026234290706112418221315641434319827527851,
  516. 113477814626329405513123655892059150026234290706112418221315641434319827527851,
  517. 113477814626329405513123655892059150026234290706112418221315641434319827527851)
  518. == 0);
  519. }
  520. }"##,
  521. );
  522. runtime.function("test", Vec::new());
  523. let mut runtime = build_solidity(
  524. r##"
  525. contract x {
  526. function test() public {
  527. assert(mulmod(113477814626329405513123655892059150026234290706112418221315641434319827527851,
  528. 113477814626329405513123655892059150026234290706112418221315641434319827527841,
  529. 233)
  530. == 12);
  531. }
  532. }"##,
  533. );
  534. runtime.function("test", Vec::new());
  535. }
  536. #[test]
  537. fn my_token() {
  538. #[derive(Debug, PartialEq, Eq, Encode, Decode)]
  539. struct TokenTest([u8; 32], bool);
  540. let mut runtime = build_solidity(
  541. "
  542. contract mytoken {
  543. function test(address account, bool sender) public view returns (address) {
  544. if (sender) {
  545. return msg.sender;
  546. }
  547. return account;
  548. }
  549. }
  550. ",
  551. );
  552. let addr: [u8; 32] = [
  553. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
  554. 0x16, 0x17, 0x18, 0x19, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30,
  555. 0x31, 0x32,
  556. ];
  557. runtime.function("test", TokenTest(addr, true).encode());
  558. assert_eq!(&runtime.caller()[..], &runtime.output()[..]);
  559. runtime.function("test", TokenTest(addr, false).encode());
  560. assert_eq!(&runtime.output()[..], &addr[..]);
  561. runtime.function(
  562. "test",
  563. TokenTest(<[u8; 32]>::try_from(&runtime.caller()[..]).unwrap(), true).encode(),
  564. );
  565. assert_eq!(&runtime.caller()[..], &runtime.output()[..]);
  566. runtime.function(
  567. "test",
  568. TokenTest(<[u8; 32]>::try_from(&runtime.caller()[..]).unwrap(), false).encode(),
  569. );
  570. assert_eq!(&runtime.caller()[..], &runtime.output()[..]);
  571. }
  572. #[test]
  573. fn hash() {
  574. let mut runtime = build_solidity(
  575. r##"
  576. import "substrate";
  577. contract Foo {
  578. Hash current;
  579. bytes32 current2;
  580. function set(Hash h) public returns (bytes32) {
  581. current = h;
  582. current2 = Hash.unwrap(h);
  583. return current2;
  584. }
  585. function get() public view returns (Hash) {
  586. return Hash.wrap(current2);
  587. }
  588. function test_encoding() public view {
  589. Hash h = Hash.wrap(current2);
  590. assert(abi.encode(current2) == abi.encode(h));
  591. }
  592. }
  593. "##,
  594. );
  595. #[derive(Encode)]
  596. struct Hash([u8; 32]);
  597. let h = Hash([
  598. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
  599. 0x16, 0x17, 0x18, 0x19, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30,
  600. 0x31, 0x32,
  601. ]);
  602. runtime.function("set", h.encode());
  603. assert_eq!(&runtime.output()[..], &h.0[..]);
  604. runtime.function("get", vec![]);
  605. assert_eq!(&runtime.output()[..], &h.0[..]);
  606. runtime.function("test_encoding", vec![]);
  607. }
  608. #[test]
  609. fn call_chain_extension() {
  610. let mut runtime = build_solidity(
  611. r##"
  612. import {chain_extension as ChainExtension} from "substrate";
  613. contract Foo {
  614. function chain_extension(bytes input) public returns (uint32, bytes) {
  615. return ChainExtension(123, input);
  616. }
  617. }"##,
  618. );
  619. let data = 0xdeadbeefu32.to_be_bytes().to_vec();
  620. runtime.function("chain_extension", data.encode());
  621. let ret = <(u32, Vec<u8>)>::decode(&mut &runtime.output()[..]).unwrap();
  622. assert_eq!(ret.0, data.iter().map(|i| *i as u32).sum::<u32>());
  623. assert_eq!(ret.1, data.iter().cloned().rev().collect::<Vec<_>>());
  624. }
  625. #[test]
  626. fn is_contract() {
  627. let mut runtime = build_solidity(
  628. r##"
  629. import "substrate";
  630. contract Foo {
  631. function test(address _a) public view returns (bool) {
  632. return is_contract(_a);
  633. }
  634. }"##,
  635. );
  636. runtime.function("test", runtime.0.data().accounts[0].address.to_vec());
  637. assert_eq!(runtime.output(), vec![1]);
  638. runtime.function("test", [0; 32].to_vec());
  639. assert_eq!(runtime.output(), vec![0]);
  640. }
  641. #[test]
  642. fn set_code_hash() {
  643. let mut runtime = build_solidity(
  644. r##"
  645. import "substrate";
  646. abstract contract SetCode {
  647. function set_code(uint8[32] code_hash) external {
  648. require(set_code_hash(code_hash) == 0);
  649. }
  650. }
  651. contract CounterV1 is SetCode {
  652. uint32 public count;
  653. function inc() external {
  654. count += 1;
  655. }
  656. }
  657. contract CounterV2 is SetCode {
  658. uint32 public count;
  659. function inc() external {
  660. count -= 1;
  661. }
  662. }"##,
  663. );
  664. runtime.function("inc", vec![]);
  665. runtime.function("count", vec![]);
  666. assert_eq!(runtime.output(), 1u32.encode());
  667. let v2_code_hash = ink_primitives::Hash::default().as_ref().to_vec();
  668. runtime.function_expect_failure("set_code", v2_code_hash);
  669. let v2_code_hash = runtime.blobs()[1].hash;
  670. runtime.function("set_code", v2_code_hash.as_ref().to_vec());
  671. runtime.function("inc", vec![]);
  672. runtime.function("count", vec![]);
  673. assert_eq!(runtime.output(), 0u32.encode());
  674. let v1_code_hash = runtime.blobs()[0].hash;
  675. runtime.function("set_code", v1_code_hash.as_ref().to_vec());
  676. runtime.function("inc", vec![]);
  677. runtime.function("count", vec![]);
  678. assert_eq!(runtime.output(), 1u32.encode());
  679. }