arrays.rs 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582
  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. #[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.storage().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.storage().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.storage().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.storage().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.storage().len(), 3);
  927. runtime.function("clear", Vec::new());
  928. assert_eq!(runtime.storage().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.storage().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.storage().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.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.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.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.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.output(), vec![0u64].encode());
  1150. }
  1151. #[test]
  1152. fn fixed_bytes() {
  1153. let mut runtime = build_solidity(
  1154. r##"
  1155. contract Storage {
  1156. bytes32[] data;
  1157. constructor() {
  1158. data.push(hex"0000000000000000000000000000000000000000000000000000000000000000");
  1159. data.push(hex"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f");
  1160. }
  1161. function uploadData(uint8 i, uint8 j) public view returns(bytes1) {
  1162. return(data[j][i]);
  1163. }
  1164. }
  1165. "##,
  1166. );
  1167. runtime.constructor(0, vec![]);
  1168. for i in 0..32u8 {
  1169. runtime.function("uploadData", vec![i, 0]);
  1170. assert_eq!(runtime.output()[..], [0]);
  1171. runtime.function("uploadData", vec![i, 1]);
  1172. assert_eq!(runtime.output()[..], [i]);
  1173. }
  1174. let mut runtime = build_solidity(
  1175. r##"
  1176. contract Memory {
  1177. constructor() {
  1178. }
  1179. function uploadData(uint8 i, uint8 j) public view returns(bytes1) {
  1180. bytes32[] data = new bytes32[](2);
  1181. data[0] = hex"0000000000000000000000000000000000000000000000000000000000000000";
  1182. data[1] = hex"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f";
  1183. return(data[j][i]);
  1184. }
  1185. }
  1186. "##,
  1187. );
  1188. runtime.constructor(0, vec![]);
  1189. for i in 0..32u8 {
  1190. runtime.function("uploadData", vec![i, 0]);
  1191. assert_eq!(runtime.output()[..], [0]);
  1192. runtime.function("uploadData", vec![i, 1]);
  1193. assert_eq!(runtime.output()[..], [i]);
  1194. }
  1195. }
  1196. #[test]
  1197. fn abi_decode_dynamic_array2() {
  1198. let mut runtime = build_solidity(
  1199. r#"
  1200. contract c {
  1201. function decode() pure public {
  1202. bytes enc = hex"2c000000000300000006000000090000000c0000000f0000001200000015000000180000001b0000001e000000";
  1203. int32[] bar = abi.decode(enc, (int32[]));
  1204. assert(bar.length == 11);
  1205. for (int32 i = 0; i <11; i++) {
  1206. assert(bar[uint32(i)] == i * 3);
  1207. }
  1208. }
  1209. function decode_empty() pure public {
  1210. bytes enc = hex"00";
  1211. int32[] bar = abi.decode(enc, (int32[]));
  1212. assert(bar.length == 0);
  1213. }
  1214. }
  1215. "#,
  1216. );
  1217. runtime.function("decode", vec![]);
  1218. runtime.function("decode_empty", vec![]);
  1219. }
  1220. #[test]
  1221. fn abi_decode_dynamic_array3() {
  1222. let mut runtime = build_solidity(
  1223. r#"
  1224. contract Arr {
  1225. function decode() pure public {
  1226. bytes enc = hex"14140001020304140102030405140203040506140304050607140405060708";
  1227. uint8[][] bar = abi.decode(enc, (uint8[][]));
  1228. assert(bar.length == 5);
  1229. for (uint8 i = 0; i <5; i++) {
  1230. for (uint8 j = 0; j <5; j++) {
  1231. assert(bar[uint32(i)][uint32(j)] == i + j);
  1232. }
  1233. }
  1234. }
  1235. function decode_empty() pure public {
  1236. bytes enc = hex"00";
  1237. uint8[][] bar = abi.decode(enc, (uint8[][]));
  1238. assert(bar.length == 0);
  1239. }
  1240. }
  1241. "#,
  1242. );
  1243. // The array in the first function was generated like this:
  1244. // let mut matrix = vec![];
  1245. // for i in 0..5 {
  1246. // matrix.push(vec![]);
  1247. // for j in 0..5 {
  1248. // matrix[i].push((i + j) as u8);
  1249. // }
  1250. // }
  1251. runtime.function("decode", vec![]);
  1252. runtime.function("decode_empty", vec![]);
  1253. }