Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Agent: warn and wait if subgraph is behind the chain head #762

Merged
merged 10 commits into from
Sep 29, 2023
50 changes: 50 additions & 0 deletions packages/indexer-agent/src/commands/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ import { displayZodParsingError } from './error-handling'
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type AgentOptions = { [key: string]: any } & Argv['argv']

const DEFAULT_SUBGRAPH_MAX_BLOCK_DISTANCE = 0
tilacog marked this conversation as resolved.
Show resolved Hide resolved
const DEFAULT_SUBGRAPH_FRESHNESS_SLEEP_MILLISECONDS = 5_000

export const start = {
command: 'start',
describe: 'Start the agent',
Expand Down Expand Up @@ -157,6 +160,20 @@ export const start = {
required: true,
group: 'Protocol',
})
.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',
})
.option('default-allocation-amount', {
description:
'Default amount of GRT to allocate to a subgraph deployment',
Expand Down Expand Up @@ -298,6 +315,7 @@ export const start = {

export async function createNetworkSpecification(
argv: AgentOptions,
logger: Logger,
): Promise<spec.NetworkSpecification> {
const gateway = {
url: argv.gatewayEndpoint,
Expand Down Expand Up @@ -333,6 +351,8 @@ export async function createNetworkSpecification(
}

const subgraphs = {
maxBlockDistance: argv.subgraphMaxBlockDistance,
freshnessSleepMilliseconds: argv.subgraphFreshnessSleepMilliseconds,
networkSubgraph: {
deployment: argv.networkSubgraphDeployment,
url: argv.networkSubgraphEndpoint,
Expand All @@ -359,6 +379,36 @@ export async function createNetworkSpecification(
const chainId = await fetchChainId(networkProvider.url)
const networkIdentifier = resolveChainId(chainId)

// Warn about inappropriate max block distance for subgraph threshold checks for given networks.
hopeyen marked this conversation as resolved.
Show resolved Hide resolved
if (networkIdentifier.startsWith('eip155:42161')) {
// Arbitrum-One and Arbitrum-Goerli
if (subgraphs.maxBlockDistance <= DEFAULT_SUBGRAPH_MAX_BLOCK_DISTANCE) {
tilacog marked this conversation as resolved.
Show resolved Hide resolved
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: subgraphs.maxBlockDistance,
},
)
}
if (
subgraphs.freshnessSleepMilliseconds <=
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: subgraphs.freshnessSleepMilliseconds,
},
)
}
}

try {
return spec.NetworkSpecification.parse({
networkIdentifier,
Expand Down
2 changes: 1 addition & 1 deletion packages/indexer-agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ async function processArgumentsAndRun(args: AgentOptions): Promise<void> {
await run(args, specifications, logger)
} else {
reviewArgumentsForWarnings(args, logger)
const specification = await createNetworkSpecification(args)
const specification = await createNetworkSpecification(args, logger)
await run(args, [specification], logger)
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/indexer-agent/src/syncing-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export const createSyncingServer = async ({

let result
try {
result = await networkSubgraph.query(parsedQuery, variables)
result = await networkSubgraph.checkedQuery(parsedQuery, variables)
} catch (err) {
logger.error(err)
return res.status(400).send({ error: err.message })
Expand Down
2 changes: 2 additions & 0 deletions packages/indexer-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"@graphprotocol/common-ts": "2.0.3",
"@graphprotocol/cost-model": "0.1.16",
"@thi.ng/heaps": "1.2.38",
"@types/lodash.clonedeep": "^4.5.7",
"@types/lodash.intersection": "^4.4.7",
"@types/lodash.xor": "^4.5.7",
"@urql/core": "2.4.4",
Expand All @@ -39,6 +40,7 @@
"graphql": "16.3.0",
"graphql-tag": "2.12.6",
"jayson": "3.6.6",
"lodash.clonedeep": "^4.5.0",
"lodash.groupby": "^4.6.0",
"lodash.intersection": "^4.4.0",
"lodash.isequal": "^4.5.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
networkIdentifier: mainnet
gateway:
url: http://gateway
indexerOptions:
address: "0x4e8a4C63Df58bf59Fef513aB67a76319a9faf448"
mnemonic: word ivory whale diesel slab pelican voyage oxygen chat find tobacco sport
url: http://indexer
geoCoordinates: [25.1, -71.2]
restakeRewards: true
rebateClaimThreshold: 400
rebateClaimBatchThreshold: 5000
rebateClaimMaxBatchSize: 10
poiDisputeMonitoring: false
poiDisputableEpochs: 5
defaultAllocationAmount: 0.05
voucherRedemptionThreshold: 2
voucherRedemptionBatchThreshold: 2000
voucherRedemptionMaxBatchSize: 15
allocationManagementMode: "auto"
autoAllocationMinBatchSize: 20
transactionMonitoring:
gasIncreaseTimeout: 10
gasIncreaseFactor: 10
baseFeePerGasMax: 10
maxTransactionAttempts: 10
subgraphs:
maxBlockDistance: -10
networkSubgraph:
deployment: QmPK1s3pNYLi9ERiq3BDxKa4XosgWwFRQUydHUtz4YgpqB
epochSubgraph:
url: http://subgraph
networkProvider:
url: http://provider
dai:
contractAddress: "0x4e8a4C63Df58bf59Fef513aB67a76319a9faf448"
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ describe('Failed deserialization', () => {
path: ['dai', 'contractAddress'],
message: 'Invalid contract address',
},
{
file: 'invalid-negative-max-block-distance.yml',
path: ['subgraphs', 'maxBlockDistance'],
message: 'Number must be greater than or equal to 0',
},
]

test.each(failedTests)(
Expand Down
Loading
Loading