Skip to content

Commit d53f48e

Browse files
author
Antoine Riard
committed
Add timer_chan_freshness_every_min
Latency/peer disconnection may trigger us to mark as disabled some of our channels. After some time, if channels are still disabled we need to broadcast ChannelUpdate to inform other network peers about the uselessness of these channels.
1 parent f564df3 commit d53f48e

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,13 @@ const ERR: () = "You need at least 32 bit pointers (well, usize, but we'll assum
318318
/// the "reorg path" (ie call block_disconnected() until you get to a common block and then call
319319
/// block_connected() to step towards your best block) upon deserialization before using the
320320
/// object!
321+
///
322+
/// Note that ChannelManager is responsible of tracking liveness of its channels and by so
323+
/// to generate ChannelUpdate messages intended to be broadcast on the gossip layer. To avoid
324+
/// spam due to quick connection/reconnection, updates should be first stagged then after a period
325+
/// of 1 min flushed to the network through call to timer_chan_freshness_every_min. You may
326+
/// delay or anticipate call to this method to suit your announcements requirements different than
327+
/// the 1 min period.
321328
pub struct ChannelManager<'a> {
322329
default_configuration: UserConfig,
323330
genesis_hash: Sha256dHash,
@@ -1487,6 +1494,30 @@ impl<'a> ChannelManager<'a> {
14871494
events.append(&mut new_events);
14881495
}
14891496

1497+
/// Latency/peer disconnection may trigger us to mark as disabled some
1498+
/// of our channels. After some time, if channels are still disabled
1499+
/// we need to broadcast ChannelUpdate to inform other network peers
1500+
/// about the uselessness of this channels.
1501+
pub fn timer_chan_freshness_every_min(&self) {
1502+
let _ = self.total_consistency_lock.read().unwrap();
1503+
let mut channel_state_lock = self.channel_state.lock().unwrap();
1504+
let channel_state = channel_state_lock.borrow_parts();
1505+
for (_, chan) in channel_state.by_id {
1506+
if chan.is_disabled_staged() && !chan.is_live() {
1507+
if let Ok(update) = self.get_channel_update(&chan) {
1508+
channel_state.pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
1509+
msg: update
1510+
});
1511+
}
1512+
chan.to_fresh();
1513+
} else if chan.is_disabled_staged() && chan.is_live() {
1514+
chan.to_fresh();
1515+
} else if chan.is_disabled_marked() {
1516+
chan.to_disabled_staged();
1517+
}
1518+
}
1519+
}
1520+
14901521
/// Indicates that the preimage for payment_hash is unknown or the received amount is incorrect
14911522
/// after a PaymentReceived event, failing the HTLC back to its origin and freeing resources
14921523
/// along the path (including in our own channel on which we received it).
@@ -2795,8 +2826,8 @@ impl<'a> ChannelMessageHandler for ChannelManager<'a> {
27952826
log_debug!(self, "Marking channels with {} disconnected and generating channel_updates", log_pubkey!(their_node_id));
27962827
channel_state.by_id.retain(|_, chan| {
27972828
if chan.get_their_node_id() == *their_node_id {
2798-
//TODO: mark channel disabled (and maybe announce such after a timeout).
27992829
let failed_adds = chan.remove_uncommitted_htlcs_and_mark_paused();
2830+
chan.to_disabled_marked();
28002831
if !failed_adds.is_empty() {
28012832
let chan_update = self.get_channel_update(&chan).map(|u| u.encode_with_len()).unwrap(); // Cannot add/recv HTLCs before we have a short_id so unwrap is safe
28022833
failed_payments.push((chan_update, failed_adds));

0 commit comments

Comments
 (0)