From a002459048dceca64ce910b08ff71dcb5a3cdefd Mon Sep 17 00:00:00 2001 From: Hai | RISE <150876604+hai-rise@users.noreply.github.com> Date: Wed, 20 Nov 2024 14:45:58 +0700 Subject: [PATCH] chore: refactor L1BlockInfo::tx_estimated_size_fjord (#1862) --- crates/revm/src/optimism/fast_lz.rs | 4 +++- crates/revm/src/optimism/l1block.rs | 24 ++++++++++++++++++------ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/crates/revm/src/optimism/fast_lz.rs b/crates/revm/src/optimism/fast_lz.rs index cf715bf3cb..91a447c7f4 100644 --- a/crates/revm/src/optimism/fast_lz.rs +++ b/crates/revm/src/optimism/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/revm/src/optimism/l1block.rs b/crates/revm/src/optimism/l1block.rs index d7195c456b..cfe3e6bb9c 100644 --- a/crates/revm/src/optimism/l1block.rs +++ b/crates/revm/src/optimism/l1block.rs @@ -35,6 +35,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` @@ -153,12 +163,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 [SpecId] passed.