-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathManagerProxy.js
More file actions
371 lines (316 loc) · 13 KB
/
ManagerProxy.js
File metadata and controls
371 lines (316 loc) · 13 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
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
import Fixture from "./helpers/Fixture"
import {contractId} from "../../utils/helpers"
import {toBuffer, bufferToHex} from "ethereumjs-util"
import ethAbi from "ethereumjs-abi"
import {web3, ethers} from "hardhat"
import chai, {expect, assert} from "chai"
import {solidity} from "ethereum-waffle"
chai.use(solidity)
describe("ManagerProxy", () => {
let fixture
let managerProxy
let managerProxyFac
let signers
describe("constructor", () => {
it("should create contract", async () => {
const targetContractId = contractId("ManagerProxyTarget")
const proxy = await managerProxyFac.deploy(
signers[0].address,
targetContractId
)
assert.equal(
await proxy.controller(),
signers[0].address,
"should set Controller address"
)
assert.equal(
await proxy.targetContractId(),
targetContractId,
"should set target contract ID"
)
})
})
before(async () => {
signers = await ethers.getSigners()
fixture = new Fixture(ethers.provider)
await fixture.deploy()
await fixture.deployAndRegister(
await ethers.getContractFactory("ManagerProxyTargetMockV1"),
"ManagerProxyTarget",
fixture.controller.address
)
managerProxyFac = await ethers.getContractFactory("ManagerProxy")
const proxy = await managerProxyFac.deploy(
fixture.controller.address,
contractId("ManagerProxyTarget")
)
managerProxy = await ethers.getContractAt(
"ManagerProxyTargetMockV1",
proxy.address
)
})
beforeEach(async () => {
await fixture.setUp()
})
afterEach(async () => {
await fixture.tearDown()
})
describe("receive", () => {
describe("target implementation implements receive()", async () => {
it("should receive ETH", async () => {
const amount = ethers.utils.parseUnits("1", "ether")
const tx = await signers[0].sendTransaction({
to: managerProxy.address,
value: amount
})
await expect(tx).to.changeEtherBalance(managerProxy, amount)
})
})
describe("target implementation does not implement receive()", async () => {
it("should revert", async () => {
// Does not implement receive()
await fixture.deployAndRegister(
await ethers.getContractFactory("ManagerProxyTargetMockV2"),
"ManagerProxyTarget",
fixture.controller.address
)
const amount = ethers.utils.parseUnits("1", "ether")
const tx = signers[0].sendTransaction({
to: managerProxy.address,
value: amount
})
await expect(tx).to.be.reverted
})
})
})
describe("fallback function", () => {
it("should fail if there is no valid contract address registered with the Controller for the target contract ID", async () => {
const newProxy = await managerProxyFac.deploy(
fixture.controller.address,
contractId("foo")
)
const target = await ethers.getContractAt(
"ManagerProxyTargetMockV1",
newProxy.address
)
await expect(target.setUint64(5)).to.be.reverted
})
describe("setting and getting uint8", () => {
it("should set a uint8", async () => {
await managerProxy.setUint8(4)
const value = await managerProxy.uint8Value()
assert.equal(value, 4, "uint8 value incorrect")
})
})
describe("setting and getting uint64", () => {
it("should set a uint64", async () => {
await managerProxy.setUint64(5)
const value = await managerProxy.uint64Value()
assert.equal(value, 5, "uint64 value incorrect")
})
})
describe("setting and getting uint256", () => {
it("should set a uint256", async () => {
await managerProxy.setUint256(6)
const value = await managerProxy.uint256Value()
assert.equal(value, 6, "uint256 value incorrect")
})
})
describe("setting and getting bytes32", () => {
const hash = web3.utils.sha3("hello")
it("should set a bytes32", async () => {
await managerProxy.setBytes32(hash)
const value = await managerProxy.bytes32Value()
assert.equal(value, hash, "bytes32 value incorrect")
})
})
describe("setting and getting address", () => {
it("should set an address", async () => {
const addr = signers[1].address
await managerProxy.setAddress(addr)
const value = await managerProxy.addressValue()
assert.equal(value, addr, "address value incorrect")
})
})
describe("setting and getting string", () => {
const str = "hello"
it("should set a string", async () => {
await managerProxy.setString(str)
const value = await managerProxy.stringValue()
assert.equal(value, str, "string value incorrect")
})
})
describe("setting and getting bytes", () => {
const h = web3.utils.sha3("hello")
it("should set a bytes", async () => {
await managerProxy.setBytes(h)
const value = await managerProxy.bytesValue()
assert.equal(value, h, "bytes value incorrect")
})
})
describe("setting and getting a tuple", () => {
const v1 = 5
const v2 = 6
const v3 = web3.utils.sha3("hello")
it("should set a tuple", async () => {
await managerProxy.setTuple(v1, v2, v3)
const values = await managerProxy.getTuple()
assert.equal(values[0], v1, "tuple value 1 incorrect")
assert.equal(values[1], v2, "tuple value 2 incorrect")
assert.equal(values[2], v3, "tuple value 3 incorrect")
})
})
})
describe("non-storage upgrade", () => {
beforeEach(async () => {
await managerProxy.setUint8(4)
await managerProxy.setUint64(5)
await managerProxy.setUint256(6)
await managerProxy.setBytes32(web3.utils.sha3("hello"))
await managerProxy.setAddress(signers[1].address)
})
it("should preserve state in proxy contract", async () => {
await fixture.deployAndRegister(
await ethers.getContractFactory("ManagerProxyTargetMockV2"),
"ManagerProxyTarget",
fixture.controller.address
)
const uint8Value = await managerProxy.uint8Value()
assert.equal(uint8Value, 4, "uint8 value incorrect")
const uint64Value = await managerProxy.uint64Value()
assert.equal(uint64Value, 5, "uint64 value incorrect")
const uint256Value = await managerProxy.uint256Value()
assert.equal(uint256Value, 6, "uint256 value incorrect")
const bytes32Value = await managerProxy.bytes32Value()
assert.equal(
bytes32Value,
web3.utils.sha3("hello"),
"bytes32 value incorrect"
)
const addressValue = await managerProxy.addressValue()
assert.equal(
addressValue,
signers[1].address,
"address value incorrect"
)
})
it("should set a uint8 and add 5", async () => {
await fixture.deployAndRegister(
await ethers.getContractFactory("ManagerProxyTargetMockV2"),
"ManagerProxyTarget",
fixture.controller.address
)
await managerProxy.setUint8(10)
const value = await managerProxy.uint8Value()
assert.equal(value, 10 + 5, "uint8 value incorrect")
})
it("should set a uint64 and add 5", async () => {
await fixture.deployAndRegister(
await ethers.getContractFactory("ManagerProxyTargetMockV2"),
"ManagerProxyTarget",
fixture.controller.address
)
await managerProxy.setUint64(10)
const value = await managerProxy.uint64Value()
assert.equal(value, 10 + 5, "uint64 value incorrect")
})
it("should set a uint256 and add 5", async () => {
await fixture.deployAndRegister(
await ethers.getContractFactory("ManagerProxyTargetMockV2"),
"ManagerProxyTarget",
fixture.controller.address
)
await managerProxy.setUint256(10)
const value = await managerProxy.uint256Value()
assert.equal(value, 10 + 5, "uint256 value incorrect")
})
it("should set a hashed bytes32", async () => {
await fixture.deployAndRegister(
await ethers.getContractFactory("ManagerProxyTargetMockV2"),
"ManagerProxyTarget",
fixture.controller.address
)
await managerProxy.setBytes32(web3.utils.sha3("bye"))
const value = await managerProxy.bytes32Value()
assert.equal(
value,
bufferToHex(
ethAbi.soliditySHA3(
["bytes"],
[toBuffer(web3.utils.sha3("bye"))]
)
),
"bytes32 value incorrect"
)
})
it("should set a null address", async () => {
await fixture.deployAndRegister(
await ethers.getContractFactory("ManagerProxyTargetMockV2"),
"ManagerProxyTarget",
fixture.controller.address
)
await managerProxy.setAddress(signers[1].address)
const value = await managerProxy.addressValue()
assert.equal(
value,
"0x0000000000000000000000000000000000000000",
"address value incorrect"
)
})
})
describe("storage upgrade with superset of original storage variables", () => {
beforeEach(async () => {
await managerProxy.setUint8(4)
await managerProxy.setUint64(5)
await managerProxy.setUint256(6)
await managerProxy.setBytes32(web3.utils.sha3("hello"))
await managerProxy.setAddress(signers[1].address)
})
it("should set a key value pair in mapping", async () => {
await fixture.deployAndRegister(
await ethers.getContractFactory("ManagerProxyTargetMockV3"),
"ManagerProxyTarget",
fixture.controller.address
)
// Need new contract binding since we added a new method
const managerProxyV3 = await ethers.getContractAt(
"ManagerProxyTargetMockV3",
managerProxy.address
)
await managerProxyV3.setKv(5, 6)
const value = await managerProxyV3.kvMap(5)
assert.equal(value, 6, "value for key incorrect")
})
it("should preserve old state in proxy contract after an update to a new storage variable", async () => {
await fixture.deployAndRegister(
await ethers.getContractFactory("ManagerProxyTargetMockV3"),
"ManagerProxyTarget",
fixture.controller.address
)
// Need new contract binding since we added a new method
const managerProxyV3 = await ethers.getContractAt(
"ManagerProxyTargetMockV3",
managerProxy.address
)
await managerProxyV3.setKv(5, 6)
const uint8Value = await managerProxy.uint8Value()
assert.equal(uint8Value, 4, "uint8 value incorrect")
const uint64Value = await managerProxy.uint64Value()
assert.equal(uint64Value, 5, "uint64 value incorrect")
const uint256Value = await managerProxy.uint256Value()
assert.equal(uint256Value, 6, "uint256 value incorrect")
const bytes32Value = await managerProxy.bytes32Value()
assert.equal(
bytes32Value,
web3.utils.sha3("hello"),
"bytes32 value incorrect"
)
const addressValue = await managerProxy.addressValue()
assert.equal(
addressValue,
signers[1].address,
"address value incorrect"
)
})
})
})