Skip to content

Commit 6f98ece

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 6f98ece

1 file changed

Lines changed: 49 additions & 5 deletions

File tree

src/handlers.rs

Lines changed: 49 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,6 @@ 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-
511514
let txid = match client {
512515
#[cfg(feature = "electrum")]
513516
Electrum {
@@ -531,8 +534,49 @@ pub(crate) async fn handle_online_wallet_subcommand(
531534
.map_err(|e| Error::Generic(e.to_string()))?,
532535

533536
#[cfg(feature = "cbf")]
534-
KyotoClient { client: _ } => {
535-
unimplemented!()
537+
KyotoClient { client } => {
538+
let LightClient {
539+
requester,
540+
mut log_subscriber,
541+
mut info_subscriber,
542+
mut warning_subscriber,
543+
update_subscriber: _,
544+
node,
545+
} = client;
546+
547+
let subscriber = tracing_subscriber::FmtSubscriber::new();
548+
tracing::subscriber::set_global_default(subscriber)
549+
.map_err(|e| Error::Generic(format!("SetGlobalDefault error: {}", e)))?;
550+
551+
tokio::task::spawn(async move { node.run().await });
552+
tokio::task::spawn(async move {
553+
select! {
554+
log = log_subscriber.recv() => {
555+
if let Some(log) = log {
556+
tracing::info!("{log}");
557+
}
558+
},
559+
warn = warning_subscriber.recv() => {
560+
if let Some(warn) = warn {
561+
tracing::warn!("{warn}");
562+
}
563+
}
564+
}
565+
});
566+
let txid = tx.compute_txid();
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+
}
579+
txid
536580
}
537581
};
538582
Ok(json!({ "txid": txid }))
@@ -846,7 +890,7 @@ async fn respond(
846890
subcommand: WalletSubCommand::OnlineWalletSubCommand(online_subcommand),
847891
} => {
848892
let blockchain =
849-
new_blockchain_client(wallet_opts, &wallet).map_err(|e| e.to_string())?;
893+
new_blockchain_client(wallet_opts, wallet).map_err(|e| e.to_string())?;
850894
let value = handle_online_wallet_subcommand(wallet, blockchain, online_subcommand)
851895
.await
852896
.map_err(|e| e.to_string())?;

0 commit comments

Comments
 (0)