forked from circlefin/stablecoin-evm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
storageSlots.behavior.ts
368 lines (321 loc) · 11 KB
/
storageSlots.behavior.ts
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
/**
* SPDX-License-Identifier: Apache-2.0
*
* Copyright (c) 2023, Circle Internet Financial, LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import BN from "bn.js";
import { FiatTokenProxyInstance } from "../../@types/generated";
import { POW_2_255_BN } from "./constants";
const FiatTokenProxy = artifacts.require("FiatTokenProxy");
const FiatTokenV1 = artifacts.require("FiatTokenV1");
const FiatTokenV1_1 = artifacts.require("FiatTokenV1_1");
const FiatTokenV2 = artifacts.require("FiatTokenV2");
const FiatTokenV2_1 = artifacts.require("FiatTokenV2_1");
const FiatTokenV2_2 = artifacts.require("FiatTokenV2_2");
export const STORAGE_SLOT_NUMBERS = {
_deprecatedBlacklisted: 3,
balanceAndBlacklistStates: 9,
};
export function usesOriginalStorageSlotPositions<
T extends Truffle.ContractInstance
>({
Contract,
version,
accounts,
}: {
Contract: Truffle.Contract<T>;
version: 1 | 1.1 | 2 | 2.1 | 2.2;
accounts: Truffle.Accounts;
}): void {
describe("uses original storage slot positions", () => {
const [name, symbol, currency, decimals] = ["USD Coin", "USDC", "USD", 6];
const [mintAllowance, minted, transferred, allowance] = [
1000e6,
100e6,
30e6,
10e6,
];
const [mintedBN, transferredBN] = [minted, transferred].map(
(v) => new BN(v, 10)
);
const [
owner,
proxyAdmin,
masterMinter,
pauser,
blacklister,
minter,
rescuer,
alice,
bob,
charlie,
lostAndFound,
] = accounts;
let fiatToken: T;
let proxy: FiatTokenProxyInstance;
let domainSeparator: string;
beforeEach(async () => {
fiatToken = await Contract.new();
proxy = await FiatTokenProxy.new(fiatToken.address);
await proxy.changeAdmin(proxyAdmin);
const proxyAsFiatTokenV1 = await FiatTokenV1.at(proxy.address);
await proxyAsFiatTokenV1.initialize(
name,
symbol,
currency,
decimals,
masterMinter,
pauser,
blacklister,
owner
);
await proxyAsFiatTokenV1.configureMinter(minter, mintAllowance, {
from: masterMinter,
});
await proxyAsFiatTokenV1.mint(alice, minted, { from: minter });
await proxyAsFiatTokenV1.transfer(bob, transferred, { from: alice });
await proxyAsFiatTokenV1.approve(charlie, allowance, { from: alice });
await proxyAsFiatTokenV1.blacklist(bob, { from: blacklister });
await proxyAsFiatTokenV1.blacklist(charlie, { from: blacklister });
await proxyAsFiatTokenV1.pause({ from: pauser });
if (version >= 1.1) {
const proxyAsFiatTokenV1_1 = await FiatTokenV1_1.at(proxy.address);
await proxyAsFiatTokenV1_1.updateRescuer(rescuer, {
from: owner,
});
}
if (version >= 2) {
const proxyAsFiatTokenV2 = await FiatTokenV2.at(proxy.address);
await proxyAsFiatTokenV2.initializeV2(name);
domainSeparator = await proxyAsFiatTokenV2.DOMAIN_SEPARATOR();
}
if (version >= 2.1) {
const proxyAsFiatTokenV2_1 = await FiatTokenV2_1.at(proxy.address);
await proxyAsFiatTokenV2_1.initializeV2_1(lostAndFound);
}
if (version >= 2.2) {
const proxyAsFiatTokenV2_2 = await FiatTokenV2_2.at(proxy.address);
await proxyAsFiatTokenV2_2.initializeV2_2([], symbol);
}
});
it("retains original storage slots 0 through 13", async () => {
const slots = new Array<string>(14);
for (let i = 0; i < slots.length; i++) {
slots[i] = await readSlot(proxy.address, i);
}
// slot 0 - owner
expect(parseAddress(slots[0])).to.equal(owner); // owner
// slot 1 - pauser, paused
// values are lower-order aligned
expect(parseInt(slots[1].slice(0, 2), 16)).to.equal(1); // paused
expect(parseAddress(slots[1].slice(2))).to.equal(pauser); // pauser
// slot 2 - blacklister
expect(parseAddress(slots[2])).to.equal(blacklister); // blacklister
// slot 3 - _deprecatedBlacklisted (mapping, slot is unused)
expect(slots[3]).to.equal("0");
// slot 4 - name
expect(parseString(slots[4])).to.equal(name);
// slot 5 - symbol
expect(parseString(slots[5])).to.equal(symbol);
// slot 6 - decimals
expect(parseUint(slots[6]).toNumber()).to.equal(decimals);
// slot 7 - currency
expect(parseString(slots[7])).to.equal(currency);
// slot 8 - masterMinter, initialized
expect(slots[8].slice(0, 2)).to.equal("01"); // initialized
expect(parseAddress(slots[8].slice(2))).to.equal(masterMinter); // masterMinter
// slot 9 - balanceAndBlacklistStates (mapping, slot is unused)
expect(slots[9]).to.equal("0");
// slot 10 - allowed (mapping, slot is unused)
expect(slots[10]).to.equal("0");
// slot 11 - totalSupply
expect(parseUint(slots[11]).toNumber()).to.equal(minted);
// slot 12 - minters (mapping, slot is unused)
expect(slots[12]).to.equal("0");
// slot 13 - minterAllowed (mapping, slot is unused)
expect(slots[13]).to.equal("0");
});
if (version >= 1.1) {
it("retains slot 14 for rescuer", async () => {
const slot = await readSlot(proxy.address, 14);
expect(parseAddress(slot)).to.equal(rescuer);
});
}
if (version >= 2) {
it("retains slot 15 for DOMAIN_SEPARATOR", async () => {
const slot = await readSlot(proxy.address, 15);
// Cached domain separator is deprecated in v2.2. But we still need to ensure the storage slot is retained.
expect("0x" + slot).to.equal(domainSeparator);
});
}
it("retains original storage slots for _deprecatedBlacklisted mapping", async () => {
// _deprecatedBlacklisted[alice]
let v = parseInt(
await readSlot(
proxy.address,
addressMappingSlot(alice, STORAGE_SLOT_NUMBERS._deprecatedBlacklisted)
),
16
);
expect(v).to.equal(0);
// _deprecatedBlacklisted[bob] - this should be set to true in pre-v2.2 versions,
// and left untouched in v2.2+ versions.
v = parseInt(
await readSlot(
proxy.address,
addressMappingSlot(bob, STORAGE_SLOT_NUMBERS._deprecatedBlacklisted)
),
16
);
if (version >= 2.2) {
expect(v).to.equal(0);
} else {
expect(v).to.equal(1);
}
// _deprecatedBlacklisted[charlie] - this should be set to true in pre-v2.2 versions,
// and left untouched in v2.2+ versions.
v = parseInt(
await readSlot(
proxy.address,
addressMappingSlot(
charlie,
STORAGE_SLOT_NUMBERS._deprecatedBlacklisted
)
),
16
);
if (version >= 2.2) {
expect(v).to.equal(0);
} else {
expect(v).to.equal(1);
}
});
it("retains original storage slots for balanceAndBlacklistStates mapping", async () => {
// balanceAndBlacklistStates[alice] - not blacklisted, has balance
let v = parseUint(
await readSlot(
proxy.address,
addressMappingSlot(
alice,
STORAGE_SLOT_NUMBERS.balanceAndBlacklistStates
)
)
);
let expectedValue = mintedBN.sub(transferredBN);
expect(v.eq(expectedValue)).to.be.true;
// balanceAndBlacklistStates[bob] - blacklisted, has balance
v = parseUint(
await readSlot(
proxy.address,
addressMappingSlot(
bob,
STORAGE_SLOT_NUMBERS.balanceAndBlacklistStates
)
)
);
expectedValue =
version >= 2.2 ? POW_2_255_BN.add(transferredBN) : transferredBN;
expect(v.eq(expectedValue)).to.be.true;
// balanceAndBlacklistStates[charlie] - blacklisted, no balance
v = parseUint(
await readSlot(
proxy.address,
addressMappingSlot(
charlie,
STORAGE_SLOT_NUMBERS.balanceAndBlacklistStates
)
)
);
expectedValue = version >= 2.2 ? POW_2_255_BN : new BN(0);
expect(v.eq(expectedValue)).to.be.true;
});
it("retains original storage slots for allowed mapping", async () => {
// allowed[alice][bob]
let v = parseInt(
await readSlot(proxy.address, address2MappingSlot(alice, bob, 10)),
16
);
expect(v).to.equal(0);
// allowed[alice][charlie]
v = parseInt(
await readSlot(proxy.address, address2MappingSlot(alice, charlie, 10)),
16
);
expect(v).to.equal(allowance);
});
it("retains original storage slots for minters mapping", async () => {
// minters[minter]
let v = parseInt(
await readSlot(proxy.address, addressMappingSlot(minter, 12)),
16
);
expect(v).to.equal(1);
// minters[alice]
v = parseInt(
await readSlot(proxy.address, addressMappingSlot(alice, 12)),
16
);
expect(v).to.equal(0);
});
it("retains original storage slots for minterAllowed mapping", async () => {
// minterAllowed[minter]
let v = parseInt(
await readSlot(proxy.address, addressMappingSlot(minter, 13)),
16
);
expect(v).to.equal(mintAllowance - minted);
// minterAllowed[alice]
v = parseInt(
await readSlot(proxy.address, addressMappingSlot(alice, 13)),
16
);
expect(v).to.equal(0);
});
});
}
export async function readSlot(
address: string,
slot: number | string
): Promise<string> {
const data = await web3.eth.getStorageAt(
address,
slot as number // does support string, but type definition file is wrong
);
return data.replace(/^0x/, "");
}
function parseAddress(hex: string): string {
return web3.utils.toChecksumAddress(hex.padStart(40, "0"));
}
function parseString(hex: string): string {
const len = parseInt(hex.slice(-2), 16);
return Buffer.from(hex.slice(0, len), "hex").toString("utf8");
}
export function parseUint(hex: string): BN {
return new BN(hex, 16);
}
function encodeUint(value: number | BN): string {
return new BN(value).toString(16).padStart(64, "0");
}
function encodeAddress(addr: string): string {
return addr.replace(/^0x/, "").toLowerCase().padStart(64, "0");
}
export function addressMappingSlot(addr: string, pos: number): string {
return web3.utils.keccak256("0x" + encodeAddress(addr) + encodeUint(pos));
}
function address2MappingSlot(addr: string, addr2: string, pos: number): string {
return web3.utils.keccak256(
"0x" + encodeAddress(addr2) + addressMappingSlot(addr, pos).slice(2)
);
}