Skip to content

Commit ee73107

Browse files
abarmatpcarranzav
authored andcommitted
test: add a test for rewards distribution accrual with multiple allocations
1 parent 190f052 commit ee73107

File tree

1 file changed

+81
-23
lines changed

1 file changed

+81
-23
lines changed

test/rewards/rewards.test.ts

Lines changed: 81 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,15 @@ describe('Rewards', () => {
5050
let rewardsManagerMock: RewardsManagerMock
5151

5252
// Derive some channel keys for each indexer used to sign attestations
53-
const channelKey = deriveChannelKey()
53+
const channelKey1 = deriveChannelKey()
54+
const channelKey2 = deriveChannelKey()
5455

5556
const subgraphDeploymentID1 = randomHexBytes()
5657
const subgraphDeploymentID2 = randomHexBytes()
5758

58-
const allocationID = channelKey.address
59+
const allocationID1 = channelKey1.address
60+
const allocationID2 = channelKey2.address
61+
5962
const metadata = HashZero
6063

6164
const ISSUANCE_RATE_PERIODS = 4 // blocks required to issue 5% rewards
@@ -97,6 +100,10 @@ describe('Rewards', () => {
97100

98101
async accrued() {
99102
const nBlocks = await this.elapsedBlocks()
103+
return this.accruedByElapsed(nBlocks)
104+
}
105+
106+
async accruedByElapsed(nBlocks: BigNumber | number) {
100107
const n = getRewardsPerSignal(
101108
new BN(this.totalSupply.toString()),
102109
new BN(ISSUANCE_RATE_PER_BLOCK.toString()).div(1e18),
@@ -395,9 +402,9 @@ describe('Rewards', () => {
395402
indexer1.address,
396403
subgraphDeploymentID1,
397404
tokensToAllocate,
398-
allocationID,
405+
allocationID1,
399406
metadata,
400-
await channelKey.generateProof(indexer1.address),
407+
await channelKey1.generateProof(indexer1.address),
401408
)
402409

403410
// Jump
@@ -433,9 +440,9 @@ describe('Rewards', () => {
433440
indexer1.address,
434441
subgraphDeploymentID1,
435442
tokensToAllocate,
436-
allocationID,
443+
allocationID1,
437444
metadata,
438-
await channelKey.generateProof(indexer1.address),
445+
await channelKey1.generateProof(indexer1.address),
439446
)
440447

441448
// Jump
@@ -477,16 +484,16 @@ describe('Rewards', () => {
477484
indexer1.address,
478485
subgraphDeploymentID1,
479486
tokensToAllocate,
480-
allocationID,
487+
allocationID1,
481488
metadata,
482-
await channelKey.generateProof(indexer1.address),
489+
await channelKey1.generateProof(indexer1.address),
483490
)
484491

485492
// Jump
486493
await advanceBlocks(ISSUANCE_RATE_PERIODS)
487494

488495
// Rewards
489-
const contractRewards = await rewardsManager.getRewards(allocationID)
496+
const contractRewards = await rewardsManager.getRewards(allocationID1)
490497

491498
// We trust using this function in the test because we tested it
492499
// standalone in a previous test
@@ -523,9 +530,9 @@ describe('Rewards', () => {
523530
indexer1.address,
524531
subgraphDeploymentID1,
525532
tokensToAllocate,
526-
allocationID,
533+
allocationID1,
527534
metadata,
528-
await channelKey.generateProof(indexer1.address),
535+
await channelKey1.generateProof(indexer1.address),
529536
)
530537
}
531538

@@ -566,9 +573,9 @@ describe('Rewards', () => {
566573
indexer1.address,
567574
subgraphDeploymentID1,
568575
tokensToAllocate,
569-
allocationID,
576+
allocationID1,
570577
metadata,
571-
await channelKey.generateProof(indexer1.address),
578+
await channelKey1.generateProof(indexer1.address),
572579
)
573580
}
574581

@@ -599,11 +606,11 @@ describe('Rewards', () => {
599606
// Close allocation. At this point rewards should be collected for that indexer
600607
const tx = await staking
601608
.connect(indexer1.signer)
602-
.closeAllocation(allocationID, randomHexBytes())
609+
.closeAllocation(allocationID1, randomHexBytes())
603610
const receipt = await tx.wait()
604611
const event = rewardsManager.interface.parseLog(receipt.logs[1]).args
605612
expect(event.indexer).eq(indexer1.address)
606-
expect(event.allocationID).eq(allocationID)
613+
expect(event.allocationID1).eq(allocationID1)
607614
expect(event.epoch).eq(await epochManager.currentEpoch())
608615
expect(toRound(event.amount)).eq(toRound(expectedIndexingRewards))
609616

@@ -658,11 +665,11 @@ describe('Rewards', () => {
658665
// Close allocation. At this point rewards should be collected for that indexer
659666
const tx = await staking
660667
.connect(indexer1.signer)
661-
.closeAllocation(allocationID, randomHexBytes())
668+
.closeAllocation(allocationID1, randomHexBytes())
662669
const receipt = await tx.wait()
663670
const event = rewardsManager.interface.parseLog(receipt.logs[1]).args
664671
expect(event.indexer).eq(indexer1.address)
665-
expect(event.allocationID).eq(allocationID)
672+
expect(event.allocationID1).eq(allocationID1)
666673
expect(event.epoch).eq(await epochManager.currentEpoch())
667674
expect(toRound(event.amount)).eq(toRound(expectedIndexingRewards))
668675

@@ -710,7 +717,7 @@ describe('Rewards', () => {
710717
const beforeIndexer1Stake = await staking.getIndexerStakedTokens(indexer1.address)
711718

712719
// Close allocation. At this point rewards should be collected for that indexer
713-
await staking.connect(indexer1.signer).closeAllocation(allocationID, randomHexBytes())
720+
await staking.connect(indexer1.signer).closeAllocation(allocationID1, randomHexBytes())
714721

715722
// After state
716723
const afterTokenSupply = await grt.totalSupply()
@@ -756,10 +763,10 @@ describe('Rewards', () => {
756763
await advanceToNextEpoch(epochManager)
757764

758765
// Close allocation. At this point rewards should be collected for that indexer
759-
const tx = staking.connect(indexer1.signer).closeAllocation(allocationID, randomHexBytes())
766+
const tx = staking.connect(indexer1.signer).closeAllocation(allocationID1, randomHexBytes())
760767
await expect(tx)
761768
.emit(rewardsManager, 'RewardsDenied')
762-
.withArgs(indexer1.address, allocationID, await epochManager.currentEpoch())
769+
.withArgs(indexer1.address, allocationID1, await epochManager.currentEpoch())
763770
})
764771
})
765772
})
@@ -791,9 +798,9 @@ describe('Rewards', () => {
791798
indexer1.address,
792799
subgraphDeploymentID1,
793800
tokensToAllocate,
794-
allocationID,
801+
allocationID1,
795802
metadata,
796-
await channelKey.generateProof(indexer1.address),
803+
await channelKey1.generateProof(indexer1.address),
797804
)
798805

799806
// Jump
@@ -804,7 +811,58 @@ describe('Rewards', () => {
804811
await curation.connect(curator1.signer).burn(subgraphDeploymentID1, curatorShares, 0)
805812

806813
// Close allocation. At this point rewards should be collected for that indexer
807-
await staking.connect(indexer1.signer).closeAllocation(allocationID, randomHexBytes())
814+
await staking.connect(indexer1.signer).closeAllocation(allocationID1, randomHexBytes())
815+
})
816+
})
817+
818+
describe('multiple allocations', function () {
819+
it('two simultanous-similar allocations should get same amount of rewards', async function () {
820+
await advanceToNextEpoch(epochManager)
821+
822+
// Setup
823+
await epochManager.setEpochLength(10)
824+
825+
// Update total signalled
826+
const signalled1 = toGRT('1500')
827+
await curation.connect(curator1.signer).mint(subgraphDeploymentID1, signalled1, 0)
828+
829+
// Stake
830+
const tokensToStake = toGRT('12500')
831+
await staking.connect(indexer1.signer).stake(tokensToStake)
832+
833+
// Allocate simultaneously
834+
const tokensToAlloc = toGRT('5000')
835+
const tx1 = await staking.populateTransaction.allocateFrom(
836+
indexer1.address,
837+
subgraphDeploymentID1,
838+
tokensToAlloc,
839+
allocationID1,
840+
metadata,
841+
await channelKey1.generateProof(indexer1.address),
842+
)
843+
const tx2 = await staking.populateTransaction.allocateFrom(
844+
indexer1.address,
845+
subgraphDeploymentID1,
846+
tokensToAlloc,
847+
allocationID2,
848+
metadata,
849+
await channelKey2.generateProof(indexer1.address),
850+
)
851+
await staking.connect(indexer1.signer).multicall([tx1.data, tx2.data])
852+
853+
// Jump
854+
await advanceToNextEpoch(epochManager)
855+
856+
// Close allocations simultaneously
857+
const tx3 = await staking.populateTransaction.closeAllocation(allocationID1, randomHexBytes())
858+
const tx4 = await staking.populateTransaction.closeAllocation(allocationID2, randomHexBytes())
859+
const tx5 = await staking.connect(indexer1.signer).multicall([tx3.data, tx4.data])
860+
861+
// Both allocations should receive the same amount of rewards
862+
const receipt = await tx5.wait()
863+
const event1 = rewardsManager.interface.parseLog(receipt.logs[1]).args
864+
const event2 = rewardsManager.interface.parseLog(receipt.logs[5]).args
865+
expect(event1.amount).eq(event2.amount)
808866
})
809867
})
810868
})

0 commit comments

Comments
 (0)