From 8f854cbbb6e410e7d6776f3ff1fa46fcb312cbb5 Mon Sep 17 00:00:00 2001 From: DevOrbitlabs Date: Fri, 3 Jan 2025 22:57:59 +0700 Subject: [PATCH] feat: merge BlockBodyTxExt trait into BlockBody (#13613) Co-authored-by: Matthias Seitz --- Cargo.lock | 1 + crates/node/core/Cargo.toml | 2 +- crates/primitives-traits/Cargo.toml | 4 +++ crates/primitives-traits/src/block/body.rs | 38 +++++++++++++++++++++- crates/primitives/src/block.rs | 4 +-- crates/primitives/src/traits.rs | 31 +----------------- 6 files changed, 46 insertions(+), 34 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 88398a9ae32a..085ce63a929a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8782,6 +8782,7 @@ dependencies = [ "proptest", "proptest-arbitrary-interop", "rand 0.8.5", + "rayon", "reth-codecs", "revm-primitives", "secp256k1", diff --git a/crates/node/core/Cargo.toml b/crates/node/core/Cargo.toml index 96919b7de7e0..7d4a417bed80 100644 --- a/crates/node/core/Cargo.toml +++ b/crates/node/core/Cargo.toml @@ -15,7 +15,7 @@ workspace = true reth-chainspec.workspace = true reth-consensus.workspace = true reth-primitives.workspace = true -reth-primitives-traits.workspace = true +reth-primitives-traits = { workspace = true, features = ["rayon"] } reth-cli-util.workspace = true reth-db = { workspace = true, features = ["mdbx"] } reth-storage-errors.workspace = true diff --git a/crates/primitives-traits/Cargo.toml b/crates/primitives-traits/Cargo.toml index a84394033704..23589be58283 100644 --- a/crates/primitives-traits/Cargo.toml +++ b/crates/primitives-traits/Cargo.toml @@ -49,6 +49,7 @@ serde = { workspace = true, optional = true} arbitrary = { workspace = true, features = ["derive"], optional = true } proptest = { workspace = true, optional = true } proptest-arbitrary-interop = { workspace = true, optional = true } +rayon = { workspace = true, optional = true } [dev-dependencies] reth-codecs.workspace = true @@ -142,3 +143,6 @@ reth-codec = [ op = [ "dep:op-alloy-consensus", ] +rayon = [ + "dep:rayon", +] diff --git a/crates/primitives-traits/src/block/body.rs b/crates/primitives-traits/src/block/body.rs index 37342733b4c9..f1f83d46e280 100644 --- a/crates/primitives-traits/src/block/body.rs +++ b/crates/primitives-traits/src/block/body.rs @@ -6,13 +6,16 @@ use crate::{ use alloc::{fmt, vec::Vec}; use alloy_consensus::{Header, Transaction}; use alloy_eips::{eip2718::Encodable2718, eip4895::Withdrawals}; -use alloy_primitives::{Bytes, B256}; +use alloy_primitives::{Address, Bytes, B256}; /// Helper trait that unifies all behaviour required by transaction to support full node operations. pub trait FullBlockBody: BlockBody + MaybeSerdeBincodeCompat {} impl FullBlockBody for T where T: BlockBody + MaybeSerdeBincodeCompat {} +#[cfg(feature = "rayon")] +use rayon::prelude::*; + /// Abstraction for block's body. pub trait BlockBody: Send @@ -97,6 +100,39 @@ pub trait BlockBody: fn encoded_2718_transactions(&self) -> Vec { self.encoded_2718_transactions_iter().map(Into::into).collect() } + + /// Recover signer addresses for all transactions in the block body. + fn recover_signers(&self) -> Option> + where + Self::Transaction: SignedTransaction, + { + #[cfg(feature = "rayon")] + { + self.transactions().into_par_iter().map(|tx| tx.recover_signer()).collect() + } + #[cfg(not(feature = "rayon"))] + { + self.transactions().iter().map(|tx| tx.recover_signer()).collect() + } + } + + /// Recover signer addresses for all transactions in the block body _without ensuring that the + /// signature has a low `s` value_. + /// + /// Returns `None`, if some transaction's signature is invalid. + fn recover_signers_unchecked(&self) -> Option> + where + Self::Transaction: SignedTransaction, + { + #[cfg(feature = "rayon")] + { + self.transactions().into_par_iter().map(|tx| tx.recover_signer_unchecked()).collect() + } + #[cfg(not(feature = "rayon"))] + { + self.transactions().iter().map(|tx| tx.recover_signer_unchecked()).collect() + } + } } impl BlockBody for alloy_consensus::BlockBody diff --git a/crates/primitives/src/block.rs b/crates/primitives/src/block.rs index ff3d5ed704b9..acf76b1e591e 100644 --- a/crates/primitives/src/block.rs +++ b/crates/primitives/src/block.rs @@ -1,6 +1,6 @@ use crate::{ - traits::BlockExt, transaction::SignedTransactionIntoRecoveredExt, BlockBodyTxExt, GotExpected, - RecoveredTx, SealedHeader, TransactionSigned, + traits::BlockExt, transaction::SignedTransactionIntoRecoveredExt, GotExpected, RecoveredTx, + SealedHeader, TransactionSigned, }; use alloc::vec::Vec; use alloy_consensus::Header; diff --git a/crates/primitives/src/traits.rs b/crates/primitives/src/traits.rs index d0d9c211b539..08a8ab3e665a 100644 --- a/crates/primitives/src/traits.rs +++ b/crates/primitives/src/traits.rs @@ -1,7 +1,4 @@ -use crate::{ - transaction::{recover_signers, recover_signers_unchecked}, - BlockWithSenders, SealedBlock, -}; +use crate::{BlockWithSenders, SealedBlock}; use alloc::vec::Vec; use reth_primitives_traits::{Block, BlockBody, SealedHeader, SignedTransaction}; use revm_primitives::{Address, B256}; @@ -52,7 +49,6 @@ pub trait BlockExt: Block { /// /// If the number of senders does not match the number of transactions in the block, this falls /// back to manually recovery, but _without ensuring that the signature has a low `s` value_. - /// See also [`recover_signers_unchecked`] /// /// Returns an error if a signature is invalid. #[track_caller] @@ -87,28 +83,3 @@ pub trait BlockExt: Block { } impl BlockExt for T {} - -/// Extension trait for [`BlockBody`] adding helper methods operating with transactions. -pub trait BlockBodyTxExt: BlockBody { - /// Recover signer addresses for all transactions in the block body. - fn recover_signers(&self) -> Option> - where - Self::Transaction: SignedTransaction, - { - recover_signers(self.transactions(), self.transactions().len()) - } - - /// Recover signer addresses for all transactions in the block body _without ensuring that the - /// signature has a low `s` value_. - /// - /// Returns `None`, if some transaction's signature is invalid, see also - /// [`recover_signers_unchecked`]. - fn recover_signers_unchecked(&self) -> Option> - where - Self::Transaction: SignedTransaction, - { - recover_signers_unchecked(self.transactions(), self.transactions().len()) - } -} - -impl BlockBodyTxExt for T {}