ckb_types/
prelude.rs

1//! This module includes several traits.
2//!
3//! Few traits are re-exported from other crates, few are used as aliases and others are syntactic sugar.
4//!
5
6pub use crate::utilities::merkle_mountain_range::ProverMessageBuilder;
7use crate::{
8    U256,
9    core::{
10        BlockBuilder, BlockView, ExtraHashView, HeaderBuilder, HeaderView, TransactionBuilder,
11        TransactionView, UncleBlockView,
12    },
13    packed,
14};
15
16pub use ckb_gen_types::prelude::*;
17
18use std::collections::HashSet;
19
20/// Trait for converting types into `TransactionView`.
21pub trait IntoTransactionView {
22    /// Converts the implementing type into a `TransactionView`.
23    fn into_view(self) -> TransactionView;
24}
25
26/// Trait for converting types into `HeaderView`.
27pub trait IntoHeaderView {
28    /// Converts the implementing type into a `HeaderView`.
29    fn into_view(self) -> HeaderView;
30}
31
32/// Trait for converting types into `UncleBlockView`.
33pub trait IntoUncleBlockView {
34    /// Converts the implementing type into an `UncleBlockView`.
35    fn into_view(self) -> UncleBlockView;
36}
37
38/// Trait for converting types into `BlockView`.
39pub trait IntoBlockView {
40    /// Converts the implementing type into a `BlockView` without resetting the header.
41    fn into_view_without_reset_header(self) -> BlockView;
42
43    /// Converts the implementing type into a `BlockView`.
44    fn into_view(self) -> BlockView;
45
46    /// Converts a packed block and associated data into a `BlockView`.
47    fn block_into_view_internal(
48        block: packed::Block,
49        tx_hashes: Vec<packed::Byte32>,
50        tx_witness_hashes: Vec<packed::Byte32>,
51    ) -> BlockView;
52}
53
54/// Trait for obtaining an advanced builder for `BlockView`.
55pub trait AsBlockBuilder {
56    /// Creates a new advanced builder for `BlockView`.
57    fn new_advanced_builder() -> BlockBuilder;
58
59    /// Gets an advanced builder from the implementing type.
60    fn as_advanced_builder(&self) -> BlockBuilder;
61}
62
63/// Trait for obtaining an advanced builder for `TransactionView`.
64pub trait AsTransactionBuilder {
65    /// Gets an advanced builder for `TransactionView` from the implementing type.
66    fn as_advanced_builder(&self) -> TransactionBuilder;
67}
68
69/// Trait for obtaining an advanced builder for `HeaderView`.
70pub trait AsHeaderBuilder {
71    /// Gets an advanced builder for `HeaderView` from the implementing type.
72    fn as_advanced_builder(&self) -> HeaderBuilder;
73}
74
75/// Trait for calculating difficulty.
76pub trait Difficulty {
77    /// Calculates and returns the difficulty value as a `U256`.
78    fn difficulty(&self) -> U256;
79}
80
81/// Trait for building a compact block from a `BlockView`.
82pub trait BuildCompactBlock {
83    /// Builds a compact block from a `BlockView` and a set of prefilled transaction indexes.
84    fn build_from_block(
85        block: &BlockView,
86        prefilled_transactions_indexes: &HashSet<usize>,
87    ) -> packed::CompactBlock;
88
89    /// Returns the short IDs of the transactions in the compact block.
90    fn block_short_ids(&self) -> Vec<Option<packed::ProposalShortId>>;
91
92    /// Returns the indexes of the short IDs in the compact block.
93    fn short_id_indexes(&self) -> Vec<usize>;
94}
95
96/// Trait for resetting the header of a packed block.
97pub trait ResetBlock {
98    /// Resets the header of the packed block.
99    fn reset_header(self) -> packed::Block;
100
101    /// Resets the header of the packed block with given transaction hashes and witness hashes.
102    fn reset_header_with_hashes(
103        self,
104        tx_hashes: &[packed::Byte32],
105        tx_witness_hashes: &[packed::Byte32],
106    ) -> packed::Block;
107}
108
109/// Trait for calculating the extra hash of a block.
110pub trait CalcExtraHash {
111    /// Calculates and returns the extra hash of the block as an `ExtraHashView`.
112    fn calc_extra_hash(&self) -> ExtraHashView;
113}