Skip to content

Commit 59f77b2

Browse files
committed
fix(test): Implement conditional channel opening based on aliases and addresses
This commit addresses flaky test issues related to conditional channel opening between nodes, considering node aliases and listening addresses. Changes in test modules: - Add/modify helper functions to randomize channel announcement flags - Generate random node aliases based on announcement flags: * Set custom alias if announce_channel is true * Use default alias otherwise - Update channel opening logic to account for node and channel announcements
1 parent 0af5df6 commit 59f77b2

File tree

5 files changed

+419
-316
lines changed

5 files changed

+419
-316
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,7 @@ impl Node {
654654
continue;
655655
}
656656
ChannelAnnouncementStatus::Announceable => {
657+
// Broadcast node announcement.
657658
let addresses = bcast_config.listening_addresses.clone().unwrap_or(Vec::new());
658659
if let Some(node_alias) = node_alias.as_ref() {
659660
bcast_pm.broadcast_node_announcement([0; 3], node_alias.0, addresses);

tests/common/mod.rs

Lines changed: 68 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -204,24 +204,36 @@ pub(crate) fn random_listening_addresses() -> Vec<SocketAddress> {
204204
pub(crate) fn random_node_alias() -> Option<NodeAlias> {
205205
let mut rng = thread_rng();
206206
let ranged_val = rng.gen_range(0..10);
207+
208+
let alias = format!("ldk-node-{}", ranged_val);
209+
let mut bytes = [0u8; 32];
210+
bytes[..alias.as_bytes().len()].copy_from_slice(alias.as_bytes());
211+
212+
Some(NodeAlias(bytes))
213+
}
214+
215+
pub(crate) fn random_announce_channel() -> bool {
216+
let mut rng = thread_rng();
217+
let ranged_val = rng.gen_range(0..=1);
207218
match ranged_val {
208-
0 => None,
209-
val => {
210-
let alias = format!("ldk-node-{}", val);
211-
let mut bytes = [0u8; 32];
212-
bytes[..alias.as_bytes().len()].copy_from_slice(alias.as_bytes());
213-
Some(NodeAlias(bytes))
214-
},
219+
0 => false,
220+
_ => true,
215221
}
216222
}
217223

218-
pub(crate) fn random_config(anchor_channels: bool) -> Config {
224+
pub(crate) fn random_config(anchor_channels: bool, announce_channel: bool) -> Config {
219225
let mut config = Config::default();
220226

221227
if !anchor_channels {
222228
config.anchor_channels_config = None;
223229
}
224230

231+
if announce_channel {
232+
let alias = random_node_alias();
233+
println!("Setting random LDK node alias: {:?}", alias);
234+
config.node_alias = alias;
235+
}
236+
225237
config.network = Network::Regtest;
226238
config.onchain_wallet_sync_interval_secs = 100000;
227239
config.wallet_sync_interval_secs = 100000;
@@ -235,10 +247,6 @@ pub(crate) fn random_config(anchor_channels: bool) -> Config {
235247
println!("Setting random LDK listening addresses: {:?}", rand_listening_addresses);
236248
config.listening_addresses = Some(rand_listening_addresses);
237249

238-
let alias = random_node_alias();
239-
println!("Setting random LDK node alias: {:?}", alias);
240-
config.node_alias = alias;
241-
242250
config.log_level = LogLevel::Gossip;
243251

244252
config
@@ -261,14 +269,15 @@ macro_rules! setup_builder {
261269
pub(crate) use setup_builder;
262270

263271
pub(crate) fn setup_two_nodes(
264-
electrsd: &ElectrsD, allow_0conf: bool, anchor_channels: bool, anchors_trusted_no_reserve: bool,
272+
electrsd: &ElectrsD, allow_0conf: bool, anchor_channels: bool,
273+
anchors_trusted_no_reserve: bool, announce_channel: bool,
265274
) -> (TestNode, TestNode) {
266275
println!("== Node A ==");
267-
let config_a = random_config(anchor_channels);
276+
let config_a = random_config(anchor_channels, announce_channel);
268277
let node_a = setup_node(electrsd, config_a);
269278

270279
println!("\n== Node B ==");
271-
let mut config_b = random_config(anchor_channels);
280+
let mut config_b = random_config(anchor_channels, announce_channel);
272281
if allow_0conf {
273282
config_b.trusted_peers_0conf.push(node_a.node_id());
274283
}
@@ -406,15 +415,28 @@ pub(crate) fn premine_and_distribute_funds<E: ElectrumApi>(
406415
pub fn open_channel(
407416
node_a: &TestNode, node_b: &TestNode, funding_amount_sat: u64, electrsd: &ElectrsD,
408417
) {
409-
node_a
410-
.open_announced_channel(
411-
node_b.node_id(),
412-
node_b.listening_addresses().unwrap().first().unwrap().clone(),
413-
funding_amount_sat,
414-
None,
415-
None,
416-
)
417-
.unwrap();
418+
if node_a.config().node_alias.is_some() {
419+
node_a
420+
.open_announced_channel(
421+
node_b.node_id(),
422+
node_b.listening_addresses().unwrap().first().unwrap().clone(),
423+
funding_amount_sat,
424+
None,
425+
None,
426+
)
427+
.unwrap();
428+
} else {
429+
node_a
430+
.open_channel(
431+
node_b.node_id(),
432+
node_b.listening_addresses().unwrap().first().unwrap().clone(),
433+
funding_amount_sat,
434+
None,
435+
None,
436+
)
437+
.unwrap();
438+
}
439+
418440
assert!(node_a.list_peers().iter().find(|c| { c.node_id == node_b.node_id() }).is_some());
419441

420442
let funding_txo_a = expect_channel_pending_event!(node_a, node_b.node_id());
@@ -450,15 +472,28 @@ pub(crate) fn do_channel_full_cycle<E: ElectrumApi>(
450472
println!("\nA -- open_channel -> B");
451473
let funding_amount_sat = 2_080_000;
452474
let push_msat = (funding_amount_sat / 2) * 1000; // balance the channel
453-
node_a
454-
.open_channel(
455-
node_b.node_id(),
456-
node_b.listening_addresses().unwrap().first().unwrap().clone(),
457-
funding_amount_sat,
458-
Some(push_msat),
459-
None,
460-
)
461-
.unwrap();
475+
476+
if node_a.config().node_alias.is_some() {
477+
node_a
478+
.open_announced_channel(
479+
node_b.node_id(),
480+
node_b.listening_addresses().unwrap().first().unwrap().clone(),
481+
funding_amount_sat,
482+
Some(push_msat),
483+
None,
484+
)
485+
.unwrap();
486+
} else {
487+
node_a
488+
.open_channel(
489+
node_b.node_id(),
490+
node_b.listening_addresses().unwrap().first().unwrap().clone(),
491+
funding_amount_sat,
492+
Some(push_msat),
493+
None,
494+
)
495+
.unwrap();
496+
}
462497

463498
assert_eq!(node_a.list_peers().first().unwrap().node_id, node_b.node_id());
464499
assert!(node_a.list_peers().first().unwrap().is_persisted);

tests/integration_tests_cln.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
mod common;
1111

12+
use common::random_announce_channel;
1213
use ldk_node::bitcoin::secp256k1::PublicKey;
1314
use ldk_node::bitcoin::Amount;
1415
use ldk_node::lightning::ln::msgs::SocketAddress;
@@ -43,7 +44,7 @@ fn test_cln() {
4344
common::generate_blocks_and_wait(&bitcoind_client, &electrs_client, 1);
4445

4546
// Setup LDK Node
46-
let config = common::random_config(true);
47+
let config = common::random_config(true, random_announce_channel());
4748
let mut builder = Builder::from_config(config);
4849
builder.set_esplora_server("http://127.0.0.1:3002".to_string());
4950

@@ -82,8 +83,19 @@ fn test_cln() {
8283
// Open the channel
8384
let funding_amount_sat = 1_000_000;
8485

85-
node.open_channel(cln_node_id, cln_address, funding_amount_sat, Some(500_000_000), None)
86+
if node.config().node_alias.is_none() {
87+
node.open_channel(cln_node_id, cln_address, funding_amount_sat, Some(500_000_000), None)
88+
.unwrap();
89+
} else {
90+
node.open_announced_channel(
91+
cln_node_id,
92+
cln_address,
93+
funding_amount_sat,
94+
Some(500_000_000),
95+
None,
96+
)
8697
.unwrap();
98+
}
8799

88100
let funding_txo = common::expect_channel_pending_event!(node, cln_node_id);
89101
common::wait_for_tx(&electrs_client, funding_txo.txid);

0 commit comments

Comments
 (0)