Skip to content

Commit 9f3f87b

Browse files
committed
feat: skip unnecessary code paths for rewards calculation under zero-allocation
1 parent f6df35f commit 9f3f87b

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

contracts/staking/Staking.sol

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,9 +1115,13 @@ contract Staking is StakingV2Storage, GraphUpgradeable, IStaking {
11151115
bytes32 digest = ECDSA.toEthSignedMessageHash(messageHash);
11161116
require(ECDSA.recover(digest, _proof) == _allocationID, "!proof");
11171117

1118-
// Needs to have free capacity not used for other purposes to allocate
11191118
if (_tokens > 0) {
1119+
// Needs to have free capacity not used for other purposes to allocate
11201120
require(getIndexerCapacity(_indexer) >= _tokens, "!capacity");
1121+
} else {
1122+
// Allocating zero-tokens still needs to have stake
1123+
// Minimum indexer stake is managed by stake/unstake
1124+
require(stakes[_indexer].tokensSecureStake() > 0, "!stake");
11211125
}
11221126

11231127
// Creates an allocation
@@ -1131,10 +1135,13 @@ contract Staking is StakingV2Storage, GraphUpgradeable, IStaking {
11311135
0, // closedAtEpoch
11321136
0, // Initialize collected fees
11331137
0, // Initialize effective allocation
1134-
_updateRewards(_subgraphDeploymentID) // Initialize accumulated rewards per stake allocated
1138+
(_tokens > 0) ? _updateRewards(_subgraphDeploymentID) : 0 // Initialize accumulated rewards per stake allocated
11351139
);
11361140
allocations[_allocationID] = alloc;
11371141

1142+
// -- Rewards Distribution --
1143+
1144+
// Process non-zero-allocation rewards tracking
11381145
if (_tokens > 0) {
11391146
// Mark allocated tokens as used
11401147
stakes[_indexer].allocate(alloc.tokens);
@@ -1175,22 +1182,26 @@ contract Staking is StakingV2Storage, GraphUpgradeable, IStaking {
11751182
require(epochs > 0, "<epochs");
11761183

11771184
// Indexer or operator can close an allocation
1178-
// Delegators are also allowed but only after maxAllocationEpochs passed
1185+
// Anyone is allowed to close ONLY under two concurrent conditions
1186+
// - After maxAllocationEpochs passed
1187+
// - When the allocation is for non-zero amount of tokens
11791188
bool isIndexer = _isAuth(alloc.indexer);
1180-
if (epochs <= maxAllocationEpochs) {
1189+
if (epochs <= maxAllocationEpochs || alloc.tokens == 0) {
11811190
require(isIndexer, "!auth");
11821191
}
11831192

1193+
// Close the allocation and start counting a period to settle remaining payments from
1194+
// state channels.
1195+
allocations[_allocationID].closedAtEpoch = alloc.closedAtEpoch;
1196+
1197+
// -- Rebate Pool --
1198+
11841199
// Calculate effective allocation for the amount of epochs it remained allocated
11851200
alloc.effectiveAllocation = _getEffectiveAllocation(
11861201
maxAllocationEpochs,
11871202
alloc.tokens,
11881203
epochs
11891204
);
1190-
1191-
// Close the allocation and start counting a period to settle remaining payments from
1192-
// state channels.
1193-
allocations[_allocationID].closedAtEpoch = alloc.closedAtEpoch;
11941205
allocations[_allocationID].effectiveAllocation = alloc.effectiveAllocation;
11951206

11961207
// Account collected fees and effective allocation in rebate pool for the epoch
@@ -1200,14 +1211,17 @@ contract Staking is StakingV2Storage, GraphUpgradeable, IStaking {
12001211
}
12011212
rebatePool.addToPool(alloc.collectedFees, alloc.effectiveAllocation);
12021213

1203-
// Distribute rewards if proof of indexing was presented by the indexer or operator
1204-
if (isIndexer && _poi != 0) {
1205-
_distributeRewards(_allocationID, alloc.indexer);
1206-
} else {
1207-
_updateRewards(alloc.subgraphDeploymentID);
1208-
}
1214+
// -- Rewards Distribution --
12091215

1216+
// Process non-zero-allocation rewards tracking
12101217
if (alloc.tokens > 0) {
1218+
// Distribute rewards if proof of indexing was presented by the indexer or operator
1219+
if (isIndexer && _poi != 0) {
1220+
_distributeRewards(_allocationID, alloc.indexer);
1221+
} else {
1222+
_updateRewards(alloc.subgraphDeploymentID);
1223+
}
1224+
12111225
// Free allocated tokens from use
12121226
stakes[alloc.indexer].unallocate(alloc.tokens);
12131227

0 commit comments

Comments
 (0)