kaspa_wrpc_server/
router.rs

1use crate::{connection::*, server::*};
2use kaspa_notify::scope::Scope;
3use kaspa_rpc_core::{api::ops::RpcApiOps, prelude::*};
4use kaspa_rpc_macros::build_wrpc_server_interface;
5use std::sync::Arc;
6use workflow_rpc::server::prelude::*;
7use workflow_serializer::prelude::*;
8
9/// A wrapper that creates an [`Interface`] instance and initializes
10/// RPC methods and notifications against this interface. The interface
11/// is later given to the RpcServer.  This wrapper exists to allow
12/// a single initialization location for both the Kaspad Server and
13/// the GRPC Proxy.
14pub struct Router {
15    pub interface: Arc<Interface<Server, Connection, RpcApiOps>>,
16    pub server_context: Server,
17}
18
19impl Router {
20    pub fn new(server_context: Server) -> Self {
21        // let router_target = server_context.router_target();
22
23        // The following macro iterates the supplied enum variants taking the variant
24        // name and creating an RPC handler using that name. For example, receiving
25        // `GetInfo` the macro will convert it to snake name for the function name
26        // as well as create `Request` and `Response` typenames and using these typenames
27        // it will create the RPC method handler.
28        // ... `GetInfo` yields: get_info_call() + GetInfoRequest + GetInfoResponse
29        #[allow(unreachable_patterns)]
30        let mut interface = build_wrpc_server_interface!(
31            server_context.clone(),
32            Server,
33            Connection,
34            RpcApiOps,
35            [
36                Ping,
37                AddPeer,
38                Ban,
39                EstimateNetworkHashesPerSecond,
40                GetBalanceByAddress,
41                GetBalancesByAddresses,
42                GetBlock,
43                GetBlockCount,
44                GetBlockDagInfo,
45                GetBlocks,
46                GetBlockTemplate,
47                GetCurrentBlockColor,
48                GetCoinSupply,
49                GetConnectedPeerInfo,
50                GetCurrentNetwork,
51                GetDaaScoreTimestampEstimate,
52                GetFeeEstimate,
53                GetFeeEstimateExperimental,
54                GetHeaders,
55                GetInfo,
56                GetInfo,
57                GetMempoolEntries,
58                GetMempoolEntriesByAddresses,
59                GetMempoolEntry,
60                GetMetrics,
61                GetConnections,
62                GetPeerAddresses,
63                GetServerInfo,
64                GetSink,
65                GetSinkBlueScore,
66                GetSubnetwork,
67                GetSyncStatus,
68                GetSystemInfo,
69                GetUtxosByAddresses,
70                GetVirtualChainFromBlock,
71                ResolveFinalityConflict,
72                Shutdown,
73                SubmitBlock,
74                SubmitTransaction,
75                SubmitTransactionReplacement,
76                Unban,
77            ]
78        );
79
80        interface.method(
81            RpcApiOps::Subscribe,
82            workflow_rpc::server::Method::new(move |manager: Server, connection: Connection, scope: Serializable<Scope>| {
83                Box::pin(async move {
84                    manager.start_notify(&connection, scope.into_inner()).await.map_err(|err| err.to_string())?;
85                    Ok(Serializable(SubscribeResponse::new(connection.id())))
86                })
87            }),
88        );
89
90        interface.method(
91            RpcApiOps::Unsubscribe,
92            workflow_rpc::server::Method::new(move |manager: Server, connection: Connection, scope: Serializable<Scope>| {
93                Box::pin(async move {
94                    manager.stop_notify(&connection, scope.into_inner()).await.unwrap_or_else(|err| {
95                        workflow_log::log_trace!("wRPC server -> error calling stop_notify(): {err}");
96                    });
97                    Ok(Serializable(UnsubscribeResponse {}))
98                })
99            }),
100        );
101
102        Router { interface: Arc::new(interface), server_context }
103    }
104}