Skip to content

Commit 6bd47ac

Browse files
chriswesselsfordN
authored andcommitted
indexer-agent, indexer-cli, indexer-common: add allocation lifetimes
1 parent 90cce23 commit 6bd47ac

20 files changed

+201
-65
lines changed

packages/indexer-agent/src/agent.ts

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -693,12 +693,9 @@ class Agent {
693693
currentEpochStartBlock: BlockPointer,
694694
maxAllocationEpochs: number,
695695
): Promise<void> {
696-
const allocationLifetime = Math.max(1, maxAllocationEpochs - 1)
697-
698696
this.logger.info(`Reconcile allocations`, {
699697
currentEpoch,
700698
maxAllocationEpochs,
701-
allocationLifetime,
702699
targetAllocations: targetAllocations.map(
703700
deployment => deployment.display,
704701
),
@@ -782,33 +779,32 @@ class Agent {
782779
? BigNumber.from(rule.allocationAmount)
783780
: this.indexer.defaultAllocationAmount
784781
const desiredNumberOfAllocations = 1
782+
const desiredAllocationLifetime = rule?.allocationLifetime
783+
? rule.allocationLifetime
784+
: Math.max(1, maxAllocationEpochs - 1)
785785
const activeAllocationAmount = activeAllocations.reduce(
786786
(sum, allocation) => sum.add(allocation.allocatedTokens),
787787
BigNumber.from('0'),
788788
)
789789

790-
if (
791-
desiredAllocationAmount !== activeAllocationAmount ||
792-
desiredNumberOfAllocations !== activeAllocations.length
793-
) {
794-
logger.info(
795-
`Reconcile deployment allocations for deployment '${deployment.ipfsHash}'`,
796-
{
797-
desiredAllocationAmount: formatGRT(desiredAllocationAmount),
790+
logger.debug(
791+
`Reconcile deployment allocations for deployment '${deployment.ipfsHash}'`,
792+
{
793+
desiredAllocationAmount: formatGRT(desiredAllocationAmount),
794+
desiredAllocationLifetime,
798795

799-
totalActiveAllocationAmount: formatGRT(activeAllocationAmount),
796+
totalActiveAllocationAmount: formatGRT(activeAllocationAmount),
800797

801-
desiredNumberOfAllocations,
802-
activeNumberOfAllocations: activeAllocations.length,
798+
desiredNumberOfAllocations,
799+
activeNumberOfAllocations: activeAllocations.length,
803800

804-
activeAllocations: activeAllocations.map(allocation => ({
805-
id: allocation.id,
806-
createdAtEpoch: allocation.createdAtEpoch,
807-
amount: formatGRT(allocation.allocatedTokens),
808-
})),
809-
},
810-
)
811-
}
801+
activeAllocations: activeAllocations.map(allocation => ({
802+
id: allocation.id,
803+
createdAtEpoch: allocation.createdAtEpoch,
804+
amount: formatGRT(allocation.allocatedTokens),
805+
})),
806+
},
807+
)
812808

813809
// Return early if the deployment is not (or no longer) worth allocating towards
814810
if (!worthAllocating) {
@@ -881,11 +877,10 @@ class Agent {
881877
)
882878
}
883879

884-
const lifetime = Math.max(1, maxAllocationEpochs - 1)
885-
886880
// For allocations that have expired, let's reallocate in one transaction (closeAndAllocate)
887881
let expiredAllocations = activeAllocations.filter(
888-
allocation => epoch >= allocation.createdAtEpoch + lifetime,
882+
allocation =>
883+
epoch >= allocation.createdAtEpoch + desiredAllocationLifetime,
889884
)
890885
// The allocations come from the network subgraph; due to short indexing
891886
// latencies, this data may be slightly outdated. Cross-check with the
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { Logger } from '@graphprotocol/common-ts'
2+
import { DataTypes, QueryInterface } from 'sequelize'
3+
4+
interface MigrationContext {
5+
queryInterface: QueryInterface
6+
logger: Logger
7+
}
8+
9+
interface Context {
10+
context: MigrationContext
11+
}
12+
13+
export async function up({ context }: Context): Promise<void> {
14+
const { queryInterface, logger } = context
15+
16+
logger.debug(`Checking if indexing rules table exists`)
17+
const tables = await queryInterface.showAllTables()
18+
if (!tables.includes('IndexingRules')) {
19+
logger.info(`Indexing rules table does not exist, migration not necessary`)
20+
return
21+
}
22+
23+
logger.debug(`Checking if 'IndexingRules' table needs to be migrated`)
24+
const table = await queryInterface.describeTable('IndexingRules')
25+
const allocationLifetime = table.allocationLifetime
26+
if (allocationLifetime) {
27+
logger.info(
28+
`'allocationLifetime' columns already exist, migration not necessary`,
29+
)
30+
return
31+
}
32+
33+
logger.info(`Add 'allocationLifetime' column to 'IndexingRules' table`)
34+
await queryInterface.addColumn('IndexingRules', 'allocationLifetime', {
35+
type: DataTypes.INTEGER,
36+
allowNull: true,
37+
defaultValue: null,
38+
})
39+
}
40+
41+
export async function down({ context }: Context): Promise<void> {
42+
const { queryInterface, logger } = context
43+
44+
return await queryInterface.sequelize.transaction({}, async transaction => {
45+
const tables = await queryInterface.showAllTables()
46+
47+
if (tables.includes('IndexingRules')) {
48+
logger.info(`Remove 'allocationLifetime' column`)
49+
await context.queryInterface.removeColumn(
50+
'IndexingRules',
51+
'allocationLifetime',
52+
{ transaction },
53+
)
54+
55+
logger.info(`Remove 'int_IndexingRules_allocationLifetime' custom type`)
56+
await queryInterface.sequelize.query(
57+
`delete from pg_type where typname = 'int_IndexingRules_allocationLifetime'`,
58+
)
59+
}
60+
})
61+
}

packages/indexer-agent/src/indexer.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ export class Indexer {
219219
identifier
220220
identifierType
221221
allocationAmount
222+
allocationLifetime
222223
parallelAllocations
223224
maxAllocationPercentage
224225
minSignal
@@ -292,6 +293,7 @@ export class Indexer {
292293
identifier
293294
identifierType
294295
allocationAmount
296+
allocationLifetime
295297
parallelAllocations
296298
maxAllocationPercentage
297299
minSignal

packages/indexer-cli/src/__tests__/indexer/rules.test.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ describe('Indexer rules tests', () => {
267267
},
268268
)
269269
cliTest(
270-
'Indexer rules set subgraph id - success - offchain',
270+
'Indexer rules set subgraph options - success',
271271
[
272272
'indexer',
273273
'rules',
@@ -277,6 +277,8 @@ describe('Indexer rules tests', () => {
277277
'1000',
278278
'decisionBasis',
279279
'offchain',
280+
'allocationLifetime',
281+
'12',
280282
],
281283
'references/indexer-rule-subgraph-offchain',
282284
{
@@ -296,16 +298,18 @@ describe('Indexer rules tests', () => {
296298
},
297299
)
298300
cliTest(
299-
'Indexer rules set deployment id - success - offchain',
301+
'Indexer rules set deployment lifetime - success',
300302
[
301303
'indexer',
302304
'rules',
303305
'set',
304306
'QmZfeJYR86UARzp9HiXbURWunYgC9ywvPvoePNbuaATrEK',
305307
'decisionBasis',
306308
'offchain',
309+
'allocationLifetime',
310+
'21',
307311
],
308-
'references/indexer-rule-deployment-offchain',
312+
'references/indexer-rule-deployment-lifetime',
309313
{
310314
expectedExitCode: 0,
311315
cwd: baseDir,
@@ -384,6 +388,16 @@ describe('Indexer rules tests', () => {
384388
timeout: 10000,
385389
},
386390
)
391+
cliTest(
392+
'Indexer rules get subgraph - success - options',
393+
['indexer', 'rules', 'get', '0x0000000000000000000000000000000000000000-2'],
394+
'references/indexer-rule-subgraph-options',
395+
{
396+
expectedExitCode: 0,
397+
cwd: baseDir,
398+
timeout: 10000,
399+
},
400+
)
387401
cliTest(
388402
'Indexer rules get global - success',
389403
['indexer', 'rules', 'get', 'global'],
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
┌────────────────────────────────────────────────┬────────────────┬──────────────────┬─────────────────────┬─────────────────────────┬───────────┬───────────┬──────────┬─────────────────────┬────────┬───────────────┐
2-
│ identifier │ identifierType │ allocationAmount │ parallelAllocations │ maxAllocationPercentage │ minSignal │ maxSignal │ minStake │ minAverageQueryFees │ custom │ decisionBasis │
3-
├────────────────────────────────────────────────┼────────────────┼──────────────────┼─────────────────────┼─────────────────────────┼───────────┼───────────┼──────────┼─────────────────────┼────────┼───────────────┤
4-
│ QmZZtzZkfzCWMNrajxBf22q7BC9HzoT5iJUK3S8qA6zNZr │ deployment │ null │ null │ null │ null │ null │ null │ null │ null │ always │
5-
└────────────────────────────────────────────────┴────────────────┴──────────────────┴─────────────────────┴─────────────────────────┴───────────┴───────────┴──────────┴─────────────────────┴────────┴───────────────┘
1+
┌────────────────────────────────────────────────┬────────────────┬──────────────────┬────────────────────┬─────────────────────┬─────────────────────────┬───────────┬───────────┬──────────┬─────────────────────┬────────┬───────────────┐
2+
│ identifier │ identifierType │ allocationAmount │ allocationLifetime │ parallelAllocations │ maxAllocationPercentage │ minSignal │ maxSignal │ minStake │ minAverageQueryFees │ custom │ decisionBasis │
3+
├────────────────────────────────────────────────┼────────────────┼──────────────────┼────────────────────┼─────────────────────┼─────────────────────────┼───────────┼───────────┼──────────┼─────────────────────┼────────┼───────────────┤
4+
│ QmZZtzZkfzCWMNrajxBf22q7BC9HzoT5iJUK3S8qA6zNZr │ deployment │ null │ null │ null │ null │ null │ null │ null │ null │ null │ always │
5+
└────────────────────────────────────────────────┴────────────────┴──────────────────┴────────────────────┴─────────────────────┴─────────────────────────┴───────────┴───────────┴──────────┴─────────────────────┴────────┴───────────────┘
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
┌────────────────────────────────────────────────┬────────────────┬──────────────────┬────────────────────┬─────────────────────┬─────────────────────────┬───────────┬───────────┬──────────┬─────────────────────┬────────┬───────────────┐
2+
│ identifier │ identifierType │ allocationAmount │ allocationLifetime │ parallelAllocations │ maxAllocationPercentage │ minSignal │ maxSignal │ minStake │ minAverageQueryFees │ custom │ decisionBasis │
3+
├────────────────────────────────────────────────┼────────────────┼──────────────────┼────────────────────┼─────────────────────┼─────────────────────────┼───────────┼───────────┼──────────┼─────────────────────┼────────┼───────────────┤
4+
│ QmZfeJYR86UARzp9HiXbURWunYgC9ywvPvoePNbuaATrEK │ deployment │ null │ 21 │ null │ null │ null │ null │ null │ null │ null │ offchain │
5+
└────────────────────────────────────────────────┴────────────────┴──────────────────┴────────────────────┴─────────────────────┴─────────────────────────┴───────────┴───────────┴──────────┴─────────────────────┴────────┴───────────────┘
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
┌────────────────────────────────────────────────┬────────────────┬──────────────────┬─────────────────────┬─────────────────────────┬───────────┬───────────┬──────────┬─────────────────────┬────────┬───────────────┐
2-
│ identifier │ identifierType │ allocationAmount │ parallelAllocations │ maxAllocationPercentage │ minSignal │ maxSignal │ minStake │ minAverageQueryFees │ custom │ decisionBasis │
3-
├────────────────────────────────────────────────┼────────────────┼──────────────────┼─────────────────────┼─────────────────────────┼───────────┼───────────┼──────────┼─────────────────────┼────────┼───────────────┤
4-
│ QmZZtzZkfzCWMNrajxBf22q7BC9HzoT5iJUK3S8qA6zNZr │ deployment │ null │ null │ null │ null │ null │ null │ null │ null │ never │
5-
└────────────────────────────────────────────────┴────────────────┴──────────────────┴─────────────────────┴─────────────────────────┴───────────┴───────────┴──────────┴─────────────────────┴────────┴───────────────┘
1+
┌────────────────────────────────────────────────┬────────────────┬──────────────────┬────────────────────┬─────────────────────┬─────────────────────────┬───────────┬───────────┬──────────┬─────────────────────┬────────┬───────────────┐
2+
│ identifier │ identifierType │ allocationAmount │ allocationLifetime │ parallelAllocations │ maxAllocationPercentage │ minSignal │ maxSignal │ minStake │ minAverageQueryFees │ custom │ decisionBasis │
3+
├────────────────────────────────────────────────┼────────────────┼──────────────────┼────────────────────┼─────────────────────┼─────────────────────────┼───────────┼───────────┼──────────┼─────────────────────┼────────┼───────────────┤
4+
│ QmZZtzZkfzCWMNrajxBf22q7BC9HzoT5iJUK3S8qA6zNZr │ deployment │ null │ null │ null │ null │ null │ null │ null │ null │ null │ never │
5+
└────────────────────────────────────────────────┴────────────────┴──────────────────┴────────────────────┴─────────────────────┴─────────────────────────┴───────────┴───────────┴──────────┴─────────────────────┴────────┴───────────────┘

0 commit comments

Comments
 (0)