Skip to content

Commit e54bfe0

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 12dfc1a commit e54bfe0

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
@@ -601,6 +601,7 @@ impl Node {
601601
let bcast_logger = Arc::clone(&self.logger);
602602
let bcast_ann_timestamp = Arc::clone(&self.latest_node_announcement_broadcast_timestamp);
603603
let mut stop_bcast = self.stop_sender.subscribe();
604+
let node_alias = self.config().node_alias;
604605
runtime.spawn(async move {
605606
// We check every 30 secs whether our last broadcast is NODE_ANN_BCAST_INTERVAL away.
606607
#[cfg(not(test))]
@@ -650,7 +651,16 @@ impl Node {
650651
continue;
651652
}
652653

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

655665
let unix_time_secs_opt =
656666
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
@@ -200,6 +200,15 @@ pub(crate) fn random_listening_addresses() -> Vec<SocketAddress> {
200200
listening_addresses
201201
}
202202

203+
pub(crate) fn random_node_alias() -> Option<String> {
204+
let mut rng = thread_rng();
205+
let ranged_val = rng.gen_range(0..10);
206+
match ranged_val {
207+
0 => None,
208+
val => Some(format!("ldk-node-{}", val)),
209+
}
210+
}
211+
203212
pub(crate) fn random_config(anchor_channels: bool) -> Config {
204213
let mut config = Config::default();
205214

@@ -220,6 +229,10 @@ pub(crate) fn random_config(anchor_channels: bool) -> Config {
220229
println!("Setting random LDK listening addresses: {:?}", rand_listening_addresses);
221230
config.listening_addresses = Some(rand_listening_addresses);
222231

232+
let alias = random_node_alias();
233+
println!("Setting random LDK node alias: {:?}", alias);
234+
config.node_alias = alias;
235+
223236
config.log_level = LogLevel::Gossip;
224237

225238
config

0 commit comments

Comments
 (0)