Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: merge BlockBodyTxExt trait into BlockBody #13613

Merged
merged 10 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/node/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions crates/primitives-traits/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -142,3 +143,6 @@ reth-codec = [
op = [
"dep:op-alloy-consensus",
]
rayon = [
"dep:rayon",
]
38 changes: 37 additions & 1 deletion crates/primitives-traits/src/block/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Transaction: FullSignedTx> + MaybeSerdeBincodeCompat {}

impl<T> FullBlockBody for T where T: BlockBody<Transaction: FullSignedTx> + MaybeSerdeBincodeCompat {}

#[cfg(feature = "rayon")]
use rayon::prelude::*;

/// Abstraction for block's body.
pub trait BlockBody:
Send
Expand Down Expand Up @@ -97,6 +100,39 @@ pub trait BlockBody:
fn encoded_2718_transactions(&self) -> Vec<Bytes> {
self.encoded_2718_transactions_iter().map(Into::into).collect()
}

/// Recover signer addresses for all transactions in the block body.
fn recover_signers(&self) -> Option<Vec<Address>>
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<Vec<Address>>
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<T> BlockBody for alloy_consensus::BlockBody<T>
Expand Down
4 changes: 2 additions & 2 deletions crates/primitives/src/block.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
31 changes: 1 addition & 30 deletions crates/primitives/src/traits.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -87,28 +83,3 @@ pub trait BlockExt: Block {
}

impl<T: Block> 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<Vec<Address>>
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<Vec<Address>>
where
Self::Transaction: SignedTransaction,
{
recover_signers_unchecked(self.transactions(), self.transactions().len())
}
}

impl<T: BlockBody> BlockBodyTxExt for T {}
Loading