Skip to content

Commit

Permalink
feat: extract and export estimate_tx_compressed_size (bluealloy#1985)
Browse files Browse the repository at this point in the history
* feat: extract and export `estimate_tx_compressed_size`

* Lint
  • Loading branch information
meyer9 authored Jan 10, 2025
1 parent a2451cd commit 9bef7d9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
21 changes: 2 additions & 19 deletions crates/optimism/src/l1block.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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");

/// <https://github.com/ethereum-optimism/op-geth/blob/647c346e2bef36219cc7b47d76b1cb87e7ca29e4/core/types/rollup_cost.go#L79>
const L1_COST_FASTLZ_COEF: u64 = 836_500;

/// <https://github.com/ethereum-optimism/op-geth/blob/647c346e2bef36219cc7b47d76b1cb87e7ca29e4/core/types/rollup_cost.go#L78>
/// Inverted to be used with `saturating_sub`.
const L1_COST_INTERCEPT: u64 = 42_585_600;

/// <https://github.com/ethereum-optimism/op-geth/blob/647c346e2bef36219cc7b47d76b1cb87e7ca29e4/core/types/rollup_cost.go#82>
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`
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion crates/optimism/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
22 changes: 22 additions & 0 deletions crates/optimism/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,25 @@ pub mod error;

pub use abstraction::{OpTransaction, OpTxTrait};
pub use error::OpTransactionError;

use crate::fast_lz::flz_compress_len;

/// <https://github.com/ethereum-optimism/op-geth/blob/647c346e2bef36219cc7b47d76b1cb87e7ca29e4/core/types/rollup_cost.go#L79>
const L1_COST_FASTLZ_COEF: u64 = 836_500;

/// <https://github.com/ethereum-optimism/op-geth/blob/647c346e2bef36219cc7b47d76b1cb87e7ca29e4/core/types/rollup_cost.go#L78>
/// Inverted to be used with `saturating_sub`.
const L1_COST_INTERCEPT: u64 = 42_585_600;

/// <https://github.com/ethereum-optimism/op-geth/blob/647c346e2bef36219cc7b47d76b1cb87e7ca29e4/core/types/rollup_cost.go#82>
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)
}

0 comments on commit 9bef7d9

Please sign in to comment.