Skip to content

Commit

Permalink
block hash from number
Browse files Browse the repository at this point in the history
  • Loading branch information
hopeyen committed Sep 16, 2022
1 parent cb4ad18 commit c2a45eb
Show file tree
Hide file tree
Showing 18 changed files with 218 additions and 166 deletions.
6 changes: 6 additions & 0 deletions docs/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,12 @@ Failed to query Block Oracle

Check `block-oracle-endpoint` query endpoint for its syncing status and the EBO contract state.

## IE070

**Summary**

Failed to query BlockHashFromNumber from graph node

**Solution**

Graph-node could not find the block hash given network and block number, check if graph-node has access to a network client that has synced to the required block.
3 changes: 2 additions & 1 deletion packages/indexer-agent/src/__tests__/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ const setup = async () => {
const blockOracleSubgraph = await BlockOracleSubgraph.create({
logger,
endpoint:
'https://api.thegraph.com/subgraphs/name/juanmardefago/block-oracle',
'https://api.thegraph.com/subgraphs/name/juanmardefago/block-oracle-2',
network: 'goerli',
})

const indexNodeIDs = ['node_1']
Expand Down
75 changes: 18 additions & 57 deletions packages/indexer-agent/src/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ class Agent {
}

const currentEpoch = timer(600_000).tryMap(
() => this.network.contracts.epochManager.currentEpoch(),
async () =>
(await this.network.contracts.epochManager.currentEpoch()).toNumber(),
{
onError: error =>
this.logger.warn(`Failed to fetch current epoch`, { error }),
Expand All @@ -200,8 +201,7 @@ class Agent {
},
)

// TODO: once EBO consistent and stable, this can later replace contract call to currentEpoch
const globalLatestValidEpoch: Eventual<number> = timer(600_000).tryMap(
const globalLatestValidEpoch = timer(600_000).tryMap(
async () => await this.networkMonitor.latestValidEpoch(),
{
onError: error =>
Expand All @@ -214,23 +214,6 @@ class Agent {
},
)

const networkLatestEpochBlocks: Eventual<NetworkEpochBlock[]> = timer(
600_000,
).tryMap(
async () => {
return await this.networkMonitor.networkLatestEpochBlocks()
},
{
onError: error =>
this.logger.warn(
`Failed to obtain supported networks' latest epoch blocks, trying again later`,
{
error,
},
),
},
)

const channelDisputeEpochs = timer(600_000).tryMap(
() => this.network.contracts.staking.channelDisputeEpochs(),
{
Expand Down Expand Up @@ -383,7 +366,7 @@ class Agent {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
({ activeAllocations, currentEpoch }) =>
this.networkMonitor.recentlyClosedAllocations(
currentEpoch.toNumber(),
currentEpoch,
1, //TODO: Parameterize with a user provided value
),
{
Expand All @@ -399,9 +382,7 @@ class Agent {
channelDisputeEpochs,
}).tryMap(
({ currentEpoch, channelDisputeEpochs }) =>
this.network.claimableAllocations(
currentEpoch.toNumber() - channelDisputeEpochs,
),
this.network.claimableAllocations(currentEpoch - channelDisputeEpochs),
{
onError: () =>
this.logger.warn(
Expand Down Expand Up @@ -460,7 +441,6 @@ class Agent {
disputableAllocations,
costModels,
globalLatestValidEpoch,
networkLatestEpochBlocks,
}).pipe(
async ({
paused,
Expand All @@ -477,39 +457,21 @@ class Agent {
disputableAllocations,
costModels,
globalLatestValidEpoch,
networkLatestEpochBlocks,
}) => {
this.logger.info(`Reconcile with the network`)

//TODO: remove later, this is for testing with some logs
const networkID = `eip155:${this.network.ethereum._network.chainId}`
const testnetLatest = networkLatestEpochBlocks.find(
networkBlock => networkBlock.network === networkID,
)
const getBlock = testnetLatest
? await this.network.ethereum.getBlock(testnetLatest.blockNumber)
: 'EBO not syncing goerli testnet'

// reconcile aliasing networks for network epoch block number
const allocNetworkEpochBlockNumbers = activeAllocations.map(
allocation =>
networkLatestEpochBlocks.find(
async b =>
b.network ===
(await this.networkMonitor.deploymentNetwork(
allocation.subgraphDeployment.id,
)),
)?.blockNumber ?? currentEpoch.toNumber(),
)
this.logger.debug(`agent logger checks allocas`, {
networkID,
epochManager: currentEpoch.toNumber(),
this.logger.info(`Reconcile with the network`, {
globalLatestValidEpoch,
networkLatestEpochBlocks,
allocNetworkEpochBlockNumbers,
getBlock,
})

if (globalLatestValidEpoch.epochNumber != currentEpoch) {
this.logger.warn(
`EBO latest valid epoch differs from the network contract, ping updates to the EBO (After this is stable, can replace currentEpoch)`,
{
currentEpoch,
currentEpochStartBlock,
globalLatestValidEpoch,
},
)
}
// Do nothing else if the network is paused
if (paused) {
return this.logger.info(
Expand Down Expand Up @@ -554,8 +516,7 @@ class Agent {

try {
const disputableEpoch =
currentEpoch.toNumber() -
this.network.indexerConfigs.poiDisputableEpochs
currentEpoch - this.network.indexerConfigs.poiDisputableEpochs
// Find disputable allocations
await this.identifyPotentialDisputes(
disputableAllocations,
Expand All @@ -576,7 +537,7 @@ class Agent {
await this.reconcileActions(
networkDeploymentAllocationDecisions,
activeAllocations,
currentEpoch.toNumber(),
currentEpoch,
currentEpochStartBlock,
maxAllocationEpochs,
)
Expand Down
1 change: 1 addition & 0 deletions packages/indexer-agent/src/commands/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,7 @@ export default {
const blockOracleSubgraph = await BlockOracleSubgraph.create({
logger,
endpoint: argv.blockOracleEndpoint,
network: networkMeta.name,
})

logger.info('Connect to network')
Expand Down
3 changes: 2 additions & 1 deletion packages/indexer-cli/src/__tests__/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ export const setup = async () => {
})
const blockOracleSubgraph = await BlockOracleSubgraph.create({
logger,
endpoint: 'https://thegraph.com/hosted-service/subgraph/juanmardefago/block-oracle',
endpoint: 'https://thegraph.com/hosted-service/subgraph/juanmardefago/block-oracle-2',
network: 'goerli',
})
const indexNodeIDs = ['node_1']
indexerManagementClient = await createIndexerManagementClient({
Expand Down
36 changes: 33 additions & 3 deletions packages/indexer-common/src/allocations/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { IndexingStatusResolver } from '@graphprotocol/indexer-common'
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */

import { Address, SubgraphDeploymentID, toAddress } from '@graphprotocol/common-ts'
Expand Down Expand Up @@ -178,7 +179,11 @@ export const parseGraphQLEpochs = (epoch: any): Epoch => ({

export interface NetworkEpochBlock {
network: string
blockNumber: number
epochNumber: number
startBlockNumber: number
startBlockHash: string
prevStartBlockNumber: number
prevStartBlockHash: string
}

export const NetworkAliases: { [key: string]: string } = {
Expand All @@ -188,8 +193,33 @@ export const NetworkAliases: { [key: string]: string } = {
gnosis: 'eip155:100',
}

const aliasInverse = (eipFormat: string): string => {
try {
return Object.keys(NetworkAliases).filter(
(name) => NetworkAliases[name] == eipFormat,
)[0]
} catch (error) {
// hardcoded alias for now, shouldn't get here
// if so this will just let blockHashFromNumber fail later
return ''
}
}
/* eslint-disable @typescript-eslint/no-explicit-any */
export const parseGraphQLNetworkEpochBlocks = (block: any): NetworkEpochBlock => ({
export const buildEpochBlock = async (
status: IndexingStatusResolver,
epochNumber: string,
block: any,
): Promise<NetworkEpochBlock> => ({
network: block.network.id,
blockNumber: +block.blockNumber,
epochNumber: +epochNumber,
startBlockNumber: +block.blockNumber,
startBlockHash: await status.blockHashFromNumber(
aliasInverse(block.network.id),
+block.blockNumber,
),
prevStartBlockNumber: +block.previousBlockNumber.blockNumber,
prevStartBlockHash: await status.blockHashFromNumber(
aliasInverse(block.network.id),
+block.previousBlockNumber.blockNumber,
),
})
7 changes: 7 additions & 0 deletions packages/indexer-common/src/block-oracle-subgraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@ import { QueryResult } from './network-subgraph'
export interface BlockOracleSubgraphCreateOptions {
logger: Logger
endpoint: string
network: string
}

interface BlockOracleSubgraphOptions {
logger: Logger
endpoint: string
network: string
}

export class BlockOracleSubgraph {
logger: Logger
endpointClient: AxiosInstance
network: string

private constructor(options: BlockOracleSubgraphOptions) {
this.logger = options.logger
Expand All @@ -30,11 +33,14 @@ export class BlockOracleSubgraph {
// Don't transform responses
transformResponse: (data) => data,
})

this.network = options.network
}

public static async create({
logger: parentLogger,
endpoint,
network,
}: BlockOracleSubgraphCreateOptions): Promise<BlockOracleSubgraph> {
const logger = parentLogger.child({
component: 'BlockOracleSubgraph',
Expand All @@ -45,6 +51,7 @@ export class BlockOracleSubgraph {
const blockOracleSubgraph = new BlockOracleSubgraph({
logger,
endpoint,
network,
})
// Any checks to be made after creating?

Expand Down
2 changes: 2 additions & 0 deletions packages/indexer-common/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export enum IndexerErrorCode {
IE067 = 'IE067',
IE068 = 'IE068',
IE069 = 'IE069',
IE070 = 'IE070',
}

export const INDEXER_ERROR_MESSAGES: Record<IndexerErrorCode, string> = {
Expand Down Expand Up @@ -153,6 +154,7 @@ export const INDEXER_ERROR_MESSAGES: Record<IndexerErrorCode, string> = {
IE067: 'Failed to query POI for current epoch start block',
IE068: 'User-provided POI did not match reference POI from graph-node',
IE069: 'Failed to query Epoch Block Oracle',
IE070: 'Failed to query BlockHashFromNumber from graph-node',
}

export type IndexerErrorCause = unknown
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,8 @@ const setup = async () => {
})
blockOracleSubgraph = await BlockOracleSubgraph.create({
logger,
endpoint: 'https://api.thegraph.com/subgraphs/name/juanmardefago/block-oracle',
endpoint: 'https://api.thegraph.com/subgraphs/name/juanmardefago/block-oracle-2',
network: 'goerli',
})
transactionManager = new TransactionManager(
ethereum,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ const setupAll = async () => {

blockOracleSubgraph = await BlockOracleSubgraph.create({
logger,
endpoint: 'https://api.thegraph.com/subgraphs/name/juanmardefago/block-oracle',
endpoint: 'https://api.thegraph.com/subgraphs/name/juanmardefago/block-oracle-2',
network: 'goerli',
})

client = await createIndexerManagementClient({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ const setupAll = async () => {

blockOracleSubgraph = await BlockOracleSubgraph.create({
logger,
endpoint: 'https://api.thegraph.com/subgraphs/name/juanmardefago/block-oracle',
endpoint: 'https://api.thegraph.com/subgraphs/name/juanmardefago/block-oracle-2',
network: 'goerli',
})

const indexNodeIDs = ['node_1']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,8 @@ const setupAll = async () => {
})
blockOracleSubgraph = await BlockOracleSubgraph.create({
logger,
endpoint: 'https://api.thegraph.com/subgraphs/name/juanmardefago/block-oracle',
endpoint: 'https://api.thegraph.com/subgraphs/name/juanmardefago/block-oracle-2',
network: 'goerli',
})

const indexNodeIDs = ['node_1']
Expand Down
Loading

0 comments on commit c2a45eb

Please sign in to comment.