Skip to content

Commit

Permalink
fix: use real hash for derived from latest (paradigmxyz#6561)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Feb 12, 2024
1 parent b4bb7f0 commit 1c21789
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
7 changes: 5 additions & 2 deletions crates/rpc/rpc/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,9 +410,12 @@ where
let mut at = block.parent_hash;
let mut replay_block_txs = true;

// but if all transactions are to be replayed, we can use the state at the block itself
// if a transaction index is provided, we need to replay the transactions until the index
let num_txs = transaction_index.index().unwrap_or(block.body.len());
if num_txs == block.body.len() {
// but if all transactions are to be replayed, we can use the state at the block itself
// this works with the exception of the PENDING block, because its state might not exist if
// built locally
if !target_block.is_pending() && num_txs == block.body.len() {
at = block.hash();
replay_block_txs = false;
}
Expand Down
6 changes: 3 additions & 3 deletions crates/rpc/rpc/src/eth/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,18 +281,18 @@ where
let latest =
self.provider().latest_header()?.ok_or_else(|| EthApiError::UnknownBlockNumber)?;

let (mut latest_header, _block_hash) = latest.split();
let (mut latest_header, block_hash) = latest.split();
// child block
latest_header.number += 1;
// assumed child block is in the next slot
// assumed child block is in the next slot: 12s
latest_header.timestamp += 12;
// base fee of the child block
let chain_spec = self.provider().chain_spec();

latest_header.base_fee_per_gas = latest_header
.next_block_base_fee(chain_spec.base_fee_params(latest_header.timestamp));

let block_hash = latest_header.hash_slow();
// we're reusing the same block hash because we need this to lookup the block's state
let latest = SealedHeader::new(latest_header, block_hash);

PendingBlockEnvOrigin::DerivedFromLatest(latest)
Expand Down
12 changes: 10 additions & 2 deletions crates/rpc/rpc/src/eth/api/pending_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,12 @@ where
pub(crate) enum PendingBlockEnvOrigin {
/// The pending block as received from the CL.
ActualPending(SealedBlockWithSenders),
/// The header of the latest block
/// The _modified_ header of the latest block.
///
/// This derives the pending state based on the latest header by modifying:
/// - the timestamp
/// - the block number
/// - fees
DerivedFromLatest(SealedHeader),
}

Expand All @@ -330,7 +335,7 @@ impl PendingBlockEnvOrigin {
/// 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.
/// identify the block by its hash (latest block).
pub(crate) fn state_block_id(&self) -> BlockId {
match self {
PendingBlockEnvOrigin::ActualPending(_) => BlockNumberOrTag::Pending.into(),
Expand All @@ -339,6 +344,9 @@ impl PendingBlockEnvOrigin {
}

/// Returns the hash of the block the pending block should be built on.
///
/// For the [PendingBlockEnvOrigin::ActualPending] this is the parent hash of the block.
/// For the [PendingBlockEnvOrigin::DerivedFromLatest] this is the hash of the _latest_ header.
fn build_target_hash(&self) -> B256 {
match self {
PendingBlockEnvOrigin::ActualPending(block) => block.parent_hash,
Expand Down

0 comments on commit 1c21789

Please sign in to comment.