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
use ckb_hash::{blake2b_256, new_blake2b};

use crate::{packed, prelude::*, vec::Vec};

/*
 * Calculate simple hash for packed bytes wrappers.
 */

// Do NOT use this trait directly.
// Please call the methods which specify the hash content.
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()
    }
}

/*
 * Calculate special hash for packed bytes wrappers.
 */

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 {
    /// Calculates the hash for cell data.
    ///
    /// Returns the empty hash if no data, otherwise, calculates the hash of the data and returns it.
    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> {
    /// Calculates the hash for raw data in `Bytes`.
    ///
    /// Returns the empty hash if no data, otherwise, calculates the hash of the data and returns it.
    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> {
    /// Calculates the hash for [self.as_slice()] as the script hash.
    ///
    /// [self.as_slice()]: ../prelude/trait.Reader.html#tymethod.as_slice
    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> {
    /// Calls [`ScriptReader.calc_script_hash()`] for [`self.lock()`].
    ///
    /// [`ScriptReader.calc_script_hash()`]: struct.ScriptReader.html#method.calc_script_hash
    /// [`self.lock()`]: #method.lock
    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> {
    /// Calculates the hash for proposals.
    ///
    /// Returns the empty hash if no proposals short ids, otherwise, calculates a hash for all
    /// proposals short ids and return it.
    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> {
    /// Calculates the hash for [self.as_slice()] as the transaction hash.
    ///
    /// [self.as_slice()]: ../prelude/trait.Reader.html#tymethod.as_slice
    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> {
    /// Calls [`RawTransactionReader.calc_tx_hash()`] for [`self.raw()`].
    ///
    /// [`RawTransactionReader.calc_tx_hash()`]: struct.RawTransactionReader.html#method.calc_tx_hash
    /// [`self.raw()`]: #method.raw
    pub fn calc_tx_hash(&self) -> packed::Byte32 {
        self.raw().calc_tx_hash()
    }

    /// Calculates the hash for [self.as_slice()] as the witness hash.
    ///
    /// [self.as_slice()]: ../prelude/trait.Reader.html#tymethod.as_slice
    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> {
    /// Calculates the hash for [self.as_slice()] as the pow hash.
    ///
    /// [self.as_slice()]: ../prelude/trait.Reader.html#tymethod.as_slice
    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> {
    /// Calls [`RawHeaderReader.calc_pow_hash()`] for [`self.raw()`].
    ///
    /// [`RawHeaderReader.calc_pow_hash()`]: struct.RawHeaderReader.html#method.calc_pow_hash
    /// [`self.raw()`]: #method.raw
    pub fn calc_pow_hash(&self) -> packed::Byte32 {
        self.raw().calc_pow_hash()
    }

    /// Calculates the hash for [self.as_slice()] as the header hash.
    ///
    /// [self.as_slice()]: ../prelude/trait.Reader.html#tymethod.as_slice
    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> {
    /// Calls [`HeaderReader.calc_header_hash()`] for [`self.header()`].
    ///
    /// [`HeaderReader.calc_header_hash()`]: struct.HeaderReader.html#method.calc_header_hash
    /// [`self.header()`]: #method.header
    pub fn calc_header_hash(&self) -> packed::Byte32 {
        self.header().calc_header_hash()
    }

    /// Calls [`ProposalShortIdVecReader.calc_proposals_hash()`] for [`self.proposals()`].
    ///
    /// [`ProposalShortIdVecReader.calc_proposals_hash()`]: struct.ProposalShortIdVecReader.html#method.calc_proposals_hash
    /// [`self.proposals()`]: #method.proposals
    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> {
    /// Calculates the hash for uncle blocks.
    ///
    /// Returns the empty hash if no uncle block, otherwise, calculates a hash for all header
    /// hashes of uncle blocks and returns it.
    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> {
    /// Calls [`HeaderReader.calc_header_hash()`] for [`self.header()`].
    ///
    /// [`HeaderReader.calc_header_hash()`]: struct.HeaderReader.html#method.calc_header_hash
    /// [`self.header()`]: #method.header
    pub fn calc_header_hash(&self) -> packed::Byte32 {
        self.header().calc_header_hash()
    }

    /// Calls [`ProposalShortIdVecReader.calc_proposals_hash()`] for [`self.proposals()`].
    ///
    /// [`ProposalShortIdVecReader.calc_proposals_hash()`]: struct.ProposalShortIdVecReader.html#method.calc_proposals_hash
    /// [`self.proposals()`]: #method.proposals
    pub fn calc_proposals_hash(&self) -> packed::Byte32 {
        self.proposals().calc_proposals_hash()
    }

    /// Calls [`UncleBlockVecReader.calc_uncles_hash()`] for [`self.uncles()`].
    ///
    /// [`UncleBlockVecReader.calc_uncles_hash()`]: struct.UncleBlockVecReader.html#method.calc_uncles_hash
    /// [`self.uncles()`]: #method.uncles
    pub fn calc_uncles_hash(&self) -> packed::Byte32 {
        self.uncles().calc_uncles_hash()
    }

    /// Calculates the hash for the extension.
    ///
    /// If there is an extension (unknown for now), calculate the hash of its data.
    pub fn calc_extension_hash(&self) -> Option<packed::Byte32> {
        self.extension()
            .map(|extension| extension.calc_raw_data_hash())
    }

    /// Calculates transaction hashes for all transactions in the block.
    pub fn calc_tx_hashes(&self) -> Vec<packed::Byte32> {
        self.transactions()
            .iter()
            .map(|tx| tx.calc_tx_hash())
            .collect::<Vec<_>>()
    }

    /// Calculates transaction witness hashes for all transactions in the block.
    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_tx_hashes, Vec<packed::Byte32>);
impl_calc_special_hash_for_entity!(Block, calc_tx_witness_hashes, Vec<packed::Byte32>);

impl<'r> packed::CompactBlockReader<'r> {
    /// Calls [`HeaderReader.calc_header_hash()`] for [`self.header()`].
    ///
    /// [`HeaderReader.calc_header_hash()`]: struct.HeaderReader.html#method.calc_header_hash
    /// [`self.header()`]: #method.header
    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> {
    /// Calculates the hash for [self.as_slice()] as the alert hash.
    ///
    /// [self.as_slice()]: ../prelude/trait.Reader.html#tymethod.as_slice
    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> {
    /// Calls [`RawAlertReader.calc_alert_hash()`] for [`self.raw()`].
    ///
    /// [`RawAlertReader.calc_alert_hash()`]: struct.RawAlertReader.html#method.calc_alert_hash
    /// [`self.raw()`]: #method.raw
    pub fn calc_alert_hash(&self) -> packed::Byte32 {
        self.raw().calc_alert_hash()
    }
}
impl_calc_special_hash_for_entity!(Alert, calc_alert_hash);

impl<'r> packed::HeaderDigestReader<'r> {
    /// Calculates the hash for [self.as_slice()] as the MMR node hash.
    ///
    /// [self.as_slice()]: ../prelude/trait.Reader.html#tymethod.as_slice
    pub fn calc_mmr_hash(&self) -> packed::Byte32 {
        self.calc_hash()
    }
}
impl_calc_special_hash_for_entity!(HeaderDigest, calc_mmr_hash);