fix serialization backwards compat 2022-02-1270-ser-backwards-compat
authorMatt Corallo <git@bluematt.me>
Fri, 4 Feb 2022 17:39:46 +0000 (17:39 +0000)
committerMatt Corallo <git@bluematt.me>
Fri, 4 Feb 2022 17:39:46 +0000 (17:39 +0000)
lightning/src/ln/channel.rs

index 72f6df1ea0b0a06f6671292715502620e798966d..82ad6c882ae70d2fb716ed94921405a3b1f4edd3 100644 (file)
@@ -39,7 +39,7 @@ use util::events::ClosureReason;
 use util::ser::{Readable, ReadableArgs, Writeable, Writer, VecWriter};
 use util::logger::Logger;
 use util::errors::APIError;
-use util::config::{UserConfig,ChannelConfig,ChannelHandshakeConfig};
+use util::config::{UserConfig,ChannelConfig};
 use util::scid_utils::scid_from_parts;
 
 use io;
@@ -5193,7 +5193,6 @@ impl<Signer: Sign> Writeable for Channel<Signer> {
                self.config.forwarding_fee_proportional_millionths.write(writer)?;
                self.config.cltv_expiry_delta.write(writer)?;
                self.config.announced_channel.write(writer)?;
-               // self.config.commit_upfront_shutdown_pubkey.write(writer)?;
                self.commit_upfront_shutdown_pubkey.write(writer)?;
 
                self.channel_id.write(writer)?;
@@ -5437,6 +5436,7 @@ impl<Signer: Sign> Writeable for Channel<Signer> {
                        (9, self.target_closing_feerate_sats_per_kw, option),
                        (11, self.monitor_pending_finalized_fulfills, vec_type),
                        (13, self.channel_creation_height, required),
+                       (15, self.commit_upfront_shutdown_pubkey, required),
                });
 
                Ok(())
@@ -5458,12 +5458,12 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<Signer>
                        config.as_mut().unwrap().forwarding_fee_proportional_millionths = Readable::read(reader)?;
                        config.as_mut().unwrap().cltv_expiry_delta = Readable::read(reader)?;
                        config.as_mut().unwrap().announced_channel = Readable::read(reader)?;
-                       // config.as_mut().unwrap().commit_upfront_shutdown_pubkey = Readable::read(reader)?;
+                       config.as_mut().unwrap().commit_upfront_shutdown_pubkey = Some(Readable::read(reader)?);
                } else {
                        // Read the 8 bytes of backwards-compatibility ChannelConfig data.
                        let mut _val: u64 = Readable::read(reader)?;
                }
-               let handshake_config = Some(ChannelHandshakeConfig::default());         let channel_id = Readable::read(reader)?;
+               let channel_id = Readable::read(reader)?;
                let channel_state = Readable::read(reader)?;
                let channel_value_satoshis = Readable::read(reader)?;
 
@@ -5677,6 +5677,7 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<Signer>
                // only, so we default to that if none was written.
                let mut channel_type = Some(ChannelTypeFeatures::only_static_remote_key());
                let mut channel_creation_height = Some(serialized_height);
+               let mut commit_upfront_shutdown_pubkey = None;
                read_tlv_fields!(reader, {
                        (0, announcement_sigs, option),
                        (1, minimum_depth, option),
@@ -5689,6 +5690,7 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<Signer>
                        (9, target_closing_feerate_sats_per_kw, option),
                        (11, monitor_pending_finalized_fulfills, vec_type),
                        (13, channel_creation_height, option),
+                       (15, commit_upfront_shutdown_pubkey, option),
                });
 
                let chan_features = channel_type.as_ref().unwrap();
@@ -5703,12 +5705,28 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<Signer>
                        return Err(DecodeError::InvalidValue);
                }
 
+               if commit_upfront_shutdown_pubkey.is_none() {
+                       // commit_upfront_shutdown_pubkey has moved around a good bit, in version 1
+                       // serialization, it was written out as a part of the explicit field list of the
+                       // `ChannelConfig`. Then, it was written out as a field in the `ChannelConfig` itself.
+                       // Now, it is written out explicitly as its own TLV (as the field has moved to
+                       // `ChannelHandshakeConfig`).
+                       // Thus, if its not in a TLV, we here pull it from the `ChannelConfig`, and if we can't
+                       // find it at all, fail.
+                       let legacy_commit_upfront_shutdown_pubkey = config.as_ref().unwrap().commit_upfront_shutdown_pubkey;
+                       if let Some(val) = legacy_commit_upfront_shutdown_pubkey {
+                               commit_upfront_shutdown_pubkey = Some(val);
+                       } else {
+                               return Err(DecodeError::InvalidValue);
+                       }
+               }
+
                let mut secp_ctx = Secp256k1::new();
                secp_ctx.seeded_randomize(&keys_source.get_secure_random_bytes());
 
                Ok(Channel {
                        user_id,
-                       commit_upfront_shutdown_pubkey: handshake_config.unwrap().commit_upfront_shutdown_pubkey,
+                       commit_upfront_shutdown_pubkey: commit_upfront_shutdown_pubkey.unwrap(),
                        config: config.unwrap(),
                        channel_id,
                        channel_state,