Skip to content

Commit 15edfdd

Browse files
committed
Stabilize post-reorg balance checks
Wait for the replacement chain to confirm close and sweep outputs before checking final balances after the reorg. Co-Authored-By: HAL 9000
1 parent 2bddfe0 commit 15edfdd

1 file changed

Lines changed: 41 additions & 16 deletions

File tree

tests/reorg_test.rs

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
mod common;
22
use std::collections::HashMap;
3+
use std::time::Duration;
34

45
use bitcoin::Amount;
56
use ldk_node::payment::{PaymentDirection, PaymentKind};
@@ -13,6 +14,31 @@ use crate::common::{
1314
setup_node, wait_for_outpoint_spend,
1415
};
1516

17+
async fn wait_for_pending_sweep_balance<F>(node: &ldk_node::Node, mut matches_pending_balance: F)
18+
where
19+
F: FnMut(&PendingSweepBalance) -> bool,
20+
{
21+
let mut delay = Duration::from_millis(64);
22+
let mut tries = 0;
23+
loop {
24+
node.sync_wallets().unwrap();
25+
let balances = node.list_balances();
26+
if balances
27+
.pending_balances_from_channel_closures
28+
.iter()
29+
.any(|balance| matches_pending_balance(balance))
30+
{
31+
return;
32+
}
33+
assert!(tries < 20, "Unexpected balance state: {:?}", balances);
34+
tries += 1;
35+
tokio::time::sleep(delay).await;
36+
if delay.as_millis() < 512 {
37+
delay = delay.mul_f32(2.0);
38+
}
39+
}
40+
}
41+
1642
proptest! {
1743
#![proptest_config(proptest::test_runner::Config::with_cases(5))]
1844
#[test]
@@ -169,27 +195,21 @@ proptest! {
169195
node.sync_wallets().unwrap();
170196
}
171197

172-
let balances = node.list_balances();
173-
assert!(
174-
balances.pending_balances_from_channel_closures.iter().any(|b| matches!(
175-
b,
198+
wait_for_pending_sweep_balance(node, |balance| {
199+
matches!(
200+
balance,
176201
PendingSweepBalance::BroadcastAwaitingConfirmation { .. }
177202
| PendingSweepBalance::AwaitingThresholdConfirmations { .. }
178-
)),
179-
"Unexpected balance state: {:?}",
180-
balances
181-
);
203+
)
204+
})
205+
.await;
182206

183207
generate_blocks_and_wait(bitcoind, electrs, 1).await;
184208
node.sync_wallets().unwrap();
185-
let balances = node.list_balances();
186-
assert!(
187-
balances.pending_balances_from_channel_closures.iter().any(|b| {
188-
matches!(b, PendingSweepBalance::AwaitingThresholdConfirmations { .. })
189-
}),
190-
"Unexpected balance state: {:?}",
191-
balances
192-
);
209+
wait_for_pending_sweep_balance(node, |balance| {
210+
matches!(balance, PendingSweepBalance::AwaitingThresholdConfirmations { .. })
211+
})
212+
.await;
193213
}
194214
assert!(found_claimable_balance);
195215
}
@@ -200,6 +220,11 @@ proptest! {
200220
reorg!(reorg_depth);
201221
sync_wallets!();
202222

223+
// The final reorg can leave close or sweep transactions below the wallet's
224+
// trusted spendable depth even after they are confirmed on the replacement chain.
225+
generate_blocks_and_wait(bitcoind, electrs, 6).await;
226+
sync_wallets!();
227+
203228
let fee_sat = 7000;
204229
// Check balance after close channel
205230
nodes.iter().for_each(|node| {

0 commit comments

Comments
 (0)