-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathPoolUpdatesWithHints.js
452 lines (382 loc) · 14.4 KB
/
PoolUpdatesWithHints.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
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
import RPC from "../../utils/rpc"
import {ethers} from "hardhat"
import chai, {assert, expect} from "chai"
import {solidity} from "ethereum-waffle"
import setupIntegrationTest from "../helpers/setupIntegrationTest"
chai.use(solidity)
describe("PoolUpdatesWithHints", () => {
let rpc
let snapshotId
let signers
let controller
let bondingManager
let roundsManager
let token
let roundLength
// Default active set size is 10
let transcoders
let delegator
let newTranscoder
// Creates a full pool using the addresses in `accs`
// Upon creation, the pool ordering (descending from first position) is:
// (accs[0], accs.length) -> (accs[1], accs.length - 1) -> .. -> (accs[accs.length - 1], 1)
const createFullPool = async accs => {
let prevAcc = {address: ethers.constants.AddressZero}
let stake = accs.length
for (const acc of accs) {
await selfBond(acc, stake, prevAcc, ethers.constants.AddressZero)
prevAcc = acc
stake--
}
await roundsManager.mineBlocks(roundLength.toNumber())
await roundsManager.initializeRound()
}
const approve = async (delegator, amount) => {
await token.transfer(delegator.address, amount)
await token.connect(delegator).approve(bondingManager.address, amount)
}
const selfBond = async (delegator, amount, newPosPrev, newPosNext) => {
await approve(delegator, amount)
await bondingManager
.connect(delegator)
.bondWithHint(
amount,
delegator.address,
ethers.constants.AddressZero,
ethers.constants.AddressZero,
newPosPrev.address,
newPosNext
)
}
const transcoderAtPoolPos = async pos => {
const pool = await transcoderPool()
return pool[pos]
}
const transcoderPool = async () => {
const pool = []
let tr = await bondingManager.getFirstTranscoderInPool()
while (tr != ethers.constants.AddressZero) {
pool.push(tr)
tr = await bondingManager.getNextTranscoderInPool(tr)
}
return pool
}
before(async () => {
signers = await ethers.getSigners()
transcoders = signers.slice(0, 10)
delegator = signers[11]
newTranscoder = signers[12]
rpc = new RPC(web3)
const fixture = await setupIntegrationTest()
controller = await ethers.getContractAt(
"Controller",
fixture.Controller.address
)
bondingManager = await ethers.getContractAt(
"BondingManager",
fixture.BondingManager.address
)
roundsManager = await ethers.getContractAt(
"AdjustableRoundsManager",
fixture.AdjustableRoundsManager.address
)
token = await ethers.getContractAt(
"LivepeerToken",
fixture.LivepeerToken.address
)
roundLength = await roundsManager.roundLength()
await controller.unpause()
await createFullPool(transcoders)
})
beforeEach(async () => {
snapshotId = await rpc.snapshot()
})
afterEach(async () => {
await rpc.revert(snapshotId)
})
it("initial transcoder pool order should be correct", async () => {
const pool = await transcoderPool()
for (let i = 0; i < transcoders.length; i++) {
assert.equal(pool[i], transcoders[i].address)
}
})
it("transcoder calls reward with hint", async () => {
// All transcoders call reward() except for the last one
const size = transcoders.length - 1
const rewardTranscoders = transcoders.slice(0, size - 1)
for (const tr of rewardTranscoders) {
await bondingManager.connect(tr).reward()
}
const testSnapshotId = await rpc.snapshot()
// Get gas cost of reward()
const txResNoHint = await (
await bondingManager.connect(transcoders[size - 1]).reward()
).wait()
assert.equal(
await transcoderAtPoolPos(size - 1),
transcoders[size - 1].address
)
await rpc.revert(testSnapshotId)
// Get gas cost rewardWithHint()
const txResHint = await (
await bondingManager
.connect(transcoders[size - 1])
.rewardWithHint(
transcoders[size - 2].address,
ethers.constants.AddressZero
)
).wait()
assert.equal(
await transcoderAtPoolPos(size - 1),
transcoders[size - 1].address
)
// Gas cost of rewardWithHint() should be less than gas cost of reward()
assert.isBelow(
txResHint.cumulativeGasUsed,
txResNoHint.cumulativeGasUsed
)
})
it("new transcoder joins the pool", async () => {
const size = transcoders.length
await approve(transcoders[size - 2], 1)
await bondingManager
.connect(transcoders[size - 2])
.bond(1, transcoders[size - 2].address)
await approve(transcoders[size - 1], 1)
await bondingManager
.connect(transcoders[size - 1])
.bond(1, transcoders[size - 1].address)
// Not enough stake to join pool
await approve(newTranscoder, 2)
await bondingManager
.connect(newTranscoder)
.bond(2, newTranscoder.address)
// After this tx, the new transcoder should have enough stake to join pool
await bondingManager.connect(transcoders[size - 1]).unbond(1)
const dr = (await roundsManager.currentRound()).add(1)
const testSnapshotId = await rpc.snapshot()
// Pool ordering (descending)
// (transcoders[size - 4], 4) -> (transcoders[size - 2], 3) -> (transcoders[size - 3], 3) -> (transcoders[size - 1], 2)
// Get gas cost of transcoder()
let tx = await bondingManager.connect(newTranscoder).transcoder(0, 0)
const txResNoHint = await tx.wait()
assert.equal(await transcoderAtPoolPos(size - 1), newTranscoder.address)
await expect(tx)
.to.emit(bondingManager, "TranscoderDeactivated")
.withArgs(transcoders[size - 1].address, dr)
await rpc.revert(testSnapshotId)
// Get gas cost of transcoderWithHint()
tx = await bondingManager
.connect(newTranscoder)
.transcoderWithHint(
0,
0,
transcoders[size - 3].address,
ethers.constants.AddressZero
)
const txResHint = await tx.wait()
assert.equal(await transcoderAtPoolPos(size - 1), newTranscoder.address)
await expect(tx)
.to.emit(bondingManager, "TranscoderDeactivated")
.withArgs(transcoders[size - 1].address, dr)
// Gas cost of transcoderWithHint() should be less than gas cost of transcoder()
assert.isBelow(
txResHint.cumulativeGasUsed,
txResNoHint.cumulativeGasUsed
)
})
it("delegator bonds with hint", async () => {
const size = transcoders.length
await approve(delegator, 1)
const testSnapshotId = await rpc.snapshot()
// Pool ordering (descending)
// (transcoders[size - 4], 4) -> (transcoders[size - 3], 3) -> (transcoders[size - 2], 2) -> (transcoders[size - 1], 1)
// Get gas cost of bond()
const txResNoHint = await (
await bondingManager
.connect(delegator)
.bond(1, transcoders[size - 2].address)
).wait()
// transcoders[size - 2] should have moved up one position
assert.equal(
await transcoderAtPoolPos(size - 3),
transcoders[size - 2].address
)
await rpc.revert(testSnapshotId)
// Get gas cost of bondWithHint()
const txResHint = await (
await bondingManager
.connect(delegator)
.bondWithHint(
1,
transcoders[size - 2].address,
ethers.constants.AddressZero,
ethers.constants.AddressZero,
transcoders[size - 4].address,
transcoders[size - 3].address
)
).wait()
// transcoders[size - 2] should have moved up one position
assert.equal(
await transcoderAtPoolPos(size - 3),
transcoders[size - 2].address
)
// Gas cost of bondWithHint() should be less than gas cost of bond()
assert.isBelow(
txResHint.cumulativeGasUsed,
txResNoHint.cumulativeGasUsed
)
})
it("delegator changes delegation with hint", async () => {
const size = transcoders.length
await approve(delegator, 1)
await bondingManager
.connect(delegator)
.bond(1, transcoders[size - 2].address)
const testSnapshotId = await rpc.snapshot()
// Pool ordering (descending)
// Before:
// (transcoders[size - 4], 4) -> (transcoders[size - 2], 3) -> (transcoders[size - 3], 3) -> (transcoders[size - 1], 1)
// After (expected):
// (transcoders[size - 4], 4) -> (transcoders[size - 3], 3) -> (transcoders[size - 1], 2) -> (transcoders[size - 2], 2)
// Get gas cost of bond()
const txResNoHint = await (
await bondingManager
.connect(delegator)
.bond(0, transcoders[size - 1].address)
).wait()
assert.equal(
await transcoderAtPoolPos(size - 1),
transcoders[size - 2].address
)
assert.equal(
await transcoderAtPoolPos(size - 2),
transcoders[size - 1].address
)
await rpc.revert(testSnapshotId)
// Get gas cost of bondWithHint()
const txResHint = await (
await bondingManager
.connect(delegator)
.bondWithHint(
0,
transcoders[size - 1].address,
transcoders[size - 3].address,
transcoders[size - 1].address,
transcoders[size - 3].address,
transcoders[size - 2].address
)
).wait()
assert.equal(
await transcoderAtPoolPos(size - 1),
transcoders[size - 2].address
)
assert.equal(
await transcoderAtPoolPos(size - 2),
transcoders[size - 1].address
)
// Gas cost of bondWithHint() should be less than gas cost of bond()
assert.isBelow(
txResHint.cumulativeGasUsed,
txResNoHint.cumulativeGasUsed
)
})
it("transcoder partially unbonds and rebonds", async () => {
const size = transcoders.length
const testSnapshotId = await rpc.snapshot()
// Pool ordering (descending)
// Before:
// (transcoders[size - 4], 4) -> (transcoders[size - 3], 3) -> (transcoders[size - 2], 2) -> (transcoders[size - 1], 1)
const txResUnbondNoHint = await (
await bondingManager.connect(transcoders[size - 4]).unbond(2)
).wait()
// Should have dropped 1 position
assert.equal(
await transcoderAtPoolPos(size - 3),
transcoders[size - 4].address
)
const txResRebondNoHint = await (
await bondingManager.connect(transcoders[size - 4]).rebond(0)
).wait()
// Should have gained 1 position
assert.equal(
await transcoderAtPoolPos(size - 4),
transcoders[size - 4].address
)
await rpc.revert(testSnapshotId)
const txResUnbondHint = await (
await bondingManager
.connect(transcoders[size - 4])
.unbondWithHint(
2,
transcoders[size - 3].address,
transcoders[size - 2].address
)
).wait()
// Should have dropped 1 position
assert.equal(
await transcoderAtPoolPos(size - 3),
transcoders[size - 4].address
)
const txResRebondHint = await (
await bondingManager
.connect(transcoders[size - 4])
.rebondWithHint(
0,
transcoders[size - 5].address,
transcoders[size - 3].address
)
).wait()
// Should have gained 1 position
assert.equal(
await transcoderAtPoolPos(size - 4),
transcoders[size - 4].address
)
assert.isBelow(
txResUnbondHint.cumulativeGasUsed,
txResUnbondNoHint.cumulativeGasUsed
)
assert.isBelow(
txResRebondHint.cumulativeGasUsed,
txResRebondNoHint.cumulativeGasUsed
)
})
it("transcoder rebonds from unbonded", async () => {
const size = transcoders.length
await bondingManager.connect(transcoders[size - 4]).unbond(4)
const testSnapshotId = await rpc.snapshot()
// Pool ordering (descending)
// Before:
// (transcoders[size - 3], 3) -> (transcoders[size - 2], 2) -> (transcoders[size - 1], 1)
// After (expected):
// (transcoders[size - 4], 4) -> (transcoders[size - 3], 3) -> (transcoders[size - 2], 2) -> (transcoders[size - 1], 1)
const txResNoHint = await (
await bondingManager
.connect(transcoders[size - 4])
.rebondFromUnbonded(transcoders[size - 4].address, 0)
).wait()
assert.equal(
await transcoderAtPoolPos(size - 4),
transcoders[size - 4].address
)
await rpc.revert(testSnapshotId)
const txResHint = await (
await bondingManager
.connect(transcoders[size - 4])
.rebondFromUnbondedWithHint(
transcoders[size - 4].address,
0,
transcoders[size - 5].address,
transcoders[size - 3].address
)
).wait()
assert.equal(
await transcoderAtPoolPos(size - 4),
transcoders[size - 4].address
)
assert.isBelow(
txResHint.cumulativeGasUsed,
txResNoHint.cumulativeGasUsed
)
})
})