]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Merge pull request #284 from TheBlueMatt/2019-01-remote-htlc-timeout-broadcast
authorMatt Corallo <649246+TheBlueMatt@users.noreply.github.com>
Sun, 13 Jan 2019 18:55:26 +0000 (13:55 -0500)
committerGitHub <noreply@github.com>
Sun, 13 Jan 2019 18:55:26 +0000 (13:55 -0500)
Check for timing-out HTLCs in remote unrevoked commitments

src/ln/chan_utils.rs
src/ln/channel.rs
src/ln/channelmonitor.rs
src/ln/functional_tests.rs

index 2efa9ff5cc3f27a48ce4582882d4b63e44d5c32f..dabc0f4f47b943d60d277524ff5b1bc2276a3f1b 100644 (file)
@@ -147,7 +147,7 @@ pub struct HTLCOutputInCommitment {
        pub amount_msat: u64,
        pub cltv_expiry: u32,
        pub payment_hash: PaymentHash,
-       pub transaction_output_index: u32,
+       pub transaction_output_index: Option<u32>,
 }
 
 #[inline]
@@ -222,12 +222,13 @@ pub fn get_htlc_redeemscript(htlc: &HTLCOutputInCommitment, keys: &TxCreationKey
        get_htlc_redeemscript_with_explicit_keys(htlc, &keys.a_htlc_key, &keys.b_htlc_key, &keys.revocation_key)
 }
 
