Skip to content

Commit

Permalink
indexer-cli,-common: indexing rules safety
Browse files Browse the repository at this point in the history
  • Loading branch information
hopeyen authored and hopeyen committed Feb 16, 2023
1 parent 9483907 commit 1f19755
Show file tree
Hide file tree
Showing 22 changed files with 173 additions and 51 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
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 safetyColumn = table.safety
if (safetyColumn) {
logger.info(`'safety' column already exist, migration not necessary`)
return
}

logger.info(`Add 'safety' column to 'IndexingRules' table`)
await queryInterface.addColumn('IndexingRules', 'safety', {
type: DataTypes.BOOLEAN,
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 'safety' column`)
await context.queryInterface.removeColumn('IndexingRules', 'safety', {
transaction,
})
}
})
}
6 changes: 5 additions & 1 deletion packages/indexer-agent/src/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ export class Indexer {
parallelAllocations: 1,
decisionBasis: 'rules',
requireSupported: true,
safety: true,
}

const defaultGlobalRule = await this.indexerManagement
Expand All @@ -308,6 +309,7 @@ export class Indexer {
custom
decisionBasis
requireSupported
safety
}
}
`,
Expand Down Expand Up @@ -631,14 +633,16 @@ export class Indexer {
allocationAmount: formatGRT(desiredAllocationAmount),
})

// Skip allocating if the previous allocation for this deployment was closed with a null or 0x00 POI
// Skip allocating if the previous allocation for this deployment was closed with 0x00 POI but rules set to un-safe
if (
deploymentAllocationDecision.ruleMatch.rule?.safety &&
mostRecentlyClosedAllocation &&
mostRecentlyClosedAllocation.poi === utils.hexlify(Array(32).fill(0))
) {
logger.warn(
`Skipping allocation to this deployment as the last allocation to it was closed with a zero POI`,
{
notSafe: !deploymentAllocationDecision.ruleMatch.rule?.safety,
deployment: deploymentAllocationDecision.deployment,
closedAllocation: mostRecentlyClosedAllocation.id,
},
Expand Down
17 changes: 17 additions & 0 deletions packages/indexer-cli/src/__tests__/indexer/rules.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,23 @@ describe('Indexer rules tests', () => {
timeout: 10000,
},
)
cliTest(
'Indexer rules set deployment id safety - success',
[
'indexer',
'rules',
'set',
'QmVEV7RA2U6BJT9Ssjxcfyrk4YQUnVqSRNX4TvYagjzh9h',
'safety',
'false',
],
'references/indexer-rule-deployment-safety',
{
expectedExitCode: 0,
cwd: baseDir,
timeout: 10000,
},
)
cliTest(
'Indexer rules set deployment id - success - offchain',
[
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
┌────────────────────────────────────────────────┬────────────────┬──────────────────┬────────────────────┬─────────────┬─────────────────────┬─────────────────────────┬───────────┬───────────┬──────────┬─────────────────────┬────────┬───────────────┬──────────────────┐
│ 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 │
└────────────────────────────────────────────────┴────────────────┴──────────────────┴────────────────────┴─────────────┴─────────────────────┴─────────────────────────┴───────────┴───────────┴──────────┴─────────────────────┴────────┴───────────────┴──────────────────┘
┌────────────────────────────────────────────────┬────────────────┬──────────────────┬────────────────────┬─────────────┬─────────────────────┬─────────────────────────┬───────────┬───────────┬──────────┬─────────────────────┬────────┬───────────────┬──────────────────┬────────
│ identifier │ identifierType │ allocationAmount │ allocationLifetime │ autoRenewal │ parallelAllocations │ maxAllocationPercentage │ minSignal │ maxSignal │ minStake │ minAverageQueryFees │ custom │ decisionBasis │ requireSupported │ safety │
├────────────────────────────────────────────────┼────────────────┼──────────────────┼────────────────────┼─────────────┼─────────────────────┼─────────────────────────┼───────────┼───────────┼──────────┼─────────────────────┼────────┼───────────────┼──────────────────┼────────
│ QmZZtzZkfzCWMNrajxBf22q7BC9HzoT5iJUK3S8qA6zNZr │ deployment │ null │ null │ true │ null │ null │ null │ null │ null │ null │ null │ always │ true │ true │
└────────────────────────────────────────────────┴────────────────┴──────────────────┴────────────────────┴─────────────┴─────────────────────┴─────────────────────────┴───────────┴───────────┴──────────┴─────────────────────┴────────┴───────────────┴──────────────────┴────────
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
┌────────────────────────────────────────────────┬────────────────┬──────────────────┬────────────────────┬─────────────┬─────────────────────┬─────────────────────────┬───────────┬───────────┬──────────┬─────────────────────┬────────┬───────────────┬──────────────────┐
│ 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 │
└────────────────────────────────────────────────┴────────────────┴──────────────────┴────────────────────┴─────────────┴─────────────────────┴─────────────────────────┴───────────┴───────────┴──────────┴─────────────────────┴────────┴───────────────┴──────────────────┘
┌────────────────────────────────────────────────┬────────────────┬──────────────────┬────────────────────┬─────────────┬─────────────────────┬─────────────────────────┬───────────┬───────────┬──────────┬─────────────────────┬────────┬───────────────┬──────────────────┬────────
│ identifier │ identifierType │ allocationAmount │ allocationLifetime │ autoRenewal │ parallelAllocations │ maxAllocationPercentage │ minSignal │ maxSignal │ minStake │ minAverageQueryFees │ custom │ decisionBasis │ requireSupported │ safety │
├────────────────────────────────────────────────┼────────────────┼──────────────────┼────────────────────┼─────────────┼─────────────────────┼─────────────────────────┼───────────┼───────────┼──────────┼─────────────────────┼────────┼───────────────┼──────────────────┼────────
│ QmZfeJYR86UARzp9HiXbURWunYgC9ywvPvoePNbuaATrEK │ deployment │ null │ 21 │ false │ null │ null │ null │ null │ null │ null │ null │ offchain │ true │ true │
└────────────────────────────────────────────────┴────────────────┴──────────────────┴────────────────────┴─────────────┴─────────────────────┴─────────────────────────┴───────────┴───────────┴──────────┴─────────────────────┴────────┴───────────────┴──────────────────┴────────
Loading

0 comments on commit 1f19755

Please sign in to comment.