Skip to content

Commit

Permalink
Pass extra block data required in subgraph block handler (#486)
Browse files Browse the repository at this point in the history
* Use codegen config directory name instead of file path

* Get block data from extra event data

* Add extra event data in processBlockAfterEvents

---------

Co-authored-by: neeraj <neeraj.rtly@gmail.com>
  • Loading branch information
nikugogoi and neerajvijay1997 authored Nov 22, 2023
1 parent 10c16cd commit 29e4e9f
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 13 deletions.
4 changes: 2 additions & 2 deletions packages/codegen/src/templates/indexer-template.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -499,10 +499,10 @@ export class Indexer implements IndexerInterface {
}

{{#if (subgraphPath)}}
async processBlockAfterEvents (blockHash: string, blockNumber: number): Promise<void> {
async processBlockAfterEvents (blockHash: string, blockNumber: number, extraData: ExtraEventData): Promise<void> {
console.time('time:indexer#processBlockAfterEvents-mapping_code');
// Call subgraph handler for block.
await this._graphWatcher.handleBlock(blockHash, blockNumber);
await this._graphWatcher.handleBlock(blockHash, blockNumber, extraData);
console.timeEnd('time:indexer#processBlockAfterEvents-mapping_code');

console.time('time:indexer#processBlockAfterEvents-dump_subgraph_state');
Expand Down
7 changes: 4 additions & 3 deletions packages/codegen/src/utils/subgraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ export async function buildSubgraph (
network?: string
}
): Promise<void> {
const subgraphDirectory = path.resolve(codegenConfigPath, subgraphConfig.directory);
const codegenConfigDirName = path.dirname(codegenConfigPath);
const subgraphDirectory = path.resolve(codegenConfigDirName, subgraphConfig.directory);
const codegenWorkingDir = process.cwd();
// Change directory to subgraph repo
shell.cd(subgraphDirectory);
Expand Down Expand Up @@ -102,7 +103,7 @@ export async function buildSubgraph (
const { code: installCode } = shell.exec(`${packageManager} install --force`);
assert(installCode === 0, 'Installing dependencies exited with error');

const subgraphConfigPath = path.resolve(codegenConfigPath, subgraphConfig.configFile);
const subgraphConfigPath = path.resolve(codegenConfigDirName, subgraphConfig.configFile);

// Run graph-cli codegen
const { code: codegenCode } = shell.exec(`${packageManager === 'npm' ? 'npx' : packageManager} graph codegen ${subgraphConfigPath}`);
Expand All @@ -112,7 +113,7 @@ export async function buildSubgraph (
let buildCommand = `${packageManager === 'npm' ? 'npx' : packageManager} graph build ${subgraphConfigPath}`;

if (subgraphConfig.networkFilePath) {
const subgraphNetworkFilePath = path.resolve(codegenConfigPath, subgraphConfig.networkFilePath);
const subgraphNetworkFilePath = path.resolve(codegenConfigDirName, subgraphConfig.networkFilePath);
assert(subgraphConfig.network, 'Config subgraph.network should be set if using networkFilePath');
const subgraphNetwork = subgraphConfig.network;
buildCommand = `${buildCommand} --network-file ${subgraphNetworkFilePath} --network ${subgraphNetwork}`;
Expand Down
2 changes: 2 additions & 0 deletions packages/codegen/subgraph-demo.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@
yarn && yarn build
```

* Configure the upstream GQL and RPC endpoints

* Run the job-runner:

```bash
Expand Down
11 changes: 5 additions & 6 deletions packages/graph-node/src/watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ export class GraphWatcher {
}
}

async handleBlock (blockHash: string, blockNumber: number) {
async handleBlock (blockHash: string, blockNumber: number, extraData: ExtraEventData) {
// Clear transactions map on handling new block.
this._transactionsMap.clear();

Expand All @@ -258,11 +258,10 @@ export class GraphWatcher {
continue;
}

// TODO: Use extraData full block
// // Check if block data is already fetched in handleEvent method for the same block.
// if (!this._context.block || this._context.block.blockHash !== blockHash) {
// this._context.block = await getFullBlock(this._ethClient, this._ethProvider, blockHash, blockNumber);
// }
// Check if block data is already fetched in handleEvent method for the same block.
if (!this._context.block || this._context.block.blockHash !== blockHash) {
this._context.block = await getFullBlock(extraData.ethFullBlock);
}

const blockData = this._context.block;
assert(blockData);
Expand Down
2 changes: 1 addition & 1 deletion packages/util/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ export const processBatchEvents = async (

if (indexer.processBlockAfterEvents) {
if (!dbBlock.isComplete) {
await indexer.processBlockAfterEvents(dbBlock.blockHash, dbBlock.blockNumber);
await indexer.processBlockAfterEvents(dbBlock.blockHash, dbBlock.blockNumber, data);
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/util/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ export interface IndexerInterface {
processInitialState: (contractAddress: string, blockHash: string) => Promise<any>
processStateCheckpoint: (contractAddress: string, blockHash: string) => Promise<boolean>
processBlock: (blockProgres: BlockProgressInterface) => Promise<void>
processBlockAfterEvents?: (blockHash: string, blockNumber: number) => Promise<void>
processBlockAfterEvents?: (blockHash: string, blockNumber: number, data: ExtraEventData) => Promise<void>
processCanonicalBlock (blockHash: string, blockNumber: number): Promise<void>
processCheckpoint (blockHash: string): Promise<void>
processCLICheckpoint (contractAddress: string, blockHash?: string): Promise<string | undefined>
Expand Down

0 comments on commit 29e4e9f

Please sign in to comment.