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: add TransactionSigned::recover_signers #4098

Merged
merged 3 commits into from
Aug 8, 2023
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.

6 changes: 2 additions & 4 deletions crates/consensus/auto-seal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,10 +342,8 @@ impl StorageInner {

let block = Block { header, body: transactions, ommers: vec![], withdrawals: None };

let senders =
block.body.iter().map(|tx| tx.recover_signer()).collect::<Option<Vec<_>>>().ok_or(
BlockExecutionError::Validation(BlockValidationError::SenderRecoveryError),
)?;
let senders = TransactionSigned::recover_signers(block.body.iter(), block.body.len())
.ok_or(BlockExecutionError::Validation(BlockValidationError::SenderRecoveryError))?;

trace!(target: "consensus::auto", transactions=?&block.body, "executing transactions");

Expand Down
1 change: 1 addition & 0 deletions crates/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ impl-serde = "0.4.0"
once_cell = "1.17.0"
zstd = { version = "0.12", features = ["experimental"] }
paste = "1.0"
rayon = "1.7"

# proof related
triehash = "0.8"
Expand Down
2 changes: 1 addition & 1 deletion crates/primitives/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ impl SealedBlock {

/// Expensive operation that recovers transaction signer. See [SealedBlockWithSenders].
pub fn senders(&self) -> Option<Vec<Address>> {
self.body.iter().map(|tx| tx.recover_signer()).collect::<Option<Vec<Address>>>()
TransactionSigned::recover_signers(self.body.iter(), self.body.len())
}

/// Seal sealed block with recovered transaction senders.
Expand Down
20 changes: 20 additions & 0 deletions crates/primitives/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use bytes::{Buf, BytesMut};
use derive_more::{AsRef, Deref};
pub use error::InvalidTransactionError;
pub use meta::TransactionMeta;
use rayon::prelude::{ParallelBridge, ParallelIterator};
use reth_codecs::{add_arbitrary_tests, derive_arbitrary, Compact};
use reth_rlp::{
length_of_length, Decodable, DecodeError, Encodable, Header, EMPTY_LIST_CODE, EMPTY_STRING_CODE,
Expand All @@ -32,6 +33,10 @@ mod signature;
mod tx_type;
pub(crate) mod util;

// Expected number of transactions where we can expect a speed-up by recovering the senders in
// parallel.
const PARALLEL_SENDER_RECOVERY_THRESHOLD: usize = 10;

/// A raw transaction.
///
/// Transaction types were introduced in [EIP-2718](https://eips.ethereum.org/EIPS/eip-2718).
Expand Down Expand Up @@ -849,6 +854,21 @@ impl TransactionSigned {
self.signature.recover_signer(signature_hash)
}

/// Recovers a list of signers from a transaction list iterator
///
/// Returns `None`, if some transaction's signature is invalid, see also
/// [Self::recover_signer].
pub fn recover_signers<'a>(
txes: impl Iterator<Item = &'a Self> + Send,
num_txes: usize,
) -> Option<Vec<Address>> {
if num_txes < PARALLEL_SENDER_RECOVERY_THRESHOLD {
txes.map(|tx| tx.recover_signer()).collect()
} else {
txes.cloned().par_bridge().map(|tx| tx.recover_signer()).collect()
}
}

/// Consumes the type, recover signer and return [`TransactionSignedEcRecovered`]
///
/// Returns `None` if the transaction's signature is invalid, see also [Self::recover_signer].
Expand Down
7 changes: 2 additions & 5 deletions crates/revm/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,8 @@ where
Err(BlockValidationError::SenderRecoveryError.into())
}
} else {
body.iter()
.map(|tx| {
tx.recover_signer().ok_or(BlockValidationError::SenderRecoveryError.into())
})
.collect()
TransactionSigned::recover_signers(body.iter(), body.len())
.ok_or(BlockValidationError::SenderRecoveryError.into())
}
}

Expand Down
19 changes: 10 additions & 9 deletions crates/storage/provider/src/providers/database/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ use reth_db::{
transaction::{DbTx, DbTxMut},
BlockNumberList, DatabaseError,
};
use reth_interfaces::Result;
use reth_interfaces::{
executor::{BlockExecutionError, BlockValidationError},
Result,
};
use reth_primitives::{
keccak256,
stage::{StageCheckpoint, StageId},
Expand Down Expand Up @@ -1899,14 +1902,12 @@ impl<'this, TX: DbTxMut<'this> + DbTx<'this>> BlockWriter for DatabaseProvider<'
let tx_iter = if Some(block.body.len()) == senders_len {
block.body.into_iter().zip(senders.unwrap()).collect::<Vec<(_, _)>>()
} else {
block
.body
.into_iter()
.map(|tx| {
let signer = tx.recover_signer();
(tx, signer.unwrap_or_default())
})
.collect::<Vec<(_, _)>>()
let senders = TransactionSigned::recover_signers(block.body.iter(), block.body.len())
.ok_or(BlockExecutionError::Validation(
BlockValidationError::SenderRecoveryError,
))?;

block.body.into_iter().zip(senders).collect()
};

for (transaction, sender) in tx_iter {
Expand Down
Loading