From 7ae8ce1d0096a32211cda406c1f1176cfc217b43 Mon Sep 17 00:00:00 2001 From: Emilia Hane Date: Sun, 17 Nov 2024 17:48:27 +0100 Subject: [PATCH] chore(sdk): Add blanket impls for refs to prim traits (#12613) --- Cargo.lock | 1 + .../execution-types/src/execution_outcome.rs | 58 +++++++++---------- crates/primitives-traits/Cargo.toml | 1 + crates/primitives-traits/src/block/body.rs | 2 +- crates/primitives-traits/src/block/header.rs | 2 - crates/primitives-traits/src/block/mod.rs | 3 +- crates/primitives-traits/src/receipt.rs | 6 +- crates/primitives-traits/src/size.rs | 1 + .../primitives-traits/src/transaction/mod.rs | 1 + .../src/transaction/signed.rs | 29 +++++----- crates/primitives/src/block.rs | 4 +- crates/primitives/src/receipt.rs | 3 + crates/primitives/src/transaction/mod.rs | 6 -- 13 files changed, 57 insertions(+), 60 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7ff1c650bda4..23503d907563 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8582,6 +8582,7 @@ dependencies = [ "alloy-primitives", "alloy-rlp", "arbitrary", + "auto_impl", "bincode", "byteorder", "bytes", diff --git a/crates/evm/execution-types/src/execution_outcome.rs b/crates/evm/execution-types/src/execution_outcome.rs index c1d9c7016501..412269ace9cd 100644 --- a/crates/evm/execution-types/src/execution_outcome.rs +++ b/crates/evm/execution-types/src/execution_outcome.rs @@ -3,7 +3,7 @@ use std::collections::HashMap; use alloy_eips::eip7685::Requests; use alloy_primitives::{Address, BlockNumber, Bloom, Log, B256, U256}; use reth_primitives::{logs_bloom, Account, Bytecode, Receipts, StorageEntry}; -use reth_primitives_traits::Receipt; +use reth_primitives_traits::{receipt::ReceiptExt, Receipt}; use reth_trie::HashedPostState; use revm::{ db::{states::BundleState, BundleAccount}, @@ -182,36 +182,6 @@ impl ExecutionOutcome { Some(index as usize) } - /// Returns an iterator over all block logs. - pub fn logs(&self, block_number: BlockNumber) -> Option> - where - T: Receipt, - { - let index = self.block_number_to_index(block_number)?; - Some(self.receipts[index].iter().filter_map(|r| Some(r.as_ref()?.logs().iter())).flatten()) - } - - /// Return blocks logs bloom - pub fn block_logs_bloom(&self, block_number: BlockNumber) -> Option - where - T: Receipt, - { - Some(logs_bloom(self.logs(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. - pub fn receipts_root_slow(&self, _block_number: BlockNumber) -> Option - where - T: Receipt, - { - #[cfg(feature = "optimism")] - panic!("This should not be called in optimism mode. Use `optimism_receipts_root_slow` instead."); - #[cfg(not(feature = "optimism"))] - self.receipts.root_slow(self.block_number_to_index(_block_number)?, T::receipts_root) - } - /// 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. @@ -364,6 +334,32 @@ impl ExecutionOutcome { } } +impl ExecutionOutcome { + /// Returns an iterator over all block logs. + pub fn logs(&self, block_number: BlockNumber) -> Option> { + let index = self.block_number_to_index(block_number)?; + Some(self.receipts[index].iter().filter_map(|r| Some(r.as_ref()?.logs().iter())).flatten()) + } + + /// Return blocks logs bloom + pub fn block_logs_bloom(&self, block_number: BlockNumber) -> Option { + Some(logs_bloom(self.logs(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. + pub fn receipts_root_slow(&self, _block_number: BlockNumber) -> Option + where + T: ReceiptExt, + { + #[cfg(feature = "optimism")] + panic!("This should not be called in optimism mode. Use `optimism_receipts_root_slow` instead."); + #[cfg(not(feature = "optimism"))] + self.receipts.root_slow(self.block_number_to_index(_block_number)?, T::receipts_root) + } +} + impl From<(BlockExecutionOutput, BlockNumber)> for ExecutionOutcome { fn from(value: (BlockExecutionOutput, BlockNumber)) -> Self { Self { diff --git a/crates/primitives-traits/Cargo.toml b/crates/primitives-traits/Cargo.toml index 30f1c43c86a9..651583f8e4d0 100644 --- a/crates/primitives-traits/Cargo.toml +++ b/crates/primitives-traits/Cargo.toml @@ -27,6 +27,7 @@ byteorder = "1" derive_more.workspace = true roaring = "0.10.2" serde_with = { workspace = true, optional = true } +auto_impl.workspace = true # required by reth-codecs bytes.workspace = true diff --git a/crates/primitives-traits/src/block/body.rs b/crates/primitives-traits/src/block/body.rs index bb52b89724b5..e9aadf409571 100644 --- a/crates/primitives-traits/src/block/body.rs +++ b/crates/primitives-traits/src/block/body.rs @@ -5,6 +5,7 @@ use alloc::fmt; use alloy_consensus::Transaction; /// Abstraction for block's body. +#[auto_impl::auto_impl(&, Arc)] pub trait BlockBody: Send + Sync @@ -19,7 +20,6 @@ pub trait BlockBody: + alloy_rlp::Encodable + alloy_rlp::Decodable + InMemorySize - + 'static { /// Ordered list of signed transactions as committed in block. // todo: requires trait for signed transaction diff --git a/crates/primitives-traits/src/block/header.rs b/crates/primitives-traits/src/block/header.rs index 0c1fc3e57f2a..779df4425388 100644 --- a/crates/primitives-traits/src/block/header.rs +++ b/crates/primitives-traits/src/block/header.rs @@ -26,7 +26,6 @@ pub trait BlockHeader: + alloy_consensus::BlockHeader + Sealable + InMemorySize - + 'static { } @@ -46,6 +45,5 @@ impl BlockHeader for T where + alloy_consensus::BlockHeader + Sealable + InMemorySize - + 'static { } diff --git a/crates/primitives-traits/src/block/mod.rs b/crates/primitives-traits/src/block/mod.rs index 33008c4381dc..6bef9ea167fd 100644 --- a/crates/primitives-traits/src/block/mod.rs +++ b/crates/primitives-traits/src/block/mod.rs @@ -18,6 +18,7 @@ impl FullBlock for T where T: Block + Compact {} // todo: make sealable super-trait, depends on // todo: make with senders extension trait, so block can be impl by block type already containing // senders +#[auto_impl::auto_impl(&, Arc)] pub trait Block: Send + Sync @@ -32,7 +33,7 @@ pub trait Block: + InMemorySize { /// Header part of the block. - type Header: BlockHeader; + type Header: BlockHeader + 'static; /// The block's body contains the transactions in the block. type Body: Send + Sync + Unpin + 'static; diff --git a/crates/primitives-traits/src/receipt.rs b/crates/primitives-traits/src/receipt.rs index f3c9ef063560..31bded015d41 100644 --- a/crates/primitives-traits/src/receipt.rs +++ b/crates/primitives-traits/src/receipt.rs @@ -10,9 +10,10 @@ use serde::{Deserialize, Serialize}; /// Helper trait that unifies all behaviour required by receipt to support full node operations. pub trait FullReceipt: Receipt + Compact {} -impl FullReceipt for T where T: Receipt + Compact {} +impl FullReceipt for T where T: ReceiptExt + Compact {} /// Abstraction of a receipt. +#[auto_impl::auto_impl(&, Arc)] pub trait Receipt: Send + Sync @@ -28,7 +29,10 @@ pub trait Receipt: { /// Returns transaction type. fn tx_type(&self) -> u8; +} +/// Extension if [`Receipt`] used in block execution. +pub trait ReceiptExt: Receipt { /// Calculates the receipts root of the given receipts. fn receipts_root(receipts: &[&Self]) -> B256; } diff --git a/crates/primitives-traits/src/size.rs b/crates/primitives-traits/src/size.rs index 0c250688e057..7d83a8af8c47 100644 --- a/crates/primitives-traits/src/size.rs +++ b/crates/primitives-traits/src/size.rs @@ -1,4 +1,5 @@ /// Trait for calculating a heuristic for the in-memory size of a struct. +#[auto_impl::auto_impl(&, Arc, Box)] pub trait InMemorySize { /// Returns a heuristic for the in-memory size of a struct. fn size(&self) -> usize; diff --git a/crates/primitives-traits/src/transaction/mod.rs b/crates/primitives-traits/src/transaction/mod.rs index 4d7ab78685fa..33ee36090acb 100644 --- a/crates/primitives-traits/src/transaction/mod.rs +++ b/crates/primitives-traits/src/transaction/mod.rs @@ -53,6 +53,7 @@ impl Transaction for T where } /// Extension trait of [`alloy_consensus::Transaction`]. +#[auto_impl::auto_impl(&, Arc)] pub trait TransactionExt: alloy_consensus::Transaction { /// Transaction envelope type ID. type Type: TxType; diff --git a/crates/primitives-traits/src/transaction/signed.rs b/crates/primitives-traits/src/transaction/signed.rs index 455a9886eb8f..958d5cd6c77e 100644 --- a/crates/primitives-traits/src/transaction/signed.rs +++ b/crates/primitives-traits/src/transaction/signed.rs @@ -8,7 +8,7 @@ use alloy_primitives::{keccak256, Address, PrimitiveSignature, TxHash, B256}; use reth_codecs::Compact; use revm_primitives::TxEnv; -use crate::{transaction::TransactionExt, FullTransaction, MaybeArbitrary, Transaction}; +use crate::{FullTransaction, MaybeArbitrary, Transaction}; /// Helper trait that unifies all behaviour required by block to support full node operations. pub trait FullSignedTx: SignedTransaction + Compact {} @@ -16,6 +16,7 @@ pub trait FullSignedTx: SignedTransaction + Compac impl FullSignedTx for T where T: SignedTransaction + Compact {} /// A signed transaction. +#[auto_impl::auto_impl(&, Arc)] pub trait SignedTransaction: Send + Sync @@ -32,7 +33,7 @@ pub trait SignedTransaction: + alloy_rlp::Decodable + Encodable2718 + Decodable2718 - + TransactionExt + + alloy_consensus::Transaction + MaybeArbitrary { /// Transaction type that is signed. @@ -65,14 +66,6 @@ pub trait SignedTransaction: /// `reth_primitives::transaction::recover_signer_unchecked`. fn recover_signer_unchecked(&self) -> Option
; - /// Create a new signed transaction from a transaction and its signature. - /// - /// This will also calculate the transaction hash using its encoding. - fn from_transaction_and_signature( - transaction: Self::Transaction, - signature: PrimitiveSignature, - ) -> Self; - /// Calculate transaction hash, eip2728 transaction does not contain rlp header and start with /// tx type. fn recalculate_hash(&self) -> B256 { @@ -83,10 +76,14 @@ pub trait SignedTransaction: fn fill_tx_env(&self, tx_env: &mut TxEnv, sender: Address); } -impl TransactionExt for T { - type Type = ::Type; - - fn signature_hash(&self) -> B256 { - self.transaction().signature_hash() - } +/// Helper trait used in testing. +#[cfg(feature = "test-utils")] +pub trait SignedTransactionTesting: SignedTransaction { + /// Create a new signed transaction from a transaction and its signature. + /// + /// This will also calculate the transaction hash using its encoding. + fn from_transaction_and_signature( + transaction: Self::Transaction, + signature: PrimitiveSignature, + ) -> Self; } diff --git a/crates/primitives/src/block.rs b/crates/primitives/src/block.rs index d6476c29b4c4..94dd578493c9 100644 --- a/crates/primitives/src/block.rs +++ b/crates/primitives/src/block.rs @@ -465,8 +465,8 @@ where impl reth_primitives_traits::Block for SealedBlock where - H: reth_primitives_traits::BlockHeader, - B: reth_primitives_traits::BlockBody, + H: reth_primitives_traits::BlockHeader + 'static, + B: reth_primitives_traits::BlockBody + 'static, Self: Serialize + for<'a> Deserialize<'a>, { type Header = H; diff --git a/crates/primitives/src/receipt.rs b/crates/primitives/src/receipt.rs index b7138183d111..b61ee7c14d2f 100644 --- a/crates/primitives/src/receipt.rs +++ b/crates/primitives/src/receipt.rs @@ -10,6 +10,7 @@ use alloy_primitives::{Bloom, Log, B256}; use alloy_rlp::{length_of_length, Decodable, Encodable, RlpDecodable, RlpEncodable}; use bytes::{Buf, BufMut}; use derive_more::{DerefMut, From, IntoIterator}; +use reth_primitives_traits::receipt::ReceiptExt; use serde::{Deserialize, Serialize}; #[cfg(feature = "reth-codec")] @@ -97,7 +98,9 @@ impl reth_primitives_traits::Receipt for Receipt { fn tx_type(&self) -> u8 { self.tx_type as u8 } +} +impl ReceiptExt for Receipt { fn receipts_root(_receipts: &[&Self]) -> B256 { #[cfg(feature = "optimism")] panic!("This should not be called in optimism mode. Use `optimism_receipts_root_slow` instead."); diff --git a/crates/primitives/src/transaction/mod.rs b/crates/primitives/src/transaction/mod.rs index f325b72776f5..015621cdcce1 100644 --- a/crates/primitives/src/transaction/mod.rs +++ b/crates/primitives/src/transaction/mod.rs @@ -1367,12 +1367,6 @@ impl SignedTransaction for TransactionSigned { recover_signer_unchecked(&self.signature, signature_hash) } - fn from_transaction_and_signature(transaction: Transaction, signature: Signature) -> Self { - let mut initial_tx = Self { transaction, hash: Default::default(), signature }; - initial_tx.hash = initial_tx.recalculate_hash(); - initial_tx - } - fn fill_tx_env(&self, tx_env: &mut TxEnv, sender: Address) { tx_env.caller = sender; match self.as_ref() {