Skip to content

Commit c9b12e1

Browse files
authored
Merge pull request #910 from TheBlueMatt/2021-05-sort-addrs
By default sort network addrs before inclusion in node_announcements
2 parents c60543c + 37fe22f commit c9b12e1

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1743,13 +1743,17 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
17431743
/// only Tor Onion addresses.
17441744
///
17451745
/// Panics if addresses is absurdly large (more than 500).
1746-
pub fn broadcast_node_announcement(&self, rgb: [u8; 3], alias: [u8; 32], addresses: Vec<NetAddress>) {
1746+
pub fn broadcast_node_announcement(&self, rgb: [u8; 3], alias: [u8; 32], mut addresses: Vec<NetAddress>) {
17471747
let _persistence_guard = PersistenceNotifierGuard::new(&self.total_consistency_lock, &self.persistence_notifier);
17481748

17491749
if addresses.len() > 500 {
17501750
panic!("More than half the message size was taken up by public addresses!");
17511751
}
17521752

1753+
// While all existing nodes handle unsorted addresses just fine, the spec requires that
1754+
// addresses be sorted for future compatibility.
1755+
addresses.sort_by_key(|addr| addr.get_id());
1756+
17531757
let announcement = msgs::UnsignedNodeAnnouncement {
17541758
features: NodeFeatures::known(),
17551759
timestamp: self.last_node_announcement_serial.fetch_add(1, Ordering::AcqRel) as u32,

lightning/src/ln/msgs.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,17 @@ pub enum NetAddress {
390390
},
391391
}
392392
impl NetAddress {
393+
/// Gets the ID of this address type. Addresses in node_announcement messages should be sorted
394+
/// by this.
395+
pub(crate) fn get_id(&self) -> u8 {
396+
match self {
397+
&NetAddress::IPv4 {..} => { 1 },
398+
&NetAddress::IPv6 {..} => { 2 },
399+
&NetAddress::OnionV2 {..} => { 3 },
400+
&NetAddress::OnionV3 {..} => { 4 },
401+
}
402+
}
403+
393404
/// Strict byte-length of address descriptor, 1-byte type not recorded
394405
fn len(&self) -> u16 {
395406
match self {

0 commit comments

Comments
 (0)