Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 7 additions & 2 deletions src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,12 @@ where
};

// Deduct l1 fee from caller.
let tx_l1_cost =
l1_block_info.calculate_tx_l1_cost(rlp_bytes, spec, ctx.tx().compression_ratio());
let tx_l1_cost = l1_block_info.calculate_tx_l1_cost(
rlp_bytes,
spec,
ctx.tx().compression_ratio(),
ctx.tx().compressed_size(),
Comment thread
greged93 marked this conversation as resolved.
);
let caller_account = ctx.journal_mut().load_account(caller)?;
if tx_l1_cost.gt(&caller_account.info.balance) {
return Err(InvalidTransaction::LackOfFundForMaxFee {
Expand Down Expand Up @@ -242,6 +246,7 @@ where
rlp_bytes,
ctx.cfg().spec(),
ctx.tx().compression_ratio(),
ctx.tx().compressed_size(),
)
} else {
U256::from(0)
Expand Down
45 changes: 44 additions & 1 deletion src/l1block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,22 +248,65 @@ impl L1BlockInfo {
.wrapping_div(TX_L1_FEE_PRECISION_U256) // account for penalty
}

fn calculate_tx_l1_cost_galileo(
&self,
input: &[u8],
spec_id: ScrollSpecId,
compressed_size: U256,
) -> U256 {
// rollup_fee(tx) = (execScalar * l1BaseFee + blobScalar * l1BlobBaseFee) *
// compressed_size(tx) / PRECISION
//
// Where:
// compressed_size(tx) = min(size(zstd(tx)), size(tx))

let tx_size = U256::from(input.len());

assert!(
compressed_size <= tx_size,
"transaction compressed size {compressed_size} must be less than or equal to the original size {tx_size}"
);
Comment thread
Thegaram marked this conversation as resolved.

let exec_scalar = self
.l1_commit_scalar
.unwrap_or_else(|| panic!("missing exec scalar in spec_id={spec_id:?}"));

let compressed_blob_scalar = self
.l1_blob_scalar
.unwrap_or_else(|| panic!("missing l1 blob scalar in spec_id={spec_id:?}"));

let l1_blob_base_fee = self
.l1_blob_base_fee
.unwrap_or_else(|| panic!("missing l1 blob base fee in spec_id={spec_id:?}"));

let component_exec = exec_scalar.saturating_mul(self.l1_base_fee);
Comment thread
Thegaram marked this conversation as resolved.
let component_blob = compressed_blob_scalar.saturating_mul(l1_blob_base_fee);
let fee_per_byte = component_exec.saturating_add(component_blob);

fee_per_byte.saturating_mul(compressed_size).wrapping_div(TX_L1_FEE_PRECISION_U256)
}

/// 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,
compression_ratio: Option<U256>,
compressed_size: Option<U256>,
) -> U256 {
let l1_cost = if !spec_id.is_enabled_in(ScrollSpecId::CURIE) {
self.calculate_tx_l1_cost_shanghai(input, spec_id)
} else if !spec_id.is_enabled_in(ScrollSpecId::FEYNMAN) {
self.calculate_tx_l1_cost_curie(input, spec_id)
} else {
} else if !spec_id.is_enabled_in(ScrollSpecId::GALILEO) {
let compression_ratio = compression_ratio.unwrap_or_else(|| {
panic!("compression ratio should be set in spec_id={spec_id:?}")
});
self.calculate_tx_l1_cost_feynman(input, spec_id, compression_ratio)
} else {
let compressed_size = compressed_size
.unwrap_or_else(|| panic!("compressed size should be set in spec_id={spec_id:?}"));
self.calculate_tx_l1_cost_galileo(input, spec_id, compressed_size)
};
l1_cost.min(U64_MAX)
}
Expand Down
28 changes: 25 additions & 3 deletions src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ pub trait ScrollTxTr: Transaction {
/// with posting the transaction on L1.
/// Note: compression_ratio(tx) = size(tx) * 1e9 / size(zstd(tx))
fn compression_ratio(&self) -> Option<U256>;

/// The compressed size of the transaction which is used to calculate the cost associated
/// with posting the transaction on L1.
/// Note: compression_ratio(tx) = size(zstd(tx))
// TODO: Decide if we should use `raw` (rlp-encoded tx) or `tx.Data()` (payload) here.
fn compressed_size(&self) -> Option<U256>;
}

/// A Scroll transaction. Wraps around a base transaction and provides the optional RLPed bytes for
Expand All @@ -36,17 +42,28 @@ pub struct ScrollTransaction<T: Transaction> {
pub base: T,
pub rlp_bytes: Option<Bytes>,
pub compression_ratio: Option<U256>,
pub compressed_size: Option<U256>,
}

impl<T: Transaction> ScrollTransaction<T> {
pub fn new(base: T, rlp_bytes: Option<Bytes>, compression_ratio: Option<U256>) -> Self {
Self { base, rlp_bytes, compression_ratio }
pub fn new(
base: T,
rlp_bytes: Option<Bytes>,
compression_ratio: Option<U256>,
compressed_size: Option<U256>,
) -> Self {
Self { base, rlp_bytes, compression_ratio, compressed_size }
}
}

impl Default for ScrollTransaction<TxEnv> {
fn default() -> Self {
Self { base: TxEnv::default(), rlp_bytes: None, compression_ratio: None }
Self {
base: TxEnv::default(),
rlp_bytes: None,
compression_ratio: None,
compressed_size: None,
}
}
}

Expand Down Expand Up @@ -137,6 +154,10 @@ impl<T: Transaction> ScrollTxTr for ScrollTransaction<T> {
fn compression_ratio(&self) -> Option<U256> {
self.compression_ratio
}

fn compressed_size(&self) -> Option<U256> {
self.compressed_size
}
}

impl<TX: Transaction + SystemCallTx> SystemCallTx for ScrollTransaction<TX> {
Expand All @@ -151,6 +172,7 @@ impl<TX: Transaction + SystemCallTx> SystemCallTx for ScrollTransaction<TX> {
TX::new_system_tx_with_caller(caller, system_contract_address, data),
None,
None,
None,
)
}
}
Loading