Skip to content

Commit c469414

Browse files
committed
feat(cbf): update broadcasting tx
- add wait time for node to connect to peers before broadcasting tx - add sync chain starting from 10 blocks below the wallet tip to ensure tx is propagated
1 parent a9fae72 commit c469414

2 files changed

Lines changed: 85 additions & 4 deletions

File tree

src/handlers.rs

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use bdk_wallet::{KeychainKind, SignOptions, Wallet};
3434
#[cfg(feature = "cbf")]
3535
use {
3636
crate::utils::BlockchainClient::KyotoClient,
37-
bdk_kyoto::{LightClient, RequesterExt, TxBroadcastPolicy::RandomPeer},
37+
bdk_kyoto::{LightClient, RequesterExt, TxBroadcast, TxBroadcastPolicy::RandomPeer},
3838
};
3939

4040
use bdk_wallet::keys::DescriptorKey::Secret;
@@ -688,7 +688,7 @@ pub(crate) async fn handle_online_wallet_subcommand(
688688
requester,
689689
log_subscriber,
690690
warning_subscriber,
691-
update_subscriber: _,
691+
mut update_subscriber,
692692
node,
693693
} = client;
694694

@@ -701,15 +701,90 @@ pub(crate) async fn handle_online_wallet_subcommand(
701701
trace_logger(log_subscriber, warning_subscriber).await
702702
});
703703

704+
// Wait for peer connections to establish
705+
const PEER_CONNECTION_DELAY: u64 = 60;
706+
tracing::info!(
707+
"Waiting {} seconds for peer connections",
708+
PEER_CONNECTION_DELAY
709+
);
710+
tokio::time::sleep(std::time::Duration::from_secs(PEER_CONNECTION_DELAY)).await;
711+
712+
tracing::info!("Proceeding with transaction broadcast");
713+
714+
// Broadcast the transaction
715+
let broadcasted_txid = tx.compute_txid();
716+
tracing::info!("Broadcasting transaction: {}", broadcasted_txid);
704717
requester
705-
.broadcast_tx(bdk_kyoto::TxBroadcast {
718+
.broadcast_tx(TxBroadcast {
706719
tx: tx.clone(),
707720
broadcast_policy: RandomPeer,
708721
})
709722
.await
710723
.map_err(|e| {
724+
tracing::error!("Failed to broadcast transaction: {}", e);
711725
Error::Generic(format!("Failed to broadcast transaction: {}", e))
712726
})?;
727+
tracing::info!("Transaction broadcasted successfully");
728+
729+
// Perform a sync to ensure transaction propagation
730+
tracing::info!("Starting sync to confirm transaction in mempool",);
731+
requester.add_revealed_scripts(&wallet).await.map_err(|e| {
732+
Error::Generic(format!("Failed to add wallet scripts: {}", e))
733+
})?;
734+
735+
let chain_tip = wallet.local_chain().tip();
736+
tracing::info!(
737+
"Starting sync to confirm transaction {} in mempool from chain tip: height={}, hash={}",
738+
broadcasted_txid,
739+
chain_tip.height(),
740+
chain_tip.hash()
741+
);
742+
743+
requester.add_revealed_scripts(&wallet).await.map_err(|e| {
744+
Error::Generic(format!("Failed to add wallet scripts: {}", e))
745+
})?;
746+
747+
let mut updates_applied = false;
748+
loop {
749+
match update_subscriber.update().await {
750+
Some(update) => {
751+
wallet.apply_update(update).map_err(|e| {
752+
Error::Generic(format!("Failed to apply update: {}", e))
753+
})?;
754+
updates_applied = true;
755+
tracing::info!(
756+
"Applied update: tx_count={}, balance={}, chain_tip={}",
757+
wallet.transactions().count(),
758+
wallet.balance().total().to_sat(),
759+
wallet.local_chain().tip().height()
760+
);
761+
break;
762+
}
763+
None => {
764+
if updates_applied {
765+
tracing::info!("No further updates available during sync");
766+
break;
767+
}
768+
tracing::debug!("No update received, waiting");
769+
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
770+
}
771+
}
772+
}
773+
774+
if !wallet
775+
.transactions()
776+
.any(|tx| tx.tx_node.txid == broadcasted_txid)
777+
{
778+
tracing::warn!(
779+
"Transaction {} not found in wallet after sync; may not be propagated",
780+
broadcasted_txid
781+
);
782+
} else {
783+
tracing::info!(
784+
"Sync completed; transaction {} likely propagated",
785+
broadcasted_txid
786+
);
787+
}
713788

714789
tx.compute_txid()
715790
}

src/utils.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,15 @@ pub(crate) fn new_blockchain_client(
194194

195195
#[cfg(feature = "cbf")]
196196
ClientType::Cbf => {
197+
// default to skipping 10 blocks away from the wallet chain tip
198+
let skip_blocks = if wallet_opts.compactfilter_opts.skip_blocks == 0 {
199+
wallet.local_chain().tip().height() - 10
200+
} else {
201+
wallet_opts.compactfilter_opts.skip_blocks
202+
};
197203
let client = LightClientBuilder::new()
198204
.connections(wallet_opts.compactfilter_opts.conn_count)
199-
.scan_after(wallet_opts.compactfilter_opts.skip_blocks)
205+
.scan_after(skip_blocks)
200206
.build(wallet)?;
201207
BlockchainClient::KyotoClient { client }
202208
}

0 commit comments

Comments
 (0)