kaspa_rpc_core/convert/
block.rs

1//! Conversion of Block related types
2
3use std::sync::Arc;
4
5use crate::{RpcBlock, RpcError, RpcRawBlock, RpcResult, RpcTransaction};
6use kaspa_consensus_core::block::{Block, MutableBlock};
7
8// ----------------------------------------------------------------------------
9// consensus_core to rpc_core
10// ----------------------------------------------------------------------------
11
12impl From<&Block> for RpcBlock {
13    fn from(item: &Block) -> Self {
14        Self {
15            header: item.header.as_ref().into(),
16            transactions: item.transactions.iter().map(RpcTransaction::from).collect(),
17            // TODO: Implement a populating process inspired from kaspad\app\rpc\rpccontext\verbosedata.go
18            verbose_data: None,
19        }
20    }
21}
22
23impl From<&Block> for RpcRawBlock {
24    fn from(item: &Block) -> Self {
25        Self { header: item.header.as_ref().into(), transactions: item.transactions.iter().map(RpcTransaction::from).collect() }
26    }
27}
28
29impl From<&MutableBlock> for RpcBlock {
30    fn from(item: &MutableBlock) -> Self {
31        Self {
32            header: item.header.as_ref().into(),
33            transactions: item.transactions.iter().map(RpcTransaction::from).collect(),
34            verbose_data: None,
35        }
36    }
37}
38
39impl From<&MutableBlock> for RpcRawBlock {
40    fn from(item: &MutableBlock) -> Self {
41        Self { header: item.header.as_ref().into(), transactions: item.transactions.iter().map(RpcTransaction::from).collect() }
42    }
43}
44
45impl From<MutableBlock> for RpcRawBlock {
46    fn from(item: MutableBlock) -> Self {
47        Self { header: item.header.into(), transactions: item.transactions.iter().map(RpcTransaction::from).collect() }
48    }
49}
50
51// ----------------------------------------------------------------------------
52// rpc_core to consensus_core
53// ----------------------------------------------------------------------------
54
55impl TryFrom<RpcBlock> for Block {
56    type Error = RpcError;
57    fn try_from(item: RpcBlock) -> RpcResult<Self> {
58        Ok(Self {
59            header: Arc::new(item.header.into()),
60            transactions: Arc::new(
61                item.transactions
62                    .into_iter()
63                    .map(kaspa_consensus_core::tx::Transaction::try_from)
64                    .collect::<RpcResult<Vec<kaspa_consensus_core::tx::Transaction>>>()?,
65            ),
66        })
67    }
68}
69
70impl TryFrom<RpcRawBlock> for Block {
71    type Error = RpcError;
72    fn try_from(item: RpcRawBlock) -> RpcResult<Self> {
73        Ok(Self {
74            header: Arc::new(item.header.into()),
75            transactions: Arc::new(
76                item.transactions
77                    .into_iter()
78                    .map(kaspa_consensus_core::tx::Transaction::try_from)
79                    .collect::<RpcResult<Vec<kaspa_consensus_core::tx::Transaction>>>()?,
80            ),
81        })
82    }
83}