Skip to content

Commit

Permalink
Modularize Environment logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ilitteri committed Nov 1, 2024
1 parent 886b8d5 commit 8c76a42
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 74 deletions.
72 changes: 72 additions & 0 deletions crates/vm/levm/src/environment.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
use crate::constants::TX_BASE_COST;
use ethereum_rust_core::{Address, H256, U256};

#[derive(Debug, Default, Clone)]
pub struct Environment {
/// The sender address of the transaction that originated
/// this execution.
pub origin: Address,
pub consumed_gas: U256,
pub refunded_gas: U256,
pub gas_limit: U256,
pub block_number: U256,
pub coinbase: Address,
pub timestamp: U256,
pub prev_randao: Option<H256>,
pub chain_id: U256,
pub base_fee_per_gas: U256,
pub gas_price: U256,
pub block_excess_blob_gas: Option<U256>,
pub block_blob_gas_used: Option<U256>,
pub tx_blob_hashes: Option<Vec<H256>>,
}

// The fee on a 1559 transaction is base_fee + priority_fee
// So because of how things work here, we got priority_fee = gas_price - base_fee_per_gas
//
// Things to do:
// - Fix fee calculations. Use EIP 1559 (base_fee + priority fee etc).
// - Send the coinbase fee to the coinbase_account.
// - Do the full gas discount at the beginning and then refund at the end.
// - Add a method for substracting/adding to the balance of an account. This is done all over the place

/*
gas_price = effective_gas_price
base_fee_per_gas = base_fee
priority_fee_per_gas = gas_price - base_fee_per_gas
effective_gas_price = priority_fee_per_gas + base_fee_per_gas
The priority fee per gas field is NOT a part of our VM. It is implicit as the difference
between the gas_price and the base_fee_per_gas.
When setting the VM for execution at the beginning, we have to calculate the priority_fee_per_gas as
priority_fee_per_gas = min(
tx.max_priority_fee_per_gas,
tx.max_fee_per_gas - base_fee_per_gas,
)
to then set the gas_price as priority_fee_per_gas + base_fee_per_gas
*/

impl Environment {
pub fn default_from_address(origin: Address) -> Self {
Self {
origin,
consumed_gas: TX_BASE_COST,
refunded_gas: U256::zero(),
gas_limit: U256::MAX,
block_number: Default::default(),
coinbase: Default::default(),
timestamp: Default::default(),
prev_randao: Default::default(),
chain_id: U256::one(),
base_fee_per_gas: Default::default(),
gas_price: Default::default(),
block_excess_blob_gas: Default::default(),
block_blob_gas_used: Default::default(),
tx_blob_hashes: Default::default(),
}
}
}
2 changes: 2 additions & 0 deletions crates/vm/levm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub mod account;
pub mod call_frame;
pub mod constants;
pub mod db;
pub mod environment;
pub mod errors;
pub mod memory;
pub mod opcode_handlers;
Expand All @@ -11,3 +12,4 @@ pub mod utils;
pub mod vm;

pub use account::*;
pub use environment::*;
3 changes: 2 additions & 1 deletion crates/vm/levm/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::{
account::{Account, AccountInfo},
db::{Cache, Db},
environment::Environment,
operations::Operation,
vm::{Environment, VM},
vm::VM,
};
use bytes::Bytes;
use ethereum_rust_core::{types::TxKind, Address, U256};
Expand Down
71 changes: 1 addition & 70 deletions crates/vm/levm/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::{
call_frame::CallFrame,
constants::*,
db::{Cache, Database},
environment::Environment,
errors::{OpcodeSuccess, ResultReason, TransactionReport, TxResult, VMError},
opcodes::Opcode,
};
Expand All @@ -23,76 +24,6 @@ pub struct Substate {
// pub accessed_storage_keys: HashSet<(Address, U256)>,
}

#[derive(Debug, Default, Clone)]
pub struct Environment {
/// The sender address of the transaction that originated
/// this execution.
pub origin: Address,
pub consumed_gas: U256,
pub refunded_gas: U256,
pub gas_limit: U256,
pub block_number: U256,
pub coinbase: Address,
pub timestamp: U256,
pub prev_randao: Option<H256>,
pub chain_id: U256,
pub base_fee_per_gas: U256,
pub gas_price: U256,
pub block_excess_blob_gas: Option<U256>,
pub block_blob_gas_used: Option<U256>,
pub tx_blob_hashes: Option<Vec<H256>>,
}

// The fee on a 1559 transaction is base_fee + priority_fee
// So because of how things work here, we got priority_fee = gas_price - base_fee_per_gas
//
// Things to do:
// - Fix fee calculations. Use EIP 1559 (base_fee + priority fee etc).
// - Send the coinbase fee to the coinbase_account.
// - Do the full gas discount at the beginning and then refund at the end.
// - Add a method for substracting/adding to the balance of an account. This is done all over the place

/*
gas_price = effective_gas_price
base_fee_per_gas = base_fee
priority_fee_per_gas = gas_price - base_fee_per_gas
effective_gas_price = priority_fee_per_gas + base_fee_per_gas
The priority fee per gas field is NOT a part of our VM. It is implicit as the difference
between the gas_price and the base_fee_per_gas.
When setting the VM for execution at the beginning, we have to calculate the priority_fee_per_gas as
priority_fee_per_gas = min(
tx.max_priority_fee_per_gas,
tx.max_fee_per_gas - base_fee_per_gas,
)
to then set the gas_price as priority_fee_per_gas + base_fee_per_gas
*/

impl Environment {
pub fn default_from_address(origin: Address) -> Self {
Self {
origin,
consumed_gas: TX_BASE_COST,
refunded_gas: U256::zero(),
gas_limit: U256::MAX,
block_number: Default::default(),
coinbase: Default::default(),
timestamp: Default::default(),
prev_randao: Default::default(),
chain_id: U256::one(),
base_fee_per_gas: Default::default(),
gas_price: Default::default(),
block_excess_blob_gas: Default::default(),
block_blob_gas_used: Default::default(),
tx_blob_hashes: Default::default(),
}
}
}

pub struct VM {
pub call_frames: Vec<CallFrame>,
pub env: Environment,
Expand Down
3 changes: 2 additions & 1 deletion crates/vm/levm/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use ethereum_rust_levm::{
errors::{TxResult, VMError},
operations::Operation,
utils::{new_vm_with_ops, new_vm_with_ops_addr_bal_db, new_vm_with_ops_db},
vm::{word_to_address, Environment, Storage, VM},
vm::{word_to_address, Storage, VM},
Environment,
};
use std::{collections::HashMap, sync::Arc};

Expand Down
4 changes: 2 additions & 2 deletions crates/vm/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ mod errors;
mod execution_result;
#[cfg(feature = "l2")]
mod mods;

use db::StoreWrapper;
use ethereum_rust_core::types::TxType;
use ethereum_rust_levm::{
db::{Cache, Database as LevmDatabase},
errors::{TransactionReport, TxResult, VMError},
vm::{Environment, VM},
vm::VM,
Environment,
};
use std::{cmp::min, collections::HashMap, sync::Arc};

Expand Down

0 comments on commit 8c76a42

Please sign in to comment.