Refactor export-state and verify checkpoint CLIs to cli package (#252)

* Refactor export-state CLI to cli package

* Refactor verify checkpoint CLI to cli package

* Update mock indexer object
This commit is contained in:
prathamesh0
2022-11-22 18:27:49 +05:30
committed by GitHub
parent ace52d9da3
commit 122a64c2f9
13 changed files with 333 additions and 590 deletions
-2
View File
@@ -30,8 +30,6 @@ interface Arguments {
export class CreateCheckpointCmd {
_argv?: Arguments
_baseCmd: BaseCmd
_database?: DatabaseInterface
_indexer?: IndexerInterface
constructor () {
this._baseCmd = new BaseCmd();
+86
View File
@@ -0,0 +1,86 @@
//
// 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, Database as GraphDatabase } from '@cerc-io/graph-node';
import {
JobQueue,
DatabaseInterface,
IndexerInterface,
ServerConfig,
Clients,
verifyCheckpointData
} from '@cerc-io/util';
import { BaseCmd } from '../base';
const log = debug('vulcanize:checkpoint-verify');
interface Arguments {
configFile: string;
cid: string;
}
export class VerifyCheckpointCmd {
_argv?: Arguments
_baseCmd: BaseCmd
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);
await this._baseCmd.init(Database, Indexer, clients);
}
async exec (): Promise<void> {
assert(this._argv);
const database = this._baseCmd.database;
const indexer = this._baseCmd.indexer;
assert(database);
assert(indexer);
const graphDb: GraphDatabase | undefined = this._baseCmd.graphDb || database.graphDatabase;
assert(graphDb);
const state = await indexer.getStateByCID(this._argv.cid);
assert(state, 'State for the provided CID doesn\'t exist.');
const data = indexer.getStateData(state);
log(`Verifying checkpoint data for contract ${state.contractAddress}`);
await verifyCheckpointData(graphDb, state.block, data);
log('Checkpoint data verified');
await database.close();
}
}
+182
View File
@@ -0,0 +1,182 @@
//
// Copyright 2022 Vulcanize, Inc.
//
import yargs from 'yargs';
import 'reflect-metadata';
import assert from 'assert';
import path from 'path';
import fs from 'fs';
import debug from 'debug';
import { ConnectionOptions } from 'typeorm';
import { JsonRpcProvider } from '@ethersproject/providers';
import { GraphWatcher } from '@cerc-io/graph-node';
import {
DEFAULT_CONFIG_PATH,
JobQueue,
DatabaseInterface,
IndexerInterface,
ServerConfig,
StateKind,
Clients
} from '@cerc-io/util';
import * as codec from '@ipld/dag-cbor';
import { BaseCmd } from './base';
const log = debug('vulcanize:export-state');
interface Arguments {
configFile: string;
exportFile: string;
blockNumber: number;
}
export class ExportStateCmd {
_argv?: Arguments
_baseCmd: BaseCmd;
constructor () {
this._baseCmd = new BaseCmd();
}
async initConfig<ConfigType> (): Promise<ConfigType> {
this._argv = this._getArgv();
assert(this._argv);
return this._baseCmd.initConfig(this._argv.configFile);
}
async init (
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> {
await this.initConfig();
await this._baseCmd.init(Database, Indexer, clients);
}
async exec (): Promise<void> {
assert(this._argv);
const database = this._baseCmd.database;
const indexer = this._baseCmd.indexer;
assert(database);
assert(indexer);
const exportData: any = {
snapshotBlock: {},
contracts: [],
stateCheckpoints: []
};
const contracts = await database.getContracts();
let block = await indexer.getLatestStateIndexedBlock();
assert(block);
if (this._argv.blockNumber) {
if (this._argv.blockNumber > block.blockNumber) {
throw new Error(`Export snapshot block height ${this._argv.blockNumber} should be less than latest state indexed block height ${block.blockNumber}`);
}
const blocksAtSnapshotHeight = await indexer.getBlocksAtHeight(this._argv.blockNumber, false);
if (!blocksAtSnapshotHeight.length) {
throw new Error(`No blocks at snapshot height ${this._argv.blockNumber}`);
}
block = blocksAtSnapshotHeight[0];
}
log(`Creating export snapshot at block height ${block.blockNumber}`);
// Export snapshot block.
exportData.snapshotBlock = {
blockNumber: block.blockNumber,
blockHash: block.blockHash
};
// Export contracts and checkpoints.
for (const contract of contracts) {
if (contract.startingBlock > block.blockNumber) {
continue;
}
exportData.contracts.push({
address: contract.address,
kind: contract.kind,
checkpoint: contract.checkpoint,
startingBlock: block.blockNumber
});
// Create and export checkpoint if checkpointing is on for the contract.
if (contract.checkpoint) {
await indexer.createCheckpoint(contract.address, block.blockHash);
const state = await indexer.getLatestState(contract.address, StateKind.Checkpoint, block.blockNumber);
assert(state);
const data = indexer.getStateData(state);
exportData.stateCheckpoints.push({
contractAddress: state.contractAddress,
cid: state.cid,
kind: state.kind,
data
});
}
}
if (this._argv.exportFile) {
const encodedExportData = codec.encode(exportData);
const filePath = path.resolve(this._argv.exportFile);
const fileDir = path.dirname(filePath);
if (!fs.existsSync(fileDir)) fs.mkdirSync(fileDir, { recursive: true });
fs.writeFileSync(filePath, encodedExportData);
} else {
log(exportData);
}
log(`Export completed at height ${block.blockNumber}`);
await database.close();
}
_getArgv (): any {
return yargs.parserConfiguration({
'parse-numbers': false
}).options({
configFile: {
alias: 'f',
type: 'string',
require: true,
demandOption: true,
describe: 'Configuration file path (toml)',
default: DEFAULT_CONFIG_PATH
},
exportFile: {
alias: 'o',
type: 'string',
describe: 'Export file path'
},
blockNumber: {
type: 'number',
describe: 'Block number to create snapshot at'
}
}).argv;
}
}
+2
View File
@@ -6,5 +6,7 @@ export * from './watch-contract';
export * from './reset/watcher';
export * from './reset/state';
export * from './checkpoint/create';
export * from './checkpoint/verify';
export * from './inspect-cid';
export * from './import-state';
export * from './export-state';