Skip to content

Commit

Permalink
feat(levm): initialize cache with sender, recipient and coinbase acco…
Browse files Browse the repository at this point in the history
…unts (#1114)

**Motivation**

<!-- Why does this pull request exist? What are its goals? -->

**Description**

As stated in [evm.codes](https://www.evm.codes/about#access_list), this
PR initializes the cache with the corresponding addresses. (Without
implementing yet the concept of an Access List transaction)
<!-- A clear and concise general description of the changes this PR
introduces -->
Notes: Our `Cache` struct replaces the need for a `Substate` that keeps
warm addresses and storage slots because we know that if an account or
storage slot is in our cache then it is already warm.

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

Closes #1088

---------

Co-authored-by: Juani Medone <[email protected]>
Co-authored-by: maximopalopoli <[email protected]>
Co-authored-by: Javier Chatruc <[email protected]>
Co-authored-by: Ivan Litteri <[email protected]>
Co-authored-by: ilitteri <[email protected]>
  • Loading branch information
6 people authored Nov 14, 2024
1 parent b9ac420 commit 9076208
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
9 changes: 9 additions & 0 deletions crates/vm/levm/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ pub struct StorageSlot {
pub current_value: U256,
}

impl From<AccountInfo> for Account {
fn from(info: AccountInfo) -> Self {
Self {
info,
storage: HashMap::new(),
}
}
}

impl Account {
pub fn new(
balance: U256,
Expand Down
15 changes: 12 additions & 3 deletions crates/vm/levm/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,25 @@ impl VM {
) -> Result<Self, VMError> {
// Maybe this decision should be made in an upper layer

// Add sender, coinbase and recipient (in the case of a Call) to cache [https://www.evm.codes/about#access_list]
let sender_account_info = db.get_account_info(env.origin);
cache.add_account(&env.origin, &Account::from(sender_account_info.clone()));

let coinbase_account_info = db.get_account_info(env.coinbase);
cache.add_account(&env.coinbase, &Account::from(coinbase_account_info));

match to {
TxKind::Call(address_to) => {
// add address_to to cache
let recipient_account_info = db.get_account_info(address_to);
cache.add_account(&address_to, &Account::from(recipient_account_info.clone()));

// CALL tx
let initial_call_frame = CallFrame::new(
env.origin,
address_to,
address_to,
db.get_account_info(address_to).bytecode,
recipient_account_info.bytecode,
value,
calldata.clone(),
false,
Expand All @@ -101,8 +112,6 @@ impl VM {
}
TxKind::Create => {
// CREATE tx
let sender_account_info = db.get_account_info(env.origin);
// Note that this is a copy of account, not the real one

// (2)
let new_contract_address =
Expand Down

0 comments on commit 9076208

Please sign in to comment.