kaspa_rpc_core/convert/
notification.rs

1//! Conversion of Notification related types
2
3use crate::{
4    convert::utxo::utxo_set_into_rpc, BlockAddedNotification, FinalityConflictNotification, FinalityConflictResolvedNotification,
5    NewBlockTemplateNotification, Notification, PruningPointUtxoSetOverrideNotification, RpcAcceptedTransactionIds,
6    SinkBlueScoreChangedNotification, UtxosChangedNotification, VirtualChainChangedNotification, VirtualDaaScoreChangedNotification,
7};
8use kaspa_consensus_notify::notification as consensus_notify;
9use kaspa_index_core::notification as index_notify;
10use std::sync::Arc;
11
12// ----------------------------------------------------------------------------
13// consensus_core to rpc_core
14// ----------------------------------------------------------------------------
15
16impl From<consensus_notify::Notification> for Notification {
17    fn from(item: consensus_notify::Notification) -> Self {
18        (&item).into()
19    }
20}
21
22impl From<&consensus_notify::Notification> for Notification {
23    fn from(item: &consensus_notify::Notification) -> Self {
24        match item {
25            consensus_notify::Notification::BlockAdded(msg) => Notification::BlockAdded(msg.into()),
26            consensus_notify::Notification::VirtualChainChanged(msg) => Notification::VirtualChainChanged(msg.into()),
27            consensus_notify::Notification::FinalityConflict(msg) => Notification::FinalityConflict(msg.into()),
28            consensus_notify::Notification::FinalityConflictResolved(msg) => Notification::FinalityConflictResolved(msg.into()),
29            consensus_notify::Notification::UtxosChanged(msg) => Notification::UtxosChanged(msg.into()),
30            consensus_notify::Notification::SinkBlueScoreChanged(msg) => Notification::SinkBlueScoreChanged(msg.into()),
31            consensus_notify::Notification::VirtualDaaScoreChanged(msg) => Notification::VirtualDaaScoreChanged(msg.into()),
32            consensus_notify::Notification::PruningPointUtxoSetOverride(msg) => Notification::PruningPointUtxoSetOverride(msg.into()),
33            consensus_notify::Notification::NewBlockTemplate(msg) => Notification::NewBlockTemplate(msg.into()),
34        }
35    }
36}
37
38impl From<&consensus_notify::BlockAddedNotification> for BlockAddedNotification {
39    fn from(item: &consensus_notify::BlockAddedNotification) -> Self {
40        Self { block: Arc::new((&item.block).into()) }
41    }
42}
43
44impl From<&consensus_notify::VirtualChainChangedNotification> for VirtualChainChangedNotification {
45    fn from(item: &consensus_notify::VirtualChainChangedNotification) -> Self {
46        Self {
47            removed_chain_block_hashes: item.removed_chain_block_hashes.clone(),
48            added_chain_block_hashes: item.added_chain_block_hashes.clone(),
49            // If acceptance data array is empty, it means that the subscription was set to not
50            // include accepted_transaction_ids. Otherwise, we expect acceptance data to correlate
51            // with the added chain block hashes
52            accepted_transaction_ids: Arc::new(if item.added_chain_blocks_acceptance_data.is_empty() {
53                vec![]
54            } else {
55                item.added_chain_block_hashes
56                    .iter()
57                    .zip(item.added_chain_blocks_acceptance_data.iter())
58                    .map(|(hash, acceptance_data)| RpcAcceptedTransactionIds {
59                        accepting_block_hash: hash.to_owned(),
60                        // We collect accepted tx ids from all mergeset blocks
61                        accepted_transaction_ids: acceptance_data
62                            .iter()
63                            .flat_map(|x| x.accepted_transactions.iter().map(|tx| tx.transaction_id))
64                            .collect(),
65                    })
66                    .collect()
67            }),
68        }
69    }
70}
71
72impl From<&consensus_notify::FinalityConflictNotification> for FinalityConflictNotification {
73    fn from(item: &consensus_notify::FinalityConflictNotification) -> Self {
74        Self { violating_block_hash: item.violating_block_hash }
75    }
76}
77
78impl From<&consensus_notify::FinalityConflictResolvedNotification> for FinalityConflictResolvedNotification {
79    fn from(item: &consensus_notify::FinalityConflictResolvedNotification) -> Self {
80        Self { finality_block_hash: item.finality_block_hash }
81    }
82}
83
84impl From<&consensus_notify::UtxosChangedNotification> for UtxosChangedNotification {
85    fn from(_: &consensus_notify::UtxosChangedNotification) -> Self {
86        // TODO: investigate if this conversion is possible
87        UtxosChangedNotification::default()
88    }
89}
90
91impl From<&consensus_notify::SinkBlueScoreChangedNotification> for SinkBlueScoreChangedNotification {
92    fn from(item: &consensus_notify::SinkBlueScoreChangedNotification) -> Self {
93        Self { sink_blue_score: item.sink_blue_score }
94    }
95}
96
97impl From<&consensus_notify::VirtualDaaScoreChangedNotification> for VirtualDaaScoreChangedNotification {
98    fn from(item: &consensus_notify::VirtualDaaScoreChangedNotification) -> Self {
99        Self { virtual_daa_score: item.virtual_daa_score }
100    }
101}
102
103impl From<&consensus_notify::PruningPointUtxoSetOverrideNotification> for PruningPointUtxoSetOverrideNotification {
104    fn from(_: &consensus_notify::PruningPointUtxoSetOverrideNotification) -> Self {
105        Self {}
106    }
107}
108
109impl From<&consensus_notify::NewBlockTemplateNotification> for NewBlockTemplateNotification {
110    fn from(_: &consensus_notify::NewBlockTemplateNotification) -> Self {
111        Self {}
112    }
113}
114
115// ----------------------------------------------------------------------------
116// index to rpc_core
117// ----------------------------------------------------------------------------
118
119impl From<index_notify::Notification> for Notification {
120    fn from(item: index_notify::Notification) -> Self {
121        (&item).into()
122    }
123}
124
125impl From<&index_notify::Notification> for Notification {
126    fn from(item: &index_notify::Notification) -> Self {
127        match item {
128            index_notify::Notification::UtxosChanged(msg) => Notification::UtxosChanged(msg.into()),
129            index_notify::Notification::PruningPointUtxoSetOverride(msg) => Notification::PruningPointUtxoSetOverride(msg.into()),
130        }
131    }
132}
133
134impl From<&index_notify::PruningPointUtxoSetOverrideNotification> for PruningPointUtxoSetOverrideNotification {
135    fn from(_: &index_notify::PruningPointUtxoSetOverrideNotification) -> Self {
136        Self {}
137    }
138}
139
140impl From<&index_notify::UtxosChangedNotification> for UtxosChangedNotification {
141    // This is not intended to be ever called because no address prefix is available.
142    // Use kaspa_rpc_service::converter::index::IndexConverter instead.
143    fn from(item: &index_notify::UtxosChangedNotification) -> Self {
144        Self { added: Arc::new(utxo_set_into_rpc(&item.added, None)), removed: Arc::new(utxo_set_into_rpc(&item.removed, None)) }
145    }
146}