Change initial checkpoint hook and hook status entity naming (#269)

This commit is contained in:
prathamesh0 2021-10-18 14:50:41 +05:30 committed by nabarun
parent 421e7498d3
commit 2aa0234da5
11 changed files with 46 additions and 42 deletions

View File

@ -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.

View File

@ -1,4 +1,4 @@
className: HooksStatus
className: HookStatus
indexOn: []
columns:
- name: latestProcessedBlockNumber

View File

@ -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) => {

View File

@ -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);
}
}

View File

@ -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) => {

View File

@ -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<HooksStatus | undefined> {
const repo = queryRunner.manager.getRepository(HooksStatus);
async getHookStatus (queryRunner: QueryRunner): Promise<HookStatus | undefined> {
const repo = queryRunner.manager.getRepository(HookStatus);
return repo.findOne();
}
async updateHooksStatusProcessedBlock (queryRunner: QueryRunner, blockNumber: number): Promise<HooksStatus> {
const repo = queryRunner.manager.getRepository(HooksStatus);
async updateHookStatusProcessedBlock (queryRunner: QueryRunner, blockNumber: number): Promise<HookStatus> {
const repo = queryRunner.manager.getRepository(HookStatus);
let entity = await repo.findOne();
if (!entity) {

View File

@ -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) {

View File

@ -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<void> {
// Store the genesis state values in an IPLDBlock.
export async function initialCheckpointHook (indexer: Indexer, block: BlockProgress, contractAddress: string): Promise<void> {
// 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;
}

View File

@ -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<HooksStatus | undefined> {
async getHookStatus (): Promise<HookStatus | undefined> {
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<HooksStatus> {
async updateHookStatusProcessedBlock (blockNumber: number): Promise<HookStatus> {
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();

View File

@ -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);

View File

@ -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.