Skip to content

Commit 202154d

Browse files
committed
feat: broadcast node announcement with set alias
What this commit does: Broadcasts node announcement with the user-provided alias, if set, else, uses the default [0u8;32]. Additionally, adds a random node alias generating function for use in the generation of random configuration.
1 parent 9a606e2 commit 202154d

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/lib.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,7 @@ impl Node {
603603
let bcast_logger = Arc::clone(&self.logger);
604604
let bcast_ann_timestamp = Arc::clone(&self.latest_node_announcement_broadcast_timestamp);
605605
let mut stop_bcast = self.stop_sender.subscribe();
606+
let node_alias = self.config().node_alias;
606607
runtime.spawn(async move {
607608
// We check every 30 secs whether our last broadcast is NODE_ANN_BCAST_INTERVAL away.
608609
#[cfg(not(test))]
@@ -652,7 +653,16 @@ impl Node {
652653
continue;
653654
}
654655

655-
bcast_pm.broadcast_node_announcement([0; 3], [0; 32], addresses);
656+
// Extract alias if set, else select the default
657+
let alias = if let Some(ref alias) = node_alias {
658+
let mut buf = [0_u8; 32];
659+
buf[..alias.as_bytes().len()].copy_from_slice(alias.as_bytes());
660+
buf
661+
} else {
662+
[0; 32]
663+
};
664+
665+
bcast_pm.broadcast_node_announcement([0; 3], alias, addresses);
656666

657667
let unix_time_secs_opt =
658668
SystemTime::now().duration_since(UNIX_EPOCH).ok().map(|d| d.as_secs());

tests/common/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,15 @@ pub(crate) fn random_listening_addresses() -> Vec<SocketAddress> {
193193
listening_addresses
194194
}
195195

196+
pub(crate) fn random_node_alias() -> Option<String> {
197+
let mut rng = thread_rng();
198+
let ranged_val = rng.gen_range(0..10);
199+
match ranged_val {
200+
0 => None,
201+
val => Some(format!("ldk-node-{}", val)),
202+
}
203+
}
204+
196205
pub(crate) fn random_config(anchor_channels: bool) -> Config {
197206
let mut config = Config::default();
198207

@@ -213,6 +222,10 @@ pub(crate) fn random_config(anchor_channels: bool) -> Config {
213222
println!("Setting random LDK listening addresses: {:?}", rand_listening_addresses);
214223
config.listening_addresses = Some(rand_listening_addresses);
215224

225+
let alias = random_node_alias();
226+
println!("Setting random LDK node alias: {:?}", alias);
227+
config.node_alias = alias;
228+
216229
config.log_level = LogLevel::Gossip;
217230

218231
config

0 commit comments

Comments
 (0)