From b6885cc63d7edb0f0e747c6714a711bcfff130e2 Mon Sep 17 00:00:00 2001 From: thesheepcat Date: Mon, 9 Sep 2024 21:18:38 +0200 Subject: [PATCH 1/4] simple client example created --- Cargo.lock | 14 ++++ Cargo.toml | 3 +- rpc/wrpc/examples/simple_client/Cargo.toml | 23 ++++++ rpc/wrpc/examples/simple_client/src/main.rs | 87 +++++++++++++++++++++ 4 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 rpc/wrpc/examples/simple_client/Cargo.toml create mode 100644 rpc/wrpc/examples/simple_client/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index 0d4703b8ad..87e2dbc6a8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3706,6 +3706,20 @@ dependencies = [ "workflow-wasm", ] +[[package]] +name = "kaspa-wrpc-example-simple_client" +version = "0.14.1" +dependencies = [ + "futures", + "kaspa-consensus-core", + "kaspa-core", + "kaspa-rpc-core", + "kaspa-wrpc-client", + "tokio", + "workflow-core", + "workflow-log", +] + [[package]] name = "kaspa-wrpc-example-subscriber" version = "0.14.1" diff --git a/Cargo.toml b/Cargo.toml index 2789c15c1a..47d46ee227 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -42,6 +42,7 @@ members = [ "rpc/wrpc/proxy", "rpc/wrpc/wasm", "rpc/wrpc/examples/subscriber", + "rpc/wrpc/examples/simple_client", "mining", "mining/errors", "protocol/p2p", @@ -58,7 +59,7 @@ members = [ "rothschild", "metrics/core", "metrics/perf_monitor", - "utils/alloc", + "utils/alloc" ] [workspace.package] diff --git a/rpc/wrpc/examples/simple_client/Cargo.toml b/rpc/wrpc/examples/simple_client/Cargo.toml new file mode 100644 index 0000000000..aa540a7028 --- /dev/null +++ b/rpc/wrpc/examples/simple_client/Cargo.toml @@ -0,0 +1,23 @@ +[package] +name = "kaspa-wrpc-example-simple_client" +description = "Kaspa wRPC simple client example" +publish = false +rust-version.workspace = true +version.workspace = true +edition.workspace = true +authors.workspace = true +include.workspace = true +license.workspace = true +repository.workspace = true + +[dependencies] +futures.workspace = true +kaspa-consensus-core.workspace = true +kaspa-core.workspace = true +kaspa-rpc-core.workspace = true +kaspa-wrpc-client.workspace = true +tokio.workspace = true +workflow-core.workspace = true +workflow-log.workspace = true + + diff --git a/rpc/wrpc/examples/simple_client/src/main.rs b/rpc/wrpc/examples/simple_client/src/main.rs new file mode 100644 index 0000000000..9ea2a085f6 --- /dev/null +++ b/rpc/wrpc/examples/simple_client/src/main.rs @@ -0,0 +1,87 @@ +use std::time::Duration; +use kaspa_wrpc_client::{prelude::NetworkId, prelude::NetworkType, KaspaRpcClient, WrpcEncoding, result::Result, client::{ConnectOptions,ConnectStrategy}}; +use kaspa_rpc_core::{api::rpc::RpcApi, GetServerInfoResponse, GetBlockDagInfoResponse}; +use std::process::ExitCode; + +#[tokio::main] +async fn main() -> ExitCode { + match check_node_status().await { + Ok(_) => { + println!("Client connection correctly worked!"); + ExitCode::SUCCESS + } + Err(error) => { + println!("An error occurred: {error}"); + ExitCode::FAILURE + } + } +} + +async fn check_node_status() -> Result<()> { + + let encoding = WrpcEncoding::Borsh; + let url = Some("ws://192.168.1.250:17110"); + let resolver = None; + let network_id = Some(NetworkId::new(NetworkType::Mainnet)); + let subscription_context = None; + + let client = KaspaRpcClient::new(encoding, url, resolver, network_id, subscription_context)?; + + let timeout = 5_000; + let options = ConnectOptions { + block_async_connect: true, + connect_timeout: Some(Duration::from_millis(timeout)), + strategy: ConnectStrategy::Fallback, + ..Default::default() + }; + + client.connect(Some(options)).await?; + + let GetServerInfoResponse { + is_synced, + server_version, + network_id, + has_utxo_index, + .. + } = client.get_server_info().await?; + + println!("Node version: {}", server_version); + println!("Network: {}", network_id); + println!("Node is synced: {}", is_synced); + println!("Node is indexing UTXOs: {}", has_utxo_index); + + let GetBlockDagInfoResponse { + block_count, + header_count, + tip_hashes, + difficulty, + past_median_time, + virtual_parent_hashes, + pruning_point_hash, + virtual_daa_score, + sink, + .. + } = client.get_block_dag_info().await?; + + println!(""); + println!("Block count: {}", block_count); + println!("Header count: {}", header_count); + println!("Tip hash:"); + for tip_hash in tip_hashes{ + println!("{}", tip_hash); + }; + println!("Difficulty: {}", difficulty); + println!("Past median time: {}", past_median_time); + println!("Virtual parent hashes:"); + for virtual_parent_hash in virtual_parent_hashes { + println!("{}", virtual_parent_hash); + } + println!("Pruning point hash: {}", pruning_point_hash); + println!("Virtual DAA score: {}", virtual_daa_score); + println!("Sink: {}", sink); + + client.disconnect().await?; + Ok(()) + +} + From 4f2c3da477b47448ba3648b3e7ab6e5135b94fd1 Mon Sep 17 00:00:00 2001 From: thesheepcat Date: Mon, 9 Sep 2024 21:21:23 +0200 Subject: [PATCH 2/4] fine-tuning on simple wRPC client example (with comments) --- Cargo.lock | 24 ++++++-------- rpc/wrpc/examples/simple_client/Cargo.toml | 7 +--- rpc/wrpc/examples/simple_client/src/main.rs | 36 ++++++++++++++++----- 3 files changed, 39 insertions(+), 28 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 87e2dbc6a8..9525fc255e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3706,20 +3706,6 @@ dependencies = [ "workflow-wasm", ] -[[package]] -name = "kaspa-wrpc-example-simple_client" -version = "0.14.1" -dependencies = [ - "futures", - "kaspa-consensus-core", - "kaspa-core", - "kaspa-rpc-core", - "kaspa-wrpc-client", - "tokio", - "workflow-core", - "workflow-log", -] - [[package]] name = "kaspa-wrpc-example-subscriber" version = "0.14.1" @@ -3781,6 +3767,16 @@ dependencies = [ "workflow-rpc", ] +[[package]] +name = "kaspa-wrpc-simple-client-example" +version = "0.14.1" +dependencies = [ + "futures", + "kaspa-rpc-core", + "kaspa-wrpc-client", + "tokio", +] + [[package]] name = "kaspa-wrpc-wasm" version = "0.14.1" diff --git a/rpc/wrpc/examples/simple_client/Cargo.toml b/rpc/wrpc/examples/simple_client/Cargo.toml index aa540a7028..c55774b685 100644 --- a/rpc/wrpc/examples/simple_client/Cargo.toml +++ b/rpc/wrpc/examples/simple_client/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "kaspa-wrpc-example-simple_client" +name = "kaspa-wrpc-simple-client-example" description = "Kaspa wRPC simple client example" publish = false rust-version.workspace = true @@ -12,12 +12,7 @@ repository.workspace = true [dependencies] futures.workspace = true -kaspa-consensus-core.workspace = true -kaspa-core.workspace = true kaspa-rpc-core.workspace = true kaspa-wrpc-client.workspace = true tokio.workspace = true -workflow-core.workspace = true -workflow-log.workspace = true - diff --git a/rpc/wrpc/examples/simple_client/src/main.rs b/rpc/wrpc/examples/simple_client/src/main.rs index 9ea2a085f6..34f68873ee 100644 --- a/rpc/wrpc/examples/simple_client/src/main.rs +++ b/rpc/wrpc/examples/simple_client/src/main.rs @@ -1,3 +1,6 @@ +//Example of simple client to connect with Kaspa node using wRPC connection and collect some node and network basic data +//Verify your Kaspa node is runnning with --rpclisten-borsh=0.0.0.0:17110 parameter + use std::time::Duration; use kaspa_wrpc_client::{prelude::NetworkId, prelude::NetworkType, KaspaRpcClient, WrpcEncoding, result::Result, client::{ConnectOptions,ConnectStrategy}}; use kaspa_rpc_core::{api::rpc::RpcApi, GetServerInfoResponse, GetBlockDagInfoResponse}; @@ -7,7 +10,8 @@ use std::process::ExitCode; async fn main() -> ExitCode { match check_node_status().await { Ok(_) => { - println!("Client connection correctly worked!"); + println!(""); + println!("Well done! You successfully completed your first client connection to Kaspa node!"); ExitCode::SUCCESS } Err(error) => { @@ -18,15 +22,25 @@ async fn main() -> ExitCode { } async fn check_node_status() -> Result<()> { - + //Select encoding method to use, depending on your node settings let encoding = WrpcEncoding::Borsh; - let url = Some("ws://192.168.1.250:17110"); + + //Define your node address and wRPC port (see previous note) + let url = Some("ws://0.0.0.0:17110"); + + //Define the network your Kaspa node is connected to + //You can select NetworkType::Mainnet, NetworkType::Testnet, NetworkType::Devnet, NetworkType::Simnet + let network_type = NetworkType::Mainnet; + let selected_network = Some(NetworkId::new(network_type)); + + //Advanced options let resolver = None; - let network_id = Some(NetworkId::new(NetworkType::Mainnet)); let subscription_context = None; - let client = KaspaRpcClient::new(encoding, url, resolver, network_id, subscription_context)?; + //Create new wRPC client with parameters defined above + let client = KaspaRpcClient::new(encoding, url, resolver, selected_network, subscription_context)?; + //Advanced connection options let timeout = 5_000; let options = ConnectOptions { block_async_connect: true, @@ -35,8 +49,10 @@ async fn check_node_status() -> Result<()> { ..Default::default() }; + //Connect to selected Kaspa node client.connect(Some(options)).await?; + //Retrieve and show Kaspa node information let GetServerInfoResponse { is_synced, server_version, @@ -49,7 +65,9 @@ async fn check_node_status() -> Result<()> { println!("Network: {}", network_id); println!("Node is synced: {}", is_synced); println!("Node is indexing UTXOs: {}", has_utxo_index); + println!(""); + //Retrieve and show Kaspa network information let GetBlockDagInfoResponse { block_count, header_count, @@ -63,10 +81,9 @@ async fn check_node_status() -> Result<()> { .. } = client.get_block_dag_info().await?; - println!(""); println!("Block count: {}", block_count); println!("Header count: {}", header_count); - println!("Tip hash:"); + println!("Tip hashes:"); for tip_hash in tip_hashes{ println!("{}", tip_hash); }; @@ -79,8 +96,11 @@ async fn check_node_status() -> Result<()> { println!("Pruning point hash: {}", pruning_point_hash); println!("Virtual DAA score: {}", virtual_daa_score); println!("Sink: {}", sink); - + + //Disconnect client from Kaspa node client.disconnect().await?; + + //Return function result Ok(()) } From 1bffa9583da7b09d11854fb2d30ae30c3e6c9914 Mon Sep 17 00:00:00 2001 From: thesheepcat Date: Mon, 9 Sep 2024 22:31:13 +0200 Subject: [PATCH 3/4] code fixed after Aspect's suggestions --- Cargo.toml | 2 +- rpc/wrpc/examples/simple_client/src/main.rs | 101 ++++++++++---------- 2 files changed, 51 insertions(+), 52 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 47d46ee227..ba35bf9c0e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -59,7 +59,7 @@ members = [ "rothschild", "metrics/core", "metrics/perf_monitor", - "utils/alloc" + "utils/alloc", ] [workspace.package] diff --git a/rpc/wrpc/examples/simple_client/src/main.rs b/rpc/wrpc/examples/simple_client/src/main.rs index 34f68873ee..e9ac7ce042 100644 --- a/rpc/wrpc/examples/simple_client/src/main.rs +++ b/rpc/wrpc/examples/simple_client/src/main.rs @@ -1,10 +1,15 @@ -//Example of simple client to connect with Kaspa node using wRPC connection and collect some node and network basic data -//Verify your Kaspa node is runnning with --rpclisten-borsh=0.0.0.0:17110 parameter +// Example of simple client to connect with Kaspa node using wRPC connection and collect some node and network basic data -use std::time::Duration; -use kaspa_wrpc_client::{prelude::NetworkId, prelude::NetworkType, KaspaRpcClient, WrpcEncoding, result::Result, client::{ConnectOptions,ConnectStrategy}}; -use kaspa_rpc_core::{api::rpc::RpcApi, GetServerInfoResponse, GetBlockDagInfoResponse}; +use kaspa_rpc_core::{api::rpc::RpcApi, GetBlockDagInfoResponse, GetServerInfoResponse}; +use kaspa_wrpc_client::{ + client::{ConnectOptions, ConnectStrategy}, + prelude::NetworkId, + prelude::NetworkType, + result::Result, + KaspaRpcClient, Resolver, WrpcEncoding, +}; use std::process::ExitCode; +use std::time::Duration; #[tokio::main] async fn main() -> ExitCode { @@ -22,25 +27,27 @@ async fn main() -> ExitCode { } async fn check_node_status() -> Result<()> { - //Select encoding method to use, depending on your node settings + // Select encoding method to use, depending on node settings let encoding = WrpcEncoding::Borsh; - - //Define your node address and wRPC port (see previous note) - let url = Some("ws://0.0.0.0:17110"); - - //Define the network your Kaspa node is connected to - //You can select NetworkType::Mainnet, NetworkType::Testnet, NetworkType::Devnet, NetworkType::Simnet + + // If you want to connect to your own node, define your node address and wRPC port using let url = Some("ws://0.0.0.0:17110") + // Verify your Kaspa node is runnning with --rpclisten-borsh=0.0.0.0:17110 parameter + // In this example we don't use a specific node but we connect through the resolver, which use a pool of public nodes + let url = None; + let resolver = Some(Resolver::default()); + + // Define the network your Kaspa node is connected to + // You can select NetworkType::Mainnet, NetworkType::Testnet, NetworkType::Devnet, NetworkType::Simnet let network_type = NetworkType::Mainnet; let selected_network = Some(NetworkId::new(network_type)); - - //Advanced options - let resolver = None; + + // Advanced options let subscription_context = None; - - //Create new wRPC client with parameters defined above + + // Create new wRPC client with parameters defined above let client = KaspaRpcClient::new(encoding, url, resolver, selected_network, subscription_context)?; - - //Advanced connection options + + // Advanced connection options let timeout = 5_000; let options = ConnectOptions { block_async_connect: true, @@ -49,25 +56,19 @@ async fn check_node_status() -> Result<()> { ..Default::default() }; - //Connect to selected Kaspa node + // Connect to selected Kaspa node client.connect(Some(options)).await?; - //Retrieve and show Kaspa node information - let GetServerInfoResponse { - is_synced, - server_version, - network_id, - has_utxo_index, - .. - } = client.get_server_info().await?; + // Retrieve and show Kaspa node information + let GetServerInfoResponse { is_synced, server_version, network_id, has_utxo_index, .. } = client.get_server_info().await?; - println!("Node version: {}", server_version); - println!("Network: {}", network_id); - println!("Node is synced: {}", is_synced); - println!("Node is indexing UTXOs: {}", has_utxo_index); + println!("Node version: {server_version}"); + println!("Network: {network_id}"); + println!("Node is synced: {is_synced}"); + println!("Node is indexing UTXOs: {has_utxo_index}"); println!(""); - //Retrieve and show Kaspa network information + // Retrieve and show Kaspa network information let GetBlockDagInfoResponse { block_count, header_count, @@ -81,27 +82,25 @@ async fn check_node_status() -> Result<()> { .. } = client.get_block_dag_info().await?; - println!("Block count: {}", block_count); - println!("Header count: {}", header_count); + println!("Block count: {block_count}"); + println!("Header count: {header_count}"); println!("Tip hashes:"); - for tip_hash in tip_hashes{ - println!("{}", tip_hash); - }; - println!("Difficulty: {}", difficulty); - println!("Past median time: {}", past_median_time); - println!("Virtual parent hashes:"); + for tip_hash in tip_hashes { + println!("{tip_hash}"); + } + println!("Difficulty: {difficulty}"); + println!("Past median time: {past_median_time}"); + println!("Virtual parent hashes:"); for virtual_parent_hash in virtual_parent_hashes { - println!("{}", virtual_parent_hash); + println!("{virtual_parent_hash}"); } - println!("Pruning point hash: {}", pruning_point_hash); - println!("Virtual DAA score: {}", virtual_daa_score); - println!("Sink: {}", sink); - - //Disconnect client from Kaspa node + println!("Pruning point hash: {pruning_point_hash}"); + println!("Virtual DAA score: {virtual_daa_score}"); + println!("Sink: {sink}"); + + // Disconnect client from Kaspa node client.disconnect().await?; - - //Return function result - Ok(()) + // Return function result + Ok(()) } - From 75f4e917b2e2b94ff9cdabc8fa5297fb11e1f7e7 Mon Sep 17 00:00:00 2001 From: thesheepcat Date: Mon, 9 Sep 2024 23:24:58 +0200 Subject: [PATCH 4/4] empty lines cleanup --- Cargo.lock | 2 +- rpc/wrpc/examples/simple_client/src/main.rs | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 767bd2d184..63dab922e8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3714,7 +3714,7 @@ dependencies = [ [[package]] name = "kaspa-wrpc-simple-client-example" -version = "0.14.1" +version = "0.14.5" dependencies = [ "futures", "kaspa-rpc-core", diff --git a/rpc/wrpc/examples/simple_client/src/main.rs b/rpc/wrpc/examples/simple_client/src/main.rs index e9ac7ce042..0d63a24c27 100644 --- a/rpc/wrpc/examples/simple_client/src/main.rs +++ b/rpc/wrpc/examples/simple_client/src/main.rs @@ -15,7 +15,6 @@ use std::time::Duration; async fn main() -> ExitCode { match check_node_status().await { Ok(_) => { - println!(""); println!("Well done! You successfully completed your first client connection to Kaspa node!"); ExitCode::SUCCESS } @@ -66,7 +65,6 @@ async fn check_node_status() -> Result<()> { println!("Network: {network_id}"); println!("Node is synced: {is_synced}"); println!("Node is indexing UTXOs: {has_utxo_index}"); - println!(""); // Retrieve and show Kaspa network information let GetBlockDagInfoResponse {