Skip to content

Commit dddd689

Browse files
authored
Merge pull request #180 from keep-network/misc-test-cleanup
Miscellaneous Cleanup This PR cleans up a round of feedback items from the following PRs: Annotate Rewards Tests #177 Sortition Tree Tests #179 Rewards Documentation #176
2 parents 5b75130 + 12bd114 commit dddd689

3 files changed

Lines changed: 57 additions & 56 deletions

File tree

docs/rewards.md

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ carry-over, which is stored in (2) `rewardRoundingDust`.
3636
Finally, the (3) `operatorRewards` keep track of each operator's individual
3737
state indexed by their `id`. An operator's `accumulated` value represents a
3838
snapshot of the `globalRewardAccumulator` at the time they were last updated.
39-
Their `available` value represents how much reward is available for withdraw.
40-
Their `weight` is their weight in the pool.
39+
Their `available` value represents how much reward is available for withdraw,
40+
as of their most recent update. Their `weight` is their weight in the pool.
4141

4242
To see how all of these pieces of state interact, we can go through some
4343
event logs.
4444

4545
#### Join -> Reward -> Withdraw
4646
```
47-
event 1) Alice (id 0) joins the pool with weight 10
47+
event 1) Alice (id 1) joins the pool with weight 10
4848
event 2) 123 rewards are granted to the pool
4949
event 3) Alice withdraws their rewards
5050
```
@@ -56,7 +56,7 @@ rewardRoundingDust: 0
5656
operatorRewards: {}
5757
```
5858

59-
Joining the pool is handled with `updateOperatorRewards(0, 10)` (some
59+
Joining the pool is handled with `updateOperatorRewards(1, 10)` (some
6060
complexity abridged to be introduced later)
6161
```
6262
function updateOperatorRewards(uint32 operator, uint32 newWeight) internal {
@@ -78,12 +78,12 @@ accruedRewards = (0 - 0) * 0 = 0
7878
o.available = 0 + 0 = 0
7979
o.accumulated = 0
8080
o.weight = 10
81-
operatorRewards: {0: {accumulated: 0, available: 0, weight: 10}}
81+
operatorRewards: {1: {accumulated: 0, available: 0, weight: 10}}
8282
8383
// new state
8484
globalRewardAccumulator: 0
8585
rewardRoundingDust: 0
86-
operatorRewards: {0: {accumulated: 0, available: 0, weight: 10}}
86+
operatorRewards: {1: {accumulated: 0, available: 0, weight: 10}}
8787
```
8888

8989
Ever time an operator is updated, they accrue rewards equal to however much the
@@ -118,7 +118,7 @@ rewardRoundingDust = 3
118118
// new state
119119
globalRewardAccumulator: 12
120120
rewardRoundingDust: 3
121-
operatorRewards: {0: {accumulated: 0, available: 0, weight: 10}}
121+
operatorRewards: {1: {accumulated: 0, available: 0, weight: 10}}
122122
```
123123

124124
Whenever rewards are distributed, we *only* update `globalRewardAccumulator`
@@ -127,7 +127,7 @@ lazily via `updateOperatorRewards`. Note that at this point, alice's
127127
accumulator is 12 rewards behind the global accumulator!
128128

129129
In order to Alice to withdraw her rewards, she shoud *update* herself first,
130-
since her `available` state is `0`. So, first we call `updateOperatorRewards(0, 10)`
130+
since her `available` state is `0`. So, first we call `updateOperatorRewards(1, 10)`
131131

132132
```
133133
acc = 12
@@ -136,12 +136,12 @@ accruedRewards = (12 - 0) * 10 = 120
136136
o.available = 0 + 120 = 120
137137
o.accumulated = 12
138138
o.weight = 10
139-
operatorRewards: {0: {accumulated: 12, available: 120, weight: 10}}
139+
operatorRewards: {1: {accumulated: 12, available: 120, weight: 10}}
140140
141141
// new state
142142
globalRewardAccumulator: 12
143143
rewardRoundingDust: 3
144-
operatorRewards: {0: {accumulated: 12, available: 120, weight: 10}}
144+
operatorRewards: {1: {accumulated: 12, available: 120, weight: 10}}
145145
```
146146

147147
Some amount of reward (an amount between 0 and the total operator weight) will
@@ -150,7 +150,7 @@ threshold, the weight precision is 1 weight = 1 T, (1e18 divisor), but rewards
150150
are represented with full precision (1e18), so numerically, even small rewards
151151
should greatly exceed the total pool weight.
152152

153-
Alice is now ready to withdraw! We call `withdrawOperatorRewards(0)`
153+
Alice is now ready to withdraw! We call `withdrawOperatorRewards(1)`
154154

155155
```
156156
function withdrawOperatorRewards(uint32 operator)
@@ -165,7 +165,7 @@ function withdrawOperatorRewards(uint32 operator)
165165
// new state
166166
globalRewardAccumulator: 12
167167
rewardRoundingDust: 3
168-
operatorRewards: {0: {accumulated: 12, available: 0, weight: 10}}
168+
operatorRewards: {1: {accumulated: 12, available: 0, weight: 10}}
169169
```
170170

171171
This returns `120` to the caller, and sets Alice's available rewards to 0. The
@@ -180,25 +180,25 @@ globalRewardAccumulator: 0
180180
rewardRoundingDust: 0
181181
operatorRewards: {}
182182
183-
event 2) Alice (id 0) joins the pool with weight 10
183+
event 2) Alice (id 1) joins the pool with weight 10
184184
globalRewardAccumulator: 0
185185
rewardRoundingDust: 0
186-
operatorRewards: {0: {accumulated: 0, available: 0, weight: 10}}
186+
operatorRewards: {1: {accumulated: 0, available: 0, weight: 10}}
187187
188188
event 3) 123 rewards are granted to the pool
189189
globalRewardAccumulator: 12
190190
rewardRoundingDust: 3
191-
operatorRewards: {0: {accumulated: 0, available: 0, weight: 10}}
191+
operatorRewards: {1: {accumulated: 0, available: 0, weight: 10}}
192192
193193
event 4) Update Alice
194194
globalRewardAccumulator: 12
195195
rewardRoundingDust: 3
196-
operatorRewards: {0: {accumulated: 12, available: 120, weight: 10}}
196+
operatorRewards: {1: {accumulated: 12, available: 120, weight: 10}}
197197
198198
event 5) Withdraw Alice's Rewards
199199
globalRewardAccumulator: 12
200200
rewardRoundingDust: 3
201-
operatorRewards: {0: {accumulated: 12, available: 0, weight: 10}}
201+
operatorRewards: {1: {accumulated: 12, available: 0, weight: 10}}
202202
return: 120
203203
```
204204

@@ -210,63 +210,63 @@ globalRewardAccumulator: 0
210210
rewardRoundingDust: 0
211211
operatorRewards: {}
212212
213-
event 2) Alice (id 0) joins the pool with weight 10
213+
event 2) Alice (id 1) joins the pool with weight 10
214214
globalRewardAccumulator: 0
215215
rewardRoundingDust: 0
216-
operatorRewards: {0: {accumulated: 0, available: 0, weight: 10}}
216+
operatorRewards: {1: {accumulated: 0, available: 0, weight: 10}}
217217
218218
event 3) 123 rewards are granted to the pool
219219
globalRewardAccumulator: 12
220220
rewardRoundingDust: 3
221-
operatorRewards: {0: {accumulated: 0, available: 0, weight: 10}}
221+
operatorRewards: {1: {accumulated: 0, available: 0, weight: 10}}
222222
223-
event 4) Bob (id 1) joins the pool with weight 20
223+
event 4) Bob (id 2) joins the pool with weight 20
224224
globalRewardAccumulator: 12
225225
rewardRoundingDust: 3
226226
operatorRewards: {
227-
0: {accumulated: 0, available: 0, weight: 10}
228-
1: {accumulated: 12, available: 0, weight: 20}
227+
1: {accumulated: 0, available: 0, weight: 10}
228+
2: {accumulated: 12, available: 0, weight: 20}
229229
}
230230
231231
event 5) 321 rewards are granted to the pool
232232
globalRewardAccumulator: 22
233233
rewardRoundingDust: 24
234234
operatorRewards: {
235-
0: {accumulated: 0, available: 0, weight: 10}
236-
1: {accumulated: 12, available: 0, weight: 20}
235+
1: {accumulated: 0, available: 0, weight: 10}
236+
2: {accumulated: 12, available: 0, weight: 20}
237237
}
238238
239239
event 6) Update Alice
240240
globalRewardAccumulator: 22
241241
rewardRoundingDust: 24
242242
operatorRewards: {
243-
0: {accumulated: 22, available: 220, weight: 10}
244-
1: {accumulated: 12, available: 0, weight: 20}
243+
1: {accumulated: 22, available: 220, weight: 10}
244+
2: {accumulated: 12, available: 0, weight: 20}
245245
}
246246
247247
event 7) Withdraw Alice's rewards
248248
globalRewardAccumulator: 22
249249
rewardRoundingDust: 24
250250
operatorRewards: {
251-
0: {accumulated: 22, available: 0, weight: 10}
252-
1: {accumulated: 12, available: 0, weight: 20}
251+
1: {accumulated: 22, available: 0, weight: 10}
252+
2: {accumulated: 12, available: 0, weight: 20}
253253
}
254254
return: 220
255255
256256
event 8) Update Bob
257257
globalRewardAccumulator: 22
258258
rewardRoundingDust: 24
259259
operatorRewards: {
260-
0: {accumulated: 22, available: 0, weight: 10}
261-
1: {accumulated: 22, available: 200, weight: 20}
260+
1: {accumulated: 22, available: 0, weight: 10}
261+
2: {accumulated: 22, available: 200, weight: 20}
262262
}
263263
264264
event 9) Withdraw Bob's Rewards
265265
globalRewardAccumulator: 22
266266
rewardRoundingDust: 24
267267
operatorRewards: {
268-
0: {accumulated: 22, available: 0, weight: 10}
269-
1: {accumulated: 22, available: 0, weight: 20}
268+
1: {accumulated: 22, available: 0, weight: 10}
269+
2: {accumulated: 22, available: 0, weight: 20}
270270
}
271271
return 200
272272
```
@@ -350,61 +350,61 @@ rewardRoundingDust: 0
350350
ineligibleEarnedRewards: 0
351351
operatorRewards: {}
352352
353-
event 2) Alice (id 0) joins the pool with weight 10
353+
event 2) Alice (id 1) joins the pool with weight 10
354354
globalRewardAccumulator: 0
355355
rewardRoundingDust: 0
356356
ineligibleEarnedRewards: 0
357-
operatorRewards: {0: {accumulated: 0, available: 0, weight: 10, ineligibleUntil: 0}}
357+
operatorRewards: {1: {accumulated: 0, available: 0, weight: 10, ineligibleUntil: 0}}
358358
359359
event 3) 123 rewards are granted to the pool
360360
globalRewardAccumulator: 12
361361
rewardRoundingDust: 3
362362
ineligibleEarnedRewards: 0
363-
operatorRewards: {0: {accumulated: 0, available: 0, weight: 10, ineligibleUntil: 0}}
363+
operatorRewards: {1: {accumulated: 0, available: 0, weight: 10, ineligibleUntil: 0}}
364364
365-
event 4) Bob (id 1) joins the pool with weight 20
365+
event 4) Bob (id 2) joins the pool with weight 20
366366
globalRewardAccumulator: 12
367367
rewardRoundingDust: 3
368368
ineligibleEarnedRewards: 0
369369
operatorRewards: {
370-
0: {accumulated: 0, available: 0, weight: 10, ineligibleUntil: 0}
371-
1: {accumulated: 12, available: 0, weight: 20, ineligibleUntil: 0}
370+
1: {accumulated: 0, available: 0, weight: 10, ineligibleUntil: 0}
371+
2: {accumulated: 12, available: 0, weight: 20, ineligibleUntil: 0}
372372
}
373373
374374
event 5) Bob is set as ineligable until 100000 seconds from now
375375
globalRewardAccumulator: 12
376376
rewardRoundingDust: 3
377377
ineligibleEarnedRewards: 0
378378
operatorRewards: {
379-
0: {accumulated: 0, available: 0, weight: 10, ineligibleUntil: 0}
380-
1: {accumulated: 12, available: 0, weight: 20, ineligibleUntil: event5-time + 10000}
379+
1: {accumulated: 0, available: 0, weight: 10, ineligibleUntil: 0}
380+
2: {accumulated: 12, available: 0, weight: 20, ineligibleUntil: event5-time + 10000}
381381
}
382382
383383
event 6) 321 rewards are granted to the pool
384384
globalRewardAccumulator: 22
385385
rewardRoundingDust: 24
386386
ineligibleEarnedRewards: 0
387387
operatorRewards: {
388-
0: {accumulated: 0, available: 0, weight: 10, ineligibleUntil: 0}
389-
1: {accumulated: 12, available: 0, weight: 20, ineligibleUntil: event5-time + 10000}
388+
1: {accumulated: 0, available: 0, weight: 10, ineligibleUntil: 0}
389+
2: {accumulated: 12, available: 0, weight: 20, ineligibleUntil: event5-time + 10000}
390390
}
391391
392392
event 7) Update Alice
393393
globalRewardAccumulator: 22
394394
rewardRoundingDust: 24
395395
ineligibleEarnedRewards: 0
396396
operatorRewards: {
397-
0: {accumulated: 22, available: 220, weight: 10, ineligibleUntil: 0}
398-
1: {accumulated: 12, available: 0, weight: 20, ineligibleUntil: event5-time + 10000}
397+
1: {accumulated: 22, available: 220, weight: 10, ineligibleUntil: 0}
398+
2: {accumulated: 12, available: 0, weight: 20, ineligibleUntil: event5-time + 10000}
399399
}
400400
401401
event 8) Withdraw Alice's rewards
402402
globalRewardAccumulator: 22
403403
rewardRoundingDust: 24
404404
ineligibleEarnedRewards: 0
405405
operatorRewards: {
406-
0: {accumulated: 22, available: 0, weight: 10, ineligibleUntil: 0}
407-
1: {accumulated: 12, available: 0, weight: 20, ineligibleUntil: event5-time + 10000}
406+
1: {accumulated: 22, available: 0, weight: 10, ineligibleUntil: 0}
407+
2: {accumulated: 12, available: 0, weight: 20, ineligibleUntil: event5-time + 10000}
408408
}
409409
return: 220
410410
@@ -413,17 +413,17 @@ globalRewardAccumulator: 22
413413
rewardRoundingDust: 24
414414
ineligibleEarnedRewards: 200
415415
operatorRewards: {
416-
0: {accumulated: 22, available: 0, weight: 10, ineligibleUntil: 0}
417-
1: {accumulated: 22, available: 0, weight: 20, ineligibleUntil: event5-time + 10000}
416+
1: {accumulated: 22, available: 0, weight: 10, ineligibleUntil: 0}
417+
2: {accumulated: 22, available: 0, weight: 20, ineligibleUntil: event5-time + 10000}
418418
}
419419
420420
event 10) Withdraw Ineligable Rewards
421421
globalRewardAccumulator: 22
422422
rewardRoundingDust: 24
423423
ineligibleEarnedRewards: 0
424424
operatorRewards: {
425-
0: {accumulated: 22, available: 0, weight: 10, ineligibleUntil: 0}
426-
1: {accumulated: 22, available: 0, weight: 20, ineligibleUntil: event5-time + 10000}
425+
1: {accumulated: 22, available: 0, weight: 10, ineligibleUntil: 0}
426+
2: {accumulated: 22, available: 0, weight: 20, ineligibleUntil: event5-time + 10000}
427427
}
428428
return: 200
429429
```

test/rewardsTest.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,15 +245,16 @@ describe("Rewards", () => {
245245
// Alice: 10; Bob: 90
246246
await rewards.payReward(100)
247247

248-
// Make Bob ineligible for 10 units of time
248+
// Make Bob ineligible for 10 seconds
249249
await rewards.makeIneligible(bob, 10)
250250

251251
// Reward only to Alice
252252
// Alice: 20; Bob: 90; Ineligible: 90
253253
await rewards.payReward(100)
254254

255-
// Ineligibility is set for a duration. Bob was ineligible for 10 units,
256-
// so we move forward 11 units to allow us to make him eligible again.
255+
// Ineligibility is set for a duration. Bob was ineligible for 10
256+
// seconds, so we move forward 11 seconds to allow us to make him
257+
// eligible again.
257258
await helpers.time.increaseTime(11)
258259

259260
await rewards.makeEligible(bob)

test/sortitionTreeTest.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ describe("SortitionTree", () => {
1919

2020
describe("setLeaf", async () => {
2121
context("when one leaf is set", () => {
22-
it("should return correct value for the tree", async () => {
22+
it("should return correct value for the tree with a leaf in the first slot", async () => {
2323
const weight = 0x1234
2424
const position = 42798
2525

@@ -128,7 +128,7 @@ describe("SortitionTree", () => {
128128
// 0x00000000,00000000,00000000,00000000,00000000,00000000,00000000,1000fff1
129129
// slot 7 slot 6 slot 5 slot 4 slot 3 slot 2 slot 1 slot 0
130130
// without the commas added for readability. All the padding zeros are
131-
// dropped when we hexlify, which simplifies to 0x1100000000.
131+
// dropped when we hexlify, which simplifies to 0x1000fff1.
132132
})
133133
})
134134

0 commit comments

Comments
 (0)