Add a script to analyze given eth getLogs requests (#534)

* Add a script to perform configured eth getLogs calls

* Add get logs requests with blockhash near head

* Add get logs requests with blockrange near head

* Add get logs requests with older blockrange

* Update get logs requests

* Export curl requests for given params to a file

* Support making requests in parallel

* Refactor duplicate code and rename files
This commit is contained in:
prathamesh0 2024-08-29 17:16:52 +05:30 committed by GitHub
parent d0f88756c3
commit ea5ff93e21
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 597 additions and 0 deletions

View File

@ -74,3 +74,11 @@
```bash
yarn get-storage-at -e http://127.0.0.1:8545 -c 0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f -s 0x1 -b 0xB5FFFF
```
## Get Logs Requests
* Run:
```bash
yarn eth-get-logs -i <input-requests-json-file> -o <output-results-json-file> -c <output-curl-requests-file> -e http://127.0.0.1:1234/rpc/v1 --parallel <true | false>
```

View File

@ -0,0 +1,66 @@
[
{
"blockHash": "0xfcac7d16db53c4a3dedd4f6cad3c2c5c74311b602c9c812672bca1f0cad3b207",
"address": [],
"topics": [[]]
},
{
"blockHash": "0xfcac7d16db53c4a3dedd4f6cad3c2c5c74311b602c9c812672bca1f0cad3b207",
"address": [
"0x60e1773636cf5e4a227d9ac24f20feca034ee25a",
"0x57e3bb9f790185cfe70cc2c15ed5d6b84dcf4adb",
"0x443a6243a36ef0ae1c46523d563c15abd787f4e9",
"0xaaa93ac72becfbbc9149f293466bbdaa4b5ef68c"
],
"topics": [[]]
},
{
"blockHash": "0xfcac7d16db53c4a3dedd4f6cad3c2c5c74311b602c9c812672bca1f0cad3b207",
"address": [],
"topics":[[
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
"0xe1fffcc4923d04b559f4d29a8bfc6cda04eb5b0d3c460751c2402c5c5cc9109c",
"0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925",
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
"0xc42079f94a6350d7e6235f29174924f928cc2ac818eb64fed8004e115fbcca67",
"0x9d9c909296d9c674451c0c24f02cb64981eb3b727f99865939192f880a755dcb",
"0xa4b3513a5f822f3e098a6a12338b3f07613cb130b75c90a250ab181402f4bb87"
]]
},
{
"blockHash": "0xfcac7d16db53c4a3dedd4f6cad3c2c5c74311b602c9c812672bca1f0cad3b207",
"address": [
"0x60e1773636cf5e4a227d9ac24f20feca034ee25a",
"0x57e3bb9f790185cfe70cc2c15ed5d6b84dcf4adb",
"0x443a6243a36ef0ae1c46523d563c15abd787f4e9",
"0xaaa93ac72becfbbc9149f293466bbdaa4b5ef68c"
],
"topics":[[
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
"0xe1fffcc4923d04b559f4d29a8bfc6cda04eb5b0d3c460751c2402c5c5cc9109c",
"0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925",
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
"0xc42079f94a6350d7e6235f29174924f928cc2ac818eb64fed8004e115fbcca67",
"0x9d9c909296d9c674451c0c24f02cb64981eb3b727f99865939192f880a755dcb",
"0xa4b3513a5f822f3e098a6a12338b3f07613cb130b75c90a250ab181402f4bb87"
]]
},
{
"blockHash": "0xfcac7d16db53c4a3dedd4f6cad3c2c5c74311b602c9c812672bca1f0cad3b207",
"address": [
"0x60e1773636cf5e4a227d9ac24f20feca034ee25a"
],
"topics":[[
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"
]]
},
{
"blockHash": "0xfcac7d16db53c4a3dedd4f6cad3c2c5c74311b602c9c812672bca1f0cad3b207",
"address": [
"0x497f5f88e0bad1a184e110514142dd9d94728ed5"
],
"topics":[[
"0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925"
]]
}
]

View File

@ -0,0 +1,72 @@
[
{
"fromBlock": "0x401CD0",
"toBlock": "0x401D34",
"address": [],
"topics": [[]]
},
{
"fromBlock": "0x401CD0",
"toBlock": "0x401D34",
"address": [
"0x60e1773636cf5e4a227d9ac24f20feca034ee25a",
"0x57e3bb9f790185cfe70cc2c15ed5d6b84dcf4adb",
"0x443a6243a36ef0ae1c46523d563c15abd787f4e9",
"0xaaa93ac72becfbbc9149f293466bbdaa4b5ef68c"
],
"topics": [[]]
},
{
"fromBlock": "0x401CD0",
"toBlock": "0x401D34",
"address": [],
"topics":[[
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
"0xe1fffcc4923d04b559f4d29a8bfc6cda04eb5b0d3c460751c2402c5c5cc9109c",
"0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925",
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
"0xc42079f94a6350d7e6235f29174924f928cc2ac818eb64fed8004e115fbcca67",
"0x9d9c909296d9c674451c0c24f02cb64981eb3b727f99865939192f880a755dcb",
"0xa4b3513a5f822f3e098a6a12338b3f07613cb130b75c90a250ab181402f4bb87"
]]
},
{
"fromBlock": "0x401CD0",
"toBlock": "0x401D34",
"address": [
"0x60e1773636cf5e4a227d9ac24f20feca034ee25a",
"0x57e3bb9f790185cfe70cc2c15ed5d6b84dcf4adb",
"0x443a6243a36ef0ae1c46523d563c15abd787f4e9",
"0xaaa93ac72becfbbc9149f293466bbdaa4b5ef68c"
],
"topics":[[
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
"0xe1fffcc4923d04b559f4d29a8bfc6cda04eb5b0d3c460751c2402c5c5cc9109c",
"0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925",
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
"0xc42079f94a6350d7e6235f29174924f928cc2ac818eb64fed8004e115fbcca67",
"0x9d9c909296d9c674451c0c24f02cb64981eb3b727f99865939192f880a755dcb",
"0xa4b3513a5f822f3e098a6a12338b3f07613cb130b75c90a250ab181402f4bb87"
]]
},
{
"fromBlock": "0x401CD0",
"toBlock": "0x401D34",
"address": [
"0x60e1773636cf5e4a227d9ac24f20feca034ee25a"
],
"topics":[[
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"
]]
},
{
"fromBlock": "0x401CD0",
"toBlock": "0x401D34",
"address": [
"0x497f5f88e0bad1a184e110514142dd9d94728ed5"
],
"topics":[[
"0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925"
]]
}
]

View File

@ -0,0 +1,122 @@
[
{
"address": [
"0x497f5f88e0bad1a184e110514142dd9d94728ed5"
],
"topics": [
[
"0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925"
]
],
"fromBlock": "0x405C02",
"toBlock": "0x405C66"
},
{
"address": [
"0x60e1773636cf5e4a227d9ac24f20feca034ee25a"
],
"topics": [
[
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"
]
],
"fromBlock": "0x405C02",
"toBlock": "0x405C66"
},
{
"address": [
"0x60e1773636cf5e4a227d9ac24f20feca034ee25a"
],
"topics": [
[
"0xe1fffcc4923d04b559f4d29a8bfc6cda04eb5b0d3c460751c2402c5c5cc9109c"
]
],
"fromBlock": "0x405C02",
"toBlock": "0x405C66"
},
{
"address": [
"0x60e1773636cf5e4a227d9ac24f20feca034ee25a"
],
"topics": [
[
"0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925"
]
],
"fromBlock": "0x405C02",
"toBlock": "0x405C66"
},
{
"address": [
"0x57e3bb9f790185cfe70cc2c15ed5d6b84dcf4adb"
],
"topics": [
[
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"
]
],
"fromBlock": "0x405C02",
"toBlock": "0x405C66"
},
{
"address": [
"0x443a6243a36ef0ae1c46523d563c15abd787f4e9"
],
"topics": [
[
"0xc42079f94a6350d7e6235f29174924f928cc2ac818eb64fed8004e115fbcca67"
]
],
"fromBlock": "0x405C02",
"toBlock": "0x405C66"
},
{
"address": [
"0xaaa93ac72becfbbc9149f293466bbdaa4b5ef68c"
],
"topics": [
[
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"
]
],
"fromBlock": "0x405C02",
"toBlock": "0x405C66"
},
{
"address": [
"0xaaa93ac72becfbbc9149f293466bbdaa4b5ef68c"
],
"topics": [
[
"0x9d9c909296d9c674451c0c24f02cb64981eb3b727f99865939192f880a755dcb"
]
],
"fromBlock": "0x405C02",
"toBlock": "0x405C66"
},
{
"address": [
"0x763b29b97e75fb54923325d46bab2807ac8c43c5"
],
"topics": [
[
"0xf154a899b3b867021c992026539485521c86f83735a958729ab118b9ce7a6407"
]
],
"fromBlock": "0x405C02",
"toBlock": "0x405C66"
},
{
"address": [
"0xd51cb0fa9a91f156a80188a18f039140704b8df7"
],
"topics": [
[
"0xa4b3513a5f822f3e098a6a12338b3f07613cb130b75c90a250ab181402f4bb87"
]
],
"fromBlock": "0x405C02",
"toBlock": "0x405C66"
}
]

View File

@ -0,0 +1,72 @@
[
{
"fromBlock": "0x405C02",
"toBlock": "0x405C66",
"address": [],
"topics": [[]]
},
{
"fromBlock": "0x405C02",
"toBlock": "0x405C66",
"address": [
"0x60e1773636cf5e4a227d9ac24f20feca034ee25a",
"0x57e3bb9f790185cfe70cc2c15ed5d6b84dcf4adb",
"0x443a6243a36ef0ae1c46523d563c15abd787f4e9",
"0xaaa93ac72becfbbc9149f293466bbdaa4b5ef68c"
],
"topics": [[]]
},
{
"fromBlock": "0x405C02",
"toBlock": "0x405C66",
"address": [],
"topics":[[
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
"0xe1fffcc4923d04b559f4d29a8bfc6cda04eb5b0d3c460751c2402c5c5cc9109c",
"0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925",
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
"0xc42079f94a6350d7e6235f29174924f928cc2ac818eb64fed8004e115fbcca67",
"0x9d9c909296d9c674451c0c24f02cb64981eb3b727f99865939192f880a755dcb",
"0xa4b3513a5f822f3e098a6a12338b3f07613cb130b75c90a250ab181402f4bb87"
]]
},
{
"fromBlock": "0x405C02",
"toBlock": "0x405C66",
"address": [
"0x60e1773636cf5e4a227d9ac24f20feca034ee25a",
"0x57e3bb9f790185cfe70cc2c15ed5d6b84dcf4adb",
"0x443a6243a36ef0ae1c46523d563c15abd787f4e9",
"0xaaa93ac72becfbbc9149f293466bbdaa4b5ef68c"
],
"topics":[[
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
"0xe1fffcc4923d04b559f4d29a8bfc6cda04eb5b0d3c460751c2402c5c5cc9109c",
"0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925",
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
"0xc42079f94a6350d7e6235f29174924f928cc2ac818eb64fed8004e115fbcca67",
"0x9d9c909296d9c674451c0c24f02cb64981eb3b727f99865939192f880a755dcb",
"0xa4b3513a5f822f3e098a6a12338b3f07613cb130b75c90a250ab181402f4bb87"
]]
},
{
"fromBlock": "0x405C02",
"toBlock": "0x405C66",
"address": [
"0x60e1773636cf5e4a227d9ac24f20feca034ee25a"
],
"topics":[[
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"
]]
},
{
"fromBlock": "0x405C02",
"toBlock": "0x405C66",
"address": [
"0x497f5f88e0bad1a184e110514142dd9d94728ed5"
],
"topics":[[
"0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925"
]]
}
]

View File

@ -8,6 +8,7 @@
"lint": "eslint .",
"build": "tsc",
"eth-call": "DEBUG=vulcanize:* ts-node src/eth-call.ts",
"eth-get-logs": "DEBUG=vulcanize:* ts-node src/eth-get-logs.ts",
"get-storage-at": "DEBUG=vulcanize:* ts-node src/get-storage-at.ts",
"test:snapshot": "DEBUG=vulcanize:* mocha src/snapshot.test.ts"
},

