Merge pull request #1166 from TheBlueMatt/2021-11-chan-size-scoring
[rust-lightning] / lightning / src / ln / channel.rs
index e39478bd740c063ce1f29a7dc853238ec75fcf48..a921f68b90ef3a22ddfdc1b80872e3172198eaf6 100644 (file)
@@ -32,7 +32,7 @@ use ln::chan_utils::{CounterpartyCommitmentSecrets, TxCreationKeys, HTLCOutputIn
 use ln::chan_utils;
 use chain::BestBlock;
 use chain::chaininterface::{FeeEstimator,ConfirmationTarget};
-use chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateStep, HTLC_FAIL_BACK_BUFFER};
+use chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateStep, LATENCY_GRACE_PERIOD_BLOCKS};
 use chain::transaction::{OutPoint, TransactionData};
 use chain::keysinterface::{Sign, KeysInterface};
 use util::ser::{Readable, ReadableArgs, Writeable, Writer, VecWriter};
@@ -4197,7 +4197,10 @@ impl<Signer: Sign> Channel<Signer> {
        pub fn best_block_updated<L: Deref>(&mut self, height: u32, highest_header_time: u32, logger: &L)
                        -> Result<(Option<msgs::FundingLocked>, Vec<(HTLCSource, PaymentHash)>), msgs::ErrorMessage> where L::Target: Logger {
                let mut timed_out_htlcs = Vec::new();
-               let unforwarded_htlc_cltv_limit = height + HTLC_FAIL_BACK_BUFFER;
+               // This mirrors the check in ChannelManager::decode_update_add_htlc_onion, refusing to
+               // forward an HTLC when our counterparty should almost certainly just fail it for expiring
+               // ~now.
+               let unforwarded_htlc_cltv_limit = height + LATENCY_GRACE_PERIOD_BLOCKS;
                self.holding_cell_htlc_updates.retain(|htlc_update| {
                        match htlc_update {
                                &HTLCUpdateAwaitingACK::AddHTLC { ref payment_hash, ref source, ref cltv_expiry, .. } => {
@@ -5254,6 +5257,13 @@ impl<Signer: Sign> Writeable for Channel<Signer> {
                        htlc.write(writer)?;
                }
 
+               // If the channel type is something other than only-static-remote-key, then we need to have
+               // older clients fail to deserialize this channel at all. If the type is
+               // only-static-remote-key, we simply consider it "default" and don't write the channel type
+               // out at all.
+               let chan_type = if self.channel_type != ChannelTypeFeatures::only_static_remote_key() {
+                       Some(&self.channel_type) } else { None };
+
                write_tlv_fields!(writer, {
                        (0, self.announcement_sigs, option),
                        // minimum_depth and counterparty_selected_channel_reserve_satoshis used to have a
@@ -5263,12 +5273,12 @@ impl<Signer: Sign> Writeable for Channel<Signer> {
                        // and new versions map the default values to None and allow the TLV entries here to
                        // override that.
                        (1, self.minimum_depth, option),
+                       (2, chan_type, option),
                        (3, self.counterparty_selected_channel_reserve_satoshis, option),
                        (5, self.config, required),
                        (7, self.shutdown_scriptpubkey, option),
                        (9, self.target_closing_feerate_sats_per_kw, option),
                        (11, self.monitor_pending_finalized_fulfills, vec_type),
-                       (13, self.channel_type, required),
                });
 
                Ok(())
@@ -5509,12 +5519,12 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<&'a K> for Channel<Signer>
                read_tlv_fields!(reader, {
                        (0, announcement_sigs, option),
                        (1, minimum_depth, option),
+                       (2, channel_type, option),
                        (3, counterparty_selected_channel_reserve_satoshis, option),
                        (5, config, option), // Note that if none is provided we will *not* overwrite the existing one.
                        (7, shutdown_scriptpubkey, option),
                        (9, target_closing_feerate_sats_per_kw, option),
                        (11, monitor_pending_finalized_fulfills, vec_type),
-                       (13, channel_type, option),
                });
 
                let chan_features = channel_type.as_ref().unwrap();