Handle template create in subgraph watcher during reorgs (#533)

* Handle template create events processing order during reorgs

* Add removeWatcher method in graph-node dummy indexer for test

* Apply GQL and RPC server middlewares ordered on requested paths

* Increment package version

* Remove console logs

---------

Co-authored-by: Prathamesh Musale <prathamesh.musale0@gmail.com>
This commit is contained in:
2024-10-11 14:50:52 +05:30
committed by GitHub
co-authored by prathamesh
parent a585500012
commit 5d7b7fe5b4
19 changed files with 126 additions and 59 deletions
+5 -5
View File
@@ -1,10 +1,10 @@
{
"name": "@cerc-io/graph-node",
"version": "0.2.107",
"version": "0.2.108",
"main": "dist/index.js",
"license": "AGPL-3.0",
"devDependencies": {
"@cerc-io/solidity-mapper": "^0.2.107",
"@cerc-io/solidity-mapper": "^0.2.108",
"@ethersproject/providers": "^5.4.4",
"@graphprotocol/graph-ts": "^0.22.0",
"@nomiclabs/hardhat-ethers": "^2.0.2",
@@ -51,9 +51,9 @@
"dependencies": {
"@apollo/client": "^3.3.19",
"@cerc-io/assemblyscript": "0.19.10-watcher-ts-0.1.2",
"@cerc-io/cache": "^0.2.107",
"@cerc-io/ipld-eth-client": "^0.2.107",
"@cerc-io/util": "^0.2.107",
"@cerc-io/cache": "^0.2.108",
"@cerc-io/ipld-eth-client": "^0.2.108",
"@cerc-io/util": "^0.2.108",
"@types/json-diff": "^0.5.2",
"@types/yargs": "^17.0.0",
"bn.js": "^4.11.9",
+30 -14
View File
@@ -734,25 +734,13 @@ export const instantiate = async (
return __newString(dataSource.network);
},
'dataSource.create': async (name: number, params: number) => {
const [addressStringPtr] = __getArray(params);
const addressString = __getString(addressStringPtr);
const contractKind = __getString(name);
assert(indexer.watchContract);
assert(context.block);
await indexer.watchContract(utils.getAddress(addressString), contractKind, true, Number(context.block.blockNumber));
await handleDataSourceCreate(name, params);
},
'dataSource.createWithContext': async (name: number, params: number, dataSourceContext: number) => {
const [addressStringPtr] = __getArray(params);
const addressString = __getString(addressStringPtr);
const contractKind = __getString(name);
const contextInstance = await Entity.wrap(dataSourceContext);
const dbData = await database.fromGraphContext(instanceExports, contextInstance);
assert(indexer.watchContract);
assert(context.block);
await indexer.watchContract(utils.getAddress(addressString), contractKind, true, Number(context.block.blockNumber), dbData);
await handleDataSourceCreate(name, params, dbData);
}
},
json: {
@@ -786,6 +774,34 @@ export const instantiate = async (
}
};
const handleDataSourceCreate = async (name: number, params: number, dbData?: {[key: string]: any}) => {
const [addressStringPtr] = __getArray(params);
const addressString = __getString(addressStringPtr);
const contractKind = __getString(name);
assert(context.block);
const contractAddress = utils.getAddress(addressString);
const watchedContracts = indexer.isContractAddressWatched(contractAddress);
// If template contract is already watched (incase of reorgs)
// Remove from watched contracts and throw error to reprocess block with correct order of template contract events
if (
watchedContracts &&
watchedContracts.some(watchedContract => watchedContract.kind === contractKind)
) {
await indexer.removeContract(contractAddress, contractKind);
throw new Error(`Template contract ${contractAddress} of kind ${contractKind} already exists; removed from watched contracts`);
}
await indexer.watchContract(
contractAddress,
contractKind,
true,
Number(context.block.blockNumber),
dbData
);
};
const instance = await loader.instantiate(source, imports);
const { exports: instanceExports } = instance;
@@ -276,6 +276,10 @@ export class Indexer implements IndexerInterface {
return undefined;
}
async removeContract (address: string, kind: string): Promise<void> {
return undefined;
}
async processBlock (blockProgress: BlockProgressInterface): Promise<void> {
return undefined;
}