|
| 1 | +import { createLogger, Logger } from '@graphprotocol/common-ts' |
| 2 | +import { assertNotOverAllocated } from '../over-allocation' |
| 3 | +import { IndexerError, IndexerErrorCode } from '../../errors' |
| 4 | + |
| 5 | +// eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 6 | +declare const __LOG_LEVEL__: any |
| 7 | + |
| 8 | +const indexer = '0x0000000000000000000000000000000000000001' |
| 9 | +const allocationId = '0x000000000000000000000000000000000000000a' |
| 10 | +const subgraphServiceAddress = '0x00000000000000000000000000000000000000ff' |
| 11 | + |
| 12 | +interface MockSubgraphService { |
| 13 | + isOverAllocated: jest.Mock |
| 14 | + allocationProvisionTracker: jest.Mock |
| 15 | + getDelegationRatio: jest.Mock |
| 16 | + target: string |
| 17 | +} |
| 18 | + |
| 19 | +interface MockHorizonStaking { |
| 20 | + getTokensAvailable: jest.Mock |
| 21 | +} |
| 22 | + |
| 23 | +const buildContracts = ( |
| 24 | + overrides: { |
| 25 | + isOverAllocated?: boolean |
| 26 | + allocatedTokens?: bigint |
| 27 | + delegationRatio?: bigint |
| 28 | + tokensAvailable?: bigint |
| 29 | + } = {}, |
| 30 | +): { |
| 31 | + contracts: { SubgraphService: MockSubgraphService; HorizonStaking: MockHorizonStaking } |
| 32 | + subgraphService: MockSubgraphService |
| 33 | + horizonStaking: MockHorizonStaking |
| 34 | +} => { |
| 35 | + const subgraphService: MockSubgraphService = { |
| 36 | + isOverAllocated: jest.fn().mockResolvedValue(overrides.isOverAllocated ?? false), |
| 37 | + allocationProvisionTracker: jest |
| 38 | + .fn() |
| 39 | + .mockResolvedValue(overrides.allocatedTokens ?? 0n), |
| 40 | + getDelegationRatio: jest.fn().mockResolvedValue(overrides.delegationRatio ?? 1n), |
| 41 | + target: subgraphServiceAddress, |
| 42 | + } |
| 43 | + const horizonStaking: MockHorizonStaking = { |
| 44 | + getTokensAvailable: jest.fn().mockResolvedValue(overrides.tokensAvailable ?? 0n), |
| 45 | + } |
| 46 | + return { |
| 47 | + contracts: { SubgraphService: subgraphService, HorizonStaking: horizonStaking }, |
| 48 | + subgraphService, |
| 49 | + horizonStaking, |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +describe('assertNotOverAllocated', () => { |
| 54 | + let logger: Logger |
| 55 | + |
| 56 | + beforeAll(() => { |
| 57 | + logger = createLogger({ |
| 58 | + name: 'over-allocation-test', |
| 59 | + async: false, |
| 60 | + level: __LOG_LEVEL__ ?? 'error', |
| 61 | + }) |
| 62 | + }) |
| 63 | + |
| 64 | + it('returns without throwing when the indexer is not over-allocated', async () => { |
| 65 | + const { contracts, subgraphService, horizonStaking } = buildContracts({ |
| 66 | + isOverAllocated: false, |
| 67 | + }) |
| 68 | + |
| 69 | + await expect( |
| 70 | + assertNotOverAllocated( |
| 71 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 72 | + contracts as any, |
| 73 | + indexer, |
| 74 | + logger, |
| 75 | + allocationId, |
| 76 | + ), |
| 77 | + ).resolves.toBeUndefined() |
| 78 | + |
| 79 | + expect(subgraphService.isOverAllocated).toHaveBeenCalledWith(indexer) |
| 80 | + // None of the diagnostic reads should fire on the happy path — they are |
| 81 | + // only needed to compose the error message when over-allocation is true. |
| 82 | + expect(subgraphService.allocationProvisionTracker).not.toHaveBeenCalled() |
| 83 | + expect(subgraphService.getDelegationRatio).not.toHaveBeenCalled() |
| 84 | + expect(horizonStaking.getTokensAvailable).not.toHaveBeenCalled() |
| 85 | + }) |
| 86 | + |
| 87 | + it('throws IE090 with the over-allocated delta when over-allocated', async () => { |
| 88 | + // 1000 GRT allocated, 600 GRT available => 400 GRT over. |
| 89 | + const { contracts } = buildContracts({ |
| 90 | + isOverAllocated: true, |
| 91 | + allocatedTokens: 1000n * 10n ** 18n, |
| 92 | + delegationRatio: 2n, |
| 93 | + tokensAvailable: 600n * 10n ** 18n, |
| 94 | + }) |
| 95 | + |
| 96 | + let captured: unknown |
| 97 | + try { |
| 98 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 99 | + await assertNotOverAllocated(contracts as any, indexer, logger, allocationId) |
| 100 | + } catch (err) { |
| 101 | + captured = err |
| 102 | + } |
| 103 | + |
| 104 | + expect(captured).toBeInstanceOf(IndexerError) |
| 105 | + const err = captured as IndexerError |
| 106 | + expect(err.code).toBe(IndexerErrorCode.IE090) |
| 107 | + // Spot-check the cause string contains the GRT delta and the actionable |
| 108 | + // hint pointing operators at the close path (which still collects rewards). |
| 109 | + const cause = String(err.cause) |
| 110 | + expect(cause).toContain('400.0') |
| 111 | + expect(cause).toContain('graph indexer allocations close') |
| 112 | + }) |
| 113 | + |
| 114 | + it('passes the SubgraphService address as the verifier to getTokensAvailable', async () => { |
| 115 | + const { contracts, horizonStaking } = buildContracts({ |
| 116 | + isOverAllocated: true, |
| 117 | + allocatedTokens: 100n, |
| 118 | + delegationRatio: 3n, |
| 119 | + tokensAvailable: 50n, |
| 120 | + }) |
| 121 | + |
| 122 | + await expect( |
| 123 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 124 | + assertNotOverAllocated(contracts as any, indexer, logger, allocationId), |
| 125 | + ).rejects.toBeInstanceOf(IndexerError) |
| 126 | + |
| 127 | + expect(horizonStaking.getTokensAvailable).toHaveBeenCalledWith( |
| 128 | + indexer, |
| 129 | + subgraphServiceAddress, |
| 130 | + 3n, |
| 131 | + ) |
| 132 | + }) |
| 133 | + |
| 134 | + it('clamps the over-allocated amount to zero if reads race to a negative delta', async () => { |
| 135 | + // tokensAvailable > allocatedTokens shouldn't happen when isOverAllocated |
| 136 | + // is true, but the three reads aren't atomic; defend against a negative |
| 137 | + // bigint formatting into the error message. |
| 138 | + const { contracts } = buildContracts({ |
| 139 | + isOverAllocated: true, |
| 140 | + allocatedTokens: 100n, |
| 141 | + delegationRatio: 1n, |
| 142 | + tokensAvailable: 200n, |
| 143 | + }) |
| 144 | + |
| 145 | + let captured: unknown |
| 146 | + try { |
| 147 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 148 | + await assertNotOverAllocated(contracts as any, indexer, logger, allocationId) |
| 149 | + } catch (err) { |
| 150 | + captured = err |
| 151 | + } |
| 152 | + |
| 153 | + const err = captured as IndexerError |
| 154 | + expect(err.code).toBe(IndexerErrorCode.IE090) |
| 155 | + const cause = String(err.cause) |
| 156 | + expect(cause).toContain('0.0 GRT') |
| 157 | + expect(cause).not.toContain('-') |
| 158 | + }) |
| 159 | +}) |
0 commit comments