Skip to content

Commit

Permalink
chore: cleanup host blockhash fn (#1430)
Browse files Browse the repository at this point in the history
  • Loading branch information
rakita authored May 17, 2024
1 parent 05f3c5b commit 2239820
Showing 1 changed file with 30 additions and 32 deletions.
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);

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

0 comments on commit 2239820

Please sign in to comment.