Skip to content

Commit 87552cb

Browse files
authored
feat: add pending RCA proposal consumer and migration (#1174)
1 parent e8eb72f commit 87552cb

16 files changed

Lines changed: 1544 additions & 16 deletions

File tree

packages/indexer-agent/src/agent.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,11 @@ export class Agent {
716716
if (!operator.dipsManager) {
717717
throw new Error('DipsManager is not available')
718718
}
719+
720+
await operator.dipsManager.acceptPendingProposals(
721+
activeAllocations,
722+
)
723+
719724
this.logger.debug(
720725
`Matching agreement allocations for network ${network.specification.networkIdentifier}`,
721726
)

packages/indexer-agent/src/commands/start.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
createIndexerManagementClient,
1616
createIndexerManagementServer,
1717
defineIndexerManagementModels,
18+
definePendingRcaProposalModel,
1819
defineQueryFeeModels,
1920
GraphNode,
2021
indexerError,
@@ -670,6 +671,9 @@ export async function run(
670671
await sequelize.sync()
671672
logger.info(`Successfully synced database models`)
672673

674+
// Define after sync so Sequelize won't try to create/alter this indexer-rs-owned table
675+
const pendingRcaModel = definePendingRcaProposalModel(sequelize)
676+
673677
// --------------------------------------------------------------------------------
674678
// * Networks
675679
// --------------------------------------------------------------------------------
@@ -710,6 +714,7 @@ export async function run(
710714
},
711715
},
712716
multiNetworks,
717+
pendingRcaModel,
713718
})
714719

715720
// --------------------------------------------------------------------------------
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { Logger } from '@graphprotocol/common-ts'
2+
3+
import { QueryInterface, DataTypes } from 'sequelize'
4+
5+
interface MigrationContext {
6+
queryInterface: QueryInterface
7+
logger: Logger
8+
}
9+
10+
interface Context {
11+
context: MigrationContext
12+
}
13+
14+
export async function up({ context }: Context): Promise<void> {
15+
const { queryInterface, logger } = context
16+
17+
const tables = await queryInterface.showAllTables()
18+
logger.debug('Checking if pending_rca_proposals table exists', { tables })
19+
20+
if (tables.includes('pending_rca_proposals')) {
21+
logger.debug(
22+
'pending_rca_proposals already exists, migration not necessary',
23+
)
24+
return
25+
}
26+
27+
logger.info('Create pending_rca_proposals')
28+
await queryInterface.createTable('pending_rca_proposals', {
29+
id: {
30+
type: DataTypes.UUID,
31+
primaryKey: true,
32+
},
33+
signed_payload: {
34+
type: DataTypes.BLOB,
35+
allowNull: false,
36+
},
37+
version: {
38+
type: DataTypes.SMALLINT,
39+
allowNull: false,
40+
defaultValue: 2,
41+
},
42+
status: {
43+
type: DataTypes.STRING(20),
44+
allowNull: false,
45+
defaultValue: 'pending',
46+
},
47+
created_at: {
48+
type: DataTypes.DATE,
49+
allowNull: false,
50+
},
51+
updated_at: {
52+
type: DataTypes.DATE,
53+
allowNull: false,
54+
},
55+
})
56+
57+
await queryInterface.addIndex(
58+
'pending_rca_proposals',
59+
['status', 'created_at'],
60+
{
61+
name: 'idx_pending_rca_status',
62+
},
63+
)
64+
await queryInterface.addIndex('pending_rca_proposals', {
65+
fields: [{ name: 'created_at', order: 'DESC' }],
66+
name: 'idx_pending_rca_created',
67+
})
68+
}
69+
70+
export async function down({ context }: Context): Promise<void> {
71+
const { queryInterface, logger } = context
72+
logger.info('Drop pending_rca_proposals')
73+
await queryInterface.dropTable('pending_rca_proposals')
74+
}

packages/indexer-common/src/indexer-management/actions.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
import { Order, Transaction } from 'sequelize'
2525
import { Eventual, join, Logger } from '@graphprotocol/common-ts'
2626
import groupBy from 'lodash.groupby'
27+
import { PendingRcaProposal } from './models/pending-rca-proposal'
2728

2829
export class ActionManager {
2930
declare multiNetworks: MultiNetworks<Network>
@@ -38,6 +39,7 @@ export class ActionManager {
3839
logger: Logger,
3940
models: IndexerManagementModels,
4041
graphNode: GraphNode,
42+
pendingRcaModel?: typeof PendingRcaProposal,
4143
): Promise<ActionManager> {
4244
const actionManager = new ActionManager()
4345
actionManager.multiNetworks = multiNetworks
@@ -52,6 +54,7 @@ export class ActionManager {
5254
models,
5355
graphNode,
5456
network,
57+
pendingRcaModel,
5558
)
5659
})
5760

