From 06f038f36959a8148e90fff5d6e033c02da6d318 Mon Sep 17 00:00:00 2001 From: SharanRP Date: Mon, 31 Mar 2025 17:15:29 +0530 Subject: [PATCH 1/4] feat(example_wallet_rpc): enhance logging with structured JSON output --- examples/example_wallet_rpc/Cargo.toml | 5 ++ examples/example_wallet_rpc/src/main.rs | 84 ++++++++++++++++--------- 2 files changed, 58 insertions(+), 31 deletions(-) diff --git a/examples/example_wallet_rpc/Cargo.toml b/examples/example_wallet_rpc/Cargo.toml index 546bc64e..67295c65 100644 --- a/examples/example_wallet_rpc/Cargo.toml +++ b/examples/example_wallet_rpc/Cargo.toml @@ -3,6 +3,10 @@ name = "example_wallet_rpc" version = "0.1.0" edition = "2021" +[[bin]] +name = "example_wallet_rpc" +path = "src/main.rs" + # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] @@ -12,3 +16,4 @@ bdk_bitcoind_rpc = { version = "0.18" } anyhow = "1" clap = { version = "4.5.17", features = ["derive", "env"] } ctrlc = "2.0.1" +serde_json = "1.0" \ No newline at end of file diff --git a/examples/example_wallet_rpc/src/main.rs b/examples/example_wallet_rpc/src/main.rs index 2021f835..8af62928 100644 --- a/examples/example_wallet_rpc/src/main.rs +++ b/examples/example_wallet_rpc/src/main.rs @@ -9,6 +9,7 @@ use bdk_wallet::{ }; use clap::{self, Parser}; use std::{path::PathBuf, sync::mpsc::sync_channel, thread::spawn, time::Instant}; +use serde_json::json; const DB_MAGIC: &str = "bdk-rpc-wallet-example"; @@ -81,8 +82,11 @@ fn main() -> anyhow::Result<()> { let rpc_client = args.client()?; println!( - "Connected to Bitcoin Core RPC at {:?}", - rpc_client.get_blockchain_info().unwrap() + "{}", + serde_json::to_string_pretty(&json!({ + "event": "connection", + "blockchain_info": rpc_client.get_blockchain_info().unwrap() + }))? ); let start_load_wallet = Instant::now(); @@ -105,19 +109,19 @@ fn main() -> anyhow::Result<()> { .create_wallet(&mut db)?, }, }; - println!( - "Loaded wallet in {}s", - start_load_wallet.elapsed().as_secs_f32() - ); - let balance = wallet.balance(); - println!("Wallet balance before syncing: {}", balance.total()); - let wallet_tip = wallet.latest_checkpoint(); println!( - "Wallet tip: {} at height {}", - wallet_tip.hash(), - wallet_tip.height() + "{}", + serde_json::to_string_pretty(&json!({ + "event": "wallet_loaded", + "duration_seconds": start_load_wallet.elapsed().as_secs_f32(), + "initial_balance": balance.total(), + "tip": { + "hash": wallet_tip.hash().to_string(), + "height": wallet_tip.height() + } + }))? ); let (sender, receiver) = sync_channel::(21); @@ -143,7 +147,13 @@ fn main() -> anyhow::Result<()> { for emission in receiver { match emission { Emission::SigTerm => { - println!("Sigterm received, exiting..."); + println!( + "{}", + serde_json::to_string_pretty(&json!({ + "event": "sigterm", + "message": "Sigterm received, exiting..." + }))? + ); break; } Emission::Block(block_emission) => { @@ -156,8 +166,15 @@ fn main() -> anyhow::Result<()> { wallet.persist(&mut db)?; let elapsed = start_apply_block.elapsed().as_secs_f32(); println!( - "Applied block {} at height {} in {}s", - hash, height, elapsed + "{}", + serde_json::to_string_pretty(&json!({ + "event": "block_applied", + "block": { + "hash": hash.to_string(), + "height": height + }, + "duration_seconds": elapsed + }))? ); } Emission::Mempool(mempool_emission) => { @@ -165,30 +182,35 @@ fn main() -> anyhow::Result<()> { wallet.apply_unconfirmed_txs(mempool_emission); wallet.persist(&mut db)?; println!( - "Applied unconfirmed transactions in {}s", - start_apply_mempool.elapsed().as_secs_f32() + "{}", + serde_json::to_string_pretty(&json!({ + "event": "mempool_applied", + "duration_seconds": start_apply_mempool.elapsed().as_secs_f32() + }))? ); break; } } } + let wallet_tip_end = wallet.latest_checkpoint(); let balance = wallet.balance(); println!( - "Synced {} blocks in {}s", - blocks_received, - start_load_wallet.elapsed().as_secs_f32(), - ); - println!( - "Wallet tip is '{}:{}'", - wallet_tip_end.height(), - wallet_tip_end.hash() - ); - println!("Wallet balance is {}", balance.total()); - println!( - "Wallet has {} transactions and {} utxos", - wallet.transactions().count(), - wallet.list_unspent().count() + "{}", + serde_json::to_string_pretty(&json!({ + "event": "sync_complete", + "blocks_processed": blocks_received, + "total_duration_seconds": start_load_wallet.elapsed().as_secs_f32(), + "final_state": { + "tip": { + "height": wallet_tip_end.height(), + "hash": wallet_tip_end.hash().to_string() + }, + "balance": balance.total(), + "transaction_count": wallet.transactions().count(), + "utxo_count": wallet.list_unspent().count() + } + }))? ); Ok(()) From 4028fb6fbfbdf5fdcddc877668dcbfe317c0210d Mon Sep 17 00:00:00 2001 From: SharanRP Date: Mon, 31 Mar 2025 17:26:23 +0530 Subject: [PATCH 2/4] fix(example_wallet_rpc): reorder imports and clean up whitespace in main.rs --- examples/example_wallet_rpc/src/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/example_wallet_rpc/src/main.rs b/examples/example_wallet_rpc/src/main.rs index 8af62928..279dbec6 100644 --- a/examples/example_wallet_rpc/src/main.rs +++ b/examples/example_wallet_rpc/src/main.rs @@ -8,8 +8,8 @@ use bdk_wallet::{ KeychainKind, Wallet, }; use clap::{self, Parser}; -use std::{path::PathBuf, sync::mpsc::sync_channel, thread::spawn, time::Instant}; use serde_json::json; +use std::{path::PathBuf, sync::mpsc::sync_channel, thread::spawn, time::Instant}; const DB_MAGIC: &str = "bdk-rpc-wallet-example"; @@ -192,7 +192,7 @@ fn main() -> anyhow::Result<()> { } } } - + let wallet_tip_end = wallet.latest_checkpoint(); let balance = wallet.balance(); println!( From bb9f328d1120f3bf51c6f791fd528aeb83927854 Mon Sep 17 00:00:00 2001 From: SharanRP Date: Mon, 31 Mar 2025 17:38:59 +0530 Subject: [PATCH 3/4] signing the commit --- examples/example_wallet_rpc/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/example_wallet_rpc/src/main.rs b/examples/example_wallet_rpc/src/main.rs index 279dbec6..3ab18f8b 100644 --- a/examples/example_wallet_rpc/src/main.rs +++ b/examples/example_wallet_rpc/src/main.rs @@ -12,7 +12,7 @@ use serde_json::json; use std::{path::PathBuf, sync::mpsc::sync_channel, thread::spawn, time::Instant}; const DB_MAGIC: &str = "bdk-rpc-wallet-example"; - + /// Bitcoind RPC example using `bdk_wallet::Wallet`. /// /// This syncs the chain block-by-block and prints the current balance, transaction count and UTXO From 570ff59e31375be1468f2b572044aadce71800fa Mon Sep 17 00:00:00 2001 From: SharanRP Date: Wed, 2 Apr 2025 20:00:14 +0530 Subject: [PATCH 4/4] style: remove trailing whitespace in main.rs --- examples/example_wallet_rpc/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/example_wallet_rpc/src/main.rs b/examples/example_wallet_rpc/src/main.rs index 3ab18f8b..279dbec6 100644 --- a/examples/example_wallet_rpc/src/main.rs +++ b/examples/example_wallet_rpc/src/main.rs @@ -12,7 +12,7 @@ use serde_json::json; use std::{path::PathBuf, sync::mpsc::sync_channel, thread::spawn, time::Instant}; const DB_MAGIC: &str = "bdk-rpc-wallet-example"; - + /// Bitcoind RPC example using `bdk_wallet::Wallet`. /// /// This syncs the chain block-by-block and prints the current balance, transaction count and UTXO