File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -248,10 +248,20 @@ where
248248
249249 /// Does any rebalance if it meets the conditions of the tunables
250250 pub async fn do_rebalance_if_needed ( & self ) {
251+ self . do_trusted_rebalance_if_needed ( ) . await ;
252+
253+ self . do_onchain_rebalance_if_needed ( ) . await ;
254+ }
255+
256+ /// Does a trusted to lightning rebalance if needed
257+ pub async fn do_trusted_rebalance_if_needed ( & self ) {
251258 if let Some ( params) = self . trigger . needs_trusted_rebalance ( ) . await {
252259 self . do_trusted_rebalance ( params) . await ;
253260 }
261+ }
254262
263+ /// Does an on-chain to lightning rebalance if needed
264+ pub async fn do_onchain_rebalance_if_needed ( & self ) {
255265 if let Some ( params) = self . trigger . needs_onchain_rebalance ( ) . await {
256266 self . do_onchain_rebalance ( params) . await ;
257267 }
Original file line number Diff line number Diff line change @@ -615,6 +615,15 @@ impl Wallet {
615615 // Wait a second to get caught up, then try to rebalance.
616616 tokio:: time:: sleep ( Duration :: from_secs ( 1 ) ) . await ;
617617 rb. do_rebalance_if_needed ( ) . await ;
618+
619+ // create loop for onchain rebalancing.
620+ // we only do onchain rebalancing here as trusted rebalancing is
621+ // handled by events but we cannot do that for onchain rebalancing.
622+ // So we need to detect onchain balance changes periodically.
623+ loop {
624+ tokio:: time:: sleep ( Duration :: from_secs ( 1 ) ) . await ;
625+ rb. do_onchain_rebalance_if_needed ( ) . await ;
626+ }
618627 } ) ;
619628
620629 let inner = Arc :: new ( WalletImpl {
Original file line number Diff line number Diff line change @@ -392,6 +392,7 @@ async fn test_receive_onchain() {
392392 . unwrap ( ) ;
393393
394394 wait_for_tx ( & electrsd. client , sent_txid) . await ;
395+ println ! ( "payment sent with txid: {sent_txid}" ) ;
395396
396397 // confirm transaction
397398 generate_blocks ( & bitcoind, & electrsd, 6 ) . await ;
@@ -404,8 +405,8 @@ async fn test_receive_onchain() {
404405 } )
405406 . await ;
406407
408+ println ! ( "waiting for onchain recv event" ) ;
407409 let event = wait_next_event ( & wallet) . await ;
408-
409410 match event {
410411 Event :: OnchainPaymentReceived { txid, amount_sat, status, .. } => {
411412 assert_eq ! ( txid, sent_txid) ;
@@ -445,6 +446,7 @@ async fn test_receive_onchain() {
445446 tokio:: time:: sleep ( Duration :: from_secs ( 5 ) ) . await ; // wait for sync
446447
447448 // wait for rebalance to be initiated
449+ println ! ( "waiting for channel opened event" ) ;
448450 let event = wait_next_event ( & wallet) . await ;
449451 match event {
450452 Event :: ChannelOpened { counterparty_node_id, .. } => {
You can’t perform that action at this time.
0 commit comments