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

chore: cleanup host blockhash fn #1430

Merged
merged 1 commit into from
May 17, 2024
Merged
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
62 changes: 30 additions & 32 deletions crates/revm/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,39 +110,37 @@ impl<EXT, DB: Database> Host for Context<EXT, DB> {
}

fn block_hash(&mut self, number: U256) -> Option<B256> {
let block_number = self.env().block.number;

match block_number.checked_sub(number) {
// blockhash should push zero if number is same as current block number.
Some(diff) if !diff.is_zero() => {
let diff = as_usize_saturated!(diff);

if diff <= BLOCK_HASH_HISTORY {
return self
.evm
.block_hash(number)
.map_err(|e| self.evm.error = Err(e))
.ok();
}

if self.evm.journaled_state.spec.is_enabled_in(PRAGUE)
&& diff <= BLOCKHASH_SERVE_WINDOW
{
let index = number.wrapping_rem(U256::from(BLOCKHASH_SERVE_WINDOW));
return self
.evm
.db
.storage(BLOCKHASH_STORAGE_ADDRESS, index)
.map_err(|e| self.evm.error = Err(EVMError::Database(e)))
.ok()
.map(|v| v.into());
}
}
_ => {
// If blockhash is requested for the current block, the hash should be 0, so we fall
// through.
}
let block_number = as_usize_saturated!(self.env().block.number);
let requested_number = as_usize_saturated!(number);
Copy link
Collaborator

@DaniPopes DaniPopes May 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

technically this breaks blockhash when block number is > usize::MAX because diff will always saturate to 0.


let Some(diff) = block_number.checked_sub(requested_number) else {
return Some(B256::ZERO);
};

// blockhash should push zero if number is same as current block number.
if diff == 0 {
return Some(B256::ZERO);
}

if diff <= BLOCK_HASH_HISTORY {
return self
.evm
.block_hash(number)
.map_err(|e| self.evm.error = Err(e))
.ok();
}

if self.evm.journaled_state.spec.is_enabled_in(PRAGUE) && diff <= BLOCKHASH_SERVE_WINDOW {
let index = number.wrapping_rem(U256::from(BLOCKHASH_SERVE_WINDOW));
return self
.evm
.db
.storage(BLOCKHASH_STORAGE_ADDRESS, index)
.map_err(|e| self.evm.error = Err(EVMError::Database(e)))
.ok()
.map(|v| v.into());
}

Some(B256::ZERO)
}

Expand Down
Loading