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

consensus: impl recover_signers for BlockBody<TxEnvelope> #1541

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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.toml
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ derive_more = { version = "1.0.0", default-features = false }
strum = { version = "0.26", default-features = false }
http = "1.1.0"
jsonwebtoken = "9.3.0"
rayon = "1.7"

## serde
serde = { version = "1.0", default-features = false, features = [
Expand Down
3 changes: 2 additions & 1 deletion crates/consensus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ derive_more = { workspace = true, features = [
"into_iterator"
], default-features = false }
auto_impl.workspace = true
rayon = { workspace = true, optional = true }

[dev-dependencies]
alloy-eips = { workspace = true, features = ["arbitrary"] }
Expand All @@ -58,7 +59,7 @@ tokio = { workspace = true, features = ["macros"] }
[features]
default = ["std"]
std = ["alloy-eips/std", "c-kzg?/std"]
k256 = ["alloy-primitives/k256", "alloy-eips/k256"]
k256 = ["alloy-primitives/k256", "alloy-eips/k256", "rayon"]
kzg = ["dep:c-kzg", "alloy-eips/kzg", "std"]
arbitrary = ["std", "dep:arbitrary", "alloy-eips/arbitrary"]
serde = [
Expand Down
11 changes: 10 additions & 1 deletion crates/consensus/src/block.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
//! Block Type

use crate::Header;
use crate::{Header, TxEnvelope};
use alloc::vec::Vec;
use alloy_eips::eip4895::Withdrawals;
use alloy_primitives::Address;
use alloy_rlp::{Decodable, Encodable, RlpDecodable, RlpEncodable};

/// Ethereum full block.
Expand Down Expand Up @@ -34,6 +35,14 @@ pub struct BlockBody<T> {
pub withdrawals: Option<Withdrawals>,
}

impl BlockBody<TxEnvelope> {
/// Recover signer addresses for all transactions in the block body.
#[cfg(feature = "k256")]
pub fn recover_signers(&self) -> Result<Vec<Address>, alloy_primitives::SignatureError> {
TxEnvelope::recover_signers(&self.transactions, self.transactions.len())
}
}

/// We need to implement RLP traits manually because we currently don't have a way to flatten
/// [`BlockBody`] into [`Block`].
mod block_rlp {
Expand Down
33 changes: 33 additions & 0 deletions crates/consensus/src/transaction/envelope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ use alloy_rlp::{Decodable, Encodable, Header};

use crate::transaction::eip4844::{TxEip4844, TxEip4844Variant, TxEip4844WithSidecar};

/// Expected number of transactions where we can expect a speed-up by recovering the senders in
/// parallel.
#[cfg(feature = "k256")]
pub(crate) static PARALLEL_SENDER_RECOVERY_THRESHOLD: std::sync::LazyLock<usize> =
std::sync::LazyLock::new(|| match rayon::current_num_threads() {
0..=1 => usize::MAX,
2..=8 => 10,
_ => 5,
});

/// Ethereum `TransactionType` flags as specified in EIPs [2718], [1559], [2930],
/// [4844], and [7702].
///
Expand Down Expand Up @@ -241,6 +251,29 @@ impl TxEnvelope {
}
}

/// Recovers a list of signers from a transaction list iterator.
///
/// Returns `None` if some transaction's signature is invalid;
/// See also [`Self::recover_signer`].
#[cfg(feature = "k256")]
pub fn recover_signers<'a, T>(
txes: T,
num_txes: usize,
) -> Result<alloc::vec::Vec<alloy_primitives::Address>, alloy_primitives::SignatureError>
where
T: rayon::prelude::IntoParallelIterator<Item = &'a Self>
+ IntoIterator<Item = &'a Self>
+ Send,
{
use rayon::iter::ParallelIterator;

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()
}
}

/// Calculate the signing hash for the transaction.
pub fn signature_hash(&self) -> B256 {
match self {
Expand Down
Loading