-
Notifications
You must be signed in to change notification settings - Fork 156
/
Copy pathSubgraphService.t.sol
502 lines (424 loc) · 22 KB
/
SubgraphService.t.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
// SPDX-License-Identifier: MIT
pragma solidity 0.8.27;
import "forge-std/Test.sol";
import { IDataService } from "@graphprotocol/horizon/contracts/data-service/interfaces/IDataService.sol";
import { IGraphPayments } from "@graphprotocol/horizon/contracts/interfaces/IGraphPayments.sol";
import { PPMMath } from "@graphprotocol/horizon/contracts/libraries/PPMMath.sol";
import { IHorizonStakingTypes } from "@graphprotocol/horizon/contracts/interfaces/internal/IHorizonStakingTypes.sol";
import { IGraphTallyCollector } from "@graphprotocol/horizon/contracts/interfaces/IGraphTallyCollector.sol";
import { ECDSA } from "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import { LinkedList } from "@graphprotocol/horizon/contracts/libraries/LinkedList.sol";
import { IDataServiceFees } from "@graphprotocol/horizon/contracts/data-service/interfaces/IDataServiceFees.sol";
import { IHorizonStakingTypes } from "@graphprotocol/horizon/contracts/interfaces/internal/IHorizonStakingTypes.sol";
import { Allocation } from "../../contracts/libraries/Allocation.sol";
import { AllocationManager } from "../../contracts/utilities/AllocationManager.sol";
import { ISubgraphService } from "../../contracts/interfaces/ISubgraphService.sol";
import { LegacyAllocation } from "../../contracts/libraries/LegacyAllocation.sol";
import { SubgraphServiceSharedTest } from "../shared/SubgraphServiceShared.t.sol";
contract SubgraphServiceTest is SubgraphServiceSharedTest {
using PPMMath for uint256;
using Allocation for Allocation.State;
using LinkedList for LinkedList.List;
/*
* MODIFIERS
*/
modifier useGovernor() {
vm.startPrank(users.governor);
_;
vm.stopPrank();
}
modifier useOperator() {
resetPrank(users.indexer);
staking.setOperator(address(subgraphService), users.operator, true);
resetPrank(users.operator);
_;
vm.stopPrank();
}
modifier useRewardsDestination() {
_setRewardsDestination(users.rewardsDestination);
_;
}
/*
* SET UP
*/
function setUp() public virtual override {
super.setUp();
}
/*
* ACTIONS
*/
function _setRewardsDestination(address _rewardsDestination) internal {
(, address indexer, ) = vm.readCallers();
vm.expectEmit(address(subgraphService));
emit AllocationManager.RewardsDestinationSet(indexer, _rewardsDestination);
// Set rewards destination
subgraphService.setRewardsDestination(_rewardsDestination);
// Check rewards destination
assertEq(subgraphService.rewardsDestination(indexer), _rewardsDestination);
}
function _acceptProvision(address _indexer, bytes memory _data) internal {
IHorizonStakingTypes.Provision memory provision = staking.getProvision(_indexer, address(subgraphService));
uint32 maxVerifierCut = provision.maxVerifierCut;
uint64 thawingPeriod = provision.thawingPeriod;
uint32 maxVerifierCutPending = provision.maxVerifierCutPending;
uint64 thawingPeriodPending = provision.thawingPeriodPending;
vm.expectEmit(address(subgraphService));
emit IDataService.ProvisionPendingParametersAccepted(_indexer);
// Accept provision
subgraphService.acceptProvisionPendingParameters(_indexer, _data);
// Update provision after acceptance
provision = staking.getProvision(_indexer, address(subgraphService));
// Check that max verifier cut updated to pending value if needed
if (maxVerifierCut != maxVerifierCutPending) {
assertEq(provision.maxVerifierCut, maxVerifierCutPending);
}
// Check that thawing period updated to pending value if needed
if (thawingPeriod != thawingPeriodPending) {
assertEq(provision.thawingPeriod, thawingPeriodPending);
}
}
function _resizeAllocation(address _indexer, address _allocationId, uint256 _tokens) internal {
// before state
Allocation.State memory beforeAllocation = subgraphService.getAllocation(_allocationId);
bytes32 subgraphDeploymentId = beforeAllocation.subgraphDeploymentId;
uint256 beforeSubgraphAllocatedTokens = subgraphService.getSubgraphAllocatedTokens(subgraphDeploymentId);
uint256 beforeAllocatedTokens = subgraphService.allocationProvisionTracker(_indexer);
uint256 allocatedTokensDelta;
if (_tokens > beforeAllocation.tokens) {
allocatedTokensDelta = _tokens - beforeAllocation.tokens;
} else {
allocatedTokensDelta = beforeAllocation.tokens - _tokens;
}
vm.expectEmit(address(subgraphService));
emit AllocationManager.AllocationResized(
_indexer,
_allocationId,
subgraphDeploymentId,
_tokens,
beforeSubgraphAllocatedTokens
);
// resize allocation
subgraphService.resizeAllocation(_indexer, _allocationId, _tokens);
// after state
uint256 afterSubgraphAllocatedTokens = subgraphService.getSubgraphAllocatedTokens(subgraphDeploymentId);
uint256 afterAllocatedTokens = subgraphService.allocationProvisionTracker(_indexer);
Allocation.State memory afterAllocation = subgraphService.getAllocation(_allocationId);
uint256 accRewardsPerAllocatedTokenDelta = afterAllocation.accRewardsPerAllocatedToken -
beforeAllocation.accRewardsPerAllocatedToken;
uint256 afterAccRewardsPending = rewardsManager.calcRewards(
beforeAllocation.tokens,
accRewardsPerAllocatedTokenDelta
);
// check state
if (_tokens > beforeAllocation.tokens) {
assertEq(afterAllocatedTokens, beforeAllocatedTokens + allocatedTokensDelta);
} else {
assertEq(afterAllocatedTokens, beforeAllocatedTokens - allocatedTokensDelta);
}
assertEq(afterAllocation.tokens, _tokens);
assertEq(afterAllocation.accRewardsPerAllocatedToken, rewardsPerSubgraphAllocationUpdate);
assertEq(afterAllocation.accRewardsPending, afterAccRewardsPending);
assertEq(afterSubgraphAllocatedTokens, _tokens);
}
function _closeStaleAllocation(address _allocationId) internal {
Allocation.State memory allocation = subgraphService.getAllocation(_allocationId);
assertTrue(allocation.isOpen());
uint256 previousSubgraphAllocatedTokens = subgraphService.getSubgraphAllocatedTokens(
allocation.subgraphDeploymentId
);
vm.expectEmit(address(subgraphService));
emit AllocationManager.AllocationClosed(
allocation.indexer,
_allocationId,
allocation.subgraphDeploymentId,
allocation.tokens
);
// close stale allocation
subgraphService.closeStaleAllocation(_allocationId);
// update allocation
allocation = subgraphService.getAllocation(_allocationId);
// check allocation
assertEq(allocation.closedAt, block.timestamp);
// check subgraph deployment allocated tokens
uint256 subgraphAllocatedTokens = subgraphService.getSubgraphAllocatedTokens(subgraphDeployment);
assertEq(subgraphAllocatedTokens, previousSubgraphAllocatedTokens - allocation.tokens);
}
struct IndexingRewardsData {
bytes32 poi;
uint256 tokensIndexerRewards;
uint256 tokensDelegationRewards;
}
struct QueryFeeData {
uint256 curationCut;
uint256 protocolPaymentCut;
}
struct CollectPaymentData {
uint256 rewardsDestinationBalance;
uint256 indexerProvisionBalance;
uint256 delegationPoolBalance;
uint256 indexerBalance;
uint256 curationBalance;
uint256 lockedTokens;
}
function _collect(address _indexer, IGraphPayments.PaymentTypes _paymentType, bytes memory _data) internal {
// Reset storage variables
uint256 paymentCollected = 0;
address allocationId;
IndexingRewardsData memory indexingRewardsData;
CollectPaymentData memory collectPaymentDataBefore = _collectPaymentDataBefore(_indexer);
if (_paymentType == IGraphPayments.PaymentTypes.QueryFee) {
paymentCollected = _handleQueryFeeCollection(_indexer, _data);
} else if (_paymentType == IGraphPayments.PaymentTypes.IndexingRewards) {
(paymentCollected, allocationId, indexingRewardsData) = _handleIndexingRewardsCollection(_data);
}
vm.expectEmit(address(subgraphService));
emit IDataService.ServicePaymentCollected(_indexer, _paymentType, paymentCollected);
// collect rewards
subgraphService.collect(_indexer, _paymentType, _data);
CollectPaymentData memory collectPaymentDataAfter = _collectPaymentDataAfter(_indexer);
if (_paymentType == IGraphPayments.PaymentTypes.QueryFee) {
_verifyQueryFeeCollection(
_indexer,
paymentCollected,
_data,
collectPaymentDataBefore,
collectPaymentDataAfter
);
} else if (_paymentType == IGraphPayments.PaymentTypes.IndexingRewards) {
_verifyIndexingRewardsCollection(
_indexer,
allocationId,
indexingRewardsData,
collectPaymentDataBefore,
collectPaymentDataAfter
);
}
}
function _collectPaymentDataBefore(address _indexer) private view returns (CollectPaymentData memory) {
address rewardsDestination = subgraphService.rewardsDestination(_indexer);
CollectPaymentData memory collectPaymentDataBefore;
collectPaymentDataBefore.rewardsDestinationBalance = token.balanceOf(rewardsDestination);
collectPaymentDataBefore.indexerProvisionBalance = staking.getProviderTokensAvailable(
_indexer,
address(subgraphService)
);
collectPaymentDataBefore.delegationPoolBalance = staking.getDelegatedTokensAvailable(
_indexer,
address(subgraphService)
);
collectPaymentDataBefore.indexerBalance = token.balanceOf(_indexer);
collectPaymentDataBefore.curationBalance = token.balanceOf(address(curation));
collectPaymentDataBefore.lockedTokens = subgraphService.feesProvisionTracker(_indexer);
return collectPaymentDataBefore;
}
function _collectPaymentDataAfter(address _indexer) private view returns (CollectPaymentData memory) {
CollectPaymentData memory collectPaymentDataAfter;
address rewardsDestination = subgraphService.rewardsDestination(_indexer);
collectPaymentDataAfter.rewardsDestinationBalance = token.balanceOf(rewardsDestination);
collectPaymentDataAfter.indexerProvisionBalance = staking.getProviderTokensAvailable(
_indexer,
address(subgraphService)
);
collectPaymentDataAfter.delegationPoolBalance = staking.getDelegatedTokensAvailable(
_indexer,
address(subgraphService)
);
collectPaymentDataAfter.indexerBalance = token.balanceOf(_indexer);
collectPaymentDataAfter.curationBalance = token.balanceOf(address(curation));
collectPaymentDataAfter.lockedTokens = subgraphService.feesProvisionTracker(_indexer);
return collectPaymentDataAfter;
}
function _handleQueryFeeCollection(
address _indexer,
bytes memory _data
) private returns (uint256 paymentCollected) {
IGraphTallyCollector.SignedRAV memory signedRav = abi.decode(_data, (IGraphTallyCollector.SignedRAV));
Allocation.State memory allocation = subgraphService.getAllocation(
address(uint160(uint256(signedRav.rav.collectionId)))
);
address payer = graphTallyCollector.isAuthorized(signedRav.rav.payer, _recoverRAVSigner(signedRav))
? signedRav.rav.payer
: address(0);
uint256 tokensCollected = graphTallyCollector.tokensCollected(
address(subgraphService),
signedRav.rav.collectionId,
_indexer,
payer
);
paymentCollected = signedRav.rav.valueAggregate - tokensCollected;
QueryFeeData memory queryFeeData = _queryFeeData(allocation.subgraphDeploymentId);
uint256 tokensProtocol = paymentCollected.mulPPMRoundUp(queryFeeData.protocolPaymentCut);
uint256 tokensCurators = (paymentCollected - tokensProtocol).mulPPMRoundUp(queryFeeData.curationCut);
vm.expectEmit(address(subgraphService));
emit ISubgraphService.QueryFeesCollected(_indexer, payer, paymentCollected, tokensCurators);
return paymentCollected;
}
function _queryFeeData(bytes32 _subgraphDeploymentId) private view returns (QueryFeeData memory) {
QueryFeeData memory queryFeeData;
queryFeeData.protocolPaymentCut = graphPayments.PROTOCOL_PAYMENT_CUT();
uint256 curationFeesCut = subgraphService.curationFeesCut();
queryFeeData.curationCut = curation.isCurated(_subgraphDeploymentId) ? curationFeesCut : 0;
return queryFeeData;
}
function _handleIndexingRewardsCollection(
bytes memory _data
) private returns (uint256 paymentCollected, address allocationId, IndexingRewardsData memory indexingRewardsData) {
(allocationId, indexingRewardsData.poi) = abi.decode(_data, (address, bytes32));
Allocation.State memory allocation = subgraphService.getAllocation(allocationId);
// Calculate accumulated tokens, this depends on the rewards manager which we have mocked
uint256 accRewardsPerTokens = allocation.tokens.mulPPM(rewardsManager.rewardsPerSignal());
// Calculate the payment collected by the indexer for this transaction
paymentCollected = accRewardsPerTokens - allocation.accRewardsPerAllocatedToken;
uint256 currentEpoch = epochManager.currentEpoch();
paymentCollected = currentEpoch > allocation.createdAtEpoch ? paymentCollected : 0;
uint256 delegatorCut = staking.getDelegationFeeCut(
allocation.indexer,
address(subgraphService),
IGraphPayments.PaymentTypes.IndexingRewards
);
IHorizonStakingTypes.DelegationPool memory delegationPool = staking.getDelegationPool(
allocation.indexer,
address(subgraphService)
);
indexingRewardsData.tokensDelegationRewards = delegationPool.shares > 0
? paymentCollected.mulPPM(delegatorCut)
: 0;
indexingRewardsData.tokensIndexerRewards = paymentCollected - indexingRewardsData.tokensDelegationRewards;
vm.expectEmit(address(subgraphService));
emit AllocationManager.IndexingRewardsCollected(
allocation.indexer,
allocationId,
allocation.subgraphDeploymentId,
paymentCollected,
indexingRewardsData.tokensIndexerRewards,
indexingRewardsData.tokensDelegationRewards,
indexingRewardsData.poi,
epochManager.currentEpoch()
);
return (paymentCollected, allocationId, indexingRewardsData);
}
function _verifyQueryFeeCollection(
address _indexer,
uint256 _paymentCollected,
bytes memory _data,
CollectPaymentData memory collectPaymentDataBefore,
CollectPaymentData memory collectPaymentDataAfter
) private view {
IGraphTallyCollector.SignedRAV memory signedRav = abi.decode(_data, (IGraphTallyCollector.SignedRAV));
Allocation.State memory allocation = subgraphService.getAllocation(
address(uint160(uint256(signedRav.rav.collectionId)))
);
QueryFeeData memory queryFeeData = _queryFeeData(allocation.subgraphDeploymentId);
uint256 tokensProtocol = _paymentCollected.mulPPMRoundUp(queryFeeData.protocolPaymentCut);
uint256 curationTokens = (_paymentCollected - tokensProtocol).mulPPMRoundUp(queryFeeData.curationCut);
uint256 expectedIndexerTokensPayment = _paymentCollected - tokensProtocol - curationTokens;
assertEq(
collectPaymentDataAfter.indexerBalance,
collectPaymentDataBefore.indexerBalance + expectedIndexerTokensPayment
);
assertEq(collectPaymentDataAfter.curationBalance, collectPaymentDataBefore.curationBalance + curationTokens);
// Check locked tokens
uint256 tokensToLock = _paymentCollected * subgraphService.stakeToFeesRatio();
assertEq(collectPaymentDataAfter.lockedTokens, collectPaymentDataBefore.lockedTokens + tokensToLock);
// Check the stake claim
LinkedList.List memory claimsList = _getClaimList(_indexer);
bytes32 claimId = _buildStakeClaimId(_indexer, claimsList.nonce - 1);
IDataServiceFees.StakeClaim memory stakeClaim = _getStakeClaim(claimId);
uint64 disputePeriod = disputeManager.getDisputePeriod();
assertEq(stakeClaim.tokens, tokensToLock);
assertEq(stakeClaim.createdAt, block.timestamp);
assertEq(stakeClaim.releasableAt, block.timestamp + disputePeriod);
assertEq(stakeClaim.nextClaim, bytes32(0));
}
function _verifyIndexingRewardsCollection(
address _indexer,
address allocationId,
IndexingRewardsData memory indexingRewardsData,
CollectPaymentData memory collectPaymentDataBefore,
CollectPaymentData memory collectPaymentDataAfter
) private {
Allocation.State memory allocation = subgraphService.getAllocation(allocationId);
// Check allocation state
assertEq(allocation.accRewardsPending, 0);
uint256 accRewardsPerAllocatedToken = rewardsManager.onSubgraphAllocationUpdate(
allocation.subgraphDeploymentId
);
assertEq(allocation.accRewardsPerAllocatedToken, accRewardsPerAllocatedToken);
assertEq(allocation.lastPOIPresentedAt, block.timestamp);
// Check indexer got paid the correct amount
address rewardsDestination = subgraphService.rewardsDestination(_indexer);
if (rewardsDestination == address(0)) {
// If rewards destination is address zero indexer should get paid to their provision balance
assertEq(
collectPaymentDataAfter.indexerProvisionBalance,
collectPaymentDataBefore.indexerProvisionBalance + indexingRewardsData.tokensIndexerRewards
);
} else {
// If rewards destination is set indexer should get paid to the rewards destination address
assertEq(
collectPaymentDataAfter.rewardsDestinationBalance,
collectPaymentDataBefore.rewardsDestinationBalance + indexingRewardsData.tokensIndexerRewards
);
}
// Check delegation pool got paid the correct amount
assertEq(
collectPaymentDataAfter.delegationPoolBalance,
collectPaymentDataBefore.delegationPoolBalance + indexingRewardsData.tokensDelegationRewards
);
// If after collecting indexing rewards the indexer is over allocated the allcation should close
uint256 tokensAvailable = staking.getTokensAvailable(
_indexer,
address(subgraphService),
subgraphService.getDelegationRatio()
);
if (allocation.tokens <= tokensAvailable) {
// Indexer isn't over allocated so allocation should still be open
assertTrue(allocation.isOpen());
} else {
// Indexer is over allocated so allocation should be closed
assertFalse(allocation.isOpen());
}
}
function _migrateLegacyAllocation(address _indexer, address _allocationId, bytes32 _subgraphDeploymentID) internal {
vm.expectEmit(address(subgraphService));
emit AllocationManager.LegacyAllocationMigrated(_indexer, _allocationId, _subgraphDeploymentID);
subgraphService.migrateLegacyAllocation(_indexer, _allocationId, _subgraphDeploymentID);
LegacyAllocation.State memory afterLegacyAllocation = subgraphService.getLegacyAllocation(_allocationId);
assertEq(afterLegacyAllocation.indexer, _indexer);
assertEq(afterLegacyAllocation.subgraphDeploymentId, _subgraphDeploymentID);
}
/*
* HELPERS
*/
function _createAndStartAllocation(address _indexer, uint256 _tokens) internal {
mint(_indexer, _tokens);
resetPrank(_indexer);
token.approve(address(staking), _tokens);
staking.stakeTo(_indexer, _tokens);
staking.provision(_indexer, address(subgraphService), _tokens, maxSlashingPercentage, disputePeriod);
_register(_indexer, abi.encode("url", "geoHash", address(0)));
(address newIndexerAllocationId, uint256 newIndexerAllocationKey) = makeAddrAndKey("newIndexerAllocationId");
bytes32 digest = subgraphService.encodeAllocationProof(_indexer, newIndexerAllocationId);
(uint8 v, bytes32 r, bytes32 s) = vm.sign(newIndexerAllocationKey, digest);
bytes memory data = abi.encode(subgraphDeployment, _tokens, newIndexerAllocationId, abi.encodePacked(r, s, v));
_startService(_indexer, data);
}
/*
* PRIVATE FUNCTIONS
*/
function _recoverRAVSigner(IGraphTallyCollector.SignedRAV memory _signedRAV) private view returns (address) {
bytes32 messageHash = graphTallyCollector.encodeRAV(_signedRAV.rav);
return ECDSA.recover(messageHash, _signedRAV.signature);
}
function _getClaimList(address _indexer) private view returns (LinkedList.List memory) {
(bytes32 head, bytes32 tail, uint256 nonce, uint256 count) = subgraphService.claimsLists(_indexer);
return LinkedList.List(head, tail, nonce, count);
}
function _buildStakeClaimId(address _indexer, uint256 _nonce) private view returns (bytes32) {
return keccak256(abi.encodePacked(address(subgraphService), _indexer, _nonce));
}
function _getStakeClaim(bytes32 _claimId) private view returns (IDataServiceFees.StakeClaim memory) {
(uint256 tokens, uint256 createdAt, uint256 releasableAt, bytes32 nextClaim) = subgraphService.claims(_claimId);
return IDataServiceFees.StakeClaim(tokens, createdAt, releasableAt, nextClaim);
}
}