Skip to content

Commit

Permalink
agent, common: Add support for local querying of TAP and Epoch subgraphs
Browse files Browse the repository at this point in the history
  • Loading branch information
fordN committed Dec 13, 2024
1 parent b1bdaf6 commit cdc4981
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 35 deletions.
78 changes: 48 additions & 30 deletions packages/indexer-agent/src/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ export class Agent {
async ({ network, operator }: NetworkAndOperator) => {
try {
await operator.ensureGlobalIndexingRule()
await this.ensureNetworkSubgraphIsIndexing(network)
await this.ensureAllSubgraphsIndexing(network)
await network.register()
} catch (err) {
this.logger.critical(
Expand Down Expand Up @@ -1241,38 +1241,56 @@ export class Agent {
)
}

// TODO: This could be a initialization check inside Network.create() once/if the Indexer Service
// uses Network instances.
async ensureNetworkSubgraphIsIndexing(network: Network) {
// TODO: After indexer-service deprecation: Move to be an initialization check inside Network.create()
async ensureSubgraphIndexing(deployment: string, networkIdentifier: string) {
try {
// TODO: Check both the local deployment and the external subgraph endpoint
// Make sure the subgraph is being indexed
await this.graphNode.ensure(
`indexer-agent/${deployment.slice(-10)}`,
new SubgraphDeploymentID(deployment),
)

// Validate if the Network Subgraph belongs to the current provider's network.
// This check must be performed after we ensure the Network Subgraph is being indexed.
await validateProviderNetworkIdentifier(
networkIdentifier,
deployment,
this.graphNode,
this.logger,
)
} catch (e) {
this.logger.warn(
'Failed to deploy and validate Network Subgraph on index-nodes. Will use external subgraph endpoint instead',
e,
)
}
}
async ensureAllSubgraphsIndexing(network: Network) {
// Network subgraph
if (
network.specification.subgraphs.networkSubgraph.deployment !== undefined
) {
try {
// TODO: Check both the local deployment and the external subgraph endpoint
// Make sure the network subgraph is being indexed
await this.graphNode.ensure(
`indexer-agent/${network.specification.subgraphs.networkSubgraph.deployment.slice(
-10,
)}`,
new SubgraphDeploymentID(
network.specification.subgraphs.networkSubgraph.deployment,
),
)

// Validate if the Network Subgraph belongs to the current provider's network.
// This check must be performed after we ensure the Network Subgraph is being indexed.
await validateProviderNetworkIdentifier(
network.specification.networkIdentifier,
network.specification.subgraphs.networkSubgraph.deployment,
this.graphNode,
this.logger,
)
} catch (e) {
this.logger.warn(
'Failed to deploy and validate Network Subgraph on index-nodes. Will use external subgraph endpoint instead',
e,
)
}
await this.ensureSubgraphIndexing(
network.specification.subgraphs.networkSubgraph.deployment,
network.specification.networkIdentifier,
)
}
// Epoch subgraph
if (
network.specification.subgraphs.epochSubgraph.deployment !== undefined
) {
await this.ensureSubgraphIndexing(
network.specification.subgraphs.epochSubgraph.deployment,
network.specification.networkIdentifier,
)
}
// TAP subgraph
if (network.specification.subgraphs.tapSubgraph?.deployment !== undefined) {
await this.ensureSubgraphIndexing(
network.specification.subgraphs.tapSubgraph.deployment,
network.specification.networkIdentifier,
)
}
}
}
Expand Down
18 changes: 15 additions & 3 deletions packages/indexer-agent/src/commands/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export const start = {
},
})
.option('network-subgraph-deployment', {
description: 'Network subgraph deployment',
description: 'Network subgraph deployment (for local hosting)',
array: false,
type: 'string',
group: 'Network Subgraph',
Expand All @@ -151,6 +151,12 @@ export const start = {
type: 'string',
group: 'Network Subgraph',
})
.option('tap-subgraph-deployment', {
description: 'TAP subgraph deployment (for local hosting)',
array: false,
type: 'string',
group: 'TAP Subgraph',
})
.option('tap-subgraph-endpoint', {
description: 'Endpoint to query the tap subgraph from',
array: false,
Expand All @@ -163,6 +169,12 @@ export const start = {
default: false,
group: 'Network Subgraph',
})
.option('epoch-subgraph-deployment', {
description: 'Epoch subgraph deployment (for local hosting)',
array: false,
type: 'string',
group: 'Network Subgraph',
})
.option('epoch-subgraph-endpoint', {
description: 'Endpoint to query the epoch block oracle subgraph from',
array: false,
Expand Down Expand Up @@ -370,11 +382,11 @@ export async function createNetworkSpecification(
url: argv.networkSubgraphEndpoint,
},
epochSubgraph: {
// TODO: We should consider indexing the Epoch Subgraph, similar
// to how we currently do it for the Network Subgraph.
deployment: argv.epochSubgraphDeployment,
url: argv.epochSubgraphEndpoint,
},
tapSubgraph: {
deployment: argv.tapSubgraphDeployment,
url: argv.tapSubgraphEndpoint,
},
}
Expand Down
24 changes: 22 additions & 2 deletions packages/indexer-common/src/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,21 @@ export class Network {
)

let tapSubgraph: SubgraphClient | undefined = undefined
if (specification.subgraphs.tapSubgraph && specification.subgraphs.tapSubgraph.url) {
if (specification.subgraphs.tapSubgraph) {
const tapSubgraphDeploymentId = specification.subgraphs.tapSubgraph.deployment
? new SubgraphDeploymentID(specification.subgraphs.tapSubgraph.deployment)
: undefined
tapSubgraph = await SubgraphClient.create({
name: 'TapSubgraph',
logger,
endpoint: specification.subgraphs.tapSubgraph!.url!,
deployment:
tapSubgraphDeploymentId !== undefined
? {
graphNode,
deployment: tapSubgraphDeploymentId,
}
: undefined,
endpoint: specification.subgraphs.tapSubgraph!.url,
subgraphFreshnessChecker: tapSubgraphFreshnessChecker,
})
}
Expand Down Expand Up @@ -188,9 +198,19 @@ export class Network {
Infinity,
)

const epochSubgraphDeploymentId = specification.subgraphs.epochSubgraph.deployment
? new SubgraphDeploymentID(specification.subgraphs.epochSubgraph.deployment)
: undefined
const epochSubgraph = await SubgraphClient.create({
name: 'EpochSubgraph',
logger,
deployment:
epochSubgraphDeploymentId !== undefined
? {
graphNode,
deployment: epochSubgraphDeploymentId,
}
: undefined,
endpoint: specification.subgraphs.epochSubgraph.url,
subgraphFreshnessChecker: epochSubgraphFreshnessChecker,
})
Expand Down

0 comments on commit cdc4981

Please sign in to comment.