reth_primitives_traits/block/
sealed.rs
1use crate::{
4 block::{error::BlockRecoveryError, RecoveredBlock},
5 transaction::signed::RecoveryError,
6 Block, BlockBody, GotExpected, InMemorySize, SealedHeader,
7};
8use alloc::vec::Vec;
9use alloy_consensus::BlockHeader;
10use alloy_eips::{eip1898::BlockWithParent, BlockNumHash};
11use alloy_primitives::{Address, BlockHash, Sealable, Sealed, B256};
12use alloy_rlp::{Decodable, Encodable};
13use bytes::BufMut;
14use core::ops::Deref;
15
16#[derive(Debug, Clone, PartialEq, Eq)]
21#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
22pub struct SealedBlock<B: Block> {
23 header: SealedHeader<B::Header>,
25 body: B::Body,
27}
28
29impl<B: Block> SealedBlock<B> {
30 pub fn seal_slow(block: B) -> Self {
35 let hash = block.header().hash_slow();
36 Self::new_unchecked(block, hash)
37 }
38
39 #[inline]
43 pub fn new_unchecked(block: B, hash: BlockHash) -> Self {
44 let (header, body) = block.split();
45 Self { header: SealedHeader::new(header, hash), body }
46 }
47
48 pub fn new_unhashed(block: B) -> Self {
50 let (header, body) = block.split();
51 Self { header: SealedHeader::new_unhashed(header), body }
52 }
53
54 pub fn seal_parts(header: B::Header, body: B::Body) -> Self {
60 Self::seal_slow(B::new(header, body))
61 }
62
63 pub fn from_parts_unhashed(header: B::Header, body: B::Body) -> Self {
65 Self::new_unhashed(B::new(header, body))
66 }
67
68 pub fn from_parts_unchecked(header: B::Header, body: B::Body, hash: BlockHash) -> Self {
70 Self::new_unchecked(B::new(header, body), hash)
71 }
72
73 pub fn from_sealed_parts(header: SealedHeader<B::Header>, body: B::Body) -> Self {
75 let (header, hash) = header.split();
76 Self::from_parts_unchecked(header, body, hash)
77 }
78
79 #[inline]
81 pub fn hash_ref(&self) -> &BlockHash {
82 self.header.hash_ref()
83 }
84
85 #[inline]
87 pub fn hash(&self) -> B256 {
88 self.header.hash()
89 }
90
91 #[doc(alias = "into_components")]
93 pub fn split(self) -> (B, BlockHash) {
94 let (header, hash) = self.header.split();
95 (B::new(header, self.body), hash)
96 }
97
98 pub fn into_block(self) -> B {
100 self.unseal()
101 }
102
103 pub fn unseal(self) -> B {
105 let header = self.header.unseal();
106 B::new(header, self.body)
107 }
108
109 pub fn clone_block(&self) -> B {
111 B::new(self.header.clone_header(), self.body.clone())
112 }
113
114 pub const fn with_senders(self, senders: Vec<Address>) -> RecoveredBlock<B> {
118 RecoveredBlock::new_sealed(self, senders)
119 }
120
121 pub fn try_with_senders(
129 self,
130 senders: Vec<Address>,
131 ) -> Result<RecoveredBlock<B>, BlockRecoveryError<Self>> {
132 RecoveredBlock::try_recover_sealed_with_senders(self, senders)
133 }
134
135 pub fn try_with_senders_unchecked(
143 self,
144 senders: Vec<Address>,
145 ) -> Result<RecoveredBlock<B>, BlockRecoveryError<Self>> {
146 RecoveredBlock::try_recover_sealed_with_senders_unchecked(self, senders)
147 }
148
149 pub fn try_recover(self) -> Result<RecoveredBlock<B>, BlockRecoveryError<Self>> {
154 RecoveredBlock::try_recover_sealed(self)
155 }
156
157 pub fn try_recover_unchecked(self) -> Result<RecoveredBlock<B>, BlockRecoveryError<Self>> {
162 RecoveredBlock::try_recover_sealed_unchecked(self)
163 }
164
165 pub const fn header(&self) -> &B::Header {
167 self.header.header()
168 }
169
170 pub const fn body(&self) -> &B::Body {
172 &self.body
173 }
174
175 pub fn rlp_length(&self) -> usize {
177 B::rlp_length(self.header(), self.body())
178 }
179
180 pub fn senders(&self) -> Result<Vec<Address>, RecoveryError> {
184 self.body().recover_signers()
185 }
186
187 pub fn num_hash(&self) -> BlockNumHash {
189 BlockNumHash::new(self.number(), self.hash())
190 }
191
192 pub fn block_with_parent(&self) -> BlockWithParent {
194 BlockWithParent { parent: self.parent_hash(), block: self.num_hash() }
195 }
196
197 pub const fn sealed_header(&self) -> &SealedHeader<B::Header> {
199 &self.header
200 }
201
202 pub fn sealed_header_ref(&self) -> SealedHeader<&B::Header> {
204 SealedHeader::new(self.header(), self.hash())
205 }
206
207 pub fn clone_sealed_header(&self) -> SealedHeader<B::Header> {
209 self.header.clone()
210 }
211
212 pub fn into_sealed_header(self) -> SealedHeader<B::Header> {
214 self.header
215 }
216
217 pub fn into_header(self) -> B::Header {
219 self.header.unseal()
220 }
221
222 pub fn into_body(self) -> B::Body {
224 self.body
225 }
226
227 pub fn split_header_body(self) -> (B::Header, B::Body) {
229 let header = self.header.unseal();
230 (header, self.body)
231 }
232
233 pub fn split_sealed_header_body(self) -> (SealedHeader<B::Header>, B::Body) {
235 (self.header, self.body)
236 }
237
238 #[inline]
240 pub fn blob_versioned_hashes_iter(&self) -> impl Iterator<Item = &B256> + '_ {
241 self.body().blob_versioned_hashes_iter()
242 }
243
244 #[inline]
246 pub fn transaction_count(&self) -> usize {
247 self.body().transaction_count()
248 }
249
250 pub fn ensure_transaction_root_valid(&self) -> Result<(), GotExpected<B256>> {
263 let calculated_root = self.body().calculate_tx_root();
264
265 if self.header().transactions_root() != calculated_root {
266 return Err(GotExpected {
267 got: calculated_root,
268 expected: self.header().transactions_root(),
269 })
270 }
271
272 Ok(())
273 }
274}
275
276impl<B> From<B> for SealedBlock<B>
277where
278 B: Block,
279{
280 fn from(block: B) -> Self {
281 Self::seal_slow(block)
282 }
283}
284
285impl<B> Default for SealedBlock<B>
286where
287 B: Block + Default,
288{
289 fn default() -> Self {
290 Self::seal_slow(Default::default())
291 }
292}
293
294impl<B: Block> InMemorySize for SealedBlock<B> {
295 #[inline]
296 fn size(&self) -> usize {
297 self.body.size() + self.header.size()
298 }
299}
300
301impl<B: Block> Deref for SealedBlock<B> {
302 type Target = B::Header;
303
304 fn deref(&self) -> &Self::Target {
305 self.header()
306 }
307}
308
309impl<B: Block> Encodable for SealedBlock<B> {
310 fn encode(&self, out: &mut dyn BufMut) {
311 self.body.encode(out);
312 }
313}
314
315impl<B: Block> Decodable for SealedBlock<B> {
316 fn decode(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
317 let block = B::decode(buf)?;
318 Ok(Self::seal_slow(block))
319 }
320}
321
322impl<B: Block> From<SealedBlock<B>> for Sealed<B> {
323 fn from(value: SealedBlock<B>) -> Self {
324 let (block, hash) = value.split();
325 Self::new_unchecked(block, hash)
326 }
327}
328
329#[cfg(any(test, feature = "arbitrary"))]
330impl<'a, B> arbitrary::Arbitrary<'a> for SealedBlock<B>
331where
332 B: Block + arbitrary::Arbitrary<'a>,
333{
334 fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
335 let block = B::arbitrary(u)?;
336 Ok(Self::seal_slow(block))
337 }
338}
339
340#[cfg(any(test, feature = "test-utils"))]
341impl<B: crate::test_utils::TestBlock> SealedBlock<B> {
342 pub const fn header_mut(&mut self) -> &mut B::Header {
344 self.header.header_mut()
345 }
346
347 pub fn set_hash(&mut self, hash: BlockHash) {
349 self.header.set_hash(hash)
350 }
351
352 pub const fn body_mut(&mut self) -> &mut B::Body {
354 &mut self.body
355 }
356
357 pub fn set_parent_hash(&mut self, hash: BlockHash) {
359 self.header.set_parent_hash(hash)
360 }
361
362 pub fn set_block_number(&mut self, number: alloy_primitives::BlockNumber) {
364 self.header.set_block_number(number)
365 }
366
367 pub fn set_state_root(&mut self, state_root: alloy_primitives::B256) {
369 self.header.set_state_root(state_root)
370 }
371
372 pub fn set_difficulty(&mut self, difficulty: alloy_primitives::U256) {
374 self.header.set_difficulty(difficulty)
375 }
376}
377
378#[cfg(feature = "serde-bincode-compat")]
380pub(super) mod serde_bincode_compat {
381 use crate::{
382 serde_bincode_compat::{self, BincodeReprFor, SerdeBincodeCompat},
383 Block,
384 };
385 use serde::{Deserialize, Deserializer, Serialize, Serializer};
386 use serde_with::{serde_as, DeserializeAs, SerializeAs};
387
388 #[serde_as]
408 #[derive(derive_more::Debug, Serialize, Deserialize)]
409 pub struct SealedBlock<
410 'a,
411 T: Block<Header: SerdeBincodeCompat, Body: SerdeBincodeCompat> + 'static,
412 > {
413 #[serde(
414 bound = "serde_bincode_compat::SealedHeader<'a, T::Header>: Serialize + serde::de::DeserializeOwned"
415 )]
416 header: serde_bincode_compat::SealedHeader<'a, T::Header>,
417 body: BincodeReprFor<'a, T::Body>,
418 }
419
420 impl<'a, T: Block<Header: SerdeBincodeCompat, Body: SerdeBincodeCompat> + 'static>
421 From<&'a super::SealedBlock<T>> for SealedBlock<'a, T>
422 {
423 fn from(value: &'a super::SealedBlock<T>) -> Self {
424 Self { header: value.header.as_repr(), body: value.body.as_repr() }
425 }
426 }
427
428 impl<'a, T: Block<Header: SerdeBincodeCompat, Body: SerdeBincodeCompat> + 'static>
429 From<SealedBlock<'a, T>> for super::SealedBlock<T>
430 {
431 fn from(value: SealedBlock<'a, T>) -> Self {
432 Self::from_sealed_parts(value.header.into(), SerdeBincodeCompat::from_repr(value.body))
433 }
434 }
435
436 impl<T: Block<Header: SerdeBincodeCompat, Body: SerdeBincodeCompat> + 'static>
437 SerializeAs<super::SealedBlock<T>> for SealedBlock<'_, T>
438 {
439 fn serialize_as<S>(source: &super::SealedBlock<T>, serializer: S) -> Result<S::Ok, S::Error>
440 where
441 S: Serializer,
442 {
443 SealedBlock::from(source).serialize(serializer)
444 }
445 }
446
447 impl<'de, T: Block<Header: SerdeBincodeCompat, Body: SerdeBincodeCompat> + 'static>
448 DeserializeAs<'de, super::SealedBlock<T>> for SealedBlock<'de, T>
449 {
450 fn deserialize_as<D>(deserializer: D) -> Result<super::SealedBlock<T>, D::Error>
451 where
452 D: Deserializer<'de>,
453 {
454 SealedBlock::deserialize(deserializer).map(Into::into)
455 }
456 }
457
458 impl<T: Block<Header: SerdeBincodeCompat, Body: SerdeBincodeCompat> + 'static>
459 SerdeBincodeCompat for super::SealedBlock<T>
460 {
461 type BincodeRepr<'a> = SealedBlock<'a, T>;
462
463 fn as_repr(&self) -> Self::BincodeRepr<'_> {
464 self.into()
465 }
466
467 fn from_repr(repr: Self::BincodeRepr<'_>) -> Self {
468 repr.into()
469 }
470 }
471}