Skip to main content

ethrex_storage/
rlp.rs

1use std::fmt::Debug;
2use std::marker::PhantomData;
3
4use ethrex_common::{
5    H256,
6    types::{Block, BlockBody, BlockHeader, Receipt},
7};
8use ethrex_rlp::{decode::RLPDecode, encode::RLPEncode};
9
10// Account types
11pub type AccountCodeHashRLP = Rlp<H256>;
12
13// Block types
14pub type BlockHeaderRLP = Rlp<BlockHeader>;
15pub type BlockBodyRLP = Rlp<BlockBody>;
16pub type BlockRLP = Rlp<Block>;
17
18// Receipt types
19#[allow(unused)]
20pub type ReceiptRLP = Rlp<Receipt>;
21
22#[derive(Clone, Debug)]
23pub struct Rlp<T>(Vec<u8>, PhantomData<T>);
24
25impl<T: RLPEncode> From<T> for Rlp<T> {
26    fn from(value: T) -> Self {
27        let mut buf = Vec::new();
28        RLPEncode::encode(&value, &mut buf);
29        Self(buf, Default::default())
30    }
31}
32
33impl<T: RLPDecode> Rlp<T> {
34    pub fn to(&self) -> Result<T, ethrex_rlp::error::RLPDecodeError> {
35        T::decode(&self.0)
36    }
37}
38
39impl<T> Rlp<T> {
40    pub fn from_bytes(bytes: Vec<u8>) -> Self {
41        Self(bytes, Default::default())
42    }
43
44    pub fn bytes(&self) -> &Vec<u8> {
45        &self.0
46    }
47
48    pub fn into_vec(self) -> Vec<u8> {
49        self.0
50    }
51}