modifier.rs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. // SPDX-License-Identifier: Apache-2.0
  2. use crate::build_solidity;
  3. use parity_scale_codec::{Decode, Encode};
  4. #[test]
  5. fn chain() {
  6. let mut runtime = build_solidity(
  7. r#"
  8. contract c {
  9. uint16 public var;
  10. modifier foo() {
  11. bool boom = true;
  12. if (boom) {
  13. _;
  14. }
  15. }
  16. function bar() foo() public {
  17. var = 7;
  18. }
  19. }"#,
  20. );
  21. runtime.constructor(0, Vec::new());
  22. let slot = [0u8; 32];
  23. assert_eq!(runtime.store.get(&(runtime.vm.account, slot)), None);
  24. runtime.function("bar", Vec::new());
  25. assert_eq!(
  26. runtime.store.get(&(runtime.vm.account, slot)).unwrap(),
  27. &vec!(7, 0)
  28. );
  29. let mut runtime = build_solidity(
  30. r##"
  31. contract c {
  32. uint16 public var;
  33. modifier mod1 {
  34. var = 3;
  35. _;
  36. var = 5;
  37. }
  38. function test() mod1 public {
  39. assert(var == 3);
  40. var = 7;
  41. }
  42. }"##,
  43. );
  44. runtime.constructor(0, Vec::new());
  45. let slot = [0u8; 32];
  46. assert_eq!(runtime.store.get(&(runtime.vm.account, slot)), None);
  47. runtime.function("test", Vec::new());
  48. assert_eq!(
  49. runtime.store.get(&(runtime.vm.account, slot)).unwrap(),
  50. &vec!(5, 0)
  51. );
  52. // now test modifier with argument and test that function argument is passed on
  53. let mut runtime = build_solidity(
  54. r##"
  55. contract c {
  56. uint16 public var;
  57. modifier mod1(uint16 v) {
  58. var = 3;
  59. _;
  60. assert(var == 11);
  61. var = v;
  62. }
  63. function test(uint16 x) mod1(x - 6) public {
  64. assert(var == 3);
  65. var = x;
  66. }
  67. }"##,
  68. );
  69. runtime.constructor(0, Vec::new());
  70. let slot = [0u8; 32];
  71. assert_eq!(runtime.store.get(&(runtime.vm.account, slot)), None);
  72. runtime.function("test", 11u16.encode());
  73. assert_eq!(
  74. runtime.store.get(&(runtime.vm.account, slot)).unwrap(),
  75. &vec!(5, 0)
  76. );
  77. // now test modifier with argument and test that function argument is passed on
  78. let mut runtime = build_solidity(
  79. r##"
  80. contract c {
  81. uint16 public var;
  82. modifier mod1(uint16 v) {
  83. assert(var == 0);
  84. var = 3;
  85. _;
  86. assert(var == 17);
  87. var = v;
  88. }
  89. modifier mod2(uint16 v) {
  90. assert(var == 3);
  91. var = 9;
  92. _;
  93. assert(var == 11);
  94. var = v;
  95. }
  96. function test(uint16 x) mod1(x - 6) mod2(x + 6) public {
  97. assert(var == 9);
  98. var = x;
  99. }
  100. }"##,
  101. );
  102. runtime.constructor(0, Vec::new());
  103. let slot = [0u8; 32];
  104. assert_eq!(runtime.store.get(&(runtime.vm.account, slot)), None);
  105. runtime.function("test", 11u16.encode());
  106. assert_eq!(
  107. runtime.store.get(&(runtime.vm.account, slot)).unwrap(),
  108. &vec!(5, 0)
  109. );
  110. // two placeholders means the following function is called twice.
  111. let mut runtime = build_solidity(
  112. r##"
  113. contract c {
  114. uint16 public var;
  115. modifier m {
  116. _;
  117. _;
  118. }
  119. function test() m public {
  120. var += 3;
  121. }
  122. }"##,
  123. );
  124. runtime.constructor(0, Vec::new());
  125. let slot = [0u8; 32];
  126. assert_eq!(runtime.store.get(&(runtime.vm.account, slot)), None);
  127. runtime.function("test", Vec::new());
  128. assert_eq!(
  129. runtime.store.get(&(runtime.vm.account, slot)).unwrap(),
  130. &vec!(6, 0)
  131. );
  132. }
  133. #[test]
  134. fn inherit_modifier() {
  135. let mut runtime = build_solidity(
  136. r##"
  137. contract c is base {
  138. function test() md2 public {
  139. assert(s2 == 2);
  140. s2 += 3;
  141. }
  142. }
  143. abstract contract base {
  144. bool private s1;
  145. int32 internal s2;
  146. modifier md2 {
  147. s2 += 2;
  148. _;
  149. s2 += 2;
  150. }
  151. }"##,
  152. );
  153. runtime.constructor(0, Vec::new());
  154. let mut slot = [0u8; 32];
  155. slot[0] = 1;
  156. assert_eq!(runtime.store.get(&(runtime.vm.account, slot)), None);
  157. runtime.function("test", Vec::new());
  158. assert_eq!(
  159. runtime.store.get(&(runtime.vm.account, slot)).unwrap(),
  160. &vec!(7, 0, 0, 0)
  161. );
  162. // now override it
  163. let mut runtime = build_solidity(
  164. r##"
  165. contract c is base {
  166. function test() md2 public {
  167. assert(s2 == 2);
  168. s2 += 3;
  169. }
  170. modifier md2 override {
  171. s2 += 2;
  172. _;
  173. s2 += 5;
  174. }
  175. }
  176. abstract contract base {
  177. bool private s1;
  178. int32 internal s2;
  179. modifier md2 virtual {
  180. s2 += 1;
  181. _;
  182. s2 += 1;
  183. }
  184. }"##,
  185. );
  186. runtime.constructor(0, Vec::new());
  187. let mut slot = [0u8; 32];
  188. slot[0] = 1;
  189. assert_eq!(runtime.store.get(&(runtime.vm.account, slot)), None);
  190. runtime.function("test", Vec::new());
  191. assert_eq!(
  192. runtime.store.get(&(runtime.vm.account, slot)).unwrap(),
  193. &vec!(10, 0, 0, 0)
  194. );
  195. }
  196. #[test]
  197. fn return_values() {
  198. // in the modifier syntax, there are no return values
  199. // however, the generated cfg has the same arguments/returns the function is on attached
  200. // return simple value
  201. let mut runtime = build_solidity(
  202. r##"
  203. contract c {
  204. int64 s2;
  205. function test() md2 public returns (int64) {
  206. assert(s2 == 2);
  207. s2 += 3;
  208. return s2;
  209. }
  210. modifier md2 {
  211. s2 += 2;
  212. _;
  213. s2 += 2;
  214. }
  215. }"##,
  216. );
  217. runtime.constructor(0, Vec::new());
  218. runtime.function("test", Vec::new());
  219. #[derive(Debug, PartialEq, Eq, Encode, Decode)]
  220. struct Val(u64);
  221. assert_eq!(runtime.vm.output, Val(5).encode());
  222. let mut runtime = build_solidity(
  223. r##"
  224. struct S {
  225. int64 f1;
  226. string f2;
  227. }
  228. contract c {
  229. int64 s2;
  230. function test() md2 public returns (bool, S) {
  231. assert(s2 == 2);
  232. s2 += 3;
  233. return (true, S({ f1: s2, f2: "Hello, World!" }));
  234. }
  235. modifier md2 {
  236. s2 += 2;
  237. _;
  238. s2 += 2;
  239. }
  240. }"##,
  241. );
  242. runtime.constructor(0, Vec::new());
  243. runtime.function("test", Vec::new());
  244. #[derive(Debug, PartialEq, Eq, Encode, Decode)]
  245. struct StructS(bool, u64, String);
  246. assert_eq!(
  247. runtime.vm.output,
  248. StructS(true, 5, String::from("Hello, World!")).encode()
  249. );
  250. }
  251. #[test]
  252. fn repeated_modifier() {
  253. let mut runtime = build_solidity(
  254. r##"
  255. contract Test {
  256. modifier notZero(uint64 num) {
  257. require(num != 0, "invalid number");
  258. _;
  259. }
  260. function contfunc(uint64 num1, uint64 num2) public notZero(num1) notZero(num2) {
  261. // any code
  262. }
  263. }"##,
  264. );
  265. runtime.constructor(0, Vec::new());
  266. runtime.function_expect_failure("contfunc", (1u64, 0u64).encode());
  267. runtime.function_expect_failure("contfunc", (0u64, 0u64).encode());
  268. runtime.function_expect_failure("contfunc", (0u64, 1u64).encode());
  269. runtime.function("contfunc", (1u64, 1u64).encode());
  270. }