From 9e3b4539a18c26659047a481724cb445e36a8469 Mon Sep 17 00:00:00 2001 From: Santiago Palladino Date: Tue, 27 Aug 2024 13:01:34 -0300 Subject: [PATCH] feat: Add CLI command for gathering proving metrics --- yarn-project/archiver/src/archiver/index.ts | 1 + yarn-project/cli/package.json | 5 +- yarn-project/cli/src/cmds/l1/index.ts | 23 ++++++++- yarn-project/cli/src/cmds/l1/prover_stats.ts | 50 ++++++++++++++++++++ yarn-project/yarn.lock | 2 + 5 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 yarn-project/cli/src/cmds/l1/prover_stats.ts diff --git a/yarn-project/archiver/src/archiver/index.ts b/yarn-project/archiver/src/archiver/index.ts index 6d1c72a21baf..ce77cc7bdfb4 100644 --- a/yarn-project/archiver/src/archiver/index.ts +++ b/yarn-project/archiver/src/archiver/index.ts @@ -5,3 +5,4 @@ export { MemoryArchiverStore } from './memory_archiver_store/memory_archiver_sto export { ArchiverDataStore } from './archiver_store.js'; export { KVArchiverDataStore } from './kv_archiver_store/kv_archiver_store.js'; export { ContractInstanceStore } from './kv_archiver_store/contract_instance_store.js'; +export { retrieveL2ProofVerifiedEvents } from './structs/data_retrieval.js'; diff --git a/yarn-project/cli/package.json b/yarn-project/cli/package.json index 4a6033e3a5f8..08b2a4a4b9ab 100644 --- a/yarn-project/cli/package.json +++ b/yarn-project/cli/package.json @@ -63,6 +63,7 @@ ] }, "dependencies": { + "@aztec/archiver": "workspace:^", "@aztec/aztec.js": "workspace:^", "@aztec/circuit-types": "workspace:^", "@aztec/foundation": "workspace:^", @@ -71,6 +72,7 @@ "@iarna/toml": "^2.2.5", "@libp2p/peer-id-factory": "^3.0.4", "commander": "^12.1.0", + "lodash.groupby": "^4.6.0", "semver": "^7.5.4", "solc": "^0.8.26", "source-map-support": "^0.5.21", @@ -84,6 +86,7 @@ "@aztec/protocol-contracts": "workspace:^", "@jest/globals": "^29.5.0", "@types/jest": "^29.5.0", + "@types/lodash.groupby": "^4.6.9", "@types/lodash.startcase": "^4.4.7", "@types/node": "^18.7.23", "@types/semver": "^7.5.2", @@ -113,4 +116,4 @@ "engines": { "node": ">=18" } -} +} \ No newline at end of file diff --git a/yarn-project/cli/src/cmds/l1/index.ts b/yarn-project/cli/src/cmds/l1/index.ts index 4ebfeb587ba3..f3ca73f36831 100644 --- a/yarn-project/cli/src/cmds/l1/index.ts +++ b/yarn-project/cli/src/cmds/l1/index.ts @@ -159,7 +159,7 @@ export function injectCommands(program: Command, log: LogFn, debugLogger: DebugL }); program - .command('set-proven-until') + .command('set-proven-until', { hidden: true }) .description( 'Instructs the L1 rollup contract to assume all blocks until the given number are automatically proven.', ) @@ -190,5 +190,26 @@ export function injectCommands(program: Command, log: LogFn, debugLogger: DebugL ); }); + program + .command('prover-stats', { hidden: true }) + .requiredOption( + '--l1-rpc-url ', + 'Url of the ethereum host. Chain identifiers localhost and testnet can be used', + ETHEREUM_HOST, + ) + .addOption(l1ChainIdOption) + .option('--start-block ', 'The block number to start from', parseBigint, 1n) + .option('--batch-size ', 'The number of blocks to query in each batch', parseBigint, 100n) + .option('--l1-rollup-address ', 'Address of the rollup contract (required if node URL is not set)') + .option( + '--node-url ', + 'JSON RPC URL of an Aztec node to retrieve the rollup contract address (required if L1 rollup address is not set)', + ) + .action(async options => { + const { proverStats } = await import('./prover_stats.js'); + const { l1RpcUrl, chainId, l1RollupAddress, startBlock, batchSize, nodeUrl } = options; + await proverStats({ l1RpcUrl, chainId, l1RollupAddress, startBlock, batchSize, nodeUrl, log }); + }); + return program; } diff --git a/yarn-project/cli/src/cmds/l1/prover_stats.ts b/yarn-project/cli/src/cmds/l1/prover_stats.ts new file mode 100644 index 000000000000..4cda8a13a455 --- /dev/null +++ b/yarn-project/cli/src/cmds/l1/prover_stats.ts @@ -0,0 +1,50 @@ +import { retrieveL2ProofVerifiedEvents } from '@aztec/archiver'; +import { createAztecNodeClient } from '@aztec/circuit-types'; +import { EthAddress } from '@aztec/circuits.js'; +import { createEthereumChain } from '@aztec/ethereum'; +import { type LogFn, createDebugLogger } from '@aztec/foundation/log'; + +import groupBy from 'lodash.groupby'; +import { createPublicClient, http } from 'viem'; + +export async function proverStats(opts: { + l1RpcUrl: string; + chainId: number; + l1RollupAddress: string | undefined; + nodeUrl: string | undefined; + log: LogFn; + startBlock: bigint; + batchSize: bigint; +}) { + const debugLog = createDebugLogger('aztec:cli:prover_stats'); + const { startBlock, chainId, l1RpcUrl, l1RollupAddress, batchSize, nodeUrl, log } = opts; + if (!l1RollupAddress && !nodeUrl) { + throw new Error('Either L1 rollup address or node URL must be set'); + } + const rollup = l1RollupAddress + ? EthAddress.fromString(l1RollupAddress) + : await createAztecNodeClient(nodeUrl!) + .getL1ContractAddresses() + .then(a => a.rollupAddress); + + const chain = createEthereumChain(l1RpcUrl, chainId).chainInfo; + const publicClient = createPublicClient({ chain, transport: http(l1RpcUrl) }); + const lastBlockNum = await publicClient.getBlockNumber(); + debugLog.verbose(`Querying events on rollup at ${rollup.toString()} from ${startBlock} up to ${lastBlockNum}`); + + let blockNum = startBlock; + const events = []; + while (blockNum <= lastBlockNum) { + const end = blockNum + batchSize > lastBlockNum + 1n ? lastBlockNum + 1n : blockNum + batchSize; + debugLog.verbose(`Querying events from block ${blockNum} to ${end}`); + const newEvents = await retrieveL2ProofVerifiedEvents(publicClient, rollup, blockNum, end); + events.push(...newEvents); + debugLog.verbose(`Got ${newEvents.length} events`); + blockNum += batchSize; + } + + const stats = groupBy(events, 'proverId'); + for (const proverId in stats) { + log(`${proverId}, ${stats[proverId].length}`); + } +} diff --git a/yarn-project/yarn.lock b/yarn-project/yarn.lock index 2f89126d3f8b..6bcaba8954c2 100644 --- a/yarn-project/yarn.lock +++ b/yarn-project/yarn.lock @@ -460,6 +460,7 @@ __metadata: "@jest/globals": ^29.5.0 "@libp2p/peer-id-factory": ^3.0.4 "@types/jest": ^29.5.0 + "@types/lodash.groupby": ^4.6.9 "@types/lodash.startcase": ^4.4.7 "@types/node": ^18.7.23 "@types/semver": ^7.5.2 @@ -467,6 +468,7 @@ __metadata: commander: ^12.1.0 jest: ^29.5.0 jest-mock-extended: ^3.0.5 + lodash.groupby: ^4.6.0 semver: ^7.5.4 solc: ^0.8.26 source-map-support: ^0.5.21