Set basic channel info in chanmon all at once, add a bit more info
authorMatt Corallo <git@bluematt.me>
Thu, 12 Dec 2019 19:56:28 +0000 (14:56 -0500)
committerMatt Corallo <git@bluematt.me>
Tue, 24 Dec 2019 17:14:09 +0000 (12:14 -0500)
lightning/src/ln/channel.rs
lightning/src/ln/channelmonitor.rs

index 889fec05c8f84bfb522222bfeb712bdd40cd2f7a..0fb7012cbe04cfa47d80790953ec849f2bcc1020 100644 (file)
@@ -644,11 +644,9 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                }
 
                let secp_ctx = Secp256k1::new();
-               let mut channel_monitor = ChannelMonitor::new(chan_keys.revocation_base_key(), chan_keys.delayed_payment_base_key(),
-                                                             chan_keys.htlc_base_key(), chan_keys.payment_base_key(), &keys_provider.get_shutdown_pubkey(), config.own_channel_config.our_to_self_delay,
-                                                             keys_provider.get_destination_script(), logger.clone());
-               channel_monitor.set_their_base_keys(&msg.htlc_basepoint, &msg.delayed_payment_basepoint);
-               channel_monitor.set_their_to_self_delay(msg.to_self_delay);
+               let channel_monitor = ChannelMonitor::new(chan_keys.revocation_base_key(), chan_keys.delayed_payment_base_key(),
+                                                         chan_keys.htlc_base_key(), chan_keys.payment_base_key(), &keys_provider.get_shutdown_pubkey(), config.own_channel_config.our_to_self_delay,
+                                                         keys_provider.get_destination_script(), logger.clone());
 
                let their_shutdown_scriptpubkey = if their_local_features.supports_upfront_shutdown_script() {
                        match &msg.shutdown_scriptpubkey {
@@ -748,7 +746,8 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                };
 
                let obscure_factor = chan.get_commitment_transaction_number_obscure_factor();
-               chan.channel_monitor.set_commitment_obscure_factor(obscure_factor);
+               let funding_redeemscript = chan.get_funding_redeemscript();
+               chan.channel_monitor.set_basic_channel_info(&msg.htlc_basepoint, &msg.delayed_payment_basepoint, msg.to_self_delay, funding_redeemscript, msg.funding_satoshis, obscure_factor);
 
                Ok(chan)
        }
@@ -1469,8 +1468,6 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                        }
                } else { None };
 
-               self.channel_monitor.set_their_base_keys(&msg.htlc_basepoint, &msg.delayed_payment_basepoint);
-
                self.their_dust_limit_satoshis = msg.dust_limit_satoshis;
                self.their_max_htlc_value_in_flight_msat = cmp::min(msg.max_htlc_value_in_flight_msat, self.channel_value_satoshis * 1000);
                self.their_channel_reserve_satoshis = msg.channel_reserve_satoshis;
@@ -1487,8 +1484,8 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                self.their_shutdown_scriptpubkey = their_shutdown_scriptpubkey;
 
                let obscure_factor = self.get_commitment_transaction_number_obscure_factor();
-               self.channel_monitor.set_commitment_obscure_factor(obscure_factor);
-               self.channel_monitor.set_their_to_self_delay(msg.to_self_delay);
+               let funding_redeemscript = self.get_funding_redeemscript();
+               self.channel_monitor.set_basic_channel_info(&msg.htlc_basepoint, &msg.delayed_payment_basepoint, msg.to_self_delay, funding_redeemscript, self.channel_value_satoshis, obscure_factor);
 
                self.channel_state = ChannelState::OurInitSent as u32 | ChannelState::TheirInitSent as u32;
 