View File

@ -0,0 +1,256 @@
//
// Copyright 2024 Vulcanize, Inc.
//
import { providers } from 'ethers';
import * as fs from 'fs';
import * as path from 'path';
import debug from 'debug';
import yargs from 'yargs';
import assert from 'assert';
const log = debug('vulcanize:test');
interface LogParams {
address: string[];
topics: string[][];
fromBlock?: string;
toBlock?: string;
blockHash?: string;
}
// Format time in milliseconds into minutes and seconds
function formatTime(ms: number): string {
const minutes = Math.floor(ms / 60000);
const seconds = ((ms % 60000) / 1000).toFixed(0);
return `${minutes}m${seconds}s`;
}
async function generateCurlCommand(rpcEndpoint: string, params: LogParams): Promise<string> {
const curlParams: any = {
address: params.address,
topics: params.topics
};
if (params.blockHash) {
curlParams.blockHash = params.blockHash;
} else {
curlParams.fromBlock = params.fromBlock;
curlParams.toBlock = params.toBlock;
}
const requestBody = {
jsonrpc: "2.0",
method: "eth_getLogs",
params: [curlParams],
id: 1
};
const curlCommand = `time curl -X POST -H "Content-Type: application/json" \\\n-d '${JSON.stringify(requestBody, null, 2)}' \\\n${rpcEndpoint}`;
return curlCommand;
}
async function getLogs(provider: providers.JsonRpcProvider, logParams: LogParams[], outputFilePath: string, curlRequestsOutputFilePath: string) {
for (const params of logParams) {
const { filter, result, blockNumber } = await buildFilter(provider, params);
const latestBlockNumber = await provider.getBlockNumber();
result.blocksBehindHead = latestBlockNumber - blockNumber
// Generate the curl command and write it to a file
const curlCommand = await generateCurlCommand('http://localhost:1234/rpc/v1', params);
fs.appendFileSync(curlRequestsOutputFilePath, curlCommand + '\n\n');
try {
// Record the start time
const startTime = Date.now();
// Fetch logs using the filter
const ethLogs = await provider.send(
'eth_getLogs',
[filter]
);
// Format raw eth_getLogs response
const logs: providers.Log[] = providers.Formatter.arrayOf(
provider.formatter.filterLog.bind(provider.formatter)
)(ethLogs);
// Record the end time and calculate the time taken
const endTime = Date.now();
const timeTakenMs = endTime - startTime;
// Store the result
result.numEvents = logs.length;
result.timeTaken = formatTime(timeTakenMs);
} catch (error) {
console.error(`Error fetching logs for params ${JSON.stringify(params)}:`, error);
} finally {
exportResult(outputFilePath, [result]);
}
}
}
async function getLogsParallel(provider: providers.JsonRpcProvider, logParams: LogParams[], outputFilePath: string, curlRequestsOutputFilePath: string) {
const filters: any[] = [];
const results: any[] = [];
const latestBlockNumber = await provider.getBlockNumber();
for (const params of logParams) {
const { filter, result, blockNumber } = await buildFilter(provider, params);
result.blocksBehindHead = latestBlockNumber - blockNumber
filters.push(filter);
results.push(result);
// Generate the curl command and write it to a file
const curlCommand = await generateCurlCommand('http://localhost:1234/rpc/v1', params);
fs.appendFileSync(curlRequestsOutputFilePath, curlCommand + '\n\n');
}
try {
// Record the start time
const startTime = Date.now();
await Promise.all(filters.map(async (filter, index) => {
// Fetch logs using the filter
const ethLogs = await provider.send(
'eth_getLogs',
[filter]
);
// Format raw eth_getLogs response
const logs: providers.Log[] = providers.Formatter.arrayOf(
provider.formatter.filterLog.bind(provider.formatter)
)(ethLogs);
// Store the result
results[index].numEvents = logs.length;
}));
// Record the end time and calculate the time taken
const endTime = Date.now();
const timeTakenMs = endTime - startTime;
const formattedTime = formatTime(timeTakenMs);
results.forEach(result => result.timeTaken = formattedTime);
} catch (error) {
console.error(`Error fetching logs:`, error);
} finally {
exportResult(outputFilePath, results);
}
}
async function buildFilter (provider: providers.JsonRpcProvider, params: LogParams): Promise<{ filter: any, result: any, blockNumber: number }> {
// Build the filter object
const filter: any = {
address: params.address.map(address => address.toLowerCase()),
topics: params.topics,
};
const result = {
...filter,
address: params.address
};
let blockNumber: number;
if (params.blockHash) {
filter.blockHash = params.blockHash;
result.blockHash = params.blockHash;
const block = await provider.getBlock(params.blockHash);
blockNumber = block.number;
result.blockNumber = blockNumber;
} else {
assert(params.toBlock && params.fromBlock, 'fromBlock or toBlock not found');
filter.fromBlock = params.fromBlock;
filter.toBlock = params.toBlock;
result.fromBlock = params.fromBlock;
result.toBlock = params.toBlock;
blockNumber = parseInt(params.toBlock, 16);
result.blocksRange = parseInt(params.toBlock, 16) - parseInt(params.fromBlock, 16);
}
return { filter, result, blockNumber };
}
function exportResult (outputFilePath: string, results: any[]): void {
let existingData = [];
// Read existing outputfile
if (fs.existsSync(outputFilePath)) {
const data = fs.readFileSync(outputFilePath, 'utf-8');
existingData = JSON.parse(data || '[]');
}
// Append new result to existing data
existingData.push(...results);
// Write the updated data back to the JSON file
fs.writeFileSync(outputFilePath, JSON.stringify(existingData, null, 2));
}
async function main() {
const argv = await yargs.parserConfiguration({
'parse-numbers': false
}).options({
endpoint: {
alias: 'e',
demandOption: true,
describe: 'Endpoint to perform eth-get-logs calls against',
type: 'string'
},
input: {
alias: 'i',
demandOption: true,
describe: 'Input file path',
type: 'string'
},
output: {
alias: 'o',
demandOption: true,
describe: 'Output file path',
type: 'string'
},
curlRequestsOutput: {
alias: 'c',
demandOption: true,
describe: 'Output file path for curl requests',
type: 'string'
},
parallel: {
alias: 'p',
default: false,
describe: 'Make requests in parallel',
type: 'boolean'
},
}).argv;
const outputFilePath = path.resolve(argv.output);
const curlRequestsOutputFilePath = path.resolve(argv.curlRequestsOutput);
// Read the input json file
const logParams: LogParams[] = JSON.parse(fs.readFileSync(path.resolve(argv.input), 'utf-8'));
// Create a provider with sufficient timeout
const timeout = 10 * 60 * 1000; // 10mins
const provider = new providers.JsonRpcProvider({ url: argv.endpoint, timeout });
// Get logs and measure performance
if (argv.parallel) {
log('Making parallel requests');
await getLogsParallel(provider, logParams, outputFilePath, curlRequestsOutputFilePath);
} else {
log('Making serial requests');
await getLogs(provider, logParams, outputFilePath, curlRequestsOutputFilePath);
}
log(`Results written to ${outputFilePath}`);
log(`CURL requests written to ${curlRequestsOutputFilePath}`);
}
main().catch(err => {
log(err);
});