From 020b479da228985dcf94c788b19bae78f8098418 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Sat, 24 Aug 2024 20:37:31 +0000 Subject: [PATCH] Avoid returning references in `NodeAnnouncementInfo` accessors --- lightning/src/routing/gossip.rs | 17 +++++++++-------- lightning/src/routing/router.rs | 2 +- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/lightning/src/routing/gossip.rs b/lightning/src/routing/gossip.rs index 3ca52ab94..db53f9f72 100644 --- a/lightning/src/routing/gossip.rs +++ b/lightning/src/routing/gossip.rs @@ -1252,9 +1252,10 @@ pub enum NodeAnnouncementInfo { } impl NodeAnnouncementInfo { - /// Protocol features the node announced support for - pub fn features(&self) -> &NodeFeatures { + pub fn features(&self) -> NodeFeatures { self.features_ref().clone() } + + pub(crate) fn features_ref(&self) -> &NodeFeatures { match self { NodeAnnouncementInfo::Relayed(relayed) => { &relayed.contents.features @@ -1294,7 +1295,7 @@ impl NodeAnnouncementInfo { /// Moniker assigned to the node. /// /// May be invalid or malicious (eg control chars), should not be exposed to the user. - pub fn alias(&self) -> &NodeAlias { + pub fn alias(&self) -> NodeAlias { match self { NodeAnnouncementInfo::Relayed(relayed) => { &relayed.contents.alias @@ -1302,11 +1303,11 @@ impl NodeAnnouncementInfo { NodeAnnouncementInfo::Local(local) => { &local.alias } - } + }.clone() } /// Internet-level addresses via which one can connect to the node - pub fn addresses(&self) -> &[SocketAddress] { + pub fn addresses(&self) -> Vec { match self { NodeAnnouncementInfo::Relayed(relayed) => { &relayed.contents.addresses @@ -1314,13 +1315,13 @@ impl NodeAnnouncementInfo { NodeAnnouncementInfo::Local(local) => { &local.addresses } - } + }.to_vec() } /// An initial announcement of the node /// /// Not stored if contains excess data to prevent DoS. - pub fn announcement_message(&self) -> Option<&NodeAnnouncement> { + pub fn announcement_message(&self) -> Option { match self { NodeAnnouncementInfo::Relayed(announcement) => { Some(announcement) @@ -1328,7 +1329,7 @@ impl NodeAnnouncementInfo { NodeAnnouncementInfo::Local(_) => { None } - } + }.cloned() } } diff --git a/lightning/src/routing/router.rs b/lightning/src/routing/router.rs index 47db2f3ec..da898f06e 100644 --- a/lightning/src/routing/router.rs +++ b/lightning/src/routing/router.rs @@ -2717,7 +2717,7 @@ where L::Target: Logger { } let features = if let Some(node_info) = $node.announcement_info.as_ref() { - &node_info.features() + node_info.features_ref() } else { &default_node_features }; -- 2.39.5