index bde08bc514a816d5f52848772d9524528b87e0e6..fef352ce4d4039a15633b496c68b4a31a3379248 100644 (file)
@@ -571,6 +571,8 @@ pub struct ChannelMonitor {
        key_storage: Storage,
        their_htlc_base_key: Option<PublicKey>,
        their_delayed_payment_base_key: Option<PublicKey>,
+       funding_redeemscript: Option<Script>,
+       channel_value_satoshis: Option<u64>,
        // first is the idx of the first of the two revocation points
        their_cur_revocation_points: Option<(u64, PublicKey, Option<PublicKey>)>,
 
@@ -696,6 +698,8 @@ impl PartialEq for ChannelMonitor {
                        self.key_storage != other.key_storage ||
                        self.their_htlc_base_key != other.their_htlc_base_key ||
                        self.their_delayed_payment_base_key != other.their_delayed_payment_base_key ||
+                       self.funding_redeemscript != other.funding_redeemscript ||
+                       self.channel_value_satoshis != other.channel_value_satoshis ||
                        self.their_cur_revocation_points != other.their_cur_revocation_points ||
                        self.our_to_self_delay != other.our_to_self_delay ||
                        self.their_to_self_delay != other.their_to_self_delay ||
@@ -743,6 +747,8 @@ impl ChannelMonitor {
                        },
                        their_htlc_base_key: None,
                        their_delayed_payment_base_key: None,
+                       funding_redeemscript: None,
+                       channel_value_satoshis: None,
                        their_cur_revocation_points: None,
 
                        our_to_self_delay: our_to_self_delay,
@@ -1052,12 +1058,6 @@ impl ChannelMonitor {
                Ok(())
        }
 
-       /// Panics if commitment_transaction_number_obscure_factor doesn't fit in 48 bits
-       pub(super) fn set_commitment_obscure_factor(&mut self, commitment_transaction_number_obscure_factor: u64) {
-               assert!(commitment_transaction_number_obscure_factor < (1 << 48));
-               self.commitment_transaction_number_obscure_factor = commitment_transaction_number_obscure_factor;
-       }
-
        /// Allows this monitor to scan only for transactions which are applicable. Note that this is
        /// optional, without it this monitor cannot be used in an SPV client, but you may wish to
        /// avoid this (or call unset_funding_info) on a monitor you wish to send to a watchtower as it
@@ -1076,13 +1076,15 @@ impl ChannelMonitor {
        }
 
        /// We log these base keys at channel opening to being able to rebuild redeemscript in case of leaked revoked commit tx
-       pub(super) fn set_their_base_keys(&mut self, their_htlc_base_key: &PublicKey, their_delayed_payment_base_key: &PublicKey) {
+       /// Panics if commitment_transaction_number_obscure_factor doesn't fit in 48 bits
+       pub(super) fn set_basic_channel_info(&mut self, their_htlc_base_key: &PublicKey, their_delayed_payment_base_key: &PublicKey, their_to_self_delay: u16, funding_redeemscript: Script, channel_value_satoshis: u64, commitment_transaction_number_obscure_factor: u64) {
                self.their_htlc_base_key = Some(their_htlc_base_key.clone());
                self.their_delayed_payment_base_key = Some(their_delayed_payment_base_key.clone());
-       }
-
-       pub(super) fn set_their_to_self_delay(&mut self, their_to_self_delay: u16) {
                self.their_to_self_delay = Some(their_to_self_delay);
+               self.funding_redeemscript = Some(funding_redeemscript);
+               self.channel_value_satoshis = Some(channel_value_satoshis);
+               assert!(commitment_transaction_number_obscure_factor < (1 << 48));
+               self.commitment_transaction_number_obscure_factor = commitment_transaction_number_obscure_factor;
        }
 
        pub(super) fn unset_funding_info(&mut self) {
@@ -1175,6 +1177,8 @@ impl ChannelMonitor {
 
                writer.write_all(&self.their_htlc_base_key.as_ref().unwrap().serialize())?;
                writer.write_all(&self.their_delayed_payment_base_key.as_ref().unwrap().serialize())?;
+               self.funding_redeemscript.as_ref().unwrap().write(writer)?;
+               self.channel_value_satoshis.unwrap().write(writer)?;
 
                match self.their_cur_revocation_points {
                        Some((idx, pubkey, second_option)) => {
@@ -2994,6 +2998,8 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelM
 
                let their_htlc_base_key = Some(Readable::read(reader)?);
                let their_delayed_payment_base_key = Some(Readable::read(reader)?);
+               let funding_redeemscript = Some(Readable::read(reader)?);
+               let channel_value_satoshis = Some(Readable::read(reader)?);
 
                let their_cur_revocation_points = {
                        let first_idx = <U48 as Readable<R>>::read(reader)?.0;
@@ -3214,6 +3220,8 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelM
                        key_storage,
                        their_htlc_base_key,
                        their_delayed_payment_base_key,
+                       funding_redeemscript,
+                       channel_value_satoshis,
                        their_cur_revocation_points,
 
                        our_to_self_delay,
@@ -3695,7 +3703,7 @@ mod tests {
                // Prune with one old state and a local commitment tx holding a few overlaps with the
                // old state.
                let mut monitor = ChannelMonitor::new(&SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
-               monitor.set_their_to_self_delay(10);
+               monitor.their_to_self_delay = Some(10);
 
                monitor.provide_latest_local_commitment_tx_info(dummy_tx.clone(), dummy_keys!(), 0, preimages_to_local_htlcs!(preimages[0..10]));
                monitor.provide_latest_remote_commitment_tx_info(&dummy_tx, preimages_slice_to_htlc_outputs!(preimages[5..15]), 281474976710655, dummy_key);