Skip to content

Commit

Permalink
Update indexer template in codegen for block optimization changes (#464)
Browse files Browse the repository at this point in the history
* Update indexer template for block optimization

* Trim address strings in graph-node host API
  • Loading branch information
nikugogoi authored Nov 10, 2023
1 parent 32d5cd1 commit edf72c1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
36 changes: 26 additions & 10 deletions packages/codegen/src/templates/indexer-template.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ import {
Clients,
EthClient,
UpstreamConfig,
ResultMeta
ResultMeta,
EthFullBlock,
EthFullTransaction,
ExtraEventData
} from '@cerc-io/util';
{{#if (subgraphPath)}}
import { GraphWatcher } from '@cerc-io/graph-node';
Expand Down Expand Up @@ -464,23 +467,23 @@ export class Indexer implements IndexerInterface {
}

{{/if}}
async triggerIndexingOnEvent (event: Event): Promise<void> {
async triggerIndexingOnEvent (event: Event, extraData: ExtraEventData): Promise<void> {
const resultEvent = this.getResultEvent(event);

{{#if (subgraphPath)}}
console.time('time:indexer#processEvent-mapping_code');
// Call subgraph handler for event.
await this._graphWatcher.handleEvent(resultEvent);
await this._graphWatcher.handleEvent(resultEvent, extraData);
console.timeEnd('time:indexer#processEvent-mapping_code');

{{/if}}
// Call custom hook function for indexing on event.
await handleEvent(this, resultEvent);
}

async processEvent (event: Event): Promise<void> {
async processEvent (event: Event, extraData: ExtraEventData): Promise<void> {
// Trigger indexing of data based on the event.
await this.triggerIndexingOnEvent(event);
await this.triggerIndexingOnEvent(event, extraData);
}

async processBlock (blockProgress: BlockProgress): Promise<void> {
Expand Down Expand Up @@ -689,15 +692,24 @@ export class Indexer implements IndexerInterface {
return this._baseIndexer.fetchEventsAndSaveBlocks(blocks, this._eventSignaturesMap, this.parseEventNameAndArgs.bind(this));
}

async fetchAndSaveFilteredEventsAndBlocks (startBlock: number, endBlock: number): Promise<{ blockProgress: BlockProgress, events: DeepPartial<Event>[] }[]> {
async fetchAndSaveFilteredEventsAndBlocks (startBlock: number, endBlock: number): Promise<{
blockProgress: BlockProgress,
events: DeepPartial<Event>[],
ethFullBlock: EthFullBlock;
ethFullTransactions: EthFullTransaction[];
}[]> {
return this._baseIndexer.fetchAndSaveFilteredEventsAndBlocks(startBlock, endBlock, this._eventSignaturesMap, this.parseEventNameAndArgs.bind(this));
}

async fetchEventsForContracts (blockHash: string, blockNumber: number, addresses: string[]): Promise<DeepPartial<Event>[]> {
return this._baseIndexer.fetchEventsForContracts(blockHash, blockNumber, addresses, this._eventSignaturesMap, this.parseEventNameAndArgs.bind(this));
}

async saveBlockAndFetchEvents (block: DeepPartial<BlockProgress>): Promise<[BlockProgress, DeepPartial<Event>[]]> {
async saveBlockAndFetchEvents (block: DeepPartial<BlockProgress>): Promise<[
BlockProgress,
DeepPartial<Event>[],
EthFullTransaction[]
]> {
return this._saveBlockAndFetchEvents(block);
}

Expand Down Expand Up @@ -810,11 +822,15 @@ export class Indexer implements IndexerInterface {
blockNumber,
blockTimestamp,
parentHash
}: DeepPartial<BlockProgress>): Promise<[BlockProgress, DeepPartial<Event>[]]> {
}: DeepPartial<BlockProgress>): Promise<[
BlockProgress,
DeepPartial<Event>[],
EthFullTransaction[]
]> {
assert(blockHash);
assert(blockNumber);

const dbEvents = await this._baseIndexer.fetchEvents(blockHash, blockNumber, this._eventSignaturesMap, this.parseEventNameAndArgs.bind(this));
const { events: dbEvents, transactions } = await this._baseIndexer.fetchEvents(blockHash, blockNumber, this._eventSignaturesMap, this.parseEventNameAndArgs.bind(this));

const dbTx = await this._db.createTransactionRunner();
try {
Expand All @@ -831,7 +847,7 @@ export class Indexer implements IndexerInterface {
await dbTx.commitTransaction();
console.timeEnd(`time:indexer#_saveBlockAndFetchEvents-db-save-${blockNumber}`);

return [blockProgress, []];
return [blockProgress, [], transactions];
} catch (error) {
await dbTx.rollbackTransaction();
throw error;
Expand Down
5 changes: 2 additions & 3 deletions packages/graph-node/src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,7 @@ export const instantiate = async (
const functionParamsPtr = await smartContractCall.functionParams;
let functionParams = __getArray(functionParamsPtr);

console.log('ethereum.call params');
console.log('functionSignature:', functionSignature);
console.log('ethereum.call functionSignature:', functionSignature);

const abi = abis[contractName];
const contractAddressStringPtr = await contractAddress.toHexString();
Expand Down Expand Up @@ -284,7 +283,7 @@ export const instantiate = async (
conversion: {
'typeConversion.stringToH160': async (s: number) => {
const string = __getString(s);
const address = utils.getAddress(string);
const address = utils.getAddress(string.trim());
const byteArray = utils.arrayify(address);

const uint8ArrayId = await getIdOfType(TypeId.Uint8Array);
Expand Down

0 comments on commit edf72c1

Please sign in to comment.