@@ -34,7 +34,7 @@ use bdk_wallet::{KeychainKind, SignOptions, Wallet};
3434#[ cfg( feature = "cbf" ) ]
3535use {
3636 crate :: utils:: BlockchainClient :: KyotoClient ,
37- bdk_kyoto:: { LightClient , RequesterExt , TxBroadcastPolicy :: RandomPeer } ,
37+ bdk_kyoto:: { LightClient , RequesterExt , TxBroadcast , TxBroadcastPolicy :: RandomPeer } ,
3838} ;
3939
4040use 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 }
0 commit comments