@@ -3,17 +3,13 @@ const expect = chai.expect
33const { ethers, helpers } = require ( "hardhat" )
44
55describe ( "Rewards" , ( ) => {
6- let alice
7- let bob
8- let carol
6+ const alice = 1
7+ const bob = 2
8+ const carol = 3
99
1010 let rewards
1111
1212 beforeEach ( async ( ) => {
13- alice = 1
14- bob = 2
15- carol = 3
16-
1713 const RewardsStub = await ethers . getContractFactory ( "RewardsStub" )
1814 rewards = await RewardsStub . deploy ( )
1915 await rewards . deployed ( )
@@ -35,7 +31,11 @@ describe("Rewards", () => {
3531 await rewards . withdrawRewards ( bob )
3632 const bobRewards = await rewards . getWithdrawnRewards ( bob )
3733
34+ // Since alice makes up 10 / 100 = 10% of the pool weight, alice expects
35+ // 10% of the rewards. 10% of 1000 is 100.
3836 expect ( aliceRewards ) . to . be . equal ( 100 )
37+ // Since bob makes up 90 / 100 = 90% of the pool weight, bob expects 90%
38+ // of the rewards. 90% of 1000 is 900.
3939 expect ( bobRewards ) . to . be . equal ( 900 )
4040 } )
4141
@@ -52,7 +52,13 @@ describe("Rewards", () => {
5252 await rewards . withdrawRewards ( bob )
5353 const bobRewards = await rewards . getWithdrawnRewards ( bob )
5454
55+ // Since alice made up 10 / 10 = 100% of the pool weight (as bob's weight
56+ // was updated to 0 before rewards were paid), alice expects 100% of
57+ // the rewards. 100% of 1000 is 1000.
5558 expect ( aliceRewards ) . to . equal ( 1000 )
59+ // Since bob made up 0 / 10 = 0% of the pool weight (as bob's weight was
60+ // updated to 0 before rewards were paid), bob expects 0% of the
61+ // rewards. 0% of 1000 is 0.
5662 expect ( bobRewards ) . to . equal ( 0 )
5763 } )
5864
@@ -61,6 +67,13 @@ describe("Rewards", () => {
6167
6268 await rewards . payReward ( 123 )
6369
70+ // The way that reward state is tracked is through a global accumulator
71+ // that simulates what a hypothetical 1-weight operator would receive in
72+ // rewards for each payout, accompanied by roundingDust variable that
73+ // stores integer division remainders. For this example, the pool has 10
74+ // weight, and a reward of 123 was just granted, so a 1-weight operator
75+ // would receive 12 rewards with 3 left over (12 * 10 + 3 = 123). The
76+ // remainder is added to the next reward.
6477 const acc1 = await rewards . getGlobalAccumulator ( )
6578 expect ( acc1 ) . to . equal ( 12 )
6679
@@ -71,6 +84,10 @@ describe("Rewards", () => {
7184
7285 await rewards . payReward ( 987 )
7386
87+ // We previously had a remainder of 3, and just received 987 rewards for
88+ // a total of 990. The total pool weight is 30, so our 1-weight operator
89+ // receives 990 / 30 = 33 rewards with 0 left over. That 33 is added to
90+ // the previous 12 to get to 45.
7491 const acc2 = await rewards . getGlobalAccumulator ( )
7592 expect ( acc2 ) . to . equal ( 45 )
7693
@@ -106,9 +123,12 @@ describe("Rewards", () => {
106123 const bobAcc1 = await rewards . getAccumulator ( bob )
107124 expect ( bobAcc1 ) . to . equal ( 10 )
108125
126+ // Accrued rewards only change when an operator is updated.
109127 const aliceRew1 = await rewards . getAccruedRewards ( alice )
110128 expect ( aliceRew1 ) . to . equal ( 0 )
111129
130+ // An operator's accumulator state only changes when an operator is
131+ // updated.
112132 const aliceAcc1 = await rewards . getAccumulator ( alice )
113133 expect ( aliceAcc1 ) . to . equal ( 0 )
114134
@@ -225,12 +245,15 @@ describe("Rewards", () => {
225245 // Alice: 10; Bob: 90
226246 await rewards . payReward ( 100 )
227247
248+ // Make Bob ineligible for 10 units of time
228249 await rewards . makeIneligible ( bob , 10 )
229250
230251 // Reward only to Alice
231252 // Alice: 20; Bob: 90; Ineligible: 90
232253 await rewards . payReward ( 100 )
233254
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.
234257 await helpers . time . increaseTime ( 11 )
235258
236259 await rewards . makeEligible ( bob )
0 commit comments