From 37756867a8a13bcd46099cb738b5b759fd4db103 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Sat, 12 Jul 2025 17:17:23 +0000 Subject: [PATCH] Use channel's real funding amounts when processing RGS data Neither `channel_announcement`s nor `channel_update`s contain a channel's actual funding amount, complicating scoring as nodes may set an `htlc_maximum_msat` to something less than the funding amount. When scoring, we use `htlc_maximum_msat` anyway if we don't know the funding amount, but its not a perfect proxy. In https://github.com/lightningdevkit/rapid-gossip-sync-server/pull/102 we started including a channel's real funding amount in RGS data, and here we start parsing it and including it in our network graph. Fixes #1559 --- lightning-background-processor/src/lib.rs | 1 + lightning-rapid-gossip-sync/src/processing.rs | 34 ++++++++++++------- lightning/src/routing/gossip.rs | 6 ++-- 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/lightning-background-processor/src/lib.rs b/lightning-background-processor/src/lib.rs index 235bb39c7d4..bb8acf05004 100644 --- a/lightning-background-processor/src/lib.rs +++ b/lightning-background-processor/src/lib.rs @@ -2521,6 +2521,7 @@ mod tests { .network_graph .add_channel_from_partial_announcement( 42, + None, 53, features, $nodes[0].node.get_our_node_id().into(), diff --git a/lightning-rapid-gossip-sync/src/processing.rs b/lightning-rapid-gossip-sync/src/processing.rs index 2a4fb6de345..8319506b574 100644 --- a/lightning-rapid-gossip-sync/src/processing.rs +++ b/lightning-rapid-gossip-sync/src/processing.rs @@ -267,8 +267,26 @@ where latest_seen_timestamp ); + let mut funding_sats: Option = None; + + if version >= 2 && has_additional_data { + // forwards compatibility + let additional_data: Vec = Readable::read(read_cursor)?; + let mut cursor = &additional_data[..]; + let funding_sats_read: BigSize = Readable::read(&mut cursor)?; + funding_sats = Some(funding_sats_read.0); + if !cursor.is_empty() { + log_gossip!( + self.logger, + "Ignoring {} bytes of additional data in channel announcement", + cursor.len() + ); + } + } + let announcement_result = network_graph.add_channel_from_partial_announcement( short_channel_id, + funding_sats, backdated_timestamp as u64, features, node_id_1, @@ -286,16 +304,6 @@ where return Err(lightning_error.into()); } } - - if version >= 2 && has_additional_data { - // forwards compatibility - let additional_data: Vec = Readable::read(read_cursor)?; - log_gossip!( - self.logger, - "Ignoring {} bytes of additional data in channel announcement", - additional_data.len() - ); - } } for modification in node_modifications { @@ -664,7 +672,7 @@ mod tests { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 1, 0, 0, 1, 0, 255, 128, 0, 0, 0, 0, 0, 0, 1, 0, 147, 23, 23, 23, 23, 23, 23, + 0, 0, 0, 1, 0, 0, 1, 0, 255, 128, 0, 0, 0, 0, 0, 0, 1, 0, 147, 42, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, @@ -685,9 +693,11 @@ mod tests { "Ignoring 255 bytes of additional data in node announcement", 3, ); + // Note that our extra data is 147 bytes long, but the first byte (42) is read as the + // channel's funding amount (as a BigSize). logger.assert_log_contains( "lightning_rapid_gossip_sync::processing", - "Ignoring 147 bytes of additional data in channel announcement", + "Ignoring 146 bytes of additional data in channel announcement", 1, ); logger.assert_log_contains( diff --git a/lightning/src/routing/gossip.rs b/lightning/src/routing/gossip.rs index 7889bc6b760..a9f45f13d4b 100644 --- a/lightning/src/routing/gossip.rs +++ b/lightning/src/routing/gossip.rs @@ -2006,8 +2006,8 @@ where /// /// All other parameters as used in [`msgs::UnsignedChannelAnnouncement`] fields. pub fn add_channel_from_partial_announcement( - &self, short_channel_id: u64, timestamp: u64, features: ChannelFeatures, node_id_1: NodeId, - node_id_2: NodeId, + &self, short_channel_id: u64, capacity_sats: Option, timestamp: u64, + features: ChannelFeatures, node_id_1: NodeId, node_id_2: NodeId, ) -> Result<(), LightningError> { if node_id_1 == node_id_2 { return Err(LightningError { @@ -2022,7 +2022,7 @@ where one_to_two: None, node_two: node_id_2, two_to_one: None, - capacity_sats: None, + capacity_sats, announcement_message: None, announcement_received_time: timestamp, node_one_counter: u32::max_value(),