Skip to content

Commit d4a2db0

Browse files
f: move errors et al to AllocationHandler
1 parent 54ef3eb commit d4a2db0

File tree

10 files changed

+156
-150
lines changed

10 files changed

+156
-150
lines changed

packages/subgraph-service/contracts/libraries/AllocationHandler.sol

Lines changed: 133 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,15 @@ import { PPMMath } from "@graphprotocol/horizon/contracts/libraries/PPMMath.sol"
1414

1515
import { Allocation } from "../libraries/Allocation.sol";
1616
import { LegacyAllocation } from "../libraries/LegacyAllocation.sol";
17-
import { AllocationManager } from "../utilities/AllocationManager.sol";
1817

18+
/**
19+
* @title AllocationHandler contract
20+
* @notice A helper contract implementing allocation lifecycle management.
21+
* Allows opening, resizing, and closing allocations, as well as collecting indexing rewards by presenting a Proof
22+
* of Indexing (POI).
23+
* @custom:security-contact Please email security+contracts@thegraph.com if you find any
24+
* bugs. We may have an active bug bounty program.
25+
*/
1926
library AllocationHandler {
2027
using ProvisionTracker for mapping(address => uint256);
2128
using Allocation for mapping(address => Allocation.State);
@@ -76,6 +83,122 @@ library AllocationHandler {
7683
address _paymentsDestination;
7784
}
7885

86+
/**
87+
* @notice Emitted when an indexer creates an allocation
88+
* @param indexer The address of the indexer
89+
* @param allocationId The id of the allocation
90+
* @param subgraphDeploymentId The id of the subgraph deployment
91+
* @param tokens The amount of tokens allocated
92+
* @param currentEpoch The current epoch
93+
*/
94+
event AllocationCreated(
95+
address indexed indexer,
96+
address indexed allocationId,
97+
bytes32 indexed subgraphDeploymentId,
98+
uint256 tokens,
99+
uint256 currentEpoch
100+
);
101+
102+
/**
103+
* @notice Emitted when an indexer collects indexing rewards for an allocation
104+
* @param indexer The address of the indexer
105+
* @param allocationId The id of the allocation
106+
* @param subgraphDeploymentId The id of the subgraph deployment
107+
* @param tokensRewards The amount of tokens collected
108+
* @param tokensIndexerRewards The amount of tokens collected for the indexer
109+
* @param tokensDelegationRewards The amount of tokens collected for delegators
110+
* @param poi The POI presented
111+
* @param currentEpoch The current epoch
112+
* @param poiMetadata The metadata associated with the POI
113+
*/
114+
event IndexingRewardsCollected(
115+
address indexed indexer,
116+
address indexed allocationId,
117+
bytes32 indexed subgraphDeploymentId,
118+
uint256 tokensRewards,
119+
uint256 tokensIndexerRewards,
120+
uint256 tokensDelegationRewards,
121+
bytes32 poi,
122+
bytes poiMetadata,
123+
uint256 currentEpoch
124+
);
125+
126+
/**
127+
* @notice Emitted when an indexer resizes an allocation
128+
* @param indexer The address of the indexer
129+
* @param allocationId The id of the allocation
130+
* @param subgraphDeploymentId The id of the subgraph deployment
131+
* @param newTokens The new amount of tokens allocated
132+
* @param oldTokens The old amount of tokens allocated
133+
*/
134+
event AllocationResized(
135+
address indexed indexer,
136+
address indexed allocationId,
137+
bytes32 indexed subgraphDeploymentId,
138+
uint256 newTokens,
139+
uint256 oldTokens
140+
);
141+
142+
/**
143+
* @dev Emitted when an indexer closes an allocation
144+
* @param indexer The address of the indexer
145+
* @param allocationId The id of the allocation
146+
* @param subgraphDeploymentId The id of the subgraph deployment
147+
* @param tokens The amount of tokens allocated
148+
* @param forceClosed Whether the allocation was force closed
149+
*/
150+
event AllocationClosed(
151+
address indexed indexer,
152+
address indexed allocationId,
153+
bytes32 indexed subgraphDeploymentId,
154+
uint256 tokens,
155+
bool forceClosed
156+
);
157+
158+
/**
159+
* @notice Emitted when a legacy allocation is migrated into the subgraph service
160+
* @param indexer The address of the indexer
161+
* @param allocationId The id of the allocation
162+
* @param subgraphDeploymentId The id of the subgraph deployment
163+
*/
164+
event LegacyAllocationMigrated(
165+
address indexed indexer,
166+
address indexed allocationId,
167+
bytes32 indexed subgraphDeploymentId
168+
);
169+
170+
/**
171+
* @notice Emitted when the maximum POI staleness is updated
172+
* @param maxPOIStaleness The max POI staleness in seconds
173+
*/
174+
event MaxPOIStalenessSet(uint256 maxPOIStaleness);
175+
176+
/**
177+
* @notice Thrown when an allocation proof is invalid
178+
* Both `signer` and `allocationId` should match for a valid proof.
179+
* @param signer The address that signed the proof
180+
* @param allocationId The id of the allocation
181+
*/
182+
error AllocationHandlerInvalidAllocationProof(address signer, address allocationId);
183+
184+
/**
185+
* @notice Thrown when attempting to create an allocation with a zero allocation id
186+
*/
187+
error AllocationHandlerInvalidZeroAllocationId();
188+
189+
/**
190+
* @notice Thrown when attempting to collect indexing rewards on a closed allocationl
191+
* @param allocationId The id of the allocation
192+
*/
193+
error AllocationHandlerAllocationClosed(address allocationId);
194+
195+
/**
196+
* @notice Thrown when attempting to resize an allocation with the same size
197+
* @param allocationId The id of the allocation
198+
* @param tokens The amount of tokens
199+
*/
200+
error AllocationHandlerAllocationSameSize(address allocationId, uint256 tokens);
201+
79202
/**
80203
* @notice Create an allocation
81204
* @dev The `_allocationProof` is a 65-bytes Ethereum signed message of `keccak256(indexerAddress,allocationId)`
@@ -98,7 +221,7 @@ library AllocationHandler {
98221
mapping(bytes32 subgraphDeploymentId => uint256 tokens) storage _subgraphAllocatedTokens,
99222
AllocateParams memory params
100223
) external {
101-
require(params._allocationId != address(0), AllocationManager.AllocationManagerInvalidZeroAllocationId());
224+
require(params._allocationId != address(0), AllocationHandler.AllocationHandlerInvalidZeroAllocationId());
102225

103226
_verifyAllocationProof(params._encodeAllocationProof, params._allocationId, params._allocationProof);
104227

@@ -124,7 +247,7 @@ library AllocationHandler {
124247
_subgraphAllocatedTokens[allocation.subgraphDeploymentId] +
125248
allocation.tokens;
126249

127-
emit AllocationManager.AllocationCreated(
250+
emit AllocationHandler.AllocationCreated(
128251
params._indexer,
129252
params._allocationId,
130253
params._subgraphDeploymentId,
@@ -166,7 +289,7 @@ library AllocationHandler {
166289
PresentParams memory params
167290
) external returns (uint256) {
168291
Allocation.State memory allocation = _allocations.get(params._allocationId);
169-
require(allocation.isOpen(), AllocationManager.AllocationManagerAllocationClosed(params._allocationId));
292+
require(allocation.isOpen(), AllocationHandler.AllocationHandlerAllocationClosed(params._allocationId));
170293

171294
// Mint indexing rewards if all conditions are met
172295
uint256 tokensRewards = (!allocation.isStale(params.maxPOIStaleness) &&
@@ -217,7 +340,7 @@ library AllocationHandler {
217340
}
218341
}
219342

220-
emit AllocationManager.IndexingRewardsCollected(
343+
emit AllocationHandler.IndexingRewardsCollected(
221344
allocation.indexer,
222345
params._allocationId,
223346
allocation.subgraphDeploymentId,
@@ -318,10 +441,10 @@ library AllocationHandler {
318441
uint32 _delegationRatio
319442
) external {
320443
Allocation.State memory allocation = _allocations.get(_allocationId);
321-
require(allocation.isOpen(), AllocationManager.AllocationManagerAllocationClosed(_allocationId));
444+
require(allocation.isOpen(), AllocationHandler.AllocationHandlerAllocationClosed(_allocationId));
322445
require(
323446
_tokens != allocation.tokens,
324-
AllocationManager.AllocationManagerAllocationSameSize(_allocationId, _tokens)
447+
AllocationHandler.AllocationHandlerAllocationSameSize(_allocationId, _tokens)
325448
);
326449

327450
// Update provision tracker
@@ -355,7 +478,7 @@ library AllocationHandler {
355478
_subgraphAllocatedTokens[allocation.subgraphDeploymentId] -= (oldTokens - _tokens);
356479
}
357480

358-
emit AllocationManager.AllocationResized(
481+
emit AllocationHandler.AllocationResized(
359482
allocation.indexer,
360483
_allocationId,
361484
allocation.subgraphDeploymentId,
@@ -421,7 +544,7 @@ library AllocationHandler {
421544
_subgraphAllocatedTokens[allocation.subgraphDeploymentId] -
422545
allocation.tokens;
423546

424-
emit AllocationManager.AllocationClosed(
547+
emit AllocationHandler.AllocationClosed(
425548
allocation.indexer,
426549
_allocationId,
427550
allocation.subgraphDeploymentId,
@@ -463,7 +586,7 @@ library AllocationHandler {
463586
address signer = ECDSA.recover(_encodeAllocationProof, _proof);
464587
require(
465588
signer == _allocationId,
466-
AllocationManager.AllocationManagerInvalidAllocationProof(signer, _allocationId)
589+
AllocationHandler.AllocationHandlerInvalidAllocationProof(signer, _allocationId)
467590
);
468591
}
469592
}

packages/subgraph-service/contracts/libraries/IndexingAgreement.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { IGraphPayments } from "@graphprotocol/horizon/contracts/interfaces/IGra
55
import { IRecurringCollector } from "@graphprotocol/horizon/contracts/interfaces/IRecurringCollector.sol";
66

77
import { ISubgraphService } from "../interfaces/ISubgraphService.sol";
8-
import { AllocationManager } from "../utilities/AllocationManager.sol";
8+
import { AllocationHandler } from "../libraries/AllocationHandler.sol";
99
import { Directory } from "../utilities/Directory.sol";
1010
import { Allocation } from "./Allocation.sol";
1111
import { IndexingAgreementDecoder } from "./IndexingAgreementDecoder.sol";
@@ -659,7 +659,7 @@ library IndexingAgreement {
659659
allocation.indexer == _indexer,
660660
ISubgraphService.SubgraphServiceAllocationNotAuthorized(_indexer, _allocationId)
661661
);
662-
require(allocation.isOpen(), AllocationManager.AllocationManagerAllocationClosed(_allocationId));
662+
require(allocation.isOpen(), AllocationHandler.AllocationHandlerAllocationClosed(_allocationId));
663663

664664
return allocation;
665665
}

packages/subgraph-service/contracts/utilities/AllocationManager.sol

Lines changed: 2 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -34,122 +34,6 @@ abstract contract AllocationManager is EIP712Upgradeable, GraphDirectory, Alloca
3434
bytes32 private constant EIP712_ALLOCATION_ID_PROOF_TYPEHASH =
3535
keccak256("AllocationIdProof(address indexer,address allocationId)");
3636

37-
/**
38-
* @notice Emitted when an indexer creates an allocation
39-
* @param indexer The address of the indexer
40-
* @param allocationId The id of the allocation
41-
* @param subgraphDeploymentId The id of the subgraph deployment
42-
* @param tokens The amount of tokens allocated
43-
* @param currentEpoch The current epoch
44-
*/
45-
event AllocationCreated(
46-
address indexed indexer,
47-
address indexed allocationId,
48-
bytes32 indexed subgraphDeploymentId,
49-
uint256 tokens,
50-
uint256 currentEpoch
51-
);
52-
53-
/**
54-
* @notice Emitted when an indexer collects indexing rewards for an allocation
55-
* @param indexer The address of the indexer
56-
* @param allocationId The id of the allocation
57-
* @param subgraphDeploymentId The id of the subgraph deployment
58-
* @param tokensRewards The amount of tokens collected
59-
* @param tokensIndexerRewards The amount of tokens collected for the indexer
60-
* @param tokensDelegationRewards The amount of tokens collected for delegators
61-
* @param poi The POI presented
62-
* @param currentEpoch The current epoch
63-
* @param poiMetadata The metadata associated with the POI
64-
*/
65-
event IndexingRewardsCollected(
66-
address indexed indexer,
67-
address indexed allocationId,
68-
bytes32 indexed subgraphDeploymentId,
69-
uint256 tokensRewards,
70-
uint256 tokensIndexerRewards,
71-
uint256 tokensDelegationRewards,
72-
bytes32 poi,
73-
bytes poiMetadata,
74-
uint256 currentEpoch
75-
);
76-
77-
/**
78-
* @notice Emitted when an indexer resizes an allocation
79-
* @param indexer The address of the indexer
80-
* @param allocationId The id of the allocation
81-
* @param subgraphDeploymentId The id of the subgraph deployment
82-
* @param newTokens The new amount of tokens allocated
83-
* @param oldTokens The old amount of tokens allocated
84-
*/
85-
event AllocationResized(
86-
address indexed indexer,
87-
address indexed allocationId,
88-
bytes32 indexed subgraphDeploymentId,
89-
uint256 newTokens,
90-
uint256 oldTokens
91-
);
92-
93-
/**
94-
* @dev Emitted when an indexer closes an allocation
95-
* @param indexer The address of the indexer
96-
* @param allocationId The id of the allocation
97-
* @param subgraphDeploymentId The id of the subgraph deployment
98-
* @param tokens The amount of tokens allocated
99-
* @param forceClosed Whether the allocation was force closed
100-
*/
101-
event AllocationClosed(
102-
address indexed indexer,
103-
address indexed allocationId,
104-
bytes32 indexed subgraphDeploymentId,
105-
uint256 tokens,
106-
bool forceClosed
107-
);
108-
109-
/**
110-
* @notice Emitted when a legacy allocation is migrated into the subgraph service
111-
* @param indexer The address of the indexer
112-
* @param allocationId The id of the allocation
113-
* @param subgraphDeploymentId The id of the subgraph deployment
114-
*/
115-
event LegacyAllocationMigrated(
116-
address indexed indexer,
117-
address indexed allocationId,
118-
bytes32 indexed subgraphDeploymentId
119-
);
120-
121-
/**
122-
* @notice Emitted when the maximum POI staleness is updated
123-
* @param maxPOIStaleness The max POI staleness in seconds
124-
*/
125-
event MaxPOIStalenessSet(uint256 maxPOIStaleness);
126-
127-
/**
128-
* @notice Thrown when an allocation proof is invalid
129-
* Both `signer` and `allocationId` should match for a valid proof.
130-
* @param signer The address that signed the proof
131-
* @param allocationId The id of the allocation
132-
*/
133-
error AllocationManagerInvalidAllocationProof(address signer, address allocationId);
134-
135-
/**
136-
* @notice Thrown when attempting to create an allocation with a zero allocation id
137-
*/
138-
error AllocationManagerInvalidZeroAllocationId();
139-
140-
/**
141-
* @notice Thrown when attempting to collect indexing rewards on a closed allocationl
142-
* @param allocationId The id of the allocation
143-
*/
144-
error AllocationManagerAllocationClosed(address allocationId);
145-
146-
/**
147-
* @notice Thrown when attempting to resize an allocation with the same size
148-
* @param allocationId The id of the allocation
149-
* @param tokens The amount of tokens
150-
*/
151-
error AllocationManagerAllocationSameSize(address allocationId, uint256 tokens);
152-
15337
/**
15438
* @notice Initializes the contract and parent contracts
15539
* @param _name The name to use for EIP712 domain separation
@@ -175,7 +59,7 @@ abstract contract AllocationManager is EIP712Upgradeable, GraphDirectory, Alloca
17559
*/
17660
function _migrateLegacyAllocation(address _indexer, address _allocationId, bytes32 _subgraphDeploymentId) internal {
17761
_legacyAllocations.migrate(_indexer, _allocationId, _subgraphDeploymentId);
178-
emit LegacyAllocationMigrated(_indexer, _allocationId, _subgraphDeploymentId);
62+
emit AllocationHandler.LegacyAllocationMigrated(_indexer, _allocationId, _subgraphDeploymentId);
17963
}
18064

18165
/**
@@ -336,7 +220,7 @@ abstract contract AllocationManager is EIP712Upgradeable, GraphDirectory, Alloca
336220
*/
337221
function _setMaxPOIStaleness(uint256 _maxPOIStaleness) internal {
338222
maxPOIStaleness = _maxPOIStaleness;
339-
emit MaxPOIStalenessSet(_maxPOIStaleness);
223+
emit AllocationHandler.MaxPOIStalenessSet(_maxPOIStaleness);
340224
}
341225

342226
/**

0 commit comments

Comments
 (0)