-
Notifications
You must be signed in to change notification settings - Fork 0
/
RateLimits.spec
374 lines (273 loc) · 14.3 KB
/
RateLimits.spec
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// RateLimits.spec
methods {
// constants
function DEFAULT_ADMIN_ROLE() external returns (bytes32) envfree;
function CONTROLLER() external returns (bytes32) envfree;
// getters
function hasRole(bytes32,address) external returns (bool) envfree;
function getRoleAdmin(bytes32) external returns (bytes32) envfree;
function getRateLimitData(bytes32) external returns (IRateLimits.RateLimitData) envfree;
//
}
definition defMin(mathint a, mathint b) returns mathint = a < b ? a : b;
definition defGetCurrentRateLimit(env e, bytes32 key) returns mathint =
currentContract._data[key].maxAmount == max_uint256
? max_uint256
: defMin(
currentContract._data[key].slope * (e.block.timestamp - currentContract._data[key].lastUpdated) + currentContract._data[key].lastAmount,
currentContract._data[key].maxAmount
);
// Verify no more entry points exist
rule entryPoints(method f) filtered { f -> !f.isView } {
env e;
calldataarg args;
f(e, args);
assert f.selector == sig:grantRole(bytes32,address).selector ||
f.selector == sig:revokeRole(bytes32,address).selector ||
f.selector == sig:renounceRole(bytes32,address).selector ||
f.selector == sig:setRateLimitData(bytes32,uint256,uint256,uint256,uint256).selector ||
f.selector == sig:setRateLimitData(bytes32,uint256,uint256).selector ||
f.selector == sig:setUnlimitedRateLimitData(bytes32).selector ||
f.selector == sig:triggerRateLimitDecrease(bytes32,uint256).selector ||
f.selector == sig:triggerRateLimitIncrease(bytes32,uint256).selector;
}
// Verify that each storage layout is only modified in the corresponding functions
rule storageAffected(method f) {
env e;
bytes32 anyBytes32;
address anyAddr;
bool hasRoleBefore = hasRole(anyBytes32, anyAddr);
bytes32 roleAdminBefore = getRoleAdmin(anyBytes32);
IRateLimits.RateLimitData dataBefore = getRateLimitData(anyBytes32);
calldataarg args;
f(e, args);
bool hasRoleAfter = hasRole(anyBytes32, anyAddr);
bytes32 roleAdminAfter = getRoleAdmin(anyBytes32);
IRateLimits.RateLimitData dataAfter = getRateLimitData(anyBytes32);
assert hasRoleAfter != hasRoleBefore =>
f.selector == sig:grantRole(bytes32,address).selector ||
f.selector == sig:revokeRole(bytes32,address).selector ||
f.selector == sig:renounceRole(bytes32,address).selector, "Assert 1";
assert roleAdminAfter == roleAdminBefore, "Assert 2";
assert dataAfter.maxAmount != dataBefore.maxAmount =>
f.selector == sig:setRateLimitData(bytes32,uint256,uint256,uint256,uint256).selector ||
f.selector == sig:setRateLimitData(bytes32,uint256,uint256).selector ||
f.selector == sig:setUnlimitedRateLimitData(bytes32).selector, "Assert 3";
assert dataAfter.slope != dataBefore.slope =>
f.selector == sig:setRateLimitData(bytes32,uint256,uint256,uint256,uint256).selector ||
f.selector == sig:setRateLimitData(bytes32,uint256,uint256).selector ||
f.selector == sig:setUnlimitedRateLimitData(bytes32).selector, "Assert 4";
assert dataAfter.lastAmount != dataBefore.lastAmount =>
f.selector == sig:setRateLimitData(bytes32,uint256,uint256,uint256,uint256).selector ||
f.selector == sig:setRateLimitData(bytes32,uint256,uint256).selector ||
f.selector == sig:setUnlimitedRateLimitData(bytes32).selector ||
f.selector == sig:triggerRateLimitDecrease(bytes32,uint256).selector ||
f.selector == sig:triggerRateLimitIncrease(bytes32,uint256).selector, "Assert 5";
assert dataAfter.lastUpdated != dataBefore.lastUpdated =>
f.selector == sig:setRateLimitData(bytes32,uint256,uint256,uint256,uint256).selector ||
f.selector == sig:setRateLimitData(bytes32,uint256,uint256).selector ||
f.selector == sig:setUnlimitedRateLimitData(bytes32).selector ||
f.selector == sig:triggerRateLimitDecrease(bytes32,uint256).selector ||
f.selector == sig:triggerRateLimitIncrease(bytes32,uint256).selector, "Assert 6";
}
// Verify correct storage changes for non reverting grantRole
rule grantRole(bytes32 role, address account) {
env e;
bytes32 otherRole;
address otherAccount;
require otherRole != role || otherAccount != account;
bool hasOtherBefore = hasRole(otherRole, otherAccount);
grantRole(e, role, account);
bool hasRoleAfter = hasRole(role, account);
bool hasOtherAfter = hasRole(otherRole, otherAccount);
assert hasRoleAfter, "Assert 1";
assert hasOtherAfter == hasOtherBefore, "Assert 2";
}
// Verify revert rules on grantRole
rule grantRole_revert(bytes32 role, address account) {
env e;
bool hasRoleAdminSender = hasRole(getRoleAdmin(role), e.msg.sender);
grantRole@withrevert(e, role, account);
bool revert1 = e.msg.value > 0;
bool revert2 = !hasRoleAdminSender;
assert lastReverted <=> revert1 || revert2, "Revert rules failed";
}
// Verify correct storage changes for non reverting revokeRole
rule revokeRole(bytes32 role, address account) {
env e;
bytes32 otherRole;
address otherAccount;
require otherRole != role || otherAccount != account;
bool hasOtherBefore = hasRole(otherRole, otherAccount);
revokeRole(e, role, account);
bool hasRoleAfter = hasRole(role, account);
bool hasOtherAfter = hasRole(otherRole, otherAccount);
assert !hasRoleAfter, "Assert 1";
assert hasOtherAfter == hasOtherBefore, "Assert 2";
}
// Verify revert rules on revokeRole
rule revokeRole_revert(bytes32 role, address account) {
env e;
bool hasRoleAdminSender = hasRole(getRoleAdmin(role), e.msg.sender);
revokeRole@withrevert(e, role, account);
bool revert1 = e.msg.value > 0;
bool revert2 = !hasRoleAdminSender;
assert lastReverted <=> revert1 || revert2, "Revert rules failed";
}
// Verify correct storage changes for non reverting renounceRole
rule renounceRole(bytes32 role, address account) {
env e;
bytes32 otherRole;
address otherAccount;
require otherRole != role || otherAccount != account;
bool hasOtherBefore = hasRole(otherRole, otherAccount);
renounceRole(e, role, account);
bool hasRoleAfter = hasRole(role, account);
bool hasOtherAfter = hasRole(otherRole, otherAccount);
assert !hasRoleAfter, "Assert 1";
assert hasOtherAfter == hasOtherBefore, "Assert 2";
}
// Verify revert rules on renounceRole
rule renounceRole_revert(bytes32 role, address account) {
env e;
renounceRole@withrevert(e, role, account);
bool revert1 = e.msg.value > 0;
bool revert2 = account != e.msg.sender;
assert lastReverted <=> revert1 || revert2, "Revert rules failed";
}
// Verify correct storage changes for non reverting setRateLimitData
rule setRateLimitData(bytes32 key, uint256 maxAmount, uint256 slope, uint256 lastAmount, uint256 lastUpdated) {
env e;
setRateLimitData(e, key, maxAmount, slope, lastAmount, lastUpdated);
IRateLimits.RateLimitData dataAfter = getRateLimitData(key);
assert dataAfter.maxAmount == maxAmount, "Assert 1";
assert dataAfter.slope == slope, "Assert 2";
assert dataAfter.lastAmount == lastAmount, "Assert 3";
assert dataAfter.lastUpdated == lastUpdated, "Assert 4";
}
// Verify revert rules on setRateLimitData
rule setRateLimitData_revert(bytes32 key, uint256 maxAmount, uint256 slope, uint256 lastAmount, uint256 lastUpdated) {
env e;
bool hasRoleAdminSender = hasRole(DEFAULT_ADMIN_ROLE(), e.msg.sender);
setRateLimitData@withrevert(e, key, maxAmount, slope, lastAmount, lastUpdated);
bool revert1 = e.msg.value > 0;
bool revert2 = !hasRoleAdminSender;
bool revert3 = lastAmount > maxAmount;
bool revert4 = lastUpdated > e.block.timestamp;
assert lastReverted <=> revert1 || revert2 || revert3 ||
revert4, "Revert rules failed";
}
// Verify correct storage changes for non reverting setRateLimitData
rule setRateLimitData2(bytes32 key, uint256 maxAmount, uint256 slope) {
env e;
setRateLimitData(e, key, maxAmount, slope);
IRateLimits.RateLimitData dataAfter = getRateLimitData(key);
assert dataAfter.maxAmount == maxAmount, "Assert 1";
assert dataAfter.slope == slope, "Assert 2";
assert dataAfter.lastAmount == maxAmount, "Assert 3";
assert dataAfter.lastUpdated == e.block.timestamp, "Assert 4";
}
// Verify revert rules on setRateLimitData
rule setRateLimitData2_revert(bytes32 key, uint256 maxAmount, uint256 slope) {
env e;
bool hasRoleAdminSender = hasRole(DEFAULT_ADMIN_ROLE(), e.msg.sender);
setRateLimitData@withrevert(e, key, maxAmount, slope);
bool revert1 = e.msg.value > 0;
bool revert2 = !hasRoleAdminSender;
assert lastReverted <=> revert1 || revert2, "Revert rules failed";
}
// Verify correct storage changes for non reverting setUnlimitedRateLimitData
rule setUnlimitedRateLimitData(bytes32 key) {
env e;
setUnlimitedRateLimitData(e, key);
IRateLimits.RateLimitData dataAfter = getRateLimitData(key);
assert dataAfter.maxAmount == max_uint256, "Assert 1";
assert dataAfter.slope == 0, "Assert 2";
assert dataAfter.lastAmount == max_uint256, "Assert 3";
assert dataAfter.lastUpdated == e.block.timestamp, "Assert 4";
}
// Verify revert rules on setUnlimitedRateLimitData
rule setUnlimitedRateLimitData_revert(bytes32 key) {
env e;
bool hasRoleAdminSender = hasRole(DEFAULT_ADMIN_ROLE(), e.msg.sender);
setUnlimitedRateLimitData@withrevert(e, key);
bool revert1 = e.msg.value > 0;
bool revert2 = !hasRoleAdminSender;
assert lastReverted <=> revert1 || revert2, "Revert rules failed";
}
// Verify correct behaviour for getRateLimitData getter
rule getRateLimitData(bytes32 key) {
env e;
IRateLimits.RateLimitData data = getRateLimitData(key);
assert data.maxAmount == currentContract._data[key].maxAmount, "Assert 1";
assert data.slope == currentContract._data[key].slope, "Assert 2";
assert data.lastAmount == currentContract._data[key].lastAmount, "Assert 3";
assert data.lastUpdated == currentContract._data[key].lastUpdated, "Assert 4";
}
// Verify correct behaviour for getCurrentRateLimit getter
rule getCurrentRateLimit(bytes32 key) {
env e;
mathint limit = defGetCurrentRateLimit(e, key);
mathint limitRet = getCurrentRateLimit(e, key);
assert limitRet == limit, "Assert 1";
}
// Verify correct storage changes for non reverting triggerRateLimitDecrease
rule triggerRateLimitDecrease(bytes32 key, uint256 amountToDecrease) {
env e;
IRateLimits.RateLimitData dataKeyBefore = getRateLimitData(key);
triggerRateLimitDecrease(e, key, amountToDecrease);
IRateLimits.RateLimitData dataKeyAfter = getRateLimitData(key);
assert dataKeyAfter.maxAmount == dataKeyBefore.maxAmount, "Assert 1";
assert dataKeyAfter.slope == dataKeyBefore.slope, "Assert 2";
assert dataKeyBefore.maxAmount == max_uint256 => dataKeyAfter.lastAmount == dataKeyBefore.lastAmount, "Assert 3";
assert dataKeyBefore.maxAmount < max_uint256 => dataKeyAfter.lastAmount == defMin(dataKeyBefore.slope * (e.block.timestamp - dataKeyBefore.lastUpdated) + dataKeyBefore.lastAmount, dataKeyBefore.maxAmount) - amountToDecrease, "Assert 4";
assert dataKeyBefore.maxAmount == max_uint256 => dataKeyAfter.lastUpdated == dataKeyBefore.lastUpdated, "Assert 5";
assert dataKeyBefore.maxAmount < max_uint256 => dataKeyAfter.lastUpdated == e.block.timestamp, "Assert 6";
}
// Verify revert rules on triggerRateLimitDecrease
rule triggerRateLimitDecrease_revert(bytes32 key, uint256 amountToDecrease) {
env e;
bool hasRoleControllerSender = hasRole(CONTROLLER(), e.msg.sender);
IRateLimits.RateLimitData dataKey = getRateLimitData(key);
mathint currentRateLimit = getCurrentRateLimit(e, key);
// Practical assumptions
require e.block.timestamp >= dataKey.lastUpdated;
triggerRateLimitDecrease@withrevert(e, key, amountToDecrease);
bool revert1 = e.msg.value > 0;
bool revert2 = !hasRoleControllerSender;
bool revert3 = dataKey.maxAmount == 0;
bool revert4 = dataKey.maxAmount < max_uint256 && dataKey.slope * (e.block.timestamp - dataKey.lastUpdated) + dataKey.lastAmount > max_uint256;
bool revert5 = amountToDecrease > currentRateLimit;
assert lastReverted <=> revert1 || revert2 || revert3 ||
revert4 || revert5, "Revert rules failed";
}
// Verify correct storage changes for non reverting triggerRateLimitIncrease
rule triggerRateLimitIncrease(bytes32 key, uint256 amountToIncrease) {
env e;
IRateLimits.RateLimitData dataKeyBefore = getRateLimitData(key);
triggerRateLimitIncrease(e, key, amountToIncrease);
IRateLimits.RateLimitData dataKeyAfter = getRateLimitData(key);
assert dataKeyAfter.maxAmount == dataKeyBefore.maxAmount, "Assert 1";
assert dataKeyAfter.slope == dataKeyBefore.slope, "Assert 2";
assert dataKeyBefore.maxAmount == max_uint256 => dataKeyAfter.lastAmount == dataKeyBefore.lastAmount, "Assert 3";
assert dataKeyBefore.maxAmount < max_uint256 => dataKeyAfter.lastAmount == defMin(dataKeyBefore.slope * (e.block.timestamp - dataKeyBefore.lastUpdated) + dataKeyBefore.lastAmount + amountToIncrease, dataKeyBefore.maxAmount), "Assert 4";
assert dataKeyBefore.maxAmount == max_uint256 => dataKeyAfter.lastUpdated == dataKeyBefore.lastUpdated, "Assert 5";
assert dataKeyBefore.maxAmount < max_uint256 => dataKeyAfter.lastUpdated == e.block.timestamp, "Assert 6";
}
// Verify revert rules on triggerRateLimitIncrease
rule triggerRateLimitIncrease_revert(bytes32 key, uint256 amountToIncrease) {
env e;
bool hasRoleControllerSender = hasRole(CONTROLLER(), e.msg.sender);
IRateLimits.RateLimitData dataKey = getRateLimitData(key);
mathint currentRateLimit = getCurrentRateLimit(e, key);
// Practical assumptions
require e.block.timestamp >= dataKey.lastUpdated;
triggerRateLimitIncrease@withrevert(e, key, amountToIncrease);
bool revert1 = e.msg.value > 0;
bool revert2 = !hasRoleControllerSender;
bool revert3 = dataKey.maxAmount == 0;
bool revert4 = dataKey.maxAmount < max_uint256 && dataKey.slope * (e.block.timestamp - dataKey.lastUpdated) + dataKey.lastAmount > max_uint256;
bool revert5 = dataKey.maxAmount < max_uint256 && currentRateLimit + amountToIncrease > max_uint256;
assert lastReverted <=> revert1 || revert2 || revert3 ||
revert4 || revert5, "Revert rules failed";
}