]> 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, 3 Sep 2024 14:00:23 +0000 (14:00 +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/offers_tests.rs
lightning/src/ln/peer_handler.rs
lightning/src/ln/priv_short_conf_tests.rs

index 641a61c31402264f8b13f9ccd8b69ed8f38f89cd..4c046c755454a986d6aef44c2108e94b8eb91e36 100644 (file)
@@ -7030,7 +7030,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;
@@ -7076,7 +7076,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 09fc1e46007e8a3aa4a91d747f7faf1e5f18c385..4333e7e50dc5c64e2453f87eca47ca7d74f74982 100644 (file)
@@ -4013,7 +4013,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 c176a8669d3cfe2ccba41b7d8bfe4c73af89d3fe..b821eb90c5916f7fe41d83c1210db1907cc0a983 100644 (file)
@@ -1165,16 +1165,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 8bbf9a0ad7df3e14caa557a5e58451bed5cd2d73..d0813bb43d9fee3f500ca4e5daca133f533cccb9 100644 (file)
@@ -126,7 +126,7 @@ fn announce_node_address<'a, 'b, 'c>(
                excess_data: Vec::new(),
        };
        let signature = node.keys_manager.sign_gossip_message(
-               UnsignedGossipMessage::NodeAnnouncement(&announcement)
+               UnsignedGossipMessage::NodeAnnouncement(announcement.clone())
        ).unwrap();
 
        let msg = NodeAnnouncement {
index 51695bba09afc01e62afac9c69c259eba591677c..b7c152b7798402c95a6b4c4a51213273c6aa7e46 100644 (file)
@@ -2659,7 +2659,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 83ff296e17727e7c7a6cfbb3031a040627a20867..3883731ba06626bb85fbd672e682e6b5311d08e2 100644 (file)
@@ -517,7 +517,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();