ckb_types/conversion/
storage.rs

1use crate::{core, packed, prelude::*};
2
3impl Pack<packed::HeaderView> for core::HeaderView {
4    fn pack(&self) -> packed::HeaderView {
5        packed::HeaderView::new_builder()
6            .data(self.data())
7            .hash(self.hash())
8            .build()
9    }
10}
11
12impl<'r> Unpack<core::HeaderView> for packed::HeaderViewReader<'r> {
13    fn unpack(&self) -> core::HeaderView {
14        core::HeaderView {
15            data: self.data().to_entity(),
16            hash: self.hash().to_entity(),
17        }
18    }
19}
20impl_conversion_for_entity_unpack!(core::HeaderView, HeaderView);
21
22impl Pack<packed::UncleBlockVecView> for core::UncleBlockVecView {
23    fn pack(&self) -> packed::UncleBlockVecView {
24        packed::UncleBlockVecView::new_builder()
25            .data(self.data())
26            .hashes(self.hashes())
27            .build()
28    }
29}
30
31impl<'r> Unpack<core::UncleBlockVecView> for packed::UncleBlockVecViewReader<'r> {
32    fn unpack(&self) -> core::UncleBlockVecView {
33        core::UncleBlockVecView {
34            data: self.data().to_entity(),
35            hashes: self.hashes().to_entity(),
36        }
37    }
38}
39impl_conversion_for_entity_unpack!(core::UncleBlockVecView, UncleBlockVecView);
40
41impl Pack<packed::TransactionView> for core::TransactionView {
42    fn pack(&self) -> packed::TransactionView {
43        packed::TransactionView::new_builder()
44            .data(self.data())
45            .hash(self.hash())
46            .witness_hash(self.witness_hash())
47            .build()
48    }
49}
50
51impl<'r> Unpack<core::TransactionView> for packed::TransactionViewReader<'r> {
52    fn unpack(&self) -> core::TransactionView {
53        core::TransactionView {
54            data: self.data().to_entity(),
55            hash: self.hash().to_entity(),
56            witness_hash: self.witness_hash().to_entity(),
57        }
58    }
59}
60impl_conversion_for_entity_unpack!(core::TransactionView, TransactionView);
61
62impl<'r> Unpack<core::BlockExt> for packed::BlockExtReader<'r> {
63    fn unpack(&self) -> core::BlockExt {
64        core::BlockExt {
65            received_at: self.received_at().unpack(),
66            total_difficulty: self.total_difficulty().unpack(),
67            total_uncles_count: self.total_uncles_count().unpack(),
68            verified: self.verified().unpack(),
69            txs_fees: self.txs_fees().unpack(),
70            cycles: None,
71            txs_sizes: None,
72        }
73    }
74}
75impl_conversion_for_entity_unpack!(core::BlockExt, BlockExt);
76
77impl Pack<packed::BlockExtV1> for core::BlockExt {
78    fn pack(&self) -> packed::BlockExtV1 {
79        packed::BlockExtV1::new_builder()
80            .received_at(self.received_at.pack())
81            .total_difficulty(self.total_difficulty.pack())
82            .total_uncles_count(self.total_uncles_count.pack())
83            .verified(self.verified.pack())
84            .txs_fees((self.txs_fees[..]).pack())
85            .cycles(self.cycles.pack())
86            .txs_sizes(self.txs_sizes.pack())
87            .build()
88    }
89}
90
91impl<'r> Unpack<core::BlockExt> for packed::BlockExtV1Reader<'r> {
92    fn unpack(&self) -> core::BlockExt {
93        core::BlockExt {
94            received_at: self.received_at().unpack(),
95            total_difficulty: self.total_difficulty().unpack(),
96            total_uncles_count: self.total_uncles_count().unpack(),
97            verified: self.verified().unpack(),
98            txs_fees: self.txs_fees().unpack(),
99            cycles: self.cycles().unpack(),
100            txs_sizes: self.txs_sizes().unpack(),
101        }
102    }
103}
104impl_conversion_for_entity_unpack!(core::BlockExt, BlockExtV1);
105
106impl Pack<packed::EpochExt> for core::EpochExt {
107    fn pack(&self) -> packed::EpochExt {
108        packed::EpochExt::new_builder()
109            .number(self.number().pack())
110            .base_block_reward(self.base_block_reward().pack())
111            .remainder_reward(self.remainder_reward().pack())
112            .previous_epoch_hash_rate(self.previous_epoch_hash_rate().pack())
113            .last_block_hash_in_previous_epoch(self.last_block_hash_in_previous_epoch())
114            .start_number(self.start_number().pack())
115            .length(self.length().pack())
116            .compact_target(self.compact_target().pack())
117            .build()
118    }
119}
120
121impl<'r> Unpack<core::EpochExt> for packed::EpochExtReader<'r> {
122    fn unpack(&self) -> core::EpochExt {
123        core::EpochExt {
124            number: self.number().unpack(),
125            base_block_reward: self.base_block_reward().unpack(),
126            remainder_reward: self.remainder_reward().unpack(),
127            previous_epoch_hash_rate: self.previous_epoch_hash_rate().unpack(),
128            last_block_hash_in_previous_epoch: self.last_block_hash_in_previous_epoch().to_entity(),
129            start_number: self.start_number().unpack(),
130            length: self.length().unpack(),
131            compact_target: self.compact_target().unpack(),
132        }
133    }
134}
135impl_conversion_for_entity_unpack!(core::EpochExt, EpochExt);
136
137impl Pack<packed::TransactionInfo> for core::TransactionInfo {
138    fn pack(&self) -> packed::TransactionInfo {
139        let key = packed::TransactionKey::new_builder()
140            .block_hash(self.block_hash.clone())
141            .index(self.index.pack())
142            .build();
143        packed::TransactionInfo::new_builder()
144            .key(key)
145            .block_number(self.block_number.pack())
146            .block_epoch(self.block_epoch.pack())
147            .build()
148    }
149}
150
151impl<'r> Unpack<core::TransactionInfo> for packed::TransactionInfoReader<'r> {
152    fn unpack(&self) -> core::TransactionInfo {
153        core::TransactionInfo {
154            block_hash: self.key().block_hash().to_entity(),
155            index: self.key().index().unpack(),
156            block_number: self.block_number().unpack(),
157            block_epoch: self.block_epoch().unpack(),
158        }
159    }
160}
161impl_conversion_for_entity_unpack!(core::TransactionInfo, TransactionInfo);