Skip to content
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

fix: rename DatabaseRef trait functions to *_ref #795

Merged
merged 3 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions crates/primitives/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,16 @@ pub trait DatabaseRef {
type Error;

/// Get basic account information.
fn basic(&self, address: Address) -> Result<Option<AccountInfo>, Self::Error>;
fn basic_ref(&self, address: Address) -> Result<Option<AccountInfo>, Self::Error>;

/// Get account code by its hash.
fn code_by_hash(&self, code_hash: B256) -> Result<Bytecode, Self::Error>;
fn code_by_hash_ref(&self, code_hash: B256) -> Result<Bytecode, Self::Error>;

/// Get storage value of address at index.
fn storage(&self, address: Address, index: U256) -> Result<U256, Self::Error>;
fn storage_ref(&self, address: Address, index: U256) -> Result<U256, Self::Error>;

/// Get block hash by block number.
fn block_hash(&self, number: U256) -> Result<B256, Self::Error>;
fn block_hash_ref(&self, number: U256) -> Result<B256, Self::Error>;
}

/// Wraps a [`DatabaseRef`] to provide a [`Database`] implementation.
Expand All @@ -68,22 +68,22 @@ impl<T: DatabaseRef> Database for WrapDatabaseRef<T> {

#[inline]
fn basic(&mut self, address: Address) -> Result<Option<AccountInfo>, Self::Error> {
self.0.basic(address)
self.0.basic_ref(address)
}

#[inline]
fn code_by_hash(&mut self, code_hash: B256) -> Result<Bytecode, Self::Error> {
self.0.code_by_hash(code_hash)
self.0.code_by_hash_ref(code_hash)
}

#[inline]
fn storage(&mut self, address: Address, index: U256) -> Result<U256, Self::Error> {
self.0.storage(address, index)
self.0.storage_ref(address, index)
}

#[inline]
fn block_hash(&mut self, number: U256) -> Result<B256, Self::Error> {
self.0.block_hash(number)
self.0.block_hash_ref(number)
}
}

