-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathMacroCustomErrorFeatureTest.lean
More file actions
262 lines (208 loc) · 8.16 KB
/
Copy pathMacroCustomErrorFeatureTest.lean
File metadata and controls
262 lines (208 loc) · 8.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import Compiler.CompilationModel
import Contracts.Common
namespace Compiler.MacroCustomErrorFeatureTest
open Compiler.CompilationModel
open Contracts
open Verity hiding pure bind
open Verity.EVM.Uint256
open Verity.Stdlib.Math
namespace MacroCustomErrorUsageSmoke
verity_contract MacroCustomErrorUsage where
storage
sentinel : Uint256 := slot 0
errors
error NonPositive(Uint256)
error AmountTooLarge(Uint256, Uint256)
function requirePositive (amount : Uint256) : Unit := do
requireError (amount != 0) NonPositive(amount)
function rejectLarge (amount : Uint256) : Unit := do
if amount > 100 then
revert AmountTooLarge(amount, 100)
else
pure ()
def requirePositiveModelUsesCustomErrorGuard : Bool :=
match MacroCustomErrorUsage.requirePositive_modelBody with
| [Stmt.requireError
(Expr.logicalNot (Expr.eq (Expr.param "amount") (Expr.literal 0)))
"NonPositive"
[Expr.param "amount"],
Stmt.stop] =>
true
| _ => false
example : requirePositiveModelUsesCustomErrorGuard = true := by native_decide
def rejectLargeModelUsesCustomErrorRevert : Bool :=
match MacroCustomErrorUsage.rejectLarge_modelBody with
| [Stmt.ite
(Expr.gt (Expr.param "amount") (Expr.literal 100))
[Stmt.revertError "AmountTooLarge" [Expr.param "amount", Expr.literal 100]]
[],
Stmt.stop] =>
true
| _ => false
example : rejectLargeModelUsesCustomErrorRevert = true := by native_decide
def requirePositiveExecutablePreservesSuccess : Bool :=
match MacroCustomErrorUsage.requirePositive 7 Verity.defaultState with
| .success () state => state.sender == Verity.defaultState.sender
| .revert _ _ => false
example : requirePositiveExecutablePreservesSuccess = true := by native_decide
def rejectLargeExecutableUsesRuntimeFallback : Bool :=
match MacroCustomErrorUsage.rejectLarge 101 Verity.defaultState with
| .revert msg state =>
msg == "AmountTooLarge(101, 100)" && state.sender == Verity.defaultState.sender
| .success _ _ => false
example : rejectLargeExecutableUsesRuntimeFallback = true := by native_decide
end MacroCustomErrorUsageSmoke
namespace MacroCustomErrorRuntimeArgSmoke
verity_contract MacroCustomErrorRuntimeArgs where
storage
sentinel : Uint256 := slot 0
errors
error ExecutionResult(Uint256, Uint256, Address, Bool)
function failRuntime (preOpGas : Uint256, paid : Uint256, target : Address, success : Bool) : Unit := do
let total := add preOpGas paid
revertError ExecutionResult(total, add paid 1, target, success)
def failRuntimeModelUsesRuntimeCustomErrorArgs : Bool :=
match MacroCustomErrorRuntimeArgs.failRuntime_modelBody with
| [Stmt.letVar "total" (Expr.add (Expr.param "preOpGas") (Expr.param "paid")),
Stmt.revertError "ExecutionResult"
[Expr.localVar "total",
Expr.add (Expr.param "paid") (Expr.literal 1),
Expr.param "target",
Expr.param "success"],
Stmt.stop] =>
true
| _ => false
example : failRuntimeModelUsesRuntimeCustomErrorArgs = true := by decide
def failRuntimeExecutableUsesRuntimeFallback : Bool :=
match MacroCustomErrorRuntimeArgs.failRuntime 3 4 9 true Verity.defaultState with
| .revert msg state =>
msg == "ExecutionResult(7, 5, 9, true)" &&
state.sender == Verity.defaultState.sender
| .success _ _ => false
example : failRuntimeExecutableUsesRuntimeFallback = true := by decide
end MacroCustomErrorRuntimeArgSmoke
namespace RequireSomeUintErrorSmoke
verity_contract RequireSomeUintErrorUsage where
storage
sentinel : Uint256 := slot 0
errors
error AddOverflow ()
error SubUnderflow ()
error DivByZero ()
error MulOverflow (Uint256, Uint256)
function checkAdd (a : Uint256, b : Uint256) : Uint256 := do
let result ← requireSomeUintError (safeAdd a b) AddOverflow()
return result
function checkSub (a : Uint256, b : Uint256) : Uint256 := do
let result ← requireSomeUintError (safeSub a b) SubUnderflow()
return result
function checkDiv (a : Uint256, b : Uint256) : Uint256 := do
let result ← requireSomeUintError (safeDiv a b) DivByZero()
return result
function checkMul (a : Uint256, b : Uint256) : Uint256 := do
let result ← requireSomeUintError (safeMul a b) MulOverflow(a, b)
return result
def checkAddLowersToTypedRequireError : Bool :=
match RequireSomeUintErrorUsage.checkAdd_modelBody with
| [Stmt.requireError
(Expr.ge (Expr.add (Expr.param "a") (Expr.param "b")) (Expr.param "a"))
"AddOverflow"
[],
Stmt.letVar "result" (Expr.add (Expr.param "a") (Expr.param "b")),
Stmt.return (Expr.localVar "result")] =>
true
| _ => false
example : checkAddLowersToTypedRequireError = true := by native_decide
def checkSubLowersToTypedRequireError : Bool :=
match RequireSomeUintErrorUsage.checkSub_modelBody with
| [Stmt.requireError
(Expr.ge (Expr.param "a") (Expr.param "b"))
"SubUnderflow"
[],
Stmt.letVar "result" (Expr.sub (Expr.param "a") (Expr.param "b")),
Stmt.return (Expr.localVar "result")] =>
true
| _ => false
example : checkSubLowersToTypedRequireError = true := by native_decide
def checkDivLowersToTypedRequireError : Bool :=
match RequireSomeUintErrorUsage.checkDiv_modelBody with
| [Stmt.requireError
(Expr.logicalNot (Expr.eq (Expr.param "b") (Expr.literal 0)))
"DivByZero"
[],
Stmt.letVar "result" (Expr.div (Expr.param "a") (Expr.param "b")),
Stmt.return (Expr.localVar "result")] =>
true
| _ => false
example : checkDivLowersToTypedRequireError = true := by native_decide
def checkMulLowersToTypedRequireError : Bool :=
match RequireSomeUintErrorUsage.checkMul_modelBody with
| [Stmt.requireError
(Expr.logicalOr
(Expr.eq (Expr.param "b") (Expr.literal 0))
(Expr.eq
(Expr.div (Expr.mul (Expr.param "a") (Expr.param "b")) (Expr.param "b"))
(Expr.param "a")))
"MulOverflow"
[Expr.param "a", Expr.param "b"],
Stmt.letVar "result" (Expr.mul (Expr.param "a") (Expr.param "b")),
Stmt.return (Expr.localVar "result")] =>
true
| _ => false
example : checkMulLowersToTypedRequireError = true := by native_decide
def checkAddExecutableReturnsSumWhenSafe : Bool :=
match RequireSomeUintErrorUsage.checkAdd 1 2 Verity.defaultState with
| .success v _ => v == 3
| .revert _ _ => false
example : checkAddExecutableReturnsSumWhenSafe = true := by native_decide
def checkAddExecutableRevertsOnOverflow : Bool :=
let maxUint : Uint256 := Verity.Core.Uint256.ofNat Verity.Stdlib.Math.MAX_UINT256
match RequireSomeUintErrorUsage.checkAdd maxUint 1 Verity.defaultState with
| .revert msg _ => msg == "AddOverflow()"
| .success _ _ => false
example : checkAddExecutableRevertsOnOverflow = true := by native_decide
end RequireSomeUintErrorSmoke
/--
error: unknown custom error 'MissingOverflow'
-/
#guard_msgs in
verity_contract RequireSomeUintErrorUnknownErrorRejected where
storage
errors
error AddOverflow ()
function bad (a : Uint256, b : Uint256) : Uint256 := do
let result ← requireSomeUintError (safeAdd a b) MissingOverflow()
return result
/--
error: custom error 'MulOverflow' expects 2 args, got 1
-/
#guard_msgs in
verity_contract RequireSomeUintErrorWrongArityRejected where
storage
errors
error MulOverflow (Uint256, Uint256)
function bad (a : Uint256, b : Uint256) : Uint256 := do
let result ← requireSomeUintError (safeMul a b) MulOverflow(a)
return result
/--
error: custom error 'NeedsAmount' arg 1 in function 'bad' expects Verity.Macro.ValueType.uint256, got Verity.Macro.ValueType.bool
-/
#guard_msgs in
verity_contract CustomErrorWrongArgTypeRejected where
storage
errors
error NeedsAmount (Uint256)
function bad (ok : Bool) : Unit := do
revertError NeedsAmount(ok)
/--
error: unsupported requireSomeUintError source; expected safeAdd, safeSub, safeMul, or safeDiv
-/
#guard_msgs in
verity_contract RequireSomeUintErrorInvalidSourceRejected where
storage
errors
error AddOverflow ()
function bad (a : Uint256) : Uint256 := do
let result ← requireSomeUintError a AddOverflow()
return result
end Compiler.MacroCustomErrorFeatureTest