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

Handle template create in subgraph watcher during reorgs #533

Merged
merged 5 commits into from
Oct 11, 2024
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
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"packages": [
"packages/*"
],
"version": "0.2.107",
"version": "0.2.108",
"npmClient": "yarn",
"useWorkspaces": true,
"command": {
Expand Down
2 changes: 1 addition & 1 deletion packages/cache/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cerc-io/cache",
"version": "0.2.107",
"version": "0.2.108",
"description": "Generic object cache",
"main": "dist/index.js",
"scripts": {
Expand Down
12 changes: 6 additions & 6 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cerc-io/cli",
"version": "0.2.107",
"version": "0.2.108",
"main": "dist/index.js",
"license": "AGPL-3.0",
"scripts": {
Expand All @@ -15,13 +15,13 @@
},
"dependencies": {
"@apollo/client": "^3.7.1",
"@cerc-io/cache": "^0.2.107",
"@cerc-io/ipld-eth-client": "^0.2.107",
"@cerc-io/cache": "^0.2.108",
"@cerc-io/ipld-eth-client": "^0.2.108",
"@cerc-io/libp2p": "^0.42.2-laconic-0.1.4",
"@cerc-io/nitro-node": "^0.1.15",
"@cerc-io/peer": "^0.2.107",
"@cerc-io/rpc-eth-client": "^0.2.107",
"@cerc-io/util": "^0.2.107",
"@cerc-io/peer": "^0.2.108",
"@cerc-io/rpc-eth-client": "^0.2.108",
"@cerc-io/util": "^0.2.108",
"@ethersproject/providers": "^5.4.4",
"@graphql-tools/utils": "^9.1.1",
"@ipld/dag-cbor": "^8.0.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/codegen/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cerc-io/codegen",
"version": "0.2.107",
"version": "0.2.108",
"description": "Code generator",
"private": true,
"main": "index.js",
Expand All @@ -20,7 +20,7 @@
},
"homepage": "https://github.com/cerc-io/watcher-ts#readme",
"dependencies": {
"@cerc-io/util": "^0.2.107",
"@cerc-io/util": "^0.2.108",
"@graphql-tools/load-files": "^6.5.2",
"@npmcli/package-json": "^5.0.0",
"@poanet/solidity-flattener": "https://github.com/vulcanize/solidity-flattener.git",
Expand Down
4 changes: 4 additions & 0 deletions packages/codegen/src/templates/indexer-template.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,10 @@ export class Indexer implements IndexerInterface {
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 {
this._baseIndexer.updateStateStatusMap(address, stateStatus);
}
Expand Down
10 changes: 5 additions & 5 deletions packages/codegen/src/templates/package-template.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@
"homepage": "https://github.com/cerc-io/watcher-ts#readme",
"dependencies": {
"@apollo/client": "^3.3.19",
"@cerc-io/cli": "^0.2.107",
"@cerc-io/ipld-eth-client": "^0.2.107",
"@cerc-io/solidity-mapper": "^0.2.107",
"@cerc-io/util": "^0.2.107",
"@cerc-io/cli": "^0.2.108",
"@cerc-io/ipld-eth-client": "^0.2.108",
"@cerc-io/solidity-mapper": "^0.2.108",
"@cerc-io/util": "^0.2.108",
{{#if (subgraphPath)}}
"@cerc-io/graph-node": "^0.2.107",
"@cerc-io/graph-node": "^0.2.108",
{{/if}}
"@ethersproject/providers": "^5.4.4",
"debug": "^4.3.1",
Expand Down
10 changes: 5 additions & 5 deletions packages/graph-node/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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",
Expand Down
44 changes: 30 additions & 14 deletions packages/graph-node/src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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;

Expand Down
4 changes: 4 additions & 0 deletions packages/graph-node/test/utils/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions packages/ipld-eth-client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cerc-io/ipld-eth-client",
"version": "0.2.107",
"version": "0.2.108",
"description": "IPLD ETH Client",
"main": "dist/index.js",
"scripts": {
Expand All @@ -20,8 +20,8 @@
"homepage": "https://github.com/cerc-io/watcher-ts#readme",
"dependencies": {
"@apollo/client": "^3.7.1",
"@cerc-io/cache": "^0.2.107",
"@cerc-io/util": "^0.2.107",
"@cerc-io/cache": "^0.2.108",
"@cerc-io/util": "^0.2.108",
"cross-fetch": "^3.1.4",
"debug": "^4.3.1",
"ethers": "^5.4.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/peer/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cerc-io/peer",
"version": "0.2.107",
"version": "0.2.108",
"description": "libp2p module",
"main": "dist/index.js",
"exports": "./dist/index.js",
Expand Down
8 changes: 4 additions & 4 deletions packages/rpc-eth-client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cerc-io/rpc-eth-client",
"version": "0.2.107",
"version": "0.2.108",
"description": "RPC ETH Client",
"main": "dist/index.js",
"scripts": {
Expand All @@ -19,9 +19,9 @@
},
"homepage": "https://github.com/cerc-io/watcher-ts#readme",
"dependencies": {
"@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",
"chai": "^4.3.4",
"ethers": "^5.4.4",
"left-pad": "^1.3.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/solidity-mapper/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cerc-io/solidity-mapper",
"version": "0.2.107",
"version": "0.2.108",
"main": "dist/index.js",
"license": "AGPL-3.0",
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/test/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cerc-io/test",
"version": "0.2.107",
"version": "0.2.108",
"main": "dist/index.js",
"license": "AGPL-3.0",
"private": true,
Expand Down
2 changes: 1 addition & 1 deletion packages/tracing-client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cerc-io/tracing-client",
"version": "0.2.107",
"version": "0.2.108",
"description": "ETH VM tracing client",
"main": "dist/index.js",
"scripts": {
Expand Down
8 changes: 4 additions & 4 deletions packages/util/package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "@cerc-io/util",
"version": "0.2.107",
"version": "0.2.108",
"main": "dist/index.js",
"license": "AGPL-3.0",
"dependencies": {
"@apollo/utils.keyvaluecache": "^1.0.1",
"@cerc-io/nitro-node": "^0.1.15",
"@cerc-io/peer": "^0.2.107",
"@cerc-io/solidity-mapper": "^0.2.107",
"@cerc-io/peer": "^0.2.108",
"@cerc-io/solidity-mapper": "^0.2.108",
"@cerc-io/ts-channel": "1.0.3-ts-nitro-0.1.1",
"@ethersproject/properties": "^5.7.0",
"@ethersproject/providers": "^5.4.4",
Expand Down Expand Up @@ -55,7 +55,7 @@
"yargs": "^17.0.1"
},
"devDependencies": {
"@cerc-io/cache": "^0.2.107",
"@cerc-io/cache": "^0.2.108",
"@nomiclabs/hardhat-waffle": "^2.0.1",
"@types/bunyan": "^1.8.8",
"@types/express": "^4.17.14",
Expand Down
16 changes: 16 additions & 0 deletions packages/util/src/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
if (!this._watchedContractsByAddressMap[contract.address]) {
this._watchedContractsByAddressMap[contract.address] = [];
Expand Down
46 changes: 36 additions & 10 deletions packages/util/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,39 +97,65 @@ export const createAndStartServer = async (

await server.start();

server.applyMiddleware({
app,
path: gqlPath
});

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
const rpcServer = jayson.Server(ethRPCHandlers);

// Mount the JSON-RPC server to ETH_RPC_PATH
// Mount the JSON-RPC server to rpcPath
app.use(
rpcPath,
jsonParser(),
(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')) {
req.method = 'POST';
}
next();
},
rpcServer.middleware()
);
};

// Apply middlewares based on path specificity
if (isPathMoreSpecific(rpcPath, gqlPath)) {
applyRPCMiddleware();
applyGraphQLMiddleware();
} else {
applyGraphQLMiddleware();
applyRPCMiddleware();
}

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}`);
}
});

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;
}
1 change: 1 addition & 0 deletions packages/util/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ export interface IndexerInterface {
addContracts?: () => Promise<void>
cacheContract: (contract: ContractInterface) => 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 }>
getRelationsMap?: () => Map<any, { [key: string]: any }>
processInitialState: (contractAddress: string, blockHash: string) => Promise<any>
Expand Down
Loading