-
Notifications
You must be signed in to change notification settings - Fork 615
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
chore: implement Default
for other databases
#691
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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 |
---|---|---|
@@ -1,80 +1,106 @@ | ||
use core::{convert::Infallible, marker::PhantomData}; | ||
use core::{convert::Infallible, fmt, marker::PhantomData}; | ||
use revm_interpreter::primitives::{ | ||
db::{Database, DatabaseRef}, | ||
keccak256, AccountInfo, Bytecode, B160, B256, U256, | ||
AccountInfo, Bytecode, B160, B256, U256, | ||
}; | ||
|
||
/// An empty database that always returns default values when queried. | ||
pub type EmptyDB = EmptyDBTyped<Infallible>; | ||
|
||
impl Default for EmptyDB { | ||
/// An empty database that always returns default values when queried. | ||
/// | ||
/// This is generic over a type which is used as the database error type. | ||
pub struct EmptyDBTyped<E> { | ||
_phantom: PhantomData<E>, | ||
} | ||
|
||
// Don't derive traits, because the type parameter is unused. | ||
impl<E> Clone for EmptyDBTyped<E> { | ||
fn clone(&self) -> Self { | ||
*self | ||
} | ||
} | ||
|
||
impl<E> Copy for EmptyDBTyped<E> {} | ||
|
||
impl<E> Default for EmptyDBTyped<E> { | ||
fn default() -> Self { | ||
Self { | ||
keccak_block_hash: false, | ||
_phantom: PhantomData, | ||
} | ||
Self::new() | ||
} | ||
} | ||
|
||
/// An empty database that always returns default values when queried. | ||
#[derive(Debug, Clone)] | ||
pub struct EmptyDBTyped<T> { | ||
pub keccak_block_hash: bool, | ||
pub _phantom: PhantomData<T>, | ||
impl<E> fmt::Debug for EmptyDBTyped<E> { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.debug_struct("EmptyDB").finish_non_exhaustive() | ||
} | ||
} | ||
|
||
impl<E> PartialEq for EmptyDBTyped<E> { | ||
fn eq(&self, _: &Self) -> bool { | ||
true | ||
} | ||
} | ||
|
||
impl<T> EmptyDBTyped<T> { | ||
impl<E> Eq for EmptyDBTyped<E> {} | ||
|
||
impl<E> EmptyDBTyped<E> { | ||
pub fn new() -> Self { | ||
Self { | ||
keccak_block_hash: false, | ||
_phantom: PhantomData, | ||
} | ||
} | ||
|
||
#[doc(hidden)] | ||
#[deprecated = "use `new` instead"] | ||
pub fn new_keccak_block_hash() -> Self { | ||
Self { | ||
keccak_block_hash: true, | ||
_phantom: PhantomData, | ||
} | ||
Self::new() | ||
} | ||
} | ||
|
||
impl<T> Database for EmptyDBTyped<T> { | ||
type Error = T; | ||
impl<E> Database for EmptyDBTyped<E> { | ||
type Error = E; | ||
|
||
#[inline] | ||
fn basic(&mut self, address: B160) -> Result<Option<AccountInfo>, Self::Error> { | ||
<Self as DatabaseRef>::basic(self, address) | ||
} | ||
|
||
#[inline] | ||
fn code_by_hash(&mut self, code_hash: B256) -> Result<Bytecode, Self::Error> { | ||
<Self as DatabaseRef>::code_by_hash(self, code_hash) | ||
} | ||
|
||
#[inline] | ||
fn storage(&mut self, address: B160, index: U256) -> Result<U256, Self::Error> { | ||
<Self as DatabaseRef>::storage(self, address, index) | ||
} | ||
|
||
#[inline] | ||
fn block_hash(&mut self, number: U256) -> Result<B256, Self::Error> { | ||
<Self as DatabaseRef>::block_hash(self, number) | ||
} | ||
} | ||
|
||
impl<T> DatabaseRef for EmptyDBTyped<T> { | ||
type Error = T; | ||
/// Get basic account information. | ||
impl<E> DatabaseRef for EmptyDBTyped<E> { | ||
type Error = E; | ||
|
||
#[inline] | ||
fn basic(&self, _address: B160) -> Result<Option<AccountInfo>, Self::Error> { | ||
Ok(None) | ||
} | ||
/// Get account code by its hash | ||
|
||
#[inline] | ||
fn code_by_hash(&self, _code_hash: B256) -> Result<Bytecode, Self::Error> { | ||
Ok(Bytecode::new()) | ||
} | ||
/// Get storage value of address at index. | ||
DaniPopes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#[inline] | ||
fn storage(&self, _address: B160, _index: U256) -> Result<U256, Self::Error> { | ||
Ok(U256::default()) | ||
} | ||
|
||
// History related | ||
#[inline] | ||
fn block_hash(&self, number: U256) -> Result<B256, Self::Error> { | ||
Ok(keccak256(&number.to_be_bytes::<{ U256::BYTES }>())) | ||
Ok(number.to_be_bytes().into()) | ||
} | ||
DaniPopes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
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
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.
Why this style and not moving
pub used componnets
behindpub mod
?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.
I don't think there's a style guide for this, but I like when it's
mod x; use x::*;
groups, like: https://github.com/alloy-rs/core/blob/d9d0ded3ca4e2dde992dad1afe6410015158d6b1/crates/syn-solidity/src/lib.rsThere 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.
I would prefer a little bit of a different order.
Something like this is easier to my eyes: https://github.com/bluealloy/revm/blob/main/crates/revm/src/db/states.rs
having
mod *
grouped together.Just bikesheding, this change in general is nice