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:
Nabarun Gogoi 2024-10-11 14:50:52 +05:30 committed by GitHub
parent a585500012
commit 5d7b7fe5b4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
19 changed files with 126 additions and 59 deletions

View File

@ -2,7 +2,7 @@
"packages": [ "packages": [
"packages/*" "packages/*"
], ],
"version": "0.2.107", "version": "0.2.108",
"npmClient": "yarn", "npmClient": "yarn",
"useWorkspaces": true, "useWorkspaces": true,
"command": { "command": {

View File

@ -1,6 +1,6 @@
{ {
"name": "@cerc-io/cache", "name": "@cerc-io/cache",
"version": "0.2.107", "version": "0.2.108",
"description": "Generic object cache", "description": "Generic object cache",
"main": "dist/index.js", "main": "dist/index.js",
"scripts": { "scripts": {

View File

@ -1,6 +1,6 @@
{ {
"name": "@cerc-io/cli", "name": "@cerc-io/cli",
"version": "0.2.107", "version": "0.2.108",
"main": "dist/index.js", "main": "dist/index.js",
"license": "AGPL-3.0", "license": "AGPL-3.0",
"scripts": { "scripts": {
@ -15,13 +15,13 @@
}, },
"dependencies": { "dependencies": {
"@apollo/client": "^3.7.1", "@apollo/client": "^3.7.1",
"@cerc-io/cache": "^0.2.107", "@cerc-io/cache": "^0.2.108",
"@cerc-io/ipld-eth-client": "^0.2.107", "@cerc-io/ipld-eth-client": "^0.2.108",
"@cerc-io/libp2p": "^0.42.2-laconic-0.1.4", "@cerc-io/libp2p": "^0.42.2-laconic-0.1.4",
"@cerc-io/nitro-node": "^0.1.15", "@cerc-io/nitro-node": "^0.1.15",
"@cerc-io/peer": "^0.2.107", "@cerc-io/peer": "^0.2.108",
"@cerc-io/rpc-eth-client": "^0.2.107", "@cerc-io/rpc-eth-client": "^0.2.108",
"@cerc-io/util": "^0.2.107", "@cerc-io/util": "^0.2.108",
"@ethersproject/providers": "^5.4.4", "@ethersproject/providers": "^5.4.4",
"@graphql-tools/utils": "^9.1.1", "@graphql-tools/utils": "^9.1.1",
"@ipld/dag-cbor": "^8.0.0", "@ipld/dag-cbor": "^8.0.0",

View File

@ -1,6 +1,6 @@
{ {
"name": "@cerc-io/codegen", "name": "@cerc-io/codegen",
"version": "0.2.107", "version": "0.2.108",
"description": "Code generator", "description": "Code generator",
"private": true, "private": true,
"main": "index.js", "main": "index.js",
@ -20,7 +20,7 @@
}, },
"homepage": "https://github.com/cerc-io/watcher-ts#readme", "homepage": "https://github.com/cerc-io/watcher-ts#readme",
"dependencies": { "dependencies": {
"@cerc-io/util": "^0.2.107", "@cerc-io/util": "^0.2.108",
"@graphql-tools/load-files": "^6.5.2", "@graphql-tools/load-files": "^6.5.2",
"@npmcli/package-json": "^5.0.0", "@npmcli/package-json": "^5.0.0",
"@poanet/solidity-flattener": "https://github.com/vulcanize/solidity-flattener.git", "@poanet/solidity-flattener": "https://github.com/vulcanize/solidity-flattener.git",

View File

@ -635,6 +635,10 @@ export class Indexer implements IndexerInterface {
return this._baseIndexer.watchContract(address, kind, checkpoint, startingBlock, context); return this._baseIndexer.watchContract(address, kind, checkpoint, startingBlock, context);
} }
async removeContract (address: string, kind: string): Promise<void> {
return this._baseIndexer.removeContract(address, kind);
}
updateStateStatusMap (address: string, stateStatus: StateStatus): void { updateStateStatusMap (address: string, stateStatus: StateStatus): void {
this._baseIndexer.updateStateStatusMap(address, stateStatus); this._baseIndexer.updateStateStatusMap(address, stateStatus);
} }

View File

@ -41,12 +41,12 @@
"homepage": "https://github.com/cerc-io/watcher-ts#readme", "homepage": "https://github.com/cerc-io/watcher-ts#readme",
"dependencies": { "dependencies": {
"@apollo/client": "^3.3.19", "@apollo/client": "^3.3.19",
"@cerc-io/cli": "^0.2.107", "@cerc-io/cli": "^0.2.108",
"@cerc-io/ipld-eth-client": "^0.2.107", "@cerc-io/ipld-eth-client": "^0.2.108",
"@cerc-io/solidity-mapper": "^0.2.107", "@cerc-io/solidity-mapper": "^0.2.108",
"@cerc-io/util": "^0.2.107", "@cerc-io/util": "^0.2.108",
{{#if (subgraphPath)}} {{#if (subgraphPath)}}
"@cerc-io/graph-node": "^0.2.107", "@cerc-io/graph-node": "^0.2.108",
{{/if}} {{/if}}
"@ethersproject/providers": "^5.4.4", "@ethersproject/providers": "^5.4.4",
"debug": "^4.3.1", "debug": "^4.3.1",

View File

@ -1,10 +1,10 @@
{ {
"name": "@cerc-io/graph-node", "name": "@cerc-io/graph-node",
"version": "0.2.107", "version": "0.2.108",
"main": "dist/index.js", "main": "dist/index.js",
"license": "AGPL-3.0", "license": "AGPL-3.0",
"devDependencies": { "devDependencies": {
"@cerc-io/solidity-mapper": "^0.2.107", "@cerc-io/solidity-mapper": "^0.2.108",
"@ethersproject/providers": "^5.4.4", "@ethersproject/providers": "^5.4.4",
"@graphprotocol/graph-ts": "^0.22.0", "@graphprotocol/graph-ts": "^0.22.0",
"@nomiclabs/hardhat-ethers": "^2.0.2", "@nomiclabs/hardhat-ethers": "^2.0.2",
@ -51,9 +51,9 @@
"dependencies": { "dependencies": {
"@apollo/client": "^3.3.19", "@apollo/client": "^3.3.19",
"@cerc-io/assemblyscript": "0.19.10-watcher-ts-0.1.2", "@cerc-io/assemblyscript": "0.19.10-watcher-ts-0.1.2",
"@cerc-io/cache": "^0.2.107", "@cerc-io/cache": "^0.2.108",
"@cerc-io/ipld-eth-client": "^0.2.107", "@cerc-io/ipld-eth-client": "^0.2.108",
"@cerc-io/util": "^0.2.107", "@cerc-io/util": "^0.2.108",
"@types/json-diff": "^0.5.2", "@types/json-diff": "^0.5.2",
"@types/yargs": "^17.0.0", "@types/yargs": "^17.0.0",
"bn.js": "^4.11.9", "bn.js": "^4.11.9",

View File

@ -734,25 +734,13 @@ export const instantiate = async (
return __newString(dataSource.network); return __newString(dataSource.network);
}, },
'dataSource.create': async (name: number, params: number) => { 'dataSource.create': async (name: number, params: number) => {
const [addressStringPtr] = __getArray(params); await handleDataSourceCreate(name, 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));
}, },
'dataSource.createWithContext': async (name: number, params: number, dataSourceContext: number) => { '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 contextInstance = await Entity.wrap(dataSourceContext);
const dbData = await database.fromGraphContext(instanceExports, contextInstance); const dbData = await database.fromGraphContext(instanceExports, contextInstance);
assert(indexer.watchContract); await handleDataSourceCreate(name, params, dbData);
assert(context.block);
await indexer.watchContract(utils.getAddress(addressString), contractKind, true, Number(context.block.blockNumber), dbData);
} }
}, },
json: { 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 instance = await loader.instantiate(source, imports);
const { exports: instanceExports } = instance; const { exports: instanceExports } = instance;

View File

@ -276,6 +276,10 @@ export class Indexer implements IndexerInterface {
return undefined; return undefined;
} }
async removeContract (address: string, kind: string): Promise<void> {
return undefined;
}
async processBlock (blockProgress: BlockProgressInterface): Promise<void> { async processBlock (blockProgress: BlockProgressInterface): Promise<void> {
return undefined; return undefined;
} }

View File

@ -1,6 +1,6 @@
{ {
"name": "@cerc-io/ipld-eth-client", "name": "@cerc-io/ipld-eth-client",
"version": "0.2.107", "version": "0.2.108",
"description": "IPLD ETH Client", "description": "IPLD ETH Client",
"main": "dist/index.js", "main": "dist/index.js",
"scripts": { "scripts": {
@ -20,8 +20,8 @@
"homepage": "https://github.com/cerc-io/watcher-ts#readme", "homepage": "https://github.com/cerc-io/watcher-ts#readme",
"dependencies": { "dependencies": {
"@apollo/client": "^3.7.1", "@apollo/client": "^3.7.1",
"@cerc-io/cache": "^0.2.107", "@cerc-io/cache": "^0.2.108",
"@cerc-io/util": "^0.2.107", "@cerc-io/util": "^0.2.108",
"cross-fetch": "^3.1.4", "cross-fetch": "^3.1.4",
"debug": "^4.3.1", "debug": "^4.3.1",
"ethers": "^5.4.4", "ethers": "^5.4.4",

View File

@ -1,6 +1,6 @@
{ {
"name": "@cerc-io/peer", "name": "@cerc-io/peer",
"version": "0.2.107", "version": "0.2.108",
"description": "libp2p module", "description": "libp2p module",
"main": "dist/index.js", "main": "dist/index.js",
"exports": "./dist/index.js", "exports": "./dist/index.js",

View File

@ -1,6 +1,6 @@
{ {
"name": "@cerc-io/rpc-eth-client", "name": "@cerc-io/rpc-eth-client",
"version": "0.2.107", "version": "0.2.108",
"description": "RPC ETH Client", "description": "RPC ETH Client",
"main": "dist/index.js", "main": "dist/index.js",
"scripts": { "scripts": {
@ -19,9 +19,9 @@
}, },
"homepage": "https://github.com/cerc-io/watcher-ts#readme", "homepage": "https://github.com/cerc-io/watcher-ts#readme",
"dependencies": { "dependencies": {
"@cerc-io/cache": "^0.2.107", "@cerc-io/cache": "^0.2.108",
"@cerc-io/ipld-eth-client": "^0.2.107", "@cerc-io/ipld-eth-client": "^0.2.108",
"@cerc-io/util": "^0.2.107", "@cerc-io/util": "^0.2.108",
"chai": "^4.3.4", "chai": "^4.3.4",
"ethers": "^5.4.4", "ethers": "^5.4.4",
"left-pad": "^1.3.0", "left-pad": "^1.3.0",

View File

@ -1,6 +1,6 @@
{ {
"name": "@cerc-io/solidity-mapper", "name": "@cerc-io/solidity-mapper",
"version": "0.2.107", "version": "0.2.108",
"main": "dist/index.js", "main": "dist/index.js",
"license": "AGPL-3.0", "license": "AGPL-3.0",
"devDependencies": { "devDependencies": {

View File

@ -1,6 +1,6 @@
{ {
"name": "@cerc-io/test", "name": "@cerc-io/test",
"version": "0.2.107", "version": "0.2.108",
"main": "dist/index.js", "main": "dist/index.js",
"license": "AGPL-3.0", "license": "AGPL-3.0",
"private": true, "private": true,

View File

@ -1,6 +1,6 @@
{ {
"name": "@cerc-io/tracing-client", "name": "@cerc-io/tracing-client",
"version": "0.2.107", "version": "0.2.108",
"description": "ETH VM tracing client", "description": "ETH VM tracing client",
"main": "dist/index.js", "main": "dist/index.js",
"scripts": { "scripts": {

View File

@ -1,13 +1,13 @@
{ {
"name": "@cerc-io/util", "name": "@cerc-io/util",
"version": "0.2.107", "version": "0.2.108",
"main": "dist/index.js", "main": "dist/index.js",
"license": "AGPL-3.0", "license": "AGPL-3.0",
"dependencies": { "dependencies": {
"@apollo/utils.keyvaluecache": "^1.0.1", "@apollo/utils.keyvaluecache": "^1.0.1",
"@cerc-io/nitro-node": "^0.1.15", "@cerc-io/nitro-node": "^0.1.15",
"@cerc-io/peer": "^0.2.107", "@cerc-io/peer": "^0.2.108",
"@cerc-io/solidity-mapper": "^0.2.107", "@cerc-io/solidity-mapper": "^0.2.108",
"@cerc-io/ts-channel": "1.0.3-ts-nitro-0.1.1", "@cerc-io/ts-channel": "1.0.3-ts-nitro-0.1.1",
"@ethersproject/properties": "^5.7.0", "@ethersproject/properties": "^5.7.0",
"@ethersproject/providers": "^5.4.4", "@ethersproject/providers": "^5.4.4",
@ -55,7 +55,7 @@
"yargs": "^17.0.1" "yargs": "^17.0.1"
}, },
"devDependencies": { "devDependencies": {
"@cerc-io/cache": "^0.2.107", "@cerc-io/cache": "^0.2.108",
"@nomiclabs/hardhat-waffle": "^2.0.1", "@nomiclabs/hardhat-waffle": "^2.0.1",
"@types/bunyan": "^1.8.8", "@types/bunyan": "^1.8.8",
"@types/express": "^4.17.14", "@types/express": "^4.17.14",

View File

@ -920,6 +920,22 @@ export class Indexer {
} }
} }
async removeContract (address: string, kind: string): Promise<void> {
const dbTx = await this._db.createTransactionRunner();
try {
await this._db.deleteEntitiesByConditions(dbTx, 'contract', { kind, address });
this._clearWatchedContracts(
watchedContract => watchedContract.kind === kind && watchedContract.address === address
);
} catch (error) {
await dbTx.rollbackTransaction();
throw error;
} finally {
await dbTx.release();
}
}
cacheContract (contract: ContractInterface): void { cacheContract (contract: ContractInterface): void {
if (!this._watchedContractsByAddressMap[contract.address]) { if (!this._watchedContractsByAddressMap[contract.address]) {
this._watchedContractsByAddressMap[contract.address] = []; this._watchedContractsByAddressMap[contract.address] = [];

View File

@ -97,23 +97,32 @@ export const createAndStartServer = async (
await server.start(); await server.start();
server.applyMiddleware({
app,
path: gqlPath
});
const rpcPath = serverConfig.ethRPC?.path ?? DEFAULT_ETH_RPC_PATH; const rpcPath = serverConfig.ethRPC?.path ?? DEFAULT_ETH_RPC_PATH;
const rpcEnabled = serverConfig.ethRPC?.enabled;
// Apply GraphQL middleware
const applyGraphQLMiddleware = () => {
server.applyMiddleware({
app,
path: gqlPath
});
};
// Apply RPC middleware
const applyRPCMiddleware = () => {
if (!rpcEnabled) {
return;
}
if (serverConfig.ethRPC?.enabled) {
// Create a JSON-RPC server to handle ETH RPC calls // Create a JSON-RPC server to handle ETH RPC calls
const rpcServer = jayson.Server(ethRPCHandlers); const rpcServer = jayson.Server(ethRPCHandlers);
// Mount the JSON-RPC server to ETH_RPC_PATH // Mount the JSON-RPC server to rpcPath
app.use( app.use(
rpcPath, rpcPath,
jsonParser(), jsonParser(),
(req: any, res: any, next: () => void) => { (req: any, res: any, next: () => void) => {
// Convert all GET requests to POST to avoid getting rejected from jayson server middleware // Convert all GET requests to POST to avoid getting rejected by jayson server middleware
if (jayson.Utils.isMethod(req, 'GET')) { if (jayson.Utils.isMethod(req, 'GET')) {
req.method = 'POST'; req.method = 'POST';
} }
@ -121,15 +130,32 @@ export const createAndStartServer = async (
}, },
rpcServer.middleware() rpcServer.middleware()
); );
};
// Apply middlewares based on path specificity
if (isPathMoreSpecific(rpcPath, gqlPath)) {
applyRPCMiddleware();
applyGraphQLMiddleware();
} else {
applyGraphQLMiddleware();
applyRPCMiddleware();
} }
httpServer.listen(port, host, () => { httpServer.listen(port, host, () => {
log(`GQL server is listening on http://${host}:${port}${server.graphqlPath}`); log(`GQL server is listening on http://${host}:${port}${gqlPath}`);
if (serverConfig.ethRPC?.enabled) { if (rpcEnabled) {
log(`ETH JSON RPC server is listening on http://${host}:${port}${rpcPath}`); log(`ETH JSON RPC server is listening on http://${host}:${port}${rpcPath}`);
} }
}); });
return server; return server;
}; };
// Determine which path is more specific (more segments)
function isPathMoreSpecific (path1: string, path2: string) {
const path1Segments = path1.split('/').filter(segment => segment !== '');
const path2Segments = path2.split('/').filter(segment => segment !== '');
return path1Segments.length > path2Segments.length;
}

View File

@ -217,6 +217,7 @@ export interface IndexerInterface {
addContracts?: () => Promise<void> addContracts?: () => Promise<void>
cacheContract: (contract: ContractInterface) => void; cacheContract: (contract: ContractInterface) => void;
watchContract: (address: string, kind: string, checkpoint: boolean, startingBlock: number, context?: any) => Promise<void> watchContract: (address: string, kind: string, checkpoint: boolean, startingBlock: number, context?: any) => Promise<void>
removeContract: (address: string, kind: string) => Promise<void>;
getEntityTypesMap?: () => Map<string, { [key: string]: string }> getEntityTypesMap?: () => Map<string, { [key: string]: string }>
getRelationsMap?: () => Map<any, { [key: string]: any }> getRelationsMap?: () => Map<any, { [key: string]: any }>
processInitialState: (contractAddress: string, blockHash: string) => Promise<any> processInitialState: (contractAddress: string, blockHash: string) => Promise<any>