Skip to content

Commit

Permalink
fix(levm): change gas type from U256 to u64 (#1528)
Browse files Browse the repository at this point in the history
**Motivation**

The execution client uses `u64` for measuring gas, while LEVM uses
`U256`. This PR makes LEVM also use u64 for gas

**Description**

<!-- A clear and concise general description of the changes this PR
introduces -->

<!-- Link to issues: Resolves #111, Resolves #222 -->

Closes #1499
  • Loading branch information
lima-limon-inc authored Dec 20, 2024
1 parent cd58549 commit 2bb8fa9
Show file tree
Hide file tree
Showing 16 changed files with 587 additions and 474 deletions.
33 changes: 32 additions & 1 deletion cmd/ef_tests/levm/deserialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,22 @@ where
)
}

/// This serializes a hexadecimal string to u64
pub fn deserialize_u64_safe<'de, D>(deserializer: D) -> Result<u64, D::Error>
where
D: serde::Deserializer<'de>,
{
u64::from_str_radix(
String::deserialize(deserializer)?.trim_start_matches("0x"),
16,
)
.map_err(|err| {
serde::de::Error::custom(format!(
"error parsing U64 when deserializing U64 safely: {err}"
))
})
}

pub fn deserialize_h256_vec_optional_safe<'de, D>(
deserializer: D,
) -> Result<Option<Vec<H256>>, D::Error>
Expand Down Expand Up @@ -201,7 +217,22 @@ where
.map(|s| {
U256::from_str(s.trim_start_matches("0x:bigint ")).map_err(|err| {
serde::de::Error::custom(format!(
"error parsing U256 when deserializing U256 safely: {err}"
"error parsing U256 when deserializing U256 vector safely: {err}"
))
})
})
.collect()
}
pub fn deserialize_u64_vec_safe<'de, D>(deserializer: D) -> Result<Vec<u64>, D::Error>
where
D: serde::Deserializer<'de>,
{
Vec::<String>::deserialize(deserializer)?
.iter()
.map(|s| {
u64::from_str_radix(s.trim_start_matches("0x"), 16).map_err(|err| {
serde::de::Error::custom(format!(
"error parsing u64 when deserializing u64 vector safely: {err}"
))
})
})
Expand Down
2 changes: 1 addition & 1 deletion cmd/ef_tests/levm/runner/levm_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ pub fn prepare_vm_for_tx(vector: &TestVector, test: &EFTest) -> Result<VM, EFTes
tx.to.clone(),
Environment {
origin: tx.sender,
refunded_gas: U256::default(),
refunded_gas: 0,
gas_limit: tx.gas_limit,
block_number: test.env.current_number,
coinbase: test.env.current_coinbase,
Expand Down
5 changes: 3 additions & 2 deletions cmd/ef_tests/levm/runner/revm_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,12 @@ pub fn prepare_revm_for_tx<'state>(
let chain_spec = initial_state
.chain_config()
.map_err(|err| EFTestRunnerError::VMInitializationFailed(err.to_string()))?;

let block_env = RevmBlockEnv {
number: RevmU256::from_limbs(test.env.current_number.0),
coinbase: RevmAddress(test.env.current_coinbase.0.into()),
timestamp: RevmU256::from_limbs(test.env.current_timestamp.0),
gas_limit: RevmU256::from_limbs(test.env.current_gas_limit.0),
gas_limit: RevmU256::from(test.env.current_gas_limit),
basefee: RevmU256::from_limbs(test.env.current_base_fee.unwrap_or_default().0),
difficulty: RevmU256::from_limbs(test.env.current_difficulty.0),
prevrandao: test.env.current_random.map(|v| v.0.into()),
Expand Down Expand Up @@ -137,7 +138,7 @@ pub fn prepare_revm_for_tx<'state>(

let tx_env = RevmTxEnv {
caller: tx.sender.0.into(),
gas_limit: tx.gas_limit.as_u64(),
gas_limit: tx.gas_limit,
gas_price: RevmU256::from_limbs(effective_gas_price(test, tx)?.0),
transact_to: match tx.to {
TxKind::Call(to) => RevmTxKind::Call(to.0.into()),
Expand Down
13 changes: 7 additions & 6 deletions cmd/ef_tests/levm/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{
deserialize_h256_vec_optional_safe, deserialize_hex_bytes, deserialize_hex_bytes_vec,
deserialize_transaction_expected_exception, deserialize_u256_optional_safe,
deserialize_u256_safe, deserialize_u256_valued_hashmap_safe, deserialize_u256_vec_safe,
deserialize_u64_safe, deserialize_u64_vec_safe,
},
report::TestVector,
};
Expand Down Expand Up @@ -61,7 +62,7 @@ impl From<&EFTest> for Genesis {
},
coinbase: test.env.current_coinbase,
difficulty: test.env.current_difficulty,
gas_limit: test.env.current_gas_limit.as_u64(),
gas_limit: test.env.current_gas_limit,
mix_hash: test.env.current_random.unwrap_or_default(),
timestamp: test.env.current_timestamp.as_u64(),
base_fee_per_gas: test.env.current_base_fee.map(|v| v.as_u64()),
Expand Down Expand Up @@ -99,8 +100,8 @@ pub struct EFTestEnv {
pub current_difficulty: U256,
#[serde(default, deserialize_with = "deserialize_u256_optional_safe")]
pub current_excess_blob_gas: Option<U256>,
#[serde(deserialize_with = "deserialize_u256_safe")]
pub current_gas_limit: U256,
#[serde(deserialize_with = "deserialize_u64_safe")]
pub current_gas_limit: u64,
#[serde(deserialize_with = "deserialize_u256_safe")]
pub current_number: U256,
pub current_random: Option<H256>,
Expand Down Expand Up @@ -268,8 +269,8 @@ pub struct EFTestAccessListItem {
pub struct EFTestRawTransaction {
#[serde(deserialize_with = "deserialize_hex_bytes_vec")]
pub data: Vec<Bytes>,
#[serde(deserialize_with = "deserialize_u256_vec_safe")]
pub gas_limit: Vec<U256>,
#[serde(deserialize_with = "deserialize_u64_vec_safe")]
pub gas_limit: Vec<u64>,
#[serde(default, deserialize_with = "deserialize_u256_optional_safe")]
pub gas_price: Option<U256>,
#[serde(deserialize_with = "deserialize_u256_safe")]
Expand All @@ -295,7 +296,7 @@ pub struct EFTestRawTransaction {
#[serde(rename_all = "camelCase")]
pub struct EFTestTransaction {
pub data: Bytes,
pub gas_limit: U256,
pub gas_limit: u64,
pub gas_price: Option<U256>,
#[serde(deserialize_with = "deserialize_u256_safe")]
pub nonce: U256,
Expand Down
10 changes: 5 additions & 5 deletions crates/vm/levm/src/call_frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ impl Stack {
/// the EVM is currently executing.
pub struct CallFrame {
/// Max gas a callframe can use
pub gas_limit: U256,
pub gas_limit: u64,
/// Keeps track of the gas that's been used in current context
pub gas_used: U256,
pub gas_used: u64,
/// Program Counter
pub pc: usize,
/// Address of the account that sent the message
Expand Down Expand Up @@ -91,7 +91,7 @@ impl CallFrame {
pub fn new_from_bytecode(bytecode: Bytes) -> Self {
let valid_jump_destinations = get_valid_jump_destinations(&bytecode).unwrap_or_default();
Self {
gas_limit: U256::MAX,
gas_limit: u64::MAX,
bytecode,
valid_jump_destinations,
..Default::default()
Expand All @@ -113,8 +113,8 @@ impl CallFrame {
msg_value: U256,
calldata: Bytes,
is_static: bool,
gas_limit: U256,
gas_used: U256,
gas_limit: u64,
gas_used: u64,
depth: usize,
create_op_called: bool,
) -> Self {
Expand Down
2 changes: 1 addition & 1 deletion crates/vm/levm/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub const EMPTY_CODE_HASH: H256 = H256([
pub const MEMORY_EXPANSION_QUOTIENT: usize = 512;

// Transaction costs in gas (in wei)
pub const TX_BASE_COST: U256 = U256([21000, 0, 0, 0]);
pub const TX_BASE_COST: u64 = 21000;

pub const MAX_CODE_SIZE: usize = 0x6000;
pub const INIT_CODE_MAX_SIZE: usize = 49152;
Expand Down
10 changes: 5 additions & 5 deletions crates/vm/levm/src/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ pub struct Environment {
/// The sender address of the transaction that originated
/// this execution.
pub origin: Address,
pub refunded_gas: U256,
pub gas_limit: U256,
pub refunded_gas: u64,
pub gas_limit: u64,
pub block_number: U256,
pub coinbase: Address,
pub timestamp: U256,
Expand All @@ -25,16 +25,16 @@ pub struct Environment {
pub tx_max_priority_fee_per_gas: Option<U256>,
pub tx_max_fee_per_gas: Option<U256>,
pub tx_max_fee_per_blob_gas: Option<U256>,
pub block_gas_limit: U256,
pub block_gas_limit: u64,
pub transient_storage: TransientStorage,
}

impl Environment {
pub fn default_from_address(origin: Address) -> Self {
Self {
origin,
refunded_gas: U256::default(),
gas_limit: U256::MAX,
refunded_gas: 0,
gas_limit: u64::MAX,
block_number: Default::default(),
coinbase: Default::default(),
timestamp: Default::default(),
Expand Down
Loading

0 comments on commit 2bb8fa9

Please sign in to comment.