Skip to content

Commit 0474476

Browse files
committed
Fix onchain rebalancing
69defd3 broke on-chain rebalancing by making it so we only do rebalances based on events. This works for trusted rebalances because we actually receive events. Sadly for ldk-node we don't receive on-chain events yet so we need to use our own rolled version of it, for this to work we need the loop.
1 parent e83dcca commit 0474476

3 files changed

Lines changed: 22 additions & 1 deletion

File tree

graduated-rebalancer/src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}

orange-sdk/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff 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 {

orange-sdk/tests/integration_tests.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff 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, .. } => {

0 commit comments

Comments
 (0)