kaspa_grpc_core/convert/
kaspad.rs

1use crate::protowire::{kaspad_request, KaspadRequest, KaspadResponse};
2
3impl From<kaspad_request::Payload> for KaspadRequest {
4    fn from(item: kaspad_request::Payload) -> Self {
5        KaspadRequest { id: 0, payload: Some(item) }
6    }
7}
8
9impl AsRef<KaspadRequest> for KaspadRequest {
10    fn as_ref(&self) -> &Self {
11        self
12    }
13}
14
15impl AsRef<KaspadResponse> for KaspadResponse {
16    fn as_ref(&self) -> &Self {
17        self
18    }
19}
20
21pub mod kaspad_request_convert {
22    use crate::protowire::*;
23    use kaspa_rpc_core::{RpcError, RpcResult};
24
25    impl_into_kaspad_request!(Shutdown);
26    impl_into_kaspad_request!(SubmitBlock);
27    impl_into_kaspad_request!(GetBlockTemplate);
28    impl_into_kaspad_request!(GetBlock);
29    impl_into_kaspad_request!(GetInfo);
30
31    impl_into_kaspad_request!(GetCurrentNetwork);
32    impl_into_kaspad_request!(GetPeerAddresses);
33    impl_into_kaspad_request!(GetSink);
34    impl_into_kaspad_request!(GetMempoolEntry);
35    impl_into_kaspad_request!(GetMempoolEntries);
36    impl_into_kaspad_request!(GetConnectedPeerInfo);
37    impl_into_kaspad_request!(AddPeer);
38    impl_into_kaspad_request!(SubmitTransaction);
39    impl_into_kaspad_request!(SubmitTransactionReplacement);
40    impl_into_kaspad_request!(GetSubnetwork);
41    impl_into_kaspad_request!(GetVirtualChainFromBlock);
42    impl_into_kaspad_request!(GetBlocks);
43    impl_into_kaspad_request!(GetBlockCount);
44    impl_into_kaspad_request!(GetBlockDagInfo);
45    impl_into_kaspad_request!(ResolveFinalityConflict);
46    impl_into_kaspad_request!(GetHeaders);
47    impl_into_kaspad_request!(GetUtxosByAddresses);
48    impl_into_kaspad_request!(GetBalanceByAddress);
49    impl_into_kaspad_request!(GetBalancesByAddresses);
50    impl_into_kaspad_request!(GetSinkBlueScore);
51    impl_into_kaspad_request!(Ban);
52    impl_into_kaspad_request!(Unban);
53    impl_into_kaspad_request!(EstimateNetworkHashesPerSecond);
54    impl_into_kaspad_request!(GetMempoolEntriesByAddresses);
55    impl_into_kaspad_request!(GetCoinSupply);
56    impl_into_kaspad_request!(Ping);
57    impl_into_kaspad_request!(GetMetrics);
58    impl_into_kaspad_request!(GetConnections);
59    impl_into_kaspad_request!(GetSystemInfo);
60    impl_into_kaspad_request!(GetServerInfo);
61    impl_into_kaspad_request!(GetSyncStatus);
62    impl_into_kaspad_request!(GetDaaScoreTimestampEstimate);
63    impl_into_kaspad_request!(GetFeeEstimate);
64    impl_into_kaspad_request!(GetFeeEstimateExperimental);
65    impl_into_kaspad_request!(GetCurrentBlockColor);
66
67    impl_into_kaspad_request!(NotifyBlockAdded);
68    impl_into_kaspad_request!(NotifyNewBlockTemplate);
69    impl_into_kaspad_request!(NotifyUtxosChanged);
70    impl_into_kaspad_request!(NotifyPruningPointUtxoSetOverride);
71    impl_into_kaspad_request!(NotifyFinalityConflict);
72    impl_into_kaspad_request!(NotifyVirtualDaaScoreChanged);
73    impl_into_kaspad_request!(NotifyVirtualChainChanged);
74    impl_into_kaspad_request!(NotifySinkBlueScoreChanged);
75
76    macro_rules! impl_into_kaspad_request {
77        ($name:tt) => {
78            paste::paste! {
79                impl_into_kaspad_request_ex!(kaspa_rpc_core::[<$name Request>],[<$name RequestMessage>],[<$name Request>]);
80            }
81        };
82    }
83
84    use impl_into_kaspad_request;
85
86    macro_rules! impl_into_kaspad_request_ex {
87        // ($($core_struct:ident)::+, $($protowire_struct:ident)::+, $($variant:ident)::+) => {
88        ($core_struct:path, $protowire_struct:ident, $variant:ident) => {
89            // ----------------------------------------------------------------------------
90            // rpc_core to protowire
91            // ----------------------------------------------------------------------------
92
93            impl From<&$core_struct> for kaspad_request::Payload {
94                fn from(item: &$core_struct) -> Self {
95                    Self::$variant(item.into())
96                }
97            }
98
99            impl From<&$core_struct> for KaspadRequest {
100                fn from(item: &$core_struct) -> Self {
101                    Self { id: 0, payload: Some(item.into()) }
102                }
103            }
104
105            impl From<$core_struct> for kaspad_request::Payload {
106                fn from(item: $core_struct) -> Self {
107                    Self::$variant((&item).into())
108                }
109            }
110
111            impl From<$core_struct> for KaspadRequest {
112                fn from(item: $core_struct) -> Self {
113                    Self { id: 0, payload: Some((&item).into()) }
114                }
115            }
116
117            // ----------------------------------------------------------------------------
118            // protowire to rpc_core
119            // ----------------------------------------------------------------------------
120
121            impl TryFrom<&kaspad_request::Payload> for $core_struct {
122                type Error = RpcError;
123                fn try_from(item: &kaspad_request::Payload) -> RpcResult<Self> {
124                    if let kaspad_request::Payload::$variant(request) = item {
125                        request.try_into()
126                    } else {
127                        Err(RpcError::MissingRpcFieldError("Payload".to_string(), stringify!($variant).to_string()))
128                    }
129                }
130            }
131
132            impl TryFrom<&KaspadRequest> for $core_struct {
133                type Error = RpcError;
134                fn try_from(item: &KaspadRequest) -> RpcResult<Self> {
135                    item.payload
136                        .as_ref()
137                        .ok_or(RpcError::MissingRpcFieldError("KaspaRequest".to_string(), "Payload".to_string()))?
138                        .try_into()
139                }
140            }
141
142            impl From<$protowire_struct> for KaspadRequest {
143                fn from(item: $protowire_struct) -> Self {
144                    Self { id: 0, payload: Some(kaspad_request::Payload::$variant(item)) }
145                }
146            }
147
148            impl From<$protowire_struct> for kaspad_request::Payload {
149                fn from(item: $protowire_struct) -> Self {
150                    kaspad_request::Payload::$variant(item)
151                }
152            }
153        };
154    }
155    use impl_into_kaspad_request_ex;
156}
157
158pub mod kaspad_response_convert {
159    use crate::protowire::*;
160    use kaspa_rpc_core::{RpcError, RpcResult};
161
162    impl_into_kaspad_response!(Shutdown);
163    impl_into_kaspad_response!(SubmitBlock);
164    impl_into_kaspad_response!(GetBlockTemplate);
165    impl_into_kaspad_response!(GetBlock);
166    impl_into_kaspad_response!(GetInfo);
167    impl_into_kaspad_response!(GetCurrentNetwork);
168
169    impl_into_kaspad_response!(GetPeerAddresses);
170    impl_into_kaspad_response!(GetSink);
171    impl_into_kaspad_response!(GetMempoolEntry);
172    impl_into_kaspad_response!(GetMempoolEntries);
173    impl_into_kaspad_response!(GetConnectedPeerInfo);
174    impl_into_kaspad_response!(AddPeer);
175    impl_into_kaspad_response!(SubmitTransaction);
176    impl_into_kaspad_response!(SubmitTransactionReplacement);
177    impl_into_kaspad_response!(GetSubnetwork);
178    impl_into_kaspad_response!(GetVirtualChainFromBlock);
179    impl_into_kaspad_response!(GetBlocks);
180    impl_into_kaspad_response!(GetBlockCount);
181    impl_into_kaspad_response!(GetBlockDagInfo);
182    impl_into_kaspad_response!(ResolveFinalityConflict);
183    impl_into_kaspad_response!(GetHeaders);
184    impl_into_kaspad_response!(GetUtxosByAddresses);
185    impl_into_kaspad_response!(GetBalanceByAddress);
186    impl_into_kaspad_response!(GetBalancesByAddresses);
187    impl_into_kaspad_response!(GetSinkBlueScore);
188    impl_into_kaspad_response!(Ban);
189    impl_into_kaspad_response!(Unban);
190    impl_into_kaspad_response!(EstimateNetworkHashesPerSecond);
191    impl_into_kaspad_response!(GetMempoolEntriesByAddresses);
192    impl_into_kaspad_response!(GetCoinSupply);
193    impl_into_kaspad_response!(Ping);
194    impl_into_kaspad_response!(GetMetrics);
195    impl_into_kaspad_response!(GetConnections);
196    impl_into_kaspad_response!(GetSystemInfo);
197    impl_into_kaspad_response!(GetServerInfo);
198    impl_into_kaspad_response!(GetSyncStatus);
199    impl_into_kaspad_response!(GetDaaScoreTimestampEstimate);
200    impl_into_kaspad_response!(GetFeeEstimate);
201    impl_into_kaspad_response!(GetFeeEstimateExperimental);
202    impl_into_kaspad_response!(GetCurrentBlockColor);
203
204    impl_into_kaspad_notify_response!(NotifyBlockAdded);
205    impl_into_kaspad_notify_response!(NotifyNewBlockTemplate);
206    impl_into_kaspad_notify_response!(NotifyUtxosChanged);
207    impl_into_kaspad_notify_response!(NotifyPruningPointUtxoSetOverride);
208    impl_into_kaspad_notify_response!(NotifyFinalityConflict);
209    impl_into_kaspad_notify_response!(NotifyVirtualDaaScoreChanged);
210    impl_into_kaspad_notify_response!(NotifyVirtualChainChanged);
211    impl_into_kaspad_notify_response!(NotifySinkBlueScoreChanged);
212
213    impl_into_kaspad_notify_response!(NotifyUtxosChanged, StopNotifyingUtxosChanged);
214    impl_into_kaspad_notify_response!(NotifyPruningPointUtxoSetOverride, StopNotifyingPruningPointUtxoSetOverride);
215
216    macro_rules! impl_into_kaspad_response {
217        ($name:tt) => {
218            paste::paste! {
219                impl_into_kaspad_response_ex!(kaspa_rpc_core::[<$name Response>],[<$name ResponseMessage>],[<$name Response>]);
220            }
221        };
222        ($core_name:tt, $protowire_name:tt) => {
223            paste::paste! {
224                impl_into_kaspad_response_base!(kaspa_rpc_core::[<$core_name Response>],[<$protowire_name ResponseMessage>],[<$protowire_name Response>]);
225            }
226        };
227    }
228    use impl_into_kaspad_response;
229
230    macro_rules! impl_into_kaspad_response_base {
231        ($core_struct:path, $protowire_struct:ident, $variant:ident) => {
232            // ----------------------------------------------------------------------------
233            // rpc_core to protowire
234            // ----------------------------------------------------------------------------
235
236            impl From<RpcResult<$core_struct>> for $protowire_struct {
237                fn from(item: RpcResult<$core_struct>) -> Self {
238                    item.as_ref().map_err(|x| (*x).clone()).into()
239                }
240            }
241
242            impl From<RpcError> for $protowire_struct {
243                fn from(item: RpcError) -> Self {
244                    let x: RpcResult<&$core_struct> = Err(item);
245                    x.into()
246                }
247            }
248
249            impl From<$protowire_struct> for kaspad_response::Payload {
250                fn from(item: $protowire_struct) -> Self {
251                    kaspad_response::Payload::$variant(item)
252                }
253            }
254
255            impl From<$protowire_struct> for KaspadResponse {
256                fn from(item: $protowire_struct) -> Self {
257                    Self { id: 0, payload: Some(kaspad_response::Payload::$variant(item)) }
258                }
259            }
260        };
261    }
262    use impl_into_kaspad_response_base;
263
264    macro_rules! impl_into_kaspad_response_ex {
265        ($core_struct:path, $protowire_struct:ident, $variant:ident) => {
266            // ----------------------------------------------------------------------------
267            // rpc_core to protowire
268            // ----------------------------------------------------------------------------
269
270            impl From<RpcResult<&$core_struct>> for kaspad_response::Payload {
271                fn from(item: RpcResult<&$core_struct>) -> Self {
272                    kaspad_response::Payload::$variant(item.into())
273                }
274            }
275
276            impl From<RpcResult<&$core_struct>> for KaspadResponse {
277                fn from(item: RpcResult<&$core_struct>) -> Self {
278                    Self { id: 0, payload: Some(item.into()) }
279                }
280            }
281
282            impl From<RpcResult<$core_struct>> for kaspad_response::Payload {
283                fn from(item: RpcResult<$core_struct>) -> Self {
284                    kaspad_response::Payload::$variant(item.into())
285                }
286            }
287
288            impl From<RpcResult<$core_struct>> for KaspadResponse {
289                fn from(item: RpcResult<$core_struct>) -> Self {
290                    Self { id: 0, payload: Some(item.into()) }
291                }
292            }
293
294            impl_into_kaspad_response_base!($core_struct, $protowire_struct, $variant);
295
296            // ----------------------------------------------------------------------------
297            // protowire to rpc_core
298            // ----------------------------------------------------------------------------
299
300            impl TryFrom<&kaspad_response::Payload> for $core_struct {
301                type Error = RpcError;
302                fn try_from(item: &kaspad_response::Payload) -> RpcResult<Self> {
303                    if let kaspad_response::Payload::$variant(response) = item {
304                        response.try_into()
305                    } else {
306                        Err(RpcError::MissingRpcFieldError("Payload".to_string(), stringify!($variant).to_string()))
307                    }
308                }
309            }
310
311            impl TryFrom<&KaspadResponse> for $core_struct {
312                type Error = RpcError;
313                fn try_from(item: &KaspadResponse) -> RpcResult<Self> {
314                    item.payload
315                        .as_ref()
316                        .ok_or(RpcError::MissingRpcFieldError("KaspaResponse".to_string(), "Payload".to_string()))?
317                        .try_into()
318                }
319            }
320        };
321    }
322    use impl_into_kaspad_response_ex;
323
324    macro_rules! impl_into_kaspad_notify_response {
325        ($name:tt) => {
326            impl_into_kaspad_response!($name);
327
328            paste::paste! {
329                impl_into_kaspad_notify_response_ex!(kaspa_rpc_core::[<$name Response>],[<$name ResponseMessage>]);
330            }
331        };
332        ($core_name:tt, $protowire_name:tt) => {
333            impl_into_kaspad_response!($core_name, $protowire_name);
334
335            paste::paste! {
336                impl_into_kaspad_notify_response_ex!(kaspa_rpc_core::[<$core_name Response>],[<$protowire_name ResponseMessage>]);
337            }
338        };
339    }
340    use impl_into_kaspad_notify_response;
341
342    macro_rules! impl_into_kaspad_notify_response_ex {
343        ($($core_struct:ident)::+, $protowire_struct:ident) => {
344            // ----------------------------------------------------------------------------
345            // rpc_core to protowire
346            // ----------------------------------------------------------------------------
347
348            impl<T> From<Result<(), T>> for $protowire_struct
349            where
350                T: Into<RpcError>,
351            {
352                fn from(item: Result<(), T>) -> Self {
353                    item
354                        .map(|_| $($core_struct)::+{})
355                        .map_err(|err| err.into()).into()
356                }
357            }
358
359        };
360    }
361    use impl_into_kaspad_notify_response_ex;
362}