Skip to content
Open
Show file tree
Hide file tree
Changes from all 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: 1 addition & 11 deletions bins/revme/src/cmd/statetest/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,7 @@ pub fn execute_test_suite(
tx.max_fee_per_blob_gas = unit
.transaction
.max_fee_per_blob_gas
.map(|b| u128::try_from(b).expect("max fee less than u128::MAX"))
.unwrap_or(u128::MAX);
.map(|b| u128::try_from(b).expect("max fee less than u128::MAX"));

// Post and execution
for (spec_name, tests) in unit.post {
Expand Down Expand Up @@ -355,15 +354,6 @@ pub fn execute_test_suite(
}

for (index, test) in tests.into_iter().enumerate() {
let Some(tx_type) = unit.transaction.tx_type(test.indexes.data) else {
if test.expect_exception.is_some() {
continue;
} else {
panic!("Invalid transaction type without expected exception");
}
};
tx.tx_type = tx_type as u8;

tx.gas_limit = unit.transaction.gas_limit[test.indexes.gas].saturating_to();
tx.data = unit
.transaction
Expand Down
39 changes: 14 additions & 25 deletions crates/context/interface/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@ pub trait Transaction {
where
Self: 'a;

/// Returns the transaction type.
///
/// Depending on this field other functions should be called.
fn tx_type(&self) -> u8;

/// Caller aka Author aka transaction signer.
///
/// Note : Common field for all transactions.
Expand Down Expand Up @@ -98,7 +93,7 @@ pub trait Transaction {
/// Max fee per data gas
///
/// Note : EIP-4844 transaction field.
fn max_fee_per_blob_gas(&self) -> u128;
fn max_fee_per_blob_gas(&self) -> Option<u128>;

/// Total gas for all blobs. Max number of blocks is already checked
/// so we dont need to check for overflow.
Expand All @@ -114,7 +109,10 @@ pub trait Transaction {
/// See EIP-4844:
/// <https://github.com/ethereum/EIPs/blob/master/EIPS/eip-4844.md#execution-layer-validation>
fn calc_max_data_fee(&self) -> U256 {
U256::from((self.total_blob_gas() as u128).saturating_mul(self.max_fee_per_blob_gas()))
U256::from(
(self.total_blob_gas() as u128)
.saturating_mul(self.max_fee_per_blob_gas().unwrap_or_default()),
)
}

/// Returns length of the authorization list.
Expand Down Expand Up @@ -154,12 +152,6 @@ pub trait Transaction {
///
/// While for transactions after Eip1559 it is minimum of max_fee and `base + max_priority_fee`.
fn effective_gas_price(&self, base_fee: u128) -> u128 {
if self.tx_type() == TransactionType::Legacy as u8
|| self.tx_type() == TransactionType::Eip2930 as u8
{
return self.gas_price();
}

// for EIP-1559 tx and onwards gas_price represents maximum price.
let max_price = self.gas_price();
let Some(max_priority_fee) = self.max_priority_fee_per_gas() else {
Expand All @@ -179,12 +171,11 @@ pub trait Transaction {
.ok_or(InvalidTransaction::OverflowPaymentInTransaction)?;

// add blob fee
if self.tx_type() == TransactionType::Eip4844 {
let data_fee = self.calc_max_data_fee();
max_balance_spending = max_balance_spending
.checked_add(data_fee)
.ok_or(InvalidTransaction::OverflowPaymentInTransaction)?;
}
let data_fee = self.calc_max_data_fee();
max_balance_spending = max_balance_spending
.checked_add(data_fee)
.ok_or(InvalidTransaction::OverflowPaymentInTransaction)?;

Ok(max_balance_spending)
}

Expand All @@ -210,12 +201,10 @@ pub trait Transaction {
.ok_or(InvalidTransaction::OverflowPaymentInTransaction)?;

// add blob fee
if self.tx_type() == TransactionType::Eip4844 {
let blob_gas = self.total_blob_gas() as u128;
effective_balance_spending = effective_balance_spending
.checked_add(U256::from(blob_price.saturating_mul(blob_gas)))
.ok_or(InvalidTransaction::OverflowPaymentInTransaction)?;
}
let blob_gas = self.total_blob_gas() as u128;
effective_balance_spending = effective_balance_spending
.checked_add(U256::from(blob_price.saturating_mul(blob_gas)))
.ok_or(InvalidTransaction::OverflowPaymentInTransaction)?;

Ok(effective_balance_spending)
}
Expand Down
118 changes: 22 additions & 96 deletions crates/context/src/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ use std::{vec, vec::Vec};
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TxEnv {
/// Transaction type
pub tx_type: u8,
/// Caller aka Author aka transaction signer
pub caller: Address,
/// The gas limit of the transaction.
Expand Down Expand Up @@ -75,7 +73,7 @@ pub struct TxEnv {
/// Incorporated as part of the Cancun upgrade via [EIP-4844].
///
/// [EIP-4844]: https://eips.ethereum.org/EIPS/eip-4844
pub max_fee_per_blob_gas: u128,
pub max_fee_per_blob_gas: Option<u128>,

/// List of authorizations
///
Expand Down Expand Up @@ -124,28 +122,28 @@ impl TxEnv {

/// Derives tx type from transaction fields and sets it to `tx_type`.
/// Returns error in case some fields were not set correctly.
pub fn derive_tx_type(&mut self) -> Result<(), DeriveTxTypeError> {
pub fn derive_tx_type(&self) -> Result<TransactionType, DeriveTxTypeError> {
let mut tx_type = TransactionType::Legacy;

if !self.access_list.0.is_empty() {
self.tx_type = TransactionType::Eip2930 as u8;
tx_type = TransactionType::Eip2930;
}

if self.gas_priority_fee.is_some() {
self.tx_type = TransactionType::Eip1559 as u8;
tx_type = TransactionType::Eip1559;
}

if !self.blob_hashes.is_empty() || self.max_fee_per_blob_gas > 0 {
if self.max_fee_per_blob_gas.is_some() {
if let TxKind::Call(_) = self.kind {
self.tx_type = TransactionType::Eip4844 as u8;
return Ok(());
return Ok(TransactionType::Eip4844);
} else {
return Err(DeriveTxTypeError::MissingTargetForEip4844);
}
}

if !self.authorization_list.is_empty() {
if let TxKind::Call(_) = self.kind {
self.tx_type = TransactionType::Eip7702 as u8;
return Ok(());
return Ok(TransactionType::Eip7702);
} else {
return Err(DeriveTxTypeError::MissingTargetForEip7702);
}
Expand All @@ -161,7 +159,7 @@ impl TxEnv {
// }
// }

Ok(())
Ok(tx_type)
}

/// Insert a list of signed authorizations into the authorization list.
Expand All @@ -179,10 +177,6 @@ impl Transaction for TxEnv {
type AccessListItem<'a> = &'a AccessListItem;
type Authorization<'a> = &'a Either<SignedAuthorization, RecoveredAuthorization>;

fn tx_type(&self) -> u8 {
self.tx_type
}

fn kind(&self) -> TxKind {
self.kind
}
Expand Down Expand Up @@ -219,7 +213,7 @@ impl Transaction for TxEnv {
self.gas_price
}

fn max_fee_per_blob_gas(&self) -> u128 {
fn max_fee_per_blob_gas(&self) -> Option<u128> {
self.max_fee_per_blob_gas
}

Expand Down Expand Up @@ -264,7 +258,7 @@ pub struct TxEnvBuilder {
access_list: AccessList,
gas_priority_fee: Option<u128>,
blob_hashes: Vec<B256>,
max_fee_per_blob_gas: u128,
max_fee_per_blob_gas: Option<u128>,
authorization_list: Vec<Either<SignedAuthorization, RecoveredAuthorization>>,
}

Expand All @@ -283,8 +277,8 @@ impl TxEnvBuilder {
chain_id: Some(1), // Mainnet chain ID is 1
access_list: Default::default(),
gas_priority_fee: None,
blob_hashes: Vec::new(),
max_fee_per_blob_gas: 0,
blob_hashes: vec![],
max_fee_per_blob_gas: None,
authorization_list: Vec::new(),
}
}
Expand Down Expand Up @@ -363,13 +357,13 @@ impl TxEnvBuilder {

/// Set the blob hashes
pub fn blob_hashes(mut self, blob_hashes: Vec<B256>) -> Self {
self.blob_hashes = blob_hashes;
self.blob_hashes.extend(blob_hashes);
self
}

/// Set the max fee per blob gas
pub fn max_fee_per_blob_gas(mut self, max_fee_per_blob_gas: u128) -> Self {
self.max_fee_per_blob_gas = max_fee_per_blob_gas;
self.max_fee_per_blob_gas = Some(max_fee_per_blob_gas);
self
}

Expand Down Expand Up @@ -447,7 +441,6 @@ impl TxEnvBuilder {
}

let mut tx = TxEnv {
tx_type: self.tx_type.unwrap_or(0),
caller: self.caller,
gas_limit: self.gas_limit,
gas_price: self.gas_price,
Expand Down Expand Up @@ -538,8 +531,7 @@ impl TxEnvBuilder {
}
}

let mut tx = TxEnv {
tx_type: self.tx_type.unwrap_or(0),
let tx = TxEnv {
caller: self.caller,
gas_limit: self.gas_limit,
gas_price: self.gas_price,
Expand Down Expand Up @@ -595,13 +587,8 @@ impl TxEnv {
mod tests {
use super::*;

fn effective_gas_setup(
tx_type: TransactionType,
gas_price: u128,
gas_priority_fee: Option<u128>,
) -> u128 {
fn effective_gas_setup(gas_price: u128, gas_priority_fee: Option<u128>) -> u128 {
let tx = TxEnv {
tx_type: tx_type as u8,
gas_price,
gas_priority_fee,
..Default::default()
Expand All @@ -612,70 +599,9 @@ mod tests {

#[test]
fn test_effective_gas_price() {
assert_eq!(90, effective_gas_setup(TransactionType::Legacy, 90, None));
assert_eq!(
90,
effective_gas_setup(TransactionType::Legacy, 90, Some(0))
);
assert_eq!(
90,
effective_gas_setup(TransactionType::Legacy, 90, Some(10))
);
assert_eq!(
120,
effective_gas_setup(TransactionType::Legacy, 120, Some(10))
);
assert_eq!(90, effective_gas_setup(TransactionType::Eip2930, 90, None));
assert_eq!(
90,
effective_gas_setup(TransactionType::Eip2930, 90, Some(0))
);
assert_eq!(
90,
effective_gas_setup(TransactionType::Eip2930, 90, Some(10))
);
assert_eq!(
120,
effective_gas_setup(TransactionType::Eip2930, 120, Some(10))
);
assert_eq!(90, effective_gas_setup(TransactionType::Eip1559, 90, None));
assert_eq!(
90,
effective_gas_setup(TransactionType::Eip1559, 90, Some(0))
);
assert_eq!(
90,
effective_gas_setup(TransactionType::Eip1559, 90, Some(10))
);
assert_eq!(
110,
effective_gas_setup(TransactionType::Eip1559, 120, Some(10))
);
assert_eq!(90, effective_gas_setup(TransactionType::Eip4844, 90, None));
assert_eq!(
90,
effective_gas_setup(TransactionType::Eip4844, 90, Some(0))
);
assert_eq!(
90,
effective_gas_setup(TransactionType::Eip4844, 90, Some(10))
);
assert_eq!(
110,
effective_gas_setup(TransactionType::Eip4844, 120, Some(10))
);
assert_eq!(90, effective_gas_setup(TransactionType::Eip7702, 90, None));
assert_eq!(
90,
effective_gas_setup(TransactionType::Eip7702, 90, Some(0))
);
assert_eq!(
90,
effective_gas_setup(TransactionType::Eip7702, 90, Some(10))
);
assert_eq!(
110,
effective_gas_setup(TransactionType::Eip7702, 120, Some(10))
);
assert_eq!(90, effective_gas_setup(90, None));
assert_eq!(90, effective_gas_setup(90, Some(0)));
assert_eq!(90, effective_gas_setup(90, Some(10)));
assert_eq!(110, effective_gas_setup(120, Some(10)));
}
}
3 changes: 1 addition & 2 deletions crates/handler/src/mainnet_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ mod test {
Bytecode,
};
use context::{Context, TxEnv};
use context_interface::{transaction::Authorization, TransactionType};
use context_interface::transaction::Authorization;
use database::{BenchmarkDB, EEADDRESS, FFADDRESS};
use primitives::{hardfork::SpecId, TxKind, U256};
use primitives::{StorageKey, StorageValue};
Expand All @@ -99,7 +99,6 @@ mod test {

let state = evm
.transact_finalize(TxEnv {
tx_type: TransactionType::Eip7702.into(),
gas_limit: 100_000,
authorization_list: vec![Either::Left(auth)],
caller: EEADDRESS,
Expand Down
Loading
Loading