kaspa_grpc_core/convert/
notification.rs

1use crate::protowire::{
2    kaspad_response::Payload, BlockAddedNotificationMessage, KaspadResponse, NewBlockTemplateNotificationMessage, RpcNotifyCommand,
3};
4use crate::protowire::{
5    FinalityConflictNotificationMessage, FinalityConflictResolvedNotificationMessage, NotifyPruningPointUtxoSetOverrideRequestMessage,
6    NotifyPruningPointUtxoSetOverrideResponseMessage, NotifyUtxosChangedRequestMessage, NotifyUtxosChangedResponseMessage,
7    PruningPointUtxoSetOverrideNotificationMessage, SinkBlueScoreChangedNotificationMessage,
8    StopNotifyingPruningPointUtxoSetOverrideRequestMessage, StopNotifyingPruningPointUtxoSetOverrideResponseMessage,
9    StopNotifyingUtxosChangedRequestMessage, StopNotifyingUtxosChangedResponseMessage, UtxosChangedNotificationMessage,
10    VirtualChainChangedNotificationMessage, VirtualDaaScoreChangedNotificationMessage,
11};
12use crate::{from, try_from};
13use kaspa_notify::subscription::Command;
14use kaspa_rpc_core::{Notification, RpcError, RpcHash};
15use std::str::FromStr;
16use std::sync::Arc;
17
18// ----------------------------------------------------------------------------
19// rpc_core to protowire
20// ----------------------------------------------------------------------------
21
22from!(item: &kaspa_rpc_core::Notification, KaspadResponse, { Self { id: 0, payload: Some(item.into()) } });
23
24from!(item: &kaspa_rpc_core::Notification, Payload, {
25    match item {
26        Notification::BlockAdded(ref notification) => Payload::BlockAddedNotification(notification.into()),
27        Notification::NewBlockTemplate(ref notification) => Payload::NewBlockTemplateNotification(notification.into()),
28        Notification::VirtualChainChanged(ref notification) => Payload::VirtualChainChangedNotification(notification.into()),
29        Notification::FinalityConflict(ref notification) => Payload::FinalityConflictNotification(notification.into()),
30        Notification::FinalityConflictResolved(ref notification) => Payload::FinalityConflictResolvedNotification(notification.into()),
31        Notification::UtxosChanged(ref notification) => Payload::UtxosChangedNotification(notification.into()),
32        Notification::SinkBlueScoreChanged(ref notification) => Payload::SinkBlueScoreChangedNotification(notification.into()),
33        Notification::VirtualDaaScoreChanged(ref notification) => Payload::VirtualDaaScoreChangedNotification(notification.into()),
34        Notification::PruningPointUtxoSetOverride(ref notification) => {
35            Payload::PruningPointUtxoSetOverrideNotification(notification.into())
36        }
37    }
38});
39
40from!(item: &kaspa_rpc_core::BlockAddedNotification, BlockAddedNotificationMessage, { Self { block: Some((&*item.block).into()) } });
41
42from!(&kaspa_rpc_core::NewBlockTemplateNotification, NewBlockTemplateNotificationMessage);
43
44from!(item: &kaspa_rpc_core::VirtualChainChangedNotification, VirtualChainChangedNotificationMessage, {
45    Self {
46        removed_chain_block_hashes: item.removed_chain_block_hashes.iter().map(|x| x.to_string()).collect(),
47        added_chain_block_hashes: item.added_chain_block_hashes.iter().map(|x| x.to_string()).collect(),
48        accepted_transaction_ids: item.accepted_transaction_ids.iter().map(|x| x.into()).collect(),
49    }
50});
51
52from!(item: &kaspa_rpc_core::FinalityConflictNotification, FinalityConflictNotificationMessage, {
53    Self { violating_block_hash: item.violating_block_hash.to_string() }
54});
55
56from!(item: &kaspa_rpc_core::FinalityConflictResolvedNotification, FinalityConflictResolvedNotificationMessage, {
57    Self { finality_block_hash: item.finality_block_hash.to_string() }
58});
59
60from!(item: &kaspa_rpc_core::UtxosChangedNotification, UtxosChangedNotificationMessage, {
61    Self {
62        added: item.added.iter().map(|x| x.into()).collect::<Vec<_>>(),
63        removed: item.removed.iter().map(|x| x.into()).collect::<Vec<_>>(),
64    }
65});
66
67from!(item: &kaspa_rpc_core::SinkBlueScoreChangedNotification, SinkBlueScoreChangedNotificationMessage, {
68    Self { sink_blue_score: item.sink_blue_score }
69});
70
71from!(item: &kaspa_rpc_core::VirtualDaaScoreChangedNotification, VirtualDaaScoreChangedNotificationMessage, {
72    Self { virtual_daa_score: item.virtual_daa_score }
73});
74
75from!(&kaspa_rpc_core::PruningPointUtxoSetOverrideNotification, PruningPointUtxoSetOverrideNotificationMessage);
76
77from!(item: Command, RpcNotifyCommand, {
78    match item {
79        Command::Start => RpcNotifyCommand::NotifyStart,
80        Command::Stop => RpcNotifyCommand::NotifyStop,
81    }
82});
83
84from!(item: &StopNotifyingUtxosChangedRequestMessage, NotifyUtxosChangedRequestMessage, {
85    Self { addresses: item.addresses.clone(), command: Command::Stop.into() }
86});
87
88from!(_item: &StopNotifyingPruningPointUtxoSetOverrideRequestMessage, NotifyPruningPointUtxoSetOverrideRequestMessage, {
89    Self { command: Command::Stop.into() }
90});
91
92// ----------------------------------------------------------------------------
93// protowire to rpc_core
94// ----------------------------------------------------------------------------
95
96try_from!(item: &KaspadResponse, kaspa_rpc_core::Notification, {
97    item.payload
98        .as_ref()
99        .ok_or_else(|| RpcError::MissingRpcFieldError("KaspadResponse".to_string(), "payload".to_string()))?
100        .try_into()?
101});
102
103try_from!(item: &Payload, kaspa_rpc_core::Notification, {
104    match item {
105        Payload::BlockAddedNotification(ref notification) => Notification::BlockAdded(notification.try_into()?),
106        Payload::NewBlockTemplateNotification(ref notification) => Notification::NewBlockTemplate(notification.try_into()?),
107        Payload::VirtualChainChangedNotification(ref notification) => Notification::VirtualChainChanged(notification.try_into()?),
108        Payload::FinalityConflictNotification(ref notification) => Notification::FinalityConflict(notification.try_into()?),
109        Payload::FinalityConflictResolvedNotification(ref notification) => {
110            Notification::FinalityConflictResolved(notification.try_into()?)
111        }
112        Payload::UtxosChangedNotification(ref notification) => Notification::UtxosChanged(notification.try_into()?),
113        Payload::SinkBlueScoreChangedNotification(ref notification) => Notification::SinkBlueScoreChanged(notification.try_into()?),
114        Payload::VirtualDaaScoreChangedNotification(ref notification) => {
115            Notification::VirtualDaaScoreChanged(notification.try_into()?)
116        }
117        Payload::PruningPointUtxoSetOverrideNotification(ref notification) => {
118            Notification::PruningPointUtxoSetOverride(notification.try_into()?)
119        }
120        _ => Err(RpcError::UnsupportedFeature)?,
121    }
122});
123
124try_from!(item: &BlockAddedNotificationMessage, kaspa_rpc_core::BlockAddedNotification, {
125    Self {
126        block: Arc::new(
127            item.block
128                .as_ref()
129                .ok_or_else(|| RpcError::MissingRpcFieldError("BlockAddedNotificationMessage".to_string(), "block".to_string()))?
130                .try_into()?,
131        ),
132    }
133});
134
135try_from!(&NewBlockTemplateNotificationMessage, kaspa_rpc_core::NewBlockTemplateNotification);
136
137try_from!(item: &VirtualChainChangedNotificationMessage, kaspa_rpc_core::VirtualChainChangedNotification, {
138    Self {
139        removed_chain_block_hashes: Arc::new(
140            item.removed_chain_block_hashes.iter().map(|x| RpcHash::from_str(x)).collect::<Result<Vec<_>, _>>()?,
141        ),
142        added_chain_block_hashes: Arc::new(
143            item.added_chain_block_hashes.iter().map(|x| RpcHash::from_str(x)).collect::<Result<Vec<_>, _>>()?,
144        ),
145        accepted_transaction_ids: Arc::new(item.accepted_transaction_ids.iter().map(|x| x.try_into()).collect::<Result<Vec<_>, _>>()?),
146    }
147});
148
149try_from!(item: &FinalityConflictNotificationMessage, kaspa_rpc_core::FinalityConflictNotification, {
150    Self { violating_block_hash: RpcHash::from_str(&item.violating_block_hash)? }
151});
152
153try_from!(item: &FinalityConflictResolvedNotificationMessage, kaspa_rpc_core::FinalityConflictResolvedNotification, {
154    Self { finality_block_hash: RpcHash::from_str(&item.finality_block_hash)? }
155});
156
157try_from!(item: &UtxosChangedNotificationMessage, kaspa_rpc_core::UtxosChangedNotification, {
158    Self {
159        added: Arc::new(item.added.iter().map(|x| x.try_into()).collect::<Result<Vec<_>, _>>()?),
160        removed: Arc::new(item.removed.iter().map(|x| x.try_into()).collect::<Result<Vec<_>, _>>()?),
161    }
162});
163
164try_from!(item: &SinkBlueScoreChangedNotificationMessage, kaspa_rpc_core::SinkBlueScoreChangedNotification, {
165    Self { sink_blue_score: item.sink_blue_score }
166});
167
168try_from!(item: &VirtualDaaScoreChangedNotificationMessage, kaspa_rpc_core::VirtualDaaScoreChangedNotification, {
169    Self { virtual_daa_score: item.virtual_daa_score }
170});
171
172try_from!(&PruningPointUtxoSetOverrideNotificationMessage, kaspa_rpc_core::PruningPointUtxoSetOverrideNotification);
173
174from!(item: RpcNotifyCommand, Command, {
175    match item {
176        RpcNotifyCommand::NotifyStart => Command::Start,
177        RpcNotifyCommand::NotifyStop => Command::Stop,
178    }
179});
180
181from!(item: NotifyUtxosChangedResponseMessage, StopNotifyingUtxosChangedResponseMessage, { Self { error: item.error } });
182
183from!(item: NotifyPruningPointUtxoSetOverrideResponseMessage, StopNotifyingPruningPointUtxoSetOverrideResponseMessage, {
184    Self { error: item.error }
185});