Skip to content

Commit c5dd087

Browse files
committed
feat(cbf): implement transaction broadcasting
The `allow(clippy...` is because the variable `txid` is immediately returned, so binding can technically be omitted. I think removing the variable assignment makes the code difficult to read, so I decided to ignore the lint. The actual implementation comes down to listening for an info message that reports the transaction was sent to a peer. For simplicity I am ignoring any wallet updates, but if the user calls the `Sync` command they can catch them.
1 parent 6ac12a1 commit c5dd087

File tree

1 file changed

+48
-5
lines changed

1 file changed

+48
-5
lines changed

src/handlers.rs

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ use std::str::FromStr;
4848

4949
#[cfg(feature = "electrum")]
5050
use crate::utils::BlockchainClient::Electrum;
51+
#[cfg(feature = "cbf")]
52+
use bdk_kyoto::{Info, LightClient};
5153
use bdk_wallet::bitcoin::base64::prelude::*;
54+
#[cfg(feature = "cbf")]
55+
use tokio::select;
5256
#[cfg(any(
5357
feature = "electrum",
5458
feature = "esplora",
@@ -423,7 +427,7 @@ pub(crate) async fn handle_online_wallet_subcommand(
423427
Ok(json!({}))
424428
}
425429
Sync => {
426-
#[cfg(any(feature = "electrum", feature = "esplora"))]
430+
#[cfg(any(feature = "electrum", feature = "esplora", feature = "cbf"))]
427431
let request = wallet
428432
.start_sync_with_revealed_spks()
429433
.inspect(|item, progress| {
@@ -507,7 +511,7 @@ pub(crate) async fn handle_online_wallet_subcommand(
507511
(Some(_), Some(_)) => panic!("Both `psbt` and `tx` options not allowed"),
508512
(None, None) => panic!("Missing `psbt` and `tx` option"),
509513
};
510-
514+
#[allow(clippy::let_unit_value)]
511515
let txid = match client {
512516
#[cfg(feature = "electrum")]
513517
Electrum {
@@ -531,8 +535,47 @@ pub(crate) async fn handle_online_wallet_subcommand(
531535
.map_err(|e| Error::Generic(e.to_string()))?,
532536

533537
#[cfg(feature = "cbf")]
534-
KyotoClient { client: _ } => {
535-
unimplemented!()
538+
KyotoClient { client } => {
539+
let LightClient {
540+
requester,
541+
mut log_subscriber,
542+
mut info_subscriber,
543+
mut warning_subscriber,
544+
update_subscriber: _,
545+
node,
546+
} = client;
547+
548+
let subscriber = tracing_subscriber::FmtSubscriber::new();
549+
tracing::subscriber::set_global_default(subscriber)
550+
.map_err(|e| Error::Generic(format!("SetGlobalDefault error: {}", e)))?;
551+
552+
tokio::task::spawn(async move { node.run().await });
553+
tokio::task::spawn(async move {
554+
select! {
555+
log = log_subscriber.recv() => {
556+
if let Some(log) = log {
557+
tracing::info!("{log}");
558+
}
559+
},
560+
warn = warning_subscriber.recv() => {
561+
if let Some(warn) = warn {
562+
tracing::warn!("{warn}");
563+
}
564+
}
565+
}
566+
});
567+
tracing::info!("Broadcastig wtxid: {}", tx.compute_wtxid());
568+
requester
569+
.broadcast_random(tx)
570+
.map_err(|e| Error::Generic(format!("{}", e)))?;
571+
tracing::info!("Waiting for peers, this may take a minute...");
572+
573+
while let Some(info) = info_subscriber.recv().await {
574+
if let Info::TxGossiped(wtxid) = info {
575+
tracing::info!("Successfully gossiped wtxid: {wtxid}");
576+
break;
577+
}
578+
}
536579
}
537580
};
538581
Ok(json!({ "txid": txid }))
@@ -846,7 +889,7 @@ async fn respond(
846889
subcommand: WalletSubCommand::OnlineWalletSubCommand(online_subcommand),
847890
} => {
848891
let blockchain =
849-
new_blockchain_client(wallet_opts, &wallet).map_err(|e| e.to_string())?;
892+
new_blockchain_client(wallet_opts, wallet).map_err(|e| e.to_string())?;
850893
let value = handle_online_wallet_subcommand(wallet, blockchain, online_subcommand)
851894
.await
852895
.map_err(|e| e.to_string())?;

0 commit comments

Comments
 (0)