Skip to content

Commit 69dcc9a

Browse files
authored
Merge pull request #640 from graphprotocol/pcv/rewards-underflow
fix: prevent underflow when subgraphs go under the minimum signal threshold
2 parents f9deb7b + 0ec8778 commit 69dcc9a

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

contracts/rewards/RewardsManager.sol

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ pragma abicoder v2;
66
import "@openzeppelin/contracts/math/SafeMath.sol";
77

88
import "../upgrades/GraphUpgradeable.sol";
9+
import "../staking/libs/MathUtils.sol";
910

1011
import "./RewardsManagerStorage.sol";
1112
import "./IRewardsManager.sol";
@@ -281,7 +282,8 @@ contract RewardsManager is RewardsManagerV3Storage, GraphUpgradeable, IRewardsMa
281282
Subgraph storage subgraph = subgraphs[_subgraphDeploymentID];
282283

283284
uint256 accRewardsForSubgraph = getAccRewardsForSubgraph(_subgraphDeploymentID);
284-
uint256 newRewardsForSubgraph = accRewardsForSubgraph.sub(
285+
uint256 newRewardsForSubgraph = MathUtils.diffOrZero(
286+
accRewardsForSubgraph,
285287
subgraph.accRewardsForSubgraphSnapshot
286288
);
287289

test/rewards/rewards.test.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,29 @@ describe('Rewards', () => {
537537
)
538538
}
539539

540+
async function setupIndexerAllocationSignalingAfter() {
541+
// Setup
542+
await epochManager.setEpochLength(10)
543+
544+
// Allocate
545+
const tokensToAllocate = toGRT('12500')
546+
await staking.connect(indexer1.signer).stake(tokensToAllocate)
547+
await staking
548+
.connect(indexer1.signer)
549+
.allocateFrom(
550+
indexer1.address,
551+
subgraphDeploymentID1,
552+
tokensToAllocate,
553+
allocationID1,
554+
metadata,
555+
await channelKey1.generateProof(indexer1.address),
556+
)
557+
558+
// Update total signalled
559+
const signalled1 = toGRT('1500')
560+
await curation.connect(curator1.signer).mint(subgraphDeploymentID1, signalled1, 0)
561+
}
562+
540563
async function setupIndexerAllocationWithDelegation(
541564
tokensToDelegate: BigNumber,
542565
delegationParams: DelegationParameters,
@@ -636,6 +659,59 @@ describe('Rewards', () => {
636659
expect(toRound(afterTokenSupply)).eq(toRound(expectedTokenSupply))
637660
})
638661

662+
it('does not revert with an underflow if the minimum signal changes', async function () {
663+
// Align with the epoch boundary
664+
await advanceToNextEpoch(epochManager)
665+
// Setup
666+
await setupIndexerAllocation()
667+
668+
await rewardsManager.connect(governor.signer).setMinimumSubgraphSignal(toGRT(14000))
669+
670+
// Jump
671+
await advanceToNextEpoch(epochManager)
672+
673+
// Close allocation. At this point rewards should be collected for that indexer
674+
const tx = staking.connect(indexer1.signer).closeAllocation(allocationID1, randomHexBytes())
675+
await expect(tx)
676+
.emit(rewardsManager, 'RewardsAssigned')
677+
.withArgs(indexer1.address, allocationID1, await epochManager.currentEpoch(), toBN(0))
678+
})
679+
680+
it('does not revert with an underflow if the minimum signal changes, and signal came after allocation', async function () {
681+
// Align with the epoch boundary
682+
await advanceToNextEpoch(epochManager)
683+
// Setup
684+
await setupIndexerAllocationSignalingAfter()
685+
686+
await rewardsManager.connect(governor.signer).setMinimumSubgraphSignal(toGRT(14000))
687+
688+
// Jump
689+
await advanceToNextEpoch(epochManager)
690+
691+
// Close allocation. At this point rewards should be collected for that indexer
692+
const tx = staking.connect(indexer1.signer).closeAllocation(allocationID1, randomHexBytes())
693+
await expect(tx)
694+
.emit(rewardsManager, 'RewardsAssigned')
695+
.withArgs(indexer1.address, allocationID1, await epochManager.currentEpoch(), toBN(0))
696+
})
697+
698+
it('does not revert if signal was already under minimum', async function () {
699+
await rewardsManager.connect(governor.signer).setMinimumSubgraphSignal(toGRT(2000))
700+
// Align with the epoch boundary
701+
await advanceToNextEpoch(epochManager)
702+
// Setup
703+
await setupIndexerAllocation()
704+
705+
// Jump
706+
await advanceToNextEpoch(epochManager)
707+
// Close allocation. At this point rewards should be collected for that indexer
708+
const tx = staking.connect(indexer1.signer).closeAllocation(allocationID1, randomHexBytes())
709+
710+
await expect(tx)
711+
.emit(rewardsManager, 'RewardsAssigned')
712+
.withArgs(indexer1.address, allocationID1, await epochManager.currentEpoch(), toBN(0))
713+
})
714+
639715
it('should distribute rewards on closed allocation and send to destination', async function () {
640716
const destinationAddress = randomHexBytes(20)
641717
await staking.connect(indexer1.signer).setRewardsDestination(destinationAddress)

0 commit comments

Comments
 (0)