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
//! This module includes several traits.
//!
//! Few traits are re-exported from other crates, few are used as aliases and others are syntactic sugar.
//!

pub use crate::utilities::merkle_mountain_range::ProverMessageBuilder;
use crate::{
    core::{
        BlockBuilder, BlockView, ExtraHashView, HeaderBuilder, HeaderView, TransactionBuilder,
        TransactionView, UncleBlockView,
    },
    packed, U256,
};

pub use ckb_gen_types::prelude::*;

use std::collections::HashSet;

/// Trait for converting types into `TransactionView`.
pub trait IntoTransactionView {
    /// Converts the implementing type into a `TransactionView`.
    fn into_view(self) -> TransactionView;
}

/// Trait for converting types into `HeaderView`.
pub trait IntoHeaderView {
    /// Converts the implementing type into a `HeaderView`.
    fn into_view(self) -> HeaderView;
}

/// Trait for converting types into `UncleBlockView`.
pub trait IntoUncleBlockView {
    /// Converts the implementing type into an `UncleBlockView`.
    fn into_view(self) -> UncleBlockView;
}

/// Trait for converting types into `BlockView`.
pub trait IntoBlockView {
    /// Converts the implementing type into a `BlockView` without resetting the header.
    fn into_view_without_reset_header(self) -> BlockView;

    /// Converts the implementing type into a `BlockView`.
    fn into_view(self) -> BlockView;

    /// Converts a packed block and associated data into a `BlockView`.
    fn block_into_view_internal(
        block: packed::Block,
        tx_hashes: Vec<packed::Byte32>,
        tx_witness_hashes: Vec<packed::Byte32>,
    ) -> BlockView;
}

/// Trait for obtaining an advanced builder for `BlockView`.
pub trait AsBlockBuilder {
    /// Creates a new advanced builder for `BlockView`.
    fn new_advanced_builder() -> BlockBuilder;

    /// Gets an advanced builder from the implementing type.
    fn as_advanced_builder(&self) -> BlockBuilder;
}

/// Trait for obtaining an advanced builder for `TransactionView`.
pub trait AsTransactionBuilder {
    /// Gets an advanced builder for `TransactionView` from the implementing type.
    fn as_advanced_builder(&self) -> TransactionBuilder;
}

/// Trait for obtaining an advanced builder for `HeaderView`.
pub trait AsHeaderBuilder {
    /// Gets an advanced builder for `HeaderView` from the implementing type.
    fn as_advanced_builder(&self) -> HeaderBuilder;
}

/// Trait for calculating difficulty.
pub trait Difficulty {
    /// Calculates and returns the difficulty value as a `U256`.
    fn difficulty(&self) -> U256;
}

/// Trait for building a compact block from a `BlockView`.
pub trait BuildCompactBlock {
    /// Builds a compact block from a `BlockView` and a set of prefilled transaction indexes.
    fn build_from_block(
        block: &BlockView,
        prefilled_transactions_indexes: &HashSet<usize>,
    ) -> packed::CompactBlock;

    /// Returns the short IDs of the transactions in the compact block.
    fn block_short_ids(&self) -> Vec<Option<packed::ProposalShortId>>;

    /// Returns the indexes of the short IDs in the compact block.
    fn short_id_indexes(&self) -> Vec<usize>;
}

/// Trait for resetting the header of a packed block.
pub trait ResetBlock {
    /// Resets the header of the packed block.
    fn reset_header(self) -> packed::Block;

    /// Resets the header of the packed block with given transaction hashes and witness hashes.
    fn reset_header_with_hashes(
        self,
        tx_hashes: &[packed::Byte32],
        tx_witness_hashes: &[packed::Byte32],
    ) -> packed::Block;
}

/// Trait for calculating the extra hash of a block.
pub trait CalcExtraHash {
    /// Calculates and returns the extra hash of the block as an `ExtraHashView`.
    fn calc_extra_hash(&self) -> ExtraHashView;
}