11use core:: fmt:: Debug ;
2+ use serde:: { de:: DeserializeOwned , Serialize } ;
23
34pub use super :: header:: { serde_bincode_compat as header, serde_bincode_compat:: * } ;
4- use serde :: { de :: DeserializeOwned , Serialize } ;
5+ pub use block_bincode :: BlockBody ;
56
67/// Trait for types that can be serialized and deserialized using bincode.
78pub trait SerdeBincodeCompat : Sized + ' static {
@@ -12,3 +13,82 @@ pub trait SerdeBincodeCompat: Sized + 'static {
1213impl SerdeBincodeCompat for alloy_consensus:: Header {
1314 type BincodeRepr < ' a > = alloy_consensus:: serde_bincode_compat:: Header < ' a > ;
1415}
16+
17+ mod block_bincode {
18+ use crate :: serde_bincode_compat:: SerdeBincodeCompat ;
19+ use alloc:: { borrow:: Cow , vec:: Vec } ;
20+ use alloy_consensus:: serde_bincode_compat:: Header ;
21+ use alloy_eips:: eip4895:: Withdrawals ;
22+ use serde:: { Deserialize , Deserializer , Serialize , Serializer } ;
23+ use serde_with:: { DeserializeAs , SerializeAs } ;
24+
25+ /// Bincode-compatible [`alloy_consensus::BlockBody`] serde implementation.
26+ ///
27+ /// Intended to use with the [`serde_with::serde_as`] macro in the following way:
28+ /// ```rust
29+ /// use reth_primitives_traits::serde_bincode_compat::{self, SerdeBincodeCompat};
30+ /// use serde::{Deserialize, Serialize};
31+ /// use serde_with::serde_as;
32+ ///
33+ /// #[serde_as]
34+ /// #[derive(Serialize, Deserialize)]
35+ /// struct Data<T: SerdeBincodeCompat> {
36+ /// #[serde_as(as = "serde_bincode_compat::BlockBody<'_, T>")]
37+ /// body: alloy_consensus::BlockBody<T>,
38+ /// }
39+ /// ```
40+ #[ derive( derive_more:: Debug , Serialize , Deserialize ) ]
41+ #[ debug( bound( ) ) ]
42+ pub struct BlockBody < ' a , T : SerdeBincodeCompat > {
43+ transactions : Vec < T :: BincodeRepr < ' a > > ,
44+ ommers : Vec < Header < ' a > > ,
45+ withdrawals : Cow < ' a , Option < Withdrawals > > ,
46+ }
47+
48+ impl < ' a , T : SerdeBincodeCompat > From < & ' a alloy_consensus:: BlockBody < T > > for BlockBody < ' a , T > {
49+ fn from ( value : & ' a alloy_consensus:: BlockBody < T > ) -> Self {
50+ Self {
51+ transactions : value. transactions . iter ( ) . map ( Into :: into) . collect ( ) ,
52+ ommers : value. ommers . iter ( ) . map ( Into :: into) . collect ( ) ,
53+ withdrawals : Cow :: Borrowed ( & value. withdrawals ) ,
54+ }
55+ }
56+ }
57+
58+ impl < ' a , T : SerdeBincodeCompat > From < BlockBody < ' a , T > > for alloy_consensus:: BlockBody < T > {
59+ fn from ( value : BlockBody < ' a , T > ) -> Self {
60+ Self {
61+ transactions : value. transactions . into_iter ( ) . map ( Into :: into) . collect ( ) ,
62+ ommers : value. ommers . into_iter ( ) . map ( Into :: into) . collect ( ) ,
63+ withdrawals : value. withdrawals . into_owned ( ) ,
64+ }
65+ }
66+ }
67+
68+ impl < T : SerdeBincodeCompat > SerializeAs < alloy_consensus:: BlockBody < T > > for BlockBody < ' _ , T > {
69+ fn serialize_as < S > (
70+ source : & alloy_consensus:: BlockBody < T > ,
71+ serializer : S ,
72+ ) -> Result < S :: Ok , S :: Error >
73+ where
74+ S : Serializer ,
75+ {
76+ BlockBody :: from ( source) . serialize ( serializer)
77+ }
78+ }
79+
80+ impl < ' de , T : SerdeBincodeCompat > DeserializeAs < ' de , alloy_consensus:: BlockBody < T > >
81+ for BlockBody < ' de , T >
82+ {
83+ fn deserialize_as < D > ( deserializer : D ) -> Result < alloy_consensus:: BlockBody < T > , D :: Error >
84+ where
85+ D : Deserializer < ' de > ,
86+ {
87+ BlockBody :: deserialize ( deserializer) . map ( Into :: into)
88+ }
89+ }
90+
91+ impl < T : SerdeBincodeCompat > SerdeBincodeCompat for alloy_consensus:: BlockBody < T > {
92+ type BincodeRepr < ' a > = BlockBody < ' a , T > ;
93+ }
94+ }
0 commit comments