packages/indexer-common/src/indexer-management/allocations.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import {
4747
encodeStopServiceData,
4848
PaymentTypes,
4949
} from '@graphprotocol/toolshed'
50+
import { PendingRcaProposal } from './models/pending-rca-proposal'
5051
import {
5152
encodeCollectIndexingRewardsData,
5253
encodePOIMetadata,
@@ -166,9 +167,16 @@ export class AllocationManager {
166167
private models: IndexerManagementModels,
167168
private graphNode: GraphNode,
168169
private network: Network,
170+
private pendingRcaModel?: typeof PendingRcaProposal,
169171
) {
170172
if (this.network.specification.indexerOptions.dipperEndpoint) {
171-
this.dipsManager = new DipsManager(this.logger, this.models, this.network, this)
173+
this.dipsManager = new DipsManager(
174+
this.logger,
175+
this.models,
176+
this.network,
177+
this,
178+
this.pendingRcaModel,
179+
)
172180
}
173181
}
174182

packages/indexer-common/src/indexer-management/client.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
Network,
2121
RulesManager,
2222
} from '@graphprotocol/indexer-common'
23+
import { PendingRcaProposal } from './models/pending-rca-proposal'
2324

2425
export interface IndexerManagementResolverContext {
2526
models: IndexerManagementModels
@@ -574,6 +575,7 @@ export interface IndexerManagementClientOptions {
574575
multiNetworks: MultiNetworks<Network> | undefined
575576
defaults: IndexerManagementDefaults
576577
actionManager?: ActionManager | undefined
578+
pendingRcaModel?: typeof PendingRcaProposal
577579
}
578580

579581
export class IndexerManagementClient extends Client {
@@ -595,7 +597,7 @@ export class IndexerManagementClient extends Client {
595597
export const createIndexerManagementClient = async (
596598
options: IndexerManagementClientOptions,
597599
): Promise<IndexerManagementClient> => {
598-
const { models, graphNode, logger, defaults, multiNetworks } = options
600+
const { models, graphNode, logger, defaults, multiNetworks, pendingRcaModel } = options
599601
const schema = buildSchema(print(SCHEMA_SDL))
600602
const resolvers = {
601603
...indexingRuleResolvers,
@@ -608,7 +610,13 @@ export const createIndexerManagementClient = async (
608610
}
609611

610612
const actionManager = multiNetworks
611-
? await ActionManager.create(multiNetworks, logger, models, graphNode)
613+
? await ActionManager.create(
614+
multiNetworks,
615+
logger,
616+
models,
617+
graphNode,
618+
pendingRcaModel,
619+
)
612620
: undefined
613621

614622
const rulesManager = multiNetworks

packages/indexer-common/src/indexer-management/models/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export * from './indexing-rule'
1111
export * from './poi-dispute'
1212
export * from './action'
1313
export * from './indexing-agreement'
14+
export * from './pending-rca-proposal'
1415

1516
export type IndexerManagementModels = IndexingRuleModels &
1617
CostModelModels &
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { DataTypes, Sequelize, Model, InferAttributes } from 'sequelize'
2+
3+
export class PendingRcaProposal extends Model<InferAttributes<PendingRcaProposal>> {
4+
declare id: string
5+
declare signed_payload: Buffer
6+
declare version: number
7+
declare status: string
8+
declare created_at: Date
9+
declare updated_at: Date
10+
}
11+
12+
export const definePendingRcaProposalModel = (
13+
sequelize: Sequelize,
14+
): typeof PendingRcaProposal => {
15+
PendingRcaProposal.init(
16+
{
17+
id: {
18+
type: DataTypes.UUID,
19+
primaryKey: true,
20+
},
21+
signed_payload: {
22+
type: DataTypes.BLOB,
23+
allowNull: false,
24+
},
25+
version: {
26+
type: DataTypes.SMALLINT,
27+
allowNull: false,
28+
defaultValue: 2,
29+
},
30+
status: {
31+
type: DataTypes.STRING(20),
32+
allowNull: false,
33+
defaultValue: 'pending',
34+
},
35+
created_at: {
36+
type: DataTypes.DATE,
37+
allowNull: false,
38+
},
39+
updated_at: {
40+
type: DataTypes.DATE,
41+
allowNull: false,
42+
},
43+
},
44+
{
45+
modelName: 'PendingRcaProposal',
46+
sequelize,
47+
tableName: 'pending_rca_proposals',
48+
timestamps: true,
49+
createdAt: 'created_at',
50+
updatedAt: 'updated_at',
51+
},
52+
)
53+
54+
return PendingRcaProposal
55+
}

0 commit comments

Comments
 (0)