diff --git a/crates/revm/src/database.rs b/crates/revm/src/database.rs index 0486043a5a..455188c634 100644 --- a/crates/revm/src/database.rs +++ b/crates/revm/src/database.rs @@ -42,6 +42,10 @@ impl StateProviderDatabase { impl Database for StateProviderDatabase { type Error = ProviderError; + /// Retrieves basic account information for a given address. + /// + /// Returns `Ok` with `Some(AccountInfo)` if the account exists, + /// `None` if it doesn't, or an error if encountered. fn basic(&mut self, address: Address) -> Result, Self::Error> { Ok(self.0.basic_account(address)?.map(|account| AccountInfo { balance: account.balance, @@ -51,18 +55,24 @@ impl Database for StateProviderDatabase { })) } + /// Retrieves the bytecode associated with a given code hash. + /// + /// Returns `Ok` with the bytecode if found, or the default bytecode otherwise. fn code_by_hash(&mut self, code_hash: B256) -> Result { - let bytecode = self.0.bytecode_by_hash(code_hash)?; - - Ok(bytecode.map(|b| b.0).unwrap_or_else(Bytecode::new)) + Ok(self.0.bytecode_by_hash(code_hash)?.unwrap_or_default().0) } + /// Retrieves the storage value at a specific index for a given address. + /// + /// Returns `Ok` with the storage value, or the default value if not found. fn storage(&mut self, address: Address, index: U256) -> Result { - let index = B256::new(index.to_be_bytes()); - let ret = self.0.storage(address, index)?.unwrap_or_default(); - Ok(ret) + Ok(self.0.storage(address, B256::new(index.to_be_bytes()))?.unwrap_or_default()) } + /// Retrieves the block hash for a given block number. + /// + /// Returns `Ok` with the block hash if found, or the default hash otherwise. + /// Note: It safely casts the `number` to `u64`. fn block_hash(&mut self, number: U256) -> Result { // The `number` represents the block number, so it is safe to cast it to u64. Ok(self.0.block_hash(number.try_into().unwrap())?.unwrap_or_default()) @@ -72,6 +82,10 @@ impl Database for StateProviderDatabase { impl DatabaseRef for StateProviderDatabase { type Error = ::Error; + /// Retrieves basic account information for a given address. + /// + /// Returns `Ok` with `Some(AccountInfo)` if the account exists, + /// `None` if it doesn't, or an error if encountered. fn basic_ref(&self, address: Address) -> Result, Self::Error> { Ok(self.0.basic_account(address)?.map(|account| AccountInfo { balance: account.balance, @@ -81,22 +95,24 @@ impl DatabaseRef for StateProviderDatabase { })) } + /// Retrieves the bytecode associated with a given code hash. + /// + /// Returns `Ok` with the bytecode if found, or the default bytecode otherwise. fn code_by_hash_ref(&self, code_hash: B256) -> Result { - let bytecode = self.0.bytecode_by_hash(code_hash)?; - - if let Some(bytecode) = bytecode { - Ok(bytecode.0) - } else { - Ok(Bytecode::new()) - } + Ok(self.0.bytecode_by_hash(code_hash)?.unwrap_or_default().0) } + /// Retrieves the storage value at a specific index for a given address. + /// + /// Returns `Ok` with the storage value, or the default value if not found. fn storage_ref(&self, address: Address, index: U256) -> Result { - let index = B256::new(index.to_be_bytes()); - let ret = self.0.storage(address, index)?.unwrap_or_default(); - Ok(ret) + Ok(self.0.storage(address, B256::new(index.to_be_bytes()))?.unwrap_or_default()) } + /// Retrieves the block hash for a given block number. + /// + /// Returns `Ok` with the block hash if found, or the default hash otherwise. + /// Note: This operation may potentially be unsafe due to the unwrap operation. fn block_hash_ref(&self, number: U256) -> Result { // Note: this unwrap is potentially unsafe Ok(self.0.block_hash(number.try_into().unwrap())?.unwrap_or_default())