modifier.rs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. use crate::{build_solidity, first_error, no_errors, parse_and_resolve};
  2. use parity_scale_codec::Encode;
  3. use parity_scale_codec_derive::{Decode, Encode};
  4. use solang::Target;
  5. #[test]
  6. fn declare() {
  7. let ns = parse_and_resolve(
  8. r#"
  9. contract c {
  10. modifier foo() public {}
  11. }"#,
  12. Target::Substrate {
  13. address_length: 32,
  14. value_length: 16,
  15. },
  16. );
  17. assert_eq!(
  18. first_error(ns.diagnostics),
  19. "‘public’: modifiers can not have visibility"
  20. );
  21. let ns = parse_and_resolve(
  22. r#"
  23. contract c {
  24. modifier foo() internal {}
  25. }"#,
  26. Target::Substrate {
  27. address_length: 32,
  28. value_length: 16,
  29. },
  30. );
  31. assert_eq!(
  32. first_error(ns.diagnostics),
  33. "‘internal’: modifiers can not have visibility"
  34. );
  35. let ns = parse_and_resolve(
  36. r#"
  37. contract c {
  38. modifier foo() payable {}
  39. }"#,
  40. Target::Substrate {
  41. address_length: 32,
  42. value_length: 16,
  43. },
  44. );
  45. assert_eq!(
  46. first_error(ns.diagnostics),
  47. "modifier cannot have mutability specifier"
  48. );
  49. let ns = parse_and_resolve(
  50. r#"
  51. contract c {
  52. modifier foo() pure {}
  53. }"#,
  54. Target::Substrate {
  55. address_length: 32,
  56. value_length: 16,
  57. },
  58. );
  59. assert_eq!(
  60. first_error(ns.diagnostics),
  61. "modifier cannot have mutability specifier"
  62. );
  63. let ns = parse_and_resolve(
  64. r#"
  65. contract c {
  66. modifier foo bar {}
  67. }"#,
  68. Target::Substrate {
  69. address_length: 32,
  70. value_length: 16,
  71. },
  72. );
  73. assert_eq!(
  74. first_error(ns.diagnostics),
  75. "function modifiers or base contracts are not allowed on modifier"
  76. );
  77. let ns = parse_and_resolve(
  78. r#"
  79. contract c {
  80. modifier foo() {}
  81. }"#,
  82. Target::Substrate {
  83. address_length: 32,
  84. value_length: 16,
  85. },
  86. );
  87. assert_eq!(first_error(ns.diagnostics), "missing ‘_’ in modifier");
  88. let ns = parse_and_resolve(
  89. r#"
  90. contract c {
  91. modifier foo(bool x) {
  92. if (true) {
  93. while (x) {
  94. _;
  95. }
  96. }
  97. }
  98. }"#,
  99. Target::Substrate {
  100. address_length: 32,
  101. value_length: 16,
  102. },
  103. );
  104. no_errors(ns.diagnostics);
  105. let ns = parse_and_resolve(
  106. r#"
  107. contract c {
  108. modifier foo() {
  109. _;
  110. }
  111. function bar() public {
  112. foo();
  113. }
  114. }"#,
  115. Target::Substrate {
  116. address_length: 32,
  117. value_length: 16,
  118. },
  119. );
  120. assert_eq!(
  121. first_error(ns.diagnostics),
  122. "unknown function or type ‘foo’"
  123. );
  124. let ns = parse_and_resolve(
  125. r#"
  126. contract c {
  127. function bar() public {
  128. _;
  129. }
  130. }"#,
  131. Target::Substrate {
  132. address_length: 32,
  133. value_length: 16,
  134. },
  135. );
  136. assert_eq!(
  137. first_error(ns.diagnostics),
  138. "underscore statement only permitted in modifiers"
  139. );
  140. }
  141. #[test]
  142. fn function_modifier() {
  143. let ns = parse_and_resolve(
  144. r#"
  145. contract c {
  146. modifier foo() { _; }
  147. function bar() foo2 public {}
  148. }"#,
  149. Target::Substrate {
  150. address_length: 32,
  151. value_length: 16,
  152. },
  153. );
  154. assert_eq!(first_error(ns.diagnostics), "unknown modifier ‘foo2’");
  155. let ns = parse_and_resolve(
  156. r#"
  157. contract c {
  158. modifier foo() { _; }
  159. function bar() foo(1) public {}
  160. }"#,
  161. Target::Substrate {
  162. address_length: 32,
  163. value_length: 16,
  164. },
  165. );
  166. assert_eq!(
  167. first_error(ns.diagnostics),
  168. "modifier expects 0 arguments, 1 provided"
  169. );
  170. let ns = parse_and_resolve(
  171. r#"
  172. contract c {
  173. modifier foo(int32 f) { _; }
  174. function bar(bool x) foo(x) public {}
  175. }"#,
  176. Target::Substrate {
  177. address_length: 32,
  178. value_length: 16,
  179. },
  180. );
  181. assert_eq!(
  182. first_error(ns.diagnostics),
  183. "conversion from bool to int32 not possible"
  184. );
  185. }
  186. #[test]
  187. fn chain() {
  188. let mut runtime = build_solidity(
  189. r#"
  190. contract c {
  191. uint16 public var;
  192. modifier foo() {
  193. bool boom = true;
  194. if (boom) {
  195. _;
  196. }
  197. }
  198. function bar() foo() public {
  199. var = 7;
  200. }
  201. }"#,
  202. );
  203. runtime.constructor(0, Vec::new());
  204. let slot = [0u8; 32];
  205. assert_eq!(runtime.store.get(&(runtime.vm.address, slot)), None);
  206. runtime.function("bar", Vec::new());
  207. assert_eq!(
  208. runtime.store.get(&(runtime.vm.address, slot)).unwrap(),
  209. &vec!(7, 0)
  210. );
  211. let mut runtime = build_solidity(
  212. r##"
  213. contract c {
  214. uint16 public var;
  215. modifier mod1 {
  216. var = 3;
  217. _;
  218. var = 5;
  219. }
  220. function test() mod1 public {
  221. assert(var == 3);
  222. var = 7;
  223. }
  224. }"##,
  225. );
  226. runtime.constructor(0, Vec::new());
  227. let slot = [0u8; 32];
  228. assert_eq!(runtime.store.get(&(runtime.vm.address, slot)), None);
  229. runtime.function("test", Vec::new());
  230. assert_eq!(
  231. runtime.store.get(&(runtime.vm.address, slot)).unwrap(),
  232. &vec!(5, 0)
  233. );
  234. // now test modifier with argument and test that function argument is passed on
  235. let mut runtime = build_solidity(
  236. r##"
  237. contract c {
  238. uint16 public var;
  239. modifier mod1(uint16 v) {
  240. var = 3;
  241. _;
  242. assert(var == 11);
  243. var = v;
  244. }
  245. function test(uint16 x) mod1(x - 6) public {
  246. assert(var == 3);
  247. var = x;
  248. }
  249. }"##,
  250. );
  251. runtime.constructor(0, Vec::new());
  252. let slot = [0u8; 32];
  253. assert_eq!(runtime.store.get(&(runtime.vm.address, slot)), None);
  254. runtime.function("test", 11u16.encode());
  255. assert_eq!(
  256. runtime.store.get(&(runtime.vm.address, slot)).unwrap(),
  257. &vec!(5, 0)
  258. );
  259. // now test modifier with argument and test that function argument is passed on
  260. let mut runtime = build_solidity(
  261. r##"
  262. contract c {
  263. uint16 public var;
  264. modifier mod1(uint16 v) {
  265. assert(var == 0);
  266. var = 3;
  267. _;
  268. assert(var == 17);
  269. var = v;
  270. }
  271. modifier mod2(uint16 v) {
  272. assert(var == 3);
  273. var = 9;
  274. _;
  275. assert(var == 11);
  276. var = v;
  277. }
  278. function test(uint16 x) mod1(x - 6) mod2(x + 6) public {
  279. assert(var == 9);
  280. var = x;
  281. }
  282. }"##,
  283. );
  284. runtime.constructor(0, Vec::new());
  285. let slot = [0u8; 32];
  286. assert_eq!(runtime.store.get(&(runtime.vm.address, slot)), None);
  287. runtime.function("test", 11u16.encode());
  288. assert_eq!(
  289. runtime.store.get(&(runtime.vm.address, slot)).unwrap(),
  290. &vec!(5, 0)
  291. );
  292. // two placeholders means the following function is called twice.
  293. let mut runtime = build_solidity(
  294. r##"
  295. contract c {
  296. uint16 public var;
  297. modifier m {
  298. _;
  299. _;
  300. }
  301. function test() m public {
  302. var += 3;
  303. }
  304. }"##,
  305. );
  306. runtime.constructor(0, Vec::new());
  307. let slot = [0u8; 32];
  308. assert_eq!(runtime.store.get(&(runtime.vm.address, slot)), None);
  309. runtime.function("test", Vec::new());
  310. assert_eq!(
  311. runtime.store.get(&(runtime.vm.address, slot)).unwrap(),
  312. &vec!(6, 0)
  313. );
  314. }
  315. #[test]
  316. fn inherit_modifier() {
  317. let mut runtime = build_solidity(
  318. r##"
  319. contract c is base {
  320. function test() md2 public {
  321. assert(s2 == 2);
  322. s2 += 3;
  323. }
  324. }
  325. contract base {
  326. bool private s1;
  327. int32 internal s2;
  328. modifier md2 {
  329. s2 += 2;
  330. _;
  331. s2 += 2;
  332. }
  333. }"##,
  334. );
  335. runtime.constructor(0, Vec::new());
  336. let mut slot = [0u8; 32];
  337. slot[0] = 1;
  338. assert_eq!(runtime.store.get(&(runtime.vm.address, slot)), None);
  339. runtime.function("test", Vec::new());
  340. assert_eq!(
  341. runtime.store.get(&(runtime.vm.address, slot)).unwrap(),
  342. &vec!(7, 0, 0, 0)
  343. );
  344. // now override it
  345. let mut runtime = build_solidity(
  346. r##"
  347. contract c is base {
  348. function test() md2 public {
  349. assert(s2 == 2);
  350. s2 += 3;
  351. }
  352. modifier md2 override {
  353. s2 += 2;
  354. _;
  355. s2 += 5;
  356. }
  357. }
  358. contract base {
  359. bool private s1;
  360. int32 internal s2;
  361. modifier md2 virtual {
  362. s2 += 1;
  363. _;
  364. s2 += 1;
  365. }
  366. }"##,
  367. );
  368. runtime.constructor(0, Vec::new());
  369. let mut slot = [0u8; 32];
  370. slot[0] = 1;
  371. assert_eq!(runtime.store.get(&(runtime.vm.address, slot)), None);
  372. runtime.function("test", Vec::new());
  373. assert_eq!(
  374. runtime.store.get(&(runtime.vm.address, slot)).unwrap(),
  375. &vec!(10, 0, 0, 0)
  376. );
  377. }
  378. #[test]
  379. fn return_values() {
  380. // in the modifier syntax, there are no return values
  381. // however, the generated cfg has the same arguments/returns the function is on attached
  382. // return simple value
  383. let mut runtime = build_solidity(
  384. r##"
  385. contract c {
  386. int64 s2;
  387. function test() md2 public returns (int64) {
  388. assert(s2 == 2);
  389. s2 += 3;
  390. return s2;
  391. }
  392. modifier md2 {
  393. s2 += 2;
  394. _;
  395. s2 += 2;
  396. }
  397. }"##,
  398. );
  399. runtime.constructor(0, Vec::new());
  400. runtime.function("test", Vec::new());
  401. #[derive(Debug, PartialEq, Encode, Decode)]
  402. struct Val(u64);
  403. assert_eq!(runtime.vm.output, Val(5).encode());
  404. let mut runtime = build_solidity(
  405. r##"
  406. struct S {
  407. int64 f1;
  408. string f2;
  409. }
  410. contract c {
  411. int64 s2;
  412. function test() md2 public returns (bool, S) {
  413. assert(s2 == 2);
  414. s2 += 3;
  415. return (true, S({ f1: s2, f2: "Hello, World!" }));
  416. }
  417. modifier md2 {
  418. s2 += 2;
  419. _;
  420. s2 += 2;
  421. }
  422. }"##,
  423. );
  424. runtime.constructor(0, Vec::new());
  425. runtime.function("test", Vec::new());
  426. #[derive(Debug, PartialEq, Encode, Decode)]
  427. struct StructS(bool, u64, String);
  428. assert_eq!(
  429. runtime.vm.output,
  430. StructS(true, 5, String::from("Hello, World!")).encode()
  431. );
  432. }
  433. #[test]
  434. fn mutability() {
  435. let ns = parse_and_resolve(
  436. r#"
  437. contract c {
  438. uint64 var;
  439. modifier foo(uint64 x) { _; }
  440. function bar() foo(var) public pure {}
  441. }"#,
  442. Target::Substrate {
  443. address_length: 32,
  444. value_length: 16,
  445. },
  446. );
  447. assert_eq!(
  448. first_error(ns.diagnostics),
  449. "function declared ‘pure’ but this expression reads from state"
  450. );
  451. let ns = parse_and_resolve(
  452. r#"
  453. contract c {
  454. uint64 var;
  455. modifier foo() { uint64 x = var; _; }
  456. function bar() foo() public pure {}
  457. }"#,
  458. Target::Substrate {
  459. address_length: 32,
  460. value_length: 16,
  461. },
  462. );
  463. assert_eq!(
  464. first_error(ns.diagnostics),
  465. "function declared ‘pure’ but this expression reads from state"
  466. );
  467. let ns = parse_and_resolve(
  468. r#"
  469. contract base {
  470. modifier foo() virtual {
  471. _;
  472. }
  473. }
  474. contract apex is base {
  475. function foo() public override {}
  476. }"#,
  477. Target::Substrate {
  478. address_length: 32,
  479. value_length: 16,
  480. },
  481. );
  482. assert_eq!(
  483. first_error(ns.diagnostics),
  484. "function ‘foo’ overrides modifier"
  485. );
  486. }
  487. #[test]
  488. fn repeated_modifier() {
  489. let mut runtime = build_solidity(
  490. r##"
  491. contract Test {
  492. modifier notZero(uint64 num) {
  493. require(num != 0, "invalid number");
  494. _;
  495. }
  496. function contfunc(uint64 num1, uint64 num2) public notZero(num1) notZero(num2) {
  497. // any code
  498. }
  499. }"##,
  500. );
  501. runtime.constructor(0, Vec::new());
  502. runtime.function_expect_failure("contfunc", (1u64, 0u64).encode());
  503. runtime.function_expect_failure("contfunc", (0u64, 0u64).encode());
  504. runtime.function_expect_failure("contfunc", (0u64, 1u64).encode());
  505. runtime.function("contfunc", (1u64, 1u64).encode());
  506. }