reth_primitives/
traits.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
use crate::{
    transaction::{recover_signers, recover_signers_unchecked},
    BlockWithSenders, SealedBlock,
};
use alloc::vec::Vec;
use reth_primitives_traits::{Block, BlockBody, SealedHeader, SignedTransaction};
use revm_primitives::{Address, B256};

/// Extension trait for [`reth_primitives_traits::Block`] implementations
/// allowing for conversions into common block parts containers such as [`SealedBlock`],
/// [`BlockWithSenders`], etc.
pub trait BlockExt: Block {
    /// Calculate the header hash and seal the block so that it can't be changed.
    fn seal_slow(self) -> SealedBlock<Self::Header, Self::Body> {
        let (header, body) = self.split();
        SealedBlock { header: SealedHeader::seal(header), body }
    }

    /// Seal the block with a known hash.
    ///
    /// WARNING: This method does not perform validation whether the hash is correct.
    fn seal(self, hash: B256) -> SealedBlock<Self::Header, Self::Body> {
        let (header, body) = self.split();
        SealedBlock { header: SealedHeader::new(header, hash), body }
    }

    /// Expensive operation that recovers transaction signer.
    fn senders(&self) -> Option<Vec<Address>>
    where
        <Self::Body as BlockBody>::Transaction: SignedTransaction,
    {
        self.body().recover_signers()
    }

    /// Transform into a [`BlockWithSenders`].
    ///
    /// # Panics
    ///
    /// If the number of senders does not match the number of transactions in the block
    /// and the signer recovery for one of the transactions fails.
    ///
    /// Note: this is expected to be called with blocks read from disk.
    #[track_caller]
    fn with_senders_unchecked(self, senders: Vec<Address>) -> BlockWithSenders<Self>
    where
        <Self::Body as BlockBody>::Transaction: SignedTransaction,
    {
        self.try_with_senders_unchecked(senders).expect("stored block is valid")
    }

    /// Transform into a [`BlockWithSenders`] using the given senders.
    ///
    /// 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]
    fn try_with_senders_unchecked(
        self,
        senders: Vec<Address>,
    ) -> Result<BlockWithSenders<Self>, Self>
    where
        <Self::Body as BlockBody>::Transaction: SignedTransaction,
    {
        let senders = if self.body().transactions().len() == senders.len() {
            senders
        } else {
            let Some(senders) = self.body().recover_signers_unchecked() else { return Err(self) };
            senders
        };

        Ok(BlockWithSenders::new_unchecked(self, senders))
    }

    /// **Expensive**. Transform into a [`BlockWithSenders`] by recovering senders in the contained
    /// transactions.
    ///
    /// Returns `None` if a transaction is invalid.
    fn with_recovered_senders(self) -> Option<BlockWithSenders<Self>>
    where
        <Self::Body as BlockBody>::Transaction: SignedTransaction,
    {
        let senders = self.senders()?;
        Some(BlockWithSenders::new_unchecked(self, senders))
    }
}

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