From d75c02e22bbc4a08d7217c0f29ae26798fc7fc49 Mon Sep 17 00:00:00 2001 From: Hai Nguyen Date: Tue, 19 Nov 2024 10:14:04 +0700 Subject: [PATCH] chore: refactor L1BlockInfo::tx_estimated_size_fjord --- crates/optimism/src/fast_lz.rs | 4 +++- crates/optimism/src/l1block.rs | 24 ++++++++++++++++++------ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/crates/optimism/src/fast_lz.rs b/crates/optimism/src/fast_lz.rs index 847170531e..67eb7be0f7 100644 --- a/crates/optimism/src/fast_lz.rs +++ b/crates/optimism/src/fast_lz.rs @@ -1,5 +1,7 @@ /// Returns the length of the data after compression through FastLZ, based on -// https://github.com/Vectorized/solady/blob/5315d937d79b335c668896d7533ac603adac5315/js/solady.js +/// +/// The u32s match op-geth's Go port: +/// pub(crate) fn flz_compress_len(input: &[u8]) -> u32 { let mut idx: u32 = 2; diff --git a/crates/optimism/src/l1block.rs b/crates/optimism/src/l1block.rs index 5fdc9c794e..a510625d81 100644 --- a/crates/optimism/src/l1block.rs +++ b/crates/optimism/src/l1block.rs @@ -40,6 +40,16 @@ 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` @@ -161,12 +171,14 @@ 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 = U256::from(flz_compress_len(input)); - - fastlz_size - .saturating_mul(U256::from(836_500)) - .saturating_sub(U256::from(42_585_600)) - .max(U256::from(100_000_000)) + 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), + ) } /// Calculate the gas cost of a transaction based on L1 block data posted on L2, depending on the [OptimismSpecId] passed.