Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix hooks and entity naming #269

Merged
merged 1 commit into from
Oct 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/codegen/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
className: HooksStatus
className: HookStatus
indexOn: []
columns:
- name: latestProcessedBlockNumber
Expand Down
14 changes: 8 additions & 6 deletions packages/codegen/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
6 changes: 3 additions & 3 deletions packages/codegen/src/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down Expand Up @@ -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);
}
}
10 changes: 6 additions & 4 deletions packages/codegen/src/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
10 changes: 5 additions & 5 deletions packages/codegen/src/templates/database-template.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { Database as BaseDatabase, MAX_REORG_DEPTH } from '@vulcanize/util';
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';

Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion packages/codegen/src/templates/events-template.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,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) {
Expand Down
16 changes: 8 additions & 8 deletions packages/codegen/src/templates/hooks-template.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
16 changes: 8 additions & 8 deletions packages/codegen/src/templates/indexer-template.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -453,18 +453,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();
Expand All @@ -476,12 +476,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();
Expand Down
4 changes: 2 additions & 2 deletions packages/codegen/src/templates/job-runner-template.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,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);

Expand Down
4 changes: 2 additions & 2 deletions packages/codegen/src/templates/readme-template.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down