From 99d31992dbb250019e0e08a533becb93c3e2f690 Mon Sep 17 00:00:00 2001 From: Rohit Narurkar Date: Tue, 10 Jun 2025 21:48:44 +0100 Subject: [PATCH 1/4] feat: rollup-fee updated for spec=feynman --- src/l1block.rs | 86 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 85 insertions(+), 1 deletion(-) diff --git a/src/l1block.rs b/src/l1block.rs index c52c422..0bbacba 100644 --- a/src/l1block.rs +++ b/src/l1block.rs @@ -42,6 +42,12 @@ const L1_COMMIT_SCALAR_SLOT: U256 = U256::from_limbs([6u64, 0, 0, 0]); /// The L1 blob scalar storage slot. const L1_BLOB_SCALAR_SLOT: U256 = U256::from_limbs([7u64, 0, 0, 0]); +/// The L1 compression scalar storage slot. +const L1_COMPRESSION_SCALAR_SLOT: U256 = U256::from_limbs([8u64, 0, 0, 0]); + +/// The L1 proof verification scalar storage slot. +const L1_VERIFICATION_SCALAR_SLOT: U256 = U256::from_limbs([9u64, 0, 0, 0]); + // L1 BLOCK INFO // ================================================================================================ @@ -65,6 +71,10 @@ pub struct L1BlockInfo { pub l1_blob_scalar: Option, /// The current call data gas (l1_blob_scalar * l1_base_fee), None if before Curie. pub calldata_gas: Option, + /// The current L1 DA compression scalar, None if before Feynman. + pub l1_compression_scalar: Option, + /// The current L1 proof verification scalar, None if before Feynman. + pub l1_verification_scalar: Option, } impl L1BlockInfo { @@ -92,6 +102,25 @@ impl L1BlockInfo { let l1_blob_scalar = db.storage(L1_GAS_PRICE_ORACLE_ADDRESS, L1_BLOB_SCALAR_SLOT)?; let calldata_gas = l1_commit_scalar.saturating_mul(l1_base_fee); + // If Feynman is not enabled, return the L1 block info without Feynman fields. + if !spec_id.is_enabled_in(ScrollSpecId::FEYNMAN) { + return Ok(L1BlockInfo { + l1_base_fee, + l1_fee_overhead, + l1_base_fee_scalar, + l1_blob_base_fee: Some(l1_blob_base_fee), + l1_commit_scalar: Some(l1_commit_scalar), + l1_blob_scalar: Some(l1_blob_scalar), + calldata_gas: Some(calldata_gas), + ..Default::default() + }); + } + + let l1_compression_scalar = + db.storage(L1_GAS_PRICE_ORACLE_ADDRESS, L1_COMPRESSION_SCALAR_SLOT)?; + let l1_verification_scalar = + db.storage(L1_GAS_PRICE_ORACLE_ADDRESS, L1_VERIFICATION_SCALAR_SLOT)?; + Ok(L1BlockInfo { l1_base_fee, l1_fee_overhead, @@ -100,6 +129,8 @@ impl L1BlockInfo { l1_commit_scalar: Some(l1_commit_scalar), l1_blob_scalar: Some(l1_blob_scalar), calldata_gas: Some(calldata_gas), + l1_compression_scalar: Some(l1_compression_scalar), + l1_verification_scalar: Some(l1_verification_scalar), }) } @@ -136,12 +167,65 @@ impl L1BlockInfo { self.calldata_gas.unwrap().saturating_add(blob_gas).wrapping_div(TX_L1_FEE_PRECISION) } + fn calculate_tx_l1_cost_feynman(&self, input: &[u8], spec_id: ScrollSpecId) -> U256 { + // rollup_fee(tx) = compression_ratio(tx) * size(tx) * (component_exec + component_blob) + // + // - compression_ratio(tx): estimated compression ratio of RLP-encoded signed tx data, + // derived by plugging in this tx into the previous finalised batch. This gives us an idea + // as to how compressible is the zstd-encoding of the data in this tx. + // + // - size(tx): denotes the size of the RLP-encoded signed tx data. + // + // - component_exec: The component that accounts towards commiting this tx as part of a L2 + // batch as well as gas costs for the eventual on-chain proof verification. + // => (compression_scalar + commit_scalar + verification_scalar) * l1_base_fee + // + // - component_blob: The component that accounts the costs associated with data + // availability, i.e. the costs of posting this tx's data in the EIP-4844 blob. + // => (compression_scalar + blob_scalar) * l1_blob_base_fee + let component_exec = { + let compression_scalar = self + .l1_compression_scalar + .unwrap_or_else(|| panic!("compression scalar in spec_id={:?}", spec_id)); + let commit_scalar = self + .l1_commit_scalar + .unwrap_or_else(|| panic!("l1 commit scalar in spec_id={:?}", spec_id)); + let verification_scalar = self + .l1_verification_scalar + .unwrap_or_else(|| panic!("verification scalar in spec_id={:?}", spec_id)); + compression_scalar.saturating_add(commit_scalar).saturating_add(verification_scalar) + }; + let component_blob = { + let compression_scalar = self + .l1_compression_scalar + .unwrap_or_else(|| panic!("compression scalar in spec_id={:?}", spec_id)); + let blob_scalar = self + .l1_blob_scalar + .unwrap_or_else(|| panic!("l1 blob scalar in spec_id={:?}", spec_id)); + compression_scalar.saturating_add(blob_scalar) + }; + + // Assume compression_ratio = 1 until we have specification for estimating compression + // ratio based on previous finalised batches. + let compression_ratio = |_input: &[u8]| -> U256 { U256::ONE }; + + // size(tx) is just the length of the RLP-encoded signed tx data. + let tx_size = |input: &[u8]| -> U256 { U256::from(input.len()) }; + + compression_ratio(input) + .saturating_mul(tx_size(input)) + .saturating_mul(component_exec.saturating_add(component_blob)) + .wrapping_div(TX_L1_FEE_PRECISION) + } + /// Calculate the gas cost of a transaction based on L1 block data posted on L2. pub fn calculate_tx_l1_cost(&self, input: &[u8], spec_id: ScrollSpecId) -> U256 { if !spec_id.is_enabled_in(ScrollSpecId::CURIE) { self.calculate_tx_l1_cost_shanghai(input, spec_id) - } else { + } else if !spec_id.is_enabled_in(ScrollSpecId::FEYNMAN) { self.calculate_tx_l1_cost_curie(input, spec_id) + } else { + self.calculate_tx_l1_cost_feynman(input, spec_id) } } } From 4b74b246e694dd335732ca8483ce0dbcb644b80b Mon Sep 17 00:00:00 2001 From: Rohit Narurkar Date: Wed, 11 Jun 2025 09:54:39 +0100 Subject: [PATCH 2/4] fix: re-use old slots for exec and blob scalar --- src/l1block.rs | 59 +++++++++++++------------------------------------- 1 file changed, 15 insertions(+), 44 deletions(-) diff --git a/src/l1block.rs b/src/l1block.rs index 0bbacba..a032ad4 100644 --- a/src/l1block.rs +++ b/src/l1block.rs @@ -37,17 +37,13 @@ const L1_SCALAR_SLOT: U256 = U256::from_limbs([3u64, 0, 0, 0]); const L1_BLOB_BASE_FEE_SLOT: U256 = U256::from_limbs([5u64, 0, 0, 0]); /// The L1 commit scalar storage slot. +/// +/// Post-FEYNMAN this represents the exec_scalar. const L1_COMMIT_SCALAR_SLOT: U256 = U256::from_limbs([6u64, 0, 0, 0]); /// The L1 blob scalar storage slot. const L1_BLOB_SCALAR_SLOT: U256 = U256::from_limbs([7u64, 0, 0, 0]); -/// The L1 compression scalar storage slot. -const L1_COMPRESSION_SCALAR_SLOT: U256 = U256::from_limbs([8u64, 0, 0, 0]); - -/// The L1 proof verification scalar storage slot. -const L1_VERIFICATION_SCALAR_SLOT: U256 = U256::from_limbs([9u64, 0, 0, 0]); - // L1 BLOCK INFO // ================================================================================================ @@ -71,10 +67,6 @@ pub struct L1BlockInfo { pub l1_blob_scalar: Option, /// The current call data gas (l1_blob_scalar * l1_base_fee), None if before Curie. pub calldata_gas: Option, - /// The current L1 DA compression scalar, None if before Feynman. - pub l1_compression_scalar: Option, - /// The current L1 proof verification scalar, None if before Feynman. - pub l1_verification_scalar: Option, } impl L1BlockInfo { @@ -102,25 +94,6 @@ impl L1BlockInfo { let l1_blob_scalar = db.storage(L1_GAS_PRICE_ORACLE_ADDRESS, L1_BLOB_SCALAR_SLOT)?; let calldata_gas = l1_commit_scalar.saturating_mul(l1_base_fee); - // If Feynman is not enabled, return the L1 block info without Feynman fields. - if !spec_id.is_enabled_in(ScrollSpecId::FEYNMAN) { - return Ok(L1BlockInfo { - l1_base_fee, - l1_fee_overhead, - l1_base_fee_scalar, - l1_blob_base_fee: Some(l1_blob_base_fee), - l1_commit_scalar: Some(l1_commit_scalar), - l1_blob_scalar: Some(l1_blob_scalar), - calldata_gas: Some(calldata_gas), - ..Default::default() - }); - } - - let l1_compression_scalar = - db.storage(L1_GAS_PRICE_ORACLE_ADDRESS, L1_COMPRESSION_SCALAR_SLOT)?; - let l1_verification_scalar = - db.storage(L1_GAS_PRICE_ORACLE_ADDRESS, L1_VERIFICATION_SCALAR_SLOT)?; - Ok(L1BlockInfo { l1_base_fee, l1_fee_overhead, @@ -129,8 +102,6 @@ impl L1BlockInfo { l1_commit_scalar: Some(l1_commit_scalar), l1_blob_scalar: Some(l1_blob_scalar), calldata_gas: Some(calldata_gas), - l1_compression_scalar: Some(l1_compression_scalar), - l1_verification_scalar: Some(l1_verification_scalar), }) } @@ -179,30 +150,29 @@ impl L1BlockInfo { // - component_exec: The component that accounts towards commiting this tx as part of a L2 // batch as well as gas costs for the eventual on-chain proof verification. // => (compression_scalar + commit_scalar + verification_scalar) * l1_base_fee + // => (new_commit_scalar) * l1_base_fee // // - component_blob: The component that accounts the costs associated with data // availability, i.e. the costs of posting this tx's data in the EIP-4844 blob. // => (compression_scalar + blob_scalar) * l1_blob_base_fee + // => (new_blob_scalar) * l1_blob_base_fee + // + // Note that the same slots for L1_COMMIT_SCALAR_SLOT and L1_BLOB_SCALAR_SLOT are + // re-used/updated for the new values post-FEYNMAN. let component_exec = { - let compression_scalar = self - .l1_compression_scalar - .unwrap_or_else(|| panic!("compression scalar in spec_id={:?}", spec_id)); - let commit_scalar = self + let exec_scalar = self .l1_commit_scalar - .unwrap_or_else(|| panic!("l1 commit scalar in spec_id={:?}", spec_id)); - let verification_scalar = self - .l1_verification_scalar - .unwrap_or_else(|| panic!("verification scalar in spec_id={:?}", spec_id)); - compression_scalar.saturating_add(commit_scalar).saturating_add(verification_scalar) + .unwrap_or_else(|| panic!("exec scalar in spec_id={:?}", spec_id)); + exec_scalar.saturating_mul(self.l1_base_fee) }; let component_blob = { - let compression_scalar = self - .l1_compression_scalar - .unwrap_or_else(|| panic!("compression scalar in spec_id={:?}", spec_id)); let blob_scalar = self .l1_blob_scalar .unwrap_or_else(|| panic!("l1 blob scalar in spec_id={:?}", spec_id)); - compression_scalar.saturating_add(blob_scalar) + let blob_base_fee = self + .l1_blob_base_fee + .unwrap_or_else(|| panic!("l1 blob base fee in spec_id={:?}", spec_id)); + blob_scalar.saturating_mul(blob_base_fee) }; // Assume compression_ratio = 1 until we have specification for estimating compression @@ -216,6 +186,7 @@ impl L1BlockInfo { .saturating_mul(tx_size(input)) .saturating_mul(component_exec.saturating_add(component_blob)) .wrapping_div(TX_L1_FEE_PRECISION) + .wrapping_div(TX_L1_FEE_PRECISION) } /// Calculate the gas cost of a transaction based on L1 block data posted on L2. From 01b55cce18fed43bf93a3a883515c1ca8a10b350 Mon Sep 17 00:00:00 2001 From: Rohit Narurkar Date: Wed, 11 Jun 2025 15:15:30 +0100 Subject: [PATCH 3/4] chore: resolve comments --- src/l1block.rs | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/l1block.rs b/src/l1block.rs index a032ad4..ef9aaa0 100644 --- a/src/l1block.rs +++ b/src/l1block.rs @@ -141,16 +141,16 @@ impl L1BlockInfo { fn calculate_tx_l1_cost_feynman(&self, input: &[u8], spec_id: ScrollSpecId) -> U256 { // rollup_fee(tx) = compression_ratio(tx) * size(tx) * (component_exec + component_blob) // - // - compression_ratio(tx): estimated compression ratio of RLP-encoded signed tx data, - // derived by plugging in this tx into the previous finalised batch. This gives us an idea - // as to how compressible is the zstd-encoding of the data in this tx. + // - compression_ratio(tx): estimated compressibility of the signed tx data. The tx is + // eventually a part of a L2 batch that should likely result in a better compression ratio, + // however a conservative estimate is the size of zstd-encoding of the signed tx. // - // - size(tx): denotes the size of the RLP-encoded signed tx data. + // - size(tx): denotes the size of the signed tx. // // - component_exec: The component that accounts towards commiting this tx as part of a L2 // batch as well as gas costs for the eventual on-chain proof verification. // => (compression_scalar + commit_scalar + verification_scalar) * l1_base_fee - // => (new_commit_scalar) * l1_base_fee + // => (exec_scalar) * l1_base_fee // // - component_blob: The component that accounts the costs associated with data // availability, i.e. the costs of posting this tx's data in the EIP-4844 blob. @@ -162,22 +162,25 @@ impl L1BlockInfo { let component_exec = { let exec_scalar = self .l1_commit_scalar - .unwrap_or_else(|| panic!("exec scalar in spec_id={:?}", spec_id)); + .unwrap_or_else(|| panic!("missing exec scalar in spec_id={:?}", spec_id)); exec_scalar.saturating_mul(self.l1_base_fee) }; let component_blob = { let blob_scalar = self .l1_blob_scalar - .unwrap_or_else(|| panic!("l1 blob scalar in spec_id={:?}", spec_id)); + .unwrap_or_else(|| panic!("missing l1 blob scalar in spec_id={:?}", spec_id)); let blob_base_fee = self .l1_blob_base_fee - .unwrap_or_else(|| panic!("l1 blob base fee in spec_id={:?}", spec_id)); + .unwrap_or_else(|| panic!("missing l1 blob base fee in spec_id={:?}", spec_id)); blob_scalar.saturating_mul(blob_base_fee) }; // Assume compression_ratio = 1 until we have specification for estimating compression // ratio based on previous finalised batches. - let compression_ratio = |_input: &[u8]| -> U256 { U256::ONE }; + // + // We use the `TX_L1_FEE_PRECISION` to allow fractions. We then divide the overall product + // by the precision value as well. + let compression_ratio = |_input: &[u8]| -> U256 { TX_L1_FEE_PRECISION }; // size(tx) is just the length of the RLP-encoded signed tx data. let tx_size = |input: &[u8]| -> U256 { U256::from(input.len()) }; From daca1e671aa2dcde14fcb814cc104ad74c9214ea Mon Sep 17 00:00:00 2001 From: Rohit Narurkar Date: Fri, 13 Jun 2025 13:02:53 +0100 Subject: [PATCH 4/4] chore(doc): fix naming related to compression ratio v factor --- src/l1block.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/l1block.rs b/src/l1block.rs index de36c59..23b8769 100644 --- a/src/l1block.rs +++ b/src/l1block.rs @@ -141,9 +141,10 @@ impl L1BlockInfo { } fn calculate_tx_l1_cost_feynman(&self, input: &[u8], spec_id: ScrollSpecId) -> U256 { - // rollup_fee(tx) = compression_ratio(tx) * size(tx) * (component_exec + component_blob) + // rollup_fee(tx) = compression_factor(tx) * size(tx) * (component_exec + component_blob) // - // - compression_ratio(tx): estimated compressibility of the signed tx data. The tx is + // - compression_factor(tx): compression_factor = 1 / compression_ratio, where + // compression_ratio is the estimated compressibility of the signed tx data. The tx is // eventually a part of a L2 batch that should likely result in a better compression ratio, // however a conservative estimate is the size of zstd-encoding of the signed tx. // @@ -177,17 +178,17 @@ impl L1BlockInfo { blob_scalar.saturating_mul(blob_base_fee) }; - // Assume compression_ratio = 1 until we have specification for estimating compression + // Assume compression_factor = 1 until we have specification for estimating compression // ratio based on previous finalised batches. // // We use the `TX_L1_FEE_PRECISION` to allow fractions. We then divide the overall product // by the precision value as well. - let compression_ratio = |_input: &[u8]| -> U256 { TX_L1_FEE_PRECISION }; + let compression_factor = |_input: &[u8]| -> U256 { TX_L1_FEE_PRECISION }; // size(tx) is just the length of the RLP-encoded signed tx data. let tx_size = |input: &[u8]| -> U256 { U256::from(input.len()) }; - compression_ratio(input) + compression_factor(input) .saturating_mul(tx_size(input)) .saturating_mul(component_exec.saturating_add(component_blob)) .wrapping_div(TX_L1_FEE_PRECISION)