1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
use ckb_hash::{blake2b_256, new_blake2b};
use crate::{core, packed, prelude::*};
pub(crate) trait CalcHash {
fn calc_hash(&self) -> packed::Byte32;
}
impl<'r, R> CalcHash for R
where
R: Reader<'r>,
{
fn calc_hash(&self) -> packed::Byte32 {
blake2b_256(self.as_slice()).pack()
}
}
macro_rules! impl_calc_special_hash_for_entity {
($entity:ident, $func_name:ident, $return:ty, $comment:expr) => {
impl packed::$entity {
#[doc = $comment]
pub fn $func_name(&self) -> $return {
self.as_reader().$func_name()
}
}
};
($entity:ident, $func_name:ident, $return:ty) => {
impl_calc_special_hash_for_entity!(
$entity,
$func_name,
$return,
concat!(
"Calls [`",
stringify!($entity),
"Reader.",
stringify!($func_name),
"()`](struct.",
stringify!($entity),
"Reader.html#method.",
stringify!($func_name),
") for [`self.as_reader()`](#method.as_reader)."
)
);
};
($entity:ident, $func_name:ident) => {
impl_calc_special_hash_for_entity!($entity, $func_name, packed::Byte32);
};
}
impl packed::CellOutput {
pub fn calc_data_hash(data: &[u8]) -> packed::Byte32 {
if data.is_empty() {
packed::Byte32::zero()
} else {
blake2b_256(data).pack()
}
}
}
impl<'r> packed::BytesReader<'r> {
pub fn calc_raw_data_hash(&self) -> packed::Byte32 {
blake2b_256(self.raw_data()).pack()
}
}
impl_calc_special_hash_for_entity!(Bytes, calc_raw_data_hash);
impl<'r> packed::ScriptReader<'r> {
pub fn calc_script_hash(&self) -> packed::Byte32 {
self.calc_hash()
}
}
impl_calc_special_hash_for_entity!(Script, calc_script_hash);
impl<'r> packed::CellOutputReader<'r> {
pub fn calc_lock_hash(&self) -> packed::Byte32 {
self.lock().calc_script_hash()
}
}
impl_calc_special_hash_for_entity!(CellOutput, calc_lock_hash);
impl<'r> packed::ProposalShortIdVecReader<'r> {
pub fn calc_proposals_hash(&self) -> packed::Byte32 {
if self.is_empty() {
packed::Byte32::zero()
} else {
let mut ret = [0u8; 32];
let mut blake2b = new_blake2b();
for id in self.iter() {
blake2b.update(id.as_slice());
}
blake2b.finalize(&mut ret);
ret.pack()
}
}
}
impl_calc_special_hash_for_entity!(ProposalShortIdVec, calc_proposals_hash);
impl<'r> packed::RawTransactionReader<'r> {
pub fn calc_tx_hash(&self) -> packed::Byte32 {
self.calc_hash()
}
}
impl_calc_special_hash_for_entity!(RawTransaction, calc_tx_hash);
impl<'r> packed::TransactionReader<'r> {
pub fn calc_tx_hash(&self) -> packed::Byte32 {
self.raw().calc_tx_hash()
}
pub fn calc_witness_hash(&self) -> packed::Byte32 {
self.calc_hash()
}
}
impl_calc_special_hash_for_entity!(Transaction, calc_tx_hash);
impl_calc_special_hash_for_entity!(Transaction, calc_witness_hash);
impl<'r> packed::RawHeaderReader<'r> {
pub fn calc_pow_hash(&self) -> packed::Byte32 {
self.calc_hash()
}
}
impl_calc_special_hash_for_entity!(RawHeader, calc_pow_hash);
impl<'r> packed::HeaderReader<'r> {
pub fn calc_pow_hash(&self) -> packed::Byte32 {
self.raw().calc_pow_hash()
}
pub fn calc_header_hash(&self) -> packed::Byte32 {
self.calc_hash()
}
}
impl_calc_special_hash_for_entity!(Header, calc_pow_hash);
impl_calc_special_hash_for_entity!(Header, calc_header_hash);
impl<'r> packed::UncleBlockReader<'r> {
pub fn calc_header_hash(&self) -> packed::Byte32 {
self.header().calc_header_hash()
}
pub fn calc_proposals_hash(&self) -> packed::Byte32 {
self.proposals().calc_proposals_hash()
}
}
impl_calc_special_hash_for_entity!(UncleBlock, calc_header_hash);
impl_calc_special_hash_for_entity!(UncleBlock, calc_proposals_hash);
impl<'r> packed::UncleBlockVecReader<'r> {
pub fn calc_uncles_hash(&self) -> packed::Byte32 {
if self.is_empty() {
packed::Byte32::zero()
} else {
let mut ret = [0u8; 32];
let mut blake2b = new_blake2b();
for uncle in self.iter() {
blake2b.update(uncle.calc_header_hash().as_slice());
}
blake2b.finalize(&mut ret);
ret.pack()
}
}
}
impl_calc_special_hash_for_entity!(UncleBlockVec, calc_uncles_hash);
impl<'r> packed::BlockReader<'r> {
pub fn calc_header_hash(&self) -> packed::Byte32 {
self.header().calc_header_hash()
}
pub fn calc_proposals_hash(&self) -> packed::Byte32 {
self.proposals().calc_proposals_hash()
}
pub fn calc_uncles_hash(&self) -> packed::Byte32 {
self.uncles().calc_uncles_hash()
}
pub fn calc_extension_hash(&self) -> Option<packed::Byte32> {
self.extension()
.map(|extension| extension.calc_raw_data_hash())
}
pub fn calc_extra_hash(&self) -> core::ExtraHashView {
core::ExtraHashView::new(self.calc_uncles_hash(), self.calc_extension_hash())
}
pub fn calc_tx_hashes(&self) -> Vec<packed::Byte32> {
self.transactions()
.iter()
.map(|tx| tx.calc_tx_hash())
.collect::<Vec<_>>()
}
pub fn calc_tx_witness_hashes(&self) -> Vec<packed::Byte32> {
self.transactions()
.iter()
.map(|tx| tx.calc_witness_hash())
.collect::<Vec<_>>()
}
}
impl_calc_special_hash_for_entity!(Block, calc_header_hash);
impl_calc_special_hash_for_entity!(Block, calc_proposals_hash);
impl_calc_special_hash_for_entity!(Block, calc_uncles_hash);
impl_calc_special_hash_for_entity!(Block, calc_extension_hash, Option<packed::Byte32>);
impl_calc_special_hash_for_entity!(Block, calc_extra_hash, core::ExtraHashView);
impl_calc_special_hash_for_entity!(Block, calc_tx_hashes, Vec<packed::Byte32>);
impl_calc_special_hash_for_entity!(Block, calc_tx_witness_hashes, Vec<packed::Byte32>);
impl<'r> packed::CompactBlockReader<'r> {
pub fn calc_header_hash(&self) -> packed::Byte32 {
self.header().calc_header_hash()
}
}
impl_calc_special_hash_for_entity!(CompactBlock, calc_header_hash);
impl<'r> packed::RawAlertReader<'r> {
pub fn calc_alert_hash(&self) -> packed::Byte32 {
self.calc_hash()
}
}
impl_calc_special_hash_for_entity!(RawAlert, calc_alert_hash);
impl<'r> packed::AlertReader<'r> {
pub fn calc_alert_hash(&self) -> packed::Byte32 {
self.raw().calc_alert_hash()
}
}
impl_calc_special_hash_for_entity!(Alert, calc_alert_hash);