Skip to content

Commit

Permalink
Export metric for total ETH RPC count (#528)
Browse files Browse the repository at this point in the history
* Export metric for total ETH RPC count by methods

* Fix endpoint switch on max retries of new block

* Upgrade package versions
  • Loading branch information
nikugogoi authored Jul 12, 2024
1 parent 2217cd3 commit 42cb688
Show file tree
Hide file tree
Showing 21 changed files with 90 additions and 77 deletions.
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.102",
"version": "0.2.103",
"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.102",
"version": "0.2.103",
"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.102",
"version": "0.2.103",
"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.102",
"@cerc-io/ipld-eth-client": "^0.2.102",
"@cerc-io/cache": "^0.2.103",
"@cerc-io/ipld-eth-client": "^0.2.103",
"@cerc-io/libp2p": "^0.42.2-laconic-0.1.4",
"@cerc-io/nitro-node": "^0.1.15",
"@cerc-io/peer": "^0.2.102",
"@cerc-io/rpc-eth-client": "^0.2.102",
"@cerc-io/util": "^0.2.102",
"@cerc-io/peer": "^0.2.103",
"@cerc-io/rpc-eth-client": "^0.2.103",
"@cerc-io/util": "^0.2.103",
"@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.102",
"version": "0.2.103",
"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.102",
"@cerc-io/util": "^0.2.103",
"@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
6 changes: 5 additions & 1 deletion packages/codegen/src/templates/indexer-template.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ export class Indexer implements IndexerInterface {
};
}

const { block: { number } } = await this._ethClient.getBlockByHash(blockHash);
const { block: { number } } = await this.getBlockByHash(blockHash);
const blockNumber = ethers.BigNumber.from(number).toNumber();

log('{{query.name}}: db miss, fetching from upstream server');
Expand Down Expand Up @@ -679,6 +679,10 @@ export class Indexer implements IndexerInterface {
return this._baseIndexer.getBlocks(blockFilter);
}

async getBlockByHash (blockHash?: string): Promise<{ block: any }> {
return this._baseIndexer.getBlockByHash(blockHash);
}

