Skip to content

Commit

Permalink
indexer-agent, indexer-cli, indexer-common: auto-renewal option
Browse files Browse the repository at this point in the history
  • Loading branch information
hopeyen authored and hopeyen committed Feb 23, 2022
1 parent 144996e commit 20ba2ca
Show file tree
Hide file tree
Showing 22 changed files with 193 additions and 85 deletions.
52 changes: 33 additions & 19 deletions packages/indexer-agent/src/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -908,28 +908,42 @@ class Agent {
)

if (expiredAllocations.length > 0) {
logger.info(`Reallocating expired allocations`, {
number: expiredAllocations.length,
expiredAllocations: expiredAllocations.map(allocation => allocation.id),
})
if (rule?.autoRenewal) {
logger.info(`Reallocating expired allocations`, {
number: expiredAllocations.length,
expiredAllocations: expiredAllocations.map(
allocation => allocation.id,
),
})

// We do a synchronous for-loop and await each iteration so that we can patch the contents
// of activeAllocations with new allocations as they are made. This is important so that each
// iteration gets an up to date copy of activeAllocations
for (let i = 0; i <= activeAllocations.length - 1; i++) {
const oldAllocation = activeAllocations[i]
if (allocationInList(expiredAllocations, oldAllocation)) {
const { newAllocation, reallocated } = await this.reallocate(
epochStartBlock,
oldAllocation,
desiredAllocationAmount,
activeAllocations,
)
if (reallocated) {
// Patch existing index with new allocation
activeAllocations[i] = newAllocation as Allocation
// We do a synchronous for-loop and await each iteration so that we can patch the contents
// of activeAllocations with new allocations as they are made. This is important so that each
// iteration gets an up to date copy of activeAllocations
for (let i = 0; i <= activeAllocations.length - 1; i++) {
const oldAllocation = activeAllocations[i]
if (allocationInList(expiredAllocations, oldAllocation)) {
const { newAllocation, reallocated } = await this.reallocate(
epochStartBlock,
oldAllocation,
desiredAllocationAmount,
activeAllocations,
)
if (reallocated) {
// Patch existing index with new allocation
activeAllocations[i] = newAllocation as Allocation
}
}
}
} else {
logger.info(
`Skipping reallocating of expired allocations since the corresponding rule has 'autoRenewal' = False`,
{
number: expiredAllocations.length,
expiredAllocations: expiredAllocations.map(
allocation => allocation.id,
),
},
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,6 @@ export async function down({ context }: Context): Promise<void> {
'allocationLifetime',
{ transaction },
)

logger.info(`Remove 'int_IndexingRules_allocationLifetime' custom type`)
await queryInterface.sequelize.query(
`delete from pg_type where typname = 'int_IndexingRules_allocationLifetime'`,
)
}
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,6 @@ export async function down({ context }: Context): Promise<void> {
'requireSupported',
{ transaction },
)

logger.info(`Remove 'bool_IndexingRules_requireSupported' custom type`)
await queryInterface.sequelize.query(
`delete from pg_type where typname = 'bool_IndexingRules_requireSupported'`,
)
}
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Logger } from '@graphprotocol/common-ts'
import { DataTypes, QueryInterface } from 'sequelize'

interface MigrationContext {
queryInterface: QueryInterface
logger: Logger
}

interface Context {
context: MigrationContext
}

export async function up({ context }: Context): Promise<void> {
const { queryInterface, logger } = context

logger.debug(`Checking if indexing rules table exists`)
const tables = await queryInterface.showAllTables()
if (!tables.includes('IndexingRules')) {
logger.info(`Indexing rules table does not exist, migration not necessary`)
return
}

logger.debug(`Checking if 'IndexingRules' table needs to be migrated`)
const table = await queryInterface.describeTable('IndexingRules')
const autoRenewal = table.autoRenewal
if (autoRenewal) {
logger.info(`'autoRenewal' columns already exist, migration not necessary`)
return
}

logger.info(`Add 'autoRenewal' column to 'IndexingRules' table`)
await queryInterface.addColumn('IndexingRules', 'autoRenewal', {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: true,
})
}

export async function down({ context }: Context): Promise<void> {
const { queryInterface, logger } = context

return await queryInterface.sequelize.transaction({}, async transaction => {
const tables = await queryInterface.showAllTables()

if (tables.includes('IndexingRules')) {
logger.info(`Remove 'autoRenewal' column`)
await context.queryInterface.removeColumn(
'IndexingRules',
'autoRenewal',
{ transaction },
)
}
})
}
2 changes: 2 additions & 0 deletions packages/indexer-agent/src/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ export class Indexer {
identifierType
allocationAmount
allocationLifetime
autoRenewal
parallelAllocations
maxAllocationPercentage
minSignal
Expand Down Expand Up @@ -297,6 +298,7 @@ export class Indexer {
identifierType
allocationAmount
allocationLifetime
autoRenewal
parallelAllocations
maxAllocationPercentage
minSignal
Expand Down
2 changes: 2 additions & 0 deletions packages/indexer-cli/src/__tests__/indexer/rules.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,8 @@ describe('Indexer rules tests', () => {
'offchain',
'allocationLifetime',
'21',
'autoRenewal',
'false',
],
'references/indexer-rule-deployment-lifetime',
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
┌────────────────────────────────────────────────┬────────────────┬──────────────────┬────────────────────┬─────────────────────┬─────────────────────────┬───────────┬───────────┬──────────┬─────────────────────┬────────┬───────────────┬──────────────────┐
│ identifier │ identifierType │ allocationAmount │ allocationLifetime │ parallelAllocations │ maxAllocationPercentage │ minSignal │ maxSignal │ minStake │ minAverageQueryFees │ custom │ decisionBasis │ requireSupported │
├────────────────────────────────────────────────┼────────────────┼──────────────────┼────────────────────┼─────────────────────┼─────────────────────────┼───────────┼───────────┼──────────┼─────────────────────┼────────┼───────────────┼──────────────────┤
│ QmZZtzZkfzCWMNrajxBf22q7BC9HzoT5iJUK3S8qA6zNZr │ deployment │ null │ null │ null │ null │ null │ null │ null │ null │ null │ always │ true │
└────────────────────────────────────────────────┴────────────────┴──────────────────┴────────────────────┴─────────────────────┴─────────────────────────┴───────────┴───────────┴──────────┴─────────────────────┴────────┴───────────────┴──────────────────┘
┌────────────────────────────────────────────────┬────────────────┬──────────────────┬────────────────────┬─────────────┬─────────────────────┬─────────────────────────┬───────────┬───────────┬──────────┬─────────────────────┬────────┬───────────────┬──────────────────┐
│ identifier │ identifierType │ allocationAmount │ allocationLifetime │ autoRenewal │ parallelAllocations │ maxAllocationPercentage │ minSignal │ maxSignal │ minStake │ minAverageQueryFees │ custom │ decisionBasis │ requireSupported │
├────────────────────────────────────────────────┼────────────────┼──────────────────┼────────────────────┼─────────────┼─────────────────────┼─────────────────────────┼───────────┼───────────┼──────────┼─────────────────────┼────────┼───────────────┼──────────────────┤
│ QmZZtzZkfzCWMNrajxBf22q7BC9HzoT5iJUK3S8qA6zNZr │ deployment │ null │ null │ true │ null │ null │ null │ null │ null │ null │ null │ always │ true │
└────────────────────────────────────────────────┴────────────────┴──────────────────┴────────────────────┴─────────────┴─────────────────────┴─────────────────────────┴───────────┴───────────┴──────────┴─────────────────────┴────────┴───────────────┴──────────────────┘
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
┌────────────────────────────────────────────────┬────────────────┬──────────────────┬────────────────────┬─────────────────────┬─────────────────────────┬───────────┬───────────┬──────────┬─────────────────────┬────────┬───────────────┬──────────────────┐
│ identifier │ identifierType │ allocationAmount │ allocationLifetime │ parallelAllocations │ maxAllocationPercentage │ minSignal │ maxSignal │ minStake │ minAverageQueryFees │ custom │ decisionBasis │ requireSupported │
├────────────────────────────────────────────────┼────────────────┼──────────────────┼────────────────────┼─────────────────────┼─────────────────────────┼───────────┼───────────┼──────────┼─────────────────────┼────────┼───────────────┼──────────────────┤
│ QmZfeJYR86UARzp9HiXbURWunYgC9ywvPvoePNbuaATrEK │ deployment │ null │ 21 │ null │ null │ null │ null │ null │ null │ null │ offchain │ true │
└────────────────────────────────────────────────┴────────────────┴──────────────────┴────────────────────┴─────────────────────┴─────────────────────────┴───────────┴───────────┴──────────┴─────────────────────┴────────┴───────────────┴──────────────────┘
┌────────────────────────────────────────────────┬────────────────┬──────────────────┬────────────────────┬─────────────┬─────────────────────┬─────────────────────────┬───────────┬───────────┬──────────┬─────────────────────┬────────┬───────────────┬──────────────────┐
│ identifier │ identifierType │ allocationAmount │ allocationLifetime │ autoRenewal │ parallelAllocations │ maxAllocationPercentage │ minSignal │ maxSignal │ minStake │ minAverageQueryFees │ custom │ decisionBasis │ requireSupported │
├────────────────────────────────────────────────┼────────────────┼──────────────────┼────────────────────┼─────────────┼─────────────────────┼─────────────────────────┼───────────┼───────────┼──────────┼─────────────────────┼────────┼───────────────┼──────────────────┤
│ QmZfeJYR86UARzp9HiXbURWunYgC9ywvPvoePNbuaATrEK │ deployment │ null │ 21 │ false │ null │ null │ null │ null │ null │ null │ null │ offchain │ true │
└────────────────────────────────────────────────┴────────────────┴──────────────────┴────────────────────┴─────────────┴─────────────────────┴─────────────────────────┴───────────┴───────────┴──────────┴─────────────────────┴────────┴───────────────┴──────────────────┘
Loading

0 comments on commit 20ba2ca

Please sign in to comment.