]> 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>
Sat, 22 Jul 2023 01:34:39 +0000 (01:34 +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 e559ac3355007808a18abe58acb562cf8e2c112f..086365bfa59bbcfe10ab079133943b4a064a961f 100644 (file)
@@ -4941,7 +4941,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                                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;
@@ -4980,7 +4980,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
                                .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()))?;
                        let our_bitcoin_sig = self.context.holder_signer.sign_channel_announcement_with_funding_key(&announcement, &self.context.secp_ctx)
                                .map_err(|_| ChannelError::Ignore("Signer rejected channel_announcement".to_owned()))?;
index cdeb2c4d16e6fdf40a2a172916e78545f2969073..03ac4ba52722e5d9958972fa5a9ce35313415a25 100644 (file)
@@ -3086,7 +3086,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 a514f4923268ce8639bafbb5065c43e6e1a7ea3d..e64d026a2aa64927f849cd432e94e8307755dd2e 100644 (file)
@@ -901,16 +901,17 @@ impl Readable for NetAddress {
 }
 
 /// 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 f8079dd027f5fbac7f87efe10824ef7319992291..feaaa8935161640d13fa36644e5ba25bdbfc0bda 100644 (file)
@@ -2445,7 +2445,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 40ab57c3e4ccac0590623fc4c34f1cb4f76b1eeb..a0cc104a5c959051aa5627fe0de1eb991358f1df 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();