Skip to content

Commit

Permalink
feat: broadcast node announcement with set alias
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
enigbe committed Jul 25, 2024
1 parent 9a606e2 commit 202154d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,7 @@ impl Node {
let bcast_logger = Arc::clone(&self.logger);
let bcast_ann_timestamp = Arc::clone(&self.latest_node_announcement_broadcast_timestamp);
let mut stop_bcast = self.stop_sender.subscribe();
let node_alias = self.config().node_alias;
runtime.spawn(async move {
// We check every 30 secs whether our last broadcast is NODE_ANN_BCAST_INTERVAL away.
#[cfg(not(test))]
Expand Down Expand Up @@ -652,7 +653,16 @@ impl Node {
continue;
}

bcast_pm.broadcast_node_announcement([0; 3], [0; 32], addresses);
// Extract alias if set, else select the default
let alias = if let Some(ref alias) = node_alias {
let mut buf = [0_u8; 32];
buf[..alias.as_bytes().len()].copy_from_slice(alias.as_bytes());
buf
} else {
[0; 32]
};

bcast_pm.broadcast_node_announcement([0; 3], alias, addresses);

let unix_time_secs_opt =
SystemTime::now().duration_since(UNIX_EPOCH).ok().map(|d| d.as_secs());
Expand Down
13 changes: 13 additions & 0 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,15 @@ pub(crate) fn random_listening_addresses() -> Vec<SocketAddress> {
listening_addresses
}

pub(crate) fn random_node_alias() -> Option<String> {
let mut rng = thread_rng();
let ranged_val = rng.gen_range(0..10);
match ranged_val {
0 => None,
val => Some(format!("ldk-node-{}", val)),
}
}

pub(crate) fn random_config(anchor_channels: bool) -> Config {
let mut config = Config::default();

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

let alias = random_node_alias();
println!("Setting random LDK node alias: {:?}", alias);
config.node_alias = alias;

config.log_level = LogLevel::Gossip;

config
Expand Down

0 comments on commit 202154d

Please sign in to comment.