Refactor create checkpoint CLI to cli package (#247)

* Refactor create checkpoint CLI to cli package

* Update mock indexer
This commit is contained in:
prathamesh0 2022-11-21 23:08:23 -06:00 committed by GitHub
parent 06e5c62fa8
commit 03050e5323
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 107 additions and 131 deletions

View File

@ -0,0 +1,76 @@
//
// Copyright 2022 Vulcanize, Inc.
//
import debug from 'debug';
import 'reflect-metadata';
import assert from 'assert';
import { ConnectionOptions } from 'typeorm';
import { JsonRpcProvider } from '@ethersproject/providers';
import { GraphWatcher } from '@cerc-io/graph-node';
import {
JobQueue,
DatabaseInterface,
IndexerInterface,
ServerConfig,
Clients
} from '@cerc-io/util';
import { BaseCmd } from '../base';
const log = debug('vulcanize:checkpoint-create');
interface Arguments {
configFile: string;
address: string;
blockHash: string;
}
export class CreateCheckpointCmd {
_argv?: Arguments
_baseCmd: BaseCmd
_database?: DatabaseInterface
_indexer?: IndexerInterface
constructor () {
this._baseCmd = new BaseCmd();
}
async initConfig<ConfigType> (configFile: string): Promise<ConfigType> {
return this._baseCmd.initConfig(configFile);
}
async init (
argv: any,
Database: new (
config: ConnectionOptions,
serverConfig?: ServerConfig
) => DatabaseInterface,
Indexer: new (
serverConfig: ServerConfig,
db: DatabaseInterface,
clients: Clients,
ethProvider: JsonRpcProvider,
jobQueue: JobQueue,
graphWatcher?: GraphWatcher
) => IndexerInterface,
clients: { [key: string]: any } = {}
): Promise<void> {
this._argv = argv;
await this.initConfig(argv.configFile);
({ database: this._database, indexer: this._indexer } = await this._baseCmd.init(Database, Indexer, clients));
}
async exec (): Promise<void> {
assert(this._argv);
assert(this._database);
assert(this._indexer);
const blockHash = await this._indexer.processCLICheckpoint(this._argv.address, this._argv.blockHash);
await this._database.close();
log(`Created a checkpoint for contract ${this._argv.address} at block-hash ${blockHash}`);
}
}

View File

@ -5,3 +5,4 @@
export * from './watch-contract';
export * from './reset/watcher';
export * from './reset/state';
export * from './checkpoint/create';

View File

@ -64,9 +64,12 @@ export class ResetWatcherCmd {
async exec (): Promise<void> {
assert(this._argv);
assert(this._database);
assert(this._indexer);
await this._indexer.resetWatcherToBlock(this._argv.blockNumber);
await this._database.close();
log('Reset watcher successfully');
}
}

View File

@ -2,17 +2,11 @@
// Copyright 2022 Vulcanize, Inc.
//
import debug from 'debug';
import assert from 'assert';
import { CreateCheckpointCmd } from '@cerc-io/cli';
import { getConfig, initClients, JobQueue, Config } from '@cerc-io/util';
import { GraphWatcher, Database as GraphDatabase } from '@cerc-io/graph-node';
import { Database, ENTITY_TO_LATEST_ENTITY_MAP } from '../../database';
import { Database } from '../../database';
import { Indexer } from '../../indexer';
const log = debug('vulcanize:checkpoint-create');
export const command = 'create';
export const desc = 'Create checkpoint';
@ -31,35 +25,8 @@ export const builder = {
};
export const handler = async (argv: any): Promise<void> => {
const config: Config = await getConfig(argv.configFile);
const { ethClient, ethProvider } = await initClients(config);
const createCheckpointCmd = new CreateCheckpointCmd();
await createCheckpointCmd.init(argv, Database, Indexer);
const db = new Database(config.database);
await db.init();
const graphDb = new GraphDatabase(config.server, db.baseDatabase, ENTITY_TO_LATEST_ENTITY_MAP);
await graphDb.init();
const graphWatcher = new GraphWatcher(graphDb, ethClient, ethProvider, config.server);
const jobQueueConfig = config.jobQueue;
assert(jobQueueConfig, 'Missing job queue config');
const { dbConnectionString, maxCompletionLagInSecs } = jobQueueConfig;
assert(dbConnectionString, 'Missing job queue db connection string');
const jobQueue = new JobQueue({ dbConnectionString, maxCompletionLag: maxCompletionLagInSecs });
await jobQueue.start();
const indexer = new Indexer(config.server, db, { ethClient }, ethProvider, jobQueue, graphWatcher);
await indexer.init();
graphWatcher.setIndexer(indexer);
await graphWatcher.init();
const blockHash = await indexer.processCLICheckpoint(argv.address, argv.blockHash);
log(`Created a checkpoint for contract ${argv.address} at block-hash ${blockHash}`);
await db.close();
await createCheckpointCmd.exec();
};

View File

@ -263,6 +263,11 @@ export class Indexer implements IndexerInterface {
// TODO Implement
}
async processCLICheckpoint (contractAddress: string, blockHash?: string): Promise<string | undefined> {
// TODO Implement
return '';
}
getStateData (state: State): any {
return this._baseIndexer.getStateData(state);
}

View File

@ -2,16 +2,11 @@
// Copyright 2022 Vulcanize, Inc.
//
import debug from 'debug';
import assert from 'assert';
import { getConfig, initClients, JobQueue, Config } from '@cerc-io/util';
import { CreateCheckpointCmd } from '@cerc-io/cli';
import { Database } from '../../database';
import { Indexer } from '../../indexer';
const log = debug('vulcanize:checkpoint-create');
export const command = 'create';
export const desc = 'Create checkpoint';
@ -30,27 +25,8 @@ export const builder = {
};
export const handler = async (argv: any): Promise<void> => {
const config: Config = await getConfig(argv.configFile);
const { ethClient, ethProvider } = await initClients(config);
const createCheckpointCmd = new CreateCheckpointCmd();
await createCheckpointCmd.init(argv, Database, Indexer);
const db = new Database(config.database);
await db.init();
const jobQueueConfig = config.jobQueue;
assert(jobQueueConfig, 'Missing job queue config');
const { dbConnectionString, maxCompletionLagInSecs } = jobQueueConfig;
assert(dbConnectionString, 'Missing job queue db connection string');
const jobQueue = new JobQueue({ dbConnectionString, maxCompletionLag: maxCompletionLagInSecs });
await jobQueue.start();
const indexer = new Indexer(config.server, db, { ethClient }, ethProvider, jobQueue);
await indexer.init();
const blockHash = await indexer.processCLICheckpoint(argv.address, argv.blockHash);
log(`Created a checkpoint for contract ${argv.address} at block-hash ${blockHash}`);
await db.close();
await createCheckpointCmd.exec();
};

View File

@ -200,6 +200,10 @@ export class Indexer implements IndexerInterface {
return undefined;
}
async processCLICheckpoint (contractAddress: string, blockHash?: string): Promise<string | undefined> {
return undefined;
}
getStateData (state: StateInterface): any {
return undefined;
}

View File

@ -2,17 +2,11 @@
// Copyright 2022 Vulcanize, Inc.
//
import debug from 'debug';
import assert from 'assert';
import { getConfig, initClients, JobQueue, Config } from '@cerc-io/util';
import { GraphWatcher, Database as GraphDatabase } from '@cerc-io/graph-node';
import { CreateCheckpointCmd } from '@cerc-io/cli';
import { Database } from '../../database';
import { Indexer } from '../../indexer';
const log = debug('vulcanize:checkpoint-create');
export const command = 'create';
export const desc = 'Create checkpoint';
@ -31,35 +25,8 @@ export const builder = {
};
export const handler = async (argv: any): Promise<void> => {
const config: Config = await getConfig(argv.configFile);
const { ethClient, ethProvider } = await initClients(config);
const createCheckpointCmd = new CreateCheckpointCmd();
await createCheckpointCmd.init(argv, Database, Indexer);
const db = new Database(config.database);
await db.init();
const graphDb = new GraphDatabase(config.server, db.baseDatabase);
await graphDb.init();
const graphWatcher = new GraphWatcher(graphDb, ethClient, ethProvider, config.server);
const jobQueueConfig = config.jobQueue;
assert(jobQueueConfig, 'Missing job queue config');
const { dbConnectionString, maxCompletionLagInSecs } = jobQueueConfig;
assert(dbConnectionString, 'Missing job queue db connection string');
const jobQueue = new JobQueue({ dbConnectionString, maxCompletionLag: maxCompletionLagInSecs });
await jobQueue.start();
const indexer = new Indexer(config.server, db, { ethClient }, ethProvider, jobQueue, graphWatcher);
await indexer.init();
graphWatcher.setIndexer(indexer);
await graphWatcher.init();
const blockHash = await indexer.processCLICheckpoint(argv.address, argv.blockHash);
log(`Created a checkpoint for contract ${argv.address} at block-hash ${blockHash}`);
await db.close();
await createCheckpointCmd.exec();
};

View File

@ -2,16 +2,11 @@
// Copyright 2022 Vulcanize, Inc.
//
import debug from 'debug';
import assert from 'assert';
import { getConfig, initClients, JobQueue, Config } from '@cerc-io/util';
import { CreateCheckpointCmd } from '@cerc-io/cli';
import { Database } from '../../database';
import { Indexer } from '../../indexer';
const log = debug('vulcanize:checkpoint-create');
export const command = 'create';
export const desc = 'Create checkpoint';
@ -30,27 +25,8 @@ export const builder = {
};
export const handler = async (argv: any): Promise<void> => {
const config: Config = await getConfig(argv.configFile);
const { ethClient, ethProvider } = await initClients(config);
const createCheckpointCmd = new CreateCheckpointCmd();
await createCheckpointCmd.init(argv, Database, Indexer);
const db = new Database(config.database);
await db.init();
const jobQueueConfig = config.jobQueue;
assert(jobQueueConfig, 'Missing job queue config');
const { dbConnectionString, maxCompletionLagInSecs } = jobQueueConfig;
assert(dbConnectionString, 'Missing job queue db connection string');
const jobQueue = new JobQueue({ dbConnectionString, maxCompletionLag: maxCompletionLagInSecs });
await jobQueue.start();
const indexer = new Indexer(config.server, db, { ethClient }, ethProvider, jobQueue);
await indexer.init();
const blockHash = await indexer.processCLICheckpoint(argv.address, argv.blockHash);
log(`Created a checkpoint for contract ${argv.address} at block-hash ${blockHash}`);
await db.close();
await createCheckpointCmd.exec();
};

View File

@ -119,6 +119,7 @@ export interface IndexerInterface {
processBlockAfterEvents?: (blockHash: string, blockNumber: number) => Promise<void>
processCanonicalBlock (blockHash: string, blockNumber: number): Promise<void>
processCheckpoint (blockHash: string): Promise<void>
processCLICheckpoint (contractAddress: string, blockHash?: string): Promise<string | undefined>
getStorageValue (storageLayout: StorageLayout, blockHash: string, contractAddress: string, variable: string, ...mappingKeys: MappingKey[]): Promise<ValueResult>
updateSubgraphState?: (contractAddress: string, data: any) => void
updateStateStatusMap (address: string, stateStatus: StateStatus): void