From 07171a8ce201b8bd5a056729c357bb1dc149a2c8 Mon Sep 17 00:00:00 2001 From: MoonBoi9001 Date: Tue, 3 Nov 2026 14:58:39 +0100 Subject: [PATCH 1/3] fix(actions): allow resizing an allocation down to 0 GRT The resize action refused any amount that was not positive, but the SubgraphService contract accepts 0 and keeps the allocation open with no stake. Only negative amounts are refused now. --- .../__tests__/resize-amount.test.ts | 84 +++++++++++++++++++ .../src/indexer-management/allocations.ts | 7 +- 2 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 packages/indexer-common/src/indexer-management/__tests__/resize-amount.test.ts diff --git a/packages/indexer-common/src/indexer-management/__tests__/resize-amount.test.ts b/packages/indexer-common/src/indexer-management/__tests__/resize-amount.test.ts new file mode 100644 index 000000000..68b6224a1 --- /dev/null +++ b/packages/indexer-common/src/indexer-management/__tests__/resize-amount.test.ts @@ -0,0 +1,84 @@ +import { createLogger, Logger } from '@graphprotocol/common-ts' +import { AllocationManager } from '../allocations' +import { IndexerErrorCode } from '../../errors' + +// eslint-disable-next-line @typescript-eslint/no-explicit-any +declare const __LOG_LEVEL__: any + +const indexer = '0x0000000000000000000000000000000000000001' +const allocationId = '0x000000000000000000000000000000000000000a' +const protocolNetwork = 'eip155:42161' + +// Only the on-chain allocation lookup is reached when preparing a resize, so the rest of +// the manager's dependencies can stay empty. +const buildManager = ( + logger: Logger, + closedAt = 0n, +): { manager: AllocationManager; getAllocation: jest.Mock } => { + const getAllocation = jest.fn().mockResolvedValue({ closedAt, indexer }) + const network = { contracts: { SubgraphService: { getAllocation } } } + const manager = new AllocationManager( + logger, + // eslint-disable-next-line @typescript-eslint/no-explicit-any + {} as any, + // eslint-disable-next-line @typescript-eslint/no-explicit-any + {} as any, + // eslint-disable-next-line @typescript-eslint/no-explicit-any + network as any, + ) + return { manager, getAllocation } +} + +describe('AllocationManager.prepareResizeParams amount checks', () => { + let logger: Logger + + beforeAll(() => { + logger = createLogger({ + name: 'resize-amount-test', + async: false, + level: __LOG_LEVEL__ ?? 'error', + }) + }) + + it('accepts a resize down to 0 GRT', async () => { + const { manager, getAllocation } = buildManager(logger) + + await expect( + manager.prepareResizeParams(logger, allocationId, 0n, 1, protocolNetwork), + ).resolves.toEqual({ + protocolNetwork, + actionID: 1, + allocationID: allocationId, + newAmount: 0n, + indexer, + }) + expect(getAllocation).toHaveBeenCalledWith(allocationId) + }) + + it('accepts a positive amount', async () => { + const { manager } = buildManager(logger) + const newAmount = 10_000n * 10n ** 18n + + await expect( + manager.prepareResizeParams(logger, allocationId, newAmount, 2, protocolNetwork), + ).resolves.toMatchObject({ newAmount }) + }) + + it('rejects a negative amount with IE061', async () => { + const { manager } = buildManager(logger) + + // The manager throws through the package's built entry point, so its error class is a + // different copy from the one in src; the error code is what identifies it + await expect( + manager.prepareResizeParams(logger, allocationId, -1n, 3, protocolNetwork), + ).rejects.toMatchObject({ code: IndexerErrorCode.IE061 }) + }) + + it('still rejects a resize to 0 on an allocation that has closed', async () => { + const { manager } = buildManager(logger, 123n) + + await expect( + manager.prepareResizeParams(logger, allocationId, 0n, 4, protocolNetwork), + ).rejects.toMatchObject({ code: IndexerErrorCode.IE065 }) + }) +}) diff --git a/packages/indexer-common/src/indexer-management/allocations.ts b/packages/indexer-common/src/indexer-management/allocations.ts index 8fe56d59f..5f7e90922 100644 --- a/packages/indexer-common/src/indexer-management/allocations.ts +++ b/packages/indexer-common/src/indexer-management/allocations.ts @@ -1469,11 +1469,12 @@ export class AllocationManager { throw indexerError(IndexerErrorCode.IE065, 'Allocation has already been closed') } - // Validate amount is positive - if (newAmount <= 0n) { + // Resizing to 0 is allowed: the SubgraphService keeps the allocation open with no + // stake, and whatever it had earned stays claimable by its next POI + if (newAmount < 0n) { throw indexerError( IndexerErrorCode.IE061, - `Invalid resize amount: ${newAmount.toString()}. Amount must be positive.`, + `Invalid resize amount: ${newAmount.toString()}. Amount cannot be negative.`, ) } From d724e4d0f9d8e1f0c87c7519b91a95622bd621b2 Mon Sep 17 00:00:00 2001 From: MoonBoi9001 Date: Tue, 3 Nov 2026 15:06:56 +0100 Subject: [PATCH 2/3] fix(actions): refuse a resize to the size an allocation already has The contract reverts such a resize, and as actions go out in 1 multicall that revert failed the whole batch. Accepting 0 made 0 to 0 reachable, so the action now fails on its own instead. --- .../__tests__/resize-amount.test.ts | 34 +++++++++++++++---- .../src/indexer-management/allocations.ts | 17 +++++++--- 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/packages/indexer-common/src/indexer-management/__tests__/resize-amount.test.ts b/packages/indexer-common/src/indexer-management/__tests__/resize-amount.test.ts index 68b6224a1..c7481a3d7 100644 --- a/packages/indexer-common/src/indexer-management/__tests__/resize-amount.test.ts +++ b/packages/indexer-common/src/indexer-management/__tests__/resize-amount.test.ts @@ -10,12 +10,15 @@ const allocationId = '0x000000000000000000000000000000000000000a' const protocolNetwork = 'eip155:42161' // Only the on-chain allocation lookup is reached when preparing a resize, so the rest of -// the manager's dependencies can stay empty. +// the manager's dependencies can stay empty. The allocation holds 5,000 GRT unless a test +// says otherwise. +const GRT = 10n ** 18n + const buildManager = ( logger: Logger, - closedAt = 0n, + { closedAt = 0n, tokens = 5_000n * GRT }: { closedAt?: bigint; tokens?: bigint } = {}, ): { manager: AllocationManager; getAllocation: jest.Mock } => { - const getAllocation = jest.fn().mockResolvedValue({ closedAt, indexer }) + const getAllocation = jest.fn().mockResolvedValue({ closedAt, indexer, tokens }) const network = { contracts: { SubgraphService: { getAllocation } } } const manager = new AllocationManager( logger, @@ -57,25 +60,42 @@ describe('AllocationManager.prepareResizeParams amount checks', () => { it('accepts a positive amount', async () => { const { manager } = buildManager(logger) - const newAmount = 10_000n * 10n ** 18n + const newAmount = 10_000n * GRT await expect( manager.prepareResizeParams(logger, allocationId, newAmount, 2, protocolNetwork), ).resolves.toMatchObject({ newAmount }) }) - it('rejects a negative amount with IE061', async () => { - const { manager } = buildManager(logger) + it('rejects a negative amount with IE061 before reading the allocation', async () => { + const { manager, getAllocation } = buildManager(logger) // The manager throws through the package's built entry point, so its error class is a // different copy from the one in src; the error code is what identifies it await expect( manager.prepareResizeParams(logger, allocationId, -1n, 3, protocolNetwork), ).rejects.toMatchObject({ code: IndexerErrorCode.IE061 }) + expect(getAllocation).not.toHaveBeenCalled() + }) + + it('rejects a resize to 0 on an allocation that already holds 0 GRT', async () => { + const { manager } = buildManager(logger, { tokens: 0n }) + + await expect( + manager.prepareResizeParams(logger, allocationId, 0n, 5, protocolNetwork), + ).rejects.toMatchObject({ code: IndexerErrorCode.IE061 }) + }) + + it('rejects any resize to the size the allocation already has', async () => { + const { manager } = buildManager(logger, { tokens: 5_000n * GRT }) + + await expect( + manager.prepareResizeParams(logger, allocationId, 5_000n * GRT, 6, protocolNetwork), + ).rejects.toMatchObject({ code: IndexerErrorCode.IE061 }) }) it('still rejects a resize to 0 on an allocation that has closed', async () => { - const { manager } = buildManager(logger, 123n) + const { manager } = buildManager(logger, { closedAt: 123n }) await expect( manager.prepareResizeParams(logger, allocationId, 0n, 4, protocolNetwork), diff --git a/packages/indexer-common/src/indexer-management/allocations.ts b/packages/indexer-common/src/indexer-management/allocations.ts index 5f7e90922..08289c57a 100644 --- a/packages/indexer-common/src/indexer-management/allocations.ts +++ b/packages/indexer-common/src/indexer-management/allocations.ts @@ -1462,6 +1462,15 @@ export class AllocationManager { newAmount: newAmount.toString(), }) + // Resizing to 0 is allowed: the SubgraphService keeps the allocation open with no + // stake, and what it had earned stays claimable by its next POI unless it is stale + if (newAmount < 0n) { + throw indexerError( + IndexerErrorCode.IE061, + `Invalid resize amount: ${newAmount.toString()}. Amount cannot be negative.`, + ) + } + // Validate the allocation is still active on chain const allocationData = await this.network.contracts.SubgraphService.getAllocation(allocationID) @@ -1469,12 +1478,12 @@ export class AllocationManager { throw indexerError(IndexerErrorCode.IE065, 'Allocation has already been closed') } - // Resizing to 0 is allowed: the SubgraphService keeps the allocation open with no - // stake, and whatever it had earned stays claimable by its next POI - if (newAmount < 0n) { + // The contract reverts a resize to the size the allocation already has, and that + // revert would fail every other action sent in the same multicall + if (newAmount === allocationData.tokens) { throw indexerError( IndexerErrorCode.IE061, - `Invalid resize amount: ${newAmount.toString()}. Amount cannot be negative.`, + `Invalid resize amount: allocation already holds ${formatGRT(newAmount)} GRT.`, ) } From 740821cde11dff290ddc3ea0276c8d39fd130fb8 Mon Sep 17 00:00:00 2001 From: MoonBoi9001 Date: Tue, 3 Nov 2026 18:59:34 +0100 Subject: [PATCH 3/3] test(actions): build resize test amounts with parseGRT The resize tests declared their own GRT unit, and the comment describing the test helper sat above that constant instead. They now use parseGRT, and the comment sits on the helper. --- .../__tests__/resize-amount.test.ts | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/packages/indexer-common/src/indexer-management/__tests__/resize-amount.test.ts b/packages/indexer-common/src/indexer-management/__tests__/resize-amount.test.ts index c7481a3d7..c0c90ae2a 100644 --- a/packages/indexer-common/src/indexer-management/__tests__/resize-amount.test.ts +++ b/packages/indexer-common/src/indexer-management/__tests__/resize-amount.test.ts @@ -1,4 +1,4 @@ -import { createLogger, Logger } from '@graphprotocol/common-ts' +import { createLogger, Logger, parseGRT } from '@graphprotocol/common-ts' import { AllocationManager } from '../allocations' import { IndexerErrorCode } from '../../errors' @@ -12,11 +12,12 @@ const protocolNetwork = 'eip155:42161' // Only the on-chain allocation lookup is reached when preparing a resize, so the rest of // the manager's dependencies can stay empty. The allocation holds 5,000 GRT unless a test // says otherwise. -const GRT = 10n ** 18n - const buildManager = ( logger: Logger, - { closedAt = 0n, tokens = 5_000n * GRT }: { closedAt?: bigint; tokens?: bigint } = {}, + { + closedAt = 0n, + tokens = parseGRT('5000'), + }: { closedAt?: bigint; tokens?: bigint } = {}, ): { manager: AllocationManager; getAllocation: jest.Mock } => { const getAllocation = jest.fn().mockResolvedValue({ closedAt, indexer, tokens }) const network = { contracts: { SubgraphService: { getAllocation } } } @@ -60,7 +61,7 @@ describe('AllocationManager.prepareResizeParams amount checks', () => { it('accepts a positive amount', async () => { const { manager } = buildManager(logger) - const newAmount = 10_000n * GRT + const newAmount = parseGRT('10000') await expect( manager.prepareResizeParams(logger, allocationId, newAmount, 2, protocolNetwork), @@ -87,10 +88,16 @@ describe('AllocationManager.prepareResizeParams amount checks', () => { }) it('rejects any resize to the size the allocation already has', async () => { - const { manager } = buildManager(logger, { tokens: 5_000n * GRT }) + const { manager } = buildManager(logger, { tokens: parseGRT('5000') }) await expect( - manager.prepareResizeParams(logger, allocationId, 5_000n * GRT, 6, protocolNetwork), + manager.prepareResizeParams( + logger, + allocationId, + parseGRT('5000'), + 6, + protocolNetwork, + ), ).rejects.toMatchObject({ code: IndexerErrorCode.IE061 }) })