reth_primitives/
traits.rs1use 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
9pub trait BlockExt: Block {
13 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 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 fn senders(&self) -> Option<Vec<Address>>
29 where
30 <Self::Body as BlockBody>::Transaction: SignedTransaction,
31 {
32 self.body().recover_signers()
33 }
34
35 #[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 #[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 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
91pub trait BlockBodyTxExt: BlockBody {
93 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 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 {}