Skip to content
Open
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
13 changes: 11 additions & 2 deletions packages/indexer-common/src/indexer-management/allocations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
uniqueAllocationID,
upsertIndexingRule,
} from '@graphprotocol/indexer-common'
import { preprocessRules } from '../subgraphs'

import {
BigNumber,
Expand Down Expand Up @@ -1037,8 +1038,16 @@ export class AllocationManager {
`SHOULD BE UNREACHABLE: No matching subgraphDeployment (${subgraphDeploymentID.ipfsHash}) found on the network`,
)
}
return isDeploymentWorthAllocatingTowards(logger, subgraphDeployment, indexingRules)
.toAllocate

const { deploymentRulesMap, globalRule } = preprocessRules(indexingRules)

return isDeploymentWorthAllocatingTowards(
logger,
subgraphDeployment,
indexingRules,
deploymentRulesMap,
globalRule,
).toAllocate
}

// Calculates the balance (GRT delta) of a single Action.
Expand Down
40 changes: 31 additions & 9 deletions packages/indexer-common/src/subgraphs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,29 +192,51 @@ export class AllocationDecision {
}
}

export interface PreprocessedRules {
deploymentRulesMap: Map<string, IndexingRuleAttributes>
globalRule: IndexingRuleAttributes | undefined
}

export function preprocessRules(rules: IndexingRuleAttributes[]): PreprocessedRules {
const globalRule = rules.find((rule) => rule.identifier === INDEXING_RULE_GLOBAL)
const deploymentRulesMap = new Map<string, IndexingRuleAttributes>()

rules.forEach((rule) => {
if (rule.identifierType === SubgraphIdentifierType.DEPLOYMENT) {
deploymentRulesMap.set(rule.identifier, rule)
}
})

return { deploymentRulesMap, globalRule }
}

export function evaluateDeployments(
logger: Logger,
networkDeployments: SubgraphDeployment[],
rules: IndexingRuleAttributes[],
): AllocationDecision[] {
const { deploymentRulesMap, globalRule } = preprocessRules(rules)

return networkDeployments.map((deployment) =>
isDeploymentWorthAllocatingTowards(logger, deployment, rules),
isDeploymentWorthAllocatingTowards(
logger,
deployment,
rules,
deploymentRulesMap,
globalRule,
),
)
}

export function isDeploymentWorthAllocatingTowards(
logger: Logger,
deployment: SubgraphDeployment,
rules: IndexingRuleAttributes[],
deploymentRulesMap: Map<string, IndexingRuleAttributes>,
globalRule: IndexingRuleAttributes | undefined,
): AllocationDecision {
const globalRule = rules.find((rule) => rule.identifier === INDEXING_RULE_GLOBAL)
const deploymentRule =
rules
.filter((rule) => rule.identifierType == SubgraphIdentifierType.DEPLOYMENT)
.find(
(rule) =>
new SubgraphDeploymentID(rule.identifier).bytes32 === deployment.id.bytes32,
) || globalRule
// Use the pre-processed map for O(1) lookup
const deploymentRule = deploymentRulesMap.get(deployment.id.ipfsHash) || globalRule

logger.trace('Evaluating whether subgraphDeployment is worth allocating towards', {
deployment,
Expand Down
Loading