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

indexer-agent,-common: Use consistent clear network/epoch var naming #511

Merged
merged 1 commit into from
Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 30 additions & 28 deletions packages/indexer-agent/src/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,16 +165,15 @@ class Agent {
await this.network.register()
}

const currentEpoch = timer(600_000).tryMap(
async () =>
(await this.network.contracts.epochManager.currentEpoch()).toNumber(),
const currentEpochNumber = timer(600_000).tryMap(
async () => this.networkMonitor.currentEpochNumber(),
{
onError: error =>
this.logger.warn(`Failed to fetch current epoch`, { error }),
},
)

const currentEpochStartBlock = currentEpoch.tryMap(
const currentEpochStartBlock = currentEpochNumber.tryMap(
async () => {
const startBlockNumber =
await this.network.contracts.epochManager.currentEpochBlock()
Expand All @@ -184,7 +183,7 @@ class Agent {
return {
number: startBlock.number,
hash: startBlock.hash,
}
} as BlockPointer
},
{
onError: error =>
Expand All @@ -194,11 +193,8 @@ class Agent {
},
)

const protocolChainLatestValidEpoch = timer(600_000).tryMap(
async () =>
await this.networkMonitor.latestValidEpoch(
this.networkMonitor.networkAlias,
),
const networkLatestEpoch = timer(600_000).tryMap(
async () => await this.networkMonitor.networkCurrentEpoch(),
{
onError: error =>
this.logger.warn(
Expand Down Expand Up @@ -357,12 +353,12 @@ class Agent {

const recentlyClosedAllocations = join({
activeAllocations,
currentEpoch,
currentEpochNumber,
}).tryMap(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
({ activeAllocations, currentEpoch }) =>
({ activeAllocations, currentEpochNumber }) =>
this.networkMonitor.recentlyClosedAllocations(
currentEpoch,
currentEpochNumber,
1, //TODO: Parameterize with a user provided value
),
{
Expand All @@ -374,11 +370,13 @@ class Agent {
)

const claimableAllocations = join({
currentEpoch,
currentEpochNumber,
channelDisputeEpochs,
}).tryMap(
({ currentEpoch, channelDisputeEpochs }) =>
this.network.claimableAllocations(currentEpoch - channelDisputeEpochs),
({ currentEpochNumber, channelDisputeEpochs }) =>
this.network.claimableAllocations(
currentEpochNumber - channelDisputeEpochs,
),
{
onError: () =>
this.logger.warn(
Expand All @@ -389,11 +387,15 @@ class Agent {
this.logger.info(`Waiting for network data before reconciling every 120s`)

const disputableAllocations = join({
currentEpoch,
currentEpochNumber,
activeDeployments,
}).tryMap(
({ currentEpoch, activeDeployments }) =>
this.network.disputableAllocations(currentEpoch, activeDeployments, 0),
({ currentEpochNumber, activeDeployments }) =>
this.network.disputableAllocations(
currentEpochNumber,
activeDeployments,
0,
),
{
onError: () =>
this.logger.warn(
Expand All @@ -406,7 +408,7 @@ class Agent {
ticker: timer(240_000),
paused: this.network.transactionManager.paused,
isOperator: this.network.transactionManager.isOperator,
currentEpoch,
currentEpochNumber,
currentEpochStartBlock,
maxAllocationEpochs,
activeDeployments,
Expand All @@ -416,12 +418,12 @@ class Agent {
recentlyClosedAllocations,
claimableAllocations,
disputableAllocations,
protocolChainLatestValidEpoch,
networkLatestEpoch,
}).pipe(
async ({
paused,
isOperator,
currentEpoch,
currentEpochNumber,
currentEpochStartBlock,
maxAllocationEpochs,
activeDeployments,
Expand All @@ -431,17 +433,17 @@ class Agent {
recentlyClosedAllocations,
claimableAllocations,
disputableAllocations,
protocolChainLatestValidEpoch,
networkLatestEpoch,
}) => {
this.logger.info(`Reconcile with the network`, {
protocolChainLatestValidEpoch,
networkLatestEpoch,
})

if (protocolChainLatestValidEpoch.epochNumber != currentEpoch) {
if (networkLatestEpoch.epochNumber != currentEpochNumber) {
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,
currentEpochNumber,
currentEpochStartBlock,
},
)
Expand Down Expand Up @@ -486,7 +488,7 @@ class Agent {

try {
const disputableEpoch =
currentEpoch - this.network.indexerConfigs.poiDisputableEpochs
currentEpochNumber - this.network.indexerConfigs.poiDisputableEpochs
// Find disputable allocations
await this.identifyPotentialDisputes(
disputableAllocations,
Expand All @@ -507,7 +509,7 @@ class Agent {
await this.reconcileActions(
networkDeploymentAllocationDecisions,
activeAllocations,
currentEpoch,
currentEpochNumber,
currentEpochStartBlock,
maxAllocationEpochs,
)
Expand Down
10 changes: 3 additions & 7 deletions packages/indexer-agent/src/commands/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -724,11 +724,7 @@ export default {
})

const epochSubgraph = argv.epochSubgraphEndpoint
? await EpochSubgraph.create({
logger,
endpoint: argv.epochSubgraphEndpoint,
network: networkMeta.name,
})
? await EpochSubgraph.create(argv.epochSubgraphEndpoint)
: undefined

logger.info('Connect to network')
Expand Down Expand Up @@ -770,10 +766,10 @@ export default {
await receiptCollector.queuePendingReceiptsFromDatabase()

const networkMonitor = new NetworkMonitor(
CAIPIds[networkMeta.name],
CAIPIds[argv.ethereumNetwork],
contracts,
toAddress(indexerAddress),
logger,
logger.child({ component: 'NetworkMonitor' }),
indexingStatusResolver,
networkSubgraph,
ethereumProvider,
Expand Down
172 changes: 2 additions & 170 deletions packages/indexer-common/src/allocations/types.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
import { Address, SubgraphDeploymentID, toAddress } from '@graphprotocol/common-ts'
import { Address } from '@graphprotocol/common-ts'
import { BigNumber } from 'ethers'

export interface SubgraphDeployment {
id: SubgraphDeploymentID
deniedAt: number
stakedTokens: BigNumber
signalledTokens: BigNumber
queryFeesAmount: BigNumber
activeAllocations: number
}
import { SubgraphDeployment } from '@graphprotocol/indexer-common'

export interface Allocation {
id: Address
Expand All @@ -34,163 +26,3 @@ export enum AllocationStatus {
FINALIZED = 'Finalized',
CLAIMED = 'Claimed',
}

export interface CreateAllocationResult {
actionID: number
type: 'allocate'
transactionID: string | undefined
allocation: string
deployment: string
allocatedTokens: string
}

export interface CloseAllocationResult {
actionID: number
type: 'unallocate'
transactionID: string | undefined
allocation: string
allocatedTokens: string
indexingRewards: string
receiptsWorthCollecting: boolean
}

export interface ReallocateAllocationResult {
actionID: number
type: 'reallocate'
transactionID: string | undefined
closedAllocation: string
indexingRewardsCollected: string
receiptsWorthCollecting: boolean
createdAllocation: string
createdAllocationStake: string
}

export interface ActionFailure {
actionID: number
transactionID?: string
failureReason: string
}

/* eslint-disable @typescript-eslint/no-explicit-any */
export const isActionFailure = (variableToCheck: any): variableToCheck is ActionFailure =>
'failureReason' in variableToCheck

export type AllocationResult =
| CreateAllocationResult
| CloseAllocationResult
| ReallocateAllocationResult
| ActionFailure

/* eslint-disable @typescript-eslint/no-explicit-any */
export const parseGraphQLSubgraphDeployment = (
subgraphDeployment: any,
): SubgraphDeployment => ({
id: new SubgraphDeploymentID(subgraphDeployment.id),
deniedAt: subgraphDeployment.deniedAt,
stakedTokens: BigNumber.from(subgraphDeployment.stakedTokens),
signalledTokens: BigNumber.from(subgraphDeployment.signalledTokens),
queryFeesAmount: BigNumber.from(subgraphDeployment.queryFeesAmount),
activeAllocations: subgraphDeployment.indexerAllocations.length,
})

/* eslint-disable @typescript-eslint/no-explicit-any */
export const parseGraphQLAllocation = (allocation: any): Allocation => ({
// Ensure the allocation ID (an address) is checksummed
id: toAddress(allocation.id),
status: allocation.status,
subgraphDeployment: {
id: new SubgraphDeploymentID(allocation.subgraphDeployment.id),
deniedAt: allocation.subgraphDeployment.deniedAt,
stakedTokens: BigNumber.from(allocation.subgraphDeployment.stakedTokens),
signalledTokens: BigNumber.from(allocation.subgraphDeployment.signalledTokens),
queryFeesAmount: BigNumber.from(allocation.subgraphDeployment.queryFeesAmount),
activeAllocations: allocation.subgraphDeployment.indexerAllocations
? allocation.subgraphDeployment.indexerAllocations.length
: 0,
},
indexer: toAddress(allocation.indexer.id),
allocatedTokens: BigNumber.from(allocation.allocatedTokens),
createdAtBlockHash: allocation.createdAtBlockHash,
createdAtEpoch: allocation.createdAtEpoch,
closedAtEpoch: allocation.closedAtEpoch,
closedAtEpochStartBlockHash: undefined,
previousEpochStartBlockHash: undefined,
closedAtBlockHash: allocation.closedAtBlockHash,
poi: allocation.poi,
queryFeeRebates: allocation.queryFeeRebates,
queryFeesCollected: allocation.queryFeesCollected,
})

export interface RewardsPool {
subgraphDeployment: SubgraphDeploymentID
allocationIndexer: Address
allocationCreatedAtBlockHash: string
closedAtEpoch: number
closedAtEpochStartBlockHash: string | undefined
closedAtEpochStartBlockNumber: number | undefined
previousEpochStartBlockHash: string | undefined
previousEpochStartBlockNumber: number | undefined
referencePOI: string | undefined
referencePreviousPOI: string | undefined
}

export const allocationRewardsPool = (allocation: Allocation): RewardsPool => ({
subgraphDeployment: allocation.subgraphDeployment.id,
allocationIndexer: allocation.indexer,
allocationCreatedAtBlockHash: allocation.createdAtBlockHash,
closedAtEpoch: allocation.closedAtEpoch,
closedAtEpochStartBlockHash: allocation.closedAtEpochStartBlockHash,
closedAtEpochStartBlockNumber: undefined,
previousEpochStartBlockHash: allocation.previousEpochStartBlockHash,
previousEpochStartBlockNumber: undefined,
referencePOI: undefined,
referencePreviousPOI: undefined,
})

export interface Epoch {
id: number
startBlock: number
startBlockHash: string | undefined
endBlock: number
signalledTokens: number
stakeDeposited: number
queryFeeRebates: number
totalRewards: number
totalIndexerRewards: number
totalDelegatorRewards: number
}

/* eslint-disable @typescript-eslint/no-explicit-any */
export const parseGraphQLEpochs = (epoch: any): Epoch => ({
id: epoch.id,
startBlock: epoch.startBlock,
startBlockHash: undefined,
endBlock: epoch.endBlock,
signalledTokens: epoch.signalledTokens,
stakeDeposited: epoch.stakeDeposited,
queryFeeRebates: epoch.queryFeeRebates,
totalRewards: epoch.totalRewards,
totalIndexerRewards: epoch.totalIndexerRewards,
totalDelegatorRewards: epoch.totalDelegatorRewards,
})

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

export const CAIPIds: { [key: string]: string } = {
mainnet: 'eip155:1',
goerli: 'eip155:5',
gnosis: 'eip155:100',
}

export const alias = (identifier: string): string => {
try {
return Object.keys(CAIPIds).filter((name) => CAIPIds[name] == identifier)[0]
} catch (error) {
throw new Error(`Failed to match chain ids to a network alias`)
}
}
Loading