Skip to content

Commit

Permalink
remove unneeded
Browse files Browse the repository at this point in the history
  • Loading branch information
lightsing committed Aug 29, 2024
1 parent eda9dc7 commit fea0949
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 27 deletions.
19 changes: 3 additions & 16 deletions src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ type StorageTrieLazyFn = Box<dyn FnOnce() -> ZkTrie<SharedMemoryDb>>;
pub struct ReadOnlyDB {
/// In-memory map of code hash to bytecode.
code_db: HashMap<B256, Bytecode>,
/// Account Cache
account_cache: RefCell<HashMap<Address, AccountInfo>>,
/// Storage root Cache
/// Storage root cache
storage_trie_refs: RefCell<HashMap<Address, Lazy<ZkTrie<SharedMemoryDb>, StorageTrieLazyFn>>>,
zktrie_root: B256,
zktrie_db: Rc<ZkMemoryDb>,
Expand Down Expand Up @@ -66,7 +64,6 @@ impl ReadOnlyDB {

Ok(ReadOnlyDB {
code_db,
account_cache: Default::default(),
storage_trie_refs: Default::default(),
zktrie_root,
zktrie_db: zktrie_state.zk_db.clone(),
Expand All @@ -83,12 +80,6 @@ impl ReadOnlyDB {
self.zktrie_root
}

/// Get the cached account information.
#[inline]
pub fn get_cached_account_info(&self, address: &Address) -> Option<AccountInfo> {
self.account_cache.borrow().get(address).cloned()
}

/// Update the database with a new block trace.
pub fn update<T: BlockTraceExt>(&mut self, l2_trace: T) -> Result<()> {
measure_duration_histogram!(update_db_duration_microseconds, self.update_inner(l2_trace))
Expand Down Expand Up @@ -150,18 +141,14 @@ impl DatabaseRef for ReadOnlyDB {
.expect("storage trie associated with account not found")
})),
);
let info = AccountInfo {
AccountInfo {
balance: U256::from_limbs(account_data.balance.0),
nonce: account_data.nonce,
code_size: account_data.code_size as usize,
code_hash,
poseidon_code_hash: B256::from(account_data.poseidon_code_hash.0),
code: self.code_db.get(&code_hash).cloned(),
};
self.account_cache
.borrow_mut()
.insert(address, info.clone());
info
}
}))
}

Expand Down
28 changes: 17 additions & 11 deletions src/executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,27 +179,30 @@ impl EvmExecutor {
let mut debug_account = std::collections::BTreeMap::new();

for (addr, db_acc) in self.db.accounts.iter() {
// If EVM didn't touch the account, we don't need to update it
if db_acc.account_state == AccountState::None {
continue;
}
let Some(info): Option<AccountInfo> = db_acc.info() else {
continue;
};
let acc = self.db.db.get_cached_account_info(addr).unwrap_or_default();
if acc.is_empty() && info.is_empty() {
continue;
}

dev_trace!("committing {addr}, {:?} {db_acc:?}", db_acc.account_state);
cycle_tracker_start!("commit account {}", addr);

cycle_tracker_start!("get acc_data");
let mut acc_data = zktrie
.get_account(addr.as_slice())
.map(AccountData::from)
.unwrap_or_default();
let (mut acc_data, is_original_empty) =
match zktrie.get_account(addr.as_slice()).map(AccountData::from) {
Some(acc_data) => (acc_data, acc_data.is_empty()),
None => (AccountData::default(), true),
};
cycle_tracker_end!("get acc_data");

// If the account is empty in both zktrie and StateDB, we don't need to update it
if is_original_empty && info.is_empty() {
continue;
}

acc_data.nonce = info.nonce;
acc_data.balance = U256(*info.balance.as_limbs());
if !db_acc.storage.is_empty() {
Expand Down Expand Up @@ -272,12 +275,15 @@ impl EvmExecutor {
}
}
// When the acc from StateDB is empty and info is not, also the code hash changes,
// we need to update the code hash and code size
if (acc.is_empty() && !info.is_empty()) || acc.code_hash != info.code_hash {
// we need to update the code hash and code size.
// Note, contracts can only be deployed to empty accounts.
if (is_original_empty && !info.is_empty())
|| acc_data.keccak_code_hash.0 != info.code_hash.0
{
assert_ne!(
info.poseidon_code_hash,
B256::ZERO,
"revm didn't update poseidon_code_hash, acc from StateDB: {acc:?}, revm: {info:?}",
"revm didn't update poseidon_code_hash, acc from revm: {info:?}",
);
acc_data.poseidon_code_hash = H256::from(info.poseidon_code_hash.0);
acc_data.keccak_code_hash = H256::from(info.code_hash.0);
Expand Down

0 comments on commit fea0949

Please sign in to comment.