Skip to content

Commit 5c86317

Browse files
HACK: partial AllocationManager to lib - 24.520
1 parent c616697 commit 5c86317

File tree

2 files changed

+153
-27
lines changed

2 files changed

+153
-27
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// SPDX-License-Identifier: GPL-3.0-or-later
2+
pragma solidity 0.8.27;
3+
4+
import { ECDSA } from "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
5+
import { IHorizonStaking } from "@graphprotocol/horizon/contracts/interfaces/IHorizonStaking.sol";
6+
import { IRewardsManager } from "@graphprotocol/contracts/contracts/rewards/IRewardsManager.sol";
7+
import { ProvisionTracker } from "@graphprotocol/horizon/contracts/data-service/libraries/ProvisionTracker.sol";
8+
9+
import { Allocation } from "../libraries/Allocation.sol";
10+
import { LegacyAllocation } from "../libraries/LegacyAllocation.sol";
11+
import { AllocationManager } from "../utilities/AllocationManager.sol";
12+
13+
library AllocationManagerLib {
14+
using ProvisionTracker for mapping(address => uint256);
15+
using Allocation for mapping(address => Allocation.State);
16+
using LegacyAllocation for mapping(address => LegacyAllocation.State);
17+
18+
///@dev EIP712 typehash for allocation id proof
19+
bytes32 private constant EIP712_ALLOCATION_ID_PROOF_TYPEHASH =
20+
keccak256("AllocationIdProof(address indexer,address allocationId)");
21+
22+
struct AllocateParams {
23+
uint256 currentEpoch;
24+
IHorizonStaking graphStaking;
25+
IRewardsManager graphRewardsManager;
26+
bytes32 _encodeAllocationProof;
27+
address _indexer;
28+
address _allocationId;
29+
bytes32 _subgraphDeploymentId;
30+
uint256 _tokens;
31+
bytes _allocationProof;
32+
uint32 _delegationRatio;
33+
}
34+
35+
/**
36+
* @notice Create an allocation
37+
* @dev The `_allocationProof` is a 65-bytes Ethereum signed message of `keccak256(indexerAddress,allocationId)`
38+
*
39+
* Requirements:
40+
* - `_allocationId` must not be the zero address
41+
*
42+
* Emits a {AllocationCreated} event
43+
*
44+
* @param _allocations The mapping of allocation ids to allocation states
45+
*/
46+
function allocate(
47+
mapping(address allocationId => Allocation.State allocation) storage _allocations,
48+
mapping(address allocationId => LegacyAllocation.State allocation) storage _legacyAllocations,
49+
mapping(address indexer => uint256 tokens) storage allocationProvisionTracker,
50+
mapping(bytes32 subgraphDeploymentId => uint256 tokens) storage _subgraphAllocatedTokens,
51+
AllocateParams memory params
52+
) external {
53+
require(params._allocationId != address(0), AllocationManager.AllocationManagerInvalidZeroAllocationId());
54+
55+
_verifyAllocationProof(params._encodeAllocationProof, params._allocationId, params._allocationProof);
56+
57+
// Ensure allocation id is not reused
58+
// need to check both subgraph service (on allocations.create()) and legacy allocations
59+
_legacyAllocations.revertIfExists(params.graphStaking, params._allocationId);
60+
61+
Allocation.State memory allocation = _allocations.create(
62+
params._indexer,
63+
params._allocationId,
64+
params._subgraphDeploymentId,
65+
params._tokens,
66+
params.graphRewardsManager.onSubgraphAllocationUpdate(params._subgraphDeploymentId),
67+
params.currentEpoch
68+
);
69+
70+
// Check that the indexer has enough tokens available
71+
// Note that the delegation ratio ensures overdelegation cannot be used
72+
allocationProvisionTracker.lock(params.graphStaking, params._indexer, params._tokens, params._delegationRatio);
73+
74+
// Update total allocated tokens for the subgraph deployment
75+
_subgraphAllocatedTokens[allocation.subgraphDeploymentId] =
76+
_subgraphAllocatedTokens[allocation.subgraphDeploymentId] +
77+
allocation.tokens;
78+
79+
emit AllocationManager.AllocationCreated(
80+
params._indexer,
81+
params._allocationId,
82+
params._subgraphDeploymentId,
83+
allocation.tokens,
84+
params.currentEpoch
85+
);
86+
}
87+
88+
/**
89+
* @notice Verifies ownership of an allocation id by verifying an EIP712 allocation proof
90+
* @dev Requirements:
91+
* - Signer must be the allocation id address
92+
* @param _allocationId The id of the allocation
93+
* @param _proof The EIP712 proof, an EIP712 signed message of (indexer,allocationId)
94+
*/
95+
function _verifyAllocationProof(
96+
bytes32 _encodeAllocationProof,
97+
address _allocationId,
98+
bytes memory _proof
99+
) private pure {
100+
address signer = ECDSA.recover(_encodeAllocationProof, _proof);
101+
require(
102+
signer == _allocationId,
103+
AllocationManager.AllocationManagerInvalidAllocationProof(signer, _allocationId)
104+
);
105+
}
106+
}

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

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { Allocation } from "../libraries/Allocation.sol";
1515
import { LegacyAllocation } from "../libraries/LegacyAllocation.sol";
1616
import { PPMMath } from "@graphprotocol/horizon/contracts/libraries/PPMMath.sol";
1717
import { ProvisionTracker } from "@graphprotocol/horizon/contracts/data-service/libraries/ProvisionTracker.sol";
18+
import { AllocationManagerLib } from "../libraries/AllocationManagerLib.sol";
1819

