Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send NodeAnnouncement messages on peer connected instead on startup #166

Merged
merged 1 commit into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/fiber/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub const DEFAULT_TLC_FEE_PROPORTIONAL_MILLIONTHS: u128 = 1000;
/// Whether to automatically announce the node on startup. false means not announcing.
pub const DEFAULT_AUTO_ANNOUNCE_NODE: bool = true;

/// The interval to reannounce NodeAnnouncement, in seconds. 0 means never reannounce.
/// The interval to reannounce NodeAnnouncement, in seconds.
pub const DEFAULT_ANNOUNCE_NODE_INTERVAL_SECONDS: u64 = 3600;

// See comment in `LdkConfig` for why do we need to specify both name and long,
Expand Down Expand Up @@ -149,23 +149,23 @@ pub struct FiberConfig {
)]
pub tlc_fee_proportional_millionths: Option<u128>,

/// Whether to automatically announce the node on startup. [default: false]
/// Whether to automatically announce the node on startup. [default: true]
#[arg(
name = "FIBER_AUTO_ANNOUNCE_NODE",
long = "fiber-auto-announce-node",
env,
help = "Whether to automatically announce the node on startup. [default: false]"
help = "Whether to automatically announce the node on startup. [default: true]"
)]
pub auto_announce_node: Option<bool>,

// TODO: the more sensible default value for this option is a reasonable interval like one day
// if this node has public channels, otherwise don't reannounce (or announce) at all.
/// The interval to reannounce NodeAnnouncement, in seconds. [default: 0 (never reannounce)]
/// The interval to reannounce NodeAnnouncement, in seconds. 0 means never reannounce. [default: 3600 (1 hour)]
#[arg(
name = "FIBER_ANNOUNCE_NODE_INTERVAL_SECONDS",
long = "fiber-announce-node-interval-seconds",
env,
help = "The interval to reannounce NodeAnnouncement, in seconds. [default: 0 (never reannounce)]"
help = "The interval to reannounce NodeAnnouncement, in seconds. 0 means never reannounce. [default: 3600 (1 hour)]"
)]
pub(crate) announce_node_interval_seconds: Option<u64>,
}
Expand Down Expand Up @@ -194,6 +194,9 @@ impl AnnouncedNodeName {

pub fn as_str(&self) -> &str {
let end = self.0.iter().position(|&b| b == 0).unwrap_or(self.0.len());
if end == 0 {
return "";
}
std::str::from_utf8(&self.0[..end]).expect("valid utf8 string")
}
}
Expand Down
72 changes: 40 additions & 32 deletions src/fiber/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1412,16 +1412,15 @@ where
}
NetworkActorCommand::BroadcastLocalInfo(kind) => match kind {
LocalInfoKind::NodeAnnouncement => {
if let Some(message) = state.get_node_announcement_message() {
myself
.send_message(NetworkActorMessage::new_command(
NetworkActorCommand::BroadcastMessage(
vec![],
FiberBroadcastMessage::NodeAnnouncement(message),
),
))
.expect(ASSUME_NETWORK_MYSELF_ALIVE);
}
let message = state.get_node_announcement_message();
myself
.send_message(NetworkActorMessage::new_command(
NetworkActorCommand::BroadcastMessage(
vec![],
FiberBroadcastMessage::NodeAnnouncement(message),
),
))
.expect(ASSUME_NETWORK_MYSELF_ALIVE);
}
},
NetworkActorCommand::MarkSyncingDone => {
Expand Down Expand Up @@ -1916,6 +1915,7 @@ pub struct NetworkActorState<S> {
node_name: Option<AnnouncedNodeName>,
peer_id: PeerId,
announced_addrs: Vec<Multiaddr>,
auto_announce: bool,
// We need to keep private key here in order to sign node announcement messages.
private_key: Privkey,
// This is the entropy used to generate various random values.
Expand Down Expand Up @@ -2001,10 +2001,10 @@ where
+ Sync
+ 'static,
{
pub fn get_node_announcement_message(&self) -> Option<NodeAnnouncement> {
let alias = self.node_name?;
pub fn get_node_announcement_message(&self) -> NodeAnnouncement {
let alias = self.node_name.unwrap_or_default();
let addresses = self.announced_addrs.clone();
Some(NodeAnnouncement::new(alias, addresses, &self.private_key))
NodeAnnouncement::new(alias, addresses, &self.private_key)
}

pub fn should_message_be_broadcasted(&mut self, message: &FiberBroadcastMessage) -> bool {
Expand Down Expand Up @@ -2498,15 +2498,31 @@ where
self.peer_pubkey_map
.insert(remote_peer_id.clone(), remote_pubkey);

if let Some(message) = self.get_node_announcement_message() {
self.network
.send_message(NetworkActorMessage::new_command(
NetworkActorCommand::BroadcastMessage(
vec![remote_peer_id.clone()],
FiberBroadcastMessage::NodeAnnouncement(message),
),
))
.expect(ASSUME_NETWORK_MYSELF_ALIVE);
if self.auto_announce {
let message = self.get_node_announcement_message();
debug!(
"Auto announcing our node to peer {:?} (message: {:?})",
remote_peer_id, &message
);
if let Err(e) = self
.send_message_to_session(
session.id,
FiberMessage::BroadcastMessage(FiberBroadcastMessage::NodeAnnouncement(
message,
)),
)
.await
{
error!(
"Failed to send NodeAnnouncement message to peer {:?}: {:?}",
remote_peer_id, e
);
}
} else {
debug!(
"Auto announcing is disabled, skipping node announcement to peer {:?}",
remote_peer_id
);
}

for channel_id in store.get_active_channel_ids_by_peer(remote_peer_id) {
Expand Down Expand Up @@ -3025,6 +3041,7 @@ where
node_name: config.announced_node_name,
peer_id: my_peer_id,
announced_addrs,
auto_announce: config.auto_announce_node(),
private_key,
entropy,
network: myself.clone(),
Expand Down Expand Up @@ -3070,19 +3087,10 @@ where
))?;
}

if config.auto_announce_node() {
// We have no easy way to know when the connections to peers are established
// in tentacle, so we just wait for a while.
myself.send_after(Duration::from_secs(1), || {
NetworkActorMessage::new_command(NetworkActorCommand::BroadcastLocalInfo(
LocalInfoKind::NodeAnnouncement,
))
});
}

let announce_node_interval_seconds = config.announce_node_interval_seconds();
if announce_node_interval_seconds > 0 {
myself.send_interval(Duration::from_secs(announce_node_interval_seconds), || {
debug!("Sending broadcasting node announcement command to network actor");
NetworkActorMessage::new_command(NetworkActorCommand::BroadcastLocalInfo(
LocalInfoKind::NodeAnnouncement,
))
Expand Down
Loading