structs.rs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. use parity_scale_codec::{Decode, Encode};
  2. use parity_scale_codec_derive::{Decode, Encode};
  3. use serde_derive::Deserialize;
  4. use crate::{build_solidity, first_error, parse_and_resolve};
  5. use solang::Target;
  6. #[derive(Debug, PartialEq, Encode, Decode)]
  7. struct Val32(u32);
  8. #[derive(Debug, PartialEq, Encode, Decode)]
  9. struct Val8(u8);
  10. #[test]
  11. fn parse_structs() {
  12. let ns = parse_and_resolve(
  13. r#"
  14. contract test_struct_parsing {
  15. struct Foo {
  16. bool a;
  17. uint a;
  18. }
  19. }"#,
  20. Target::Substrate {
  21. address_length: 32,
  22. value_length: 16,
  23. },
  24. );
  25. assert_eq!(
  26. first_error(ns.diagnostics),
  27. "struct ‘Foo’ has duplicate struct field ‘a’"
  28. );
  29. let ns = parse_and_resolve(
  30. r#"
  31. contract test_struct_parsing {
  32. struct Foo {
  33. bool a;
  34. uint storage b;
  35. }
  36. }"#,
  37. Target::Substrate {
  38. address_length: 32,
  39. value_length: 16,
  40. },
  41. );
  42. assert_eq!(
  43. first_error(ns.diagnostics),
  44. "storage location ‘storage’ not allowed for struct field"
  45. );
  46. let ns = parse_and_resolve(
  47. r#"
  48. contract test_struct_parsing {
  49. struct Foo {
  50. bool a;
  51. uint calldata b;
  52. }
  53. }"#,
  54. Target::Substrate {
  55. address_length: 32,
  56. value_length: 16,
  57. },
  58. );
  59. assert_eq!(
  60. first_error(ns.diagnostics),
  61. "storage location ‘calldata’ not allowed for struct field"
  62. );
  63. let ns = parse_and_resolve(
  64. r#"
  65. contract test_struct_parsing {
  66. struct Foo {
  67. bool memory a;
  68. uint calldata b;
  69. }
  70. }"#,
  71. Target::Substrate {
  72. address_length: 32,
  73. value_length: 16,
  74. },
  75. );
  76. assert_eq!(
  77. first_error(ns.diagnostics),
  78. "storage location ‘memory’ not allowed for struct field"
  79. );
  80. let ns = parse_and_resolve(
  81. r#"
  82. contract test_struct_parsing {
  83. struct Foo {
  84. }
  85. }"#,
  86. Target::Substrate {
  87. address_length: 32,
  88. value_length: 16,
  89. },
  90. );
  91. assert_eq!(
  92. first_error(ns.diagnostics),
  93. "struct definition for ‘Foo’ has no fields"
  94. );
  95. let ns = parse_and_resolve(
  96. r#"
  97. contract test_struct_parsing {
  98. struct Foo {
  99. boolean x;
  100. }
  101. }"#,
  102. Target::Substrate {
  103. address_length: 32,
  104. value_length: 16,
  105. },
  106. );
  107. assert_eq!(first_error(ns.diagnostics), "type ‘boolean’ not found");
  108. // is it impossible to define recursive structs
  109. let ns = parse_and_resolve(
  110. r#"
  111. contract test_struct_parsing {
  112. struct Foo {
  113. bool x;
  114. Foo y;
  115. }
  116. }"#,
  117. Target::Substrate {
  118. address_length: 32,
  119. value_length: 16,
  120. },
  121. );
  122. assert_eq!(
  123. first_error(ns.diagnostics),
  124. "struct ‘Foo’ has infinite size"
  125. );
  126. // is it impossible to define recursive structs
  127. let ns = parse_and_resolve(
  128. r#"
  129. contract c {
  130. s z;
  131. }
  132. struct s {
  133. bool f1;
  134. int32 f2;
  135. s2 f3;
  136. }
  137. struct s2 {
  138. bytes4 selector;
  139. s foo;
  140. }"#,
  141. Target::Substrate {
  142. address_length: 32,
  143. value_length: 16,
  144. },
  145. );
  146. assert_eq!(first_error(ns.diagnostics), "struct ‘s2’ has infinite size");
  147. // literal initializers
  148. let ns = parse_and_resolve(
  149. r#"
  150. contract test_struct_parsing {
  151. struct Foo {
  152. bool x;
  153. int32 y;
  154. }
  155. function f() private {
  156. Foo a = Foo();
  157. }
  158. }"#,
  159. Target::Substrate {
  160. address_length: 32,
  161. value_length: 16,
  162. },
  163. );
  164. assert_eq!(
  165. first_error(ns.diagnostics),
  166. "struct ‘Foo’ has 2 fields, not 0"
  167. );
  168. // literal initializers
  169. let ns = parse_and_resolve(
  170. r#"
  171. contract test_struct_parsing {
  172. struct Foo {
  173. bool x;
  174. int32 y;
  175. }
  176. function f() private {
  177. Foo a = Foo(true, true, true);
  178. }
  179. }"#,
  180. Target::Substrate {
  181. address_length: 32,
  182. value_length: 16,
  183. },
  184. );
  185. assert_eq!(
  186. first_error(ns.diagnostics),
  187. "struct ‘Foo’ has 2 fields, not 3"
  188. );
  189. // literal initializers
  190. let ns = parse_and_resolve(
  191. r#"
  192. contract test_struct_parsing {
  193. struct Foo {
  194. bool x;
  195. int32 y;
  196. }
  197. function f() private {
  198. Foo a = Foo({ });
  199. }
  200. }"#,
  201. Target::Substrate {
  202. address_length: 32,
  203. value_length: 16,
  204. },
  205. );
  206. assert_eq!(
  207. first_error(ns.diagnostics),
  208. "struct ‘Foo’ has 2 fields, not 0"
  209. );
  210. // literal initializers
  211. let ns = parse_and_resolve(
  212. r#"
  213. contract test_struct_parsing {
  214. struct Foo {
  215. bool x;
  216. int32 y;
  217. }
  218. function f() private {
  219. Foo a = Foo({ x: true, y: 1, z: 2 });
  220. }
  221. }"#,
  222. Target::Substrate {
  223. address_length: 32,
  224. value_length: 16,
  225. },
  226. );
  227. assert_eq!(
  228. first_error(ns.diagnostics),
  229. "struct ‘Foo’ has 2 fields, not 3"
  230. );
  231. // literal initializers
  232. let ns = parse_and_resolve(
  233. r#"
  234. contract test_struct_parsing {
  235. struct Foo {
  236. bool x;
  237. int32 y;
  238. }
  239. function f() private {
  240. Foo a = Foo({ x: true, z: 1 });
  241. }
  242. }"#,
  243. Target::Substrate {
  244. address_length: 32,
  245. value_length: 16,
  246. },
  247. );
  248. assert_eq!(first_error(ns.diagnostics), "struct ‘Foo’ has no field ‘z’");
  249. }
  250. #[test]
  251. fn struct_members() {
  252. let mut runtime = build_solidity(
  253. r##"
  254. pragma solidity 0;
  255. pragma experimental ABIEncoderV2;
  256. contract test_struct_parsing {
  257. struct foo {
  258. bool x;
  259. uint32 y;
  260. bytes31 d;
  261. }
  262. function test() public {
  263. foo f;
  264. f.x = true;
  265. f.y = 64;
  266. assert(f.x == true);
  267. assert(f.y == 64);
  268. assert(f.d.length == 31);
  269. foo f2 = foo(false, 32168, hex"DEAD");
  270. assert(f2.x == false);
  271. assert(f2.y == 32168);
  272. assert(f2.d == hex"dead");
  273. foo f3 = foo({ x: true, y: 102, d: hex"00DEAD" });
  274. assert(f3.x == true);
  275. assert(f3.y == 102);
  276. assert(f3.d == hex"00dead");
  277. }
  278. }"##,
  279. );
  280. runtime.function("test", Vec::new());
  281. }
  282. #[test]
  283. fn structs_as_ref_args() {
  284. let mut runtime = build_solidity(
  285. r##"
  286. contract test_struct_parsing {
  287. struct foo {
  288. bool x;
  289. uint32 y;
  290. }
  291. function func(foo f) private {
  292. // assigning to f members dereferences f
  293. f.x = true;
  294. f.y = 64;
  295. // assigning to f changes the reference
  296. f = foo({ x: false, y: 256 });
  297. // f no longer point to f in caller function
  298. f.x = false;
  299. f.y = 98123;
  300. }
  301. function test() public {
  302. foo f = foo(false, 2);
  303. func(f);
  304. assert(f.x == true);
  305. assert(f.y == 64);
  306. }
  307. }"##,
  308. );
  309. runtime.function("test", Vec::new());
  310. }
  311. #[test]
  312. fn structs_encode() {
  313. #[derive(Debug, PartialEq, Encode, Decode)]
  314. struct Foo {
  315. f1: [u8; 3],
  316. f2: bool,
  317. }
  318. let mut runtime = build_solidity(
  319. r##"
  320. contract test_struct_parsing {
  321. struct foo {
  322. bytes3 f1;
  323. bool f2;
  324. }
  325. function test(foo f) public {
  326. assert(f.f1 == "ABC");
  327. assert(f.f2 == true);
  328. }
  329. }"##,
  330. );
  331. runtime.function(
  332. "test",
  333. Foo {
  334. f1: [0x41, 0x42, 0x43],
  335. f2: true,
  336. }
  337. .encode(),
  338. );
  339. }
  340. #[test]
  341. fn structs_decode() {
  342. #[derive(Debug, PartialEq, Encode, Decode)]
  343. struct Foo {
  344. f1: [u8; 3],
  345. f2: i32,
  346. }
  347. let mut runtime = build_solidity(
  348. r##"
  349. contract test_struct_parsing {
  350. struct foo {
  351. bytes3 f1;
  352. int32 f2;
  353. }
  354. function test() public returns (foo) {
  355. foo f;
  356. f.f1 = hex"f33ec3";
  357. f.f2 = 0xfd7f;
  358. return f;
  359. }
  360. }"##,
  361. );
  362. runtime.function("test", Vec::new());
  363. assert_eq!(
  364. runtime.vm.output,
  365. Foo {
  366. f1: [0xf3, 0x3e, 0xc3],
  367. f2: 0xfd7f,
  368. }
  369. .encode(),
  370. );
  371. // allocate an array of structs. On dereference, the
  372. // struct elements are allocated. Here we are testing
  373. // the allocation and also that the abi encoder can
  374. // an struct when the pointer is still null
  375. let mut runtime = build_solidity(
  376. r##"
  377. contract test_struct_parsing {
  378. struct foo {
  379. bytes3 f1;
  380. int32 f2;
  381. }
  382. function test() public returns (foo[]) {
  383. foo[] f = new foo[](3);
  384. f[1].f1 = hex"f33ec3";
  385. f[1].f2 = 0xfd7f;
  386. return f;
  387. }
  388. }"##,
  389. );
  390. runtime.constructor(0, Vec::new());
  391. runtime.function("test", Vec::new());
  392. assert_eq!(
  393. runtime.vm.output,
  394. vec![
  395. Foo {
  396. f1: [0, 0, 0],
  397. f2: 0,
  398. },
  399. Foo {
  400. f1: [0xf3, 0x3e, 0xc3],
  401. f2: 0xfd7f,
  402. },
  403. Foo {
  404. f1: [0, 0, 0],
  405. f2: 0,
  406. },
  407. ]
  408. .encode(),
  409. );
  410. #[derive(Debug, PartialEq, Encode, Decode, Deserialize)]
  411. struct Foo2 {
  412. f1: [u8; 3],
  413. f2: i32,
  414. f3: String,
  415. f4: Vec<i64>,
  416. f5: [bool; 4],
  417. }
  418. let mut runtime = build_solidity(
  419. r##"
  420. contract test_struct_parsing {
  421. struct foo {
  422. bytes3 f1;
  423. int32 f2;
  424. string f3;
  425. int64[] f4;
  426. bool[4] f5;
  427. }
  428. function test() public returns (foo[] f) {
  429. f = new foo[](1);
  430. }
  431. function test_zero() public returns (foo[] f) { }
  432. }"##,
  433. );
  434. runtime.constructor(0, Vec::new());
  435. runtime.function("test", Vec::new());
  436. let mut output: &[u8] = &runtime.vm.output;
  437. assert_eq!(
  438. Vec::<Foo2>::decode(&mut output).expect("decode failed"),
  439. vec![Foo2 {
  440. f1: [0, 0, 0],
  441. f2: 0,
  442. f3: String::from(""),
  443. f4: Vec::new(),
  444. f5: [false; 4],
  445. }]
  446. );
  447. runtime.function("test_zero", Vec::new());
  448. let mut output: &[u8] = &runtime.vm.output;
  449. assert_eq!(
  450. Vec::<Foo2>::decode(&mut output).expect("decode failed"),
  451. vec![]
  452. );
  453. }
  454. #[test]
  455. fn struct_in_struct() {
  456. let mut runtime = build_solidity(
  457. r##"
  458. pragma solidity 0;
  459. contract struct_in_struct {
  460. struct foo {
  461. bool x;
  462. uint32 y;
  463. }
  464. struct bar {
  465. address a;
  466. bytes7 b;
  467. foo c;
  468. }
  469. function test() public pure returns (bytes7) {
  470. bar memory f = bar({ a: address(0), b: hex"fe", c: foo({ x: true, y: 102 }) });
  471. foo memory m = foo(false, 50);
  472. f.c = m;
  473. f.c.y = 300;
  474. assert(m.y == 300);
  475. return f.b;
  476. }
  477. }"##,
  478. );
  479. runtime.function("test", Vec::new());
  480. }
  481. #[test]
  482. fn structs_in_structs_decode() {
  483. #[derive(Debug, PartialEq, Encode, Decode)]
  484. struct Foo {
  485. f1: [u8; 3],
  486. f2: i32,
  487. }
  488. #[derive(Debug, PartialEq, Encode, Decode)]
  489. struct Bar {
  490. a: bool,
  491. b: Foo,
  492. c: Foo,
  493. }
  494. let mut runtime = build_solidity(
  495. r##"
  496. contract test_struct_parsing {
  497. struct foo {
  498. bytes3 f1;
  499. int32 f2;
  500. }
  501. struct bar {
  502. bool a;
  503. foo b;
  504. foo c;
  505. }
  506. function test() public returns (bar) {
  507. bar f = bar({ a: true, b: foo({ f1: hex"c30000", f2: 0xff7f}), c: foo({ f1: hex"f7f6f5", f2: 0x4002 })});
  508. return f;
  509. }
  510. }"##,
  511. );
  512. runtime.function("test", Vec::new());
  513. assert_eq!(
  514. runtime.vm.output,
  515. Bar {
  516. a: true,
  517. b: Foo {
  518. f1: [0xc3, 0x00, 0x00],
  519. f2: 0xff7f,
  520. },
  521. c: Foo {
  522. f1: [0xf7, 0xf6, 0xf5],
  523. f2: 0x4002,
  524. }
  525. }
  526. .encode(),
  527. );
  528. }
  529. #[test]
  530. fn structs_in_structs_encode() {
  531. #[derive(Debug, PartialEq, Encode, Decode)]
  532. struct Foo {
  533. f1: [u8; 3],
  534. f2: i32,
  535. }
  536. #[derive(Debug, PartialEq, Encode, Decode)]
  537. struct Bar {
  538. a: bool,
  539. b: Foo,
  540. c: Foo,
  541. }
  542. let mut runtime = build_solidity(
  543. r##"
  544. contract test_struct_parsing {
  545. struct foo {
  546. bytes3 f1;
  547. int32 f2;
  548. }
  549. function test(other.bar f) public {
  550. assert(f.c.f2 == 0x4002);
  551. assert(f.b.f1 == hex"c30000");
  552. }
  553. }
  554. contract other {
  555. struct bar {
  556. bool a;
  557. test_struct_parsing.foo b;
  558. test_struct_parsing.foo c;
  559. }
  560. }"##,
  561. );
  562. runtime.function(
  563. "test",
  564. Bar {
  565. a: true,
  566. b: Foo {
  567. f1: [0xc3, 0x00, 0x00],
  568. f2: 0xff7f,
  569. },
  570. c: Foo {
  571. f1: [0xf7, 0xf6, 0xf5],
  572. f2: 0x4002,
  573. },
  574. }
  575. .encode(),
  576. );
  577. }
  578. #[test]
  579. fn struct_storage_to_memory() {
  580. let mut runtime = build_solidity(
  581. r##"
  582. contract test_struct_parsing {
  583. struct foo {
  584. bytes3 f1;
  585. int64 f2;
  586. }
  587. foo bar;
  588. constructor() public {
  589. bar.f1 = hex"123456";
  590. bar.f2 = 0x0123456789abcdef;
  591. }
  592. function test() public {
  593. foo f = bar;
  594. assert(f.f1 == hex"123456");
  595. assert(f.f2 == 81985529216486895);
  596. }
  597. }"##,
  598. );
  599. runtime.constructor(0, Vec::new());
  600. runtime.function("test", Vec::new());
  601. }
  602. #[test]
  603. fn return_from_struct_storage() {
  604. #[derive(Debug, PartialEq, Encode, Decode)]
  605. struct Foo {
  606. f1: [u8; 3],
  607. f2: u32,
  608. }
  609. let mut runtime = build_solidity(
  610. r##"
  611. struct foo {
  612. bytes3 f1;
  613. uint32 f2;
  614. }
  615. contract test_struct_parsing {
  616. foo bar;
  617. constructor() public {
  618. bar.f1 = "png";
  619. bar.f2 = 0x89abcdef;
  620. }
  621. function test() public returns (foo) {
  622. return bar;
  623. }
  624. }"##,
  625. );
  626. runtime.constructor(0, Vec::new());
  627. runtime.function("test", Vec::new());
  628. assert_eq!(
  629. runtime.vm.output,
  630. Foo {
  631. f1: [0x70, 0x6e, 0x67],
  632. f2: 0x89ab_cdef,
  633. }
  634. .encode(),
  635. );
  636. }
  637. #[test]
  638. fn struct_in_init_return() {
  639. #[derive(Debug, PartialEq, Encode, Decode)]
  640. struct Card {
  641. value: u8,
  642. suit: u8,
  643. }
  644. #[derive(Debug, PartialEq, Encode, Decode)]
  645. struct Hand {
  646. card1: Card,
  647. card2: Card,
  648. card3: Card,
  649. card4: Card,
  650. card5: Card,
  651. }
  652. let mut runtime = build_solidity(
  653. r#"
  654. enum suit { club, diamonds, hearts, spades }
  655. enum value { two, three, four, five, six, seven, eight, nine, ten, jack, queen, king, ace }
  656. struct card {
  657. value v;
  658. suit s;
  659. }
  660. contract structs {
  661. card card1 = card({ s: suit.hearts, v: value.two });
  662. card card2 = card({ s: suit.diamonds, v: value.three });
  663. card card3 = card({ s: suit.club, v: value.four });
  664. card card4 = card({ s: suit.diamonds, v: value.ten });
  665. card card5 = card({ s: suit.hearts, v: value.jack });
  666. function test() public {
  667. assert(card1.s == suit.hearts);
  668. assert(card1.v == value.two);
  669. assert(card2.s == suit.diamonds);
  670. assert(card2.v == value.three);
  671. assert(card3.s == suit.club);
  672. assert(card3.v == value.four);
  673. assert(card4.s == suit.diamonds);
  674. assert(card4.v == value.ten);
  675. assert(card5.s == suit.hearts);
  676. assert(card5.v == value.jack);
  677. }
  678. }"#,
  679. );
  680. runtime.constructor(0, Vec::new());
  681. runtime.function("test", Vec::new());
  682. }
  683. #[test]
  684. fn struct_struct_in_init_and_return() {
  685. #[derive(Debug, PartialEq, Encode, Decode)]
  686. struct Card {
  687. v: u8,
  688. s: u8,
  689. }
  690. #[derive(Debug, PartialEq, Encode, Decode)]
  691. struct Hand {
  692. card1: Card,
  693. card2: Card,
  694. card3: Card,
  695. card4: Card,
  696. card5: Card,
  697. }
  698. let mut runtime = build_solidity(
  699. r#"
  700. contract structs {
  701. enum suit { club, diamonds, hearts, spades }
  702. enum value { two, three, four, five, six, seven, eight, nine, ten, jack, queen, king, ace }
  703. struct card {
  704. value v;
  705. suit s;
  706. }
  707. struct hand {
  708. card card1;
  709. card card2;
  710. card card3;
  711. card card4;
  712. card card5;
  713. }
  714. hand h = hand({
  715. card1: card({ s: suit.hearts, v: value.two }),
  716. card2: card({ s: suit.diamonds, v: value.three }),
  717. card3: card({ s: suit.club, v: value.four }),
  718. card4: card({ s: suit.diamonds, v: value.ten }),
  719. card5: card({ s: suit.hearts, v: value.jack })
  720. });
  721. function return_struct_from_storage(hand storage n) private returns (hand) {
  722. return n;
  723. }
  724. function test() public {
  725. hand l = return_struct_from_storage(h);
  726. assert(l.card1.s == suit.hearts);
  727. assert(l.card1.v == value.two);
  728. assert(l.card2.s == suit.diamonds);
  729. assert(l.card2.v == value.three);
  730. assert(l.card3.s == suit.club);
  731. assert(l.card3.v == value.four);
  732. assert(l.card4.s == suit.diamonds);
  733. assert(l.card4.v == value.ten);
  734. assert(l.card5.s == suit.hearts);
  735. assert(l.card5.v == value.jack);
  736. }
  737. }
  738. "#,
  739. );
  740. runtime.constructor(0, Vec::new());
  741. runtime.function("test", Vec::new());
  742. }