1920
/**
2021
* @title AllocationManager contract
@@ -204,34 +205,53 @@ abstract contract AllocationManager is EIP712Upgradeable, GraphDirectory, Alloca
204205
bytes memory _allocationProof,
205206
uint32 _delegationRatio
206207
) internal {
207-
require(_allocationId != address(0), AllocationManagerInvalidZeroAllocationId());
208-
209-
_verifyAllocationProof(_indexer, _allocationId, _allocationProof);
210-
211-
// Ensure allocation id is not reused
212-
// need to check both subgraph service (on allocations.create()) and legacy allocations
213-
_legacyAllocations.revertIfExists(_graphStaking(), _allocationId);
214-
215-
uint256 currentEpoch = _graphEpochManager().currentEpoch();
216-
Allocation.State memory allocation = _allocations.create(
217-
_indexer,
218-
_allocationId,
219-
_subgraphDeploymentId,
220-
_tokens,
221-
_graphRewardsManager().onSubgraphAllocationUpdate(_subgraphDeploymentId),
222-
currentEpoch
208+
// require(_allocationId != address(0), AllocationManagerInvalidZeroAllocationId());
209+
210+
// _verifyAllocationProof(_indexer, _allocationId, _allocationProof);
211+
212+
// // Ensure allocation id is not reused
213+
// // need to check both subgraph service (on allocations.create()) and legacy allocations
214+
// _legacyAllocations.revertIfExists(_graphStaking(), _allocationId);
215+
216+
// uint256 currentEpoch = _graphEpochManager().currentEpoch();
217+
// Allocation.State memory allocation = _allocations.create(
218+
// _indexer,
219+
// _allocationId,
220+
// _subgraphDeploymentId,
221+
// _tokens,
222+
// _graphRewardsManager().onSubgraphAllocationUpdate(_subgraphDeploymentId),
223+
// currentEpoch
224+
// );
225+
226+
// // Check that the indexer has enough tokens available
227+
// // Note that the delegation ratio ensures overdelegation cannot be used
228+
// allocationProvisionTracker.lock(_graphStaking(), _indexer, _tokens, _delegationRatio);
229+
230+
// // Update total allocated tokens for the subgraph deployment
231+
// _subgraphAllocatedTokens[allocation.subgraphDeploymentId] =
232+
// _subgraphAllocatedTokens[allocation.subgraphDeploymentId] +
233+
// allocation.tokens;
234+
235+
// emit AllocationCreated(_indexer, _allocationId, _subgraphDeploymentId, allocation.tokens, currentEpoch);
236+
237+
AllocationManagerLib.allocate(
238+
_allocations,
239+
_legacyAllocations,
240+
allocationProvisionTracker,
241+
_subgraphAllocatedTokens,
242+
AllocationManagerLib.AllocateParams({
243+
_allocationId: _allocationId,
244+
_allocationProof: _allocationProof,
245+
_encodeAllocationProof: _encodeAllocationProof(_indexer, _allocationId),
246+
_delegationRatio: _delegationRatio,
247+
_indexer: _indexer,
248+
_subgraphDeploymentId: _subgraphDeploymentId,
249+
_tokens: _tokens,
250+
currentEpoch: _graphEpochManager().currentEpoch(),
251+
graphRewardsManager: _graphRewardsManager(),
252+
graphStaking: _graphStaking()
253+
})
223254
);
224-
225-
// Check that the indexer has enough tokens available
226-
// Note that the delegation ratio ensures overdelegation cannot be used
227-
allocationProvisionTracker.lock(_graphStaking(), _indexer, _tokens, _delegationRatio);
228-
229-
// Update total allocated tokens for the subgraph deployment
230-
_subgraphAllocatedTokens[allocation.subgraphDeploymentId] =
231-
_subgraphAllocatedTokens[allocation.subgraphDeploymentId] +
232-
allocation.tokens;
233-
234-
emit AllocationCreated(_indexer, _allocationId, _subgraphDeploymentId, allocation.tokens, currentEpoch);
235255
}
236256

237257
/**

0 commit comments

Comments
 (0)