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: better conversion error handling in block_hash_ref #5870

Merged
merged 4 commits into from
Dec 28, 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
5 changes: 4 additions & 1 deletion crates/interfaces/src/provider.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use reth_primitives::{
Address, BlockHash, BlockHashOrNumber, BlockNumber, GotExpected, SnapshotSegment,
TxHashOrNumber, TxNumber, B256,
TxHashOrNumber, TxNumber, B256, U256,
};
use std::path::PathBuf;
use thiserror::Error;
Expand Down Expand Up @@ -122,6 +122,9 @@ pub enum ProviderError {
/// Snapshot file is not found for requested transaction.
#[error("not able to find {0} snapshot file for transaction id {1}")]
MissingSnapshotTx(SnapshotSegment, TxNumber),
/// Error encountered when the block number conversion from U256 to u64 causes an overflow.
#[error("failed to convert block number U256 to u64: {0}")]
BlockNumberOverflow(U256),
}

impl From<reth_nippy_jar::NippyJarError> for ProviderError {
Expand Down
20 changes: 15 additions & 5 deletions crates/revm/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,13 @@ impl<DB: StateProvider> Database for StateProviderDatabase<DB> {
/// 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<B256, Self::Error> {
// 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())
// Attempt to convert U256 to u64
let block_number = match number.try_into() {
Ok(value) => value,
Err(_) => return Err(Self::Error::BlockNumberOverflow(number)),
};

Ok(self.0.block_hash(block_number)?.unwrap_or_default())
}
}

Expand Down Expand Up @@ -112,9 +117,14 @@ impl<DB: StateProvider> DatabaseRef for StateProviderDatabase<DB> {
/// 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<B256, Self::Error> {
// Note: this unwrap is potentially unsafe
Ok(self.0.block_hash(number.try_into().unwrap())?.unwrap_or_default())
// Attempt to convert U256 to u64
let block_number = match number.try_into() {
Ok(value) => value,
Err(_) => return Err(Self::Error::BlockNumberOverflow(number)),
};

// Get the block hash or default hash
Ok(self.0.block_hash(block_number)?.unwrap_or_default())
}
}
Loading