|
| 1 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 2 | +import { Logger } from '@graphprotocol/common-ts' |
| 3 | +import { DipsManager } from '../dips' |
| 4 | + |
| 5 | +const logger = { |
| 6 | + child: jest.fn().mockReturnThis(), |
| 7 | + info: jest.fn(), |
| 8 | + debug: jest.fn(), |
| 9 | + warn: jest.fn(), |
| 10 | + error: jest.fn(), |
| 11 | + trace: jest.fn(), |
| 12 | +} as unknown as Logger |
| 13 | + |
| 14 | +const mockQuery = jest.fn() |
| 15 | +const mockNetworkSubgraph = { query: mockQuery } as any |
| 16 | + |
| 17 | +const mockGetCollectionInfo = jest.fn() |
| 18 | +const mockCollectEstimateGas = jest.fn() |
| 19 | +const mockCollect = jest.fn() |
| 20 | +const mockGetAgreement = jest.fn() |
| 21 | + |
| 22 | +const mockContracts = { |
| 23 | + RecurringCollector: { |
| 24 | + getCollectionInfo: mockGetCollectionInfo, |
| 25 | + getAgreement: mockGetAgreement, |
| 26 | + }, |
| 27 | + SubgraphService: { |
| 28 | + collect: Object.assign(mockCollect, { |
| 29 | + estimateGas: mockCollectEstimateGas, |
| 30 | + }), |
| 31 | + }, |
| 32 | +} as any |
| 33 | + |
| 34 | +const mockExecuteTransaction = jest.fn() |
| 35 | +const mockTransactionManager = { |
| 36 | + executeTransaction: mockExecuteTransaction, |
| 37 | +} as any |
| 38 | + |
| 39 | +const mockGraphNode = { |
| 40 | + entityCount: jest.fn(), |
| 41 | + proofOfIndexing: jest.fn(), |
| 42 | + blockHashFromNumber: jest.fn(), |
| 43 | +} as any |
| 44 | + |
| 45 | +const mockNetwork = { |
| 46 | + contracts: mockContracts, |
| 47 | + networkSubgraph: mockNetworkSubgraph, |
| 48 | + transactionManager: mockTransactionManager, |
| 49 | + specification: { |
| 50 | + indexerOptions: { |
| 51 | + address: '0x1234567890abcdef1234567890abcdef12345678', |
| 52 | + dipperEndpoint: undefined, |
| 53 | + dipsCollectionTarget: 50, |
| 54 | + }, |
| 55 | + networkIdentifier: 'eip155:421614', |
| 56 | + }, |
| 57 | + networkProvider: { |
| 58 | + getBlockNumber: jest.fn().mockResolvedValue(1000), |
| 59 | + getBlock: jest.fn().mockResolvedValue({ timestamp: Math.floor(Date.now() / 1000) }), |
| 60 | + }, |
| 61 | +} as any |
| 62 | + |
| 63 | +const mockModels = {} as any |
| 64 | + |
| 65 | +function createDipsManager(): DipsManager { |
| 66 | + return new DipsManager(logger, mockModels, mockNetwork, mockGraphNode, null) |
| 67 | +} |
| 68 | + |
| 69 | +// Helper: agreement that was last collected long ago (ready to collect) |
| 70 | +function makeReadyAgreement(id = '0x00000000000000000000000000000001') { |
| 71 | + return { |
| 72 | + id, |
| 73 | + allocationId: '0xaaaa', |
| 74 | + subgraphDeploymentId: |
| 75 | + '0xbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb', |
| 76 | + state: 1, |
| 77 | + lastCollectionAt: '0', // never collected → always ready |
| 78 | + endsAt: '9999999999', |
| 79 | + maxInitialTokens: '1000000', |
| 80 | + maxOngoingTokensPerSecond: '100', |
| 81 | + tokensPerSecond: '50', |
| 82 | + tokensPerEntityPerSecond: '10', |
| 83 | + minSecondsPerCollection: 3600, |
| 84 | + maxSecondsPerCollection: 86400, |
| 85 | + canceledAt: '0', |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +function makeAgreementData() { |
| 90 | + return { |
| 91 | + dataService: '0x0000', |
| 92 | + payer: '0x0000', |
| 93 | + serviceProvider: '0x0000', |
| 94 | + acceptedAt: 1000n, |
| 95 | + lastCollectionAt: 0n, |
| 96 | + endsAt: 9999999999n, |
| 97 | + maxInitialTokens: 1000000n, |
| 98 | + maxOngoingTokensPerSecond: 100n, |
| 99 | + minSecondsPerCollection: 3600, |
| 100 | + maxSecondsPerCollection: 86400, |
| 101 | + updateNonce: 0, |
| 102 | + canceledAt: 0n, |
| 103 | + state: 1, |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +describe('DipsManager.collectAgreementPayments', () => { |
| 108 | + beforeEach(() => { |
| 109 | + jest.clearAllMocks() |
| 110 | + }) |
| 111 | + |
| 112 | + test('skips when no collectable agreements found', async () => { |
| 113 | + mockQuery.mockResolvedValueOnce({ |
| 114 | + data: { indexingAgreements: [] }, |
| 115 | + }) |
| 116 | + |
| 117 | + const dm = createDipsManager() |
| 118 | + await dm.collectAgreementPayments() |
| 119 | + |
| 120 | + expect(mockExecuteTransaction).not.toHaveBeenCalled() |
| 121 | + }) |
| 122 | + |
| 123 | + test('skips agreement when tracker says not ready yet', async () => { |
| 124 | + const recentlyCollected = makeReadyAgreement() |
| 125 | + // Collected very recently — min is 3600, target at 50% is ~45000s |
| 126 | + recentlyCollected.lastCollectionAt = String(Math.floor(Date.now() / 1000) - 100) |
| 127 | + |
| 128 | + mockQuery.mockResolvedValueOnce({ |
| 129 | + data: { indexingAgreements: [recentlyCollected] }, |
| 130 | + }) |
| 131 | + |
| 132 | + const dm = createDipsManager() |
| 133 | + await dm.collectAgreementPayments() |
| 134 | + |
| 135 | + // Should not even call getAgreement since tracker skips it |
| 136 | + expect(mockGetAgreement).not.toHaveBeenCalled() |
| 137 | + expect(mockExecuteTransaction).not.toHaveBeenCalled() |
| 138 | + }) |
| 139 | + |
| 140 | + test('skips agreement when getCollectionInfo says not collectable', async () => { |
| 141 | + mockQuery.mockResolvedValueOnce({ |
| 142 | + data: { indexingAgreements: [makeReadyAgreement()] }, |
| 143 | + }) |
| 144 | + |
| 145 | + mockGetAgreement.mockResolvedValueOnce(makeAgreementData()) |
| 146 | + mockGetCollectionInfo.mockResolvedValueOnce([false, 0n, 1]) |
| 147 | + |
| 148 | + const dm = createDipsManager() |
| 149 | + await dm.collectAgreementPayments() |
| 150 | + |
| 151 | + expect(mockExecuteTransaction).not.toHaveBeenCalled() |
| 152 | + }) |
| 153 | + |
| 154 | + test('collects payment when agreement is ready and collectable', async () => { |
| 155 | + mockQuery.mockResolvedValueOnce({ |
| 156 | + data: { indexingAgreements: [makeReadyAgreement()] }, |
| 157 | + }) |
| 158 | + |
| 159 | + mockGetAgreement.mockResolvedValueOnce(makeAgreementData()) |
| 160 | + mockGetCollectionInfo.mockResolvedValueOnce([true, 7200n, 0]) |
| 161 | + mockGraphNode.entityCount.mockResolvedValueOnce([500]) |
| 162 | + mockGraphNode.blockHashFromNumber.mockResolvedValueOnce('0x' + 'ab'.repeat(32)) |
| 163 | + mockGraphNode.proofOfIndexing.mockResolvedValueOnce('0x' + 'cd'.repeat(32)) |
| 164 | + mockExecuteTransaction.mockResolvedValueOnce({ hash: '0xtxhash', status: 1 }) |
| 165 | + |
| 166 | + const dm = createDipsManager() |
| 167 | + await dm.collectAgreementPayments() |
| 168 | + |
| 169 | + expect(mockExecuteTransaction).toHaveBeenCalledTimes(1) |
| 170 | + }) |
| 171 | + |
| 172 | + test('updates tracker after successful collection', async () => { |
| 173 | + mockQuery |
| 174 | + .mockResolvedValueOnce({ data: { indexingAgreements: [makeReadyAgreement()] } }) |
| 175 | + .mockResolvedValueOnce({ data: { indexingAgreements: [makeReadyAgreement()] } }) |
| 176 | + |
| 177 | + mockGetAgreement.mockResolvedValue(makeAgreementData()) |
| 178 | + mockGetCollectionInfo.mockResolvedValue([true, 7200n, 0]) |
| 179 | + mockGraphNode.entityCount.mockResolvedValue([500]) |
| 180 | + mockGraphNode.blockHashFromNumber.mockResolvedValue('0x' + 'ab'.repeat(32)) |
| 181 | + mockGraphNode.proofOfIndexing.mockResolvedValue('0x' + 'cd'.repeat(32)) |
| 182 | + mockExecuteTransaction.mockResolvedValue({ hash: '0xtxhash', status: 1 }) |
| 183 | + |
| 184 | + const dm = createDipsManager() |
| 185 | + |
| 186 | + // First call: collects |
| 187 | + await dm.collectAgreementPayments() |
| 188 | + expect(mockExecuteTransaction).toHaveBeenCalledTimes(1) |
| 189 | + |
| 190 | + // Second call: tracker should skip (just collected) |
| 191 | + await dm.collectAgreementPayments() |
| 192 | + // Still only 1 call — second was skipped by tracker |
| 193 | + expect(mockExecuteTransaction).toHaveBeenCalledTimes(1) |
| 194 | + }) |
| 195 | + |
| 196 | + test('still attempts collection when POI is unavailable (best effort)', async () => { |
| 197 | + mockQuery.mockResolvedValueOnce({ |
| 198 | + data: { indexingAgreements: [makeReadyAgreement()] }, |
| 199 | + }) |
| 200 | + |
| 201 | + mockGetAgreement.mockResolvedValueOnce(makeAgreementData()) |
| 202 | + mockGetCollectionInfo.mockResolvedValueOnce([true, 7200n, 0]) |
| 203 | + mockGraphNode.entityCount.mockResolvedValueOnce([500]) |
| 204 | + mockGraphNode.blockHashFromNumber.mockResolvedValueOnce('0x' + 'ab'.repeat(32)) |
| 205 | + mockGraphNode.proofOfIndexing.mockResolvedValueOnce(null) // POI unavailable |
| 206 | + mockExecuteTransaction.mockResolvedValueOnce({ hash: '0xtxhash', status: 1 }) |
| 207 | + |
| 208 | + const dm = createDipsManager() |
| 209 | + await dm.collectAgreementPayments() |
| 210 | + |
| 211 | + // Should log warning but still attempt collection with zero POI |
| 212 | + expect(logger.warn).toHaveBeenCalled() |
| 213 | + expect(mockExecuteTransaction).toHaveBeenCalledTimes(1) |
| 214 | + }) |
| 215 | + |
| 216 | + test('handles deterministic error gracefully', async () => { |
| 217 | + mockQuery.mockResolvedValueOnce({ |
| 218 | + data: { indexingAgreements: [makeReadyAgreement()] }, |
| 219 | + }) |
| 220 | + |
| 221 | + mockGetAgreement.mockResolvedValueOnce(makeAgreementData()) |
| 222 | + mockGetCollectionInfo.mockResolvedValueOnce([true, 7200n, 0]) |
| 223 | + mockGraphNode.entityCount.mockResolvedValueOnce([500]) |
| 224 | + mockGraphNode.blockHashFromNumber.mockResolvedValueOnce('0x' + 'ab'.repeat(32)) |
| 225 | + mockGraphNode.proofOfIndexing.mockResolvedValueOnce('0x' + 'cd'.repeat(32)) |
| 226 | + mockExecuteTransaction.mockRejectedValueOnce( |
| 227 | + Object.assign(new Error('revert'), { code: 'CALL_EXCEPTION' }), |
| 228 | + ) |
| 229 | + |
| 230 | + const dm = createDipsManager() |
| 231 | + await dm.collectAgreementPayments() |
| 232 | + |
| 233 | + expect(logger.warn).toHaveBeenCalled() |
| 234 | + }) |
| 235 | +}) |
0 commit comments