From 2aa0234da5e4da7a94e6187075ba62fd3377c8ad Mon Sep 17 00:00:00 2001 From: prathamesh0 <42446521+prathamesh0@users.noreply.github.com> Date: Mon, 18 Oct 2021 14:50:41 +0530 Subject: [PATCH] Change initial checkpoint hook and hook status entity naming (#269) --- packages/codegen/README.md | 4 ++-- .../{HooksStatus.yaml => HookStatus.yaml} | 2 +- packages/codegen/src/database.ts | 14 ++++++++------ packages/codegen/src/entity.ts | 6 +++--- packages/codegen/src/indexer.ts | 10 ++++++---- .../src/templates/database-template.handlebars | 10 +++++----- .../src/templates/events-template.handlebars | 2 +- .../src/templates/hooks-template.handlebars | 16 ++++++++-------- .../src/templates/indexer-template.handlebars | 16 ++++++++-------- .../src/templates/job-runner-template.handlebars | 4 ++-- .../src/templates/readme-template.handlebars | 4 ++-- 11 files changed, 46 insertions(+), 42 deletions(-) rename packages/codegen/src/data/entities/{HooksStatus.yaml => HookStatus.yaml} (90%) diff --git a/packages/codegen/README.md b/packages/codegen/README.md index fe167e66..a5f1cc57 100644 --- a/packages/codegen/README.md +++ b/packages/codegen/README.md @@ -81,9 +81,9 @@ * Edit the custom hook function `handleEvent` (triggered on an event) in `src/hooks.ts` to perform corresponding indexing using the `Indexer` object. - * Edit the custom hook function `handleBlock` (triggered on a block) in `src/hooks.ts` to save `IPLDBlock`s using the `Indexer` object. + * Edit the custom hook function `postBlockHook` (triggered on a block) in `src/hooks.ts` to save `IPLDBlock`s using the `Indexer` object. - * Edit the custom hook function `genesisHook` (triggered on watch-contract) in `src/hooks.ts` to save a genesis checkpoint `IPLDBlock` using the `Indexer` object. + * Edit the custom hook function `initialCheckpointHook` (triggered on watch-contract) in `src/hooks.ts` to save an initial checkpoint `IPLDBlock` using the `Indexer` object. * The existing example hooks in `src/hooks.ts` are for an `ERC20` contract. diff --git a/packages/codegen/src/data/entities/HooksStatus.yaml b/packages/codegen/src/data/entities/HookStatus.yaml similarity index 90% rename from packages/codegen/src/data/entities/HooksStatus.yaml rename to packages/codegen/src/data/entities/HookStatus.yaml index 1e7461ec..222e7145 100644 --- a/packages/codegen/src/data/entities/HooksStatus.yaml +++ b/packages/codegen/src/data/entities/HookStatus.yaml @@ -1,4 +1,4 @@ -className: HooksStatus +className: HookStatus indexOn: [] columns: - name: latestProcessedBlockNumber diff --git a/packages/codegen/src/database.ts b/packages/codegen/src/database.ts index a416dc83..e0c291d4 100644 --- a/packages/codegen/src/database.ts +++ b/packages/codegen/src/database.ts @@ -47,13 +47,15 @@ export class Database { // eth_call mode: Capitalize first letter of entity name (balanceOf -> BalanceOf, getBalanceOf, saveBalanceOf). // storage mode: Capiltalize second letter of entity name (_balances -> _Balances, _getBalances, _saveBalances). if (name.charAt(0) === '_') { - queryObject.entityName = `_${name.charAt(1).toUpperCase()}${name.slice(2)}`; - queryObject.getQueryName = `_get${name.charAt(1).toUpperCase()}${name.slice(2)}`; - queryObject.saveQueryName = `_save${name.charAt(1).toUpperCase()}${name.slice(2)}`; + const capitalizedName = `${name.charAt(1).toUpperCase()}${name.slice(2)}`; + queryObject.entityName = `_${capitalizedName}`; + queryObject.getQueryName = `_get${capitalizedName}`; + queryObject.saveQueryName = `_save${capitalizedName}`; } else { - queryObject.entityName = `${name.charAt(0).toUpperCase()}${name.slice(1)}`; - queryObject.getQueryName = `get${name.charAt(0).toUpperCase()}${name.slice(1)}`; - queryObject.saveQueryName = `save${name.charAt(0).toUpperCase()}${name.slice(1)}`; + const capitalizedName = `${name.charAt(0).toUpperCase()}${name.slice(1)}`; + queryObject.entityName = capitalizedName; + queryObject.getQueryName = `get${capitalizedName}`; + queryObject.saveQueryName = `save${capitalizedName}`; } queryObject.params = queryObject.params.map((param) => { diff --git a/packages/codegen/src/entity.ts b/packages/codegen/src/entity.ts index 5942e55c..d2f60e53 100644 --- a/packages/codegen/src/entity.ts +++ b/packages/codegen/src/entity.ts @@ -188,7 +188,7 @@ export class Entity { this._addContractEntity(); this._addBlockProgressEntity(); this._addIPLDBlockEntity(); - this._addHooksStatusEntity(); + this._addHookStatusEntity(); const template = Handlebars.compile(this._templateString); this._entities.forEach(entityObj => { @@ -225,8 +225,8 @@ export class Entity { this._entities.push(entity); } - _addHooksStatusEntity (): void { - const entity = yaml.load(fs.readFileSync(path.resolve(__dirname, TABLES_DIR, 'HooksStatus.yaml'), 'utf8')); + _addHookStatusEntity (): void { + const entity = yaml.load(fs.readFileSync(path.resolve(__dirname, TABLES_DIR, 'HookStatus.yaml'), 'utf8')); this._entities.push(entity); } } diff --git a/packages/codegen/src/indexer.ts b/packages/codegen/src/indexer.ts index 58a2f83d..329ad66a 100644 --- a/packages/codegen/src/indexer.ts +++ b/packages/codegen/src/indexer.ts @@ -49,11 +49,13 @@ export class Indexer { }; if (name.charAt(0) === '_') { - queryObject.getQueryName = `_get${name.charAt(1).toUpperCase()}${name.slice(2)}`; - queryObject.saveQueryName = `_save${name.charAt(1).toUpperCase()}${name.slice(2)}`; + const capitalizedName = `${name.charAt(1).toUpperCase()}${name.slice(2)}`; + queryObject.getQueryName = `_get${capitalizedName}`; + queryObject.saveQueryName = `_save${capitalizedName}`; } else { - queryObject.getQueryName = `get${name.charAt(0).toUpperCase()}${name.slice(1)}`; - queryObject.saveQueryName = `save${name.charAt(0).toUpperCase()}${name.slice(1)}`; + const capitalizedName = `${name.charAt(0).toUpperCase()}${name.slice(1)}`; + queryObject.getQueryName = `get${capitalizedName}`; + queryObject.saveQueryName = `save${capitalizedName}`; } queryObject.params = queryObject.params.map((param) => { diff --git a/packages/codegen/src/templates/database-template.handlebars b/packages/codegen/src/templates/database-template.handlebars index b0543ab4..e0e6d6b2 100644 --- a/packages/codegen/src/templates/database-template.handlebars +++ b/packages/codegen/src/templates/database-template.handlebars @@ -11,7 +11,7 @@ import { Database as BaseDatabase, QueryOptions, Where, MAX_REORG_DEPTH } from ' import { Contract } from './entity/Contract'; import { Event } from './entity/Event'; import { SyncStatus } from './entity/SyncStatus'; -import { HooksStatus } from './entity/HooksStatus'; +import { HookStatus } from './entity/HookStatus'; import { BlockProgress } from './entity/BlockProgress'; import { IPLDBlock } from './entity/IPLDBlock'; @@ -240,14 +240,14 @@ export class Database { return repo.save(ipldBlock); } - async getHooksStatus (queryRunner: QueryRunner): Promise { - const repo = queryRunner.manager.getRepository(HooksStatus); + async getHookStatus (queryRunner: QueryRunner): Promise { + const repo = queryRunner.manager.getRepository(HookStatus); return repo.findOne(); } - async updateHooksStatusProcessedBlock (queryRunner: QueryRunner, blockNumber: number): Promise { - const repo = queryRunner.manager.getRepository(HooksStatus); + async updateHookStatusProcessedBlock (queryRunner: QueryRunner, blockNumber: number): Promise { + const repo = queryRunner.manager.getRepository(HookStatus); let entity = await repo.findOne(); if (!entity) { diff --git a/packages/codegen/src/templates/events-template.handlebars b/packages/codegen/src/templates/events-template.handlebars index 9d23dcee..cae2ea10 100644 --- a/packages/codegen/src/templates/events-template.handlebars +++ b/packages/codegen/src/templates/events-template.handlebars @@ -122,7 +122,7 @@ export class EventWatcher { this._jobQueue.onComplete(QUEUE_HOOKS, async (job) => { const { data: { request: { data: { blockHash, blockNumber } } } } = job; - await this._indexer.updateHooksStatusProcessedBlock(blockNumber); + await this._indexer.updateHookStatusProcessedBlock(blockNumber); // Push checkpointing job only after post-block hook job is marked complete and checkpointing is on. if (this._indexer._serverConfig.checkpointing) { diff --git a/packages/codegen/src/templates/hooks-template.handlebars b/packages/codegen/src/templates/hooks-template.handlebars index aad06dda..132f8e34 100644 --- a/packages/codegen/src/templates/hooks-template.handlebars +++ b/packages/codegen/src/templates/hooks-template.handlebars @@ -15,13 +15,13 @@ const ACCOUNTS = [ ]; /** - * Genesis hook function. + * Initial checkpoint hook function. * @param indexer Indexer instance. * @param block Concerned block. * @param contractAddress Address of the concerned contract. */ -export async function genesisHook (indexer: Indexer, block: BlockProgress, contractAddress: string): Promise { - // Store the genesis state values in an IPLDBlock. +export async function initialCheckpointHook (indexer: Indexer, block: BlockProgress, contractAddress: string): Promise { + // Store the initial state values in an IPLDBlock. const ipldBlockData: any = {}; // Setting the initial balances of accounts. @@ -120,13 +120,13 @@ export async function handleEvent (indexer: Indexer, eventData: ResultEvent): Pr // Therefore, trigger indexing for both sender and receiver. // Get event fields from eventData. - // const { from, to } = eventData.event; + const { from, to } = eventData.event; // Update balance entry for sender in the database. - // await indexer.balanceOf(eventData.block.hash, eventData.contract, from); + await indexer.balanceOf(eventData.block.hash, eventData.contract, from); // Update balance entry for receiver in the database. - // await indexer.balanceOf(eventData.block.hash, eventData.contract, to); + await indexer.balanceOf(eventData.block.hash, eventData.contract, to); break; } @@ -135,10 +135,10 @@ export async function handleEvent (indexer: Indexer, eventData: ResultEvent): Pr // On an approval, allowance for (owner, spender) combination changes. // Get event fields from eventData. - // const { owner, spender } = eventData.event; + const { owner, spender } = eventData.event; // Update allowance entry for (owner, spender) combination in the database. - // await indexer.allowance(eventData.block.hash, eventData.contract, owner, spender); + await indexer.allowance(eventData.block.hash, eventData.contract, owner, spender); break; } diff --git a/packages/codegen/src/templates/indexer-template.handlebars b/packages/codegen/src/templates/indexer-template.handlebars index 533968f5..c8d89a1b 100644 --- a/packages/codegen/src/templates/indexer-template.handlebars +++ b/packages/codegen/src/templates/indexer-template.handlebars @@ -22,11 +22,11 @@ import { Database } from './database'; import { Contract } from './entity/Contract'; import { Event } from './entity/Event'; import { SyncStatus } from './entity/SyncStatus'; -import { HooksStatus } from './entity/HooksStatus'; +import { HookStatus } from './entity/HookStatus'; import { BlockProgress } from './entity/BlockProgress'; import { IPLDBlock } from './entity/IPLDBlock'; import artifacts from './artifacts/{{inputFileName}}.json'; -import { genesisHook, handleEvent, postBlockHook } from './hooks'; +import { initialCheckpointHook, handleEvent, postBlockHook } from './hooks'; const log = debug('vulcanize:indexer'); @@ -449,18 +449,18 @@ export class Indexer { const currentBlock = await this._db.getLatestBlockProgress(); assert(currentBlock); - // Call custom genesis hook. - await genesisHook(this, currentBlock, address); + // Call custom initial checkpoint hook. + await initialCheckpointHook(this, currentBlock, address); return true; } - async getHooksStatus (): Promise { + async getHookStatus (): Promise { const dbTx = await this._db.createTransactionRunner(); let res; try { - res = await this._db.getHooksStatus(dbTx); + res = await this._db.getHookStatus(dbTx); await dbTx.commitTransaction(); } catch (error) { await dbTx.rollbackTransaction(); @@ -472,12 +472,12 @@ export class Indexer { return res; } - async updateHooksStatusProcessedBlock (blockNumber: number): Promise { + async updateHookStatusProcessedBlock (blockNumber: number): Promise { const dbTx = await this._db.createTransactionRunner(); let res; try { - res = await this._db.updateHooksStatusProcessedBlock(dbTx, blockNumber); + res = await this._db.updateHookStatusProcessedBlock(dbTx, blockNumber); await dbTx.commitTransaction(); } catch (error) { await dbTx.rollbackTransaction(); diff --git a/packages/codegen/src/templates/job-runner-template.handlebars b/packages/codegen/src/templates/job-runner-template.handlebars index 5fff8cc4..44e460fe 100644 --- a/packages/codegen/src/templates/job-runner-template.handlebars +++ b/packages/codegen/src/templates/job-runner-template.handlebars @@ -76,9 +76,9 @@ export class JobRunner { await this._jobQueue.subscribe(QUEUE_HOOKS, async (job) => { const { data: { blockNumber } } = job; - const hooksStatus = await this._indexer.getHooksStatus(); + const hookStatus = await this._indexer.getHookStatus(); - if (hooksStatus && hooksStatus.latestProcessedBlockNumber !== blockNumber - 1) { + if (hookStatus && hookStatus.latestProcessedBlockNumber !== blockNumber - 1) { const message = `Hooks for blockNumber ${blockNumber - 1} not processed yet, aborting`; log(message); diff --git a/packages/codegen/src/templates/readme-template.handlebars b/packages/codegen/src/templates/readme-template.handlebars index 2880faff..6b214eb6 100644 --- a/packages/codegen/src/templates/readme-template.handlebars +++ b/packages/codegen/src/templates/readme-template.handlebars @@ -47,9 +47,9 @@ * Edit the custom hook function `handleEvent` (triggered on an event) in [hooks.ts](./src/hooks.ts) to perform corresponding indexing using the `Indexer` object. - * Edit the custom hook function `handleBlock` (triggered on a block) in [hooks.ts](./src/hooks.ts) to save `IPLDBlock`s using the `Indexer` object. + * Edit the custom hook function `postBlockHook` (triggered on a block) in [hooks.ts](./src/hooks.ts) to save `IPLDBlock`s using the `Indexer` object. - * Edit the custom hook function `genesisHook` (triggered on watch-contract) in [hooks.ts](./src/hooks.ts) to save a genesis checkpoint `IPLDBlock` using the `Indexer` object. + * Edit the custom hook function `initialCheckpointHook` (triggered on watch-contract) in [hooks.ts](./src/hooks.ts) to save an initial checkpoint `IPLDBlock` using the `Indexer` object. * The existing example hooks in [hooks.ts](./src/hooks.ts) are for an `ERC20` contract.