diff --git a/Cargo.lock b/Cargo.lock index 67e41478e..fcc6c35d2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2404,6 +2404,7 @@ dependencies = [ "kaspa-txscript", "kaspa-utils", "kaspa-utxoindex", + "kaspa-wrpc-core", "log", "tokio", ] @@ -2679,6 +2680,10 @@ dependencies = [ "workflow-wasm", ] +[[package]] +name = "kaspa-wrpc-core" +version = "0.1.6" + [[package]] name = "kaspa-wrpc-proxy" version = "0.1.6" @@ -2689,6 +2694,7 @@ dependencies = [ "kaspa-grpc-client", "kaspa-rpc-core", "kaspa-rpc-macros", + "kaspa-wrpc-core", "kaspa-wrpc-server", "num_cpus", "thiserror", @@ -2713,6 +2719,7 @@ dependencies = [ "kaspa-rpc-macros", "kaspa-rpc-service", "kaspa-utils", + "kaspa-wrpc-core", "log", "num_cpus", "paste", diff --git a/Cargo.toml b/Cargo.toml index 6c9699a55..7b9a13d72 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,6 +29,7 @@ members = [ "rpc/grpc/client", "rpc/grpc/server", "rpc/core", + "rpc/wrpc/core", "rpc/wrpc/server", "rpc/wrpc/client", "rpc/wrpc/proxy", @@ -103,6 +104,7 @@ kaspa-wallet-cli = { version = "0.1.6", path = "wallet/cli" } kaspa-wallet-cli-wasm = { version = "0.1.6", path = "wallet/wasm" } kaspa-wallet-core = { version = "0.1.6", path = "wallet/core" } kaspa-wasm = { version = "0.1.6", path = "wasm" } +kaspa-wrpc-core = { version = "0.1.6", path = "rpc/wrpc/core" } kaspa-wrpc-client = { version = "0.1.6", path = "rpc/wrpc/client" } kaspa-wrpc-proxy = { version = "0.1.6", path = "rpc/wrpc/proxy" } kaspa-wrpc-server = { version = "0.1.6", path = "rpc/wrpc/server" } diff --git a/kaspad/src/main.rs b/kaspad/src/main.rs index 247504ff8..29644e6f1 100644 --- a/kaspad/src/main.rs +++ b/kaspad/src/main.rs @@ -220,7 +220,8 @@ do you confirm? (answer y/n or pass --yes to the Kaspad command line to confirm let (notification_send, notification_recv) = unbounded(); let notification_root = Arc::new(ConsensusNotificationRoot::new(notification_send)); let processing_counters = Arc::new(ProcessingCounters::default()); - let wrpc_server_counters = Arc::new(WrpcServerCounters::default()); + let wrpc_borsh_counters = Arc::new(WrpcServerCounters::default()); + let wrpc_json_counters = Arc::new(WrpcServerCounters::default()); // Use `num_cpus` background threads for the consensus database as recommended by rocksdb let consensus_db_parallelism = num_cpus::get(); @@ -279,7 +280,8 @@ do you confirm? (answer y/n or pass --yes to the Kaspad command line to confirm config, core.clone(), processing_counters, - wrpc_server_counters, + wrpc_borsh_counters.clone(), + wrpc_json_counters.clone(), )); let grpc_service = Arc::new(GrpcService::new(grpc_server_addr, rpc_core_service.clone(), args.rpc_max_clients)); @@ -298,15 +300,15 @@ do you confirm? (answer y/n or pass --yes to the Kaspad command line to confirm let wrpc_service_tasks: usize = 2; // num_cpus::get() / 2; // Register wRPC servers based on command line arguments - [(args.rpclisten_borsh, WrpcEncoding::Borsh), (args.rpclisten_json, WrpcEncoding::SerdeJson)] - .iter() - .filter_map(|(listen_address, encoding)| { + [(args.rpclisten_borsh, WrpcEncoding::Borsh, wrpc_borsh_counters), (args.rpclisten_json, WrpcEncoding::SerdeJson, wrpc_json_counters)] + .into_iter() + .filter_map(|(listen_address, encoding, wrpc_server_counters)| { listen_address.as_ref().map(|listen_address| { Arc::new(WrpcService::new( wrpc_service_tasks, Some(rpc_core_service.clone()), - encoding, - wrpc_server_counters.clone(), + &encoding, + wrpc_server_counters, WrpcServerOptions { listen_address: listen_address.to_string(), // TODO: use a normalized ContextualNetAddress instead of a String verbose: args.wrpc_verbose, diff --git a/rpc/core/src/model/message.rs b/rpc/core/src/model/message.rs index 15b7588ad..4c38b9f01 100644 --- a/rpc/core/src/model/message.rs +++ b/rpc/core/src/model/message.rs @@ -686,11 +686,19 @@ pub struct GetMetricsRequest { #[derive(Default, Clone, Debug, Serialize, Deserialize, BorshSerialize, BorshDeserialize, BorshSchema)] #[serde(rename_all = "camelCase")] pub struct ProcessMetrics { - pub uptime: u64, - pub memory_used: u64, - pub storage_used: u64, - pub grpc_connections: u32, - pub wrpc_connections: u32, + // pub uptime: u64, + // pub memory_used: u64, + // pub storage_used: u64, + // pub grpc_connections: u32, + // pub wrpc_connections: u32, + + pub borsh_live_connections : u64, + pub borsh_connection_attempts : u64, + pub borsh_handshake_failures : u64, + pub json_live_connections : u64, + pub json_connection_attempts : u64, + pub json_handshake_failures : u64, + } #[derive(Default, Clone, Debug, Serialize, Deserialize, BorshSerialize, BorshDeserialize, BorshSchema)] diff --git a/rpc/service/Cargo.toml b/rpc/service/Cargo.toml index ea16e65fa..cd8c2dce3 100644 --- a/rpc/service/Cargo.toml +++ b/rpc/service/Cargo.toml @@ -24,7 +24,7 @@ kaspa-p2p-lib.workspace = true kaspa-p2p-flows.workspace = true kaspa-math.workspace = true kaspa-utxoindex.workspace = true -kaspa-wrpc-server.workspace = true +kaspa-wrpc-core.workspace = true log.workspace = true async-trait.workspace = true diff --git a/rpc/service/src/service.rs b/rpc/service/src/service.rs index 5f561548e..0c21985b0 100644 --- a/rpc/service/src/service.rs +++ b/rpc/service/src/service.rs @@ -49,7 +49,7 @@ use kaspa_txscript::{extract_script_pub_key_address, pay_to_address_script}; use kaspa_utils::{channel::Channel, triggers::SingleTrigger}; use kaspa_utxoindex::api::UtxoIndexProxy; use std::{iter::once, sync::{Arc, atomic::Ordering}, vec}; -use kaspa_wrpc_server::service::ServerCounters as WrpcServerCounters; +use kaspa_wrpc_core::ServerCounters as WrpcServerCounters; /// A service implementing the Rpc API at kaspa_rpc_core level. /// @@ -80,7 +80,8 @@ pub struct RpcCoreService { protocol_converter: Arc, core: Arc, processing_counters: Arc, - wrpc_server_counters : Arc, + wrpc_borsh_counters : Arc, + wrpc_json_counters : Arc, shutdown: SingleTrigger, } @@ -97,7 +98,8 @@ impl RpcCoreService { config: Arc, core: Arc, processing_counters : Arc, - wrpc_server_counters : Arc, + wrpc_borsh_counters : Arc, + wrpc_json_counters : Arc, ) -> Self { // Prepare consensus-notify objects let consensus_notify_channel = Channel::::default(); @@ -155,7 +157,8 @@ impl RpcCoreService { protocol_converter, core, processing_counters, - wrpc_server_counters, + wrpc_borsh_counters, + wrpc_json_counters, shutdown: SingleTrigger::default(), } } @@ -621,7 +624,14 @@ impl RpcApi for RpcCoreService { async fn get_metrics_call(&self, req: GetMetricsRequest) -> RpcResult { let process_metrics = if req.process_metrics { - Some(ProcessMetrics::default()) + Some(ProcessMetrics { + borsh_live_connections: self.wrpc_borsh_counters.live_connections.load(Ordering::Relaxed), + borsh_connection_attempts: self.wrpc_borsh_counters.connection_attempts.load(Ordering::Relaxed), + borsh_handshake_failures: self.wrpc_borsh_counters.handshake_failures.load(Ordering::Relaxed), + json_live_connections: self.wrpc_json_counters.live_connections.load(Ordering::Relaxed), + json_connection_attempts: self.wrpc_json_counters.connection_attempts.load(Ordering::Relaxed), + json_handshake_failures: self.wrpc_json_counters.handshake_failures.load(Ordering::Relaxed), + }) } else { None }; let consensus_metrics = if req.consensus_metrics { diff --git a/rpc/wrpc/core/Cargo.toml b/rpc/wrpc/core/Cargo.toml new file mode 100644 index 000000000..7e70a6373 --- /dev/null +++ b/rpc/wrpc/core/Cargo.toml @@ -0,0 +1,34 @@ +[package] +name = "kaspa-wrpc-core" +description = "Kaspa wRPC core" +version.workspace = true +edition.workspace = true +authors.workspace = true +include.workspace = true +license.workspace = true + +[lib] +crate-type = ["cdylib", "lib"] + +[dependencies] +# async-std.workspace = true +# async-trait.workspace = true +# borsh.workspace = true +# futures.workspace = true +# js-sys.workspace = true +# kaspa-addresses.workspace = true +# kaspa-notify.workspace = true +# kaspa-rpc-core.workspace = true +# kaspa-rpc-macros.workspace = true +# paste.workspace = true +# regex.workspace = true +# serde_json.workspace = true +# serde-wasm-bindgen.workspace = true +# serde.workspace = true +# thiserror.workspace = true +# wasm-bindgen-futures.workspace = true +# wasm-bindgen.workspace = true +# workflow-core.workspace = true +# workflow-log.workspace = true +# workflow-rpc.workspace = true +# workflow-wasm.workspace = true diff --git a/rpc/wrpc/core/src/lib.rs b/rpc/wrpc/core/src/lib.rs new file mode 100644 index 000000000..ea430ba45 --- /dev/null +++ b/rpc/wrpc/core/src/lib.rs @@ -0,0 +1,8 @@ +use std::sync::atomic::AtomicU64; + +#[derive(Debug, Default)] +pub struct ServerCounters { + pub live_connections : AtomicU64, + pub connection_attempts : AtomicU64, + pub handshake_failures : AtomicU64, +} diff --git a/rpc/wrpc/proxy/Cargo.toml b/rpc/wrpc/proxy/Cargo.toml index 08efa4762..dbcb6ee76 100644 --- a/rpc/wrpc/proxy/Cargo.toml +++ b/rpc/wrpc/proxy/Cargo.toml @@ -21,7 +21,7 @@ workflow-log.workspace = true async-trait.workspace = true kaspa-rpc-macros.workspace = true num_cpus.workspace = true - +kaspa-wrpc-core.workspace = true [package.metadata.emanate.build] folder = "setup" diff --git a/rpc/wrpc/proxy/src/main.rs b/rpc/wrpc/proxy/src/main.rs index 8b26230ef..9a55b5123 100644 --- a/rpc/wrpc/proxy/src/main.rs +++ b/rpc/wrpc/proxy/src/main.rs @@ -8,8 +8,9 @@ use kaspa_wrpc_server::{ connection::Connection, router::Router, server::Server, - service::{KaspaRpcHandler, Options, ServerCounters as WrpcServerCounters}, + service::{KaspaRpcHandler, Options}, }; +use kaspa_wrpc_core::ServerCounters as WrpcServerCounters; use result::Result; use std::sync::Arc; use workflow_log::*; diff --git a/rpc/wrpc/server/Cargo.toml b/rpc/wrpc/server/Cargo.toml index 404db4821..e13f4e0e9 100644 --- a/rpc/wrpc/server/Cargo.toml +++ b/rpc/wrpc/server/Cargo.toml @@ -22,6 +22,7 @@ kaspa-rpc-service.workspace = true kaspa-rpc-macros.workspace = true kaspa-grpc-client.workspace = true kaspa-utils.workspace = true +kaspa-wrpc-core.workspace = true log.workspace = true paste.workspace = true diff --git a/rpc/wrpc/server/src/service.rs b/rpc/wrpc/server/src/service.rs index 34c67e3eb..3176f5c30 100644 --- a/rpc/wrpc/server/src/service.rs +++ b/rpc/wrpc/server/src/service.rs @@ -12,7 +12,8 @@ use std::sync::Arc; use tokio::sync::oneshot::{channel as oneshot_channel, Sender as OneshotSender}; use workflow_rpc::server::prelude::*; pub use workflow_rpc::server::Encoding as WrpcEncoding; -use std::sync::atomic::{AtomicU64,Ordering}; +pub use kaspa_wrpc_core::ServerCounters; +use std::sync::atomic::Ordering; /// Options for configuring the wRPC server pub struct Options { @@ -27,13 +28,6 @@ impl Default for Options { } } -#[derive(Debug, Default)] -pub struct ServerCounters { - pub live_connections : AtomicU64, - pub connection_attempts : AtomicU64, - pub handshake_failures : AtomicU64, -} - /// ### KaspaRpcHandler ///