From 8c76a42455be01a33830a9b896f18805f67dbae8 Mon Sep 17 00:00:00 2001 From: ilitteri Date: Fri, 1 Nov 2024 14:39:03 -0300 Subject: [PATCH] Modularize Environment logic --- crates/vm/levm/src/environment.rs | 72 +++++++++++++++++++++++++++++++ crates/vm/levm/src/lib.rs | 2 + crates/vm/levm/src/utils.rs | 3 +- crates/vm/levm/src/vm.rs | 71 +----------------------------- crates/vm/levm/tests/tests.rs | 3 +- crates/vm/vm.rs | 4 +- 6 files changed, 81 insertions(+), 74 deletions(-) create mode 100644 crates/vm/levm/src/environment.rs diff --git a/crates/vm/levm/src/environment.rs b/crates/vm/levm/src/environment.rs new file mode 100644 index 0000000000..31db3b5427 --- /dev/null +++ b/crates/vm/levm/src/environment.rs @@ -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, + pub chain_id: U256, + pub base_fee_per_gas: U256, + pub gas_price: U256, + pub block_excess_blob_gas: Option, + pub block_blob_gas_used: Option, + pub tx_blob_hashes: Option>, +} + +// 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(), + } + } +} diff --git a/crates/vm/levm/src/lib.rs b/crates/vm/levm/src/lib.rs index 015727b1de..9ffbbfd75d 100644 --- a/crates/vm/levm/src/lib.rs +++ b/crates/vm/levm/src/lib.rs @@ -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; @@ -11,3 +12,4 @@ pub mod utils; pub mod vm; pub use account::*; +pub use environment::*; diff --git a/crates/vm/levm/src/utils.rs b/crates/vm/levm/src/utils.rs index 45dd972977..0a9d0d2c35 100644 --- a/crates/vm/levm/src/utils.rs +++ b/crates/vm/levm/src/utils.rs @@ -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}; diff --git a/crates/vm/levm/src/vm.rs b/crates/vm/levm/src/vm.rs index c03e332296..97ea100f77 100644 --- a/crates/vm/levm/src/vm.rs +++ b/crates/vm/levm/src/vm.rs @@ -3,6 +3,7 @@ use crate::{ call_frame::CallFrame, constants::*, db::{Cache, Database}, + environment::Environment, errors::{OpcodeSuccess, ResultReason, TransactionReport, TxResult, VMError}, opcodes::Opcode, }; @@ -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, - pub chain_id: U256, - pub base_fee_per_gas: U256, - pub gas_price: U256, - pub block_excess_blob_gas: Option, - pub block_blob_gas_used: Option, - pub tx_blob_hashes: Option>, -} - -// 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, pub env: Environment, diff --git a/crates/vm/levm/tests/tests.rs b/crates/vm/levm/tests/tests.rs index 6ee610c512..ecf4766e1b 100644 --- a/crates/vm/levm/tests/tests.rs +++ b/crates/vm/levm/tests/tests.rs @@ -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}; diff --git a/crates/vm/vm.rs b/crates/vm/vm.rs index b865b907d9..b95656c42f 100644 --- a/crates/vm/vm.rs +++ b/crates/vm/vm.rs @@ -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};