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