stratus/eth/storage/permanent/rocks/types/
block.rs

1use std::fmt::Debug;
2
3use super::address::AddressRocksdb;
4use super::block_header::BlockHeaderRocksdb;
5use super::block_number::BlockNumberRocksdb;
6use super::hash::HashRocksdb;
7use super::transaction_mined::TransactionMinedRocksdb;
8use crate::eth::primitives::Address;
9use crate::eth::primitives::Block;
10use crate::eth::primitives::BlockHeader;
11use crate::eth::primitives::BlockNumber;
12use crate::eth::primitives::Hash;
13use crate::eth::primitives::TransactionMined;
14use crate::eth::storage::permanent::rocks::SerializeDeserializeWithContext;
15
16#[derive(Debug, Clone, PartialEq, Eq, bincode::Encode, bincode::Decode, serde::Serialize, serde::Deserialize)]
17#[cfg_attr(test, derive(fake::Dummy))]
18pub struct BlockRocksdb {
19    pub header: BlockHeaderRocksdb,
20    pub transactions: Vec<TransactionMinedRocksdb>,
21}
22
23impl From<Block> for BlockRocksdb {
24    fn from(item: Block) -> Self {
25        BlockRocksdb {
26            header: BlockHeaderRocksdb {
27                number: BlockNumberRocksdb::from(item.header.number),
28                hash: HashRocksdb::from(item.header.hash),
29                transactions_root: HashRocksdb::from(item.header.transactions_root),
30                gas_used: item.header.gas_used.into(),
31                gas_limit: item.header.gas_limit.into(),
32                bloom: item.header.bloom.into(),
33                timestamp: item.header.timestamp.into(),
34                parent_hash: HashRocksdb::from(item.header.parent_hash),
35                author: AddressRocksdb::from(item.header.author),
36                extra_data: item.header.extra_data.into(),
37                miner: AddressRocksdb::from(item.header.miner),
38                difficulty: item.header.difficulty.into(),
39                receipts_root: HashRocksdb::from(item.header.receipts_root),
40                uncle_hash: HashRocksdb::from(item.header.uncle_hash),
41                size: item.header.size.into(),
42                state_root: HashRocksdb::from(item.header.state_root),
43                total_difficulty: item.header.total_difficulty.into(),
44                nonce: item.header.nonce.into(),
45            },
46            transactions: item.transactions.into_iter().map(TransactionMinedRocksdb::from).collect(),
47        }
48    }
49}
50
51impl From<BlockRocksdb> for Block {
52    fn from(item: BlockRocksdb) -> Self {
53        let header = BlockHeader {
54            number: BlockNumber::from(item.header.number),
55            hash: Hash::from(item.header.hash),
56            transactions_root: Hash::from(item.header.transactions_root),
57            gas_used: item.header.gas_used.into(),
58            gas_limit: item.header.gas_limit.into(),
59            bloom: item.header.bloom.into(),
60            timestamp: item.header.timestamp.into(),
61            parent_hash: Hash::from(item.header.parent_hash),
62            author: Address::from(item.header.author),
63            extra_data: item.header.extra_data.into(),
64            miner: Address::from(item.header.miner),
65            difficulty: item.header.difficulty.into(),
66            receipts_root: Hash::from(item.header.receipts_root),
67            uncle_hash: Hash::from(item.header.uncle_hash),
68            size: item.header.size.into(),
69            state_root: Hash::from(item.header.state_root),
70            total_difficulty: item.header.total_difficulty.into(),
71            nonce: item.header.nonce.into(),
72        };
73        let transactions = item
74            .transactions
75            .into_iter()
76            .map(|tx| TransactionMined::from_rocks_primitives(tx, header.number.into(), header.hash.into()))
77            .collect();
78        Block { header, transactions }
79    }
80}
81
82impl SerializeDeserializeWithContext for BlockRocksdb {}