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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
use ckb_hash::{blake2b_256, new_blake2b};

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

/*
 * 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::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 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_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);

#[cfg(test)]
mod tests {
    use crate::{h256, packed, prelude::*, H256};
    use ckb_hash::blake2b_256;

    #[test]
    fn proposals_hash() {
        let proposal1 = [1; 10].pack();
        let proposal2 = [2; 10].pack();
        let proposals = vec![proposal1, proposal2].pack();
        let expect = h256!("0xd1670e45af1deb9cc00951d71c09ce80932e7ddf9fb151d744436bd04ac4a562");
        assert_eq!(proposals.calc_proposals_hash(), expect.pack());
    }

    #[test]
    fn empty_proposals_hash() {
        let proposals = packed::ProposalShortIdVec::new_builder().build();
        let expect = h256!("0x0");
        assert_eq!(proposals.calc_proposals_hash(), expect.pack());
    }

    #[test]
    fn uncles_hash() {
        let uncle1_raw_header = packed::RawHeader::new_builder()
            .version(0u32.pack())
            .compact_target(0x1e08_3126u32.pack())
            .timestamp(0x5cd2_b117u64.pack())
            .number(0x400u64.pack())
            .epoch(0x0007_0800_1800_0001u64.pack())
            .parent_hash(
                h256!("0x8381df265c9442d5c27559b167892c5a6a8322871112d3cc8ef45222c6624831").pack(),
            )
            .transactions_root(
                h256!("0x12214693b8bd5c3d8f96e270dc8fe32b1702bd97630a9eab53a69793e6bc893f").pack(),
            )
            .proposals_hash(
                h256!("0xd1670e45af1deb9cc00951d71c09ce80932e7ddf9fb151d744436bd04ac4a562").pack(),
            )
            .uncles_hash(
                h256!("0x0000000000000000000000000000000000000000000000000000000000000000").pack(),
            )
            .dao(h256!("0xb54bdd7f6be90000bb52f392d41cd70024f7ef29b437000000febffacf030000").pack())
            .build();
        let uncle1_header = packed::Header::new_builder()
            .raw(uncle1_raw_header)
            .nonce(0x5ff1_389a_f870_6543_11a2_bee6_1237u128.pack())
            .build();
        let uncle1_proposals = vec![[1; 10].pack(), [2; 10].pack()].pack();
        let uncle1 = packed::UncleBlock::new_builder()
            .header(uncle1_header)
            .proposals(uncle1_proposals)
            .build();

        let uncle2_raw_header = packed::RawHeader::new_builder()
            .version(0u32.pack())
            .compact_target(0x2001_0000u32.pack())
            .timestamp(0x5cd2_1a16u64.pack())
            .number(0x400u64.pack())
            .epoch(0x0007_0800_1800_0001u64.pack())
            .parent_hash(
                h256!("0x8381df265c9442d5c27559b167892c5a6a8322871112d3cc8ef45222c6624831").pack(),
            )
            .transactions_root(
                h256!("0x12214693b8bd5c3d8f96e270dc8fe32b1702bd97630a9eab53a69793e6bc893f").pack(),
            )
            .proposals_hash(
                h256!("0x0000000000000000000000000000000000000000000000000000000000000000").pack(),
            )
            .uncles_hash(
                h256!("0x0000000000000000000000000000000000000000000000000000000000000000").pack(),
            )
            .dao(h256!("0xb54bdd7f6be90000bb52f392d41cd70024f7ef29b437000000febffacf030000").pack())
            .build();
        let uncle2_header = packed::Header::new_builder()
            .raw(uncle2_raw_header)
            .nonce(0x2f39_2d41_cd70_024fu128.pack())
            .build();
        let uncle2 = packed::UncleBlock::new_builder()
            .header(uncle2_header)
            .build();

        let uncles = vec![uncle1, uncle2].pack();
        let expect = h256!("0x0135d01f169a870bd9c92b2b37aecfa0fbfb7c1862cc176e03bb525fab0649d9");
        assert_eq!(uncles.calc_uncles_hash(), expect.pack());
    }

    #[test]
    fn empty_uncles_hash() {
        let uncles = packed::UncleBlockVec::new_builder().build();
        let expect = h256!("0x0");
        assert_eq!(uncles.calc_uncles_hash(), expect.pack());
    }

    #[test]
    fn empty_script_hash() {
        let script = packed::Script::new_builder().build();
        let expect = h256!("0x77c93b0632b5b6c3ef922c5b7cea208fb0a7c427a13d50e13d3fefad17e0c590");
        assert_eq!(script.calc_script_hash(), expect.pack());
    }

    #[test]
    fn always_success_script_hash() {
        let always_success = include_bytes!("../../../../script/testdata/always_success");
        let always_success_hash = blake2b_256(&always_success[..]);

        let script = packed::Script::new_builder()
            .code_hash(always_success_hash.pack())
            .build();
        let expect = h256!("0x4ceaa32f692948413e213ce6f3a83337145bde6e11fd8cb94377ce2637dcc412");
        assert_eq!(script.calc_script_hash(), expect.pack());
    }

    #[test]
    fn one_arg_script_hash() {
        let script = packed::Script::new_builder().args(vec![1].pack()).build();
        let expect = h256!("0x67951b34bce20cb71b7e235c1f8cda259628d99d94825bffe549c23b4dd2930f");
        assert_eq!(script.calc_script_hash(), expect.pack());
    }
}