From f2179e37738021d627c7726562c609843a8ce6f2 Mon Sep 17 00:00:00 2001 From: Mark Tyneway Date: Tue, 23 Nov 2021 10:44:58 -0800 Subject: [PATCH 1/3] contracts: fetch-batches hardhat task Adds a hardhat task to fetch batches. Logs are written to stderr so that the stdout can be written to a file or passed to `jq`. ```bash export CONTRACTS_TARGET_NETWORK=... export CONTRACTS_DEPLOYER_KEY=... export CONTRACTS_RPC_URL=... npx hardhat fetch-batches --network main --start 14311261 ``` --- .changeset/few-pears-think.md | 5 ++ packages/contracts/hardhat.config.ts | 1 + packages/contracts/tasks/fetch-batches.ts | 59 +++++++++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 .changeset/few-pears-think.md create mode 100644 packages/contracts/tasks/fetch-batches.ts diff --git a/.changeset/few-pears-think.md b/.changeset/few-pears-think.md new file mode 100644 index 0000000000000..1c0880dffa042 --- /dev/null +++ b/.changeset/few-pears-think.md @@ -0,0 +1,5 @@ +--- +'@eth-optimism/contracts': patch +--- + +Add a fetch batches hardhat task diff --git a/packages/contracts/hardhat.config.ts b/packages/contracts/hardhat.config.ts index d788060988a63..a1d34d1ae95dd 100644 --- a/packages/contracts/hardhat.config.ts +++ b/packages/contracts/hardhat.config.ts @@ -20,6 +20,7 @@ import './tasks/validate-address-dictator' import './tasks/validate-chugsplash-dictator' import './tasks/whitelist' import './tasks/withdraw-fees' +import './tasks/fetch-batches' import 'hardhat-gas-reporter' import '@primitivefi/hardhat-dodoc' import 'hardhat-output-validator' diff --git a/packages/contracts/tasks/fetch-batches.ts b/packages/contracts/tasks/fetch-batches.ts new file mode 100644 index 0000000000000..fa40d743be060 --- /dev/null +++ b/packages/contracts/tasks/fetch-batches.ts @@ -0,0 +1,59 @@ +import { ethers } from 'ethers' +import { task } from 'hardhat/config' +import * as types from 'hardhat/internal/core/params/argumentTypes' +import { SequencerBatch } from '@eth-optimism/core-utils' + +import { names } from '../src/address-names' +import { getContractFromArtifact } from '../src/deploy-utils' + +// Need to export env vars +// CONTRACTS_TARGET_NETWORK +// CONTRACTS_DEPLOYER_KEY +// CONTRACTS_RPC_URL +task('fetch-batches') + .addOptionalParam( + 'contractsRpcUrl', + 'Ethereum HTTP Endpoint', + process.env.CONTRACTS_RPC_URL || 'http://127.0.0.1:8545', + types.string + ) + .addOptionalParam('start', 'Start block height', 0, types.int) + .addOptionalParam('end', 'End block height', undefined, types.int) + .setAction(async (args, hre) => { + const provider = new ethers.providers.StaticJsonRpcProvider( + args.contractsRpcUrl + ) + + let CanonicalTransactionChain = await getContractFromArtifact( + hre, + names.managed.contracts.CanonicalTransactionChain + ) + CanonicalTransactionChain = CanonicalTransactionChain.connect(provider) + + const start = args.start + let end = args.end + if (!end) { + end = await provider.getBlockNumber() + } + + const batches = [] + + for (let i = start; i < end; i += 2001) { + const tip = Math.min(i + 2000, end) + console.error(`Querying events ${i}-${tip}`) + + const events = await CanonicalTransactionChain.queryFilter( + CanonicalTransactionChain.filters.SequencerBatchAppended(), + i, + tip + ) + + for (const event of events) { + const tx = await provider.getTransaction(event.transactionHash) + const batch = (SequencerBatch as any).fromHex(tx.data) + batches.push(batch.toJSON()) + } + } + + console.log(JSON.stringify(batches, null, 2)) + }) From 5a6f539c268504f60ca60f3ba57720ca0d690fec Mon Sep 17 00:00:00 2001 From: Mark Tyneway Date: Wed, 2 Mar 2022 18:34:21 -0800 Subject: [PATCH 2/3] core-utils: add `toJSON` methods on batch primitives Allows for easy serialization and display in frontends or for data collection --- .changeset/soft-dogs-hide.md | 5 +++ .../core-utils/src/optimism/batch-encoding.ts | 39 +++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 .changeset/soft-dogs-hide.md diff --git a/.changeset/soft-dogs-hide.md b/.changeset/soft-dogs-hide.md new file mode 100644 index 0000000000000..1251c9d8efb66 --- /dev/null +++ b/.changeset/soft-dogs-hide.md @@ -0,0 +1,5 @@ +--- +'@eth-optimism/core-utils': patch +--- + +Add toJSON methods to the batch primitives diff --git a/packages/core-utils/src/optimism/batch-encoding.ts b/packages/core-utils/src/optimism/batch-encoding.ts index 4a57d7ff8d7f9..69e261a9be11e 100644 --- a/packages/core-utils/src/optimism/batch-encoding.ts +++ b/packages/core-utils/src/optimism/batch-encoding.ts @@ -149,6 +149,15 @@ export class Context extends Struct { this.blockNumber = br.readU40BE() return this } + + toJSON() { + return { + numSequencedTransactions: this.numSequencedTransactions, + numSubsequentQueueTransactions: this.numSubsequentQueueTransactions, + timestamp: this.timestamp, + blockNumber: this.blockNumber, + } + } } // transaction @@ -229,6 +238,27 @@ export class BatchedTx extends Struct { ) } + toJSON() { + if (!this.tx) { + this.tx = parse(this.raw) + } + + return { + nonce: this.tx.nonce, + gasPrice: this.tx.gasPrice.toString(), + gasLimit: this.tx.gasLimit.toString(), + to: this.tx.to, + value: this.tx.value.toString(), + data: this.tx.data, + v: this.tx.v, + r: this.tx.r, + s: this.tx.s, + chainId: this.tx.chainId, + hash: this.tx.hash, + from: this.tx.from, + } + } + // TODO: inconsistent API with toTransaction // but unnecessary right now // this should be fromHexTransaction @@ -390,4 +420,13 @@ export class SequencerBatch extends Struct { toHex(): string { return '0x' + this.encode().toString('hex') } + + toJSON() { + return { + shouldStartAtElement: this.shouldStartAtElement, + totalElementsToAppend: this.totalElementsToAppend, + contexts: this.contexts.map((c) => c.toJSON()), + transactions: this.transactions.map((tx) => tx.toJSON()), + } + } } From 4106fd55fc99a0c8bcd74173b3252b6c648db66f Mon Sep 17 00:00:00 2001 From: Mark Tyneway Date: Thu, 3 Mar 2022 11:36:58 -0800 Subject: [PATCH 3/3] contracts: fix bug in fetch batches script --- packages/contracts/tasks/fetch-batches.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/contracts/tasks/fetch-batches.ts b/packages/contracts/tasks/fetch-batches.ts index fa40d743be060..dcf9ca671b988 100644 --- a/packages/contracts/tasks/fetch-batches.ts +++ b/packages/contracts/tasks/fetch-batches.ts @@ -38,7 +38,7 @@ task('fetch-batches') const batches = [] - for (let i = start; i < end; i += 2001) { + for (let i = start; i <= end; i += 2001) { const tip = Math.min(i + 2000, end) console.error(`Querying events ${i}-${tip}`)