From e7cf178195e30c7ddf3625b3ca8784a2c61b156f Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Mon, 20 Jan 2025 13:53:20 +0100 Subject: [PATCH 1/3] chore: use correct compressed size estimation fn --- Cargo.lock | 1 + Cargo.toml | 4 +++- crates/optimism/node/Cargo.toml | 1 + crates/optimism/node/src/txpool.rs | 25 +++++++++++++++++++------ 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7e7c5ff87dc0..939863e05c8f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8333,6 +8333,7 @@ dependencies = [ "derive_more", "eyre", "futures", + "maili-flz", "op-alloy-consensus", "op-alloy-rpc-types-engine", "parking_lot", diff --git a/Cargo.toml b/Cargo.toml index 42a94ea63f63..d2186ac016cf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -472,9 +472,11 @@ alloy-transport-ws = { version = "0.9.2", default-features = false } # op op-alloy-rpc-types = { version = "0.9.0", default-features = false } op-alloy-rpc-types-engine = { version = "0.9.0", default-features = false } -maili-rpc = { version = "0.1.6", default-features = false } op-alloy-network = { version = "0.9.0", default-features = false } op-alloy-consensus = { version = "0.9.0", default-features = false } +## op-maili +maili-rpc = { version = "0.1.6", default-features = false } +maili-flz = { version = "0.1.6", default-features = false } # misc aquamarine = "0.6" diff --git a/crates/optimism/node/Cargo.toml b/crates/optimism/node/Cargo.toml index 8a597b9ae8a7..3910b7feccbb 100644 --- a/crates/optimism/node/Cargo.toml +++ b/crates/optimism/node/Cargo.toml @@ -52,6 +52,7 @@ alloy-eips.workspace = true alloy-primitives.workspace = true op-alloy-consensus.workspace = true op-alloy-rpc-types-engine.workspace = true +maili-flz.workspace = true alloy-rpc-types-engine.workspace = true alloy-consensus.workspace = true diff --git a/crates/optimism/node/src/txpool.rs b/crates/optimism/node/src/txpool.rs index e2f50cb09d98..40ab9c75d8ec 100644 --- a/crates/optimism/node/src/txpool.rs +++ b/crates/optimism/node/src/txpool.rs @@ -45,7 +45,7 @@ pub struct OpPooledTransaction { #[deref] inner: EthPooledTransaction, /// The estimated size of this transaction, lazily computed. - estimated_tx_compressed_size: OnceLock, + estimated_tx_compressed_size: OnceLock, } impl OpPooledTransaction { @@ -58,14 +58,27 @@ impl OpPooledTransaction { } /// Returns the estimated compressed size of a transaction in bytes scaled by 1e6. - // This value is computed based on the following formula: - // `max(minTransactionSize, intercept + fastlzCoef*fastlzSize)` - pub fn estimated_compressed_size(&self) -> u32 { - // TODO(mattsse): use standalone flz compute function - *self.estimated_tx_compressed_size.get_or_init(|| self.inner.encoded_length as u32) + /// This value is computed based on the following formula: + /// `max(minTransactionSize, intercept + fastlzCoef*fastlzSize)` + pub fn estimated_compressed_size(&self) -> &U256 { + self.estimated_tx_compressed_size + .get_or_init(|| tx_estimated_size_fjord(&self.inner.transaction().encoded_2718())) } } +/// Calculate the estimated compressed transaction size in bytes, scaled by 1e6. +/// This value is computed based on the following formula: +/// max(minTransactionSize, intercept + fastlzCoef*fastlzSize) +// TODO(mattsse): replace with library fn from revm or maili once available +fn tx_estimated_size_fjord(input: &[u8]) -> U256 { + let fastlz_size = U256::from(maili_flz::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)) +} + impl From> for OpPooledTransaction { fn from(tx: RecoveredTx) -> Self { let encoded_len = tx.encode_2718_len(); From 87069fb20af11ff9181fabc6ba173b6845920db7 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Mon, 20 Jan 2025 14:05:38 +0100 Subject: [PATCH 2/3] use u32 --- crates/optimism/node/src/txpool.rs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/crates/optimism/node/src/txpool.rs b/crates/optimism/node/src/txpool.rs index 40ab9c75d8ec..31ca4ffd1b25 100644 --- a/crates/optimism/node/src/txpool.rs +++ b/crates/optimism/node/src/txpool.rs @@ -45,7 +45,7 @@ pub struct OpPooledTransaction { #[deref] inner: EthPooledTransaction, /// The estimated size of this transaction, lazily computed. - estimated_tx_compressed_size: OnceLock, + estimated_tx_compressed_size: OnceLock, } impl OpPooledTransaction { @@ -60,8 +60,9 @@ impl OpPooledTransaction { /// Returns the estimated compressed size of a transaction in bytes scaled by 1e6. /// This value is computed based on the following formula: /// `max(minTransactionSize, intercept + fastlzCoef*fastlzSize)` - pub fn estimated_compressed_size(&self) -> &U256 { - self.estimated_tx_compressed_size + pub fn estimated_compressed_size(&self) -> u32 { + *self + .estimated_tx_compressed_size .get_or_init(|| tx_estimated_size_fjord(&self.inner.transaction().encoded_2718())) } } @@ -70,13 +71,10 @@ impl OpPooledTransaction { /// This value is computed based on the following formula: /// max(minTransactionSize, intercept + fastlzCoef*fastlzSize) // TODO(mattsse): replace with library fn from revm or maili once available -fn tx_estimated_size_fjord(input: &[u8]) -> U256 { - let fastlz_size = U256::from(maili_flz::flz_compress_len(input)); +fn tx_estimated_size_fjord(input: &[u8]) -> u32 { + let fastlz_size = maili_flz::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)) + fastlz_size.saturating_mul(836_500).saturating_sub(42_585_600).max(100_000_000) } impl From> for OpPooledTransaction { From d44251468a10b7d46cc6583093f2cc3416538f54 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Mon, 20 Jan 2025 14:22:41 +0100 Subject: [PATCH 3/3] use u64 --- crates/optimism/node/src/txpool.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/optimism/node/src/txpool.rs b/crates/optimism/node/src/txpool.rs index 31ca4ffd1b25..51506e7a90d0 100644 --- a/crates/optimism/node/src/txpool.rs +++ b/crates/optimism/node/src/txpool.rs @@ -45,7 +45,7 @@ pub struct OpPooledTransaction { #[deref] inner: EthPooledTransaction, /// The estimated size of this transaction, lazily computed. - estimated_tx_compressed_size: OnceLock, + estimated_tx_compressed_size: OnceLock, } impl OpPooledTransaction { @@ -60,7 +60,7 @@ impl OpPooledTransaction { /// Returns the estimated compressed size of a transaction in bytes scaled by 1e6. /// This value is computed based on the following formula: /// `max(minTransactionSize, intercept + fastlzCoef*fastlzSize)` - pub fn estimated_compressed_size(&self) -> u32 { + pub fn estimated_compressed_size(&self) -> u64 { *self .estimated_tx_compressed_size .get_or_init(|| tx_estimated_size_fjord(&self.inner.transaction().encoded_2718())) @@ -71,8 +71,8 @@ impl OpPooledTransaction { /// This value is computed based on the following formula: /// max(minTransactionSize, intercept + fastlzCoef*fastlzSize) // TODO(mattsse): replace with library fn from revm or maili once available -fn tx_estimated_size_fjord(input: &[u8]) -> u32 { - let fastlz_size = maili_flz::flz_compress_len(input); +fn tx_estimated_size_fjord(input: &[u8]) -> u64 { + let fastlz_size = maili_flz::flz_compress_len(input) as u64; fastlz_size.saturating_mul(836_500).saturating_sub(42_585_600).max(100_000_000) }