reth_primitives/
traits.rs

1use crate::{
2    transaction::{recover_signers, recover_signers_unchecked},
3    BlockWithSenders, SealedBlock,
4};
5use alloc::vec::Vec;
6use reth_primitives_traits::{Block, BlockBody, SealedHeader, SignedTransaction};
7use revm_primitives::{Address, B256};
8
9/// Extension trait for [`reth_primitives_traits::Block`] implementations
10/// allowing for conversions into common block parts containers such as [`SealedBlock`],
11/// [`BlockWithSenders`], etc.
12pub trait BlockExt: Block {
13    /// Calculate the header hash and seal the block so that it can't be changed.
14    fn seal_slow(self) -> SealedBlock<Self::Header, Self::Body> {
15        let (header, body) = self.split();
16        SealedBlock { header: SealedHeader::seal(header), body }
17    }
18
19    /// Seal the block with a known hash.
20    ///
21    /// WARNING: This method does not perform validation whether the hash is correct.
22    fn seal(self, hash: B256) -> SealedBlock<Self::Header, Self::Body> {
23        let (header, body) = self.split();
24        SealedBlock { header: SealedHeader::new(header, hash), body }
25    }
26
27    /// Expensive operation that recovers transaction signer.
28    fn senders(&self) -> Option<Vec<Address>>
29    where
30        <Self::Body as BlockBody>::Transaction: SignedTransaction,
31    {
32        self.body().recover_signers()
33    }
34
35    /// Transform into a [`BlockWithSenders`].
36    ///
37    /// # Panics
38    ///
39    /// If the number of senders does not match the number of transactions in the block
40    /// and the signer recovery for one of the transactions fails.
41    ///
42    /// Note: this is expected to be called with blocks read from disk.
43    #[track_caller]
44    fn with_senders_unchecked(self, senders: Vec<Address>) -> BlockWithSenders<Self>
45    where
46        <Self::Body as BlockBody>::Transaction: SignedTransaction,
47    {
48        self.try_with_senders_unchecked(senders).expect("stored block is valid")
49    }
50
51    /// Transform into a [`BlockWithSenders`] using the given senders.
52    ///
53    /// If the number of senders does not match the number of transactions in the block, this falls
54    /// back to manually recovery, but _without ensuring that the signature has a low `s` value_.
55    /// See also [`recover_signers_unchecked`]
56    ///
57    /// Returns an error if a signature is invalid.
58    #[track_caller]
59    fn try_with_senders_unchecked(
60        self,
61        senders: Vec<Address>,
62    ) -> Result<BlockWithSenders<Self>, Self>
63    where
64        <Self::Body as BlockBody>::Transaction: SignedTransaction,
65    {
66        let senders = if self.body().transactions().len() == senders.len() {
67            senders
68        } else {
69            let Some(senders) = self.body().recover_signers_unchecked() else { return Err(self) };
70            senders
71        };
72
73        Ok(BlockWithSenders::new_unchecked(self, senders))
74    }
75
76    /// **Expensive**. Transform into a [`BlockWithSenders`] by recovering senders in the contained
77    /// transactions.
78    ///
79    /// Returns `None` if a transaction is invalid.
80    fn with_recovered_senders(self) -> Option<BlockWithSenders<Self>>
81    where
82        <Self::Body as BlockBody>::Transaction: SignedTransaction,
83    {
84        let senders = self.senders()?;
85        Some(BlockWithSenders::new_unchecked(self, senders))
86    }
87}
88
89impl<T: Block> BlockExt for T {}
90
91/// Extension trait for [`BlockBody`] adding helper methods operating with transactions.
92pub trait BlockBodyTxExt: BlockBody {
93    /// Recover signer addresses for all transactions in the block body.
94    fn recover_signers(&self) -> Option<Vec<Address>>
95    where
96        Self::Transaction: SignedTransaction,
97    {
98        recover_signers(self.transactions(), self.transactions().len())
99    }
100
101    /// Recover signer addresses for all transactions in the block body _without ensuring that the
102    /// signature has a low `s` value_.
103    ///
104    /// Returns `None`, if some transaction's signature is invalid, see also
105    /// [`recover_signers_unchecked`].
106    fn recover_signers_unchecked(&self) -> Option<Vec<Address>>
107    where
108        Self::Transaction: SignedTransaction,
109    {
110        recover_signers_unchecked(self.transactions(), self.transactions().len())
111    }
112}
113
114impl<T: BlockBody> BlockBodyTxExt for T {}