feat: merge BlockBodyTxExt trait into BlockBody (#13613)

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
This commit is contained in:
DevOrbitlabs
2025-01-03 22:57:59 +07:00
committed by GitHub
parent afdffadafd
commit 8f854cbbb6
6 changed files with 46 additions and 34 deletions

1
Cargo.lock generated
View File

@ -8782,6 +8782,7 @@ dependencies = [
"proptest",
"proptest-arbitrary-interop",
"rand 0.8.5",
"rayon",
"reth-codecs",
"revm-primitives",
"secp256k1",

View File

@ -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

View File

@ -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",
]

View File

@ -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
@ -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>

View File

@ -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;

View File

@ -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<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 {}