Expand All @@ -108,21 +108,21 @@ impl<'a, E> Database for RefDBWrapper<'a, E> {

#[inline]
fn basic(&mut self, address: Address) -> Result<Option<AccountInfo>, Self::Error> {
self.db.basic(address)
self.db.basic_ref(address)
}

#[inline]
fn code_by_hash(&mut self, code_hash: B256) -> Result<Bytecode, Self::Error> {
self.db.code_by_hash(code_hash)
self.db.code_by_hash_ref(code_hash)
}

#[inline]
fn storage(&mut self, address: Address, index: U256) -> Result<U256, Self::Error> {
self.db.storage(address, index)
self.db.storage_ref(address, index)
}

#[inline]
fn block_hash(&mut self, number: U256) -> Result<B256, Self::Error> {
self.db.block_hash(number)
self.db.block_hash_ref(number)
}
}
8 changes: 4 additions & 4 deletions crates/primitives/src/db/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,23 @@ impl<S: State, BH: BlockHash> Database for DatabaseComponents<S, BH> {
impl<S: StateRef, BH: BlockHashRef> DatabaseRef for DatabaseComponents<S, BH> {
type Error = DatabaseComponentError<S::Error, BH::Error>;

fn basic(&self, address: Address) -> Result<Option<AccountInfo>, Self::Error> {
fn basic_ref(&self, address: Address) -> Result<Option<AccountInfo>, Self::Error> {
self.state.basic(address).map_err(Self::Error::State)
}

fn code_by_hash(&self, code_hash: B256) -> Result<Bytecode, Self::Error> {
fn code_by_hash_ref(&self, code_hash: B256) -> Result<Bytecode, Self::Error> {
self.state
.code_by_hash(code_hash)
.map_err(Self::Error::State)
}

fn storage(&self, address: Address, index: U256) -> Result<U256, Self::Error> {
fn storage_ref(&self, address: Address, index: U256) -> Result<U256, Self::Error> {
self.state
.storage(address, index)
.map_err(Self::Error::State)
}

fn block_hash(&self, number: U256) -> Result<B256, Self::Error> {
fn block_hash_ref(&self, number: U256) -> Result<B256, Self::Error> {
self.block_hash
.block_hash(number)
.map_err(Self::Error::BlockHash)
Expand Down
16 changes: 8 additions & 8 deletions crates/revm/src/db/emptydb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,45 +62,45 @@ impl<E> Database for EmptyDBTyped<E> {

#[inline]
fn basic(&mut self, address: Address) -> Result<Option<AccountInfo>, Self::Error> {
<Self as DatabaseRef>::basic(self, address)
<Self as DatabaseRef>::basic_ref(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)
<Self as DatabaseRef>::code_by_hash_ref(self, code_hash)
}

#[inline]
fn storage(&mut self, address: Address, index: U256) -> Result<U256, Self::Error> {
<Self as DatabaseRef>::storage(self, address, index)
<Self as DatabaseRef>::storage_ref(self, address, index)
}

#[inline]
fn block_hash(&mut self, number: U256) -> Result<B256, Self::Error> {
<Self as DatabaseRef>::block_hash(self, number)
<Self as DatabaseRef>::block_hash_ref(self, number)
}
}

impl<E> DatabaseRef for EmptyDBTyped<E> {
type Error = E;

#[inline]
fn basic(&self, _address: Address) -> Result<Option<AccountInfo>, Self::Error> {
fn basic_ref(&self, _address: Address) -> Result<Option<AccountInfo>, Self::Error> {
Ok(None)
}

#[inline]
fn code_by_hash(&self, _code_hash: B256) -> Result<Bytecode, Self::Error> {
fn code_by_hash_ref(&self, _code_hash: B256) -> Result<Bytecode, Self::Error> {
Ok(Bytecode::new())
}

#[inline]
fn storage(&self, _address: Address, _index: U256) -> Result<U256, Self::Error> {
fn storage_ref(&self, _address: Address, _index: U256) -> Result<U256, Self::Error> {
Ok(U256::default())
}

#[inline]
fn block_hash(&self, number: U256) -> Result<B256, Self::Error> {
fn block_hash_ref(&self, number: U256) -> Result<B256, Self::Error> {
Ok(number.to_be_bytes().into())
}
}
22 changes: 11 additions & 11 deletions crates/revm/src/db/ethersdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl<M: Middleware> EthersDB<M> {
impl<M: Middleware> DatabaseRef for EthersDB<M> {
type Error = ();

fn basic(&self, address: Address) -> Result<Option<AccountInfo>, Self::Error> {
fn basic_ref(&self, address: Address) -> Result<Option<AccountInfo>, Self::Error> {
let add = eH160::from(address.0 .0);

let f = async {
Expand Down Expand Up @@ -78,12 +78,12 @@ impl<M: Middleware> DatabaseRef for EthersDB<M> {
)))
}

fn code_by_hash(&self, _code_hash: B256) -> Result<Bytecode, Self::Error> {
fn code_by_hash_ref(&self, _code_hash: B256) -> Result<Bytecode, Self::Error> {
panic!("Should not be called. Code is already loaded");
// not needed because we already load code with basic info
}

fn storage(&self, address: Address, index: U256) -> Result<U256, Self::Error> {
fn storage_ref(&self, address: Address, index: U256) -> Result<U256, Self::Error> {
let add = eH160::from(address.0 .0);
let index = H256::from(index.to_be_bytes());
let f = async {
Expand All @@ -97,7 +97,7 @@ impl<M: Middleware> DatabaseRef for EthersDB<M> {
Ok(self.block_on(f))
}

fn block_hash(&self, number: U256) -> Result<B256, Self::Error> {
fn block_hash_ref(&self, number: U256) -> Result<B256, Self::Error> {
// saturate usize
if number > U256::from(u64::MAX) {
return Ok(KECCAK_EMPTY);
Expand All @@ -119,22 +119,22 @@ impl<M: Middleware> Database for EthersDB<M> {

#[inline]
fn basic(&mut self, address: Address) -> Result<Option<AccountInfo>, Self::Error> {
<Self as DatabaseRef>::basic(self, address)
<Self as DatabaseRef>::basic_ref(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)
<Self as DatabaseRef>::code_by_hash_ref(self, code_hash)
}

#[inline]
fn storage(&mut self, address: Address, index: U256) -> Result<U256, Self::Error> {
<Self as DatabaseRef>::storage(self, address, index)
<Self as DatabaseRef>::storage_ref(self, address, index)
}

#[inline]
fn block_hash(&mut self, number: U256) -> Result<B256, Self::Error> {
<Self as DatabaseRef>::block_hash(self, number)
<Self as DatabaseRef>::block_hash_ref(self, number)
}
}

Expand Down Expand Up @@ -166,7 +166,7 @@ mod tests {
.unwrap();
let address = address.as_fixed_bytes().into();

let acc_info = ethersdb.basic(address).unwrap().unwrap();
let acc_info = ethersdb.basic_ref(address).unwrap().unwrap();

// check if not empty
assert!(acc_info.exists());
Expand Down Expand Up @@ -194,7 +194,7 @@ mod tests {

// select test index
let index = U256::from(5);
let storage = ethersdb.storage(address, index).unwrap();
let storage = ethersdb.storage_ref(address, index).unwrap();

// https://etherscan.io/address/0x0d4a11d5EEaaC28EC3F61d100daF4d40471f1852#readContract
// storage[5] -> factory: address
Expand All @@ -219,7 +219,7 @@ mod tests {

// block number to test
let block_num = U256::from(16148323);
let block_hash = ethersdb.block_hash(block_num).unwrap();
let block_hash = ethersdb.block_hash_ref(block_num).unwrap();

// https://etherscan.io/block/16148323
let actual =
Expand Down
32 changes: 16 additions & 16 deletions crates/revm/src/db/in_memory_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl<ExtDB: DatabaseRef> CacheDB<ExtDB> {
match self.accounts.entry(address) {
Entry::Occupied(entry) => Ok(entry.into_mut()),
Entry::Vacant(entry) => Ok(entry.insert(
db.basic(address)?
db.basic_ref(address)?
.map(|info| DbAccount {
info,
..Default::default()
Expand Down Expand Up @@ -170,7 +170,7 @@ impl<ExtDB: DatabaseRef> Database for CacheDB<ExtDB> {
Entry::Occupied(entry) => entry.into_mut(),
Entry::Vacant(entry) => entry.insert(
self.db
.basic(address)?
.basic_ref(address)?
.map(|info| DbAccount {
info,
..Default::default()
Expand All @@ -186,7 +186,7 @@ impl<ExtDB: DatabaseRef> Database for CacheDB<ExtDB> {
Entry::Occupied(entry) => Ok(entry.get().clone()),
Entry::Vacant(entry) => {
// if you return code bytes when basic fn is called this function is not needed.
Ok(entry.insert(self.db.code_by_hash(code_hash)?).clone())
Ok(entry.insert(self.db.code_by_hash_ref(code_hash)?).clone())
}
}
}
Expand All @@ -207,7 +207,7 @@ impl<ExtDB: DatabaseRef> Database for CacheDB<ExtDB> {
) {
Ok(U256::ZERO)
} else {
let slot = self.db.storage(address, index)?;
let slot = self.db.storage_ref(address, index)?;
entry.insert(slot);
Ok(slot)
}
Expand All @@ -216,9 +216,9 @@ impl<ExtDB: DatabaseRef> Database for CacheDB<ExtDB> {
}
Entry::Vacant(acc_entry) => {
// acc needs to be loaded for us to access slots.
let info = self.db.basic(address)?;
let info = self.db.basic_ref(address)?;
let (account, value) = if info.is_some() {
let value = self.db.storage(address, index)?;
let value = self.db.storage_ref(address, index)?;
let mut account: DbAccount = info.into();
account.storage.insert(index, value);
(account, value)
Expand All @@ -235,7 +235,7 @@ impl<ExtDB: DatabaseRef> Database for CacheDB<ExtDB> {
match self.block_hashes.entry(number) {
Entry::Occupied(entry) => Ok(*entry.get()),
Entry::Vacant(entry) => {
let hash = self.db.block_hash(number)?;
let hash = self.db.block_hash_ref(number)?;
entry.insert(hash);
Ok(hash)
}
Expand All @@ -246,21 +246,21 @@ impl<ExtDB: DatabaseRef> Database for CacheDB<ExtDB> {
impl<ExtDB: DatabaseRef> DatabaseRef for CacheDB<ExtDB> {
type Error = ExtDB::Error;

fn basic(&self, address: Address) -> Result<Option<AccountInfo>, Self::Error> {
fn basic_ref(&self, address: Address) -> Result<Option<AccountInfo>, Self::Error> {
match self.accounts.get(&address) {
Some(acc) => Ok(acc.info()),
None => self.db.basic(address),
None => self.db.basic_ref(address),
}
}

fn code_by_hash(&self, code_hash: B256) -> Result<Bytecode, Self::Error> {
fn code_by_hash_ref(&self, code_hash: B256) -> Result<Bytecode, Self::Error> {
match self.contracts.get(&code_hash) {
Some(entry) => Ok(entry.clone()),
None => self.db.code_by_hash(code_hash),
None => self.db.code_by_hash_ref(code_hash),
}
}

fn storage(&self, address: Address, index: U256) -> Result<U256, Self::Error> {
fn storage_ref(&self, address: Address, index: U256) -> Result<U256, Self::Error> {
match self.accounts.get(&address) {
Some(acc_entry) => match acc_entry.storage.get(&index) {
Some(entry) => Ok(*entry),
Expand All @@ -271,18 +271,18 @@ impl<ExtDB: DatabaseRef> DatabaseRef for CacheDB<ExtDB> {
) {
Ok(U256::ZERO)
} else {
self.db.storage(address, index)
self.db.storage_ref(address, index)
}
}
},
None => self.db.storage(address, index),
None => self.db.storage_ref(address, index),
}
}

fn block_hash(&self, number: U256) -> Result<B256, Self::Error> {
fn block_hash_ref(&self, number: U256) -> Result<B256, Self::Error> {
match self.block_hashes.get(&number) {
Some(entry) => Ok(*entry),
None => self.db.block_hash(number),
None => self.db.block_hash_ref(number),
}
}
}
Expand Down
Loading