-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(levm): clean
vm.rs
file (#1796)
**Motivation** The `vm.rs` file is filled with functions that are used as "helper" functions. It would be better, especially for newcomers to the project, if the `vm.rs` file had only the bare essential "big picture" functions. **Description** This PR aims to move a lot of the "additional" logic to `utils.rs` file and only keep the "big picture functions" in `vm.rs` This PR moved the following functions from `vm.rs` to `utils.rs`: - address_to_word - calculate_create_address - calculate_create2_address - get_valid_jump_destinations - get_account - get_account_no_push_cache - get_account_mut_vm<'vm> - increase_account_balance - decrease_account_balance - update_account_bytecode - get_intrinsic_gas - max_blobs_per_block - get_blob_base_fee_update_fraction_value - get_base_fee_per_blob_gas - get_max_blob_gas_price - get_blob_gas_price - get_n_value - get_number_of_topics - increment_account_nonce - decrement_account_nonce - word_to_address - has_delegation - get_authorized_address - eip7702_recover_address - eip7702_get_code And deleted the following: - VM::get_account - VM::get_account_no_push_cache These were simple wrapping functions around functions that had the same name. The logic inside the functions was left unchanged, besides adding parameters to the functions. Closes #1632
- Loading branch information
1 parent
b1c4c9c
commit ef46d69
Showing
11 changed files
with
852 additions
and
704 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
use crate::{ | ||
account::{Account, AccountInfo}, | ||
db::{cache, CacheDB, Db}, | ||
environment::Environment, | ||
errors::{InternalError, VMError}, | ||
operations::Operation, | ||
vm::VM, | ||
}; | ||
use bytes::Bytes; | ||
use ethrex_core::{types::TxKind, Address, U256}; | ||
use std::{collections::HashMap, sync::Arc}; | ||
|
||
pub fn ops_to_bytecode(operations: &[Operation]) -> Result<Bytes, VMError> { | ||
let mut bytecode = Vec::new(); | ||
for op in operations { | ||
bytecode.extend_from_slice( | ||
&op.to_bytecode() | ||
.map_err(|_| VMError::Internal(InternalError::UtilsError))?, | ||
); // for now it is just a utils error... | ||
} | ||
Ok(bytecode.into()) | ||
} | ||
|
||
pub fn new_vm_with_bytecode(bytecode: Bytes) -> Result<VM, VMError> { | ||
new_vm_with_ops_addr_bal_db( | ||
bytecode, | ||
Address::from_low_u64_be(100), | ||
U256::MAX, | ||
Db::new(), | ||
CacheDB::default(), | ||
) | ||
} | ||
|
||
pub fn new_vm_with_ops(operations: &[Operation]) -> Result<VM, VMError> { | ||
let bytecode = ops_to_bytecode(operations)?; | ||
new_vm_with_ops_addr_bal_db( | ||
bytecode, | ||
Address::from_low_u64_be(100), | ||
U256::MAX, | ||
Db::new(), | ||
CacheDB::default(), | ||
) | ||
} | ||
|
||
pub fn new_vm_with_ops_db(operations: &[Operation], db: Db) -> Result<VM, VMError> { | ||
let bytecode = ops_to_bytecode(operations)?; | ||
new_vm_with_ops_addr_bal_db( | ||
bytecode, | ||
Address::from_low_u64_be(100), | ||
U256::MAX, | ||
db, | ||
CacheDB::default(), | ||
) | ||
} | ||
|
||
/// This function is for testing purposes only. | ||
pub fn new_vm_with_ops_addr_bal_db( | ||
contract_bytecode: Bytes, | ||
sender_address: Address, | ||
sender_balance: U256, | ||
mut db: Db, | ||
mut cache: CacheDB, | ||
) -> Result<VM, VMError> { | ||
let accounts = [ | ||
// This is the contract account that is going to be executed | ||
( | ||
Address::from_low_u64_be(42), | ||
Account { | ||
info: AccountInfo { | ||
nonce: 0, | ||
balance: U256::MAX, | ||
bytecode: contract_bytecode, | ||
}, | ||
storage: HashMap::new(), | ||
}, | ||
), | ||
( | ||
// This is the sender account | ||
sender_address, | ||
Account { | ||
info: AccountInfo { | ||
nonce: 0, | ||
balance: sender_balance, | ||
bytecode: Bytes::default(), | ||
}, | ||
storage: HashMap::new(), | ||
}, | ||
), | ||
]; | ||
|
||
db.add_accounts(accounts.to_vec()); | ||
|
||
// add to cache accounts from list accounts | ||
cache::insert_account(&mut cache, accounts[0].0, accounts[0].1.clone()); | ||
cache::insert_account(&mut cache, accounts[1].0, accounts[1].1.clone()); | ||
|
||
let env = Environment::default_from_address(sender_address); | ||
|
||
VM::new( | ||
TxKind::Call(Address::from_low_u64_be(42)), | ||
env, | ||
Default::default(), | ||
Default::default(), | ||
Arc::new(db), | ||
cache, | ||
Vec::new(), | ||
None, | ||
) | ||
} |
Oops, something went wrong.