Skip to content

Commit

Permalink
@mattsse review
Browse files Browse the repository at this point in the history
  • Loading branch information
clabby committed Nov 24, 2023
1 parent 4a30b48 commit 5964130
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 27 deletions.
4 changes: 2 additions & 2 deletions crates/consensus/auto-seal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ impl StorageInner {
bundle_state: &BundleStateWithReceipts,
client: &S,
gas_used: u64,
#[cfg(feature = "optimism")] chain_spec: Arc<ChainSpec>,
#[cfg(feature = "optimism")] chain_spec: &ChainSpec,
) -> Result<Header, BlockExecutionError> {
let receipts = bundle_state.receipts_by_block(header.number);
header.receipts_root = if receipts.is_empty() {
Expand Down Expand Up @@ -405,7 +405,7 @@ impl StorageInner {
client,
gas_used,
#[cfg(feature = "optimism")]
chain_spec,
chain_spec.as_ref(),
)?;

trace!(target: "consensus::auto", root=?header.state_root, ?body, "calculated root");
Expand Down
2 changes: 1 addition & 1 deletion crates/payload/basic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,7 @@ where
.receipts_root_slow(
block_number,
#[cfg(feature = "optimism")]
chain_spec.clone(),
chain_spec.as_ref(),
#[cfg(feature = "optimism")]
attributes.timestamp,
)
Expand Down
2 changes: 1 addition & 1 deletion crates/payload/basic/src/optimism.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ where
block_number,
);
let receipts_root = bundle
.receipts_root_slow(block_number, chain_spec.clone(), attributes.timestamp)
.receipts_root_slow(block_number, chain_spec.as_ref(), attributes.timestamp)
.expect("Number is in range");
let logs_bloom = bundle.block_logs_bloom(block_number).expect("Number is in range");

Expand Down
32 changes: 24 additions & 8 deletions crates/primitives/src/proofs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,23 @@ pub fn calculate_withdrawals_root(withdrawals: &[Withdrawal]) -> B256 {
}

/// Calculates the receipt root for a header.
#[cfg(not(feature = "optimism"))]
pub fn calculate_receipt_root(receipts: &[ReceiptWithBloom]) -> B256 {
ordered_trie_root_with_encoder(receipts, |r, buf| r.encode_inner(buf, false))
}

/// Calculates the receipt root for a header.
#[cfg(feature = "optimism")]
pub fn calculate_receipt_root(
receipts: &[ReceiptWithBloom],
#[cfg(feature = "optimism")] chain_spec: std::sync::Arc<crate::ChainSpec>,
#[cfg(feature = "optimism")] timestamp: u64,
chain_spec: &crate::ChainSpec,
timestamp: u64,
) -> B256 {
// There is a minor bug in op-geth and op-erigon where in the Regolith hardfork,
// the receipt root calculation does not include the deposit nonce in the receipt
// encoding. In the Regolith Hardfork, we must strip the deposit nonce from the
// receipts before calculating the receipt root. This was corrected in the Canyon
// hardfork.
#[cfg(feature = "optimism")]
if chain_spec.is_fork_active_at_timestamp(crate::Hardfork::Regolith, timestamp) &&
!chain_spec.is_fork_active_at_timestamp(crate::Hardfork::Canyon, timestamp)
{
Expand All @@ -103,17 +109,27 @@ pub fn calculate_receipt_root(
/// Calculates the receipt root for a header for the reference type of [Receipt].
///
/// NOTE: Prefer [calculate_receipt_root] if you have log blooms memoized.
#[cfg(not(feature = "optimism"))]
pub fn calculate_receipt_root_ref(receipts: &[&Receipt]) -> B256 {
ordered_trie_root_with_encoder(receipts, |r, buf| {
ReceiptWithBloomRef::from(*r).encode_inner(buf, false)
})
}

/// Calculates the receipt root for a header for the reference type of [Receipt].
///
/// NOTE: Prefer [calculate_receipt_root] if you have log blooms memoized.
#[cfg(feature = "optimism")]
pub fn calculate_receipt_root_ref(
receipts: &[&Receipt],
#[cfg(feature = "optimism")] chain_spec: std::sync::Arc<crate::ChainSpec>,
#[cfg(feature = "optimism")] timestamp: u64,
chain_spec: &crate::ChainSpec,
timestamp: u64,
) -> B256 {
// There is a minor bug in op-geth and op-erigon where in the Regolith hardfork,
// the receipt root calculation does not include the deposit nonce in the receipt
// encoding. In the Regolith Hardfork, we must strip the deposit nonce from the
// receipts before calculating the receipt root. This was corrected in the Canyon
// hardfork.
#[cfg(feature = "optimism")]
if chain_spec.is_fork_active_at_timestamp(crate::Hardfork::Regolith, timestamp) &&
!chain_spec.is_fork_active_at_timestamp(crate::Hardfork::Canyon, timestamp)
{
Expand Down Expand Up @@ -465,7 +481,7 @@ mod tests {
bloom: Bloom(hex!("00000000000000000000000000000000400000000000000000000000000000000000004000000000000001000000000000000002000000000100000000000000000000000000000000000008000000000000000000000000000000000000000004000000020000000000000000000800000000000000000000000010200100200008000002000000000000000000800000000000000000000002000000000000000000000000000000080000000000000000000000004000000000000000000000000002000000000000000000000000000000000000200000000000000020002000000000000000002000000000000000000000000000000000000000000000").into()),
},
];
let root = calculate_receipt_root(&receipts, OP_GOERLI.clone(), case.1);
let root = calculate_receipt_root(&receipts, OP_GOERLI.as_ref(), case.1);
assert_eq!(root, case.2);
}
}
Expand All @@ -489,7 +505,7 @@ mod tests {
let root = calculate_receipt_root(
&receipt,
#[cfg(feature = "optimism")]
crate::OP_GOERLI.clone(),
crate::OP_GOERLI.as_ref(),
#[cfg(feature = "optimism")]
0,
);
Expand Down
15 changes: 11 additions & 4 deletions crates/primitives/src/receipt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,24 @@ impl Receipts {
}

/// Retrieves the receipt root for all recorded receipts from index.
#[cfg(not(feature = "optimism"))]
pub fn root_slow(&self, index: usize) -> Option<B256> {
Some(calculate_receipt_root_ref(
&self.receipt_vec[index].iter().map(Option::as_ref).collect::<Option<Vec<_>>>()?,
))
}

/// Retrieves the receipt root for all recorded receipts from index.
#[cfg(feature = "optimism")]
pub fn root_slow(
&self,
index: usize,
#[cfg(feature = "optimism")] chain_spec: std::sync::Arc<crate::ChainSpec>,
#[cfg(feature = "optimism")] timestamp: u64,
chain_spec: &crate::ChainSpec,
timestamp: u64,
) -> Option<B256> {
Some(calculate_receipt_root_ref(
&self.receipt_vec[index].iter().map(Option::as_ref).collect::<Option<Vec<_>>>()?,
#[cfg(feature = "optimism")]
chain_spec,
#[cfg(feature = "optimism")]
timestamp,
))
}
Expand Down
2 changes: 1 addition & 1 deletion crates/revm/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ pub fn verify_receipt<'a>(
let receipts_root = reth_primitives::proofs::calculate_receipt_root(
&receipts_with_bloom,
#[cfg(feature = "optimism")]
chain_spec,
chain_spec.as_ref(),
#[cfg(feature = "optimism")]
timestamp,
);
Expand Down
2 changes: 1 addition & 1 deletion crates/rpc/rpc/src/eth/api/pending_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ impl PendingBlockEnv {
.receipts_root_slow(
block_number,
#[cfg(feature = "optimism")]
chain_spec.clone(),
chain_spec.as_ref(),
#[cfg(feature = "optimism")]
block_env.timestamp.to::<u64>(),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,19 +250,22 @@ impl BundleStateWithReceipts {
/// Returns the receipt root for all recorded receipts.
/// Note: this function calculated Bloom filters for every receipt and created merkle trees
/// of receipt. This is a expensive operation.
#[cfg(not(feature = "optimism"))]
pub fn receipts_root_slow(&self, block_number: BlockNumber) -> Option<B256> {
self.receipts.root_slow(self.block_number_to_index(block_number)?)
}

/// Returns the receipt root for all recorded receipts.
/// Note: this function calculated Bloom filters for every receipt and created merkle trees
/// of receipt. This is a expensive operation.
#[cfg(feature = "optimism")]
pub fn receipts_root_slow(
&self,
block_number: BlockNumber,
#[cfg(feature = "optimism")] chain_spec: std::sync::Arc<reth_primitives::ChainSpec>,
#[cfg(feature = "optimism")] timestamp: u64,
chain_spec: &reth_primitives::ChainSpec,
timestamp: u64,
) -> Option<B256> {
self.receipts.root_slow(
self.block_number_to_index(block_number)?,
#[cfg(feature = "optimism")]
chain_spec,
#[cfg(feature = "optimism")]
timestamp,
)
self.receipts.root_slow(self.block_number_to_index(block_number)?, chain_spec, timestamp)
}

/// Return reference to receipts.
Expand Down

0 comments on commit 5964130

Please sign in to comment.