Skip to content

Commit

Permalink
agent: add tests for allocation decision consolidation
Browse files Browse the repository at this point in the history
  • Loading branch information
tilacog committed Sep 6, 2023
1 parent 7a1aea4 commit abe55fd
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 8 deletions.
44 changes: 43 additions & 1 deletion packages/indexer-agent/src/__tests__/agent.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { convertSubgraphBasedRulesToDeploymentBased } from '../agent'
import {
convertSubgraphBasedRulesToDeploymentBased,
consolidateAllocationDecisions,
} from '../agent'
import {
INDEXING_RULE_GLOBAL,
IndexingDecisionBasis,
Expand Down Expand Up @@ -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)
})
})
24 changes: 17 additions & 7 deletions packages/indexer-agent/src/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -572,13 +572,8 @@ export class Agent {
}).tryMap(
async ({ indexingRules, networkDeploymentAllocationDecisions }) => {
logger.trace('Resolving target deployments')
const targetDeploymentIDs: Set<SubgraphDeploymentID> = 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<SubgraphDeploymentID> =
consolidateAllocationDecisions(networkDeploymentAllocationDecisions)

// Add offchain subgraphs to the deployment list from rules
Object.values(indexingRules)
Expand Down Expand Up @@ -1337,3 +1332,18 @@ export class Agent {
}
}
}

export interface AllocationDecisionInterface {
toAllocate: boolean
deployment: SubgraphDeploymentID
}
export function consolidateAllocationDecisions(
allocationDecisions: Record<string, AllocationDecisionInterface[]>,
): Set<SubgraphDeploymentID> {
return new Set(
Object.values(allocationDecisions)
.flat()
.filter(decision => decision.toAllocate === true)
.map(decision => decision.deployment),
)
}

0 comments on commit abe55fd

Please sign in to comment.