Skip to content

Commit 2bddfe0

Browse files
committed
Stabilize reorg test setup
Use available listener ports and tolerate intermediate force-close balances so the property test exercises reorg behavior instead of setup timing. Co-Authored-By: HAL 9000
1 parent 1d40ed0 commit 2bddfe0

2 files changed

Lines changed: 52 additions & 38 deletions

File tree

tests/common/mod.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ pub(crate) mod lnd;
2121
use std::collections::{HashMap, HashSet};
2222
use std::env;
2323
use std::future::Future;
24+
use std::net::TcpListener;
2425
use std::path::PathBuf;
2526
use std::str::FromStr;
26-
use std::sync::atomic::{AtomicU16, Ordering};
2727
use std::sync::Arc;
2828
use std::time::Duration;
2929

@@ -358,13 +358,14 @@ pub(crate) fn random_storage_path() -> PathBuf {
358358
temp_path
359359
}
360360

361-
static NEXT_PORT: AtomicU16 = AtomicU16::new(20000);
362-
363361
pub(crate) fn generate_listening_addresses() -> Vec<SocketAddress> {
364-
let port = NEXT_PORT.fetch_add(2, Ordering::Relaxed);
362+
let listener_a = TcpListener::bind(("127.0.0.1", 0)).expect("available listener port");
363+
let listener_b = TcpListener::bind(("127.0.0.1", 0)).expect("available listener port");
364+
let port_a = listener_a.local_addr().expect("listener address").port();
365+
let port_b = listener_b.local_addr().expect("listener address").port();
365366
vec![
366-
SocketAddress::TcpIpV4 { addr: [127, 0, 0, 1], port },
367-
SocketAddress::TcpIpV4 { addr: [127, 0, 0, 1], port: port + 1 },
367+
SocketAddress::TcpIpV4 { addr: [127, 0, 0, 1], port: port_a },
368+
SocketAddress::TcpIpV4 { addr: [127, 0, 0, 1], port: port_b },
368369
]
369370
}
370371

tests/reorg_test.rs

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -143,43 +143,56 @@ proptest! {
143143
generate_blocks_and_wait(bitcoind, electrs, 1).await;
144144
sync_wallets!();
145145

146-
if force_close {
147-
for node in &nodes {
148-
node.sync_wallets().unwrap();
149-
// If there is no more balance, there is nothing to process here.
150-
if node.list_balances().lightning_balances.len() < 1 {
151-
return;
152-
}
153-
match node.list_balances().lightning_balances[0] {
154-
LightningBalance::ClaimableAwaitingConfirmations {
155-
confirmation_height,
156-
..
157-
} => {
158-
let cur_height = node.status().current_best_block.height;
159-
let blocks_to_go = confirmation_height - cur_height;
146+
if force_close {
147+
let mut found_claimable_balance = false;
148+
for node in &nodes {
149+
node.sync_wallets().unwrap();
150+
let balances = node.list_balances();
151+
let confirmation_height = balances.lightning_balances.iter().find_map(|b| {
152+
match b {
153+
LightningBalance::ClaimableAwaitingConfirmations {
154+
confirmation_height,
155+
..
156+
} => Some(*confirmation_height),
157+
_ => None,
158+
}
159+
});
160+
let Some(confirmation_height) = confirmation_height else {
161+
continue;
162+
};
163+
found_claimable_balance = true;
164+
165+
let cur_height = node.status().current_best_block.height;
166+
let blocks_to_go = confirmation_height.saturating_sub(cur_height);
167+
if blocks_to_go > 0 {
160168
generate_blocks_and_wait(bitcoind, electrs, blocks_to_go as usize).await;
161169
node.sync_wallets().unwrap();
162-
},
163-
_ => panic!("Unexpected balance state for node_hub!"),
164-
}
165-
166-
assert!(node.list_balances().lightning_balances.len() < 2);
167-
assert!(node.list_balances().pending_balances_from_channel_closures.len() > 0);
168-
match node.list_balances().pending_balances_from_channel_closures[0] {
169-
PendingSweepBalance::BroadcastAwaitingConfirmation { .. } => {},
170-
_ => panic!("Unexpected balance state!"),
171-
}
170+
}
172171

173-
generate_blocks_and_wait(&bitcoind, electrs, 1).await;
174-
node.sync_wallets().unwrap();
175-
assert!(node.list_balances().lightning_balances.len() < 2);
176-
assert!(node.list_balances().pending_balances_from_channel_closures.len() > 0);
177-
match node.list_balances().pending_balances_from_channel_closures[0] {
178-
PendingSweepBalance::AwaitingThresholdConfirmations { .. } => {},
179-
_ => panic!("Unexpected balance state!"),
172+
let balances = node.list_balances();
173+
assert!(
174+
balances.pending_balances_from_channel_closures.iter().any(|b| matches!(
175+
b,
176+
PendingSweepBalance::BroadcastAwaitingConfirmation { .. }
177+
| PendingSweepBalance::AwaitingThresholdConfirmations { .. }
178+
)),
179+
"Unexpected balance state: {:?}",
180+
balances
181+
);
182+
183+
generate_blocks_and_wait(bitcoind, electrs, 1).await;
184+
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+
);
180193
}
194+
assert!(found_claimable_balance);
181195
}
182-
}
183196

184197
generate_blocks_and_wait(bitcoind, electrs, 6).await;
185198
sync_wallets!();

0 commit comments

Comments
 (0)