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

Use EpochSubgraph to fetch block numbers instead of EpochManager #573

Merged
merged 8 commits into from
Jan 20, 2023
2 changes: 2 additions & 0 deletions packages/indexer-agent/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Changed
- The `--epoch-subgraph-endpoint` is now a required parameter to start the Agent

## [0.20.7] - 2022-12-20
### Changed
Expand Down
10 changes: 3 additions & 7 deletions packages/indexer-agent/src/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,10 @@ class Agent {

const currentEpochStartBlock = currentEpochNumber.tryMap(
async () => {
const startBlockNumber =
await this.network.contracts.epochManager.currentEpochBlock()
const startBlock = await this.network.ethereum.getBlock(
startBlockNumber.toNumber(),
)
const currentEpoch = await this.networkMonitor.networkCurrentEpoch()
return {
number: startBlock.number,
hash: startBlock.hash,
number: currentEpoch.startBlockNumber,
hash: currentEpoch.startBlockHash,
} as BlockPointer
},
{
Expand Down
6 changes: 2 additions & 4 deletions packages/indexer-agent/src/commands/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export default {
.option('epoch-subgraph-endpoint', {
description: 'Endpoint to query the epoch block oracle subgraph from',
type: 'string',
required: false,
required: true,
group: 'Protocol',
})
.option('index-node-ids', {
Expand Down Expand Up @@ -612,9 +612,7 @@ export default {
: undefined,
})

const epochSubgraph = argv.epochSubgraphEndpoint
? await EpochSubgraph.create(argv.epochSubgraphEndpoint)
: undefined
const epochSubgraph = await EpochSubgraph.create(argv.epochSubgraphEndpoint)

logger.info('Connect to network')
const maxGasFee = argv.baseFeeGasMax || argv.gasPriceMax
Expand Down
2 changes: 2 additions & 0 deletions packages/indexer-common/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Changed
- Epoch block tracking is now handled by the Epoch Subgraph instead of the Epoch Manager contract

## [0.20.8] - 2022-12-21
### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ let networkSubgraph: NetworkSubgraph
let client: IndexerManagementClient
let transactionManager: TransactionManager
let wallet: Wallet
let epochSubgraph: EpochSubgraph

// Make global Jest variables available
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down Expand Up @@ -302,6 +303,10 @@ const setup = async () => {
0,
)

epochSubgraph = await EpochSubgraph.create(
'https://api.thegraph.com/subgraphs/name/graphprotocol/goerli-epoch-block-oracle',
)

const receiptCollector = new AllocationReceiptCollector({
logger,
transactionManager: transactionManager,
Expand All @@ -321,6 +326,7 @@ const setup = async () => {
indexingStatusResolver,
networkSubgraph,
ethereum,
epochSubgraph,
)

client = await createIndexerManagementClient({
Expand Down
35 changes: 10 additions & 25 deletions packages/indexer-common/src/indexer-management/monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class NetworkMonitor {
private indexingStatusResolver: IndexingStatusResolver,
private networkSubgraph: NetworkSubgraph,
private ethereum: providers.BaseProvider,
private epochSubgraph?: EpochSubgraph,
private epochSubgraph: EpochSubgraph,
) {}

async currentEpochNumber(): Promise<number> {
Expand Down Expand Up @@ -559,33 +559,11 @@ export class NetworkMonitor {
}

async networkCurrentEpoch(): Promise<NetworkEpoch> {
const epochNumber = await this.currentEpochNumber()
const startBlockNumber = (
await this.contracts.epochManager.currentEpochBlock()
).toNumber()
const startBlockHash = (await this.ethereum.getBlock(startBlockNumber)).hash
return {
networkID: this.networkCAIPID,
epochNumber,
startBlockNumber,
startBlockHash,
}
return this.currentEpoch(this.networkCAIPID)
}

async currentEpoch(networkID: string): Promise<NetworkEpoch> {
const networkAlias = await resolveChainAlias(networkID)
if (!this.epochSubgraph) {
if (networkID == this.networkCAIPID) {
return await this.networkCurrentEpoch()
}
this.logger.error(`Epoch start block not available for the network`, {
networkName: networkID,
})
throw indexerError(
IndexerErrorCode.IE071,
`Epoch start block not available for network: ${networkID}`,
)
}

const queryEpochSubgraph = async () => {
// We know it is non-null because of the check above for a null case that will end execution of fn if true
Expand All @@ -609,6 +587,11 @@ export class NetworkMonitor {
}
}
}
_meta {
block {
number
}
}
}
`,
{
Expand Down Expand Up @@ -636,12 +619,14 @@ export class NetworkMonitor {
networkAlias,
+validBlock.blockNumber,
)
const latestBlock = result.data._meta.block.number

return {
networkID,
epochNumber: +validBlock.epochNumber,
startBlockNumber: +validBlock.blockNumber,
startBlockHash,
latestBlock,
}
}

Expand Down Expand Up @@ -777,7 +762,7 @@ export class NetworkMonitor {
throw indexerError(
IndexerErrorCode.IE068,
`User provided POI does not match reference fetched from the graph-node. Use '--force' to bypass this POI accuracy check.
POI: ${poi},
POI: ${poi},
referencePOI: ${generatedPOI}`,
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NetworkMonitor } from '@graphprotocol/indexer-common'
import { NetworkMonitor, epochElapsedBlocks } from '@graphprotocol/indexer-common'
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
/* eslint-disable @typescript-eslint/ban-types */

Expand Down Expand Up @@ -360,19 +360,25 @@ async function resolvePOI(
export default {
allocations: async (
{ filter }: { filter: AllocationFilter },
{ address, contracts, logger, networkSubgraph }: IndexerManagementResolverContext,
{
address,
contracts,
logger,
networkSubgraph,
networkMonitor,
}: IndexerManagementResolverContext,
): Promise<object[]> => {
logger.debug('Execute allocations() query', {
filter,
})

const allocations: AllocationInfo[] = []

const currentEpoch = await contracts.epochManager.currentEpoch()
const currentEpoch = await networkMonitor.networkCurrentEpoch()
const disputeEpochs = await contracts.staking.channelDisputeEpochs()
const variables = {
indexer: toAddress(address),
disputableEpoch: currentEpoch.sub(disputeEpochs).toNumber(),
disputableEpoch: currentEpoch.epochNumber - disputeEpochs,
allocation: filter.allocation
? filter.allocation === 'all'
? null
Expand All @@ -381,14 +387,10 @@ export default {
status: filter.status,
}
const context = {
currentEpoch: currentEpoch.toNumber(),
currentEpochStartBlock: (
await contracts.epochManager.currentEpochBlock()
).toNumber(),
currentEpochElapsedBlocks: (
await contracts.epochManager.currentEpochBlockSinceStart()
).toNumber(),
latestBlock: (await contracts.epochManager.blockNum()).toNumber(),
currentEpoch: currentEpoch.epochNumber,
currentEpochStartBlock: currentEpoch.startBlockNumber,
currentEpochElapsedBlocks: epochElapsedBlocks(currentEpoch),
latestBlock: currentEpoch.latestBlock,
maxAllocationEpochs: await contracts.staking.maxAllocationEpochs(),
blocksPerEpoch: (await contracts.epochManager.epochLength()).toNumber(),
avgBlockTime: 13_000,
Expand Down
5 changes: 5 additions & 0 deletions packages/indexer-common/src/indexer-management/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ export interface NetworkEpoch {
epochNumber: number
startBlockNumber: number
startBlockHash: string
latestBlock: number
}

export function epochElapsedBlocks(networkEpoch: NetworkEpoch): number {
return networkEpoch.startBlockNumber - networkEpoch.latestBlock
}

const Caip2ByChainAlias: { [key: string]: string } = {
Expand Down