/
SortedCdps.spec
268 lines (182 loc) · 7.69 KB
/
SortedCdps.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
import "./base/sortedCdps.spec";
import "./dependencies/CdpManager.spec";
import "./dependencies/helper.spec";
/////////////////// METHODS ///////////////////////
///////////////// DEFINITIONS /////////////////////
////////////////// FUNCTIONS //////////////////////
///////////////// GHOSTS & HOOKS //////////////////
///////////////// INITIAL PROPERTIES /////////////
// toCdpId() return uniq result for uniq input
rule uniqunessOfId(address o1, address o2, uint256 b1, uint256 b2, uint256 n1, uint256 n2) {
// Calls to toCdpId are to the public solidity function which calls the internal one that is summarized
// therefore, this rule actually checks the summarization uniqueId
assert((o1 != o2 || b1 != b2 || n1 != n2) => toCdpId(o1, b1, n1) != toCdpId(o2, b2, n2));
}
// Special functions could change list head
rule changeToFirst(method f, env e, calldataarg args)
filtered { f -> !HARNESS_OR_VIEW_FUNCTIONS(f) } {
bytes32 before = getFirst();
f(e, args);
bytes32 after = getFirst();
assert(after != before =>(
f.selector == sig:insert(address,uint256,bytes32,bytes32).selector ||
f.selector == sig:reInsert(bytes32,uint256,bytes32,bytes32).selector ||
f.selector == sig:remove(bytes32).selector ||
f.selector == sig:batchRemove(bytes32[]).selector
));
}
// List size could not be more than maxSize
rule isFullCanNotIncrease(method f, env e, calldataarg args)
filtered { f -> !HARNESS_OR_VIEW_FUNCTIONS(f) } {
bool isFullBefore = isFull();
uint256 sizeBefore = getSize();
f(e, args);
assert(isFullBefore => getSize() <= sizeBefore);
}
// Possibility of not been reverted
use rule helperSanity;
///////////////// SETUP PROPERTIES ////////////////
// Require valid list structure initially
use invariant isListStructureValidInv;
// [2] Max size could not be zero
use invariant sortedCdpsMaxSizeGtZero;
// [3] Could not add more than maxSize
use invariant sortedCdpsSizeLeqMaxSize;
// Empty list solvency
use invariant sortedCdpsListEmptySolvency;
// One item list solvency
use invariant sortedCdpsListOneItemSolvency;
// [4] Several items list solvency
use invariant sortedCdpsListSeveralItemsSolvency;
// Item in the list should not link to self
use invariant sortedCdpsListNotLinkedToSelf;
// Item with zero id is not support
use invariant sortedCdpsListZeroIdsNotSupported;
// List should not contain duplicates
use invariant sortedCdpsListNoDuplicates;
// SL-01 The NICR ranking in the sorted list should follow descending order
use invariant sortedCdpsListNICRDescendingOrder;
///////////////// PROPERTIES //////////////////////
// Only BorrowerOperations or CdpManager should modify state
rule onlyBOorCdpMShouldModityState(env e, method f, calldataarg args)
filtered { f -> !HARNESS_OR_VIEW_FUNCTIONS(f) } {
storage before = lastStorage;
f(e, args);
storage after = lastStorage;
// State was changed
assert(before[currentContract] != after[currentContract]
=> (e.msg.sender == borrowerOperationsAddress() || e.msg.sender == cdpManager())
);
}
// Only insert should increase list size and next CDP nonce
rule onlyInsertShouldIncreaseSizeAndNonce(env e, method f, calldataarg args)
filtered { f -> !HARNESS_OR_VIEW_FUNCTIONS(f) } {
mathint sizeBefore = ghostSize;
mathint nextCdpNonceBefore = ghostNextCdpNonce;
f(e, args);
mathint sizeAfter = ghostSize;
mathint nextCdpNonceAfter = ghostNextCdpNonce;
assert(sizeAfter > sizeBefore <=> INSERT_FUNCTION(f));
assert((nextCdpNonceAfter == nextCdpNonceBefore + 1) <=> INSERT_FUNCTION(f));
}
// Next CDP nonce always increments
invariant nextCdpNonceIncrements() ghostNextCdpNoncePrev != 0 && ghostNextCdpNonce != 0
=> ghostNextCdpNonce == ghostNextCdpNoncePrev + to_mathint(1)
filtered { f -> !HARNESS_OR_VIEW_FUNCTIONS(f) }
// reInsert() should not change list size
rule reInsertNotAffectSize(env e, bytes32 _id, uint256 _newNICR, bytes32 _prevId, bytes32 _nextId) {
mathint sizeBefore = ghostSize;
reInsert(e, _id, _newNICR, _prevId, _nextId);
mathint sizeAfter = ghostSize;
assert(sizeBefore == sizeAfter);
}
// reInsert() should remove existing id
rule reInsertRemoveExistingId(env e, bytes32 _id, uint256 _newNICR, bytes32 _prevId, bytes32 _nextId) {
// Require a valid list structure
setupList();
bool inList = isNodeInList(_id);
reInsert@withrevert(e, _id, _newNICR, _prevId, _nextId);
assert(!inList => lastReverted);
}
// Only remove functions should decrease list size
rule onlyRemoveFunctionsShouldIncreaseSize(env e, method f, calldataarg args)
filtered { f -> !HARNESS_OR_VIEW_FUNCTIONS(f) } {
mathint sizeBefore = ghostSize;
f(e, args);
mathint sizeAfter = ghostSize;
assert(sizeAfter < sizeBefore <=> REMOVE_FUNCTIONS(f));
}
// Only CdpManager should remove ids
rule onlyCdpMShouldRemove(env e, method f, calldataarg args)
filtered { f -> !HARNESS_OR_VIEW_FUNCTIONS(f) } {
mathint sizeBefore = ghostSize;
f(e, args);
mathint sizeAfter = ghostSize;
assert(sizeBefore > sizeAfter => e.msg.sender == cdpManager());
}
// [g239,g240,g241,g242,g243] contains() integrity
rule containsIntegrity(bytes32 _id) {
// Require a valid list structure
setupList();
assert(contains(_id) == isNodeInList(_id));
}
// [g14,g15,g16,g17,g18,g19,g20] getOwnerAddress() integrity
rule getOwnerAddressIntegrity(bytes32 cdpId) {
address owner = getOwnerAddress(cdpId);
// The owner address is stored in the first 20 bytes of the CdpId
assert(verifyAddressFormatting(cdpId, owner));
}
// [g228] Prove possibility to remove() when list is full
rule removeWhenMaxSize(env e) {
// Require a valid list structure
setupList();
// Require list to be full
require(to_mathint(maxSize()) == 4);
require(to_mathint(maxSize()) == ghostSize);
remove(e, ghostTail);
satisfy(ghostSize == to_mathint(maxSize()) - 1);
}
// [g245] validInsertPosition() integrity
rule validInsertPositionIntegrity(env e, uint256 _NICR, bytes32 _prevId, bytes32 _nextId) {
// Require a valid list structure
setupList();
bool isValid = validInsertPosition(e, _NICR, _prevId, _nextId);
assert(_prevId == dummyId() && _nextId == dummyId()
=> isValid == isEmpty());
assert(_prevId == dummyId() && _nextId != dummyId()
=> (isValid == (ghostHead == _nextId && to_mathint(_NICR) >= ghostCachedNominalICR[_nextId])));
assert(_prevId != dummyId() && _nextId == dummyId()
=> (isValid == (ghostTail == _prevId && to_mathint(_NICR) <= ghostCachedNominalICR[_prevId])));
assert(_prevId != dummyId() && _nextId != dummyId()
=> (isValid == (_prevId == ghostPrevId[_nextId]
&& ghostCachedNominalICR[_prevId] >= to_mathint(_NICR)
&& to_mathint(_NICR) >= ghostCachedNominalICR[_nextId])
));
}
// maxSize() could not be set to zero in constructor
invariant maxSizeNotZeroInConstructor() to_mathint(maxSize()) != 0
filtered { f -> f.selector == 0 } {
preserved {
require(false);
}
}
// [g189] batchRemove() decrease size
rule batchRemoveDecreaseSize(env e, bytes32[] _ids) {
// Require a valid list structure
setupList();
mathint before = ghostSize;
batchRemove(e, _ids);
mathint after = ghostSize;
assert(assert_uint256(before - after) == _ids.length);
}
// [g160,g161,g163] batchRemove() should revert passing non-exist node
rule batchRemoveOnlyExistingIdsSupported(env e, bytes32[] _ids) {
// Require a valid list structure
setupList();
uint256 i;
require(i < _ids.length);
bool inList = contains(_ids[i]);
batchRemove@withrevert(e, _ids);
bool reverted = lastReverted;
assert(!inList => reverted);
}