From cdc49810e95f87e9c2120080c1edfc6fb5b43d14 Mon Sep 17 00:00:00 2001 From: Ford Date: Fri, 13 Dec 2024 11:45:20 -0800 Subject: [PATCH] agent, common: Add support for local querying of TAP and Epoch subgraphs --- packages/indexer-agent/src/agent.ts | 78 ++++++++++++-------- packages/indexer-agent/src/commands/start.ts | 18 ++++- packages/indexer-common/src/network.ts | 24 +++++- 3 files changed, 85 insertions(+), 35 deletions(-) diff --git a/packages/indexer-agent/src/agent.ts b/packages/indexer-agent/src/agent.ts index abf1c5dde..6771896fd 100644 --- a/packages/indexer-agent/src/agent.ts +++ b/packages/indexer-agent/src/agent.ts @@ -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( @@ -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, + ) } } } diff --git a/packages/indexer-agent/src/commands/start.ts b/packages/indexer-agent/src/commands/start.ts index 08dc0a5ff..f22943ddb 100644 --- a/packages/indexer-agent/src/commands/start.ts +++ b/packages/indexer-agent/src/commands/start.ts @@ -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', @@ -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, @@ -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, @@ -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, }, } diff --git a/packages/indexer-common/src/network.ts b/packages/indexer-common/src/network.ts index c330bf8cd..ec73f84c8 100644 --- a/packages/indexer-common/src/network.ts +++ b/packages/indexer-common/src/network.ts @@ -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, }) } @@ -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, })