Skip to content

Commit

Permalink
service: Add SubgraphFreshnesschecker to Network Subgraph
Browse files Browse the repository at this point in the history
  • Loading branch information
tilacog committed Sep 6, 2023
1 parent 7fc993d commit 8039eca
Showing 1 changed file with 68 additions and 10 deletions.
78 changes: 68 additions & 10 deletions packages/indexer-service/src/commands/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { BigNumber, Wallet } from 'ethers'
import fs from 'fs'
import { parse as yaml_parse } from 'yaml'

const DEFAULT_SUBGRAPH_MAX_BLOCK_DISTANCE = 10
const DEFAULT_SUBGRAPH_FRESHNESS_SLEEP_MILLISECONDS = 10_000

import {
connectContracts,
connectDatabase,
Expand All @@ -26,6 +29,7 @@ import {
registerIndexerErrorMetrics,
resolveChainId,
validateProviderNetworkIdentifier,
SubgraphFreshnessChecker,
} from '@graphprotocol/indexer-common'

import { createServer } from '../server'
Expand Down Expand Up @@ -176,6 +180,19 @@ export default {
type: 'string',
required: false,
})
.option('subgraph-max-block-distance', {
description: 'How many blocks subgraphs are allowed to stay behind chain head',
type: 'number',
default: DEFAULT_SUBGRAPH_MAX_BLOCK_DISTANCE,
group: 'Protocol',
})
.option('subgraph-freshness-sleep-milliseconds', {
description: 'How long to wait before retrying subgraph query if it is not fresh',
type: 'number',
default: DEFAULT_SUBGRAPH_FRESHNESS_SLEEP_MILLISECONDS,
group: 'Protocol',
})

.check(argv => {
if (!argv['network-subgraph-endpoint'] && !argv['network-subgraph-deployment']) {
return `At least one of --network-subgraph-endpoint and --network-subgraph-deployment must be provided`
Expand Down Expand Up @@ -287,6 +304,56 @@ export default {
argv.graphNodeStatusEndpoint,
argv.indexNodeIds,
)

const networkProvider = await Network.provider(
logger,
metrics,
'_',
argv.networkProvider,
argv.ethereumPollingInterval,
)
const networkIdentifier = await networkProvider.getNetwork()
const protocolNetwork = resolveChainId(networkIdentifier.chainId)

// Warn about inappropriate max block distance for subgraph threshold checks for given networks.
if (protocolNetwork.startsWith('eip155:42161')) {
// Arbitrum-One and Arbitrum-Goerli
if (argv.subgraphMaxBlockDistance <= DEFAULT_SUBGRAPH_MAX_BLOCK_DISTANCE) {
logger.warn(
`Consider increasing 'subgraph-max-block-distance' for Arbitrum networks`,
{
problem:
'A low subgraph freshness threshold might cause the Agent to discard too many subgraph queries in fast-paced networks.',
hint: `Increase the 'subgraph-max-block-distance' parameter to a value that accomodates for block and indexing speeds.`,
configuredValue: argv.subgraphMaxBlockDistance,
},
)
}
if (
argv.subgraphFreshnessSleepMilliseconds <=
DEFAULT_SUBGRAPH_FRESHNESS_SLEEP_MILLISECONDS
) {
logger.warn(
`Consider increasing 'subgraph-freshness-sleep-milliseconds' for Arbitrum networks`,
{
problem:
'A short subgraph freshness wait time might be insufficient for the subgraph to sync with fast-paced networks.',
hint: `Increase the 'subgraph-freshness-sleep-milliseconds' parameter to a value that accomodates for block and indexing speeds.`,
configuredValue: argv.subgraphFreshnessSleepMilliseconds,
},
)
}
}

const subgraphFreshnessChecker = new SubgraphFreshnessChecker(
'Network Subgraph',
networkProvider,
argv.subgraphMaxBlockDistance,
argv.subgraphFreshnessSleepMilliseconds,
logger.child({ component: 'FreshnessChecker' }),
Infinity,
)

const networkSubgraph = await NetworkSubgraph.create({
logger,
endpoint: argv.networkSubgraphEndpoint,
Expand All @@ -296,19 +363,10 @@ export default {
deployment: new SubgraphDeploymentID(argv.networkSubgraphDeployment),
}
: undefined,
subgraphFreshnessChecker,
})
logger.info(`Successfully connected to network subgraph`)

const networkProvider = await Network.provider(
logger,
metrics,
'_',
argv.networkProvider,
argv.ethereumPollingInterval,
)
const networkIdentifier = await networkProvider.getNetwork()
const protocolNetwork = resolveChainId(networkIdentifier.chainId)

// If the network subgraph deployment is present, validate if the `chainId` we get from our
// provider is consistent.
if (argv.networkSubgraphDeployment) {
Expand Down

0 comments on commit 8039eca

Please sign in to comment.