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 1 commit
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
6 changes: 3 additions & 3 deletions crates/interfaces/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +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 during conversion from U256 to u64.
#[error("failed to convert U256 to u64")]
ConversionError(U256),
/// Error encountered when the block number conversion from U256 to u64 causes an overflow.
#[error("Failed to convert block number U256 to u64: {0}")]
DaniPopes marked this conversation as resolved.
Show resolved Hide resolved
BlockNumberOverflow(U256),
}

impl From<reth_nippy_jar::NippyJarError> for ProviderError {
Expand Down
16 changes: 9 additions & 7 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 @@ -116,13 +121,10 @@ impl<DB: StateProvider> DatabaseRef for StateProviderDatabase<DB> {
// Attempt to convert U256 to u64
let block_number = match number.try_into() {
Ok(value) => value,
Err(_) => return Err(Self::Error::ConversionError(number)),
Err(_) => return Err(Self::Error::BlockNumberOverflow(number)),
};

// Get the block hash or default hash
match self.0.block_hash(block_number)? {
Some(hash) => Ok(hash),
None => Ok(B256::default()),
}
Ok(self.0.block_hash(block_number)?.unwrap_or_default())
}
}
Loading