Skip to content

Commit e38e530

Browse files
committed
Make block/epoch alignment in tests more predictable
1 parent 5faa832 commit e38e530

File tree

2 files changed

+42
-14
lines changed

2 files changed

+42
-14
lines changed

test/lib/testHelpers.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,13 @@ export const advanceBlocks = async (blocks: string | number | BigNumber): Promis
7474
const steps = typeof blocks === 'number' || typeof blocks === 'string' ? toBN(blocks) : blocks
7575
const currentBlock = await latestBlock()
7676
const toBlock = currentBlock.add(steps)
77-
await advanceBlockTo(toBlock)
77+
return advanceBlockTo(toBlock)
7878
}
7979

8080
export const advanceToNextEpoch = async (epochManager: EpochManager): Promise<void> => {
81-
const currentBlock = await latestBlock()
82-
const epochLength = await epochManager.epochLength()
83-
const nextEpochBlock = currentBlock.add(epochLength)
84-
await advanceBlockTo(nextEpochBlock)
81+
const blocksSinceEpoch = await epochManager.currentEpochBlockSinceStart()
82+
const epochLen = await epochManager.epochLength()
83+
return advanceBlocks(epochLen.sub(blocksSinceEpoch))
8584
}
8685

8786
export const evmSnapshot = async (): Promise<number> => provider().send('evm_snapshot', [])

test/rewards/rewards.test.ts

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -573,19 +573,29 @@ describe('Rewards', () => {
573573
}
574574

575575
it('should distribute rewards on closed allocation and stake', async function () {
576+
577+
// Align with the epoch boundary
578+
await advanceToNextEpoch(epochManager)
576579
// Setup
577580
await setupIndexerAllocation()
578581

579582
// Jump
580-
await advanceBlocks(await epochManager.epochLength())
583+
await advanceToNextEpoch(epochManager)
581584

582585
// Before state
583586
const beforeTokenSupply = await grt.totalSupply()
584587
const beforeIndexer1Stake = await staking.getIndexerStakedTokens(indexer1.address)
585588
const beforeIndexer1Balance = await grt.balanceOf(indexer1.address)
586589
const beforeStakingBalance = await grt.balanceOf(staking.address)
587590

588-
const expectedIndexingRewards = toGRT('1471954234')
591+
// All the rewards in this subgraph go to this allocation.
592+
// Rewards per token will be (totalSupply * issuanceRate^nBlocks - totalSupply) / allocatedTokens
593+
// The first snapshot is after allocating, that is 2 blocks after the signal is minted:
594+
// startRewardsPerToken = (10004000000 * 1.01227 ^ 2 - 10004000000) / 12500 = 122945.16
595+
// The final snapshot is when we close the allocation, that happens 9 blocks later:
596+
// endRewardsPerToken = (10004000000 * 1.01227 ^ 9 - 10004000000) / 12500 = 92861.24
597+
// Then our expected rewards are (endRewardsPerToken - startRewardsPerToken) * 12500.
598+
const expectedIndexingRewards = toGRT('913715958')
589599

590600
// Close allocation. At this point rewards should be collected for that indexer
591601
const tx = await staking
@@ -623,19 +633,28 @@ describe('Rewards', () => {
623633
const destinationAddress = randomHexBytes(20)
624634
await staking.connect(indexer1.signer).setRewardsDestination(destinationAddress)
625635

636+
// Align with the epoch boundary
637+
await advanceToNextEpoch(epochManager)
626638
// Setup
627639
await setupIndexerAllocation()
628640

629641
// Jump
630-
await advanceBlocks(await epochManager.epochLength())
642+
await advanceToNextEpoch(epochManager)
631643

632644
// Before state
633645
const beforeTokenSupply = await grt.totalSupply()
634646
const beforeIndexer1Stake = await staking.getIndexerStakedTokens(indexer1.address)
635647
const beforeDestinationBalance = await grt.balanceOf(destinationAddress)
636648
const beforeStakingBalance = await grt.balanceOf(staking.address)
637649

638-
const expectedIndexingRewards = toGRT('1471954234')
650+
// All the rewards in this subgraph go to this allocation.
651+
// Rewards per token will be (totalSupply * issuanceRate^nBlocks - totalSupply) / allocatedTokens
652+
// The first snapshot is after allocating, that is 2 blocks after the signal is minted:
653+
// startRewardsPerToken = (10004000000 * 1.01227 ^ 2 - 10004000000) / 12500 = 122945.16
654+
// The final snapshot is when we close the allocation, that happens 9 blocks later:
655+
// endRewardsPerToken = (10004000000 * 1.01227 ^ 9 - 10004000000) / 12500 = 92861.24
656+
// Then our expected rewards are (endRewardsPerToken - startRewardsPerToken) * 12500.
657+
const expectedIndexingRewards = toGRT('913715958')
639658

640659
// Close allocation. At this point rewards should be collected for that indexer
641660
const tx = await staking
@@ -678,10 +697,13 @@ describe('Rewards', () => {
678697
}
679698
const tokensToDelegate = toGRT('2000')
680699

700+
// Align with the epoch boundary
701+
await advanceToNextEpoch(epochManager)
702+
// Setup the allocation and delegators
681703
await setupIndexerAllocationWithDelegation(tokensToDelegate, delegationParams)
682704

683705
// Jump
684-
await advanceBlocks(await epochManager.epochLength())
706+
await advanceToNextEpoch(epochManager)
685707

686708
// Before state
687709
const beforeTokenSupply = await grt.totalSupply()
@@ -698,8 +720,15 @@ describe('Rewards', () => {
698720

699721
// Check that rewards are put into indexer stake (only indexer cut)
700722
// Check that rewards are put into delegators pool accordingly
701-
// NOTE: calculated manually on a spreadsheet
702-
const expectedIndexingRewards = toGRT('1454109066')
723+
724+
// All the rewards in this subgraph go to this allocation.
725+
// Rewards per token will be (totalSupply * issuanceRate^nBlocks - totalSupply) / allocatedTokens
726+
// The first snapshot is after allocating, that is 2 blocks after the signal is minted:
727+
// startRewardsPerToken = (10004000000 * 1.01227 ^ 2 - 10004000000) / 14500 = 8466.995
728+
// The final snapshot is when we close the allocation, that happens 4 blocks later:
729+
// endRewardsPerToken = (10004000000 * 1.01227 ^ 4 - 10004000000) / 14500 = 34496.55
730+
// Then our expected rewards are (endRewardsPerToken - startRewardsPerToken) * 14500.
731+
const expectedIndexingRewards = toGRT('377428566.77')
703732
// Calculate delegators cut
704733
const indexerRewards = delegationParams.indexingRewardCut
705734
.mul(expectedIndexingRewards)
@@ -725,7 +754,7 @@ describe('Rewards', () => {
725754
await setupIndexerAllocation()
726755

727756
// Jump
728-
await advanceBlocks(await epochManager.epochLength())
757+
await advanceToNextEpoch(epochManager)
729758

730759
// Close allocation. At this point rewards should be collected for that indexer
731760
const tx = staking.connect(indexer1.signer).closeAllocation(allocationID, randomHexBytes())
@@ -769,7 +798,7 @@ describe('Rewards', () => {
769798
)
770799

771800
// Jump
772-
await advanceBlocks(await epochManager.epochLength())
801+
await advanceToNextEpoch(epochManager)
773802

774803
// Remove all signal from the subgraph
775804
const curatorShares = await curation.getCuratorSignal(curator1.address, subgraphDeploymentID1)

0 commit comments

Comments
 (0)