From 9bef7d903bbabf0dafff8b5de85d40654adf4cbd Mon Sep 17 00:00:00 2001 From: Julian Meyer Date: Fri, 10 Jan 2025 02:38:25 -0800 Subject: [PATCH] feat: extract and export `estimate_tx_compressed_size` (#1985) * feat: extract and export `estimate_tx_compressed_size` * Lint --- crates/optimism/src/l1block.rs | 21 ++------------------- crates/optimism/src/lib.rs | 2 +- crates/optimism/src/transaction.rs | 22 ++++++++++++++++++++++ 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/crates/optimism/src/l1block.rs b/crates/optimism/src/l1block.rs index 4ea6f47449..95e935f04b 100644 --- a/crates/optimism/src/l1block.rs +++ b/crates/optimism/src/l1block.rs @@ -1,4 +1,4 @@ -use crate::{fast_lz::flz_compress_len, OpSpecId}; +use crate::{transaction::estimate_tx_compressed_size, OpSpecId}; use core::ops::Mul; use revm::{ context_interface::Journal, @@ -43,16 +43,6 @@ pub const BASE_FEE_RECIPIENT: Address = address!("420000000000000000000000000000 /// The address of the L1Block contract. pub const L1_BLOCK_CONTRACT: Address = address!("4200000000000000000000000000000000000015"); -/// -const L1_COST_FASTLZ_COEF: u64 = 836_500; - -/// -/// Inverted to be used with `saturating_sub`. -const L1_COST_INTERCEPT: u64 = 42_585_600; - -/// -const MIN_TX_SIZE_SCALED: u64 = 100 * 1_000_000; - /// L1 block info /// /// We can extract L1 epoch data from each L2 block, by looking at the `setL1BlockValues` @@ -171,14 +161,7 @@ impl L1BlockInfo { // This value is computed based on the following formula: // max(minTransactionSize, intercept + fastlzCoef*fastlzSize) fn tx_estimated_size_fjord(&self, input: &[u8]) -> U256 { - let fastlz_size = flz_compress_len(input) as u64; - - U256::from( - fastlz_size - .saturating_mul(L1_COST_FASTLZ_COEF) - .saturating_sub(L1_COST_INTERCEPT) - .max(MIN_TX_SIZE_SCALED), - ) + U256::from(estimate_tx_compressed_size(input)) } /// Calculate the gas cost of a transaction based on L1 block data posted on L2, depending on the [OpSpec] passed. diff --git a/crates/optimism/src/lib.rs b/crates/optimism/src/lib.rs index ada77d4fa7..2468597be1 100644 --- a/crates/optimism/src/lib.rs +++ b/crates/optimism/src/lib.rs @@ -19,4 +19,4 @@ pub use l1block::{ }; pub use result::OptimismHaltReason; pub use spec::*; -pub use transaction::{error::OpTransactionError, OpTransaction}; +pub use transaction::{error::OpTransactionError, estimate_tx_compressed_size, OpTransaction}; diff --git a/crates/optimism/src/transaction.rs b/crates/optimism/src/transaction.rs index 813dc425ae..3a955d3956 100644 --- a/crates/optimism/src/transaction.rs +++ b/crates/optimism/src/transaction.rs @@ -4,3 +4,25 @@ pub mod error; pub use abstraction::{OpTransaction, OpTxTrait}; pub use error::OpTransactionError; + +use crate::fast_lz::flz_compress_len; + +/// +const L1_COST_FASTLZ_COEF: u64 = 836_500; + +/// +/// Inverted to be used with `saturating_sub`. +const L1_COST_INTERCEPT: u64 = 42_585_600; + +/// +const MIN_TX_SIZE_SCALED: u64 = 100 * 1_000_000; + +/// Estimates the compressed size of a transaction. +pub fn estimate_tx_compressed_size(input: &[u8]) -> u64 { + let fastlz_size = flz_compress_len(input) as u64; + + fastlz_size + .saturating_mul(L1_COST_FASTLZ_COEF) + .saturating_sub(L1_COST_INTERCEPT) + .max(MIN_TX_SIZE_SCALED) +}