Fix codegen for non subgraph watchers (#120)

* Fix codegen for non subgraph watchers

* Remove graphWatcher.init from generated non subgraph watcher
This commit is contained in:
nikugogoi 2022-05-26 18:00:17 +05:30 committed by GitHub
parent b2233e16ef
commit 168689a7c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
30 changed files with 94 additions and 46 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
node_modules/
dist/
out/
.vscode

View File

@ -13,9 +13,9 @@ const TEMPLATE_FILE = './templates/checkpoint-template.handlebars';
* Writes the checkpoint file generated from a template to a stream.
* @param outStream A writable output stream to write the checkpoint file to.
*/
export function exportCheckpoint (outStream: Writable): void {
export function exportCheckpoint (outStream: Writable, subgraphPath: string): void {
const templateString = fs.readFileSync(path.resolve(__dirname, TEMPLATE_FILE)).toString();
const template = Handlebars.compile(templateString);
const checkpoint = template({});
const checkpoint = template({ subgraphPath });
outStream.write(checkpoint);
}

View File

@ -13,9 +13,9 @@ const TEMPLATE_FILE = './templates/export-state-template.handlebars';
* Writes the export-state file generated from a template to a stream.
* @param outStream A writable output stream to write the export-state file to.
*/
export function exportState (outStream: Writable): void {
export function exportState (outStream: Writable, subgraphPath: string): void {
const templateString = fs.readFileSync(path.resolve(__dirname, TEMPLATE_FILE)).toString();
const template = Handlebars.compile(templateString);
const exportState = template({});
const exportState = template({ subgraphPath });
outStream.write(exportState);
}

View File

@ -13,9 +13,9 @@ const TEMPLATE_FILE = './templates/fill-template.handlebars';
* Writes the fill file generated from a template to a stream.
* @param outStream A writable output stream to write the fill file to.
*/
export function exportFill (outStream: Writable): void {
export function exportFill (outStream: Writable, subgraphPath: string): void {
const templateString = fs.readFileSync(path.resolve(__dirname, TEMPLATE_FILE)).toString();
const template = Handlebars.compile(templateString);
const fill = template({});
const fill = template({ subgraphPath });
outStream.write(fill);
}

View File

@ -188,7 +188,7 @@ function generateWatcher (visitor: Visitor, contracts: any[], config: any) {
outStream = outputDir
? fs.createWriteStream(path.join(outputDir, 'src/server.ts'))
: process.stdout;
exportServer(outStream);
exportServer(outStream, config.subgraphPath);
outStream = outputDir
? fs.createWriteStream(path.join(outputDir, 'environments/local.toml'))
@ -228,17 +228,17 @@ function generateWatcher (visitor: Visitor, contracts: any[], config: any) {
outStream = outputDir
? fs.createWriteStream(path.join(outputDir, 'src/job-runner.ts'))
: process.stdout;
exportJobRunner(outStream);
exportJobRunner(outStream, config.subgraphPath);
outStream = outputDir
? fs.createWriteStream(path.join(outputDir, 'src/cli/watch-contract.ts'))
: process.stdout;
exportWatchContract(outStream);
exportWatchContract(outStream, config.subgraphPath);
outStream = outputDir
? fs.createWriteStream(path.join(outputDir, 'src/cli/checkpoint.ts'))
: process.stdout;
exportCheckpoint(outStream);
exportCheckpoint(outStream, config.subgraphPath);
outStream = outputDir
? fs.createWriteStream(path.join(outputDir, 'src/hooks.ts'))
@ -248,7 +248,7 @@ function generateWatcher (visitor: Visitor, contracts: any[], config: any) {
outStream = outputDir
? fs.createWriteStream(path.join(outputDir, 'src/fill.ts'))
: process.stdout;
exportFill(outStream);
exportFill(outStream, config.subgraphPath);
outStream = outputDir
? fs.createWriteStream(path.join(outputDir, 'src/types.ts'))
@ -284,22 +284,22 @@ function generateWatcher (visitor: Visitor, contracts: any[], config: any) {
resetStateOutStream = process.stdout;
}
visitor.exportReset(resetOutStream, resetJQOutStream, resetStateOutStream);
visitor.exportReset(resetOutStream, resetJQOutStream, resetStateOutStream, config.subgraphPath);
outStream = outputDir
? fs.createWriteStream(path.join(outputDir, 'src/cli/export-state.ts'))
: process.stdout;
exportState(outStream);
exportState(outStream, config.subgraphPath);
outStream = outputDir
? fs.createWriteStream(path.join(outputDir, 'src/cli/import-state.ts'))
: process.stdout;
importState(outStream);
importState(outStream, config.subgraphPath);
outStream = outputDir
? fs.createWriteStream(path.join(outputDir, 'src/cli/inspect-cid.ts'))
: process.stdout;
exportInspectCID(outStream);
exportInspectCID(outStream, config.subgraphPath);
}
function getConfig (configFile: string): any {

View File

@ -13,9 +13,9 @@ const TEMPLATE_FILE = './templates/import-state-template.handlebars';
* Writes the import-state file generated from a template to a stream.
* @param outStream A writable output stream to write the import-state file to.
*/
export function importState (outStream: Writable): void {
export function importState (outStream: Writable, subgraphPath: string): void {
const templateString = fs.readFileSync(path.resolve(__dirname, TEMPLATE_FILE)).toString();
const template = Handlebars.compile(templateString);
const importState = template({});
const importState = template({ subgraphPath });
outStream.write(importState);
}

View File

@ -13,9 +13,9 @@ const TEMPLATE_FILE = './templates/inspect-cid-template.handlebars';
* Writes the inspect-cid file generated from a template to a stream.
* @param outStream A writable output stream to write the inspect-cid file to.
*/
export function exportInspectCID (outStream: Writable): void {
export function exportInspectCID (outStream: Writable, subgraphPath: string): void {
const templateString = fs.readFileSync(path.resolve(__dirname, TEMPLATE_FILE)).toString();
const template = Handlebars.compile(templateString);
const inspectCid = template({});
const inspectCid = template({ subgraphPath });
outStream.write(inspectCid);
}

View File

@ -13,9 +13,9 @@ const TEMPLATE_FILE = './templates/job-runner-template.handlebars';
* Writes the job-runner file generated from a template to a stream.
* @param outStream A writable output stream to write the events file to.
*/
export function exportJobRunner (outStream: Writable): void {
export function exportJobRunner (outStream: Writable, subgraphPath: string): void {
const templateString = fs.readFileSync(path.resolve(__dirname, TEMPLATE_FILE)).toString();
const template = Handlebars.compile(templateString);
const events = template({});
const events = template({ subgraphPath });
outStream.write(events);
}

View File

@ -75,7 +75,7 @@ export class Reset {
* @param resetJQOutStream A writable output stream to write the reset job-queue file to.
* @param resetStateOutStream A writable output stream to write the reset state file to.
*/
exportReset (resetOutStream: Writable, resetJQOutStream: Writable, resetStateOutStream: Writable): void {
exportReset (resetOutStream: Writable, resetJQOutStream: Writable, resetStateOutStream: Writable, subgraphPath: string): void {
const resetTemplate = Handlebars.compile(this._resetTemplateString);
const resetString = resetTemplate({});
resetOutStream.write(resetString);
@ -86,7 +86,8 @@ export class Reset {
const resetStateTemplate = Handlebars.compile(this._resetStateTemplateString);
const obj = {
queries: this._queries
queries: this._queries,
subgraphPath
};
const resetState = resetStateTemplate(obj);
resetStateOutStream.write(resetState);

View File

@ -13,9 +13,9 @@ const TEMPLATE_FILE = './templates/server-template.handlebars';
* Writes the server file generated from a template to a stream.
* @param outStream A writable output stream to write the server file to.
*/
export function exportServer (outStream: Writable): void {
export function exportServer (outStream: Writable, subgraphPath: string): void {
const templateString = fs.readFileSync(path.resolve(__dirname, TEMPLATE_FILE)).toString();
const template = Handlebars.compile(templateString);
const server = template({});
const server = template({ subgraphPath });
outStream.write(server);
}

View File

@ -64,7 +64,9 @@ const main = async (): Promise<void> => {
await indexer.init();
graphWatcher.setIndexer(indexer);
{{#if subgraphPath}}
await graphWatcher.init();
{{/if}}
const blockHash = await indexer.processCLICheckpoint(argv.address, argv.blockHash);

View File

@ -61,7 +61,9 @@ const main = async (): Promise<void> => {
await indexer.init();
graphWatcher.setIndexer(indexer);
{{#if subgraphPath}}
await graphWatcher.init();
{{/if}}
const exportData: any = {
snapshotBlock: {},

View File

@ -76,7 +76,9 @@ export const main = async (): Promise<any> => {
await indexer.init();
graphWatcher.setIndexer(indexer);
{{#if subgraphPath}}
await graphWatcher.init();
{{/if}}
// Note: In-memory pubsub works fine for now, as each watcher is a single process anyway.
// Later: https://www.apollographql.com/docs/apollo-server/data/subscriptions/#production-pubsub-libraries

View File

@ -69,7 +69,9 @@ export const main = async (): Promise<any> => {
await indexer.init();
graphWatcher.setIndexer(indexer);
{{#if subgraphPath}}
await graphWatcher.init();
{{/if}}
const eventWatcher = new EventWatcher(config.upstream, ethClient, postgraphileClient, indexer, pubsub, jobQueue);

View File

@ -15,7 +15,7 @@ import { EthClient } from '@vulcanize/ipld-eth-client';
import { StorageLayout } from '@vulcanize/solidity-mapper';
import {
IPLDIndexer as BaseIndexer,
IndexerInterface,
IPLDIndexerInterface,
ValueResult,
UNKNOWN_EVENT_NAME,
ServerConfig,
@ -26,7 +26,8 @@ import {
updateStateForMappingType,
BlockHeight,
IPFSClient,
StateKind
StateKind,
IpldStatus as IpldStatusInterface
} from '@vulcanize/util';
import { GraphWatcher } from '@vulcanize/graph-node';
@ -94,7 +95,7 @@ export type ResultIPLDBlock = {
data: string;
};
export class Indexer implements IndexerInterface {
export class Indexer implements IPLDIndexerInterface {
_db: Database
_ethClient: EthClient
_ethProvider: BaseProvider
@ -563,11 +564,15 @@ export class Indexer implements IndexerInterface {
}
async watchContract (address: string, kind: string, checkpoint: boolean, startingBlock: number): Promise<void> {
this._baseIndexer.updateIPLDStatusMap(address, {});
await this.updateIPLDStatusMap(address, {});
return this._baseIndexer.watchContract(address, kind, checkpoint, startingBlock);
}
async updateIPLDStatusMap (address: string, ipldStatus: IpldStatusInterface): Promise<void> {
await this._baseIndexer.updateIPLDStatusMap(address, ipldStatus);
}
cacheContract (contract: Contract): void {
return this._baseIndexer.cacheContract(contract);
}

View File

@ -61,7 +61,9 @@ const main = async (): Promise<void> => {
await indexer.init();
graphWatcher.setIndexer(indexer);
{{#if subgraphPath}}
await graphWatcher.init();
{{/if}}
const ipldBlock = await indexer.getIPLDBlockByCid(argv.cid);
assert(ipldBlock, 'IPLDBlock for the provided CID doesn\'t exist.');

View File

@ -271,10 +271,12 @@ export const main = async (): Promise<any> => {
await indexer.init();
graphWatcher.setIndexer(indexer);
{{#if subgraphPath}}
await graphWatcher.init();
// Watching all the contracts in the subgraph.
await graphWatcher.addContracts();
{{/if}}
const jobRunner = new JobRunner(jobQueueConfig, indexer, jobQueue);
await jobRunner.start();

View File

@ -57,7 +57,9 @@ export const handler = async (argv: any): Promise<void> => {
await indexer.init();
graphWatcher.setIndexer(indexer);
{{#if subgraphPath}}
await graphWatcher.init();
{{/if}}
const blockProgresses = await indexer.getBlocksAtHeight(argv.blockNumber, false);
assert(blockProgresses.length, `No blocks at specified block number ${argv.blockNumber}`);

View File

@ -64,7 +64,9 @@ export const main = async (): Promise<any> => {
await indexer.init();
graphWatcher.setIndexer(indexer);
{{#if subgraphPath}}
await graphWatcher.init();
{{/if}}
const eventWatcher = new EventWatcher(config.upstream, ethClient, postgraphileClient, indexer, pubsub, jobQueue);

View File

@ -77,7 +77,9 @@ const main = async (): Promise<void> => {
await indexer.init();
graphWatcher.setIndexer(indexer);
{{#if subgraphPath}}
await graphWatcher.init();
{{/if}}
await indexer.watchContract(argv.address, argv.kind, argv.checkpoint, argv.startingBlock);

View File

@ -218,8 +218,8 @@ export class Visitor {
* @param resetJQOutStream A writable output stream to write the reset job-queue file to.
* @param resetStateOutStream A writable output stream to write the reset state file to.
*/
exportReset (resetOutStream: Writable, resetJQOutStream: Writable, resetStateOutStream: Writable): void {
this._reset.exportReset(resetOutStream, resetJQOutStream, resetStateOutStream);
exportReset (resetOutStream: Writable, resetJQOutStream: Writable, resetStateOutStream: Writable, subgraphPath: string): void {
this._reset.exportReset(resetOutStream, resetJQOutStream, resetStateOutStream, subgraphPath);
}
/**

View File

@ -13,9 +13,9 @@ const TEMPLATE_FILE = './templates/watch-contract-template.handlebars';
* Writes the watch-contract file generated from a template to a stream.
* @param outStream A writable output stream to write the watch-contract file to.
*/
export function exportWatchContract (outStream: Writable): void {
export function exportWatchContract (outStream: Writable, subgraphPath: string): void {
const templateString = fs.readFileSync(path.resolve(__dirname, TEMPLATE_FILE)).toString();
const template = Handlebars.compile(templateString);
const events = template({});
const events = template({ subgraphPath });
outStream.write(events);
}

View File

@ -15,7 +15,6 @@ import { EthClient } from '@vulcanize/ipld-eth-client';
import { StorageLayout } from '@vulcanize/solidity-mapper';
import {
IPLDIndexer as BaseIndexer,
IndexerInterface,
UNKNOWN_EVENT_NAME,
ServerConfig,
JobQueue,
@ -23,7 +22,9 @@ import {
QueryOptions,
BlockHeight,
IPFSClient,
StateKind
StateKind,
IPLDIndexerInterface,
IpldStatus as IpldStatusInterface
} from '@vulcanize/util';
import { GraphWatcher } from '@vulcanize/graph-node';
@ -125,7 +126,7 @@ export type ResultIPLDBlock = {
data: string;
};
export class Indexer implements IndexerInterface {
export class Indexer implements IPLDIndexerInterface {
_db: Database
_ethClient: EthClient
_ethProvider: BaseProvider
@ -899,11 +900,15 @@ export class Indexer implements IndexerInterface {
}
async watchContract (address: string, kind: string, checkpoint: boolean, startingBlock: number): Promise<void> {
this._baseIndexer.updateIPLDStatusMap(address, {});
await this.updateIPLDStatusMap(address, {});
return this._baseIndexer.watchContract(address, kind, checkpoint, startingBlock);
}
async updateIPLDStatusMap (address: string, ipldStatus: IpldStatusInterface): Promise<void> {
await this._baseIndexer.updateIPLDStatusMap(address, ipldStatus);
}
cacheContract (contract: Contract): void {
return this._baseIndexer.cacheContract(contract);
}

View File

@ -25,7 +25,7 @@ task('token-approve', 'Move tokens to recipient')
const TransferEvent = receipt.events.find(el => el.event === 'Approval');
if (TransferEvent && TransferEvent.args) {
console.log('Approval Event');
console.log('Approval Event at block:', receipt.blockNumber, receipt.blockHash);
console.log('owner:', TransferEvent.args.owner.toString());
console.log('spender:', TransferEvent.args.spender.toString());
console.log('value:', TransferEvent.args.value.toString());

View File

@ -15,5 +15,7 @@ task('token-deploy', 'Deploys GLD token')
const Token = await hre.ethers.getContractFactory('GLDToken');
const token = await Token.deploy(hre.ethers.BigNumber.from(initialSupply));
const receipt = await token.deployTransaction.wait();
console.log('GLD Token deployed to:', token.address);
console.log('Deployed at block:', receipt.blockNumber, receipt.blockHash);
});

View File

@ -28,7 +28,7 @@ task('token-transfer-from', 'Send tokens as spender')
const TransferEvent = receipt.events.find(el => el.event === 'Transfer');
if (TransferEvent && TransferEvent.args) {
console.log('Transfer Event');
console.log('Transfer Event at block:', receipt.blockNumber, receipt.blockHash);
console.log('from:', TransferEvent.args.from.toString());
console.log('to:', TransferEvent.args.to.toString());
console.log('value:', TransferEvent.args.value.toString());

View File

@ -24,7 +24,7 @@ task('token-transfer', 'Move tokens to recipient')
const TransferEvent = receipt.events.find(el => el.event === 'Transfer');
if (TransferEvent && TransferEvent.args) {
console.log('Transfer Event');
console.log('Transfer Event at block:', receipt.blockNumber, receipt.blockHash);
console.log('from:', TransferEvent.args.from.toString());
console.log('to:', TransferEvent.args.to.toString());
console.log('value:', TransferEvent.args.value.toString());

View File

@ -15,7 +15,6 @@ import { EthClient } from '@vulcanize/ipld-eth-client';
import { StorageLayout } from '@vulcanize/solidity-mapper';
import {
IPLDIndexer as BaseIndexer,
IndexerInterface,
ValueResult,
UNKNOWN_EVENT_NAME,
ServerConfig,
@ -25,7 +24,9 @@ import {
QueryOptions,
BlockHeight,
IPFSClient,
StateKind
StateKind,
IPLDIndexerInterface,
IpldStatus as IpldStatusInterface
} from '@vulcanize/util';
import { GraphWatcher } from '@vulcanize/graph-node';
@ -92,7 +93,7 @@ export type ResultIPLDBlock = {
data: string;
};
export class Indexer implements IndexerInterface {
export class Indexer implements IPLDIndexerInterface {
_db: Database
_ethClient: EthClient
_ethProvider: BaseProvider
@ -592,11 +593,15 @@ export class Indexer implements IndexerInterface {
}
async watchContract (address: string, kind: string, checkpoint: boolean, startingBlock: number): Promise<void> {
this._baseIndexer.updateIPLDStatusMap(address, {});
await this.updateIPLDStatusMap(address, {});
return this._baseIndexer.watchContract(address, kind, checkpoint, startingBlock);
}
async updateIPLDStatusMap (address: string, ipldStatus: IpldStatusInterface): Promise<void> {
await this._baseIndexer.updateIPLDStatusMap(address, ipldStatus);
}
cacheContract (contract: Contract): void {
return this._baseIndexer.cacheContract(contract);
}

View File

@ -18,7 +18,7 @@ import {
QUEUE_EVENT_PROCESSING
} from './constants';
import { JobQueue } from './job-queue';
import { EventInterface, IndexerInterface, SyncStatusInterface } from './types';
import { EventInterface, IndexerInterface, IPLDIndexerInterface, SyncStatusInterface } from './types';
import { wait } from './misc';
import { createPruningJob } from './common';
import { OrderDirection } from './database';
@ -28,7 +28,7 @@ const DEFAULT_EVENTS_IN_BATCH = 50;
const log = debug('vulcanize:job-runner');
export class JobRunner {
_indexer: IndexerInterface
_indexer: IndexerInterface | IPLDIndexerInterface
_jobQueue: JobQueue
_jobQueueConfig: JobQueueConfig
_blockProcessStartTime?: Date
@ -331,5 +331,10 @@ export class JobRunner {
assert(this._indexer.cacheContract);
this._indexer.cacheContract(contract);
const ipldIndexer = this._indexer as IPLDIndexerInterface;
if (ipldIndexer.updateIPLDStatusMap) {
ipldIndexer.updateIPLDStatusMap(contract.address, {});
}
}
}

View File

@ -5,6 +5,7 @@
import { Connection, DeepPartial, FindConditions, FindManyOptions, QueryRunner } from 'typeorm';
import { Where, QueryOptions } from './database';
import { IpldStatus } from './ipld-indexer';
export enum StateKind {
Diff = 'diff',
@ -104,6 +105,10 @@ export interface IndexerInterface {
processBlock?: (blockHash: string, blockNumber: number) => Promise<void>
}
export interface IPLDIndexerInterface extends IndexerInterface {
updateIPLDStatusMap (address: string, ipldStatus: IpldStatus): Promise<void>
}
export interface EventWatcherInterface {
getBlockProgressEventIterator (): AsyncIterator<any>
initBlockProcessingOnCompleteHandler (): Promise<void>