]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Avoid enums containing references with lifetimes
authorMatt Corallo <git@bluematt.me>
Sun, 5 Mar 2023 20:38:42 +0000 (20:38 +0000)
committerMatt Corallo <git@bluematt.me>
Tue, 14 May 2024 20:46:40 +0000 (20:46 +0000)
Having struct fields with references to other structs is tough in
our bindings logic, but even worse if the fields are in an enum.
Its simplest to just take the clone penalty here.

lightning/src/ln/channel.rs
lightning/src/ln/channelmanager.rs
lightning/src/ln/msgs.rs
lightning/src/ln/peer_handler.rs
lightning/src/ln/priv_short_conf_tests.rs

index 880d2ae6ab1a687118bb30f9cf3f39274f8d9e9d..14d7b5ee212b0944b6e8c5138c6ebcbe42173caa 100644 (file)
@@ -6762,7 +6762,7 @@ impl<SP: Deref> Channel<SP> where
                                return None;
                        }
                };
-               let our_node_sig = match node_signer.sign_gossip_message(msgs::UnsignedGossipMessage::ChannelAnnouncement(&announcement)) {
+               let our_node_sig = match node_signer.sign_gossip_message(msgs::UnsignedGossipMessage::ChannelAnnouncement(announcement.clone())) {
                        Err(_) => {
                                log_error!(logger, "Failed to generate node signature for channel_announcement. Channel will not be announced!");
                                return None;
@@ -6808,7 +6808,7 @@ impl<SP: Deref> Channel<SP> where
                                .map_err(|_| ChannelError::Ignore("Signer failed to retrieve own public key".to_owned()))?);
                        let were_node_one = announcement.node_id_1 == our_node_key;
 
-                       let our_node_sig = node_signer.sign_gossip_message(msgs::UnsignedGossipMessage::ChannelAnnouncement(&announcement))
+                       let our_node_sig = node_signer.sign_gossip_message(msgs::UnsignedGossipMessage::ChannelAnnouncement(announcement.clone()))
                                .map_err(|_| ChannelError::Ignore("Failed to generate node signature for channel_announcement".to_owned()))?;
                        match &self.context.holder_signer {
                                ChannelSignerType::Ecdsa(ecdsa) => {
index 04acfa1a2ea601ad236569016f8830129f245278..bc418f118a96458a0257595f5451ba13f98c0e2b 100644 (file)
@@ -4071,7 +4071,7 @@ where
                // If we returned an error and the `node_signer` cannot provide a signature for whatever
                // reason`, we wouldn't be able to receive inbound payments through the corresponding
                // channel.
-               let sig = self.node_signer.sign_gossip_message(msgs::UnsignedGossipMessage::ChannelUpdate(&unsigned)).unwrap();
+               let sig = self.node_signer.sign_gossip_message(msgs::UnsignedGossipMessage::ChannelUpdate(unsigned.clone())).unwrap();
 
                Ok(msgs::ChannelUpdate {
                        signature: sig,
index 87e8a814d33c11db0abb494ff16de1c79f6dcb92..2de0397eb2f89ee7147d0cc58beed11cda5dde76 100644 (file)
@@ -1113,16 +1113,17 @@ impl FromStr for SocketAddress {
 }
 
 /// Represents the set of gossip messages that require a signature from a node's identity key.
-pub enum UnsignedGossipMessage<'a> {
+#[derive(Clone)]
+pub enum UnsignedGossipMessage {
        /// An unsigned channel announcement.
-       ChannelAnnouncement(&'a UnsignedChannelAnnouncement),
+       ChannelAnnouncement(UnsignedChannelAnnouncement),
        /// An unsigned channel update.
-       ChannelUpdate(&'a UnsignedChannelUpdate),
+       ChannelUpdate(UnsignedChannelUpdate),
        /// An unsigned node announcement.
-       NodeAnnouncement(&'a UnsignedNodeAnnouncement)
+       NodeAnnouncement(UnsignedNodeAnnouncement)
 }
 
-impl<'a> Writeable for UnsignedGossipMessage<'a> {
+impl Writeable for UnsignedGossipMessage {
        fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
                match self {
                        UnsignedGossipMessage::ChannelAnnouncement(ref msg) => msg.write(writer),
index 17e46a274b19275a3992a9141a60fe5ba53a9c18..3f20a07b3949cbc17993986c9ad64654b4654a0f 100644 (file)
@@ -2638,7 +2638,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
                        excess_data: Vec::new(),
                };
                let node_announce_sig = match self.node_signer.sign_gossip_message(
-                       msgs::UnsignedGossipMessage::NodeAnnouncement(&announcement)
+                       msgs::UnsignedGossipMessage::NodeAnnouncement(announcement.clone())
                ) {
                        Ok(sig) => sig,
                        Err(_) => {
index fba97eae382905d8d28f544b6afc65897f440ecc..d01a9574073b8d3003f4e532193d05c4fd892249 100644 (file)
@@ -516,7 +516,7 @@ fn test_scid_alias_returned() {
                fee_proportional_millionths: last_hop[0].counterparty.forwarding_info.as_ref().unwrap().fee_proportional_millionths,
                excess_data: Vec::new(),
        };
-       let signature = nodes[1].keys_manager.sign_gossip_message(msgs::UnsignedGossipMessage::ChannelUpdate(&contents)).unwrap();
+       let signature = nodes[1].keys_manager.sign_gossip_message(msgs::UnsignedGossipMessage::ChannelUpdate(contents.clone())).unwrap();
        let msg = msgs::ChannelUpdate { signature, contents };
 
        let mut err_data = Vec::new();