arrays.rs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461
  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.vm.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.vm.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.vm.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.vm.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.vm.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.vm.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.vm.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.vm.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.vm.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.vm.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.vm.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.vm.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.vm.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. #[should_panic]
  472. fn array_bounds_dynamic_array() {
  473. let mut runtime = build_solidity(
  474. r#"
  475. contract foo {
  476. function test() public returns (int32) {
  477. int32[] memory a = new int32[](5);
  478. a[5] = 102;
  479. return a[3];
  480. }
  481. }"#,
  482. );
  483. runtime.function("test", Vec::new());
  484. }
  485. #[test]
  486. #[should_panic]
  487. fn empty_array_bounds_dynamic_array() {
  488. let mut runtime = build_solidity(
  489. r#"
  490. contract foo {
  491. function test() public returns (bytes32) {
  492. bytes32[] memory a = new bytes32[](0);
  493. a[0] = "yo";
  494. return a[0];
  495. }
  496. }"#,
  497. );
  498. runtime.function("test", Vec::new());
  499. }
  500. #[test]
  501. fn memory_dynamic_array_types_call_return() {
  502. let mut runtime = build_solidity(
  503. r#"
  504. contract foo {
  505. function a(bool cond) public returns (bytes27[]) {
  506. bytes27[] foo;
  507. foo = new bytes27[](5);
  508. foo[1] = "cond was true";
  509. return foo;
  510. }
  511. function b(bytes27[] x) private {
  512. x[1] = "b was called";
  513. x = new bytes27[](3);
  514. x[1] = "should not be";
  515. }
  516. function test() public {
  517. bytes27[] x = a(true);
  518. assert(x.length == 5);
  519. assert(x[1] == "cond was true");
  520. b(x);
  521. assert(x.length == 5);
  522. assert(x[1] == "b was called");
  523. }
  524. }"#,
  525. );
  526. runtime.function("test", Vec::new());
  527. }
  528. #[test]
  529. fn dynamic_arrays_need_phi_node() {
  530. let mut runtime = build_solidity(
  531. r#"
  532. pragma solidity 0;
  533. contract foo {
  534. enum bar { bar1, bar2, bar3, bar4 }
  535. function a(bool cond) public returns (bar[] memory) {
  536. bar[] memory foo;
  537. if (cond) {
  538. foo = new bar[](5);
  539. foo[1] = bar.bar2;
  540. } else {
  541. foo = new bar[](3);
  542. foo[1] = bar.bar3;
  543. }
  544. return foo;
  545. }
  546. function test() public {
  547. bar[] memory x = a(true);
  548. assert(x.length == 5);
  549. assert(x[1] == bar.bar2);
  550. x = a(false);
  551. assert(x.length == 3);
  552. assert(x[1] == bar.bar3);
  553. }
  554. }"#,
  555. );
  556. runtime.function("test", Vec::new());
  557. }
  558. // test:
  559. // alignment of array elements
  560. // arrays of other structs/arrays/darrays
  561. // nil pointer should fail
  562. // copy to/from storage <=> memory
  563. // abi encode/decode
  564. // string/bytes
  565. #[test]
  566. fn storage_dynamic_array_length() {
  567. let mut runtime = build_solidity(
  568. r#"
  569. pragma solidity 0;
  570. contract foo {
  571. int32[] bar;
  572. function test() public {
  573. assert(bar.length == 0);
  574. }
  575. }"#,
  576. );
  577. runtime.function("test", Vec::new());
  578. }
  579. #[test]
  580. fn dynamic_array_push() {
  581. let mut runtime = build_solidity(
  582. r#"
  583. pragma solidity 0;
  584. contract foo {
  585. function test() public {
  586. int[] bar = (new int[])(1);
  587. bar[0] = 128;
  588. bar.push(64);
  589. assert(bar.length == 2);
  590. assert(bar[1] == 64);
  591. }
  592. }
  593. "#,
  594. );
  595. runtime.function("test", Vec::new());
  596. let mut runtime = build_solidity(
  597. r#"
  598. pragma solidity 0;
  599. contract foo {
  600. function test() public {
  601. bytes bar = (new bytes)(1);
  602. bar[0] = 128;
  603. bar.push(64);
  604. assert(bar.length == 2);
  605. assert(bar[1] == 64);
  606. }
  607. }
  608. "#,
  609. );
  610. runtime.function("test", Vec::new());
  611. let mut runtime = build_solidity(
  612. r#"
  613. pragma solidity 0;
  614. contract foo {
  615. struct s {
  616. int32 f1;
  617. bool f2;
  618. }
  619. function test() public {
  620. s[] bar = new s[](1);
  621. bar[0] = s({f1: 0, f2: false});
  622. bar.push(s({f1: 1, f2: true}));
  623. assert(bar.length == 2);
  624. assert(bar[1].f1 == 1);
  625. assert(bar[1].f2 == true);
  626. }
  627. }
  628. "#,
  629. );
  630. runtime.function("test", Vec::new());
  631. let mut runtime = build_solidity(
  632. r#"
  633. pragma solidity 0;
  634. contract foo {
  635. enum enum1 { val1, val2, val3 }
  636. function test() public {
  637. enum1[] bar = new enum1[](1);
  638. bar[0] = enum1.val1;
  639. bar.push(enum1.val2);
  640. assert(bar.length == 2);
  641. assert(bar[1] == enum1.val2);
  642. }
  643. }
  644. "#,
  645. );
  646. runtime.function("test", Vec::new());
  647. // push() returns a reference to the thing
  648. let mut runtime = build_solidity(
  649. r#"
  650. pragma solidity 0;
  651. contract foo {
  652. struct s {
  653. int32 f1;
  654. bool f2;
  655. }
  656. function test() public {
  657. s[] bar = new s[](0);
  658. s memory n = bar.push();
  659. n.f1 = 102;
  660. n.f2 = true;
  661. assert(bar[0].f1 == 102);
  662. assert(bar[0].f2 == true);
  663. }
  664. }"#,
  665. );
  666. runtime.function("test", Vec::new());
  667. }
  668. #[test]
  669. fn dynamic_array_pop() {
  670. let mut runtime = build_solidity(
  671. r#"
  672. pragma solidity 0;
  673. contract foo {
  674. function test() public {
  675. int[] bar = new int[](1);
  676. bar[0] = 128;
  677. assert(bar.length == 1);
  678. assert(128 == bar.pop());
  679. assert(bar.length == 0);
  680. }
  681. }
  682. "#,
  683. );
  684. runtime.function("test", Vec::new());
  685. let mut runtime = build_solidity(
  686. r#"
  687. pragma solidity 0;
  688. contract foo {
  689. function test() public {
  690. bytes bar = new bytes(1);
  691. bar[0] = 128;
  692. assert(bar.length == 1);
  693. assert(128 == bar.pop());
  694. assert(bar.length == 0);
  695. }
  696. }
  697. "#,
  698. );
  699. runtime.function("test", Vec::new());
  700. let mut runtime = build_solidity(
  701. r#"
  702. pragma solidity 0;
  703. contract foo {
  704. struct s {
  705. int32 f1;
  706. bool f2;
  707. }
  708. function test() public {
  709. s[] bar = new s[](1);
  710. bar[0] = s(128, true);
  711. assert(bar.length == 1);
  712. s baz = bar.pop();
  713. assert(baz.f1 == 128);
  714. assert(baz.f2 == true);
  715. assert(bar.length == 0);
  716. }
  717. }
  718. "#,
  719. );
  720. runtime.function("test", Vec::new());
  721. let mut runtime = build_solidity(
  722. r#"
  723. pragma solidity 0;
  724. contract foo {
  725. enum enum1 { val1, val2, val3 }
  726. function test() public {
  727. enum1[] bar = new enum1[](1);
  728. bar[0] = enum1.val2;
  729. assert(bar.length == 1);
  730. assert(enum1.val2 == bar.pop());
  731. assert(bar.length == 0);
  732. }
  733. }
  734. "#,
  735. );
  736. runtime.function("test", Vec::new());
  737. }
  738. #[test]
  739. #[should_panic]
  740. fn dynamic_array_pop_empty_array() {
  741. let mut runtime = build_solidity(
  742. r#"
  743. pragma solidity 0;
  744. contract foo {
  745. function test() public {
  746. int[] bar = new int[](0);
  747. bar.pop();
  748. }
  749. }"#,
  750. );
  751. runtime.function("test", Vec::new());
  752. }
  753. #[test]
  754. #[should_panic]
  755. fn dynamic_array_pop_bounds() {
  756. let mut runtime = build_solidity(
  757. r#"
  758. pragma solidity 0;
  759. contract foo {
  760. function test() public {
  761. int[] bar = new int[](1);
  762. bar[0] = 12;
  763. bar.pop();
  764. assert(bar[0] == 12);
  765. }
  766. }"#,
  767. );
  768. runtime.function("test", Vec::new());
  769. }
  770. #[test]
  771. fn storage_dynamic_array_push() {
  772. let mut runtime = build_solidity(
  773. r#"
  774. pragma solidity 0;
  775. contract foo {
  776. int32[] bar;
  777. function test() public {
  778. assert(bar.length == 0);
  779. bar.push(102);
  780. assert(bar[0] == 102);
  781. assert(bar.length == 1);
  782. bar.push();
  783. assert(bar[1] == 0);
  784. assert(bar.length == 2);
  785. }
  786. }"#,
  787. );
  788. runtime.function("test", Vec::new());
  789. // push() returns a reference to the thing
  790. let mut runtime = build_solidity(
  791. r#"
  792. pragma solidity 0;
  793. contract foo {
  794. struct s {
  795. int32 f1;
  796. bool f2;
  797. }
  798. s[] bar;
  799. function test() public {
  800. s storage n = bar.push();
  801. n.f1 = 102;
  802. n.f2 = true;
  803. assert(bar[0].f1 == 102);
  804. }
  805. }"#,
  806. );
  807. runtime.function("test", Vec::new());
  808. }
  809. #[test]
  810. fn storage_dynamic_array_pop() {
  811. let mut runtime = build_solidity(
  812. r#"
  813. pragma solidity 0;
  814. contract foo {
  815. int32[] bar;
  816. function test() public {
  817. assert(bar.length == 0);
  818. bar.push(102);
  819. assert(bar[0] == 102);
  820. assert(bar.length == 1);
  821. int32 v = bar.pop();
  822. assert(bar.length == 0);
  823. assert(v == 102);
  824. }
  825. }"#,
  826. );
  827. runtime.function("test", Vec::new());
  828. // We should have one entry for the length; pop should have removed the 102 entry
  829. assert_eq!(runtime.store.len(), 1);
  830. // ensure that structs and fixed arrays are wiped by pop
  831. let mut runtime = build_solidity(
  832. r#"
  833. pragma solidity 0;
  834. contract foo {
  835. enum enum1 { val1, val2, val3 }
  836. struct s {
  837. bool f1;
  838. bytes3 f2;
  839. enum1 f3;
  840. uint64 f4;
  841. int64[2] f5;
  842. }
  843. s[] bar;
  844. function test() public {
  845. s storage first = bar.push();
  846. first.f1 = true;
  847. first.f2 = "abc";
  848. first.f3 = enum1.val2;
  849. first.f4 = 65536;
  850. first.f5[0] = -1;
  851. first.f5[1] = 5;
  852. assert(bar[0].f5[1] == 5);
  853. // now erase it again
  854. bar.pop();
  855. }
  856. }"#,
  857. );
  858. runtime.function("test", Vec::new());
  859. // We should have one entry for the length; pop should have removed the 102 entry
  860. assert_eq!(runtime.store.len(), 1);
  861. }
  862. #[test]
  863. fn storage_delete() {
  864. // ensure that structs and fixed arrays are wiped by pop
  865. let mut runtime = build_solidity(
  866. r#"
  867. pragma solidity 0;
  868. contract foo {
  869. uint64 bar;
  870. function test() public {
  871. bar = 0xdeaddeaddeaddead;
  872. delete bar;
  873. }
  874. }"#,
  875. );
  876. runtime.function("test", Vec::new());
  877. // We should have one entry for the length; pop should have removed the 102 entry
  878. assert!(runtime.store.is_empty());
  879. // ensure that structs and fixed arrays are wiped by delete
  880. let mut runtime = build_solidity(
  881. r#"
  882. pragma solidity 0;
  883. contract foo {
  884. enum enum1 { val1, val2, val3 }
  885. struct s {
  886. bool f1;
  887. bytes3 f2;
  888. enum1 f3;
  889. uint64 f4;
  890. int64[2] f5;
  891. }
  892. s[] bar;
  893. function test() public {
  894. s storage first = bar.push();
  895. first.f1 = true;
  896. first.f2 = "abc";
  897. first.f3 = enum1.val2;
  898. first.f4 = 65536;
  899. first.f5[0] = -1;
  900. first.f5[1] = 5;
  901. assert(bar[0].f5[1] == 5);
  902. // now erase it again
  903. delete bar[0];
  904. }
  905. }"#,
  906. );
  907. runtime.function("test", Vec::new());
  908. // We should have one entry for the length; delete should have removed the entry
  909. assert_eq!(runtime.store.len(), 1);
  910. // ensure that structs and fixed arrays are wiped by delete
  911. let mut runtime = build_solidity(
  912. r#"
  913. pragma solidity 0;
  914. contract foo {
  915. int[] bar;
  916. function setup() public {
  917. bar.push(102);
  918. bar.push(305);
  919. }
  920. function clear() public {
  921. delete bar;
  922. }
  923. }"#,
  924. );
  925. runtime.function("setup", Vec::new());
  926. assert_eq!(runtime.store.len(), 3);
  927. runtime.function("clear", Vec::new());
  928. assert_eq!(runtime.store.len(), 0);
  929. // our delete operator has to iterate over the dynamic array. Ensure it works if the array is empty
  930. runtime.function("clear", Vec::new());
  931. assert_eq!(runtime.store.len(), 0);
  932. }
  933. #[test]
  934. fn storage_dynamic_copy() {
  935. let mut runtime = build_solidity(
  936. r#"
  937. contract c {
  938. int32[] foo;
  939. constructor() public {
  940. for (int32 i = 0; i <11; i++) {
  941. foo.push(i * 3);
  942. }
  943. }
  944. function storage_to_memory() view public {
  945. int32[] memory bar = foo;
  946. assert(bar.length == 11);
  947. for (int32 i = 0; i <11; i++) {
  948. assert(bar[uint32(i)] == i * 3);
  949. }
  950. }
  951. function memory_to_storage() public {
  952. int32[] memory bar = new int32[](5);
  953. for (int32 i = 0; i < 5; i++) {
  954. bar[uint32(i)] = 5 * (i + 7);
  955. }
  956. foo = bar;
  957. assert(foo.length == 5);
  958. for (int32 i = 0; i < 5; i++) {
  959. assert(foo[uint32(i)] == 5 * (i + 7));
  960. }
  961. }
  962. }
  963. "#,
  964. );
  965. runtime.constructor(0, Vec::new());
  966. runtime.function("storage_to_memory", Vec::new());
  967. runtime.function("memory_to_storage", Vec::new());
  968. assert_eq!(runtime.store.len(), 6);
  969. }
  970. #[test]
  971. fn abi_encode_dynamic_array() {
  972. #[derive(Debug, PartialEq, Eq, Encode, Decode)]
  973. struct Int32Array(Vec<i32>);
  974. let mut runtime = build_solidity(
  975. r#"
  976. contract c {
  977. function encode() pure public returns (int32[]) {
  978. int32[] memory bar = new int32[](11);
  979. for (int32 i = 0; i <11; i++) {
  980. bar[uint32(i)] = i * 3;
  981. }
  982. return bar;
  983. }
  984. }
  985. "#,
  986. );
  987. runtime.constructor(0, Vec::new());
  988. runtime.function("encode", Vec::new());
  989. assert_eq!(
  990. runtime.vm.output,
  991. Int32Array(vec!(0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30)).encode()
  992. );
  993. }
  994. #[test]
  995. fn abi_decode_dynamic_array() {
  996. #[derive(Debug, PartialEq, Eq, Encode, Decode)]
  997. struct Int32Array(Vec<i32>);
  998. let mut runtime = build_solidity(
  999. r#"
  1000. contract c {
  1001. function decode(int32[] bar) pure public {
  1002. assert(bar.length == 11);
  1003. for (int32 i = 0; i <11; i++) {
  1004. assert(bar[uint32(i)] == i * 3);
  1005. }
  1006. }
  1007. function decode_empty(int32[] bar) pure public {
  1008. assert(bar.length == 0);
  1009. }
  1010. }
  1011. "#,
  1012. );
  1013. runtime.constructor(0, Vec::new());
  1014. runtime.function(
  1015. "decode",
  1016. Int32Array(vec![0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30]).encode(),
  1017. );
  1018. runtime.function("decode_empty", Int32Array(vec![]).encode());
  1019. }
  1020. #[test]
  1021. fn abi_encode_dynamic_array2() {
  1022. #[derive(Debug, PartialEq, Eq, Encode, Decode)]
  1023. struct Array(Vec<(bool, u32)>);
  1024. let mut runtime = build_solidity(
  1025. r#"
  1026. contract structs {
  1027. struct foo {
  1028. bool x;
  1029. uint32 y;
  1030. }
  1031. function test() public returns (foo[]) {
  1032. foo[] x = new foo[](3);
  1033. x[0] = foo({x: true, y: 64});
  1034. x[1] = foo({x: false, y: 102});
  1035. x[2] = foo({x: true, y: 0x800});
  1036. return x;
  1037. }
  1038. }
  1039. "#,
  1040. );
  1041. runtime.constructor(0, Vec::new());
  1042. runtime.function("test", Vec::new());
  1043. assert_eq!(
  1044. runtime.vm.output,
  1045. Array(vec!((true, 64), (false, 102), (true, 0x800))).encode()
  1046. );
  1047. }
  1048. #[test]
  1049. fn abi_encode_dynamic_array3() {
  1050. #[derive(Debug, PartialEq, Eq, Encode, Decode)]
  1051. struct Array(Vec<(bool, u32, String)>);
  1052. let mut runtime = build_solidity(
  1053. r#"
  1054. contract structs {
  1055. struct foo {
  1056. bool x;
  1057. uint32 y;
  1058. string z;
  1059. }
  1060. function test() public returns (foo[]) {
  1061. foo[] x = new foo[](3);
  1062. x[0] = foo({x: true, y: 64, z: "abc"});
  1063. x[1] = foo({x: false, y: 102, z: "a"});
  1064. x[2] = foo({x: true, y: 0x800, z: "abcdef"});
  1065. return x;
  1066. }
  1067. }
  1068. "#,
  1069. );
  1070. runtime.constructor(0, Vec::new());
  1071. runtime.function("test", Vec::new());
  1072. assert_eq!(
  1073. runtime.vm.output,
  1074. Array(vec!(
  1075. (true, 64, "abc".to_owned()),
  1076. (false, 102, "a".to_owned()),
  1077. (true, 0x800, "abcdef".to_owned())
  1078. ))
  1079. .encode()
  1080. );
  1081. }
  1082. #[test]
  1083. fn abi_encode_dynamic_array4() {
  1084. #[derive(Debug, PartialEq, Eq, Encode, Decode)]
  1085. struct Array([(bool, u32, String); 3]);
  1086. let mut runtime = build_solidity(
  1087. r#"
  1088. contract structs {
  1089. struct foo {
  1090. bool x;
  1091. uint32 y;
  1092. string z;
  1093. }
  1094. function test() public returns (foo[3]) {
  1095. foo[3] x;
  1096. x[0] = foo({x: true, y: 64, z: "abc"});
  1097. x[1] = foo({x: false, y: 102, z: "a"});
  1098. x[2] = foo({x: true, y: 0x800, z: "abcdef"});
  1099. return x;
  1100. }
  1101. }
  1102. "#,
  1103. );
  1104. runtime.constructor(0, Vec::new());
  1105. runtime.function("test", Vec::new());
  1106. runtime.heap_verify();
  1107. assert_eq!(
  1108. runtime.vm.output,
  1109. Array([
  1110. (true, 64, "abc".to_owned()),
  1111. (false, 102, "a".to_owned()),
  1112. (true, 0x800, "abcdef".to_owned())
  1113. ])
  1114. .encode()
  1115. );
  1116. }
  1117. #[test]
  1118. fn large_index_ty_in_bounds() {
  1119. let mut runtime = build_solidity(
  1120. r#"
  1121. contract foo {
  1122. uint128 storage_index;
  1123. function test(uint128 index) public returns (uint16) {
  1124. uint16[] memory a = new uint16[](16);
  1125. storage_index = index;
  1126. return a[storage_index];
  1127. }
  1128. }"#,
  1129. );
  1130. runtime.constructor(0, Vec::new());
  1131. runtime.function("test", 15u128.encode());
  1132. runtime.function_expect_failure("test", 17u128.encode());
  1133. runtime.function_expect_failure("test", 0xfffffffffffffu128.encode());
  1134. }
  1135. #[test]
  1136. fn alloc_size_from_storage() {
  1137. let mut runtime = build_solidity(
  1138. r#"
  1139. contract Test {
  1140. uint32 length = 1;
  1141. function contfunc() public view returns (uint64[] memory) {
  1142. uint64[] memory values = new uint64[](length);
  1143. return values;
  1144. }
  1145. }"#,
  1146. );
  1147. runtime.constructor(0, Vec::new());
  1148. runtime.function("contfunc", Vec::new());
  1149. assert_eq!(runtime.vm.output, vec![0u64].encode());
  1150. }