Skip to content

Commit 8cb2125

Browse files
authored
feat: add cast da-estimate (#10588)
* feat: add cast da-estimate * use sh-println
1 parent f6af152 commit 8cb2125

File tree

8 files changed

+82
-3
lines changed

8 files changed

+82
-3
lines changed

Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ alloy-trie = "0.8.1"
247247
## op-alloy
248248
op-alloy-consensus = "0.16.0"
249249
op-alloy-rpc-types = "0.16.0"
250+
op-alloy-flz = "0.13.0"
250251

251252
## revm
252253
revm = { version = "23.1.0", default-features = false }

crates/cast/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ alloy-sol-types.workspace = true
5454
alloy-transport.workspace = true
5555
alloy-ens = { workspace = true, features = ["provider"] }
5656

57+
op-alloy-flz.workspace = true
58+
op-alloy-consensus = { workspace = true, features = ["alloy-compat"] }
59+
5760
chrono.workspace = true
5861
eyre.workspace = true
5962
futures.workspace = true

crates/cast/src/args.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,9 @@ pub async fn run_command(args: CastArgs) -> Result<()> {
715715
}
716716
}
717717
CastSubcommand::TxPool { command } => command.run().await?,
718+
CastSubcommand::DAEstimate(cmd) => {
719+
cmd.run().await?;
720+
}
718721
};
719722

720723
/// Prints slice of tokens using [`format_tokens`] or [`format_tokens_raw`] depending whether

crates/cast/src/cmd/da_estimate.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//! Estimates the data availability size of a block for opstack.
2+
3+
use alloy_consensus::BlockHeader;
4+
use alloy_network::eip2718::Encodable2718;
5+
use alloy_provider::Provider;
6+
use alloy_rpc_types::BlockId;
7+
use clap::Parser;
8+
use foundry_cli::{
9+
opts::RpcOpts,
10+
utils::{self, LoadConfig},
11+
};
12+
use op_alloy_consensus::OpTxEnvelope;
13+
14+
/// CLI arguments for `cast da-estimate`.
15+
#[derive(Debug, Parser)]
16+
pub struct DAEstimateArgs {
17+
/// The block to estimate the data availability size for.
18+
pub block: BlockId,
19+
#[command(flatten)]
20+
pub rpc: RpcOpts,
21+
}
22+
23+
impl DAEstimateArgs {
24+
/// Load the RPC URL from the config file.
25+
pub async fn run(self) -> eyre::Result<()> {
26+
let Self { block, rpc } = self;
27+
let config = rpc.load_config()?;
28+
let provider = utils::get_provider(&config)?;
29+
let block = provider
30+
.get_block(block)
31+
.full()
32+
.await?
33+
.ok_or_else(|| eyre::eyre!("Block not found"))?;
34+
35+
let block_number = block.header.number();
36+
let tx_count = block.transactions.len();
37+
let mut da_estimate = 0;
38+
for tx in block.into_transactions_iter() {
39+
// try to convert into opstack transaction
40+
let tx = OpTxEnvelope::try_from(tx)?;
41+
da_estimate += op_alloy_flz::tx_estimated_size_fjord(&tx.encoded_2718());
42+
}
43+
44+
sh_println!("Estimated data availability size for block {block_number} with {tx_count} transactions: {da_estimate}")?;
45+
46+
Ok(())
47+
}
48+
}

crates/cast/src/cmd/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub mod call;
1212
pub mod constructor_args;
1313
pub mod create2;
1414
pub mod creation_code;
15+
pub mod da_estimate;
1516
pub mod estimate;
1617
pub mod find_block;
1718
pub mod interface;

crates/cast/src/opts.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use crate::cmd::{
22
access_list::AccessListArgs, artifact::ArtifactArgs, bind::BindArgs, call::CallArgs,
33
constructor_args::ConstructorArgsArgs, create2::Create2Args, creation_code::CreationCodeArgs,
4-
estimate::EstimateArgs, find_block::FindBlockArgs, interface::InterfaceArgs, logs::LogsArgs,
5-
mktx::MakeTxArgs, rpc::RpcArgs, run::RunArgs, send::SendTxArgs, storage::StorageArgs,
6-
txpool::TxPoolSubcommands, wallet::WalletSubcommands,
4+
da_estimate::DAEstimateArgs, estimate::EstimateArgs, find_block::FindBlockArgs,
5+
interface::InterfaceArgs, logs::LogsArgs, mktx::MakeTxArgs, rpc::RpcArgs, run::RunArgs,
6+
send::SendTxArgs, storage::StorageArgs, txpool::TxPoolSubcommands, wallet::WalletSubcommands,
77
};
88
use alloy_ens::NameOrAddress;
99
use alloy_primitives::{Address, Selector, B256, U256};
@@ -1065,6 +1065,9 @@ pub enum CastSubcommand {
10651065
#[command(subcommand)]
10661066
command: TxPoolSubcommands,
10671067
},
1068+
/// Estimates the data availability size of a given opstack block.
1069+
#[command(name = "da-estimate")]
1070+
DAEstimate(DAEstimateArgs),
10681071
}
10691072

10701073
/// CLI arguments for `cast --to-base`.

crates/cast/tests/cli/main.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2616,3 +2616,13 @@ Error: Failed to estimate gas: server returned an error response: error code 3:
26162616
26172617
"#]]);
26182618
});
2619+
2620+
// <https://basescan.org/block/30558838>
2621+
casttest!(estimate_base_da, |_prj, cmd| {
2622+
cmd.args(["da-estimate", "30558838", "-r", "https://mainnet.base.org/"])
2623+
.assert_success()
2624+
.stdout_eq(str![[r#"
2625+
Estimated data availability size for block 30558838 with 225 transactions: 52916546100
2626+
2627+
"#]]);
2628+
});

0 commit comments

Comments
 (0)