Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(op): use RollupCostData instead of full encoding #1491

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions crates/interpreter/src/gas/calc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,15 @@ pub const fn memory_gas(num_words: u64) -> u64 {
.saturating_add(num_words.saturating_mul(num_words) / 512)
}

/// This counts the number of zero, and non-zero bytes in the input data.
///
/// Returns a tuple of (zero_bytes, non_zero_bytes).
pub fn count_zero_bytes(data: &[u8]) -> (u64, u64) {
let zero_data_len = data.iter().filter(|v| **v == 0).count() as u64;
let non_zero_data_len = data.len() as u64 - zero_data_len;
(zero_data_len, non_zero_data_len)
}

/// Initial gas that is deducted for transaction to be included.
/// Initial gas contains initial stipend gas, gas for access list and input data.
pub fn validate_initial_tx_gas(
Expand All @@ -360,8 +369,7 @@ pub fn validate_initial_tx_gas(
access_list: &[(Address, Vec<U256>)],
) -> u64 {
let mut initial_gas = 0;
let zero_data_len = input.iter().filter(|v| **v == 0).count() as u64;
let non_zero_data_len = input.len() as u64 - zero_data_len;
let (zero_data_len, non_zero_data_len) = count_zero_bytes(input);

// initdate stipend
initial_gas += zero_data_len * TRANSACTION_ZERO_DATA;
Expand Down
2 changes: 2 additions & 0 deletions crates/revm/src/optimism.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
mod fast_lz;
mod handler_register;
mod l1block;
mod rollup_cost;

pub use handler_register::{
deduct_caller, end, last_frame_return, load_accounts, load_precompiles,
optimism_handle_register, output, reward_beneficiary, validate_env, validate_tx_against_state,
};
pub use l1block::{L1BlockInfo, BASE_FEE_RECIPIENT, L1_BLOCK_CONTRACT, L1_FEE_RECIPIENT};
pub use rollup_cost::RollupCostData;
30 changes: 16 additions & 14 deletions crates/revm/src/optimism/l1block.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::optimism::fast_lz::flz_compress_len;
use crate::optimism::rollup_cost::RollupCostData;
use crate::primitives::{address, db::Database, Address, SpecId, U256};
use core::ops::Mul;

Expand Down Expand Up @@ -125,21 +125,20 @@ impl L1BlockInfo {
/// Prior to regolith, an extra 68 non-zero bytes were included in the rollup data costs to
/// account for the empty signature.
pub fn data_gas(&self, input: &[u8], spec_id: SpecId) -> U256 {
let cost_data = RollupCostData::from_bytes(input);
if spec_id.is_enabled_in(SpecId::FJORD) {
let estimated_size = self.tx_estimated_size_fjord(input);
let estimated_size = self.tx_estimated_size_fjord(cost_data);

return estimated_size
.saturating_mul(U256::from(NON_ZERO_BYTE_COST))
.wrapping_div(U256::from(1_000_000));
};

let mut rollup_data_gas_cost = U256::from(input.iter().fold(0, |acc, byte| {
acc + if *byte == 0x00 {
ZERO_BYTE_COST
} else {
NON_ZERO_BYTE_COST
}
}));
let mut rollup_data_gas_cost = U256::from(cost_data.ones.get())
.saturating_mul(U256::from(NON_ZERO_BYTE_COST))
.saturating_add(
U256::from(cost_data.zeroes.get()).saturating_mul(U256::from(ZERO_BYTE_COST)),
);
Copy link
Collaborator

@DaniPopes DaniPopes Jun 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pretty sure all the arithmetic in this function and *fjord can be scaled down to use u64


// Prior to regolith, an extra 68 non zero bytes were included in the rollup data costs.
if !spec_id.is_enabled_in(SpecId::REGOLITH) {
Expand All @@ -152,8 +151,8 @@ impl L1BlockInfo {
// 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)
fn tx_estimated_size_fjord(&self, input: &[u8]) -> U256 {
let fastlz_size = U256::from(flz_compress_len(input));
fn tx_estimated_size_fjord(&self, cost_data: RollupCostData) -> U256 {
let fastlz_size = U256::from(cost_data.fastlz_size);

fastlz_size
.saturating_mul(U256::from(836_500))
Expand All @@ -168,8 +167,11 @@ impl L1BlockInfo {
return U256::ZERO;
}

// let's create a `RollupCostData` to make the computation easier
let cost_data = RollupCostData::from_bytes(input);

if spec_id.is_enabled_in(SpecId::FJORD) {
self.calculate_tx_l1_cost_fjord(input)
self.calculate_tx_l1_cost_fjord(cost_data)
} else if spec_id.is_enabled_in(SpecId::ECOTONE) {
self.calculate_tx_l1_cost_ecotone(input, spec_id)
} else {
Expand Down Expand Up @@ -217,9 +219,9 @@ impl L1BlockInfo {
///
/// [SpecId::FJORD] L1 cost function:
/// `estimatedSize*(baseFeeScalar*l1BaseFee*16 + blobFeeScalar*l1BlobBaseFee)/1e12`
fn calculate_tx_l1_cost_fjord(&self, input: &[u8]) -> U256 {
fn calculate_tx_l1_cost_fjord(&self, cost_data: RollupCostData) -> U256 {
let l1_fee_scaled = self.calculate_l1_fee_scaled_ecotone();
let estimated_size = self.tx_estimated_size_fjord(input);
let estimated_size = self.tx_estimated_size_fjord(cost_data);

estimated_size
.saturating_mul(l1_fee_scaled)
Expand Down
35 changes: 35 additions & 0 deletions crates/revm/src/optimism/rollup_cost.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//! This contains a struct, [`RollupCostData`], that is used to compute the data availability costs
//! for a transaction.

use crate::optimism::fast_lz::flz_compress_len;
use core::num::NonZeroU64;
use revm_interpreter::gas::count_zero_bytes;

/// RollupCostData contains three fields, which are used depending on the current optimism fork.
///
/// The `zeroes` and `ones` fields are used to compute the data availability costs for a
/// transaction pre-fjord.
///
/// The `fastlz_size` field is used to compute the data availability costs for a transaction
/// post-fjord.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct RollupCostData {
/// The number of zeroes in the transaction.
pub(crate) zeroes: NonZeroU64,
/// The number of ones in the transaction.
pub(crate) ones: NonZeroU64,
/// The size of the transaction after fastLZ compression.
pub(crate) fastlz_size: u32,
}

impl RollupCostData {
/// This takes bytes as input, creating a [`RollupCostData`] struct based on the encoded data.
pub fn from_bytes(bytes: &[u8]) -> Self {
let (zeroes, ones) = count_zero_bytes(bytes);
Self {
zeroes: NonZeroU64::new(zeroes).unwrap(),
ones: NonZeroU64::new(ones).unwrap(),
fastlz_size: flz_compress_len(bytes),
}
}
}
Loading