Skip to content

Commit

Permalink
agent: validate networkID between Network Subgraph and provider
Browse files Browse the repository at this point in the history
  • Loading branch information
tilacog committed Mar 28, 2023
1 parent 42276a8 commit eed5ca2
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions packages/indexer-agent/src/commands/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
parseGRT,
SubgraphDeploymentID,
toAddress,
Logger,
} from '@graphprotocol/common-ts'
import {
AllocationReceiptCollector,
Expand All @@ -35,6 +36,7 @@ import {
import { startAgent } from '../agent'
import { Indexer } from '../indexer'
import { Wallet } from 'ethers'
import { Network as NetworkMetadata } from '@ethersproject/networks'
import { startCostModelAutomation } from '../cost'
import { createSyncingServer } from '../syncing-server'
import { monitorEthBalance } from '../utils'
Expand Down Expand Up @@ -721,6 +723,20 @@ export default {
)
}

// 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.
try {
await validateNetworkId(
networkMeta,
argv.networkSubgraphDeployment,
indexingStatusResolver,
logger,
)
} catch (e) {
logger.critical("Failed to validate Network Subgraph. Exiting.", e)
process.exit(1)
}

// Monitor ETH balance of the operator and write the latest value to a metric
await monitorEthBalance(logger, wallet, metrics)

Expand Down Expand Up @@ -758,3 +774,39 @@ export default {
})
},
}

// Compares the CAIP-2 chain ID between the Ethereum provider and the Network Subgraph and requires
// they are equal.
async function validateNetworkId(
providerNetwork: NetworkMetadata,
networkSubgraphDeploymentIpfsHash: string,
indexingStatusResolver: IndexingStatusResolver,
logger: Logger,
) {
const subgraphNetworkId = new SubgraphDeploymentID(
networkSubgraphDeploymentIpfsHash,
)
const { network: subgraphNetworkChainName } =
await indexingStatusResolver.subgraphFeatures(subgraphNetworkId)

if (!subgraphNetworkChainName) {
// This is unlikely to happen because we expect that the Network Subgraph manifest is valid.
const errorMsg = 'Failed to fetch the networkId for the Network Subgraph'
logger.error(errorMsg, { networkSubgraphDeploymentIpfsHash })
throw new Error(errorMsg)
}

const providerChainId = await resolveChainId(providerNetwork.chainId)
const networkSubgraphChainId = await resolveChainId(subgraphNetworkChainName)
if (providerChainId !== networkSubgraphChainId) {
const errorMsg =
'The configured provider and the Network Subgraph have different CAIP-2 chain IDs. ' +
'Please ensure that both Network Subgraph and the Ethereum provider are correctly configured.'
logger.error(errorMsg, {
networkSubgraphDeploymentIpfsHash,
networkSubgraphChainId,
providerChainId,
})
throw new Error(errorMsg)
}
}

0 comments on commit eed5ca2

Please sign in to comment.