Skip to content
Closed
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
2 changes: 1 addition & 1 deletion bins/revm-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"
[dependencies]
bytes = "1.7"
hex = "0.4"
revm = { path = "../../crates/revm", version = "14.0.0", default-features=false }
revm = { path = "../../crates/revm", version = "14.0.0", features=["morph-default-handler"] }
microbench = "0.5"
alloy-sol-macro = "0.8.0"
alloy-sol-types = "0.8.0"
Expand Down
363 changes: 363 additions & 0 deletions bins/revm-test/src/bin/morph.rs

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions bins/revme/src/cmd/statetest/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,7 @@ pub fn execute_test_suite(
let bytecode = to_analysed(Bytecode::new_raw(info.code));
let acc_info = revm::primitives::AccountInfo {
balance: info.balance,
#[cfg(feature = "morph")]
code_size,
code_size: 0,
code_hash: keccak_code_hash,
#[cfg(feature = "morph-poseidon-codehash")]
poseidon_code_hash,
Expand Down
6 changes: 6 additions & 0 deletions crates/precompile/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ rstest = "0.22.0"
serde = "1.0"
serde_json = "1.0"
serde_derive = "1.0"
secp256k1 = { version = ">=0.28, <=0.29", default-features = false, features = [
"alloc",
"recovery",
"rand",
"global-context",
] }

[features]
default = ["std", "kzg-rs", "portable"]
Expand Down
44 changes: 41 additions & 3 deletions crates/primitives/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ impl Env {
pub fn validate_tx_against_state<SPEC: Spec>(
&self,
account: &mut Account,
token_info: (U256, U256, U256),
) -> Result<(), InvalidTransaction> {
// EIP-3607: Reject transactions from senders with deployed code
// This EIP is introduced after london but there was no collision in past
Expand Down Expand Up @@ -268,9 +269,12 @@ impl Env {
}
}

let mut balance_check = U256::from(self.tx.gas_limit)
let gas_cost = U256::from(self.tx.gas_limit)
.checked_mul(self.tx.gas_price)
.and_then(|gas_cost| gas_cost.checked_add(self.tx.value))
.ok_or(InvalidTransaction::OverflowPaymentInTransaction)?;

let mut balance_check = gas_cost
.checked_add(self.tx.value)
.ok_or(InvalidTransaction::OverflowPaymentInTransaction)?;

if SPEC::enabled(SpecId::CANCUN) {
Expand All @@ -283,7 +287,17 @@ impl Env {

// Check if account has enough balance for gas_limit*gas_price and value transfer.
// Transfer will be done inside `*_inner` functions.
if balance_check > account.info.balance {
let lack_of_fund_for_max_fee = if self.tx.fee_token_id.unwrap_or_default() != 0 {
let mut fee_limit = U256::from(self.tx.fee_limit.unwrap_or_default());
if fee_limit.is_zero() || fee_limit > token_info.0 {
fee_limit = token_info.0
}
let token_check = eth_to_token(gas_cost, token_info.1, token_info.2);
token_check.is_zero() || token_check > fee_limit || self.tx.value > account.info.balance
} else {
balance_check > account.info.balance
};
if lack_of_fund_for_max_fee {
cfg_if::cfg_if! {
if #[cfg(not(feature = "morph"))] {
if self.cfg.is_balance_check_disabled() {
Expand Down Expand Up @@ -635,6 +649,13 @@ pub struct TxEnv {
#[cfg(feature = "morph")]
/// Morph fields
pub morph: MorphFields,

#[cfg(feature = "morph")]
/// For AltFeeType
pub fee_token_id: Option<u16>,
#[cfg(feature = "morph")]
/// For AltFeeType
pub fee_limit: Option<u64>,
}

pub enum TxType {
Expand Down Expand Up @@ -680,6 +701,8 @@ impl Default for TxEnv {
optimism: OptimismFields::default(),
#[cfg(feature = "morph")]
morph: MorphFields::default(),
fee_token_id: None,
fee_limit: None,
}
}
}
Expand Down Expand Up @@ -781,6 +804,21 @@ pub enum AnalysisKind {
Analyse,
}

pub fn eth_to_token(eth_amount: U256, rate: U256, token_scale: U256) -> U256 {
if rate.is_zero() {
return U256::ZERO;
}
// EthToToken token_amount = ethAmount / (tokenRate / tokenScale) = ethAmount * tokenScale / tokenRate
// Calculate: (eth_amount * token_scale) / rate
let (token_amount, remainder) = eth_amount.saturating_mul(token_scale).div_rem(rate);
// If there's a remainder, round up by adding 1
if !remainder.is_zero() {
token_amount.saturating_add(U256::from(1))
} else {
token_amount
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
2 changes: 1 addition & 1 deletion crates/revm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ rstest = "0.22.0"
alloy-provider = "0.3"

[features]
default = ["std", "kzg-rs", "portable"]
default = ["std", "kzg-rs", "portable", "morph-default-handler"]
std = [
"serde?/std",
"serde_json?/std",
Expand Down
4 changes: 4 additions & 0 deletions crates/revm/src/context/evm_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,8 @@ pub(crate) mod test_utils {
error: Ok(()),
#[cfg(any(feature = "optimism", feature = "morph"))]
l1_block_info: None,
#[cfg(feature = "morph")]
token_fee_info: None,
},
precompiles: ContextPrecompiles::default(),
}
Expand All @@ -545,6 +547,8 @@ pub(crate) mod test_utils {
error: Ok(()),
#[cfg(any(feature = "optimism", feature = "morph"))]
l1_block_info: None,
#[cfg(feature = "morph")]
token_fee_info: None,
},
precompiles: ContextPrecompiles::default(),
}
Expand Down
13 changes: 13 additions & 0 deletions crates/revm/src/context/inner_evm_context.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#[cfg(feature = "morph")]
use crate::morph::TokenFeeInfo;
use crate::{
db::Database,
interpreter::{
Expand Down Expand Up @@ -33,6 +35,9 @@ pub struct InnerEvmContext<DB: Database> {
/// Used as temporary value holder to store L1 block info.
#[cfg(feature = "morph")]
pub l1_block_info: Option<crate::morph::L1BlockInfo>,
/// Used as temporary value holder to store alt fee info.
#[cfg(feature = "morph")]
pub token_fee_info: Option<TokenFeeInfo>,
}

impl<DB: Database + Clone> Clone for InnerEvmContext<DB>
Expand All @@ -47,6 +52,8 @@ where
error: self.error.clone(),
#[cfg(any(feature = "optimism", feature = "morph"))]
l1_block_info: self.l1_block_info.clone(),
#[cfg(feature = "morph")]
token_fee_info: self.token_fee_info.clone(),
}
}
}
Expand All @@ -60,6 +67,8 @@ impl<DB: Database> InnerEvmContext<DB> {
error: Ok(()),
#[cfg(any(feature = "optimism", feature = "morph"))]
l1_block_info: None,
#[cfg(feature = "morph")]
token_fee_info: None,
}
}

Expand All @@ -73,6 +82,8 @@ impl<DB: Database> InnerEvmContext<DB> {
error: Ok(()),
#[cfg(any(feature = "optimism", feature = "morph"))]
l1_block_info: None,
#[cfg(feature = "morph")]
token_fee_info: None,
}
}

Expand All @@ -88,6 +99,8 @@ impl<DB: Database> InnerEvmContext<DB> {
error: Ok(()),
#[cfg(any(feature = "optimism", feature = "morph"))]
l1_block_info: self.l1_block_info,
#[cfg(feature = "morph")]
token_fee_info: self.token_fee_info,
}
}

Expand Down
Loading
Loading