Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

agent: add tests for allocation decision consolidation #767

Merged
merged 1 commit into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -540,13 +540,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 @@ -1261,3 +1256,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),
)
}
Loading