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

Rule to set automatic renewal for allocations #366

Merged
merged 1 commit into from
Feb 23, 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
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about merging this with the conditional that is one level up?

So it would be something like:
if (rule?.autoRenewal && expiredAllocation.length > 0) {...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason I kept them separate was for the log info below. Do you think the message is useful? If not, I can merge them :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, ok. Let's leave it in.

Yeah, I think it's useful. It allows an indexer to see why their allocations aren't being reallocated to.

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