From fb8c3c828d2b00c0200d9f552b37eddcc2cd7b68 Mon Sep 17 00:00:00 2001 From: tilacog Date: Wed, 6 Sep 2023 11:19:19 -0300 Subject: [PATCH] agent: add tests for allocation decision consolidation --- packages/indexer-agent/src/__tests__/agent.ts | 44 ++++++++++++++++++- packages/indexer-agent/src/agent.ts | 24 +++++++--- 2 files changed, 60 insertions(+), 8 deletions(-) diff --git a/packages/indexer-agent/src/__tests__/agent.ts b/packages/indexer-agent/src/__tests__/agent.ts index e1f764240..700e15f98 100644 --- a/packages/indexer-agent/src/__tests__/agent.ts +++ b/packages/indexer-agent/src/__tests__/agent.ts @@ -1,4 +1,7 @@ -import { convertSubgraphBasedRulesToDeploymentBased } from '../agent' +import { + convertSubgraphBasedRulesToDeploymentBased, + consolidateAllocationDecisions, +} from '../agent' import { INDEXING_RULE_GLOBAL, IndexingDecisionBasis, @@ -166,3 +169,42 @@ describe('Agent convenience function tests', () => { ).toEqual(inputRules) }) }) + +describe('consolidateAllocationDecisions function', () => { + it('produces a set with unique deployment ids', () => { + const a = new SubgraphDeploymentID( + 'QmXZiV6S13ha6QXq4dmaM3TB4CHcDxBMvGexSNu9Kc28EH', + ) + const b = new SubgraphDeploymentID( + 'QmRKs2ZfuwvmZA3QAWmCqrGUjV9pxtBUDP3wuc6iVGnjA2', + ) + const c = new SubgraphDeploymentID( + 'QmULAfA3eS5yojxeSR2KmbyuiwCGYPjymsFcpa6uYsu6CJ', + ) + + const allocationDecisions = { + 'eip155:0': [ + { deployment: a, toAllocate: false }, + { deployment: b, toAllocate: true }, + ], + 'eip155:1': [ + { deployment: b, toAllocate: true }, + { deployment: c, toAllocate: false }, + ], + 'eip155:2': [ + { deployment: c, toAllocate: true }, + { deployment: a, toAllocate: false }, + ], + } + + const expected = new Set([c, b]) + + const result = consolidateAllocationDecisions(allocationDecisions) + + expect(result).toStrictEqual(expected) + expect(result).toHaveProperty('size', 2) + expect(result).toContain(c) + expect(result).toContain(b) + expect(result).not.toContain(a) + }) +}) diff --git a/packages/indexer-agent/src/agent.ts b/packages/indexer-agent/src/agent.ts index b91801786..2d2033cf2 100644 --- a/packages/indexer-agent/src/agent.ts +++ b/packages/indexer-agent/src/agent.ts @@ -540,13 +540,8 @@ export class Agent { }).tryMap( async ({ indexingRules, networkDeploymentAllocationDecisions }) => { logger.trace('Resolving target deployments') - const targetDeploymentIDs: Set = new Set( - // Concatenate all AllocationDecisions from all protocol networks - Object.values(networkDeploymentAllocationDecisions) - .flat() - .filter(decision => decision.toAllocate === true) - .map(decision => decision.deployment), - ) + const targetDeploymentIDs: Set = + consolidateAllocationDecisions(networkDeploymentAllocationDecisions) // Add offchain subgraphs to the deployment list from rules Object.values(indexingRules) @@ -1261,3 +1256,18 @@ export class Agent { } } } + +export interface AllocationDecisionInterface { + toAllocate: boolean + deployment: SubgraphDeploymentID +} +export function consolidateAllocationDecisions( + allocationDecisions: Record, +): Set { + return new Set( + Object.values(allocationDecisions) + .flat() + .filter(decision => decision.toAllocate === true) + .map(decision => decision.deployment), + ) +}