arrays.rs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578
  1. // SPDX-License-Identifier: Apache-2.0
  2. use parity_scale_codec::{Decode, Encode};
  3. use rand::Rng;
  4. use crate::build_solidity;
  5. #[derive(Debug, PartialEq, Eq, Encode, Decode)]
  6. struct Val32(u32);
  7. #[derive(Debug, PartialEq, Eq, Encode, Decode)]
  8. struct Val8(u8);
  9. #[test]
  10. fn const_array_array() {
  11. let mut runtime = build_solidity(
  12. r##"
  13. contract foo {
  14. int8[8] constant bar = [ int8(1), 2, 3, 4, 5, 6, 7, 8 ];
  15. function f(uint32 i1) public returns (int8) {
  16. return bar[i1];
  17. }
  18. }"##,
  19. );
  20. runtime.function("f", Val32(1).encode());
  21. assert_eq!(runtime.output(), Val8(2).encode());
  22. }
  23. #[test]
  24. fn votes() {
  25. #[derive(Debug, PartialEq, Eq, Encode, Decode)]
  26. struct Votes([bool; 11]);
  27. let mut runtime = build_solidity(
  28. r##"
  29. contract foo {
  30. /// In a jury, do the ayes have it?
  31. function f(bool[11] votes) public pure returns (bool) {
  32. uint32 i;
  33. uint32 ayes = 0;
  34. for (i=0; i<votes.length; i++) {
  35. if (votes[i]) {
  36. ayes += 1;
  37. }
  38. }
  39. return ayes > votes.length / 2;
  40. }
  41. }"##,
  42. );
  43. runtime.function(
  44. "f",
  45. Votes([
  46. true, true, true, true, true, true, false, false, false, false, false,
  47. ])
  48. .encode(),
  49. );
  50. assert_eq!(runtime.output(), true.encode());
  51. runtime.function(
  52. "f",
  53. Votes([
  54. true, true, true, true, true, false, false, false, false, false, false,
  55. ])
  56. .encode(),
  57. );
  58. assert_eq!(runtime.output(), false.encode());
  59. }
  60. #[test]
  61. fn return_array() {
  62. #[derive(Debug, PartialEq, Eq, Encode, Decode)]
  63. struct Res([u64; 4]);
  64. let mut runtime = build_solidity(
  65. r##"
  66. contract foo {
  67. function array() pure public returns (int64[4]) {
  68. return [ int64(4), 84564, 31213, 1312 ];
  69. }
  70. }"##,
  71. );
  72. runtime.function("array", Vec::new());
  73. assert_eq!(runtime.output(), Res([4, 84564, 31213, 1312]).encode());
  74. }
  75. #[test]
  76. fn storage_arrays() {
  77. #[derive(Debug, PartialEq, Eq, Encode, Decode)]
  78. struct Val(i32);
  79. #[derive(Debug, PartialEq, Eq, Encode, Decode)]
  80. struct SetArg(u32, i32);
  81. #[derive(Debug, PartialEq, Eq, Encode, Decode)]
  82. struct GetArg(u32);
  83. let mut runtime = build_solidity(
  84. r##"
  85. contract foo {
  86. int32[type(uint32).max] bigarray;
  87. function set(uint32 index, int32 val) public {
  88. bigarray[index] = val;
  89. }
  90. function get(uint32 index) public returns (int32) {
  91. return bigarray[index];
  92. }
  93. }"##,
  94. );
  95. let mut rng = rand::thread_rng();
  96. let mut vals = Vec::new();
  97. for _ in 0..100 {
  98. let index = rng.gen::<u32>();
  99. let val = rng.gen::<i32>();
  100. runtime.function("set", SetArg(index, val).encode());
  101. vals.push((index, val));
  102. }
  103. for val in vals {
  104. runtime.function("get", GetArg(val.0).encode());
  105. assert_eq!(runtime.output(), Val(val.1).encode());
  106. }
  107. }
  108. #[test]
  109. fn enum_arrays() {
  110. #[derive(Encode, Decode)]
  111. struct Arg([u8; 100]);
  112. #[derive(Debug, PartialEq, Eq, Encode, Decode)]
  113. struct Ret(u32);
  114. let mut runtime = build_solidity(
  115. r##"
  116. contract enum_array {
  117. enum Foo { Bar1, Bar2, Bar3, Bar4 }
  118. function count_bar2(Foo[100] calldata foos) public returns (uint32) {
  119. uint32 count = 0;
  120. uint32 i;
  121. for (i = 0; i < foos.length; i++) {
  122. if (foos[i] == Foo.Bar2) {
  123. count++;
  124. }
  125. }
  126. return count;
  127. }
  128. }"##,
  129. );
  130. let mut rng = rand::thread_rng();
  131. let mut a = [0u8; 100];
  132. let mut count = 0;
  133. #[allow(clippy::needless_range_loop)]
  134. for i in 0..a.len() {
  135. a[i] = rng.gen::<u8>() % 4;
  136. if a[i] == 1 {
  137. count += 1;
  138. }
  139. }
  140. runtime.function("count_bar2", Arg(a).encode());
  141. assert_eq!(runtime.output(), Ret(count).encode());
  142. }
  143. #[test]
  144. fn storage_ref_arg() {
  145. let mut runtime = build_solidity(
  146. r##"
  147. contract storage_refs {
  148. int32[10] a;
  149. int32[10] b;
  150. function set(int32[10] storage array, uint8 index, int32 val) private {
  151. array[index] = val;
  152. }
  153. function test() public {
  154. set(a, 2, 5);
  155. set(b, 2, 102);
  156. assert(a[2] == 5);
  157. assert(b[2] == 102);
  158. }
  159. }"##,
  160. );
  161. runtime.function("test", Vec::new());
  162. }
  163. #[test]
  164. fn storage_ref_var() {
  165. let mut runtime = build_solidity(
  166. r##"
  167. contract storage_refs {
  168. int32[10] a;
  169. int32[10] b;
  170. function set(int32[10] storage array, uint8 index, int32 val) private {
  171. array[index] = val;
  172. }
  173. function test() public {
  174. int32[10] storage ref = a;
  175. set(ref, 2, 5);
  176. ref = b;
  177. set(ref, 2, 102);
  178. assert(a[2] == 5);
  179. assert(b[2] == 102);
  180. }
  181. }"##,
  182. );
  183. runtime.function("test", Vec::new());
  184. }
  185. #[test]
  186. fn storage_ref_returns() {
  187. let mut runtime = build_solidity(
  188. r##"
  189. contract storage_refs {
  190. int32[10] a;
  191. int32[10] b;
  192. function a_or_b(bool want_a) private returns (int32[10] storage) {
  193. if (want_a) {
  194. return a;
  195. }
  196. return b;
  197. }
  198. function set(int32[10] storage array, uint8 index, int32 val) private {
  199. array[index] = val;
  200. }
  201. function test() public {
  202. int32[10] storage ref = a_or_b(true);
  203. set(ref, 2, 5);
  204. ref = a_or_b(false);
  205. set(ref, 2, 102);
  206. assert(a[2] == 5);
  207. assert(b[2] == 102);
  208. }
  209. }"##,
  210. );
  211. runtime.function("test", Vec::new());
  212. }
  213. #[test]
  214. fn storage_to_memory() {
  215. #[derive(Debug, PartialEq, Eq, Encode, Decode)]
  216. struct Ret([u32; 10]);
  217. let mut runtime = build_solidity(
  218. r##"
  219. contract storage_refs {
  220. uint32[10] a;
  221. function test() public returns (uint32[10]) {
  222. for (uint32 i = 0; i < 10; ) {
  223. uint32 index = i;
  224. a[index] = 7 * ++i;
  225. }
  226. return a;
  227. }
  228. }"##,
  229. );
  230. runtime.function("test", Vec::new());
  231. let val = Ret([7, 14, 21, 28, 35, 42, 49, 56, 63, 70]);
  232. assert_eq!(runtime.output(), val.encode());
  233. }
  234. #[test]
  235. fn memory_to_storage() {
  236. #[derive(Debug, PartialEq, Eq, Encode, Decode)]
  237. struct Ret([u32; 10]);
  238. let mut runtime = build_solidity(
  239. r##"
  240. contract storage_refs {
  241. int32[10] a;
  242. function test() public returns (int32[10]) {
  243. int32[10] b = [ int32(7), 14, 21, 28, 35, 42, 49, 56, 63, 70 ];
  244. a = b;
  245. return a;
  246. }
  247. }"##,
  248. );
  249. runtime.function("test", Vec::new());
  250. let val = Ret([7, 14, 21, 28, 35, 42, 49, 56, 63, 70]);
  251. assert_eq!(runtime.output(), val.encode());
  252. }
  253. #[test]
  254. fn array_dimensions() {
  255. let mut runtime = build_solidity(
  256. r##"
  257. contract storage_refs {
  258. int32[32] a;
  259. function test() public {
  260. assert(a.length == 32);
  261. }
  262. }"##,
  263. );
  264. runtime.function("test", Vec::new());
  265. }
  266. #[test]
  267. fn array_in_struct() {
  268. #[derive(Debug, PartialEq, Eq, Encode, Decode)]
  269. struct Ret([u32; 10]);
  270. let mut runtime = build_solidity(
  271. r##"
  272. contract storage_refs {
  273. struct foo {
  274. int32[10] f1;
  275. }
  276. function test() public returns (int32[10]) {
  277. foo a = foo({f1: [ int32(7), 14, 21, 28, 35, 42, 49, 56, 63, 0 ]});
  278. assert(a.f1[1] == 14);
  279. a.f1[9] = 70;
  280. return a.f1;
  281. }
  282. }"##,
  283. );
  284. runtime.function("test", Vec::new());
  285. let val = Ret([7, 14, 21, 28, 35, 42, 49, 56, 63, 70]);
  286. assert_eq!(runtime.output(), val.encode());
  287. }
  288. #[test]
  289. fn struct_in_array() {
  290. #[derive(Debug, PartialEq, Eq, Encode, Decode)]
  291. struct S(u64, bool);
  292. let mut runtime = build_solidity(
  293. r##"
  294. struct S {
  295. uint64 f1;
  296. bool f2;
  297. }
  298. contract foo {
  299. S[] store;
  300. function set(S[] memory n) public {
  301. store = n;
  302. }
  303. function copy() public returns (S[] memory) {
  304. return store;
  305. }
  306. }"##,
  307. );
  308. let val = vec![S(102, true), S(u64::MAX, false)];
  309. runtime.function("set", val.encode());
  310. runtime.function("copy", vec![]);
  311. assert_eq!(runtime.output(), val.encode());
  312. }
  313. #[test]
  314. fn struct_in_fixed_array() {
  315. #[derive(Debug, PartialEq, Eq, Encode, Decode)]
  316. struct S(u64, bool);
  317. let mut runtime = build_solidity(
  318. r##"
  319. struct S {
  320. uint64 f1;
  321. bool f2;
  322. }
  323. contract foo {
  324. S[2] store;
  325. function set(S[2] memory n) public {
  326. store = n;
  327. }
  328. function copy() public returns (S[2] memory) {
  329. return store;
  330. }
  331. }"##,
  332. );
  333. let val = [S(102, true), S(u64::MAX, false)];
  334. runtime.function("set", val.encode());
  335. runtime.function("copy", vec![]);
  336. assert_eq!(runtime.output(), val.encode());
  337. }
  338. #[test]
  339. fn struct_array_struct() {
  340. let mut runtime = build_solidity(
  341. r##"
  342. contract flipper {
  343. struct bool_struct {
  344. bool foo_bool;
  345. }
  346. struct struct_bool_struct_array {
  347. bool_struct[1] foo_struct_array;
  348. }
  349. function get_memory() public pure returns (bool) {
  350. bool_struct memory foo = bool_struct({foo_bool: true});
  351. bool_struct[1] memory foo_array = [foo];
  352. struct_bool_struct_array memory foo_struct = struct_bool_struct_array({foo_struct_array: foo_array});
  353. /* return foo_array[0].foo_bool; */
  354. return foo_struct.foo_struct_array[0].foo_bool;
  355. }
  356. }"##,
  357. );
  358. runtime.function("get_memory", Vec::new());
  359. assert_eq!(runtime.output(), true.encode());
  360. }
  361. #[test]
  362. fn struct_array_struct_abi() {
  363. #[derive(Debug, PartialEq, Eq, Encode, Decode)]
  364. struct Foo {
  365. f1: u32,
  366. f2: bool,
  367. }
  368. #[derive(Debug, PartialEq, Eq, Encode, Decode)]
  369. struct Bar {
  370. bars: [Foo; 10],
  371. }
  372. let mut runtime = build_solidity(
  373. r##"
  374. contract flipper {
  375. struct foo {
  376. int32 f1;
  377. bool f2;
  378. }
  379. struct bar {
  380. foo[10] bars;
  381. }
  382. bar s;
  383. function get_bar() public returns (bar) {
  384. bar memory a = bar({ bars: [
  385. foo({ f1: 1, f2: true}),
  386. foo({ f1: 2, f2: true}),
  387. foo({ f1: 3, f2: true}),
  388. foo({ f1: 4, f2: true}),
  389. foo({ f1: 5, f2: true}),
  390. foo({ f1: 6, f2: true}),
  391. foo({ f1: 7, f2: false}),
  392. foo({ f1: 8, f2: true}),
  393. foo({ f1: 9, f2: true}),
  394. foo({ f1: 10, f2: true})
  395. ]});
  396. s = a;
  397. return a;
  398. }
  399. function set_bar(bar memory a) public {
  400. for (uint32 i = 0; i < 10; i++) {
  401. assert(a.bars[i].f1 == i + 1);
  402. assert(a.bars[i].f2 == (i != 6));
  403. }
  404. a = s;
  405. }
  406. }"##,
  407. );
  408. let b = Bar {
  409. bars: [
  410. Foo { f1: 1, f2: true },
  411. Foo { f1: 2, f2: true },
  412. Foo { f1: 3, f2: true },
  413. Foo { f1: 4, f2: true },
  414. Foo { f1: 5, f2: true },
  415. Foo { f1: 6, f2: true },
  416. Foo { f1: 7, f2: false },
  417. Foo { f1: 8, f2: true },
  418. Foo { f1: 9, f2: true },
  419. Foo { f1: 10, f2: true },
  420. ],
  421. };
  422. runtime.function("get_bar", Vec::new());
  423. assert_eq!(runtime.output(), b.encode());
  424. runtime.function("set_bar", b.encode());
  425. }
  426. #[test]
  427. fn memory_dynamic_array_new() {
  428. let mut runtime = build_solidity(
  429. r#"
  430. contract foo {
  431. function test() public {
  432. int32[] memory a = new int32[](5);
  433. assert(a.length == 5);
  434. }
  435. }"#,
  436. );
  437. runtime.function("test", Vec::new());
  438. // The Ethereum Foundation solc allows you to create arrays of length 0
  439. let mut runtime = build_solidity(
  440. r#"
  441. contract foo {
  442. function test() public {
  443. bool[] memory a = new bool[](0);
  444. assert(a.length == 0);
  445. }
  446. }"#,
  447. );
  448. runtime.function("test", Vec::new());
  449. }
  450. #[test]
  451. fn memory_dynamic_array_deref() {
  452. // The Ethereum Foundation solc allows you to create arrays of length 0
  453. let mut runtime = build_solidity(
  454. r#"
  455. contract foo {
  456. function test() public {
  457. int32[] memory a = new int32[](5);
  458. assert(a.length == 5);
  459. a[0] = 102;
  460. a[1] = -5;
  461. a[4] = 0x7cafeeed;
  462. assert(a[0] == 102);
  463. assert(a[1] == -5);
  464. assert(a[4] == 0x7cafeeed);
  465. }
  466. }"#,
  467. );
  468. runtime.function("test", Vec::new());
  469. }
  470. #[test]
  471. fn array_bounds_dynamic_array() {
  472. let mut runtime = build_solidity(
  473. r#"
  474. contract foo {
  475. function test() public returns (int32) {
  476. int32[] memory a = new int32[](5);
  477. a[5] = 102;
  478. return a[3];
  479. }
  480. }"#,
  481. );
  482. runtime.function_expect_failure("test", Vec::new());
  483. }
  484. #[test]
  485. fn empty_array_bounds_dynamic_array() {
  486. let mut runtime = build_solidity(
  487. r#"
  488. contract foo {
  489. function test() public returns (bytes32) {
  490. bytes32[] memory a = new bytes32[](0);
  491. a[0] = "yo";
  492. return a[0];
  493. }
  494. }"#,
  495. );
  496. runtime.function_expect_failure("test", Vec::new());
  497. }
  498. #[test]
  499. fn memory_dynamic_array_types_call_return() {
  500. let mut runtime = build_solidity(
  501. r#"
  502. contract foo {
  503. function a(bool cond) public returns (bytes27[]) {
  504. bytes27[] foo;
  505. foo = new bytes27[](5);
  506. foo[1] = "cond was true";
  507. return foo;
  508. }
  509. function b(bytes27[] x) private {
  510. x[1] = "b was called";
  511. x = new bytes27[](3);
  512. x[1] = "should not be";
  513. }
  514. function test() public {
  515. bytes27[] x = a(true);
  516. assert(x.length == 5);
  517. assert(x[1] == "cond was true");
  518. b(x);
  519. assert(x.length == 5);
  520. assert(x[1] == "b was called");
  521. }
  522. }"#,
  523. );
  524. runtime.function("test", Vec::new());
  525. }
  526. #[test]
  527. fn dynamic_arrays_need_phi_node() {
  528. let mut runtime = build_solidity(
  529. r#"
  530. pragma solidity 0;
  531. contract foo {
  532. enum bar { bar1, bar2, bar3, bar4 }
  533. function a(bool cond) public returns (bar[] memory) {
  534. bar[] memory foo;
  535. if (cond) {
  536. foo = new bar[](5);
  537. foo[1] = bar.bar2;
  538. } else {
  539. foo = new bar[](3);
  540. foo[1] = bar.bar3;
  541. }
  542. return foo;
  543. }
  544. function test() public {
  545. bar[] memory x = a(true);
  546. assert(x.length == 5);
  547. assert(x[1] == bar.bar2);
  548. x = a(false);
  549. assert(x.length == 3);
  550. assert(x[1] == bar.bar3);
  551. }
  552. }"#,
  553. );
  554. runtime.function("test", Vec::new());
  555. }
  556. // test:
  557. // alignment of array elements
  558. // arrays of other structs/arrays/darrays
  559. // nil pointer should fail
  560. // copy to/from storage <=> memory
  561. // abi encode/decode
  562. // string/bytes
  563. #[test]
  564. fn storage_dynamic_array_length() {
  565. let mut runtime = build_solidity(
  566. r#"
  567. pragma solidity 0;
  568. contract foo {
  569. int32[] bar;
  570. function test() public {
  571. assert(bar.length == 0);
  572. }
  573. }"#,
  574. );
  575. runtime.function("test", Vec::new());
  576. }
  577. #[test]
  578. fn dynamic_array_push() {
  579. let mut runtime = build_solidity(
  580. r#"
  581. pragma solidity 0;
  582. contract foo {
  583. function test() public {
  584. int[] bar = (new int[])(1);
  585. bar[0] = 128;
  586. bar.push(64);
  587. assert(bar.length == 2);
  588. assert(bar[1] == 64);
  589. }
  590. }
  591. "#,
  592. );
  593. runtime.function("test", Vec::new());
  594. let mut runtime = build_solidity(
  595. r#"
  596. pragma solidity 0;
  597. contract foo {
  598. function test() public {
  599. bytes bar = (new bytes)(1);
  600. bar[0] = 128;
  601. bar.push(64);
  602. assert(bar.length == 2);
  603. assert(bar[1] == 64);
  604. }
  605. }
  606. "#,
  607. );
  608. runtime.function("test", Vec::new());
  609. let mut runtime = build_solidity(
  610. r#"
  611. pragma solidity 0;
  612. contract foo {
  613. struct s {
  614. int32 f1;
  615. bool f2;
  616. }
  617. function test() public {
  618. s[] bar = new s[](1);
  619. bar[0] = s({f1: 0, f2: false});
  620. bar.push(s({f1: 1, f2: true}));
  621. assert(bar.length == 2);
  622. assert(bar[1].f1 == 1);
  623. assert(bar[1].f2 == true);
  624. }
  625. }
  626. "#,
  627. );
  628. runtime.function("test", Vec::new());
  629. let mut runtime = build_solidity(
  630. r#"
  631. pragma solidity 0;
  632. contract foo {
  633. enum enum1 { val1, val2, val3 }
  634. function test() public {
  635. enum1[] bar = new enum1[](1);
  636. bar[0] = enum1.val1;
  637. bar.push(enum1.val2);
  638. assert(bar.length == 2);
  639. assert(bar[1] == enum1.val2);
  640. }
  641. }
  642. "#,
  643. );
  644. runtime.function("test", Vec::new());
  645. // push() returns a reference to the thing
  646. let mut runtime = build_solidity(
  647. r#"
  648. pragma solidity 0;
  649. contract foo {
  650. struct s {
  651. int32 f1;
  652. bool f2;
  653. }
  654. function test() public {
  655. s[] bar = new s[](0);
  656. s memory n = bar.push();
  657. n.f1 = 102;
  658. n.f2 = true;
  659. assert(bar[0].f1 == 102);
  660. assert(bar[0].f2 == true);
  661. }
  662. }"#,
  663. );
  664. runtime.function("test", Vec::new());
  665. }
  666. #[test]
  667. fn dynamic_array_pop() {
  668. let mut runtime = build_solidity(
  669. r#"
  670. pragma solidity 0;
  671. contract foo {
  672. function test() public {
  673. int[] bar = new int[](1);
  674. bar[0] = 128;
  675. assert(bar.length == 1);
  676. assert(128 == bar.pop());
  677. assert(bar.length == 0);
  678. }
  679. }
  680. "#,
  681. );
  682. runtime.function("test", Vec::new());
  683. let mut runtime = build_solidity(
  684. r#"
  685. pragma solidity 0;
  686. contract foo {
  687. function test() public {
  688. bytes bar = new bytes(1);
  689. bar[0] = 128;
  690. assert(bar.length == 1);
  691. assert(128 == bar.pop());
  692. assert(bar.length == 0);
  693. }
  694. }
  695. "#,
  696. );
  697. runtime.function("test", Vec::new());
  698. let mut runtime = build_solidity(
  699. r#"
  700. pragma solidity 0;
  701. contract foo {
  702. struct s {
  703. int32 f1;
  704. bool f2;
  705. }
  706. function test() public {
  707. s[] bar = new s[](1);
  708. bar[0] = s(128, true);
  709. assert(bar.length == 1);
  710. s baz = bar.pop();
  711. assert(baz.f1 == 128);
  712. assert(baz.f2 == true);
  713. assert(bar.length == 0);
  714. }
  715. }
  716. "#,
  717. );
  718. runtime.function("test", Vec::new());
  719. let mut runtime = build_solidity(
  720. r#"
  721. pragma solidity 0;
  722. contract foo {
  723. enum enum1 { val1, val2, val3 }
  724. function test() public {
  725. enum1[] bar = new enum1[](1);
  726. bar[0] = enum1.val2;
  727. assert(bar.length == 1);
  728. assert(enum1.val2 == bar.pop());
  729. assert(bar.length == 0);
  730. }
  731. }
  732. "#,
  733. );
  734. runtime.function("test", Vec::new());
  735. }
  736. #[test]
  737. fn dynamic_array_pop_empty_array() {
  738. let mut runtime = build_solidity(
  739. r#"
  740. pragma solidity 0;
  741. contract foo {
  742. function test() public returns (int) {
  743. int[] bar = new int[](0);
  744. return bar.pop();
  745. }
  746. }"#,
  747. );
  748. runtime.function_expect_failure("test", Vec::new());
  749. }
  750. #[test]
  751. fn dynamic_array_pop_bounds() {
  752. let mut runtime = build_solidity(
  753. r#"
  754. pragma solidity 0;
  755. contract foo {
  756. function test() public {
  757. int[] bar = new int[](1);
  758. bar[0] = 12;
  759. bar.pop();
  760. assert(bar[0] == 12);
  761. }
  762. }"#,
  763. );
  764. runtime.function_expect_failure("test", Vec::new());
  765. }
  766. #[test]
  767. fn storage_dynamic_array_push() {
  768. let mut runtime = build_solidity(
  769. r#"
  770. pragma solidity 0;
  771. contract foo {
  772. int32[] bar;
  773. function test() public {
  774. assert(bar.length == 0);
  775. bar.push(102);
  776. assert(bar[0] == 102);
  777. assert(bar.length == 1);
  778. bar.push();
  779. assert(bar[1] == 0);
  780. assert(bar.length == 2);
  781. }
  782. }"#,
  783. );
  784. runtime.function("test", Vec::new());
  785. // push() returns a reference to the thing
  786. let mut runtime = build_solidity(
  787. r#"
  788. pragma solidity 0;
  789. contract foo {
  790. struct s {
  791. int32 f1;
  792. bool f2;
  793. }
  794. s[] bar;
  795. function test() public {
  796. s storage n = bar.push();
  797. n.f1 = 102;
  798. n.f2 = true;
  799. assert(bar[0].f1 == 102);
  800. }
  801. }"#,
  802. );
  803. runtime.function("test", Vec::new());
  804. }
  805. #[test]
  806. fn storage_dynamic_array_pop() {
  807. let mut runtime = build_solidity(
  808. r#"
  809. pragma solidity 0;
  810. contract foo {
  811. int32[] bar;
  812. function test() public {
  813. assert(bar.length == 0);
  814. bar.push(102);
  815. assert(bar[0] == 102);
  816. assert(bar.length == 1);
  817. int32 v = bar.pop();
  818. assert(bar.length == 0);
  819. assert(v == 102);
  820. }
  821. }"#,
  822. );
  823. runtime.function("test", Vec::new());
  824. // We should have one entry for the length; pop should have removed the 102 entry
  825. assert_eq!(runtime.storage().len(), 1);
  826. // ensure that structs and fixed arrays are wiped by pop
  827. let mut runtime = build_solidity(
  828. r#"
  829. pragma solidity 0;
  830. contract foo {
  831. enum enum1 { val1, val2, val3 }
  832. struct s {
  833. bool f1;
  834. bytes3 f2;
  835. enum1 f3;
  836. uint64 f4;
  837. int64[2] f5;
  838. }
  839. s[] bar;
  840. function test() public {
  841. s storage first = bar.push();
  842. first.f1 = true;
  843. first.f2 = "abc";
  844. first.f3 = enum1.val2;
  845. first.f4 = 65536;
  846. first.f5[0] = -1;
  847. first.f5[1] = 5;
  848. assert(bar[0].f5[1] == 5);
  849. // now erase it again
  850. bar.pop();
  851. }
  852. }"#,
  853. );
  854. runtime.function("test", Vec::new());
  855. // We should have one entry for the length; pop should have removed the 102 entry
  856. assert_eq!(runtime.storage().len(), 1);
  857. }
  858. #[test]
  859. fn storage_delete() {
  860. // ensure that structs and fixed arrays are wiped by pop
  861. let mut runtime = build_solidity(
  862. r#"
  863. pragma solidity 0;
  864. contract foo {
  865. uint64 bar;
  866. function test() public {
  867. bar = 0xdeaddeaddeaddead;
  868. delete bar;
  869. }
  870. }"#,
  871. );
  872. runtime.function("test", Vec::new());
  873. // We should have one entry for the length; pop should have removed the 102 entry
  874. assert!(runtime.storage().is_empty());
  875. // ensure that structs and fixed arrays are wiped by delete
  876. let mut runtime = build_solidity(
  877. r#"
  878. pragma solidity 0;
  879. contract foo {
  880. enum enum1 { val1, val2, val3 }
  881. struct s {
  882. bool f1;
  883. bytes3 f2;
  884. enum1 f3;
  885. uint64 f4;
  886. int64[2] f5;
  887. }
  888. s[] bar;
  889. function test() public {
  890. s storage first = bar.push();
  891. first.f1 = true;
  892. first.f2 = "abc";
  893. first.f3 = enum1.val2;
  894. first.f4 = 65536;
  895. first.f5[0] = -1;
  896. first.f5[1] = 5;
  897. assert(bar[0].f5[1] == 5);
  898. // now erase it again
  899. delete bar[0];
  900. }
  901. }"#,
  902. );
  903. runtime.function("test", Vec::new());
  904. // We should have one entry for the length; delete should have removed the entry
  905. assert_eq!(runtime.storage().len(), 1);
  906. // ensure that structs and fixed arrays are wiped by delete
  907. let mut runtime = build_solidity(
  908. r#"
  909. pragma solidity 0;
  910. contract foo {
  911. int[] bar;
  912. function setup() public {
  913. bar.push(102);
  914. bar.push(305);
  915. }
  916. function clear() public {
  917. delete bar;
  918. }
  919. }"#,
  920. );
  921. runtime.function("setup", Vec::new());
  922. assert_eq!(runtime.storage().len(), 3);
  923. runtime.function("clear", Vec::new());
  924. assert_eq!(runtime.storage().len(), 0);
  925. // our delete operator has to iterate over the dynamic array. Ensure it works if the array is empty
  926. runtime.function("clear", Vec::new());
  927. assert_eq!(runtime.storage().len(), 0);
  928. }
  929. #[test]
  930. fn storage_dynamic_copy() {
  931. let mut runtime = build_solidity(
  932. r#"
  933. contract c {
  934. int32[] foo;
  935. constructor() public {
  936. for (int32 i = 0; i <11; i++) {
  937. foo.push(i * 3);
  938. }
  939. }
  940. function storage_to_memory() view public {
  941. int32[] memory bar = foo;
  942. assert(bar.length == 11);
  943. for (int32 i = 0; i <11; i++) {
  944. assert(bar[uint32(i)] == i * 3);
  945. }
  946. }
  947. function memory_to_storage() public {
  948. int32[] memory bar = new int32[](5);
  949. for (int32 i = 0; i < 5; i++) {
  950. bar[uint32(i)] = 5 * (i + 7);
  951. }
  952. foo = bar;
  953. assert(foo.length == 5);
  954. for (int32 i = 0; i < 5; i++) {
  955. assert(foo[uint32(i)] == 5 * (i + 7));
  956. }
  957. }
  958. }
  959. "#,
  960. );
  961. runtime.constructor(0, Vec::new());
  962. runtime.function("storage_to_memory", Vec::new());
  963. runtime.function("memory_to_storage", Vec::new());
  964. assert_eq!(runtime.storage().len(), 6);
  965. }
  966. #[test]
  967. fn abi_encode_dynamic_array() {
  968. #[derive(Debug, PartialEq, Eq, Encode, Decode)]
  969. struct Int32Array(Vec<i32>);
  970. let mut runtime = build_solidity(
  971. r#"
  972. contract c {
  973. function encode() pure public returns (int32[]) {
  974. int32[] memory bar = new int32[](11);
  975. for (int32 i = 0; i <11; i++) {
  976. bar[uint32(i)] = i * 3;
  977. }
  978. return bar;
  979. }
  980. }
  981. "#,
  982. );
  983. runtime.constructor(0, Vec::new());
  984. runtime.function("encode", Vec::new());
  985. assert_eq!(
  986. runtime.output(),
  987. Int32Array(vec!(0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30)).encode()
  988. );
  989. }
  990. #[test]
  991. fn abi_decode_dynamic_array() {
  992. #[derive(Debug, PartialEq, Eq, Encode, Decode)]
  993. struct Int32Array(Vec<i32>);
  994. let mut runtime = build_solidity(
  995. r#"
  996. contract c {
  997. function decode(int32[] bar) pure public {
  998. assert(bar.length == 11);
  999. for (int32 i = 0; i <11; i++) {
  1000. assert(bar[uint32(i)] == i * 3);
  1001. }
  1002. }
  1003. function decode_empty(int32[] bar) pure public {
  1004. assert(bar.length == 0);
  1005. }
  1006. }
  1007. "#,
  1008. );
  1009. runtime.constructor(0, Vec::new());
  1010. runtime.function(
  1011. "decode",
  1012. Int32Array(vec![0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30]).encode(),
  1013. );
  1014. runtime.function("decode_empty", Int32Array(vec![]).encode());
  1015. }
  1016. #[test]
  1017. fn abi_encode_dynamic_array2() {
  1018. #[derive(Debug, PartialEq, Eq, Encode, Decode)]
  1019. struct Array(Vec<(bool, u32)>);
  1020. let mut runtime = build_solidity(
  1021. r#"
  1022. contract structs {
  1023. struct foo {
  1024. bool x;
  1025. uint32 y;
  1026. }
  1027. function test() public returns (foo[]) {
  1028. foo[] x = new foo[](3);
  1029. x[0] = foo({x: true, y: 64});
  1030. x[1] = foo({x: false, y: 102});
  1031. x[2] = foo({x: true, y: 0x800});
  1032. return x;
  1033. }
  1034. }
  1035. "#,
  1036. );
  1037. runtime.constructor(0, Vec::new());
  1038. runtime.function("test", Vec::new());
  1039. assert_eq!(
  1040. runtime.output(),
  1041. Array(vec!((true, 64), (false, 102), (true, 0x800))).encode()
  1042. );
  1043. }
  1044. #[test]
  1045. fn abi_encode_dynamic_array3() {
  1046. #[derive(Debug, PartialEq, Eq, Encode, Decode)]
  1047. struct Array(Vec<(bool, u32, String)>);
  1048. let mut runtime = build_solidity(
  1049. r#"
  1050. contract structs {
  1051. struct foo {
  1052. bool x;
  1053. uint32 y;
  1054. string z;
  1055. }
  1056. function test() public returns (foo[]) {
  1057. foo[] x = new foo[](3);
  1058. x[0] = foo({x: true, y: 64, z: "abc"});
  1059. x[1] = foo({x: false, y: 102, z: "a"});
  1060. x[2] = foo({x: true, y: 0x800, z: "abcdef"});
  1061. return x;
  1062. }
  1063. }
  1064. "#,
  1065. );
  1066. runtime.constructor(0, Vec::new());
  1067. runtime.function("test", Vec::new());
  1068. assert_eq!(
  1069. runtime.output(),
  1070. Array(vec!(
  1071. (true, 64, "abc".to_owned()),
  1072. (false, 102, "a".to_owned()),
  1073. (true, 0x800, "abcdef".to_owned())
  1074. ))
  1075. .encode()
  1076. );
  1077. }
  1078. #[test]
  1079. fn abi_encode_dynamic_array4() {
  1080. #[derive(Debug, PartialEq, Eq, Encode, Decode)]
  1081. struct Array([(bool, u32, String); 3]);
  1082. let mut runtime = build_solidity(
  1083. r#"
  1084. contract structs {
  1085. struct foo {
  1086. bool x;
  1087. uint32 y;
  1088. string z;
  1089. }
  1090. function test() public returns (foo[3]) {
  1091. foo[3] x;
  1092. x[0] = foo({x: true, y: 64, z: "abc"});
  1093. x[1] = foo({x: false, y: 102, z: "a"});
  1094. x[2] = foo({x: true, y: 0x800, z: "abcdef"});
  1095. return x;
  1096. }
  1097. }
  1098. "#,
  1099. );
  1100. runtime.constructor(0, Vec::new());
  1101. runtime.function("test", Vec::new());
  1102. runtime.heap_verify();
  1103. assert_eq!(
  1104. runtime.output(),
  1105. Array([
  1106. (true, 64, "abc".to_owned()),
  1107. (false, 102, "a".to_owned()),
  1108. (true, 0x800, "abcdef".to_owned())
  1109. ])
  1110. .encode()
  1111. );
  1112. }
  1113. #[test]
  1114. fn large_index_ty_in_bounds() {
  1115. let mut runtime = build_solidity(
  1116. r#"
  1117. contract foo {
  1118. uint128 storage_index;
  1119. function test(uint128 index) public returns (uint16) {
  1120. uint16[] memory a = new uint16[](16);
  1121. storage_index = index;
  1122. return a[storage_index];
  1123. }
  1124. }"#,
  1125. );
  1126. runtime.constructor(0, Vec::new());
  1127. runtime.function("test", 15u128.encode());
  1128. runtime.function_expect_failure("test", 17u128.encode());
  1129. runtime.function_expect_failure("test", 0xfffffffffffffu128.encode());
  1130. }
  1131. #[test]
  1132. fn alloc_size_from_storage() {
  1133. let mut runtime = build_solidity(
  1134. r#"
  1135. contract Test {
  1136. uint32 length = 1;
  1137. function contfunc() public view returns (uint64[] memory) {
  1138. uint64[] memory values = new uint64[](length);
  1139. return values;
  1140. }
  1141. }"#,
  1142. );
  1143. runtime.constructor(0, Vec::new());
  1144. runtime.function("contfunc", Vec::new());
  1145. assert_eq!(runtime.output(), vec![0u64].encode());
  1146. }
  1147. #[test]
  1148. fn fixed_bytes() {
  1149. let mut runtime = build_solidity(
  1150. r#"
  1151. contract Storage {
  1152. bytes32[] data;
  1153. constructor() {
  1154. data.push(hex"0000000000000000000000000000000000000000000000000000000000000000");
  1155. data.push(hex"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f");
  1156. }
  1157. function uploadData(uint8 i, uint8 j) public view returns(bytes1) {
  1158. return(data[j][i]);
  1159. }
  1160. }
  1161. "#,
  1162. );
  1163. runtime.constructor(0, vec![]);
  1164. for i in 0..32u8 {
  1165. runtime.function("uploadData", vec![i, 0]);
  1166. assert_eq!(runtime.output()[..], [0]);
  1167. runtime.function("uploadData", vec![i, 1]);
  1168. assert_eq!(runtime.output()[..], [i]);
  1169. }
  1170. let mut runtime = build_solidity(
  1171. r#"
  1172. contract Memory {
  1173. constructor() {
  1174. }
  1175. function uploadData(uint8 i, uint8 j) public view returns(bytes1) {
  1176. bytes32[] data = new bytes32[](2);
  1177. data[0] = hex"0000000000000000000000000000000000000000000000000000000000000000";
  1178. data[1] = hex"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f";
  1179. return(data[j][i]);
  1180. }
  1181. }
  1182. "#,
  1183. );
  1184. runtime.constructor(0, vec![]);
  1185. for i in 0..32u8 {
  1186. runtime.function("uploadData", vec![i, 0]);
  1187. assert_eq!(runtime.output()[..], [0]);
  1188. runtime.function("uploadData", vec![i, 1]);
  1189. assert_eq!(runtime.output()[..], [i]);
  1190. }
  1191. }
  1192. #[test]
  1193. fn abi_decode_dynamic_array2() {
  1194. let mut runtime = build_solidity(
  1195. r#"
  1196. contract c {
  1197. function decode() pure public {
  1198. bytes enc = hex"2c000000000300000006000000090000000c0000000f0000001200000015000000180000001b0000001e000000";
  1199. int32[] bar = abi.decode(enc, (int32[]));
  1200. assert(bar.length == 11);
  1201. for (int32 i = 0; i <11; i++) {
  1202. assert(bar[uint32(i)] == i * 3);
  1203. }
  1204. }
  1205. function decode_empty() pure public {
  1206. bytes enc = hex"00";
  1207. int32[] bar = abi.decode(enc, (int32[]));
  1208. assert(bar.length == 0);
  1209. }
  1210. }
  1211. "#,
  1212. );
  1213. runtime.function("decode", vec![]);
  1214. runtime.function("decode_empty", vec![]);
  1215. }
  1216. #[test]
  1217. fn abi_decode_dynamic_array3() {
  1218. let mut runtime = build_solidity(
  1219. r#"
  1220. contract Arr {
  1221. function decode() pure public {
  1222. bytes enc = hex"14140001020304140102030405140203040506140304050607140405060708";
  1223. uint8[][] bar = abi.decode(enc, (uint8[][]));
  1224. assert(bar.length == 5);
  1225. for (uint8 i = 0; i <5; i++) {
  1226. for (uint8 j = 0; j <5; j++) {
  1227. assert(bar[uint32(i)][uint32(j)] == i + j);
  1228. }
  1229. }
  1230. }
  1231. function decode_empty() pure public {
  1232. bytes enc = hex"00";
  1233. uint8[][] bar = abi.decode(enc, (uint8[][]));
  1234. assert(bar.length == 0);
  1235. }
  1236. }
  1237. "#,
  1238. );
  1239. // The array in the first function was generated like this:
  1240. // let mut matrix = vec![];
  1241. // for i in 0..5 {
  1242. // matrix.push(vec![]);
  1243. // for j in 0..5 {
  1244. // matrix[i].push((i + j) as u8);
  1245. // }
  1246. // }
  1247. runtime.function("decode", vec![]);
  1248. runtime.function("decode_empty", vec![]);
  1249. }