kaspa_rpc_core/model/
block.rs

1use super::RpcRawHeader;
2use crate::prelude::{RpcHash, RpcHeader, RpcTransaction};
3use serde::{Deserialize, Serialize};
4use workflow_serializer::prelude::*;
5
6/// Raw Rpc block type - without a cached header hash and without verbose data.
7/// Used for mining APIs (get_block_template & submit_block)
8#[derive(Clone, Debug, Serialize, Deserialize)]
9#[serde(rename_all = "camelCase")]
10pub struct RpcRawBlock {
11    pub header: RpcRawHeader,
12    pub transactions: Vec<RpcTransaction>,
13}
14
15#[derive(Clone, Debug, Serialize, Deserialize)]
16#[serde(rename_all = "camelCase")]
17pub struct RpcBlock {
18    pub header: RpcHeader,
19    pub transactions: Vec<RpcTransaction>,
20    pub verbose_data: Option<RpcBlockVerboseData>,
21}
22
23impl Serializer for RpcBlock {
24    fn serialize<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
25        store!(u16, &1, writer)?;
26        serialize!(RpcHeader, &self.header, writer)?;
27        serialize!(Vec<RpcTransaction>, &self.transactions, writer)?;
28        serialize!(Option<RpcBlockVerboseData>, &self.verbose_data, writer)?;
29
30        Ok(())
31    }
32}
33
34impl Deserializer for RpcBlock {
35    fn deserialize<R: std::io::Read>(reader: &mut R) -> std::io::Result<Self> {
36        let _version = load!(u16, reader)?;
37        let header = deserialize!(RpcHeader, reader)?;
38        let transactions = deserialize!(Vec<RpcTransaction>, reader)?;
39        let verbose_data = deserialize!(Option<RpcBlockVerboseData>, reader)?;
40
41        Ok(Self { header, transactions, verbose_data })
42    }
43}
44
45impl Serializer for RpcRawBlock {
46    fn serialize<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
47        store!(u16, &1, writer)?;
48        serialize!(RpcRawHeader, &self.header, writer)?;
49        serialize!(Vec<RpcTransaction>, &self.transactions, writer)?;
50
51        Ok(())
52    }
53}
54
55impl Deserializer for RpcRawBlock {
56    fn deserialize<R: std::io::Read>(reader: &mut R) -> std::io::Result<Self> {
57        let _version = load!(u16, reader)?;
58        let header = deserialize!(RpcRawHeader, reader)?;
59        let transactions = deserialize!(Vec<RpcTransaction>, reader)?;
60
61        Ok(Self { header, transactions })
62    }
63}
64
65#[derive(Clone, Debug, Serialize, Deserialize)]
66#[serde(rename_all = "camelCase")]
67pub struct RpcBlockVerboseData {
68    pub hash: RpcHash,
69    pub difficulty: f64,
70    pub selected_parent_hash: RpcHash,
71    pub transaction_ids: Vec<RpcHash>,
72    pub is_header_only: bool,
73    pub blue_score: u64,
74    pub children_hashes: Vec<RpcHash>,
75    pub merge_set_blues_hashes: Vec<RpcHash>,
76    pub merge_set_reds_hashes: Vec<RpcHash>,
77    pub is_chain_block: bool,
78}
79
80impl Serializer for RpcBlockVerboseData {
81    fn serialize<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
82        store!(u8, &1, writer)?;
83        store!(RpcHash, &self.hash, writer)?;
84        store!(f64, &self.difficulty, writer)?;
85        store!(RpcHash, &self.selected_parent_hash, writer)?;
86        store!(Vec<RpcHash>, &self.transaction_ids, writer)?;
87        store!(bool, &self.is_header_only, writer)?;
88        store!(u64, &self.blue_score, writer)?;
89        store!(Vec<RpcHash>, &self.children_hashes, writer)?;
90        store!(Vec<RpcHash>, &self.merge_set_blues_hashes, writer)?;
91        store!(Vec<RpcHash>, &self.merge_set_reds_hashes, writer)?;
92        store!(bool, &self.is_chain_block, writer)?;
93
94        Ok(())
95    }
96}
97
98impl Deserializer for RpcBlockVerboseData {
99    fn deserialize<R: std::io::Read>(reader: &mut R) -> std::io::Result<Self> {
100        let _version = load!(u8, reader)?;
101        let hash = load!(RpcHash, reader)?;
102        let difficulty = load!(f64, reader)?;
103        let selected_parent_hash = load!(RpcHash, reader)?;
104        let transaction_ids = load!(Vec<RpcHash>, reader)?;
105        let is_header_only = load!(bool, reader)?;
106        let blue_score = load!(u64, reader)?;
107        let children_hashes = load!(Vec<RpcHash>, reader)?;
108        let merge_set_blues_hashes = load!(Vec<RpcHash>, reader)?;
109        let merge_set_reds_hashes = load!(Vec<RpcHash>, reader)?;
110        let is_chain_block = load!(bool, reader)?;
111
112        Ok(Self {
113            hash,
114            difficulty,
115            selected_parent_hash,
116            transaction_ids,
117            is_header_only,
118            blue_score,
119            children_hashes,
120            merge_set_blues_hashes,
121            merge_set_reds_hashes,
122            is_chain_block,
123        })
124    }
125}
126
127cfg_if::cfg_if! {
128    if #[cfg(feature = "wasm32-sdk")] {
129        use wasm_bindgen::prelude::*;
130
131        #[wasm_bindgen(typescript_custom_section)]
132        const TS_BLOCK: &'static str = r#"
133        /**
134         * Interface defining the structure of a block.
135         * 
136         * @category Consensus
137         */
138        export interface IBlock {
139            header: IHeader;
140            transactions: ITransaction[];
141            verboseData?: IBlockVerboseData;
142        }
143
144        /**
145         * Interface defining the structure of a block verbose data.
146         * 
147         * @category Node RPC
148         */
149        export interface IBlockVerboseData {
150            hash: HexString;
151            difficulty: number;
152            selectedParentHash: HexString;
153            transactionIds: HexString[];
154            isHeaderOnly: boolean;
155            blueScore: number;
156            childrenHashes: HexString[];
157            mergeSetBluesHashes: HexString[];
158            mergeSetRedsHashes: HexString[];
159            isChainBlock: boolean;
160        }
161
162        /**
163         * Interface defining the structure of a raw block.
164         * 
165         * Raw block is a structure used by GetBlockTemplate and SubmitBlock RPCs
166         * and differs from `IBlock` in that it does not include verbose data and carries
167         * `IRawHeader` that does not include a cached block hash.
168         * 
169         * @category Consensus
170         */
171        export interface IRawBlock {
172            header: IRawHeader;
173            transactions: ITransaction[];
174        }
175
176        "#;
177    }
178}