Skip to content

Commit

Permalink
fix: use pending block tag for actual pending block (#4966)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Oct 11, 2023
1 parent 39f566e commit e042922
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 11 deletions.
5 changes: 3 additions & 2 deletions crates/rpc/rpc/src/eth/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,9 @@ where

let mut cfg = CfgEnv::default();
let mut block_env = BlockEnv::default();
self.provider().fill_block_env_with_header(&mut block_env, origin.header())?;
self.provider().fill_cfg_env_with_header(&mut cfg, origin.header())?;
// Note: for the PENDING block we assume it is past the known merge block and thus this will
// not fail when looking up the total difficulty value for the blockenv.
self.provider().fill_env_with_header(&mut cfg, &mut block_env, origin.header())?;

Ok(PendingBlockEnv { cfg, block_env, origin })
}
Expand Down
17 changes: 14 additions & 3 deletions crates/rpc/rpc/src/eth/api/pending_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use crate::eth::error::{EthApiError, EthResult};
use core::fmt::Debug;
use reth_primitives::{
constants::{eip4844::MAX_DATA_GAS_PER_BLOCK, BEACON_NONCE},
proofs, Block, ChainSpec, Header, IntoRecoveredTransaction, Receipt, Receipts, SealedBlock,
SealedHeader, B256, EMPTY_OMMER_ROOT, U256,
proofs, Block, BlockId, BlockNumberOrTag, ChainSpec, Header, IntoRecoveredTransaction, Receipt,
Receipts, SealedBlock, SealedHeader, B256, EMPTY_OMMER_ROOT, U256,
};
use reth_provider::{BundleStateWithReceipts, ChainSpecProvider, StateProviderFactory};
use reth_revm::{
Expand Down Expand Up @@ -305,7 +305,18 @@ impl PendingBlockEnvOrigin {
}
}

/// Returns the hash of the pending block should be built on
/// Returns the [BlockId] that represents the state of the block.
///
/// If this is the actual pending block, the state is the "Pending" tag, otherwise we can safely
/// identify the block by its hash.
pub(crate) fn state_block_id(&self) -> BlockId {
match self {
PendingBlockEnvOrigin::ActualPending(_) => BlockNumberOrTag::Pending.into(),
PendingBlockEnvOrigin::DerivedFromLatest(header) => BlockId::Hash(header.hash.into()),
}
}

/// Returns the hash of the block the pending block should be built on.
fn build_target_hash(&self) -> B256 {
match self {
PendingBlockEnvOrigin::ActualPending(block) => block.parent_hash,
Expand Down
6 changes: 4 additions & 2 deletions crates/rpc/rpc/src/eth/api/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@ pub trait EthTransactions: Send + Sync {

/// Returns the revm evm env for the requested [BlockId]
///
/// If the [BlockId] this will return the [BlockId::Hash] of the block the env was configured
/// If the [BlockId] this will return the [BlockId] of the block the env was configured
/// for.
/// If the [BlockId] is pending, this will return the "Pending" tag, otherwise this returns the
/// hash of the exact block.
async fn evm_env_at(&self, at: BlockId) -> EthResult<(CfgEnv, BlockEnv, BlockId)>;

/// Returns the revm evm env for the raw block header
Expand Down Expand Up @@ -279,7 +281,7 @@ where
async fn evm_env_at(&self, at: BlockId) -> EthResult<(CfgEnv, BlockEnv, BlockId)> {
if at.is_pending() {
let PendingBlockEnv { cfg, block_env, origin } = self.pending_block_env_and_cfg()?;
Ok((cfg, block_env, origin.header().hash.into()))
Ok((cfg, block_env, origin.state_block_id()))
} else {
// Use cached values if there is no pending block
let block_hash = self
Expand Down
12 changes: 9 additions & 3 deletions crates/storage/provider/src/providers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,14 +521,20 @@ where
state
}

/// Storage provider for pending state.
/// Returns the state provider for pending state.
///
/// If there's no pending block available then the latest state provider is returned:
/// [Self::latest]
fn pending(&self) -> RethResult<StateProviderBox<'_>> {
trace!(target: "providers::blockchain", "Getting provider for pending state");

if let Some(block) = self.tree.pending_block_num_hash() {
let pending = self.tree.pending_state_provider(block.hash)?;
return self.pending_with_provider(pending)
if let Ok(pending) = self.tree.pending_state_provider(block.hash) {
return self.pending_with_provider(pending)
}
}

// fallback to latest state if the pending block is not available
self.latest()
}

Expand Down
3 changes: 2 additions & 1 deletion crates/storage/provider/src/traits/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ pub trait StateProviderFactory: BlockIdReader + Send + Sync {

/// Returns a [StateProvider] indexed by the given [BlockId].
///
/// Note: if a number or hash is provided this will only look at historical(canonical) state.
/// Note: if a number or hash is provided this will __only__ look at historical(canonical)
/// state.
fn state_by_block_id(&self, block_id: BlockId) -> RethResult<StateProviderBox<'_>> {
match block_id {
BlockId::Number(block_number) => self.state_by_block_number_or_tag(block_number),
Expand Down

0 comments on commit e042922

Please sign in to comment.