From 7ed01744a32c2f21bfcef2e1aa8a6d09d8ddff66 Mon Sep 17 00:00:00 2001 From: Khanh Hoa Date: Wed, 1 Jan 2025 10:44:00 +0700 Subject: [PATCH 1/9] bet --- crates/primitives/Cargo.toml | 4 +++- crates/primitives/src/block.rs | 6 +++--- crates/primitives/src/traits.rs | 26 ++++++++++++-------------- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/crates/primitives/Cargo.toml b/crates/primitives/Cargo.toml index e7036f1752bc..6b243ba7d256 100644 --- a/crates/primitives/Cargo.toml +++ b/crates/primitives/Cargo.toml @@ -53,7 +53,7 @@ derive_more.workspace = true modular-bitfield = { workspace = true, optional = true } once_cell.workspace = true rand = { workspace = true, optional = true } -rayon.workspace = true +rayon = {workspace = true, optional = true } serde.workspace = true serde_with = { workspace = true, optional = true } @@ -183,6 +183,8 @@ serde-bincode-compat = [ "reth-primitives-traits/serde-bincode-compat", "reth-trie-common/serde-bincode-compat", ] +rayon = ["dep:rayon"] + [[bench]] name = "recover_ecdsa_crit" diff --git a/crates/primitives/src/block.rs b/crates/primitives/src/block.rs index 399ebaa24af9..bd1807d5cb7f 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; @@ -259,7 +259,7 @@ where impl SealedBlock where H: reth_primitives_traits::BlockHeader, - B: reth_primitives_traits::BlockBody, + B: reth_primitives_traits::BlockBody + BlockExt, { /// Expensive operation that recovers transaction signer. See [`SealedBlockWithSenders`]. pub fn senders(&self) -> Option> diff --git a/crates/primitives/src/traits.rs b/crates/primitives/src/traits.rs index 3f009bba84bb..d0cf71a5911c 100644 --- a/crates/primitives/src/traits.rs +++ b/crates/primitives/src/traits.rs @@ -29,7 +29,7 @@ pub trait BlockExt: Block { where ::Transaction: SignedTransaction, { - self.body().recover_signers() + self.recover_signers() } /// Transform into a [`BlockWithSenders`]. @@ -66,7 +66,7 @@ pub trait BlockExt: Block { let senders = if self.body().transactions().len() == senders.len() { senders } else { - let Some(senders) = self.body().recover_signers_unchecked() else { return Err(self) }; + let Some(senders) = self.recover_signers_unchecked() else { return Err(self) }; senders }; @@ -84,31 +84,29 @@ pub trait BlockExt: Block { let senders = self.senders()?; Some(BlockWithSenders::new_unchecked(self, senders)) } -} - -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. + #[cfg(feature = "rayon")] fn recover_signers(&self) -> Option> where - Self::Transaction: SignedTransaction, + ::Transaction: SignedTransaction, { - recover_signers(self.transactions(), self.transactions().len()) + let txs = self.body().transactions(); + recover_signers(txs, txs.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`]. + /// Returns `None`, if some transaction's signature is invalid. + #[cfg(feature = "rayon")] fn recover_signers_unchecked(&self) -> Option> where - Self::Transaction: SignedTransaction, + ::Transaction: SignedTransaction, { - recover_signers_unchecked(self.transactions(), self.transactions().len()) + let txs = self.body().transactions(); + recover_signers_unchecked(txs, txs.len()) } } -impl BlockBodyTxExt for T {} +impl BlockExt for T {} From be20af241edf1faa9115bc6cde6f808dcdfd7c1f Mon Sep 17 00:00:00 2001 From: Khanh Hoa Date: Wed, 1 Jan 2025 11:29:49 +0700 Subject: [PATCH 2/9] bet --- Cargo.lock | 1 + crates/primitives-traits/Cargo.toml | 4 +++ crates/primitives-traits/src/block/body.rs | 23 ++++++++++++++++- crates/primitives/src/block.rs | 2 +- crates/primitives/src/traits.rs | 29 +++------------------- 5 files changed, 31 insertions(+), 28 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 95a570376bef..2cb60b9436d2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8773,6 +8773,7 @@ dependencies = [ "proptest", "proptest-arbitrary-interop", "rand 0.8.5", + "rayon", "reth-codecs", "revm-primitives", "secp256k1", 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..bc03c3758477 100644 --- a/crates/primitives-traits/src/block/body.rs +++ b/crates/primitives-traits/src/block/body.rs @@ -6,7 +6,7 @@ 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 {} @@ -97,6 +97,27 @@ 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. + #[cfg(feature = "rayon")] + 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 BlockBody for alloy_consensus::BlockBody diff --git a/crates/primitives/src/block.rs b/crates/primitives/src/block.rs index bd1807d5cb7f..29e3117d10e8 100644 --- a/crates/primitives/src/block.rs +++ b/crates/primitives/src/block.rs @@ -259,7 +259,7 @@ where impl SealedBlock where H: reth_primitives_traits::BlockHeader, - B: reth_primitives_traits::BlockBody + BlockExt, + B: reth_primitives_traits::BlockBody, { /// Expensive operation that recovers transaction signer. See [`SealedBlockWithSenders`]. pub fn senders(&self) -> Option> diff --git a/crates/primitives/src/traits.rs b/crates/primitives/src/traits.rs index d0cf71a5911c..e35fb2178f7a 100644 --- a/crates/primitives/src/traits.rs +++ b/crates/primitives/src/traits.rs @@ -29,7 +29,7 @@ pub trait BlockExt: Block { where ::Transaction: SignedTransaction, { - self.recover_signers() + self.body().recover_signers() } /// Transform into a [`BlockWithSenders`]. @@ -66,7 +66,7 @@ pub trait BlockExt: Block { let senders = if self.body().transactions().len() == senders.len() { senders } else { - let Some(senders) = self.recover_signers_unchecked() else { return Err(self) }; + let Some(senders) = self.body().recover_signers_unchecked() else { return Err(self) }; senders }; @@ -84,29 +84,6 @@ pub trait BlockExt: Block { let senders = self.senders()?; Some(BlockWithSenders::new_unchecked(self, senders)) } - - /// Recover signer addresses for all transactions in the block body. - #[cfg(feature = "rayon")] - fn recover_signers(&self) -> Option> - where - ::Transaction: SignedTransaction, - { - let txs = self.body().transactions(); - recover_signers(txs, txs.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. - #[cfg(feature = "rayon")] - fn recover_signers_unchecked(&self) -> Option> - where - ::Transaction: SignedTransaction, - { - let txs = self.body().transactions(); - recover_signers_unchecked(txs, txs.len()) - } } -impl BlockExt for T {} +impl BlockExt for T {} \ No newline at end of file From 8ef540481b71d18835377bed3e579f7d148aadc6 Mon Sep 17 00:00:00 2001 From: Khanh Hoa Date: Wed, 1 Jan 2025 15:28:39 +0700 Subject: [PATCH 3/9] bet --- crates/primitives-traits/src/block/body.rs | 25 +++++++++++++++++----- crates/primitives/src/traits.rs | 7 ++---- crates/primitives/src/transaction/mod.rs | 9 +++++++- 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/crates/primitives-traits/src/block/body.rs b/crates/primitives-traits/src/block/body.rs index bc03c3758477..f1f83d46e280 100644 --- a/crates/primitives-traits/src/block/body.rs +++ b/crates/primitives-traits/src/block/body.rs @@ -13,6 +13,9 @@ pub trait FullBlockBody: BlockBody + MaybeSerdeBincod impl FullBlockBody for T where T: BlockBody + MaybeSerdeBincodeCompat {} +#[cfg(feature = "rayon")] +use rayon::prelude::*; + /// Abstraction for block's body. pub trait BlockBody: Send @@ -99,24 +102,36 @@ pub trait BlockBody: } /// Recover signer addresses for all transactions in the block body. - #[cfg(feature = "rayon")] fn recover_signers(&self) -> Option> where Self::Transaction: SignedTransaction, { - recover_signers(self.transactions(), self.transactions().len()) + #[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, see also - /// [`recover_signers_unchecked`]. + /// Returns `None`, if some transaction's signature is invalid. fn recover_signers_unchecked(&self) -> Option> where Self::Transaction: SignedTransaction, { - recover_signers_unchecked(self.transactions(), self.transactions().len()) + #[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() + } } } diff --git a/crates/primitives/src/traits.rs b/crates/primitives/src/traits.rs index e35fb2178f7a..c77ccbcae124 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}; @@ -86,4 +83,4 @@ pub trait BlockExt: Block { } } -impl BlockExt for T {} \ No newline at end of file +impl BlockExt for T {} diff --git a/crates/primitives/src/transaction/mod.rs b/crates/primitives/src/transaction/mod.rs index ca971550c373..6bcaf715b607 100644 --- a/crates/primitives/src/transaction/mod.rs +++ b/crates/primitives/src/transaction/mod.rs @@ -1904,7 +1904,14 @@ where if num_txes < *PARALLEL_SENDER_RECOVERY_THRESHOLD { txes.into_iter().map(|tx| tx.recover_signer()).collect() } else { - txes.into_par_iter().map(|tx| tx.recover_signer()).collect() + #[cfg(feature = "rayon")] + { + txes.into_par_iter().map(|tx| tx.recover_signer()).collect() + } + #[cfg(not(feature = "rayon"))] + { + txes.into_iter().map(|tx| tx.recover_signer()).collect() + } } } From b36c32933cea01bb2bd304748924a9d740db899c Mon Sep 17 00:00:00 2001 From: Khanh Hoa Date: Wed, 1 Jan 2025 15:55:32 +0700 Subject: [PATCH 4/9] bet --- .github/workflows/unit.yml | 4 ++-- crates/primitives/Cargo.toml | 4 ++-- crates/primitives/src/transaction/mod.rs | 3 ++- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/.github/workflows/unit.yml b/.github/workflows/unit.yml index 4c927df8be00..f77ad899ac57 100644 --- a/.github/workflows/unit.yml +++ b/.github/workflows/unit.yml @@ -27,11 +27,11 @@ jobs: matrix: include: - type: ethereum - args: --features "asm-keccak ethereum" --locked + args: --features "asm-keccak ethereum rayon" --locked partition: 1 total_partitions: 2 - type: ethereum - args: --features "asm-keccak ethereum" --locked + args: --features "asm-keccak ethereum rayon" --locked partition: 2 total_partitions: 2 - type: optimism diff --git a/crates/primitives/Cargo.toml b/crates/primitives/Cargo.toml index 6b243ba7d256..cb59c3c51394 100644 --- a/crates/primitives/Cargo.toml +++ b/crates/primitives/Cargo.toml @@ -13,7 +13,7 @@ workspace = true [dependencies] # reth -reth-primitives-traits = { workspace = true, features = ["serde"] } +reth-primitives-traits = { workspace = true, features = ["serde", "rayon"] } reth-ethereum-forks.workspace = true reth-static-file-types.workspace = true revm-primitives = { workspace = true, features = ["serde"] } @@ -183,7 +183,7 @@ serde-bincode-compat = [ "reth-primitives-traits/serde-bincode-compat", "reth-trie-common/serde-bincode-compat", ] -rayon = ["dep:rayon"] +rayon = ["dep:rayon", "reth-primitives-traits/rayon"] [[bench]] diff --git a/crates/primitives/src/transaction/mod.rs b/crates/primitives/src/transaction/mod.rs index 6bcaf715b607..1225658d8d38 100644 --- a/crates/primitives/src/transaction/mod.rs +++ b/crates/primitives/src/transaction/mod.rs @@ -28,7 +28,8 @@ use op_alloy_consensus::DepositTransaction; #[cfg(feature = "optimism")] use op_alloy_consensus::TxDeposit; pub use pooled::PooledTransactionsElementEcRecovered; -use rayon::prelude::{IntoParallelIterator, ParallelIterator}; +#[cfg(feature = "rayon")] +use rayon::iter::{IntoParallelIterator, ParallelIterator}; pub use reth_primitives_traits::{ transaction::error::{ InvalidTransactionError, TransactionConversionError, TryFromRecoveredTransactionError, From 50f1b92d1ec73edd4fc0530f58b5b7695165767e Mon Sep 17 00:00:00 2001 From: Khanh Hoa Date: Wed, 1 Jan 2025 16:04:41 +0700 Subject: [PATCH 5/9] bet --- crates/primitives/Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/primitives/Cargo.toml b/crates/primitives/Cargo.toml index cb59c3c51394..0eb544bce30d 100644 --- a/crates/primitives/Cargo.toml +++ b/crates/primitives/Cargo.toml @@ -96,6 +96,7 @@ pprof = { workspace = true, features = [ ] } [features] +ethereum = ["rayon"] default = ["c-kzg", "alloy-compat", "std", "reth-codec", "secp256k1"] std = [ "reth-primitives-traits/std", From dc7cbe2d223bb7919e54b6a58dfb6d11d1fb757b Mon Sep 17 00:00:00 2001 From: Khanh Hoa Date: Wed, 1 Jan 2025 16:31:39 +0700 Subject: [PATCH 6/9] bet --- crates/primitives/Cargo.toml | 7 ++----- crates/primitives/src/transaction/mod.rs | 12 ++---------- 2 files changed, 4 insertions(+), 15 deletions(-) diff --git a/crates/primitives/Cargo.toml b/crates/primitives/Cargo.toml index 0eb544bce30d..e7036f1752bc 100644 --- a/crates/primitives/Cargo.toml +++ b/crates/primitives/Cargo.toml @@ -13,7 +13,7 @@ workspace = true [dependencies] # reth -reth-primitives-traits = { workspace = true, features = ["serde", "rayon"] } +reth-primitives-traits = { workspace = true, features = ["serde"] } reth-ethereum-forks.workspace = true reth-static-file-types.workspace = true revm-primitives = { workspace = true, features = ["serde"] } @@ -53,7 +53,7 @@ derive_more.workspace = true modular-bitfield = { workspace = true, optional = true } once_cell.workspace = true rand = { workspace = true, optional = true } -rayon = {workspace = true, optional = true } +rayon.workspace = true serde.workspace = true serde_with = { workspace = true, optional = true } @@ -96,7 +96,6 @@ pprof = { workspace = true, features = [ ] } [features] -ethereum = ["rayon"] default = ["c-kzg", "alloy-compat", "std", "reth-codec", "secp256k1"] std = [ "reth-primitives-traits/std", @@ -184,8 +183,6 @@ serde-bincode-compat = [ "reth-primitives-traits/serde-bincode-compat", "reth-trie-common/serde-bincode-compat", ] -rayon = ["dep:rayon", "reth-primitives-traits/rayon"] - [[bench]] name = "recover_ecdsa_crit" diff --git a/crates/primitives/src/transaction/mod.rs b/crates/primitives/src/transaction/mod.rs index 1225658d8d38..ca971550c373 100644 --- a/crates/primitives/src/transaction/mod.rs +++ b/crates/primitives/src/transaction/mod.rs @@ -28,8 +28,7 @@ use op_alloy_consensus::DepositTransaction; #[cfg(feature = "optimism")] use op_alloy_consensus::TxDeposit; pub use pooled::PooledTransactionsElementEcRecovered; -#[cfg(feature = "rayon")] -use rayon::iter::{IntoParallelIterator, ParallelIterator}; +use rayon::prelude::{IntoParallelIterator, ParallelIterator}; pub use reth_primitives_traits::{ transaction::error::{ InvalidTransactionError, TransactionConversionError, TryFromRecoveredTransactionError, @@ -1905,14 +1904,7 @@ where if num_txes < *PARALLEL_SENDER_RECOVERY_THRESHOLD { txes.into_iter().map(|tx| tx.recover_signer()).collect() } else { - #[cfg(feature = "rayon")] - { - txes.into_par_iter().map(|tx| tx.recover_signer()).collect() - } - #[cfg(not(feature = "rayon"))] - { - txes.into_iter().map(|tx| tx.recover_signer()).collect() - } + txes.into_par_iter().map(|tx| tx.recover_signer()).collect() } } From fa50e7f07e6c65ca67b43519ae98a4ffc8d4f204 Mon Sep 17 00:00:00 2001 From: Khanh Hoa Date: Wed, 1 Jan 2025 16:38:50 +0700 Subject: [PATCH 7/9] bet --- crates/primitives/src/traits.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/crates/primitives/src/traits.rs b/crates/primitives/src/traits.rs index c77ccbcae124..8f4544be6ce3 100644 --- a/crates/primitives/src/traits.rs +++ b/crates/primitives/src/traits.rs @@ -49,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] From 0aa8efe32a065e2e42202f6335e44180fe36dff7 Mon Sep 17 00:00:00 2001 From: Khanh Hoa Date: Fri, 3 Jan 2025 20:26:04 +0700 Subject: [PATCH 8/9] bet --- .github/workflows/unit.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/unit.yml b/.github/workflows/unit.yml index f77ad899ac57..4c927df8be00 100644 --- a/.github/workflows/unit.yml +++ b/.github/workflows/unit.yml @@ -27,11 +27,11 @@ jobs: matrix: include: - type: ethereum - args: --features "asm-keccak ethereum rayon" --locked + args: --features "asm-keccak ethereum" --locked partition: 1 total_partitions: 2 - type: ethereum - args: --features "asm-keccak ethereum rayon" --locked + args: --features "asm-keccak ethereum" --locked partition: 2 total_partitions: 2 - type: optimism From 3d212cc43134947ab0baf215a53ff76789e2835a Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Fri, 3 Jan 2025 16:44:43 +0100 Subject: [PATCH 9/9] enable rayon --- crates/node/core/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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