+/// panics if htlc.transaction_output_index.is_none()!
 pub fn build_htlc_transaction(prev_hash: &Sha256dHash, feerate_per_kw: u64, to_self_delay: u16, htlc: &HTLCOutputInCommitment, a_delayed_payment_key: &PublicKey, revocation_key: &PublicKey) -> Transaction {
        let mut txins: Vec<TxIn> = Vec::new();
        txins.push(TxIn {
                previous_output: OutPoint {
                        txid: prev_hash.clone(),
-                       vout: htlc.transaction_output_index,
+                       vout: htlc.transaction_output_index.expect("Can't build an HTLC transaction for a dust output"),
                },
                script_sig: Script::new(),
                sequence: 0,
index ddfb809ccdeaf6f7b542c0c08e2850f7646a80cf..237ccd840f561a86d89d915bf2c4c635157f37b7 100644 (file)
@@ -132,18 +132,6 @@ struct OutboundHTLCOutput {
        fail_reason: Option<HTLCFailReason>,
 }
 
-macro_rules! get_htlc_in_commitment {
-       ($htlc: expr, $offered: expr) => {
-               HTLCOutputInCommitment {
-                       offered: $offered,
-                       amount_msat: $htlc.amount_msat,
-                       cltv_expiry: $htlc.cltv_expiry,
-                       payment_hash: $htlc.payment_hash,
-                       transaction_output_index: 0
-               }
-       }
-}
-
 /// See AwaitingRemoteRevoke ChannelState for more info
 enum HTLCUpdateAwaitingACK {
        AddHTLC {
@@ -759,8 +747,12 @@ impl Channel {
        /// have not yet committed it. Such HTLCs will only be included in transactions which are being
        /// generated by the peer which proposed adding the HTLCs, and thus we need to understand both
        /// which peer generated this transaction and "to whom" this transaction flows.
+       /// Returns (the transaction built, the number of HTLC outputs which were present in the
+       /// transaction, the list of HTLCs which were not ignored when building the transaction).
+       /// Note that below-dust HTLCs are included in the third return value, but not the second, and
+       /// sources are provided only for outbound HTLCs in the third return value.
        #[inline]
-       fn build_commitment_transaction(&self, commitment_number: u64, keys: &TxCreationKeys, local: bool, generated_by_local: bool, feerate_per_kw: u64) -> (Transaction, Vec<HTLCOutputInCommitment>, Vec<(PaymentHash, &HTLCSource, Option<u32>)>) {
+       fn build_commitment_transaction(&self, commitment_number: u64, keys: &TxCreationKeys, local: bool, generated_by_local: bool, feerate_per_kw: u64) -> (Transaction, usize, Vec<(HTLCOutputInCommitment, Option<&HTLCSource>)>) {
                let obscured_commitment_transaction_number = self.get_commitment_transaction_number_obscure_factor() ^ (INITIAL_COMMITMENT_NUMBER - commitment_number);
 
                let txins = {
@@ -775,38 +767,46 @@ impl Channel {
                };
 
                let mut txouts: Vec<(TxOut, Option<(HTLCOutputInCommitment, Option<&HTLCSource>)>)> = Vec::with_capacity(self.pending_inbound_htlcs.len() + self.pending_outbound_htlcs.len() + 2);
-               let mut unincluded_htlc_sources: Vec<(PaymentHash, &HTLCSource, Option<u32>)> = Vec::new();
+               let mut included_dust_htlcs: Vec<(HTLCOutputInCommitment, Option<&HTLCSource>)> = Vec::new();
 
                let dust_limit_satoshis = if local { self.our_dust_limit_satoshis } else { self.their_dust_limit_satoshis };
                let mut remote_htlc_total_msat = 0;
                let mut local_htlc_total_msat = 0;
                let mut value_to_self_msat_offset = 0;
 
+               macro_rules! get_htlc_in_commitment {
+                       ($htlc: expr, $offered: expr) => {
+                               HTLCOutputInCommitment {
+                                       offered: $offered,
+                                       amount_msat: $htlc.amount_msat,
+                                       cltv_expiry: $htlc.cltv_expiry,
+                                       payment_hash: $htlc.payment_hash,
+                                       transaction_output_index: None
+                               }
+                       }
+               }
+
                macro_rules! add_htlc_output {
                        ($htlc: expr, $outbound: expr, $source: expr) => {
                                if $outbound == local { // "offered HTLC output"
+                                       let htlc_in_tx = get_htlc_in_commitment!($htlc, true);
                                        if $htlc.amount_msat / 1000 >= dust_limit_satoshis + (feerate_per_kw * HTLC_TIMEOUT_TX_WEIGHT / 1000) {
-                                               let htlc_in_tx = get_htlc_in_commitment!($htlc, true);
                                                txouts.push((TxOut {
                                                        script_pubkey: chan_utils::get_htlc_redeemscript(&htlc_in_tx, &keys).to_v0_p2wsh(),
                                                        value: $htlc.amount_msat / 1000
                                                }, Some((htlc_in_tx, $source))));
                                        } else {
-                                               if let Some(source) = $source {
-                                                       unincluded_htlc_sources.push(($htlc.payment_hash, source, None));
-                                               }
+                                               included_dust_htlcs.push((htlc_in_tx, $source));
                                        }
                                } else {
+                                       let htlc_in_tx = get_htlc_in_commitment!($htlc, false);
                                        if $htlc.amount_msat / 1000 >= dust_limit_satoshis + (feerate_per_kw * HTLC_SUCCESS_TX_WEIGHT / 1000) {
-                                               let htlc_in_tx = get_htlc_in_commitment!($htlc, false);
                                                txouts.push((TxOut { // "received HTLC output"
                                                        script_pubkey: chan_utils::get_htlc_redeemscript(&htlc_in_tx, &keys).to_v0_p2wsh(),
                                                        value: $htlc.amount_msat / 1000
                                                }, Some((htlc_in_tx, $source))));
                                        } else {
-                                               if let Some(source) = $source {
-                                                       unincluded_htlc_sources.push(($htlc.payment_hash, source, None));
-                                               }
+                                               included_dust_htlcs.push((htlc_in_tx, $source));
                                        }
                                }
                        }
@@ -919,26 +919,23 @@ impl Channel {
                transaction_utils::sort_outputs(&mut txouts);
 
                let mut outputs: Vec<TxOut> = Vec::with_capacity(txouts.len());
-               let mut htlcs_included: Vec<HTLCOutputInCommitment> = Vec::with_capacity(txouts.len());
-               let mut htlc_sources: Vec<(PaymentHash, &HTLCSource, Option<u32>)> = Vec::with_capacity(txouts.len() + unincluded_htlc_sources.len());
-               for (idx, out) in txouts.drain(..).enumerate() {
+               let mut htlcs_included: Vec<(HTLCOutputInCommitment, Option<&HTLCSource>)> = Vec::with_capacity(txouts.len() + included_dust_htlcs.len());
+               for (idx, mut out) in txouts.drain(..).enumerate() {
                        outputs.push(out.0);
-                       if let Some((mut htlc, source_option)) = out.1 {
-                               htlc.transaction_output_index = idx as u32;
-                               if let Some(source) = source_option {
-                                       htlc_sources.push((htlc.payment_hash, source, Some(idx as u32)));
-                               }
-                               htlcs_included.push(htlc);
+                       if let Some((mut htlc, source_option)) = out.1.take() {
+                               htlc.transaction_output_index = Some(idx as u32);
+                               htlcs_included.push((htlc, source_option));
                        }
                }
-               htlc_sources.append(&mut unincluded_htlc_sources);
+               let non_dust_htlc_count = htlcs_included.len();
+               htlcs_included.append(&mut included_dust_htlcs);
 
                (Transaction {
                        version: 2,
                        lock_time: ((0x20 as u32) << 8*3) | ((obscured_commitment_transaction_number & 0xffffffu64) as u32),
                        input: txins,
                        output: outputs,
-               }, htlcs_included, htlc_sources)
+               }, non_dust_htlc_count, htlcs_included)
        }
 
        #[inline]
@@ -1451,9 +1448,9 @@ impl Channel {
 
                // Now that we're past error-generating stuff, update our local state:
 
-               self.channel_monitor.provide_latest_remote_commitment_tx_info(&remote_initial_commitment_tx, Vec::new(), Vec::new(), self.cur_remote_commitment_transaction_number, self.their_cur_commitment_point.unwrap());
+               self.channel_monitor.provide_latest_remote_commitment_tx_info(&remote_initial_commitment_tx, Vec::new(), self.cur_remote_commitment_transaction_number, self.their_cur_commitment_point.unwrap());
                self.last_local_commitment_txn = vec![local_initial_commitment_tx.clone()];
-               self.channel_monitor.provide_latest_local_commitment_tx_info(local_initial_commitment_tx, local_keys, self.feerate_per_kw, Vec::new(), Vec::new());
+               self.channel_monitor.provide_latest_local_commitment_tx_info(local_initial_commitment_tx, local_keys, self.feerate_per_kw, Vec::new());
                self.channel_state = ChannelState::FundingSent as u32;
                self.channel_id = funding_txo.to_channel_id();
                self.cur_remote_commitment_transaction_number -= 1;
@@ -1490,7 +1487,7 @@ impl Channel {
                secp_check!(self.secp_ctx.verify(&local_sighash, &msg.signature, &self.their_funding_pubkey.unwrap()), "Invalid funding_signed signature from peer");
 
                self.sign_commitment_transaction(&mut local_initial_commitment_tx, &msg.signature);
-               self.channel_monitor.provide_latest_local_commitment_tx_info(local_initial_commitment_tx.clone(), local_keys, self.feerate_per_kw, Vec::new(), Vec::new());
+               self.channel_monitor.provide_latest_local_commitment_tx_info(local_initial_commitment_tx.clone(), local_keys, self.feerate_per_kw, Vec::new());
                self.last_local_commitment_txn = vec![local_initial_commitment_tx];
                self.channel_state = ChannelState::FundingSent as u32;
                self.cur_local_commitment_transaction_number -= 1;
@@ -1693,7 +1690,7 @@ impl Channel {
 
                let mut local_commitment_tx = {
                        let mut commitment_tx = self.build_commitment_transaction(self.cur_local_commitment_transaction_number, &local_keys, true, false, feerate_per_kw);
-                       let htlcs_cloned: Vec<_> = commitment_tx.2.drain(..).map(|htlc_source| (htlc_source.0, htlc_source.1.clone(), htlc_source.2)).collect();
+                       let htlcs_cloned: Vec<_> = commitment_tx.2.drain(..).map(|htlc| (htlc.0, htlc.1.map(|h| h.clone()))).collect();
                        (commitment_tx.0, commitment_tx.1, htlcs_cloned)
                };
                let local_commitment_txid = local_commitment_tx.0.txid();
@@ -1702,7 +1699,7 @@ impl Channel {
 
                //If channel fee was updated by funder confirm funder can afford the new fee rate when applied to the current local commitment transaction
                if update_fee {
-                       let num_htlcs = local_commitment_tx.1.len();
+                       let num_htlcs = local_commitment_tx.1;
                        let total_fee: u64 = feerate_per_kw as u64 * (COMMITMENT_TX_BASE_WEIGHT + (num_htlcs as u64) * COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000;
 
                        if self.channel_value_satoshis - self.value_to_self_msat / 1000 < total_fee + self.their_channel_reserve_satoshis {
@@ -1710,28 +1707,32 @@ impl Channel {
                        }
                }
 
-               if msg.htlc_signatures.len() != local_commitment_tx.1.len() {
+               if msg.htlc_signatures.len() != local_commitment_tx.1 {
                        return Err(ChannelError::Close("Got wrong number of HTLC signatures from remote"));
                }
 
-               let mut new_local_commitment_txn = Vec::with_capacity(local_commitment_tx.1.len() + 1);
+               let mut new_local_commitment_txn = Vec::with_capacity(local_commitment_tx.1 + 1);
                self.sign_commitment_transaction(&mut local_commitment_tx.0, &msg.signature);
                new_local_commitment_txn.push(local_commitment_tx.0.clone());
 
-               let mut htlcs_and_sigs = Vec::with_capacity(local_commitment_tx.1.len());
-               for (idx, htlc) in local_commitment_tx.1.drain(..).enumerate() {
-                       let mut htlc_tx = self.build_htlc_transaction(&local_commitment_txid, &htlc, true, &local_keys, feerate_per_kw);
-                       let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, &local_keys);
-                       let htlc_sighash = Message::from_slice(&bip143::SighashComponents::new(&htlc_tx).sighash_all(&htlc_tx.input[0], &htlc_redeemscript, htlc.amount_msat / 1000)[..]).unwrap();
-                       secp_check!(self.secp_ctx.verify(&htlc_sighash, &msg.htlc_signatures[idx], &local_keys.b_htlc_key), "Invalid HTLC tx signature from peer");
-                       let htlc_sig = if htlc.offered {
-                               let htlc_sig = self.sign_htlc_transaction(&mut htlc_tx, &msg.htlc_signatures[idx], &None, &htlc, &local_keys)?;
-                               new_local_commitment_txn.push(htlc_tx);
-                               htlc_sig
+               let mut htlcs_and_sigs = Vec::with_capacity(local_commitment_tx.2.len());
+               for (idx, (htlc, source)) in local_commitment_tx.2.drain(..).enumerate() {
+                       if let Some(_) = htlc.transaction_output_index {
+                               let mut htlc_tx = self.build_htlc_transaction(&local_commitment_txid, &htlc, true, &local_keys, feerate_per_kw);
+                               let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, &local_keys);
+                               let htlc_sighash = Message::from_slice(&bip143::SighashComponents::new(&htlc_tx).sighash_all(&htlc_tx.input[0], &htlc_redeemscript, htlc.amount_msat / 1000)[..]).unwrap();
+                               secp_check!(self.secp_ctx.verify(&htlc_sighash, &msg.htlc_signatures[idx], &local_keys.b_htlc_key), "Invalid HTLC tx signature from peer");
+                               let htlc_sig = if htlc.offered {
+                                       let htlc_sig = self.sign_htlc_transaction(&mut htlc_tx, &msg.htlc_signatures[idx], &None, &htlc, &local_keys)?;
+                                       new_local_commitment_txn.push(htlc_tx);
+                                       htlc_sig
+                               } else {
+                                       self.create_htlc_tx_signature(&htlc_tx, &htlc, &local_keys)?.1
+                               };
+                               htlcs_and_sigs.push((htlc, Some((msg.htlc_signatures[idx], htlc_sig)), source));
                        } else {
-                               self.create_htlc_tx_signature(&htlc_tx, &htlc, &local_keys)?.1
-                       };
-                       htlcs_and_sigs.push((htlc, msg.htlc_signatures[idx], htlc_sig));
+                               htlcs_and_sigs.push((htlc, None, source));
+                       }
                }
 
                let next_per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &self.build_local_commitment_secret(self.cur_local_commitment_transaction_number - 1));
@@ -1758,7 +1759,7 @@ impl Channel {
                        self.monitor_pending_order = None;
                }
 
-               self.channel_monitor.provide_latest_local_commitment_tx_info(local_commitment_tx.0, local_keys, self.feerate_per_kw, htlcs_and_sigs, local_commitment_tx.2);
+               self.channel_monitor.provide_latest_local_commitment_tx_info(local_commitment_tx.0, local_keys, self.feerate_per_kw, htlcs_and_sigs);
 
                for htlc in self.pending_inbound_htlcs.iter_mut() {
                        let new_forward = if let &InboundHTLCState::RemoteAnnounced(ref forward_info) = &htlc.state {
@@ -3028,7 +3029,7 @@ impl Channel {
                let temporary_channel_id = self.channel_id;
 
                // Now that we're past error-generating stuff, update our local state:
-               self.channel_monitor.provide_latest_remote_commitment_tx_info(&commitment_tx, Vec::new(), Vec::new(), self.cur_remote_commitment_transaction_number, self.their_cur_commitment_point.unwrap());
+               self.channel_monitor.provide_latest_remote_commitment_tx_info(&commitment_tx, Vec::new(), self.cur_remote_commitment_transaction_number, self.their_cur_commitment_point.unwrap());
                self.channel_state = ChannelState::FundingCreated as u32;
                self.channel_id = funding_txo.to_channel_id();
                self.cur_remote_commitment_transaction_number -= 1;
@@ -3260,23 +3261,23 @@ impl Channel {
                        }
                }
 
-               let (res, remote_commitment_tx, htlcs, htlc_sources) = match self.send_commitment_no_state_update() {
-                       Ok((res, (remote_commitment_tx, htlcs, mut htlc_sources))) => {
+               let (res, remote_commitment_tx, htlcs) = match self.send_commitment_no_state_update() {
+                       Ok((res, (remote_commitment_tx, mut htlcs))) => {
                                // Update state now that we've passed all the can-fail calls...
-                               let htlc_sources_no_ref = htlc_sources.drain(..).map(|htlc_source| (htlc_source.0, htlc_source.1.clone(), htlc_source.2)).collect();
-                               (res, remote_commitment_tx, htlcs, htlc_sources_no_ref)
+                               let htlcs_no_ref = htlcs.drain(..).map(|(htlc, htlc_source)| (htlc, htlc_source.map(|source_ref| Box::new(source_ref.clone())))).collect();
+                               (res, remote_commitment_tx, htlcs_no_ref)
                        },
                        Err(e) => return Err(e),
                };
 
-               self.channel_monitor.provide_latest_remote_commitment_tx_info(&remote_commitment_tx, htlcs, htlc_sources, self.cur_remote_commitment_transaction_number, self.their_cur_commitment_point.unwrap());
+               self.channel_monitor.provide_latest_remote_commitment_tx_info(&remote_commitment_tx, htlcs, self.cur_remote_commitment_transaction_number, self.their_cur_commitment_point.unwrap());
                self.channel_state |= ChannelState::AwaitingRemoteRevoke as u32;
                Ok((res, self.channel_monitor.clone()))
        }
 
        /// Only fails in case of bad keys. Used for channel_reestablish commitment_signed generation
        /// when we shouldn't change HTLC/channel state.
-       fn send_commitment_no_state_update(&self) -> Result<(msgs::CommitmentSigned, (Transaction, Vec<HTLCOutputInCommitment>, Vec<(PaymentHash, &HTLCSource, Option<u32>)>)), ChannelError> {
+       fn send_commitment_no_state_update(&self) -> Result<(msgs::CommitmentSigned, (Transaction, Vec<(HTLCOutputInCommitment, Option<&HTLCSource>)>)), ChannelError> {
                let funding_script = self.get_funding_redeemscript();
 
                let mut feerate_per_kw = self.feerate_per_kw;
@@ -3292,21 +3293,22 @@ impl Channel {
                let remote_sighash = Message::from_slice(&bip143::SighashComponents::new(&remote_commitment_tx.0).sighash_all(&remote_commitment_tx.0.input[0], &funding_script, self.channel_value_satoshis)[..]).unwrap();
                let our_sig = self.secp_ctx.sign(&remote_sighash, &self.local_keys.funding_key);
 
-               let mut htlc_sigs = Vec::new();
-
-               for ref htlc in remote_commitment_tx.1.iter() {
-                       let htlc_tx = self.build_htlc_transaction(&remote_commitment_txid, htlc, false, &remote_keys, feerate_per_kw);
-                       let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, &remote_keys);
-                       let htlc_sighash = Message::from_slice(&bip143::SighashComponents::new(&htlc_tx).sighash_all(&htlc_tx.input[0], &htlc_redeemscript, htlc.amount_msat / 1000)[..]).unwrap();
-                       let our_htlc_key = secp_check!(chan_utils::derive_private_key(&self.secp_ctx, &remote_keys.per_commitment_point, &self.local_keys.htlc_base_key), "Derived invalid key, peer is maliciously selecting parameters");
-                       htlc_sigs.push(self.secp_ctx.sign(&htlc_sighash, &our_htlc_key));
+               let mut htlc_sigs = Vec::with_capacity(remote_commitment_tx.1);
+               for &(ref htlc, _) in remote_commitment_tx.2.iter() {
+                       if let Some(_) = htlc.transaction_output_index {
+                               let htlc_tx = self.build_htlc_transaction(&remote_commitment_txid, htlc, false, &remote_keys, feerate_per_kw);
+                               let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, &remote_keys);
+                               let htlc_sighash = Message::from_slice(&bip143::SighashComponents::new(&htlc_tx).sighash_all(&htlc_tx.input[0], &htlc_redeemscript, htlc.amount_msat / 1000)[..]).unwrap();
+                               let our_htlc_key = secp_check!(chan_utils::derive_private_key(&self.secp_ctx, &remote_keys.per_commitment_point, &self.local_keys.htlc_base_key), "Derived invalid key, peer is maliciously selecting parameters");
+                               htlc_sigs.push(self.secp_ctx.sign(&htlc_sighash, &our_htlc_key));
+                       }
                }
 
                Ok((msgs::CommitmentSigned {
                        channel_id: self.channel_id,
                        signature: our_sig,
                        htlc_signatures: htlc_sigs,
-               }, remote_commitment_tx))
+               }, (remote_commitment_tx.0, remote_commitment_tx.2)))
        }
 
        /// Adds a pending outbound HTLC to this channel, and creates a signed commitment transaction
@@ -4020,8 +4022,11 @@ mod tests {
                macro_rules! test_commitment {
                        ( $their_sig_hex: expr, $our_sig_hex: expr, $tx_hex: expr) => {
                                unsigned_tx = {
-                                       let res = chan.build_commitment_transaction(0xffffffffffff - 42, &keys, true, false, chan.feerate_per_kw);
-                                       (res.0, res.1)
+                                       let mut res = chan.build_commitment_transaction(0xffffffffffff - 42, &keys, true, false, chan.feerate_per_kw);
+                                       let htlcs = res.2.drain(..)
+                                               .filter_map(|(htlc, _)| if htlc.transaction_output_index.is_some() { Some(htlc) } else { None })
+                                               .collect();
+                                       (res.0, htlcs)
                                };
                                let their_signature = Signature::from_der(&secp_ctx, &hex::decode($their_sig_hex).unwrap()[..]).unwrap();
                                let sighash = Message::from_slice(&bip143::SighashComponents::new(&unsigned_tx.0).sighash_all(&unsigned_tx.0.input[0], &chan.get_funding_redeemscript(), chan.channel_value_satoshis)[..]).unwrap();
index ea3e279213c88cca40a7e3fa5c800828fbc20f12..3cd76203439b73417b79b222bbb916265e4f219a 100644 (file)
@@ -332,8 +332,7 @@ struct LocalSignedTx {
        b_htlc_key: PublicKey,
        delayed_payment_key: PublicKey,
        feerate_per_kw: u64,
-       htlc_outputs: Vec<(HTLCOutputInCommitment, Signature, Signature)>,
-       htlc_sources: Vec<(PaymentHash, HTLCSource, Option<u32>)>,
+       htlc_outputs: Vec<(HTLCOutputInCommitment, Option<(Signature, Signature)>, Option<HTLCSource>)>,
 }
 
 const SERIALIZATION_VERSION: u8 = 1;
@@ -358,7 +357,7 @@ pub struct ChannelMonitor {
        their_to_self_delay: Option<u16>,
 
        old_secrets: [([u8; 32], u64); 49],
-       remote_claimable_outpoints: HashMap<Sha256dHash, (Vec<HTLCOutputInCommitment>, Vec<(PaymentHash, HTLCSource, Option<u32>)>)>,
+       remote_claimable_outpoints: HashMap<Sha256dHash, Vec<(HTLCOutputInCommitment, Option<Box<HTLCSource>>)>>,
        /// We cannot identify HTLC-Success or HTLC-Timeout transactions by themselves on the chain.
        /// Nor can we figure out their commitment numbers without the commitment transaction they are
        /// spending. Thus, in order to claim them via revocation key, we track all the remote
@@ -515,7 +514,9 @@ impl ChannelMonitor {
                // TODO: We should probably consider whether we're really getting the next secret here.
                if let Storage::Local { ref mut prev_remote_commitment_txid, .. } = self.key_storage {
                        if let Some(txid) = prev_remote_commitment_txid.take() {
-                               self.remote_claimable_outpoints.get_mut(&txid).unwrap().1 = Vec::new();
+                               for &mut (_, ref mut source) in self.remote_claimable_outpoints.get_mut(&txid).unwrap() {
+                                       *source = None;
+                               }
                        }
                }
 
@@ -558,12 +559,12 @@ impl ChannelMonitor {
        /// The monitor watches for it to be broadcasted and then uses the HTLC information (and
        /// possibly future revocation/preimage information) to claim outputs where possible.
        /// We cache also the mapping hash:commitment number to lighten pruning of old preimages by watchtowers.
-       pub(super) fn provide_latest_remote_commitment_tx_info(&mut self, unsigned_commitment_tx: &Transaction, htlc_outputs: Vec<HTLCOutputInCommitment>, htlc_sources: Vec<(PaymentHash, HTLCSource, Option<u32>)>, commitment_number: u64, their_revocation_point: PublicKey) {
+       pub(super) fn provide_latest_remote_commitment_tx_info(&mut self, unsigned_commitment_tx: &Transaction, htlc_outputs: Vec<(HTLCOutputInCommitment, Option<Box<HTLCSource>>)>, commitment_number: u64, their_revocation_point: PublicKey) {
                // TODO: Encrypt the htlc_outputs data with the single-hash of the commitment transaction
                // so that a remote monitor doesn't learn anything unless there is a malicious close.
                // (only maybe, sadly we cant do the same for local info, as we need to be aware of
                // timeouts)
-               for ref htlc in &htlc_outputs {
+               for &(ref htlc, _) in &htlc_outputs {
                        self.remote_hash_commitment_number.insert(htlc.payment_hash, commitment_number);
                }
 
@@ -574,7 +575,7 @@ impl ChannelMonitor {
                        *prev_remote_commitment_txid = current_remote_commitment_txid.take();
                        *current_remote_commitment_txid = Some(new_txid);
                }
-               self.remote_claimable_outpoints.insert(new_txid, (htlc_outputs, htlc_sources));
+               self.remote_claimable_outpoints.insert(new_txid, htlc_outputs);
                self.current_remote_commitment_number = commitment_number;
                //TODO: Merge this into the other per-remote-transaction output storage stuff
                match self.their_cur_revocation_points {
@@ -604,7 +605,7 @@ impl ChannelMonitor {
        /// Panics if set_their_to_self_delay has never been called.
        /// Also update Storage with latest local per_commitment_point to derive local_delayedkey in
        /// case of onchain HTLC tx
-       pub(super) fn provide_latest_local_commitment_tx_info(&mut self, signed_commitment_tx: Transaction, local_keys: chan_utils::TxCreationKeys, feerate_per_kw: u64, htlc_outputs: Vec<(HTLCOutputInCommitment, Signature, Signature)>, htlc_sources: Vec<(PaymentHash, HTLCSource, Option<u32>)>) {
+       pub(super) fn provide_latest_local_commitment_tx_info(&mut self, signed_commitment_tx: Transaction, local_keys: chan_utils::TxCreationKeys, feerate_per_kw: u64, htlc_outputs: Vec<(HTLCOutputInCommitment, Option<(Signature, Signature)>, Option<HTLCSource>)>) {
                assert!(self.their_to_self_delay.is_some());
                self.prev_local_signed_commitment_tx = self.current_local_signed_commitment_tx.take();
                self.current_local_signed_commitment_tx = Some(LocalSignedTx {
@@ -616,7 +617,6 @@ impl ChannelMonitor {
                        delayed_payment_key: local_keys.a_delayed_payment_key,
                        feerate_per_kw,
                        htlc_outputs,
-                       htlc_sources,
                });
 
                if let Storage::Local { ref mut latest_per_commitment_point, .. } = self.key_storage {
@@ -776,8 +776,20 @@ impl ChannelMonitor {
                // Set in initial Channel-object creation, so should always be set by now:
                U48(self.commitment_transaction_number_obscure_factor).write(writer)?;
 
+               macro_rules! write_option {
+                       ($thing: expr) => {
+                               match $thing {
+                                       &Some(ref t) => {
+                                               1u8.write(writer)?;
+                                               t.write(writer)?;
+                                       },
+                                       &None => 0u8.write(writer)?,
+                               }
+                       }
+               }
+
                match self.key_storage {
-                       Storage::Local { ref revocation_base_key, ref htlc_base_key, ref delayed_payment_base_key, ref payment_base_key, ref shutdown_pubkey, ref prev_latest_per_commitment_point, ref latest_per_commitment_point, ref funding_info, current_remote_commitment_txid, prev_remote_commitment_txid } => {
+                       Storage::Local { ref revocation_base_key, ref htlc_base_key, ref delayed_payment_base_key, ref payment_base_key, ref shutdown_pubkey, ref prev_latest_per_commitment_point, ref latest_per_commitment_point, ref funding_info, ref current_remote_commitment_txid, ref prev_remote_commitment_txid } => {
                                writer.write_all(&[0; 1])?;
                                writer.write_all(&revocation_base_key[..])?;
                                writer.write_all(&htlc_base_key[..])?;
@@ -806,18 +818,8 @@ impl ChannelMonitor {
                                                debug_assert!(false, "Try to serialize a useless Local monitor !");
                                        },
                                }
-                               if let Some(ref txid) = current_remote_commitment_txid {
-                                       writer.write_all(&[1; 1])?;
-                                       writer.write_all(&txid[..])?;
-                               } else {
-                                       writer.write_all(&[0; 1])?;
-                               }
-                               if let Some(ref txid) = prev_remote_commitment_txid {
-                                       writer.write_all(&[1; 1])?;
-                                       writer.write_all(&txid[..])?;
-                               } else {
-                                       writer.write_all(&[0; 1])?;
-                               }
+                               write_option!(current_remote_commitment_txid);
+                               write_option!(prev_remote_commitment_txid);
                        },
                        Storage::Watchtower { .. } => unimplemented!(),
                }
@@ -857,34 +859,17 @@ impl ChannelMonitor {
                                writer.write_all(&byte_utils::be64_to_array($htlc_output.amount_msat))?;
                                writer.write_all(&byte_utils::be32_to_array($htlc_output.cltv_expiry))?;
                                writer.write_all(&$htlc_output.payment_hash.0[..])?;
-                               writer.write_all(&byte_utils::be32_to_array($htlc_output.transaction_output_index))?;
+                               write_option!(&$htlc_output.transaction_output_index);
                        }
                }
 
-               macro_rules! serialize_htlc_source {
-                       ($htlc_source: expr) => {
-                               $htlc_source.0.write(writer)?;
-                               $htlc_source.1.write(writer)?;
-                               if let &Some(ref txo) = &$htlc_source.2 {
-                                       writer.write_all(&[1; 1])?;
-                                       txo.write(writer)?;
-                               } else {
-                                       writer.write_all(&[0; 1])?;
-                               }
-                       }
-               }
-
-
                writer.write_all(&byte_utils::be64_to_array(self.remote_claimable_outpoints.len() as u64))?;
-               for (ref txid, &(ref htlc_infos, ref htlc_sources)) in self.remote_claimable_outpoints.iter() {
+               for (ref txid, ref htlc_infos) in self.remote_claimable_outpoints.iter() {
                        writer.write_all(&txid[..])?;
                        writer.write_all(&byte_utils::be64_to_array(htlc_infos.len() as u64))?;
-                       for ref htlc_output in htlc_infos.iter() {
+                       for &(ref htlc_output, ref htlc_source) in htlc_infos.iter() {
                                serialize_htlc_in_commitment!(htlc_output);
-                       }
-                       writer.write_all(&byte_utils::be64_to_array(htlc_sources.len() as u64))?;
-                       for ref htlc_source in htlc_sources.iter() {
-                               serialize_htlc_source!(htlc_source);
+                               write_option!(htlc_source);
                        }
                }
 
@@ -924,14 +909,16 @@ impl ChannelMonitor {
 
                                writer.write_all(&byte_utils::be64_to_array($local_tx.feerate_per_kw))?;
                                writer.write_all(&byte_utils::be64_to_array($local_tx.htlc_outputs.len() as u64))?;
-                               for &(ref htlc_output, ref their_sig, ref our_sig) in $local_tx.htlc_outputs.iter() {
+                               for &(ref htlc_output, ref sigs, ref htlc_source) in $local_tx.htlc_outputs.iter() {
                                        serialize_htlc_in_commitment!(htlc_output);
-                                       writer.write_all(&their_sig.serialize_compact(&self.secp_ctx))?;
-                                       writer.write_all(&our_sig.serialize_compact(&self.secp_ctx))?;
-                               }
-                               writer.write_all(&byte_utils::be64_to_array($local_tx.htlc_sources.len() as u64))?;
-                               for ref htlc_source in $local_tx.htlc_sources.iter() {
-                                       serialize_htlc_source!(htlc_source);
+                                       if let &Some((ref their_sig, ref our_sig)) = sigs {
+                                               1u8.write(writer)?;
+                                               writer.write_all(&their_sig.serialize_compact(&self.secp_ctx))?;
+                                               writer.write_all(&our_sig.serialize_compact(&self.secp_ctx))?;
+                                       } else {
+                                               0u8.write(writer)?;
+                                       }
+                                       write_option!(htlc_source);
                                }
                        }
                }
@@ -1115,7 +1102,7 @@ impl ChannelMonitor {
                                                let (sig, redeemscript) = match self.key_storage {
                                                        Storage::Local { ref revocation_base_key, .. } => {
                                                                let redeemscript = if $htlc_idx.is_none() { revokeable_redeemscript.clone() } else {
-                                                                       let htlc = &per_commitment_option.unwrap().0[$htlc_idx.unwrap()];
+                                                                       let htlc = &per_commitment_option.unwrap()[$htlc_idx.unwrap()].0;
                                                                        chan_utils::get_htlc_redeemscript_with_explicit_keys(htlc, &a_htlc_key, &b_htlc_key, &revocation_pubkey)
                                                                };
                                                                let sighash = ignore_error!(Message::from_slice(&$sighash_parts.sighash_all(&$input, &redeemscript, $amount)[..]));
@@ -1138,43 +1125,45 @@ impl ChannelMonitor {
                                }
                        }
 
-                       if let Some(&(ref per_commitment_data, _)) = per_commitment_option {
+                       if let Some(ref per_commitment_data) = per_commitment_option {
                                inputs.reserve_exact(per_commitment_data.len());
 
-                               for (idx, ref htlc) in per_commitment_data.iter().enumerate() {
-                                       let expected_script = chan_utils::get_htlc_redeemscript_with_explicit_keys(&htlc, &a_htlc_key, &b_htlc_key, &revocation_pubkey);
-                                       if htlc.transaction_output_index as usize >= tx.output.len() ||
-                                                       tx.output[htlc.transaction_output_index as usize].value != htlc.amount_msat / 1000 ||
-                                                       tx.output[htlc.transaction_output_index as usize].script_pubkey != expected_script.to_v0_p2wsh() {
-                                               return (txn_to_broadcast, (commitment_txid, watch_outputs), spendable_outputs, htlc_updated); // Corrupted per_commitment_data, fuck this user
-                                       }
-                                       let input = TxIn {
-                                               previous_output: BitcoinOutPoint {
-                                                       txid: commitment_txid,
-                                                       vout: htlc.transaction_output_index,
-                                               },
-                                               script_sig: Script::new(),
-                                               sequence: 0xfffffffd,
-                                               witness: Vec::new(),
-                                       };
-                                       if htlc.cltv_expiry > height + CLTV_SHARED_CLAIM_BUFFER {
-                                               inputs.push(input);
-                                               htlc_idxs.push(Some(idx));
-                                               values.push(tx.output[htlc.transaction_output_index as usize].value);
-                                               total_value += htlc.amount_msat / 1000;
-                                       } else {
-                                               let mut single_htlc_tx = Transaction {
-                                                       version: 2,
-                                                       lock_time: 0,
-                                                       input: vec![input],
-                                                       output: vec!(TxOut {
-                                                               script_pubkey: self.destination_script.clone(),
-                                                               value: htlc.amount_msat / 1000, //TODO: - fee
-                                                       }),
+                               for (idx, &(ref htlc, _)) in per_commitment_data.iter().enumerate() {
+                                       if let Some(transaction_output_index) = htlc.transaction_output_index {
+                                               let expected_script = chan_utils::get_htlc_redeemscript_with_explicit_keys(&htlc, &a_htlc_key, &b_htlc_key, &revocation_pubkey);
+                                               if transaction_output_index as usize >= tx.output.len() ||
+                                                               tx.output[transaction_output_index as usize].value != htlc.amount_msat / 1000 ||
+                                                               tx.output[transaction_output_index as usize].script_pubkey != expected_script.to_v0_p2wsh() {
+                                                       return (txn_to_broadcast, (commitment_txid, watch_outputs), spendable_outputs, htlc_updated); // Corrupted per_commitment_data, fuck this user
+                                               }
+                                               let input = TxIn {
+                                                       previous_output: BitcoinOutPoint {
+                                                               txid: commitment_txid,
+                                                               vout: transaction_output_index,
+                                                       },
+                                                       script_sig: Script::new(),
+                                                       sequence: 0xfffffffd,
+                                                       witness: Vec::new(),
                                                };
-                                               let sighash_parts = bip143::SighashComponents::new(&single_htlc_tx);
-                                               sign_input!(sighash_parts, single_htlc_tx.input[0], Some(idx), htlc.amount_msat / 1000);
-                                               txn_to_broadcast.push(single_htlc_tx);
+                                               if htlc.cltv_expiry > height + CLTV_SHARED_CLAIM_BUFFER {
+                                                       inputs.push(input);
+                                                       htlc_idxs.push(Some(idx));
+                                                       values.push(tx.output[transaction_output_index as usize].value);
+                                                       total_value += htlc.amount_msat / 1000;
+                                               } else {
+                                                       let mut single_htlc_tx = Transaction {
+                                                               version: 2,
+                                                               lock_time: 0,
+                                                               input: vec![input],
+                                                               output: vec!(TxOut {
+                                                                       script_pubkey: self.destination_script.clone(),
+                                                                       value: htlc.amount_msat / 1000, //TODO: - fee
+                                                               }),
+                                                       };
+                                                       let sighash_parts = bip143::SighashComponents::new(&single_htlc_tx);
+                                                       sign_input!(sighash_parts, single_htlc_tx.input[0], Some(idx), htlc.amount_msat / 1000);
+                                                       txn_to_broadcast.push(single_htlc_tx);
+                                               }
                                        }
                                }
                        }
@@ -1190,10 +1179,12 @@ impl ChannelMonitor {
                                // on-chain claims, so we can do that at the same time.
                                macro_rules! check_htlc_fails {
                                        ($txid: expr, $commitment_tx: expr) => {
-                                               if let Some(&(_, ref outpoints)) = self.remote_claimable_outpoints.get(&$txid) {
-                                                       for &(ref payment_hash, ref source, _) in outpoints.iter() {
-                                                               log_trace!(self, "Failing HTLC with payment_hash {} from {} remote commitment tx due to broadcast of revoked remote commitment transaction", log_bytes!(payment_hash.0), $commitment_tx);
-                                                               htlc_updated.push(((*source).clone(), None, payment_hash.clone()));
+                                               if let Some(ref outpoints) = self.remote_claimable_outpoints.get(&$txid) {
+                                                       for &(ref htlc, ref source_option) in outpoints.iter() {
+                                                               if let &Some(ref source) = source_option {
+                                                                       log_trace!(self, "Failing HTLC with payment_hash {} from {} remote commitment tx due to broadcast of revoked remote commitment transaction", log_bytes!(htlc.payment_hash.0), $commitment_tx);
+                                                                       htlc_updated.push(((**source).clone(), None, htlc.payment_hash.clone()));
+                                                               }
                                                        }
                                                }
                                        }
@@ -1252,24 +1243,26 @@ impl ChannelMonitor {
                        // on-chain claims, so we can do that at the same time.
                        macro_rules! check_htlc_fails {
                                ($txid: expr, $commitment_tx: expr, $id: tt) => {
-                                       if let Some(&(_, ref latest_outpoints)) = self.remote_claimable_outpoints.get(&$txid) {
-                                               $id: for &(ref payment_hash, ref source, _) in latest_outpoints.iter() {
-                                                       // Check if the HTLC is present in the commitment transaction that was
-                                                       // broadcast, but not if it was below the dust limit, which we should
-                                                       // fail backwards immediately as there is no way for us to learn the
-                                                       // payment_preimage.
-                                                       // Note that if the dust limit were allowed to change between
-                                                       // commitment transactions we'd want to be check whether *any*
-                                                       // broadcastable commitment transaction has the HTLC in it, but it
-                                                       // cannot currently change after channel initialization, so we don't
-                                                       // need to here.
-                                                       for &(_, ref broadcast_source, ref output_idx) in per_commitment_data.1.iter() {
-                                                               if output_idx.is_some() && source == broadcast_source {
-                                                                       continue $id;
+                                       if let Some(ref latest_outpoints) = self.remote_claimable_outpoints.get(&$txid) {
+                                               $id: for &(ref htlc, ref source_option) in latest_outpoints.iter() {
+                                                       if let &Some(ref source) = source_option {
+                                                               // Check if the HTLC is present in the commitment transaction that was
+                                                               // broadcast, but not if it was below the dust limit, which we should
+                                                               // fail backwards immediately as there is no way for us to learn the
+                                                               // payment_preimage.
+                                                               // Note that if the dust limit were allowed to change between
+                                                               // commitment transactions we'd want to be check whether *any*
+                                                               // broadcastable commitment transaction has the HTLC in it, but it
+                                                               // cannot currently change after channel initialization, so we don't
+                                                               // need to here.
+                                                               for &(ref broadcast_htlc, ref broadcast_source) in per_commitment_data.iter() {
+                                                                       if broadcast_htlc.transaction_output_index.is_some() && Some(source) == broadcast_source.as_ref() {
+                                                                               continue $id;
+                                                                       }
                                                                }
+                                                               log_trace!(self, "Failing HTLC with payment_hash {} from {} remote commitment tx due to broadcast of remote commitment transaction", log_bytes!(htlc.payment_hash.0), $commitment_tx);
+                                                               htlc_updated.push(((**source).clone(), None, htlc.payment_hash.clone()));
                                                        }
-                                                       log_trace!(self, "Failing HTLC with payment_hash {} from {} remote commitment tx due to broadcast of remote commitment transaction", log_bytes!(payment_hash.0), $commitment_tx);
-                                                       htlc_updated.push(((*source).clone(), None, payment_hash.clone()));
                                                }
                                        }
                                }
@@ -1332,7 +1325,7 @@ impl ChannelMonitor {
                                                        {
                                                                let (sig, redeemscript) = match self.key_storage {
                                                                        Storage::Local { ref htlc_base_key, .. } => {
-                                                                               let htlc = &per_commitment_option.unwrap().0[$input.sequence as usize];
+                                                                               let htlc = &per_commitment_option.unwrap()[$input.sequence as usize].0;
                                                                                let redeemscript = chan_utils::get_htlc_redeemscript_with_explicit_keys(htlc, &a_htlc_key, &b_htlc_key, &revocation_pubkey);
                                                                                let sighash = ignore_error!(Message::from_slice(&$sighash_parts.sighash_all(&$input, &redeemscript, $amount)[..]));
                                                                                let htlc_key = ignore_error!(chan_utils::derive_private_key(&self.secp_ctx, revocation_point, &htlc_base_key));
@@ -1350,71 +1343,73 @@ impl ChannelMonitor {
                                                }
                                        }
 
-                                       for (idx, ref htlc) in per_commitment_data.0.iter().enumerate() {
-                                               let expected_script = chan_utils::get_htlc_redeemscript_with_explicit_keys(&htlc, &a_htlc_key, &b_htlc_key, &revocation_pubkey);
-                                               if htlc.transaction_output_index as usize >= tx.output.len() ||
-                                                               tx.output[htlc.transaction_output_index as usize].value != htlc.amount_msat / 1000 ||
-                                                               tx.output[htlc.transaction_output_index as usize].script_pubkey != expected_script.to_v0_p2wsh() {
-                                                       return (txn_to_broadcast, (commitment_txid, watch_outputs), spendable_outputs, htlc_updated); // Corrupted per_commitment_data, fuck this user
-                                               }
-                                               if let Some(payment_preimage) = self.payment_preimages.get(&htlc.payment_hash) {
-                                                       let input = TxIn {
-                                                               previous_output: BitcoinOutPoint {
-                                                                       txid: commitment_txid,
-                                                                       vout: htlc.transaction_output_index,
-                                                               },
-                                                               script_sig: Script::new(),
-                                                               sequence: idx as u32, // reset to 0xfffffffd in sign_input
-                                                               witness: Vec::new(),
-                                                       };
-                                                       if htlc.cltv_expiry > height + CLTV_SHARED_CLAIM_BUFFER {
-                                                               inputs.push(input);
-                                                               values.push((tx.output[htlc.transaction_output_index as usize].value, payment_preimage));
-                                                               total_value += htlc.amount_msat / 1000;
-                                                       } else {
-                                                               let mut single_htlc_tx = Transaction {
+                                       for (idx, &(ref htlc, _)) in per_commitment_data.iter().enumerate() {
+                                               if let Some(transaction_output_index) = htlc.transaction_output_index {
+                                                       let expected_script = chan_utils::get_htlc_redeemscript_with_explicit_keys(&htlc, &a_htlc_key, &b_htlc_key, &revocation_pubkey);
+                                                       if transaction_output_index as usize >= tx.output.len() ||
+                                                                       tx.output[transaction_output_index as usize].value != htlc.amount_msat / 1000 ||
+                                                                       tx.output[transaction_output_index as usize].script_pubkey != expected_script.to_v0_p2wsh() {
+                                                               return (txn_to_broadcast, (commitment_txid, watch_outputs), spendable_outputs, htlc_updated); // Corrupted per_commitment_data, fuck this user
+                                                       }
+                                                       if let Some(payment_preimage) = self.payment_preimages.get(&htlc.payment_hash) {
+                                                               let input = TxIn {
+                                                                       previous_output: BitcoinOutPoint {
+                                                                               txid: commitment_txid,
+                                                                               vout: transaction_output_index,
+                                                                       },
+                                                                       script_sig: Script::new(),
+                                                                       sequence: idx as u32, // reset to 0xfffffffd in sign_input
+                                                                       witness: Vec::new(),
+                                                               };
+                                                               if htlc.cltv_expiry > height + CLTV_SHARED_CLAIM_BUFFER {
+                                                                       inputs.push(input);
+                                                                       values.push((tx.output[transaction_output_index as usize].value, payment_preimage));
+                                                                       total_value += htlc.amount_msat / 1000;
+                                                               } else {
+                                                                       let mut single_htlc_tx = Transaction {
+                                                                               version: 2,
+                                                                               lock_time: 0,
+                                                                               input: vec![input],
+                                                                               output: vec!(TxOut {
+                                                                                       script_pubkey: self.destination_script.clone(),
+                                                                                       value: htlc.amount_msat / 1000, //TODO: - fee
+                                                                               }),
+                                                                       };
+                                                                       let sighash_parts = bip143::SighashComponents::new(&single_htlc_tx);
+                                                                       sign_input!(sighash_parts, single_htlc_tx.input[0], htlc.amount_msat / 1000, payment_preimage.0.to_vec());
+                                                                       spendable_outputs.push(SpendableOutputDescriptor::StaticOutput {
+                                                                               outpoint: BitcoinOutPoint { txid: single_htlc_tx.txid(), vout: 0 },
+                                                                               output: single_htlc_tx.output[0].clone(),
+                                                                       });
+                                                                       txn_to_broadcast.push(single_htlc_tx);
+                                                               }
+                                                       }
+                                                       if !htlc.offered {
+                                                               // TODO: If the HTLC has already expired, potentially merge it with the
+                                                               // rest of the claim transaction, as above.
+                                                               let input = TxIn {
+                                                                       previous_output: BitcoinOutPoint {
+                                                                               txid: commitment_txid,
+                                                                               vout: transaction_output_index,
+                                                                       },
+                                                                       script_sig: Script::new(),
+                                                                       sequence: idx as u32,
+                                                                       witness: Vec::new(),
+                                                               };
+                                                               let mut timeout_tx = Transaction {
                                                                        version: 2,
-                                                                       lock_time: 0,
+                                                                       lock_time: htlc.cltv_expiry,
                                                                        input: vec![input],
                                                                        output: vec!(TxOut {
                                                                                script_pubkey: self.destination_script.clone(),
-                                                                               value: htlc.amount_msat / 1000, //TODO: - fee
+                                                                               value: htlc.amount_msat / 1000,
                                                                        }),
                                                                };
-                                                               let sighash_parts = bip143::SighashComponents::new(&single_htlc_tx);
-                                                               sign_input!(sighash_parts, single_htlc_tx.input[0], htlc.amount_msat / 1000, payment_preimage.0.to_vec());
-                                                               spendable_outputs.push(SpendableOutputDescriptor::StaticOutput {
-                                                                       outpoint: BitcoinOutPoint { txid: single_htlc_tx.txid(), vout: 0 },
-                                                                       output: single_htlc_tx.output[0].clone(),
-                                                               });
-                                                               txn_to_broadcast.push(single_htlc_tx);
+                                                               let sighash_parts = bip143::SighashComponents::new(&timeout_tx);
+                                                               sign_input!(sighash_parts, timeout_tx.input[0], htlc.amount_msat / 1000, vec![0]);
+                                                               txn_to_broadcast.push(timeout_tx);
                                                        }
                                                }
-                                               if !htlc.offered {
-                                                       // TODO: If the HTLC has already expired, potentially merge it with the
-                                                       // rest of the claim transaction, as above.
-                                                       let input = TxIn {
-                                                               previous_output: BitcoinOutPoint {
-                                                                       txid: commitment_txid,
-                                                                       vout: htlc.transaction_output_index,
-                                                               },
-                                                               script_sig: Script::new(),
-                                                               sequence: idx as u32,
-                                                               witness: Vec::new(),
-                                                       };
-                                                       let mut timeout_tx = Transaction {
-                                                               version: 2,
-                                                               lock_time: htlc.cltv_expiry,
-                                                               input: vec![input],
-                                                               output: vec!(TxOut {
-                                                                       script_pubkey: self.destination_script.clone(),
-                                                                       value: htlc.amount_msat / 1000,
-                                                               }),
-                                                       };
-                                                       let sighash_parts = bip143::SighashComponents::new(&timeout_tx);
-                                                       sign_input!(sighash_parts, timeout_tx.input[0], htlc.amount_msat / 1000, vec![0]);
-                                                       txn_to_broadcast.push(timeout_tx);
-                                               }
                                        }
 
                                        if inputs.is_empty() { return (txn_to_broadcast, (commitment_txid, watch_outputs), spendable_outputs, htlc_updated); } // Nothing to be done...probably a false positive/local tx
@@ -1569,41 +1564,47 @@ impl ChannelMonitor {
                        }
                }
 
-               for &(ref htlc, ref their_sig, ref our_sig) in local_tx.htlc_outputs.iter() {
-                       if htlc.offered {
-                               let mut htlc_timeout_tx = chan_utils::build_htlc_transaction(&local_tx.txid, local_tx.feerate_per_kw, self.their_to_self_delay.unwrap(), htlc, &local_tx.delayed_payment_key, &local_tx.revocation_key);
+               for &(ref htlc, ref sigs, _) in local_tx.htlc_outputs.iter() {
+                       if let Some(transaction_output_index) = htlc.transaction_output_index {
+                               if let &Some((ref their_sig, ref our_sig)) = sigs {
+                                       if htlc.offered {
+                                               log_trace!(self, "Broadcasting HTLC-Timeout transaction against local commitment transactions");
+                                               let mut htlc_timeout_tx = chan_utils::build_htlc_transaction(&local_tx.txid, local_tx.feerate_per_kw, self.their_to_self_delay.unwrap(), htlc, &local_tx.delayed_payment_key, &local_tx.revocation_key);
 
-                               htlc_timeout_tx.input[0].witness.push(Vec::new()); // First is the multisig dummy
+                                               htlc_timeout_tx.input[0].witness.push(Vec::new()); // First is the multisig dummy
 
-                               htlc_timeout_tx.input[0].witness.push(their_sig.serialize_der(&self.secp_ctx).to_vec());
-                               htlc_timeout_tx.input[0].witness[1].push(SigHashType::All as u8);
-                               htlc_timeout_tx.input[0].witness.push(our_sig.serialize_der(&self.secp_ctx).to_vec());
-                               htlc_timeout_tx.input[0].witness[2].push(SigHashType::All as u8);
+                                               htlc_timeout_tx.input[0].witness.push(their_sig.serialize_der(&self.secp_ctx).to_vec());
+                                               htlc_timeout_tx.input[0].witness[1].push(SigHashType::All as u8);
+                                               htlc_timeout_tx.input[0].witness.push(our_sig.serialize_der(&self.secp_ctx).to_vec());
+                                               htlc_timeout_tx.input[0].witness[2].push(SigHashType::All as u8);
 
-                               htlc_timeout_tx.input[0].witness.push(Vec::new());
-                               htlc_timeout_tx.input[0].witness.push(chan_utils::get_htlc_redeemscript_with_explicit_keys(htlc, &local_tx.a_htlc_key, &local_tx.b_htlc_key, &local_tx.revocation_key).into_bytes());
+                                               htlc_timeout_tx.input[0].witness.push(Vec::new());
+                                               htlc_timeout_tx.input[0].witness.push(chan_utils::get_htlc_redeemscript_with_explicit_keys(htlc, &local_tx.a_htlc_key, &local_tx.b_htlc_key, &local_tx.revocation_key).into_bytes());
 
-                               add_dynamic_output!(htlc_timeout_tx, 0);
-                               res.push(htlc_timeout_tx);
-                       } else {
-                               if let Some(payment_preimage) = self.payment_preimages.get(&htlc.payment_hash) {
-                                       let mut htlc_success_tx = chan_utils::build_htlc_transaction(&local_tx.txid, local_tx.feerate_per_kw, self.their_to_self_delay.unwrap(), htlc, &local_tx.delayed_payment_key, &local_tx.revocation_key);
+                                               add_dynamic_output!(htlc_timeout_tx, 0);
+                                               res.push(htlc_timeout_tx);
+                                       } else {
+                                               if let Some(payment_preimage) = self.payment_preimages.get(&htlc.payment_hash) {
+                                                       log_trace!(self, "Broadcasting HTLC-Success transaction against local commitment transactions");
+                                                       let mut htlc_success_tx = chan_utils::build_htlc_transaction(&local_tx.txid, local_tx.feerate_per_kw, self.their_to_self_delay.unwrap(), htlc, &local_tx.delayed_payment_key, &local_tx.revocation_key);
 
-                                       htlc_success_tx.input[0].witness.push(Vec::new()); // First is the multisig dummy
+                                                       htlc_success_tx.input[0].witness.push(Vec::new()); // First is the multisig dummy
 
-                                       htlc_success_tx.input[0].witness.push(their_sig.serialize_der(&self.secp_ctx).to_vec());
-                                       htlc_success_tx.input[0].witness[1].push(SigHashType::All as u8);
-                                       htlc_success_tx.input[0].witness.push(our_sig.serialize_der(&self.secp_ctx).to_vec());
-                                       htlc_success_tx.input[0].witness[2].push(SigHashType::All as u8);
+                                                       htlc_success_tx.input[0].witness.push(their_sig.serialize_der(&self.secp_ctx).to_vec());
+                                                       htlc_success_tx.input[0].witness[1].push(SigHashType::All as u8);
+                                                       htlc_success_tx.input[0].witness.push(our_sig.serialize_der(&self.secp_ctx).to_vec());
+                                                       htlc_success_tx.input[0].witness[2].push(SigHashType::All as u8);
 
-                                       htlc_success_tx.input[0].witness.push(payment_preimage.0.to_vec());
-                                       htlc_success_tx.input[0].witness.push(chan_utils::get_htlc_redeemscript_with_explicit_keys(htlc, &local_tx.a_htlc_key, &local_tx.b_htlc_key, &local_tx.revocation_key).into_bytes());
+                                                       htlc_success_tx.input[0].witness.push(payment_preimage.0.to_vec());
+                                                       htlc_success_tx.input[0].witness.push(chan_utils::get_htlc_redeemscript_with_explicit_keys(htlc, &local_tx.a_htlc_key, &local_tx.b_htlc_key, &local_tx.revocation_key).into_bytes());
 
-                                       add_dynamic_output!(htlc_success_tx, 0);
-                                       res.push(htlc_success_tx);
-                               }
+                                                       add_dynamic_output!(htlc_success_tx, 0);
+                                                       res.push(htlc_success_tx);
+                                               }
+                                       }
+                                       watch_outputs.push(local_tx.tx.output[transaction_output_index as usize].clone());
+                               } else { panic!("Should have sigs for non-dust local tx outputs!") }
                        }
-                       watch_outputs.push(local_tx.tx.output[htlc.transaction_output_index as usize].clone());
                }
 
                (res, spendable_outputs, watch_outputs)
@@ -1619,6 +1620,7 @@ impl ChannelMonitor {
                // weren't yet included in our commitment transaction(s).
                if let &Some(ref local_tx) = &self.current_local_signed_commitment_tx {
                        if local_tx.txid == commitment_txid {
+                               log_trace!(self, "Got latest local commitment tx broadcast, searching for available HTLCs to claim");
                                match self.key_storage {
                                        Storage::Local { ref delayed_payment_base_key, ref latest_per_commitment_point, .. } => {
                                                let (local_txn, spendable_outputs, watch_outputs) = self.broadcast_by_local_state(local_tx, latest_per_commitment_point, &Some(*delayed_payment_base_key));
@@ -1633,6 +1635,7 @@ impl ChannelMonitor {
                }
                if let &Some(ref local_tx) = &self.prev_local_signed_commitment_tx {
                        if local_tx.txid == commitment_txid {
+                               log_trace!(self, "Got previous local commitment tx broadcast, searching for available HTLCs to claim");
                                match self.key_storage {
                                        Storage::Local { ref delayed_payment_base_key, ref prev_latest_per_commitment_point, .. } => {
                                                let (local_txn, spendable_outputs, watch_outputs) = self.broadcast_by_local_state(local_tx, prev_latest_per_commitment_point, &Some(*delayed_payment_base_key));
@@ -1788,44 +1791,69 @@ impl ChannelMonitor {
        }
 
        pub(super) fn would_broadcast_at_height(&self, height: u32) -> bool {
-               // TODO: We need to consider HTLCs which weren't included in latest local commitment
-               // transaction (or in any of the latest two local commitment transactions). This probably
-               // needs to use the same logic as the revoked-tx-announe logic - checking the last two
-               // remote commitment transactions. This probably has implications for what data we need to
-               // store in local commitment transactions.
-               // TODO: We need to consider HTLCs which were below dust threshold here - while they don't
+               // We need to consider all HTLCs which are:
+               //  * in any unrevoked remote commitment transaction, as they could broadcast said
+               //    transactions and we'd end up in a race, or
+               //  * are in our latest local commitment transaction, as this is the thing we will
+               //    broadcast if we go on-chain.
+               // Note that we consider HTLCs which were below dust threshold here - while they don't
                // strictly imply that we need to fail the channel, we need to go ahead and fail them back
                // to the source, and if we don't fail the channel we will have to ensure that the next
                // updates that peer sends us are update_fails, failing the channel if not. It's probably
                // easier to just fail the channel as this case should be rare enough anyway.
+               macro_rules! scan_commitment {
+                       ($htlcs: expr, $local_tx: expr) => {
+                               for ref htlc in $htlcs {
+                                       // For inbound HTLCs which we know the preimage for, we have to ensure we hit the
+                                       // chain with enough room to claim the HTLC without our counterparty being able to
+                                       // time out the HTLC first.
+                                       // For outbound HTLCs which our counterparty hasn't failed/claimed, our primary
+                                       // concern is being able to claim the corresponding inbound HTLC (on another
+                                       // channel) before it expires. In fact, we don't even really care if our
+                                       // counterparty here claims such an outbound HTLC after it expired as long as we
+                                       // can still claim the corresponding HTLC. Thus, to avoid needlessly hitting the
+                                       // chain when our counterparty is waiting for expiration to off-chain fail an HTLC
+                                       // we give ourselves a few blocks of headroom after expiration before going
+                                       // on-chain for an expired HTLC.
+                                       // Note that, to avoid a potential attack whereby a node delays claiming an HTLC
+                                       // from us until we've reached the point where we go on-chain with the
+                                       // corresponding inbound HTLC, we must ensure that outbound HTLCs go on chain at
+                                       // least CLTV_CLAIM_BUFFER blocks prior to the inbound HTLC.
+                                       //  aka outbound_cltv + HTLC_FAIL_TIMEOUT_BLOCKS == height - CLTV_CLAIM_BUFFER
+                                       //      inbound_cltv == height + CLTV_CLAIM_BUFFER
+                                       //      outbound_cltv + HTLC_FAIL_TIMEOUT_BLOCKS + CLTV_CLAIM_BUFFER <= inbound_cltv - CLTV_CLAIM_BUFFER
+                                       //      HTLC_FAIL_TIMEOUT_BLOCKS + 2*CLTV_CLAIM_BUFFER <= inbound_cltv - outbound_cltv
+                                       //      CLTV_EXPIRY_DELTA <= inbound_cltv - outbound_cltv (by check in ChannelManager::decode_update_add_htlc_onion)
+                                       //      HTLC_FAIL_TIMEOUT_BLOCKS + 2*CLTV_CLAIM_BUFFER <= CLTV_EXPIRY_DELTA
+                                       //  The final, above, condition is checked for statically in channelmanager
+                                       //  with CHECK_CLTV_EXPIRY_SANITY_2.
+                                       let htlc_outbound = $local_tx == htlc.offered;
+                                       if ( htlc_outbound && htlc.cltv_expiry + HTLC_FAIL_TIMEOUT_BLOCKS <= height) ||
+                                          (!htlc_outbound && htlc.cltv_expiry <= height + CLTV_CLAIM_BUFFER && self.payment_preimages.contains_key(&htlc.payment_hash)) {
+                                               log_info!(self, "Force-closing channel due to {} HTLC timeout, HTLC expiry is {}", if htlc_outbound { "outbound" } else { "inbound "}, htlc.cltv_expiry);
+                                               return true;
+                                       }
+                               }
+                       }
+               }
+
                if let Some(ref cur_local_tx) = self.current_local_signed_commitment_tx {
-                       for &(ref htlc, _, _) in cur_local_tx.htlc_outputs.iter() {
-                               // For inbound HTLCs which we know the preimage for, we have to ensure we hit the
-                               // chain with enough room to claim the HTLC without our counterparty being able to
-                               // time out the HTLC first.
-                               // For outbound HTLCs which our counterparty hasn't failed/claimed, our primary
-                               // concern is being able to claim the corresponding inbound HTLC (on another
-                               // channel) before it expires. In fact, we don't even really care if our
-                               // counterparty here claims such an outbound HTLC after it expired as long as we
-                               // can still claim the corresponding HTLC. Thus, to avoid needlessly hitting the
-                               // chain when our counterparty is waiting for expiration to off-chain fail an HTLC
-                               // we give ourselves a few blocks of headroom after expiration before going
-                               // on-chain for an expired HTLC.
-                               // Note that, to avoid a potential attack whereby a node delays claiming an HTLC
-                               // from us until we've reached the point where we go on-chain with the
-                               // corresponding inbound HTLC, we must ensure that outbound HTLCs go on chain at
-                               // least CLTV_CLAIM_BUFFER blocks prior to the inbound HTLC.
-                               //  aka outbound_cltv + HTLC_FAIL_TIMEOUT_BLOCKS == height - CLTV_CLAIM_BUFFER
-                               //      inbound_cltv == height + CLTV_CLAIM_BUFFER
-                               //      outbound_cltv + HTLC_FAIL_TIMEOUT_BLOCKS + CLTV_CLAIM_BUFER <= inbound_cltv - CLTV_CLAIM_BUFFER
-                               //      HTLC_FAIL_TIMEOUT_BLOCKS + 2*CLTV_CLAIM_BUFER <= inbound_cltv - outbound_cltv
-                               //      HTLC_FAIL_TIMEOUT_BLOCKS + 2*CLTV_CLAIM_BUFER <= CLTV_EXPIRY_DELTA
-                               if ( htlc.offered && htlc.cltv_expiry + HTLC_FAIL_TIMEOUT_BLOCKS <= height) ||
-                                  (!htlc.offered && htlc.cltv_expiry <= height + CLTV_CLAIM_BUFFER && self.payment_preimages.contains_key(&htlc.payment_hash)) {
-                                       return true;
+                       scan_commitment!(cur_local_tx.htlc_outputs.iter().map(|&(ref a, _, _)| a), true);
+               }
+
+               if let Storage::Local { ref current_remote_commitment_txid, ref prev_remote_commitment_txid, .. } = self.key_storage {
+                       if let &Some(ref txid) = current_remote_commitment_txid {
+                               if let Some(ref htlc_outputs) = self.remote_claimable_outpoints.get(txid) {
+                                       scan_commitment!(htlc_outputs.iter().map(|&(ref a, _)| a), false);
+                               }
+                       }
+                       if let &Some(ref txid) = prev_remote_commitment_txid {
+                               if let Some(ref htlc_outputs) = self.remote_claimable_outpoints.get(txid) {
+                                       scan_commitment!(htlc_outputs.iter().map(|&(ref a, _)| a), false);
                                }
                        }
                }
+
                false
        }
 
@@ -1842,42 +1870,40 @@ impl ChannelMonitor {
                        let offered_preimage_claim = input.witness.len() == 3 && input.witness[2].len() == OFFERED_HTLC_SCRIPT_WEIGHT;
 
                        macro_rules! log_claim {
-                               ($source: expr, $local_tx: expr, $outbound_htlc: expr, $payment_hash: expr, $source_avail: expr) => {
+                               ($tx_info: expr, $local_tx: expr, $htlc: expr, $source_avail: expr) => {
                                        // We found the output in question, but aren't failing it backwards
                                        // as we have no corresponding source. This implies either it is an
                                        // inbound HTLC or an outbound HTLC on a revoked transaction.
+                                       let outbound_htlc = $local_tx == $htlc.offered;
                                        if ($local_tx && revocation_sig_claim) ||
-                                                       ($outbound_htlc && !$source_avail && (accepted_preimage_claim || offered_preimage_claim)) {
+                                                       (outbound_htlc && !$source_avail && (accepted_preimage_claim || offered_preimage_claim)) {
                                                log_error!(self, "Input spending {} ({}:{}) in {} resolves {} HTLC with payment hash {} with {}!",
-                                                       $source, input.previous_output.txid, input.previous_output.vout, tx.txid(),
-                                                       if $outbound_htlc { "outbound" } else { "inbound" }, log_bytes!($payment_hash.0),
+                                                       $tx_info, input.previous_output.txid, input.previous_output.vout, tx.txid(),
+                                                       if outbound_htlc { "outbound" } else { "inbound" }, log_bytes!($htlc.payment_hash.0),
                                                        if revocation_sig_claim { "revocation sig" } else { "preimage claim after we'd passed the HTLC resolution back" });
                                        } else {
                                                log_info!(self, "Input spending {} ({}:{}) in {} resolves {} HTLC with payment hash {} with {}",
-                                                       $source, input.previous_output.txid, input.previous_output.vout, tx.txid(),
-                                                       if $outbound_htlc { "outbound" } else { "inbound" }, log_bytes!($payment_hash.0),
+                                                       $tx_info, input.previous_output.txid, input.previous_output.vout, tx.txid(),
+                                                       if outbound_htlc { "outbound" } else { "inbound" }, log_bytes!($htlc.payment_hash.0),
                                                        if revocation_sig_claim { "revocation sig" } else if accepted_preimage_claim || offered_preimage_claim { "preimage" } else { "timeout" });
                                        }
                                }
                        }
 
                        macro_rules! scan_commitment {
-                               ($htlc_outputs: expr, $htlc_sources: expr, $source: expr, $local_tx: expr) => {
-                                       for &(ref payment_hash, ref source, ref vout) in $htlc_sources.iter() {
-                                               if &Some(input.previous_output.vout) == vout {
-                                                       log_claim!($source, $local_tx, true, payment_hash, true);
-                                                       // We have a resolution of an HTLC either from one of our latest
-                                                       // local commitment transactions or an unrevoked remote commitment
-                                                       // transaction. This implies we either learned a preimage, the HTLC
-                                                       // has timed out, or we screwed up. In any case, we should now
-                                                       // resolve the source HTLC with the original sender.
-                                                       payment_data = Some((source.clone(), *payment_hash));
-                                               }
-                                       }
-                                       if payment_data.is_none() {
-                                               for htlc_output in $htlc_outputs {
-                                                       if input.previous_output.vout == htlc_output.transaction_output_index {
-                                                               log_claim!($source, $local_tx, $local_tx == htlc_output.offered, htlc_output.payment_hash, false);
+                               ($htlcs: expr, $tx_info: expr, $local_tx: expr) => {
+                                       for (ref htlc_output, source_option) in $htlcs {
+                                               if Some(input.previous_output.vout) == htlc_output.transaction_output_index {
+                                                       if let Some(ref source) = source_option {
+                                                               log_claim!($tx_info, $local_tx, htlc_output, true);
+                                                               // We have a resolution of an HTLC either from one of our latest
+                                                               // local commitment transactions or an unrevoked remote commitment
+                                                               // transaction. This implies we either learned a preimage, the HTLC
+                                                               // has timed out, or we screwed up. In any case, we should now
+                                                               // resolve the source HTLC with the original sender.
+                                                               payment_data = Some(((*source).clone(), htlc_output.payment_hash));
+                                                       } else {
+                                                               log_claim!($tx_info, $local_tx, htlc_output, false);
                                                                continue 'outer_loop;
                                                        }
                                                }
@@ -1887,20 +1913,19 @@ impl ChannelMonitor {
 
                        if let Some(ref current_local_signed_commitment_tx) = self.current_local_signed_commitment_tx {
                                if input.previous_output.txid == current_local_signed_commitment_tx.txid {
-                                       scan_commitment!(current_local_signed_commitment_tx.htlc_outputs.iter().map(|&(ref a, _, _)| a),
-                                               current_local_signed_commitment_tx.htlc_sources,
+                                       scan_commitment!(current_local_signed_commitment_tx.htlc_outputs.iter().map(|&(ref a, _, ref b)| (a, b.as_ref())),
                                                "our latest local commitment tx", true);
                                }
                        }
                        if let Some(ref prev_local_signed_commitment_tx) = self.prev_local_signed_commitment_tx {
                                if input.previous_output.txid == prev_local_signed_commitment_tx.txid {
-                                       scan_commitment!(prev_local_signed_commitment_tx.htlc_outputs.iter().map(|&(ref a, _, _)| a),
-                                               prev_local_signed_commitment_tx.htlc_sources,
+                                       scan_commitment!(prev_local_signed_commitment_tx.htlc_outputs.iter().map(|&(ref a, _, ref b)| (a, b.as_ref())),
                                                "our previous local commitment tx", true);
                                }
                        }
-                       if let Some(&(ref htlc_outputs, ref htlc_sources)) = self.remote_claimable_outpoints.get(&input.previous_output.txid) {
-                               scan_commitment!(htlc_outputs, htlc_sources, "remote commitment tx", false);
+                       if let Some(ref htlc_outputs) = self.remote_claimable_outpoints.get(&input.previous_output.txid) {
+                               scan_commitment!(htlc_outputs.iter().map(|&(ref a, ref b)| (a, (b.as_ref().clone()).map(|boxed| &**boxed))),
+                                       "remote commitment tx", false);
                        }
 
                        // Check that scan_commitment, above, decided there is some source worth relaying an
@@ -1935,6 +1960,13 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelM
                                }
                        }
                }
+               macro_rules! read_option { () => {
+                       match <u8 as Readable<R>>::read(reader)? {
+                               0 => None,
+                               1 => Some(Readable::read(reader)?),
+                               _ => return Err(DecodeError::InvalidValue),
+                       }
+               } }
 
                let _ver: u8 = Readable::read(reader)?;
                let min_ver: u8 = Readable::read(reader)?;
@@ -1968,16 +2000,8 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelM
                                        index: Readable::read(reader)?,
                                };
                                let funding_info = Some((outpoint, Readable::read(reader)?));
-                               let current_remote_commitment_txid = match <u8 as Readable<R>>::read(reader)? {
-                                       0 => None,
-                                       1 => Some(Readable::read(reader)?),
-                                       _ => return Err(DecodeError::InvalidValue),
-                               };
-                               let prev_remote_commitment_txid = match <u8 as Readable<R>>::read(reader)? {
-                                       0 => None,
-                                       1 => Some(Readable::read(reader)?),
-                                       _ => return Err(DecodeError::InvalidValue),
-                               };
+                               let current_remote_commitment_txid = read_option!();
+                               let prev_remote_commitment_txid = read_option!();
                                Storage::Local {
                                        revocation_base_key,
                                        htlc_base_key,
@@ -2028,7 +2052,7 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelM
                                        let amount_msat: u64 = Readable::read(reader)?;
                                        let cltv_expiry: u32 = Readable::read(reader)?;
                                        let payment_hash: PaymentHash = Readable::read(reader)?;
-                                       let transaction_output_index: u32 = Readable::read(reader)?;
+                                       let transaction_output_index: Option<u32> = read_option!();
 
                                        HTLCOutputInCommitment {
                                                offered, amount_msat, cltv_expiry, payment_hash, transaction_output_index
@@ -2037,35 +2061,16 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelM
                        }
                }
 
-               macro_rules! read_htlc_source {
-                       () => {
-                               {
-                                       (Readable::read(reader)?, Readable::read(reader)?,
-                                               match <u8 as Readable<R>>::read(reader)? {
-                                                       0 => None,
-                                                       1 => Some(Readable::read(reader)?),
-                                                       _ => return Err(DecodeError::InvalidValue),
-                                               }
-                                       )
-                               }
-                       }
-               }
-
                let remote_claimable_outpoints_len: u64 = Readable::read(reader)?;
                let mut remote_claimable_outpoints = HashMap::with_capacity(cmp::min(remote_claimable_outpoints_len as usize, MAX_ALLOC_SIZE / 64));
                for _ in 0..remote_claimable_outpoints_len {
                        let txid: Sha256dHash = Readable::read(reader)?;
-                       let outputs_count: u64 = Readable::read(reader)?;
-                       let mut outputs = Vec::with_capacity(cmp::min(outputs_count as usize, MAX_ALLOC_SIZE / 32));
-                       for _ in 0..outputs_count {
-                               outputs.push(read_htlc_in_commitment!());
+                       let htlcs_count: u64 = Readable::read(reader)?;
+                       let mut htlcs = Vec::with_capacity(cmp::min(htlcs_count as usize, MAX_ALLOC_SIZE / 32));
+                       for _ in 0..htlcs_count {
+                               htlcs.push((read_htlc_in_commitment!(), read_option!().map(|o: HTLCSource| Box::new(o))));
                        }
-                       let sources_count: u64 = Readable::read(reader)?;
-                       let mut sources = Vec::with_capacity(cmp::min(sources_count as usize, MAX_ALLOC_SIZE / 32));
-                       for _ in 0..sources_count {
-                               sources.push(read_htlc_source!());
-                       }
-                       if let Some(_) = remote_claimable_outpoints.insert(txid, (outputs, sources)) {
+                       if let Some(_) = remote_claimable_outpoints.insert(txid, htlcs) {
                                return Err(DecodeError::InvalidValue);
                        }
                }
@@ -2117,23 +2122,22 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelM
                                        let delayed_payment_key = Readable::read(reader)?;
                                        let feerate_per_kw: u64 = Readable::read(reader)?;
 
-                                       let htlc_outputs_len: u64 = Readable::read(reader)?;
-                                       let mut htlc_outputs = Vec::with_capacity(cmp::min(htlc_outputs_len as usize, MAX_ALLOC_SIZE / 128));
-                                       for _ in 0..htlc_outputs_len {
-                                               let out = read_htlc_in_commitment!();
-                                               let sigs = (Readable::read(reader)?, Readable::read(reader)?);
-                                               htlc_outputs.push((out, sigs.0, sigs.1));
-                                       }
-
-                                       let htlc_sources_len: u64 = Readable::read(reader)?;
-                                       let mut htlc_sources = Vec::with_capacity(cmp::min(htlc_outputs_len as usize, MAX_ALLOC_SIZE / 128));
-                                       for _ in 0..htlc_sources_len {
-                                               htlc_sources.push(read_htlc_source!());
+                                       let htlcs_len: u64 = Readable::read(reader)?;
+                                       let mut htlcs = Vec::with_capacity(cmp::min(htlcs_len as usize, MAX_ALLOC_SIZE / 128));
+                                       for _ in 0..htlcs_len {
+                                               let htlc = read_htlc_in_commitment!();
+                                               let sigs = match <u8 as Readable<R>>::read(reader)? {
+                                                       0 => None,
+                                                       1 => Some((Readable::read(reader)?, Readable::read(reader)?)),
+                                                       _ => return Err(DecodeError::InvalidValue),
+                                               };
+                                               htlcs.push((htlc, sigs, read_option!()));
                                        }
 
                                        LocalSignedTx {
                                                txid: tx.txid(),
-                                               tx, revocation_key, a_htlc_key, b_htlc_key, delayed_payment_key, feerate_per_kw, htlc_outputs, htlc_sources
+                                               tx, revocation_key, a_htlc_key, b_htlc_key, delayed_payment_key, feerate_per_kw,
+                                               htlc_outputs: htlcs
                                        }
                                }
                        }
@@ -2213,7 +2217,7 @@ mod tests {
        use ln::chan_utils::{HTLCOutputInCommitment, TxCreationKeys};
        use util::test_utils::TestLogger;
        use secp256k1::key::{SecretKey,PublicKey};
-       use secp256k1::{Secp256k1, Signature};
+       use secp256k1::Secp256k1;
        use rand::{thread_rng,Rng};
        use std::sync::Arc;
 
@@ -2576,7 +2580,6 @@ mod tests {
        fn test_prune_preimages() {
                let secp_ctx = Secp256k1::new();
                let logger = Arc::new(TestLogger::new());
-               let dummy_sig = Signature::from_der(&secp_ctx, &hex::decode("3045022100fa86fa9a36a8cd6a7bb8f06a541787d51371d067951a9461d5404de6b928782e02201c8b7c334c10aed8976a3a465be9a28abff4cb23acbf00022295b378ce1fa3cd").unwrap()[..]).unwrap();
 
                let dummy_key = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &[42; 32]).unwrap());
                macro_rules! dummy_keys {
@@ -2611,13 +2614,13 @@ mod tests {
                                {
                                        let mut res = Vec::new();
                                        for (idx, preimage) in $preimages_slice.iter().enumerate() {
-                                               res.push(HTLCOutputInCommitment {
+                                               res.push((HTLCOutputInCommitment {
                                                        offered: true,
                                                        amount_msat: 0,
                                                        cltv_expiry: 0,
                                                        payment_hash: preimage.1.clone(),
-                                                       transaction_output_index: idx as u32,
-                                               });
+                                                       transaction_output_index: Some(idx as u32),
+                                               }, None));
                                        }
                                        res
                                }
@@ -2627,7 +2630,7 @@ mod tests {
                        ($preimages_slice: expr) => {
                                {
                                        let mut inp = preimages_slice_to_htlc_outputs!($preimages_slice);
-                                       let res: Vec<_> = inp.drain(..).map(|e| { (e, dummy_sig.clone(), dummy_sig.clone()) }).collect();
+                                       let res: Vec<_> = inp.drain(..).map(|e| { (e.0, None, e.1) }).collect();
                                        res
                                }
                        }
@@ -2646,11 +2649,11 @@ mod tests {
                let mut monitor = ChannelMonitor::new(&SecretKey::from_slice(&secp_ctx, &[42; 32]).unwrap(), &SecretKey::from_slice(&secp_ctx, &[43; 32]).unwrap(), &SecretKey::from_slice(&secp_ctx, &[44; 32]).unwrap(), &SecretKey::from_slice(&secp_ctx, &[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &[45; 32]).unwrap()), 0, Script::new(), logger.clone());
                monitor.set_their_to_self_delay(10);
 
-               monitor.provide_latest_local_commitment_tx_info(dummy_tx.clone(), dummy_keys!(), 0, preimages_to_local_htlcs!(preimages[0..10]), Vec::new());
-               monitor.provide_latest_remote_commitment_tx_info(&dummy_tx, preimages_slice_to_htlc_outputs!(preimages[5..15]), Vec::new(), 281474976710655, dummy_key);
-               monitor.provide_latest_remote_commitment_tx_info(&dummy_tx, preimages_slice_to_htlc_outputs!(preimages[15..20]), Vec::new(), 281474976710654, dummy_key);
-               monitor.provide_latest_remote_commitment_tx_info(&dummy_tx, preimages_slice_to_htlc_outputs!(preimages[17..20]), Vec::new(), 281474976710653, dummy_key);
-               monitor.provide_latest_remote_commitment_tx_info(&dummy_tx, preimages_slice_to_htlc_outputs!(preimages[18..20]), Vec::new(), 281474976710652, dummy_key);
+               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);
+               monitor.provide_latest_remote_commitment_tx_info(&dummy_tx, preimages_slice_to_htlc_outputs!(preimages[15..20]), 281474976710654, dummy_key);
+               monitor.provide_latest_remote_commitment_tx_info(&dummy_tx, preimages_slice_to_htlc_outputs!(preimages[17..20]), 281474976710653, dummy_key);
+               monitor.provide_latest_remote_commitment_tx_info(&dummy_tx, preimages_slice_to_htlc_outputs!(preimages[18..20]), 281474976710652, dummy_key);
                for &(ref preimage, ref hash) in preimages.iter() {
                        monitor.provide_payment_preimage(hash, preimage);
                }
@@ -2672,7 +2675,7 @@ mod tests {
 
                // Now update local commitment tx info, pruning only element 18 as we still care about the
                // previous commitment tx's preimages too
-               monitor.provide_latest_local_commitment_tx_info(dummy_tx.clone(), dummy_keys!(), 0, preimages_to_local_htlcs!(preimages[0..5]), Vec::new());
+               monitor.provide_latest_local_commitment_tx_info(dummy_tx.clone(), dummy_keys!(), 0, preimages_to_local_htlcs!(preimages[0..5]));
                secret[0..32].clone_from_slice(&hex::decode("2273e227a5b7449b6e70f1fb4652864038b1cbf9cd7c043a7d6456b7fc275ad8").unwrap());
                monitor.provide_secret(281474976710653, secret.clone()).unwrap();
                assert_eq!(monitor.payment_preimages.len(), 12);
@@ -2680,7 +2683,7 @@ mod tests {
                test_preimages_exist!(&preimages[18..20], monitor);
 
                // But if we do it again, we'll prune 5-10
-               monitor.provide_latest_local_commitment_tx_info(dummy_tx.clone(), dummy_keys!(), 0, preimages_to_local_htlcs!(preimages[0..3]), Vec::new());
+               monitor.provide_latest_local_commitment_tx_info(dummy_tx.clone(), dummy_keys!(), 0, preimages_to_local_htlcs!(preimages[0..3]));
                secret[0..32].clone_from_slice(&hex::decode("27cddaa5624534cb6cb9d7da077cf2b22ab21e9b506fd4998a51d54502e99116").unwrap());
                monitor.provide_secret(281474976710652, secret.clone()).unwrap();
                assert_eq!(monitor.payment_preimages.len(), 5);
index 6dd09e1ee4c073a6a8394b0dd56163785ac4681f..de2b42d3fa372e32f1aa5fdf64f051b84a9acb01 100644 (file)
@@ -50,11 +50,12 @@ use std::sync::atomic::Ordering;
 use std::time::Instant;
 use std::mem;
 
+const CHAN_CONFIRM_DEPTH: u32 = 100;
 fn confirm_transaction(chain: &chaininterface::ChainWatchInterfaceUtil, tx: &Transaction, chan_id: u32) {
        assert!(chain.does_match_tx(tx));
        let mut header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
        chain.block_connected_checked(&header, 1, &[tx; 1], &[chan_id; 1]);
-       for i in 2..100 {
+       for i in 2..CHAN_CONFIRM_DEPTH {
                header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
                chain.block_connected_checked(&header, i, &[tx; 0], &[0; 0]);
        }
@@ -6046,6 +6047,146 @@ fn test_static_output_closing_tx() {
        check_spends!(spend_txn[0], closing_tx);
 }
 
+fn do_htlc_claim_local_commitment_only(use_dust: bool) {
+       let nodes = create_network(2);
+       let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
+
+       let (our_payment_preimage, _) = route_payment(&nodes[0], &[&nodes[1]], if use_dust { 50000 } else { 3000000 });
+
+       // Claim the payment, but don't deliver A's commitment_signed, resulting in the HTLC only being
+       // present in B's local commitment transaction, but none of A's commitment transactions.
+       assert!(nodes[1].node.claim_funds(our_payment_preimage));
+       check_added_monitors!(nodes[1], 1);
+
+       let bs_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
+       nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_updates.update_fulfill_htlcs[0]).unwrap();
+       let events = nodes[0].node.get_and_clear_pending_events();
+       assert_eq!(events.len(), 1);
+       match events[0] {
+               Event::PaymentSent { payment_preimage } => {
+                       assert_eq!(payment_preimage, our_payment_preimage);
+               },
+               _ => panic!("Unexpected event"),
+       }
+
+       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_updates.commitment_signed).unwrap();
+       check_added_monitors!(nodes[0], 1);
+       let as_updates = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
+       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_updates.0).unwrap();
+       check_added_monitors!(nodes[1], 1);
+
+       let mut header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+       for i in 1..TEST_FINAL_CLTV - CLTV_CLAIM_BUFFER + CHAN_CONFIRM_DEPTH + 1 {
+               nodes[1].chain_monitor.block_connected_checked(&header, i, &Vec::new(), &Vec::new());
+               header.prev_blockhash = header.bitcoin_hash();
+       }
+       test_txn_broadcast(&nodes[1], &chan, None, if use_dust { HTLCType::NONE } else { HTLCType::SUCCESS });
+       check_closed_broadcast!(nodes[1]);
+}
+
+fn do_htlc_claim_current_remote_commitment_only(use_dust: bool) {
+       let mut nodes = create_network(2);
+       let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
+
+       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), if use_dust { 50000 } else { 3000000 }, TEST_FINAL_CLTV).unwrap();
+       let (_, payment_hash) = get_payment_preimage_hash!(nodes[0]);
+       nodes[0].node.send_payment(route, payment_hash).unwrap();
+       check_added_monitors!(nodes[0], 1);
+
+       let _as_update = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
+
+       // As far as A is concerened, the HTLC is now present only in the latest remote commitment
+       // transaction, however it is not in A's latest local commitment, so we can just broadcast that
+       // to "time out" the HTLC.
+
+       let mut header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+       for i in 1..TEST_FINAL_CLTV + HTLC_FAIL_TIMEOUT_BLOCKS + CHAN_CONFIRM_DEPTH + 1 {
+               nodes[0].chain_monitor.block_connected_checked(&header, i, &Vec::new(), &Vec::new());
+               header.prev_blockhash = header.bitcoin_hash();
+       }
+       test_txn_broadcast(&nodes[0], &chan, None, HTLCType::NONE);
+       check_closed_broadcast!(nodes[0]);
+}
+
+fn do_htlc_claim_previous_remote_commitment_only(use_dust: bool, check_revoke_no_close: bool) {
+       let nodes = create_network(3);
+       let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
+
+       // Fail the payment, but don't deliver A's final RAA, resulting in the HTLC only being present
+       // in B's previous (unrevoked) commitment transaction, but none of A's commitment transactions.
+       // Also optionally test that we *don't* fail the channel in case the commitment transaction was
+       // actually revoked.
+       let htlc_value = if use_dust { 50000 } else { 3000000 };
+       let (_, our_payment_hash) = route_payment(&nodes[0], &[&nodes[1]], htlc_value);
+       assert!(nodes[1].node.fail_htlc_backwards(&our_payment_hash, htlc_value));
+       expect_pending_htlcs_forwardable!(nodes[1]);
+       check_added_monitors!(nodes[1], 1);
+
+       let bs_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
+       nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &bs_updates.update_fail_htlcs[0]).unwrap();
+       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_updates.commitment_signed).unwrap();
+       check_added_monitors!(nodes[0], 1);
+       let as_updates = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
+       nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_updates.0).unwrap();
+       check_added_monitors!(nodes[1], 1);
+       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_updates.1).unwrap();
+       check_added_monitors!(nodes[1], 1);
+       let bs_revoke_and_ack = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
+
+       if check_revoke_no_close {
+               nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_revoke_and_ack).unwrap();
+               check_added_monitors!(nodes[0], 1);
+       }
+
+       let mut header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+       for i in 1..TEST_FINAL_CLTV + HTLC_FAIL_TIMEOUT_BLOCKS + CHAN_CONFIRM_DEPTH + 1 {
+               nodes[0].chain_monitor.block_connected_checked(&header, i, &Vec::new(), &Vec::new());
+               header.prev_blockhash = header.bitcoin_hash();
+       }
+       if !check_revoke_no_close {
+               test_txn_broadcast(&nodes[0], &chan, None, HTLCType::NONE);
+               check_closed_broadcast!(nodes[0]);
+       } else {
+               let events = nodes[0].node.get_and_clear_pending_events();
+               assert_eq!(events.len(), 1);
+               match events[0] {
+                       Event::PaymentFailed { payment_hash, rejected_by_dest, .. } => {
+                               assert_eq!(payment_hash, our_payment_hash);
+                               assert!(rejected_by_dest);
+                       },
+                       _ => panic!("Unexpected event"),
+               }
+       }
+}
+
+// Test that we close channels on-chain when broadcastable HTLCs reach their timeout window.
+// There are only a few cases to test here:
+//  * its not really normative behavior, but we test that below-dust HTLCs "included" in
+//    broadcastable commitment transactions result in channel closure,
+//  * its included in an unrevoked-but-previous remote commitment transaction,
+//  * its included in the latest remote or local commitment transactions.
+// We test each of the three possible commitment transactions individually and use both dust and
+// non-dust HTLCs.
+// Note that we don't bother testing both outbound and inbound HTLC failures for each case, and we
+// assume they are handled the same across all six cases, as both outbound and inbound failures are
+// tested for at least one of the cases in other tests.
+#[test]
+fn htlc_claim_single_commitment_only_a() {
+       do_htlc_claim_local_commitment_only(true);
+       do_htlc_claim_local_commitment_only(false);
+
+       do_htlc_claim_current_remote_commitment_only(true);
+       do_htlc_claim_current_remote_commitment_only(false);
+}
+
+#[test]
+fn htlc_claim_single_commitment_only_b() {
+       do_htlc_claim_previous_remote_commitment_only(true, false);
+       do_htlc_claim_previous_remote_commitment_only(false, false);
+       do_htlc_claim_previous_remote_commitment_only(true, true);
+       do_htlc_claim_previous_remote_commitment_only(false, true);
+}
+
 fn run_onion_failure_test<F1,F2>(_name: &str, test_case: u8, nodes: &Vec<Node>, route: &Route, payment_hash: &PaymentHash, callback_msg: F1, callback_node: F2, expected_retryable: bool, expected_error_code: Option<u16>, expected_channel_update: Option<HTLCFailChannelUpdate>)
        where F1: for <'a> FnMut(&'a mut msgs::UpdateAddHTLC),
                                F2: FnMut(),