diff --git a/packages/subgraph-service/contracts/utilities/AllocationManager.sol b/packages/subgraph-service/contracts/utilities/AllocationManager.sol index 5bb22cbe2..4ab5fd418 100644 --- a/packages/subgraph-service/contracts/utilities/AllocationManager.sol +++ b/packages/subgraph-service/contracts/utilities/AllocationManager.sol @@ -57,6 +57,7 @@ abstract contract AllocationManager is EIP712Upgradeable, GraphDirectory, Alloca * @param tokensIndexerRewards The amount of tokens collected for the indexer * @param tokensDelegationRewards The amount of tokens collected for delegators * @param poi The POI presented + * @param currentEpoch The current epoch */ event IndexingRewardsCollected( address indexed indexer, @@ -65,7 +66,8 @@ abstract contract AllocationManager is EIP712Upgradeable, GraphDirectory, Alloca uint256 tokensRewards, uint256 tokensIndexerRewards, uint256 tokensDelegationRewards, - bytes32 poi + bytes32 poi, + uint256 currentEpoch ); /** @@ -321,7 +323,8 @@ abstract contract AllocationManager is EIP712Upgradeable, GraphDirectory, Alloca tokensRewards, tokensIndexerRewards, tokensDelegationRewards, - _poi + _poi, + _graphEpochManager().currentEpoch() ); // Check if the indexer is over-allocated and close the allocation if necessary diff --git a/packages/subgraph-service/test/SubgraphBaseTest.t.sol b/packages/subgraph-service/test/SubgraphBaseTest.t.sol index f1aa9b96c..6ab0c6d24 100644 --- a/packages/subgraph-service/test/SubgraphBaseTest.t.sol +++ b/packages/subgraph-service/test/SubgraphBaseTest.t.sol @@ -26,6 +26,7 @@ import { Utils } from "./utils/Utils.sol"; import { MockCuration } from "./mocks/MockCuration.sol"; import { MockGRTToken } from "./mocks/MockGRTToken.sol"; import { MockRewardsManager } from "./mocks/MockRewardsManager.sol"; +import { MockEpochManager } from "./mocks/MockEpochManager.sol"; abstract contract SubgraphBaseTest is Utils, Constants { @@ -50,6 +51,7 @@ abstract contract SubgraphBaseTest is Utils, Constants { MockCuration curation; MockGRTToken token; MockRewardsManager rewardsManager; + MockEpochManager epochManager; /* Users */ @@ -91,6 +93,7 @@ abstract contract SubgraphBaseTest is Utils, Constants { GraphProxy stakingProxy = new GraphProxy(address(0), address(proxyAdmin)); rewardsManager = new MockRewardsManager(token, rewardsPerSignal, rewardsPerSubgraphAllocationUpdate); curation = new MockCuration(); + epochManager = new MockEpochManager(); // GraphPayments predict address bytes32 saltGraphPayments = keccak256("GraphPaymentsSalt"); @@ -126,7 +129,7 @@ abstract contract SubgraphBaseTest is Utils, Constants { controller.setContractProxy(keccak256("RewardsManager"), address(rewardsManager)); controller.setContractProxy(keccak256("GraphPayments"), predictedGraphPaymentsAddress); controller.setContractProxy(keccak256("PaymentsEscrow"), predictedEscrowAddress); - controller.setContractProxy(keccak256("EpochManager"), makeAddr("EpochManager")); + controller.setContractProxy(keccak256("EpochManager"), address(epochManager)); controller.setContractProxy(keccak256("GraphTokenGateway"), makeAddr("GraphTokenGateway")); controller.setContractProxy(keccak256("GraphProxyAdmin"), makeAddr("GraphProxyAdmin")); controller.setContractProxy(keccak256("Curation"), address(curation)); @@ -187,6 +190,7 @@ abstract contract SubgraphBaseTest is Utils, Constants { subgraphService.transferOwnership(users.governor); resetPrank(users.governor); staking.setMaxThawingPeriod(MAX_THAWING_PERIOD); + epochManager.setEpochLength(EPOCH_LENGTH); subgraphService.setMaxPOIStaleness(maxPOIStaleness); subgraphService.setCurationCut(curationCut); } diff --git a/packages/subgraph-service/test/mocks/MockEpochManager.sol b/packages/subgraph-service/test/mocks/MockEpochManager.sol new file mode 100644 index 000000000..060a92e21 --- /dev/null +++ b/packages/subgraph-service/test/mocks/MockEpochManager.sol @@ -0,0 +1,63 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +pragma solidity 0.8.27; + +import { IEpochManager } from "@graphprotocol/contracts/contracts/epochs/IEpochManager.sol"; + +contract MockEpochManager is IEpochManager { + // -- Variables -- + + uint256 public epochLength; + uint256 public lastRunEpoch; + uint256 public lastLengthUpdateEpoch; + uint256 public lastLengthUpdateBlock; + + // -- Configuration -- + + function setEpochLength(uint256 _epochLength) public { + lastLengthUpdateEpoch = 1; + lastLengthUpdateBlock = blockNum(); + epochLength = _epochLength; + } + + // -- Epochs + + function runEpoch() public { + lastRunEpoch = currentEpoch(); + } + + // -- Getters -- + + function isCurrentEpochRun() public view returns (bool) { + return lastRunEpoch == currentEpoch(); + } + + function blockNum() public view returns (uint256) { + return block.number; + } + + function blockHash(uint256 _block) public view returns (bytes32) { + return blockhash(_block); + } + + function currentEpoch() public view returns (uint256) { + return lastLengthUpdateEpoch + epochsSinceUpdate(); + } + + function currentEpochBlock() public view returns (uint256) { + return lastLengthUpdateBlock + (epochsSinceUpdate() * epochLength); + } + + function currentEpochBlockSinceStart() public view returns (uint256) { + return blockNum() - currentEpochBlock(); + } + + function epochsSince(uint256 _epoch) public view returns (uint256) { + uint256 epoch = currentEpoch(); + return _epoch < epoch ? (epoch - _epoch) : 0; + } + + function epochsSinceUpdate() public view returns (uint256) { + return (blockNum() - lastLengthUpdateBlock) / epochLength; + } +} diff --git a/packages/subgraph-service/test/subgraphService/SubgraphService.t.sol b/packages/subgraph-service/test/subgraphService/SubgraphService.t.sol index 91c486a7b..e62ce8211 100644 --- a/packages/subgraph-service/test/subgraphService/SubgraphService.t.sol +++ b/packages/subgraph-service/test/subgraphService/SubgraphService.t.sol @@ -273,7 +273,8 @@ contract SubgraphServiceTest is SubgraphServiceSharedTest { paymentCollected, indexingRewardsData.tokensIndexerRewards, indexingRewardsData.tokensDelegationRewards, - indexingRewardsData.poi + indexingRewardsData.poi, + epochManager.currentEpoch() ); } diff --git a/packages/subgraph-service/test/utils/Constants.sol b/packages/subgraph-service/test/utils/Constants.sol index 1dbfd082e..e70ca8b41 100644 --- a/packages/subgraph-service/test/utils/Constants.sol +++ b/packages/subgraph-service/test/utils/Constants.sol @@ -4,6 +4,7 @@ pragma solidity 0.8.27; abstract contract Constants { uint256 internal constant MAX_TOKENS = 10_000_000_000 ether; uint256 internal constant MAX_PPM = 1_000_000; + uint256 internal constant EPOCH_LENGTH = 1; // Dispute Manager uint64 internal constant disputePeriod = 300; // 5 minutes uint256 internal constant disputeDeposit = 100 ether; // 100 GRT @@ -26,4 +27,4 @@ abstract contract Constants { // RewardsMananger parameters uint256 public constant rewardsPerSignal = 10000; uint256 public constant rewardsPerSubgraphAllocationUpdate = 1000; -} \ No newline at end of file +}