-
Notifications
You must be signed in to change notification settings - Fork 179
/
Copy pathgas-custom-errors.js
310 lines (246 loc) · 9.42 KB
/
gas-custom-errors.js
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
const assert = require('assert')
const {
assertNoWarnings,
assertNoErrors,
assertErrorMessage,
assertErrorCount,
assertWarnsCount,
} = require('../../common/asserts')
const linter = require('../../../lib/index')
const { funcWith } = require('../../common/contract-builder')
function replaceSolidityVersion(code, newVersion) {
// Regular expression to match the version number in the pragma directive
const versionRegex = /pragma solidity \d+\.\d+\.\d+;/
// Replace the matched version with the newVersion
return code.replace(versionRegex, `pragma solidity ${newVersion};`)
}
describe('Linter - gas-custom-errors', () => {
it('should raise error for require with a non-function-call second argument', () => {
// second arg is a numeric literal instead of a function call
const code = `
pragma solidity 0.8.5;
contract A {
function test() external {
require(msg.sender != address(0), 123);
}
}
`
const report = linter.processStr(code, {
rules: { 'gas-custom-errors': 'error' },
})
assertErrorCount(report, 1)
assertErrorMessage(report, 'Use Custom Errors instead of require statements')
})
it('should raise error for require with no second argument', () => {
const code = `
pragma solidity 0.8.5;
contract A {
function test() external {
require(msg.sender != address(0));
}
}
`
const report = linter.processStr(code, {
rules: { 'gas-custom-errors': 'error' },
})
assertErrorCount(report, 1)
assertErrorMessage(report, 'Use Custom Errors instead of require statements')
})
it('should NOT raise error for require with comparison and custom error()', () => {
let code = funcWith(`require(a > b, CustomErrorEmitted());`)
code = replaceSolidityVersion(code, '^0.8.4')
const report = linter.processStr(code, {
rules: { 'gas-custom-errors': 'error' },
})
assertNoWarnings(report)
assertNoErrors(report)
})
it('should NOT raise error for require with function call and comparison along with custom error', () => {
let code = funcWith(`require(isAok(a) > b, CustomErrorEmitted(param1));`)
code = replaceSolidityVersion(code, '^0.8.4')
const report = linter.processStr(code, {
rules: { 'gas-custom-errors': 'error' },
})
assertNoWarnings(report)
assertNoErrors(report)
})
it('should NOT raise error for require with boolean check and custom error call', () => {
let code = funcWith(`require(success, CustomErrorEmitted(param1, param2));`)
code = replaceSolidityVersion(code, '^0.8.4')
const report = linter.processStr(code, {
rules: { 'gas-custom-errors': 'error' },
})
assertNoWarnings(report)
assertNoErrors(report)
})
it('should NOT raise error for require with function call and custom error call', () => {
let code = funcWith(
`require(isSuccess(param1) == true && value > 10, CustomErrorEmitted(param1, param2));`
)
code = replaceSolidityVersion(code, '^0.8.4')
const report = linter.processStr(code, {
rules: { 'gas-custom-errors': 'error' },
})
assertNoWarnings(report)
assertNoErrors(report)
})
it('should NOT raise error for require and mapping access with boolean value check and custom error call', () => {
let code = funcWith(
`require(users[msg.sender].isRegistered, CustomErrorEmitted(param1, param2));`
)
code = replaceSolidityVersion(code, '^0.8.4')
const report = linter.processStr(code, {
rules: { 'gas-custom-errors': 'error' },
})
assertNoWarnings(report)
assertNoErrors(report)
})
it('should raise error for revert()', () => {
let code = funcWith(`revert();`)
code = replaceSolidityVersion(code, '^0.8.4')
const report = linter.processStr(code, {
rules: { 'gas-custom-errors': 'error' },
})
assertErrorCount(report, 1)
assertErrorMessage(report, 'GC: Use Custom Errors instead of revert statement')
})
it('should raise error for revert([string])', () => {
let code = funcWith(`revert("Insufficient funds");`)
code = replaceSolidityVersion(code, '0.8.4')
const report = linter.processStr(code, {
rules: { 'gas-custom-errors': 'error' },
})
assertErrorCount(report, 1)
assertErrorMessage(report, 'GC: Use Custom Errors instead of revert statement')
})
it('should NOT raise error for revert ErrorFunction()', () => {
let code = funcWith(`revert ErrorFunction();`)
code = replaceSolidityVersion(code, '^0.8.22')
const report = linter.processStr(code, {
rules: { 'gas-custom-errors': 'error' },
})
assertNoWarnings(report)
assertNoErrors(report)
})
it('should NOT raise error for revert ErrorFunction() with arguments', () => {
let code = funcWith(`revert ErrorFunction({ msg: "Insufficient funds msg" });`)
code = replaceSolidityVersion(code, '^0.8.5')
const report = linter.processStr(code, {
rules: { 'gas-custom-errors': 'error' },
})
assertNoWarnings(report)
assertNoErrors(report)
})
it('should raise error for require', () => {
let code = funcWith(`require(!has(role, account), "Roles: account already has role");
role.bearer[account] = true;role.bearer[account] = true;
`)
code = replaceSolidityVersion(code, '0.8.21')
const report = linter.processStr(code, {
rules: { 'gas-custom-errors': 'error' },
})
assertErrorCount(report, 1)
assertErrorMessage(report, 'GC: Use Custom Errors instead of require statement')
})
it('should NOT raise error for regular function call', () => {
let code = funcWith(`callOtherFunction();
role.bearer[account] = true;role.bearer[account] = true;
`)
code = replaceSolidityVersion(code, '^0.9.0')
const report = linter.processStr(code, {
rules: { 'gas-custom-errors': 'error' },
})
assertNoWarnings(report)
assertNoErrors(report)
})
it('should raise error for require, revert message and not for revert CustomError() for [recommended] config', () => {
let code = funcWith(`require(!has(role, account), "Roles: account already has role");
revert("RevertMessage");
revert CustomError();
`)
code = replaceSolidityVersion(code, '0.8.20')
const report = linter.processStr(code, {
extends: 'solhint:recommended',
rules: { 'compiler-version': 'off' },
})
assertWarnsCount(report, 2)
assert.equal(report.reports[0].message, 'GC: Use Custom Errors instead of require statements')
assert.equal(report.reports[1].message, 'GC: Use Custom Errors instead of revert statements')
})
it('should NOT raise error for default config because rule is not part of default', () => {
const code = funcWith(`require(!has(role, account), "Roles: account already has role");
revert("RevertMessage");
revert CustomError();
`)
const report = linter.processStr(code, {
extends: 'solhint:default',
})
assertWarnsCount(report, 0)
assertErrorCount(report, 0)
})
it('should NOT raise error for lower versions 0.8.3', () => {
let code = funcWith(`revert();`)
code = replaceSolidityVersion(code, '0.8.3')
const report = linter.processStr(code, {
rules: { 'gas-custom-errors': 'error' },
})
assertErrorCount(report, 0)
})
it('should NOT raise error for lower versions 0.4.4', () => {
const code = funcWith(`revert("Insufficient funds");`)
const report = linter.processStr(code, {
rules: { 'gas-custom-errors': 'error' },
})
assertErrorCount(report, 0)
})
it('should raise error for higher versions 0.8', () => {
let code = funcWith(`revert();`)
code = replaceSolidityVersion(code, '0.8')
const report = linter.processStr(code, {
rules: { 'gas-custom-errors': 'error' },
})
assertErrorCount(report, 1)
assert.equal(report.reports[0].message, 'GC: Use Custom Errors instead of revert statements')
})
it('should raise error for higher versions 0.9', () => {
let code = funcWith(`revert();`)
code = replaceSolidityVersion(code, '0.9')
const report = linter.processStr(code, {
rules: { 'gas-custom-errors': 'error' },
})
assertErrorCount(report, 1)
assert.equal(report.reports[0].message, 'GC: Use Custom Errors instead of revert statements')
})
it('should NOT raise error for lower versions 0.7', () => {
let code = funcWith(`revert();`)
code = replaceSolidityVersion(code, '0.7')
const report = linter.processStr(code, {
rules: { 'gas-custom-errors': 'error' },
})
assertErrorCount(report, 0)
})
it('should NOT raise error for range versions lower than 0.8.4', () => {
let code = funcWith(`revert();`)
code = replaceSolidityVersion(code, '>= 0.8.1 <= 0.8.3')
const report = linter.processStr(code, {
rules: { 'gas-custom-errors': 'error' },
})
assertErrorCount(report, 0)
})
it('should raise error for range versions higher than 0.8.4', () => {
let code = funcWith(`revert();`)
code = replaceSolidityVersion(code, '> 0.8.4 <= 0.9')
const report = linter.processStr(code, {
rules: { 'gas-custom-errors': 'error' },
})
assertErrorCount(report, 1)
})
it('should raise error for range versions containing 0.8.4', () => {
let code = funcWith(`revert();`)
code = replaceSolidityVersion(code, '> 0.8.1 <= 0.8.6')
const report = linter.processStr(code, {
rules: { 'gas-custom-errors': 'error' },
})
assertErrorCount(report, 1)
})
})