async updateSyncStatusIndexedBlock (blockHash: string, blockNumber: number, force = false): Promise<SyncStatus> {
return this._baseIndexer.updateSyncStatusIndexedBlock(blockHash, blockNumber, force);
}
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.102",
"@cerc-io/ipld-eth-client": "^0.2.102",
"@cerc-io/solidity-mapper": "^0.2.102",
"@cerc-io/util": "^0.2.102",
"@cerc-io/cli": "^0.2.103",
"@cerc-io/ipld-eth-client": "^0.2.103",
"@cerc-io/solidity-mapper": "^0.2.103",
"@cerc-io/util": "^0.2.103",
{{#if (subgraphPath)}}
"@cerc-io/graph-node": "^0.2.102",
"@cerc-io/graph-node": "^0.2.103",
{{/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.102",
"version": "0.2.103",
"main": "dist/index.js",
"license": "AGPL-3.0",
"devDependencies": {
"@cerc-io/solidity-mapper": "^0.2.102",
"@cerc-io/solidity-mapper": "^0.2.103",
"@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.102",
"@cerc-io/ipld-eth-client": "^0.2.102",
"@cerc-io/util": "^0.2.102",
"@cerc-io/cache": "^0.2.103",
"@cerc-io/ipld-eth-client": "^0.2.103",
"@cerc-io/util": "^0.2.103",
"@types/json-diff": "^0.5.2",
"@types/yargs": "^17.0.0",
"bn.js": "^4.11.9",
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 @@ -95,6 +95,10 @@ export class Indexer implements IndexerInterface {
return undefined;
}

async getBlockByHash (blockHash?: string): Promise<{ block: any }> {
return { block: undefined };
}

async getBlocksAtHeight (height: number, isPruned: boolean): Promise<BlockProgressInterface[]> {
assert(height);
assert(isPruned);
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.102",
"version": "0.2.103",
"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.102",
"@cerc-io/util": "^0.2.102",
"@cerc-io/cache": "^0.2.103",
"@cerc-io/util": "^0.2.103",
"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.102",
"version": "0.2.103",
"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.102",
"version": "0.2.103",
"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.102",
"@cerc-io/ipld-eth-client": "^0.2.102",
"@cerc-io/util": "^0.2.102",
"@cerc-io/cache": "^0.2.103",
"@cerc-io/ipld-eth-client": "^0.2.103",
"@cerc-io/util": "^0.2.103",
"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.102",
"version": "0.2.103",
"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.102",
"version": "0.2.103",
"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.102",
"version": "0.2.103",
"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.102",
"version": "0.2.103",
"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.102",
"@cerc-io/solidity-mapper": "^0.2.102",
"@cerc-io/peer": "^0.2.103",
"@cerc-io/solidity-mapper": "^0.2.103",
"@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 @@ -54,7 +54,7 @@
"yargs": "^17.0.1"
},
"devDependencies": {
"@cerc-io/cache": "^0.2.102",
"@cerc-io/cache": "^0.2.103",
"@nomiclabs/hardhat-waffle": "^2.0.1",
"@types/bunyan": "^1.8.8",
"@types/express": "^4.17.14",
Expand Down
57 changes: 31 additions & 26 deletions packages/util/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,22 @@ export const fetchBlocksAtHeight = async (

// Try fetching blocks from eth-server until found.
while (!blocks.length) {
const { block: latestBlock } = await indexer.getBlockByHash();
const blockProcessingOffset = jobQueueConfig.blockProcessingOffset ?? 0;

// Process block if it is blockProcessingOffset blocks behind latest block
if (latestBlock.number < blockNumber + blockProcessingOffset) {
// Check number of retries for fetching new block
if (jobQueueConfig.maxNewBlockRetries && newBlockRetries > jobQueueConfig.maxNewBlockRetries) {
throw new Error(NEW_BLOCK_MAX_RETRIES_ERROR);
}

newBlockRetries++;
log(`Latest block: ${latestBlock.number}, blockProcessingOffset: ${blockProcessingOffset}; retry block to process: ${blockNumber} after ${jobQueueConfig.blockDelayInMilliSecs}ms`);
await wait(jobQueueConfig.blockDelayInMilliSecs);
continue;
}

console.time(`time:common#_fetchBlocks-eth-server-${blockNumber}`);
const ethFullBlocks = await indexer.getBlocks({ blockNumber });
console.timeEnd(`time:common#_fetchBlocks-eth-server-${blockNumber}`);
Expand All @@ -84,32 +100,21 @@ export const fetchBlocksAtHeight = async (

// Fitler null blocks
blocks = ethFullBlocks.filter(block => Boolean(block)) as EthFullBlock[];

if (!blocks.length) {
log(`No blocks fetched for block number ${blockNumber}, retrying after ${jobQueueConfig.blockDelayInMilliSecs} ms delay.`);

// Check number of retries for fetching new block
if (jobQueueConfig.maxNewBlockRetries && newBlockRetries > jobQueueConfig.maxNewBlockRetries) {
throw new Error(NEW_BLOCK_MAX_RETRIES_ERROR);
}

newBlockRetries++;
await wait(jobQueueConfig.blockDelayInMilliSecs);
} else {
blocks.forEach(block => {
blockAndEventsMap.set(
block.blockHash,
{
// Block is set later in job-runner when saving to database
block: {} as BlockProgressInterface,
events: [],
ethFullBlock: block,
// Transactions are set later in job-runner when fetching events
ethFullTransactions: []
}
);
});
}
assert(blocks.length, `Blocks at ${blockNumber} should exist as latest block is ${latestBlock}`);

blocks.forEach(block => {
blockAndEventsMap.set(
block.blockHash,
{
// Block is set later in job-runner when saving to database
block: {} as BlockProgressInterface,
events: [],
ethFullBlock: block,
// Transactions are set later in job-runner when fetching events
ethFullTransactions: []
}
);
});
}

assert(blocks.length, 'Blocks not fetched');
Expand Down
16 changes: 2 additions & 14 deletions packages/util/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { createPruningJob, processBlockByNumber } from './common';
import { OrderDirection } from './database';
import { HistoricalJobData, HistoricalJobResponseData } from './job-runner';
import { JobQueueConfig, ServerConfig } from './config';
import { wait } from './misc';

const EVENT = 'event';
const BLOCK_PROGRESS_EVENT = 'block-progress-event';
Expand Down Expand Up @@ -105,7 +104,7 @@ export class EventWatcher {
// Get latest block in chain and sync status from DB
// Also get historical-processing queue size
const [{ block: latestBlock }, syncStatus, historicalProcessingQueueSize] = await Promise.all([
this._ethClient.getBlockByHash(),
this._indexer.getBlockByHash(),
this._indexer.getSyncStatus(),
this._jobQueue.getQueueSize(QUEUE_HISTORICAL_PROCESSING, 'completed')
]);
Expand Down Expand Up @@ -196,18 +195,7 @@ export class EventWatcher {
}

if (isComplete) {
while (true) {
const { block: latestBlock } = await this._ethClient.getBlockByHash();

// Process block if it is blockProcessingOffset blocks behind latest block
if (latestBlock.number >= blockNumber + (this._config.jobQueue.blockProcessingOffset ?? 0)) {
await processBlockByNumber(this._jobQueue, blockNumber + 1);
break;
}

log(`Latest block: ${latestBlock.number}; retry next block to process: ${blockNumber + 1} after ${this._config.jobQueue.blockDelayInMilliSecs}ms`);
await wait(this._config.jobQueue.blockDelayInMilliSecs);
}
await processBlockByNumber(this._jobQueue, blockNumber + 1);
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions packages/util/src/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,10 @@ export class Indexer {
return blocks;
}

async getBlockByHash (blockHash?: string): Promise<{ block: any }> {
return this._ethClient.getBlockByHash(blockHash);
}

async getBlockProgress (blockHash: string): Promise<BlockProgressInterface | undefined> {
return this._db.getBlockProgress(blockHash);
}
Expand Down
6 changes: 6 additions & 0 deletions packages/util/src/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ export const isSyncingHistoricalBlocks = new client.Gauge({
});
isSyncingHistoricalBlocks.set(Number(undefined));

export const ethRpcCount = new client.Counter({
name: 'watcher_eth_rpc_total',
help: 'Total number of ETH RPC requests',
labelNames: ['method', 'provider']
});

export const ethRpcErrors = new client.Counter({
name: 'watcher_eth_rpc_errors',
help: 'Number of ETH RPC request errors',
Expand Down
3 changes: 2 additions & 1 deletion packages/util/src/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { ResultEvent } from './indexer';
import { EventInterface, EthFullBlock, EthFullTransaction } from './types';
import { BlockHeight } from './database';
import { Transaction } from './graph/utils';
import { ethRpcErrors, ethRpcRequestDuration } from './metrics';
import { ethRpcCount, ethRpcErrors, ethRpcRequestDuration } from './metrics';

const JSONbigNative = JSONbig({ useNativeBigInt: true });

Expand Down Expand Up @@ -379,6 +379,7 @@ export class MonitoredStaticJsonRpcProvider extends providers.StaticJsonRpcProvi
// Rethrow the error
throw err;
} finally {
ethRpcCount.inc({ method, provider: this.connection.url }, 1);
endTimer();
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/util/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ export interface IndexerInterface {
getSyncStatus (): Promise<SyncStatusInterface | undefined>
getStateSyncStatus (): Promise<StateSyncStatusInterface | undefined>
getBlocks (blockFilter: { blockHash?: string, blockNumber?: number }): Promise<Array<EthFullBlock | null>>
getBlockByHash (blockHash?: string): Promise<{ block: any }>
getBlocksAtHeight (height: number, isPruned: boolean): Promise<BlockProgressInterface[]>
getLatestCanonicalBlock (): Promise<BlockProgressInterface | undefined>
getLatestStateIndexedBlock (): Promise<BlockProgressInterface>
Expand Down

0 comments on commit 42cb688

Please sign in to comment.