From 204398e86124b4025d73d80505b9bcd3753c37cb Mon Sep 17 00:00:00 2001 From: Tiram <18632023+tiram88@users.noreply.github.com> Date: Tue, 31 Oct 2023 13:12:35 +0100 Subject: [PATCH] Implement 2 gPRC missing methods (#307) * Implement gRPC GetServerInfo method * Implement gRPC GetSyncStatus method --- rpc/core/src/error.rs | 3 ++ rpc/grpc/client/src/lib.rs | 22 +--------- rpc/grpc/core/proto/messages.proto | 4 ++ rpc/grpc/core/proto/rpc.proto | 22 +++++++++- rpc/grpc/core/src/convert/kaspad.rs | 4 ++ rpc/grpc/core/src/convert/message.rs | 40 +++++++++++++++++++ rpc/grpc/core/src/ops.rs | 2 + .../server/src/request_handler/factory.rs | 2 + 8 files changed, 78 insertions(+), 21 deletions(-) diff --git a/rpc/core/src/error.rs b/rpc/core/src/error.rs index b499e9f07..40546ef3a 100644 --- a/rpc/core/src/error.rs +++ b/rpc/core/src/error.rs @@ -26,6 +26,9 @@ pub enum RpcError { #[error("Ip address parsing error {0}")] ParseIpAddressError(#[from] AddrParseError), + #[error("Wrong rpc api version format")] + RpcApiVersionFormatError, + #[error("Invalid script class: {0}")] InvalidRpcScriptClass(String), diff --git a/rpc/grpc/client/src/lib.rs b/rpc/grpc/client/src/lib.rs index 7b631d80a..3316c07c6 100644 --- a/rpc/grpc/client/src/lib.rs +++ b/rpc/grpc/client/src/lib.rs @@ -24,7 +24,6 @@ use kaspa_notify::{ subscription::{array::ArrayBuilder, Command, Mutation, SingleSubscription}, }; use kaspa_rpc_core::{ - api::ops::RPC_API_VERSION, api::rpc::RpcApi, error::RpcError, error::RpcResult, @@ -182,26 +181,9 @@ impl RpcApi for GrpcClient { // self.inner.call(KaspadPayloadOps::SubmitBlock, request).await?.as_ref().try_into() // } - async fn get_server_info_call(&self, _request: GetServerInfoRequest) -> RpcResult { - let GetInfoResponse { server_version, is_synced, is_utxo_indexed: has_utxo_index, .. } = self.get_info().await?; - let GetBlockDagInfoResponse { virtual_daa_score, network: network_id, .. } = self.get_block_dag_info().await?; - - Ok(GetServerInfoResponse { - rpc_api_version: RPC_API_VERSION, - server_version, - network_id, - has_utxo_index, - is_synced, - virtual_daa_score, - }) - } - - async fn get_sync_status_call(&self, _request: GetSyncStatusRequest) -> RpcResult { - let GetInfoResponse { is_synced, .. } = self.get_info().await?; - Ok(GetSyncStatusResponse { is_synced }) - } - route!(ping_call, Ping); + route!(get_sync_status_call, GetSyncStatus); + route!(get_server_info_call, GetServerInfo); route!(get_metrics_call, GetMetrics); route!(submit_block_call, SubmitBlock); route!(get_block_template_call, GetBlockTemplate); diff --git a/rpc/grpc/core/proto/messages.proto b/rpc/grpc/core/proto/messages.proto index 1cdb0cacc..8521e4ff0 100644 --- a/rpc/grpc/core/proto/messages.proto +++ b/rpc/grpc/core/proto/messages.proto @@ -56,6 +56,8 @@ message KaspadRequest { GetCoinSupplyRequestMessage getCoinSupplyRequest = 1086; PingRequestMessage pingRequest = 1088; GetMetricsRequestMessage getMetricsRequest = 1090; + GetServerInfoRequestMessage getServerInfoRequest = 1092; + GetSyncStatusRequestMessage getSyncStatusRequest = 1094; } } @@ -112,6 +114,8 @@ message KaspadResponse { GetCoinSupplyResponseMessage getCoinSupplyResponse= 1087; PingResponseMessage pingResponse= 1089; GetMetricsResponseMessage getMetricsResponse= 1091; + GetServerInfoResponseMessage getServerInfoResponse = 1093; + GetSyncStatusResponseMessage getSyncStatusResponse = 1095; } } diff --git a/rpc/grpc/core/proto/rpc.proto b/rpc/grpc/core/proto/rpc.proto index e5076a53c..f87eff0ca 100644 --- a/rpc/grpc/core/proto/rpc.proto +++ b/rpc/grpc/core/proto/rpc.proto @@ -779,7 +779,6 @@ message ConsensusMetrics{ uint64 txsCounts = 5; uint64 chainBlockCounts = 6; uint64 massCounts = 7; - } message GetMetricsRequestMessage{ @@ -793,3 +792,24 @@ message GetMetricsResponseMessage{ ConsensusMetrics consensusMetrics = 12; RPCError error = 1000; } + +message GetServerInfoRequestMessage{ +} + +message GetServerInfoResponseMessage{ + repeated uint32 rpcApiVersion = 1; // Expecting exactly 4 elements + string serverVersion = 2; + string networkId = 3; + bool hasUtxoIndex = 4; + bool isSynced = 5; + uint64 virtualDaaScore = 6; + RPCError error = 1000; +} + +message GetSyncStatusRequestMessage{ +} + +message GetSyncStatusResponseMessage{ + bool isSynced = 1; + RPCError error = 1000; +} \ No newline at end of file diff --git a/rpc/grpc/core/src/convert/kaspad.rs b/rpc/grpc/core/src/convert/kaspad.rs index 3414eafce..9658f3e81 100644 --- a/rpc/grpc/core/src/convert/kaspad.rs +++ b/rpc/grpc/core/src/convert/kaspad.rs @@ -54,6 +54,8 @@ pub mod kaspad_request_convert { impl_into_kaspad_request!(GetCoinSupply); impl_into_kaspad_request!(Ping); impl_into_kaspad_request!(GetMetrics); + impl_into_kaspad_request!(GetServerInfo); + impl_into_kaspad_request!(GetSyncStatus); impl_into_kaspad_request!(NotifyBlockAdded); impl_into_kaspad_request!(NotifyNewBlockTemplate); @@ -182,6 +184,8 @@ pub mod kaspad_response_convert { impl_into_kaspad_response!(GetCoinSupply); impl_into_kaspad_response!(Ping); impl_into_kaspad_response!(GetMetrics); + impl_into_kaspad_response!(GetServerInfo); + impl_into_kaspad_response!(GetSyncStatus); impl_into_kaspad_notify_response!(NotifyBlockAdded); impl_into_kaspad_notify_response!(NotifyNewBlockTemplate); diff --git a/rpc/grpc/core/src/convert/message.rs b/rpc/grpc/core/src/convert/message.rs index 160e19467..f32e392c7 100644 --- a/rpc/grpc/core/src/convert/message.rs +++ b/rpc/grpc/core/src/convert/message.rs @@ -1,4 +1,5 @@ use crate::protowire::{self, submit_block_response_message::RejectReason}; +use kaspa_consensus_core::network::NetworkId; use kaspa_notify::subscription::Command; use kaspa_rpc_core::{ RpcContextualPeerAddress, RpcError, RpcExtraData, RpcHash, RpcIpAddress, RpcNetworkType, RpcPeerAddress, RpcResult, @@ -365,6 +366,26 @@ from!(item: RpcResult<&kaspa_rpc_core::GetMetricsResponse>, protowire::GetMetric error: None, } }); +from!(&kaspa_rpc_core::GetServerInfoRequest, protowire::GetServerInfoRequestMessage); +from!(item: RpcResult<&kaspa_rpc_core::GetServerInfoResponse>, protowire::GetServerInfoResponseMessage, { + Self { + rpc_api_version: item.rpc_api_version.iter().map(|x| *x as u32).collect(), + server_version: item.server_version.clone(), + network_id: item.network_id.to_string(), + has_utxo_index: item.has_utxo_index, + is_synced: item.is_synced, + virtual_daa_score: item.virtual_daa_score, + error: None, + } +}); + +from!(&kaspa_rpc_core::GetSyncStatusRequest, protowire::GetSyncStatusRequestMessage); +from!(item: RpcResult<&kaspa_rpc_core::GetSyncStatusResponse>, protowire::GetSyncStatusResponseMessage, { + Self { + is_synced: item.is_synced, + error: None, + } +}); from!(item: &kaspa_rpc_core::NotifyUtxosChangedRequest, protowire::NotifyUtxosChangedRequestMessage, { Self { addresses: item.addresses.iter().map(|x| x.into()).collect(), command: item.command.into() } @@ -704,6 +725,25 @@ try_from!(item: &protowire::GetMetricsResponseMessage, RpcResult, { + Self { + rpc_api_version: item.rpc_api_version.iter().map(|x| *x as u16).collect::>().as_slice().try_into().map_err(|_| RpcError::RpcApiVersionFormatError)?, + server_version: item.server_version.clone(), + network_id: NetworkId::from_str(&item.network_id)?, + has_utxo_index: item.has_utxo_index, + is_synced: item.is_synced, + virtual_daa_score: item.virtual_daa_score, + } +}); + +try_from!(&protowire::GetSyncStatusRequestMessage, kaspa_rpc_core::GetSyncStatusRequest); +try_from!(item: &protowire::GetSyncStatusResponseMessage, RpcResult, { + Self { + is_synced: item.is_synced, + } +}); + try_from!(item: &protowire::NotifyUtxosChangedRequestMessage, kaspa_rpc_core::NotifyUtxosChangedRequest, { Self { addresses: item.addresses.iter().map(|x| x.as_str().try_into()).collect::, _>>()?, diff --git a/rpc/grpc/core/src/ops.rs b/rpc/grpc/core/src/ops.rs index f5421dd66..651ba1904 100644 --- a/rpc/grpc/core/src/ops.rs +++ b/rpc/grpc/core/src/ops.rs @@ -78,6 +78,8 @@ pub enum KaspadPayloadOps { GetCoinSupply, Ping, GetMetrics, + GetServerInfo, + GetSyncStatus, // Subscription commands for starting/stopping notifications NotifyBlockAdded, diff --git a/rpc/grpc/server/src/request_handler/factory.rs b/rpc/grpc/server/src/request_handler/factory.rs index e407504d4..3c3a33a39 100644 --- a/rpc/grpc/server/src/request_handler/factory.rs +++ b/rpc/grpc/server/src/request_handler/factory.rs @@ -64,6 +64,8 @@ impl Factory { GetCoinSupply, Ping, GetMetrics, + GetServerInfo, + GetSyncStatus, NotifyBlockAdded, NotifyNewBlockTemplate, NotifyFinalityConflict,