-
Notifications
You must be signed in to change notification settings - Fork 11
fix: cache misses now miss for real for real #141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -166,26 +166,15 @@ impl<Db: DatabaseRef> Database for CacheOnWrite<Db> { | |
| } | ||
|
|
||
| fn code_by_hash(&mut self, code_hash: B256) -> Result<Bytecode, Self::Error> { | ||
| if let Some(code) = self.cache.contracts.get(&code_hash) { | ||
| return Ok(code.clone()); | ||
| } | ||
| self.inner.code_by_hash_ref(code_hash) | ||
| Self::code_by_hash_ref(self, code_hash) | ||
| } | ||
|
|
||
| fn storage(&mut self, address: Address, index: U256) -> Result<U256, Self::Error> { | ||
| if let Some(storage) = | ||
| self.cache.accounts.get(&address).map(|a| a.storage.get(&index).cloned()) | ||
| { | ||
| return Ok(storage.unwrap_or_default()); | ||
| } | ||
| self.inner.storage_ref(address, index) | ||
| Self::storage_ref(self, address, index) | ||
| } | ||
|
|
||
| fn block_hash(&mut self, number: u64) -> Result<B256, Self::Error> { | ||
| if let Some(hash) = self.cache.block_hashes.get(&U256::from(number)) { | ||
| return Ok(*hash); | ||
| } | ||
| self.inner.block_hash_ref(number) | ||
| Self::block_hash_ref(self, number) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -207,11 +196,14 @@ impl<Db: DatabaseRef> DatabaseRef for CacheOnWrite<Db> { | |
| } | ||
|
|
||
| fn storage_ref(&self, address: Address, index: U256) -> Result<U256, Self::Error> { | ||
| // Check if account exists in cache, and the storage state is occupied | ||
| // in the cache | ||
| if let Some(storage) = | ||
| self.cache.accounts.get(&address).map(|a| a.storage.get(&index).cloned()) | ||
| self.cache.accounts.get(&address).and_then(|a| a.storage.get(&index).cloned()) | ||
| { | ||
| return Ok(storage.unwrap_or_default()); | ||
| return Ok(storage); | ||
| } | ||
| // Cache miss (no account) or partial cache miss (no storage slot) - query inner DB | ||
| self.inner.storage_ref(address, index) | ||
| } | ||
|
|
||
|
|
@@ -263,6 +255,73 @@ impl<Db> DatabaseCommit for CacheOnWrite<Db> { | |
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use revm::{ | ||
| database::InMemoryDB, | ||
| state::{AccountStatus, EvmStorageSlot}, | ||
| }; | ||
|
|
||
| #[test] | ||
| fn state_isolation_regression() { | ||
| let mut mem_db = InMemoryDB::default(); | ||
|
|
||
| let address = Address::with_last_byte(42); | ||
|
|
||
| // Write an account with storage at index 0 to the underlying DB | ||
| let account = Account { | ||
| info: AccountInfo { balance: U256::from(5000), ..Default::default() }, | ||
| storage: [(U256::from(0), EvmStorageSlot::new_changed(U256::ZERO, U256::from(42), 0))] | ||
| .into_iter() | ||
| .collect(), | ||
| transaction_id: 0, | ||
| status: AccountStatus::Touched, | ||
| }; | ||
|
|
||
| let mut changes: std::collections::HashMap< | ||
| Address, | ||
| Account, | ||
| revm::primitives::map::DefaultHashBuilder, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
| > = Default::default(); | ||
| changes.insert(address, account); | ||
| mem_db.commit(changes); | ||
|
|
||
| // Read the items at index 1 and index 0 from the COW DB | ||
| assert_eq!(mem_db.storage(address, U256::from(0)).unwrap(), U256::from(42)); | ||
| assert_eq!(mem_db.storage(address, U256::from(1)).unwrap(), U256::ZERO); | ||
|
|
||
| // Stand up the COW DB on top of the underlying DB | ||
| let mut cow_db = CacheOnWrite::new(mem_db); | ||
|
|
||
| // Read the items at index 1 and index 0 from the COW DB | ||
| assert_eq!(cow_db.storage(address, U256::from(0)).unwrap(), U256::from(42)); | ||
| assert_eq!(cow_db.storage(address, U256::from(1)).unwrap(), U256::ZERO); | ||
|
|
||
| // Write a storage item at index 1 to the COW DB | ||
| let account = Account { | ||
| info: AccountInfo { balance: U256::from(5000), ..Default::default() }, | ||
| storage: [(U256::from(1), EvmStorageSlot::new_changed(U256::ZERO, U256::from(42), 0))] | ||
| .into_iter() | ||
| .collect(), | ||
| transaction_id: 0, | ||
| status: AccountStatus::Touched, | ||
| }; | ||
|
|
||
| let mut changes: std::collections::HashMap< | ||
| Address, | ||
| Account, | ||
| revm::primitives::map::DefaultHashBuilder, | ||
| > = Default::default(); | ||
| changes.insert(address, account); | ||
| cow_db.commit(changes); | ||
|
|
||
| // Read the items at index 1 and index 0 from the COW DB | ||
| assert_eq!(cow_db.storage(address, U256::from(0)).unwrap(), U256::from(42)); | ||
| assert_eq!(cow_db.storage(address, U256::from(1)).unwrap(), U256::from(42)); | ||
| } | ||
| } | ||
|
|
||
| // Some code above and documentation is adapted from the revm crate, and is | ||
| // reproduced here under the terms of the MIT license. | ||
| // | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's just import the type
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no, i have done this many ways and just fully naming it prevents inference bugs. this is me, not claude lol