Skip to content

Commit 4389cbc

Browse files
authored
refactor: simplify merge_db_account_data (#9223)
* refactor: simplify `merge_db_account_data` * chore: fmt
1 parent 3e901af commit 4389cbc

File tree

1 file changed

+24
-18
lines changed
  • crates/evm/core/src/backend

1 file changed

+24
-18
lines changed

crates/evm/core/src/backend/mod.rs

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1877,25 +1877,31 @@ fn merge_db_account_data<ExtDB: DatabaseRef>(
18771877
) {
18781878
trace!(?addr, "merging database data");
18791879

1880-
let mut acc = if let Some(acc) = active.accounts.get(&addr).cloned() {
1881-
acc
1882-
} else {
1883-
// Account does not exist
1884-
return;
1885-
};
1886-
1887-
if let Some(code) = active.contracts.get(&acc.info.code_hash).cloned() {
1888-
fork_db.contracts.insert(acc.info.code_hash, code);
1889-
}
1890-
1891-
if let Some(fork_account) = fork_db.accounts.get_mut(&addr) {
1892-
// This will merge the fork's tracked storage with active storage and update values
1893-
fork_account.storage.extend(std::mem::take(&mut acc.storage));
1894-
// swap them so we can insert the account as whole in the next step
1895-
std::mem::swap(&mut fork_account.storage, &mut acc.storage);
1880+
let Some(acc) = active.accounts.get(&addr) else { return };
1881+
1882+
// port contract cache over
1883+
if let Some(code) = active.contracts.get(&acc.info.code_hash) {
1884+
trace!("merging contract cache");
1885+
fork_db.contracts.insert(acc.info.code_hash, code.clone());
1886+
}
1887+
1888+
// port account storage over
1889+
use std::collections::hash_map::Entry;
1890+
match fork_db.accounts.entry(addr) {
1891+
Entry::Vacant(vacant) => {
1892+
trace!("target account not present - inserting from active");
1893+
// if the fork_db doesn't have the target account
1894+
// insert the entire thing
1895+
vacant.insert(acc.clone());
1896+
}
1897+
Entry::Occupied(mut occupied) => {
1898+
trace!("target account present - merging storage slots");
1899+
// if the fork_db does have the system,
1900+
// extend the existing storage (overriding)
1901+
let fork_account = occupied.get_mut();
1902+
fork_account.storage.extend(&acc.storage);
1903+
}
18961904
}
1897-
1898-
fork_db.accounts.insert(addr, acc);
18991905
}
19001906

19011907
/// Returns true of the address is a contract

0 commit comments

Comments
 (0)