diff --git a/crates/consensus/auto-seal/src/lib.rs b/crates/consensus/auto-seal/src/lib.rs index 3f2c40d0b1630..e2dfc9c91111f 100644 --- a/crates/consensus/auto-seal/src/lib.rs +++ b/crates/consensus/auto-seal/src/lib.rs @@ -333,7 +333,7 @@ impl StorageInner { bundle_state: &BundleStateWithReceipts, client: &S, gas_used: u64, - #[cfg(feature = "optimism")] chain_spec: Arc, + #[cfg(feature = "optimism")] chain_spec: &ChainSpec, ) -> Result { let receipts = bundle_state.receipts_by_block(header.number); header.receipts_root = if receipts.is_empty() { @@ -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"); diff --git a/crates/payload/basic/src/lib.rs b/crates/payload/basic/src/lib.rs index 0bcda4b9175e6..3f806d5b50dde 100644 --- a/crates/payload/basic/src/lib.rs +++ b/crates/payload/basic/src/lib.rs @@ -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, ) diff --git a/crates/payload/basic/src/optimism.rs b/crates/payload/basic/src/optimism.rs index 59feba96bcd9f..8fade69b70237 100644 --- a/crates/payload/basic/src/optimism.rs +++ b/crates/payload/basic/src/optimism.rs @@ -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"); diff --git a/crates/primitives/src/proofs.rs b/crates/primitives/src/proofs.rs index 7e56b7fd05628..0efc906397f4f 100644 --- a/crates/primitives/src/proofs.rs +++ b/crates/primitives/src/proofs.rs @@ -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, - #[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) { @@ -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, - #[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) { @@ -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); } } @@ -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, ); diff --git a/crates/primitives/src/receipt.rs b/crates/primitives/src/receipt.rs index d5dcce1aaa6de..b92b698f05d70 100644 --- a/crates/primitives/src/receipt.rs +++ b/crates/primitives/src/receipt.rs @@ -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 { + Some(calculate_receipt_root_ref( + &self.receipt_vec[index].iter().map(Option::as_ref).collect::>>()?, + )) + } + + /// 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, - #[cfg(feature = "optimism")] timestamp: u64, + chain_spec: &crate::ChainSpec, + timestamp: u64, ) -> Option { Some(calculate_receipt_root_ref( &self.receipt_vec[index].iter().map(Option::as_ref).collect::>>()?, - #[cfg(feature = "optimism")] chain_spec, - #[cfg(feature = "optimism")] timestamp, )) } diff --git a/crates/revm/src/processor.rs b/crates/revm/src/processor.rs index bb251466e2700..f7395445a425d 100644 --- a/crates/revm/src/processor.rs +++ b/crates/revm/src/processor.rs @@ -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, ); diff --git a/crates/rpc/rpc/src/eth/api/pending_block.rs b/crates/rpc/rpc/src/eth/api/pending_block.rs index 73349c0e5e1d0..7bd2053c9febb 100644 --- a/crates/rpc/rpc/src/eth/api/pending_block.rs +++ b/crates/rpc/rpc/src/eth/api/pending_block.rs @@ -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::(), ) diff --git a/crates/storage/provider/src/bundle_state/bundle_state_with_receipts.rs b/crates/storage/provider/src/bundle_state/bundle_state_with_receipts.rs index c522f0fd50b36..6077a2f6560f2 100644 --- a/crates/storage/provider/src/bundle_state/bundle_state_with_receipts.rs +++ b/crates/storage/provider/src/bundle_state/bundle_state_with_receipts.rs @@ -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 { + 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, - #[cfg(feature = "optimism")] timestamp: u64, + chain_spec: &reth_primitives::ChainSpec, + timestamp: u64, ) -> Option { - 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.