feat: Block::iter_recovered (#18476)

This commit is contained in:
Arsenii Kulikov
2025-09-16 00:26:19 +04:00
committed by GitHub
parent 5844ff7b17
commit 5f38ff7981
2 changed files with 18 additions and 21 deletions

View File

@@ -5,7 +5,7 @@ use crate::{
MaybeSerdeBincodeCompat, SignedTransaction,
};
use alloc::{fmt, vec::Vec};
use alloy_consensus::{Transaction, Typed2718};
use alloy_consensus::{transaction::Recovered, Transaction, Typed2718};
use alloy_eips::{eip2718::Encodable2718, eip4895::Withdrawals};
use alloy_primitives::{Address, Bytes, B256};
@@ -157,20 +157,14 @@ pub trait BlockBody:
}
/// Recover signer addresses for all transactions in the block body.
fn recover_signers(&self) -> Result<Vec<Address>, RecoveryError>
where
Self::Transaction: SignedTransaction,
{
fn recover_signers(&self) -> Result<Vec<Address>, RecoveryError> {
crate::transaction::recover::recover_signers(self.transactions())
}
/// Recover signer addresses for all transactions in the block body.
///
/// Returns an error if some transaction's signature is invalid.
fn try_recover_signers(&self) -> Result<Vec<Address>, RecoveryError>
where
Self::Transaction: SignedTransaction,
{
fn try_recover_signers(&self) -> Result<Vec<Address>, RecoveryError> {
self.recover_signers()
}
@@ -178,10 +172,7 @@ pub trait BlockBody:
/// signature has a low `s` value_.
///
/// Returns `RecoveryError`, if some transaction's signature is invalid.
fn recover_signers_unchecked(&self) -> Result<Vec<Address>, RecoveryError>
where
Self::Transaction: SignedTransaction,
{
fn recover_signers_unchecked(&self) -> Result<Vec<Address>, RecoveryError> {
crate::transaction::recover::recover_signers_unchecked(self.transactions())
}
@@ -189,12 +180,21 @@ pub trait BlockBody:
/// signature has a low `s` value_.
///
/// Returns an error if some transaction's signature is invalid.
fn try_recover_signers_unchecked(&self) -> Result<Vec<Address>, RecoveryError>
where
Self::Transaction: SignedTransaction,
{
fn try_recover_signers_unchecked(&self) -> Result<Vec<Address>, RecoveryError> {
self.recover_signers_unchecked()
}
/// Recovers signers for all transactions in the block body and returns a vector of
/// [`Recovered`].
fn recover_transactions(&self) -> Result<Vec<Recovered<Self::Transaction>>, RecoveryError> {
self.recover_signers().map(|signers| {
self.transactions()
.iter()
.zip(signers)
.map(|(tx, signer)| tx.clone().with_signer(signer))
.collect()
})
}
}
impl<T, H> BlockBody for alloy_consensus::BlockBody<T, H>

View File

@@ -190,10 +190,7 @@ pub trait Block:
/// transactions.
///
/// Returns the block as error if a signature is invalid.
fn try_into_recovered(self) -> Result<RecoveredBlock<Self>, BlockRecoveryError<Self>>
where
<Self::Body as BlockBody>::Transaction: SignedTransaction,
{
fn try_into_recovered(self) -> Result<RecoveredBlock<Self>, BlockRecoveryError<Self>> {
let Ok(signers) = self.body().recover_signers() else {
return Err(BlockRecoveryError::new(self))
};