Log chain calls in ChainMonitor, reducing logs in ChannelMonitor
[rust-lightning] / lightning / src / chain / channelmonitor.rs
index 23696e7b8dbdb9cf89d0671e47f8ed854121b1f3..dc009c4d52cf3e5ef228e505abda9bf3b25d5ea2 100644 (file)
@@ -106,7 +106,9 @@ impl Readable for ChannelMonitorUpdate {
                let len: u64 = Readable::read(r)?;
                let mut updates = Vec::with_capacity(cmp::min(len as usize, MAX_ALLOC_SIZE / ::core::mem::size_of::<ChannelMonitorUpdateStep>()));
                for _ in 0..len {
-                       updates.push(Readable::read(r)?);
+                       if let Some(upd) = MaybeReadable::read(r)? {
+                               updates.push(upd);
+                       }
                }
                read_tlv_fields!(r, {});
                Ok(Self { update_id, updates })
@@ -394,13 +396,36 @@ enum OnchainEvent {
        },
 }
 
-impl_writeable_tlv_based!(OnchainEventEntry, {
-       (0, txid, required),
-       (2, height, required),
-       (4, event, required),
-});
+impl Writeable for OnchainEventEntry {
+       fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
+               write_tlv_fields!(writer, {
+                       (0, self.txid, required),
+                       (2, self.height, required),
+                       (4, self.event, required),
+               });
+               Ok(())
+       }
+}
 
-impl_writeable_tlv_based_enum!(OnchainEvent,
+impl MaybeReadable for OnchainEventEntry {
+       fn read<R: io::Read>(reader: &mut R) -> Result<Option<Self>, DecodeError> {
+               let mut txid = Default::default();
+               let mut height = 0;
+               let mut event = None;
+               read_tlv_fields!(reader, {
+                       (0, txid, required),
+                       (2, height, required),
+                       (4, event, ignorable),
+               });
+               if let Some(ev) = event {
+                       Ok(Some(Self { txid, height, event: ev }))
+               } else {
+                       Ok(None)
+               }
+       }
+}
+
+impl_writeable_tlv_based_enum_upgradable!(OnchainEvent,
        (0, HTLCUpdate) => {
                (0, source, required),
                (1, onchain_value_satoshis, option),
@@ -409,7 +434,7 @@ impl_writeable_tlv_based_enum!(OnchainEvent,
        (1, MaturingOutput) => {
                (0, descriptor, required),
        },
-;);
+);
 
 #[cfg_attr(any(test, feature = "fuzztarget", feature = "_test_utils"), derive(PartialEq))]
 #[derive(Clone)]
@@ -438,9 +463,12 @@ pub(crate) enum ChannelMonitorUpdateStep {
                /// think we've fallen behind!
                should_broadcast: bool,
        },
+       ShutdownScript {
+               scriptpubkey: Script,
+       },
 }
 
-impl_writeable_tlv_based_enum!(ChannelMonitorUpdateStep,
+impl_writeable_tlv_based_enum_upgradable!(ChannelMonitorUpdateStep,
        (0, LatestHolderCommitmentTXInfo) => {
                (0, commitment_tx, required),
                (2, htlc_outputs, vec_type),
@@ -461,7 +489,10 @@ impl_writeable_tlv_based_enum!(ChannelMonitorUpdateStep,
        (4, ChannelForceClosed) => {
                (0, should_broadcast, required),
        },
-;);
+       (5, ShutdownScript) => {
+               (0, scriptpubkey, required),
+       },
+);
 
 /// A ChannelMonitor handles chain events (blocks connected and disconnected) and generates
 /// on-chain transactions to ensure no loss of funds occurs.
@@ -493,7 +524,7 @@ pub(crate) struct ChannelMonitorImpl<Signer: Sign> {
        destination_script: Script,
        broadcasted_holder_revokable_script: Option<(Script, PublicKey, PublicKey)>,
        counterparty_payment_script: Script,
-       shutdown_script: Script,
+       shutdown_script: Option<Script>,
 
        channel_keys_id: [u8; 32],
        holder_revocation_basepoint: PublicKey,
@@ -510,6 +541,9 @@ pub(crate) struct ChannelMonitorImpl<Signer: Sign> {
        on_holder_tx_csv: u16,
 
        commitment_secrets: CounterpartyCommitmentSecrets,
+       /// The set of outpoints in each counterparty commitment transaction. We always need at least
+       /// the payment hash from `HTLCOutputInCommitment` to claim even a revoked commitment
+       /// transaction broadcast as we need to be able to construct the witness script in all cases.
        counterparty_claimable_outpoints: HashMap<Txid, 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
@@ -669,7 +703,10 @@ impl<Signer: Sign> Writeable for ChannelMonitorImpl<Signer> {
                }
 
                self.counterparty_payment_script.write(writer)?;
-               self.shutdown_script.write(writer)?;
+               match &self.shutdown_script {
+                       Some(script) => script.write(writer)?,
+                       None => Script::new().write(writer)?,
+               }
 
                self.channel_keys_id.write(writer)?;
                self.holder_revocation_basepoint.write(writer)?;
@@ -799,7 +836,7 @@ impl<Signer: Sign> Writeable for ChannelMonitorImpl<Signer> {
 }
 
 impl<Signer: Sign> ChannelMonitor<Signer> {
-       pub(crate) fn new(secp_ctx: Secp256k1<secp256k1::All>, keys: Signer, shutdown_pubkey: &PublicKey,
+       pub(crate) fn new(secp_ctx: Secp256k1<secp256k1::All>, keys: Signer, shutdown_script: Option<Script>,
                          on_counterparty_tx_csv: u16, destination_script: &Script, funding_info: (OutPoint, Script),
                          channel_parameters: &ChannelTransactionParameters,
                          funding_redeemscript: Script, channel_value_satoshis: u64,
@@ -808,8 +845,6 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
                          best_block: BestBlock) -> ChannelMonitor<Signer> {
 
                assert!(commitment_transaction_number_obscure_factor <= (1 << 48));
-               let our_channel_close_key_hash = WPubkeyHash::hash(&shutdown_pubkey.serialize());
-               let shutdown_script = Builder::new().push_opcode(opcodes::all::OP_PUSHBYTES_0).push_slice(&our_channel_close_key_hash[..]).into_script();
                let payment_key_hash = WPubkeyHash::hash(&keys.pubkeys().payment_point.serialize());
                let counterparty_payment_script = Builder::new().push_opcode(opcodes::all::OP_PUSHBYTES_0).push_slice(&payment_key_hash[..]).into_script();
 
@@ -1204,6 +1239,18 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
 /// Compares a broadcasted commitment transaction's HTLCs with those in the latest state,
 /// failing any HTLCs which didn't make it into the broadcasted commitment transaction back
 /// after ANTI_REORG_DELAY blocks.
+///
+/// We always compare against the set of HTLCs in counterparty commitment transactions, as those
+/// are the commitment transactions which are generated by us. The off-chain state machine in
+/// `Channel` will automatically resolve any HTLCs which were never included in a commitment
+/// transaction when it detects channel closure, but it is up to us to ensure any HTLCs which were
+/// included in a remote commitment transaction are failed back if they are not present in the
+/// broadcasted commitment transaction.
+///
+/// Specifically, the removal process for HTLCs in `Channel` is always based on the counterparty
+/// sending a `revoke_and_ack`, which causes us to clear `prev_counterparty_commitment_txid`. Thus,
+/// as long as we examine both the current counterparty commitment transaction and, if it hasn't
+/// been revoked yet, the previous one, we we will never "forget" to resolve an HTLC.
 macro_rules! fail_unbroadcast_htlcs {
        ($self: expr, $commitment_tx_type: expr, $commitment_tx_conf_height: expr, $confirmed_htlcs_list: expr, $logger: expr) => { {
                macro_rules! check_htlc_fails {
@@ -1499,7 +1546,13 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
                                                // shouldn't print the scary warning above.
                                                log_info!(logger, "Channel off-chain state closed after we broadcasted our latest commitment transaction.");
                                        }
-                               }
+                               },
+                               ChannelMonitorUpdateStep::ShutdownScript { scriptpubkey } => {
+                                       log_trace!(logger, "Updating ChannelMonitor with shutdown script");
+                                       if let Some(shutdown_script) = self.shutdown_script.replace(scriptpubkey.clone()) {
+                                               panic!("Attempted to replace shutdown script {} with {}", shutdown_script, scriptpubkey);
+                                       }
+                               },
                        }
                }
                self.latest_update_id = updates.update_id;
@@ -1780,6 +1833,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
                        let res = self.get_broadcasted_holder_claims(&self.current_holder_commitment_tx, height);
                        let mut to_watch = self.get_broadcasted_holder_watch_outputs(&self.current_holder_commitment_tx, tx);
                        append_onchain_update!(res, to_watch);
+                       fail_unbroadcast_htlcs!(self, "latest holder", height, self.current_holder_commitment_tx.htlc_outputs.iter().map(|(a, _, c)| (a, c.as_ref())), logger);
                } else if let &Some(ref holder_tx) = &self.prev_holder_signed_commitment_tx {
                        if holder_tx.txid == commitment_txid {
                                is_holder_tx = true;
@@ -1787,45 +1841,11 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
                                let res = self.get_broadcasted_holder_claims(holder_tx, height);
                                let mut to_watch = self.get_broadcasted_holder_watch_outputs(holder_tx, tx);
                                append_onchain_update!(res, to_watch);
-                       }
-               }
-
-               macro_rules! fail_dust_htlcs_after_threshold_conf {
-                       ($holder_tx: expr, $commitment_tx: expr) => {
-                               for &(ref htlc, _, ref source) in &$holder_tx.htlc_outputs {
-                                       if htlc.transaction_output_index.is_none() {
-                                               if let &Some(ref source) = source {
-                                                       self.onchain_events_awaiting_threshold_conf.retain(|ref entry| {
-                                                               if entry.height != height { return true; }
-                                                               match entry.event {
-                                                                       OnchainEvent::HTLCUpdate { source: ref update_source, .. } => {
-                                                                               update_source != source
-                                                                       },
-                                                                       _ => true,
-                                                               }
-                                                       });
-                                                       let entry = OnchainEventEntry {
-                                                               txid: commitment_txid,
-                                                               height,
-                                                               event: OnchainEvent::HTLCUpdate {
-                                                                       source: source.clone(), payment_hash: htlc.payment_hash,
-                                                                       onchain_value_satoshis: Some(htlc.amount_msat / 1000)
-                                                               },
-                                                       };
-                                                       log_trace!(logger, "Failing HTLC with payment_hash {} from {} holder commitment tx due to broadcast of transaction, waiting confirmation (at height{})",
-                                                               log_bytes!(htlc.payment_hash.0), $commitment_tx, entry.confirmation_threshold());
-                                                       self.onchain_events_awaiting_threshold_conf.push(entry);
-                                               }
-                                       }
-                               }
+                               fail_unbroadcast_htlcs!(self, "previous holder", height, holder_tx.htlc_outputs.iter().map(|(a, _, c)| (a, c.as_ref())), logger);
                        }
                }
 
                if is_holder_tx {
-                       fail_dust_htlcs_after_threshold_conf!(self.current_holder_commitment_tx, "latest");
-                       if let &Some(ref holder_tx) = &self.prev_holder_signed_commitment_tx {
-                               fail_dust_htlcs_after_threshold_conf!(holder_tx, "previous");
-                       }
                }
 
                (claim_requests, (commitment_txid, watch_outputs))
@@ -1893,7 +1913,6 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
                                        L::Target: Logger,
        {
                let block_hash = header.block_hash();
-               log_trace!(logger, "New best block {} at height {}", block_hash, height);
                self.best_block = BestBlock::new(block_hash, height);
 
                self.transactions_confirmed(header, txdata, height, broadcaster, fee_estimator, logger)
@@ -1913,7 +1932,6 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
                L::Target: Logger,
        {
                let block_hash = header.block_hash();
-               log_trace!(logger, "New best block {} at height {}", block_hash, height);
 
                if height > self.best_block.height() {
                        self.best_block = BestBlock::new(block_hash, height);
@@ -1951,7 +1969,6 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
                }
 
                let block_hash = header.block_hash();
-               log_trace!(logger, "Block {} at height {} connected with {} txn matched", block_hash, height, txn_matched.len());
 
                let mut watch_outputs = Vec::new();
                let mut claimable_outpoints = Vec::new();
@@ -2024,6 +2041,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
                F::Target: FeeEstimator,
                L::Target: Logger,
        {
+               log_trace!(logger, "Processing {} matched transactions for block at height {}.", txn_matched.len(), conf_height);
                debug_assert!(self.best_block.height() >= conf_height);
 
                let should_broadcast = self.should_broadcast_holder_commitment_txn(logger);
@@ -2463,7 +2481,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
                                }));
                                break;
                        }
-                       if outp.script_pubkey == self.shutdown_script {
+                       if self.shutdown_script.as_ref() == Some(&outp.script_pubkey) {
                                spendable_output = Some(SpendableOutputDescriptor::StaticOutput {
                                        outpoint: OutPoint { txid: tx.txid(), index: i as u16 },
                                        output: outp.clone(),
@@ -2600,7 +2618,10 @@ impl<'a, Signer: Sign, K: KeysInterface<Signer = Signer>> ReadableArgs<&'a K>
                        _ => return Err(DecodeError::InvalidValue),
                };
                let counterparty_payment_script = Readable::read(reader)?;
-               let shutdown_script = Readable::read(reader)?;
+               let shutdown_script = {
+                       let script = <Script as Readable>::read(reader)?;
+                       if script.is_empty() { None } else { Some(script) }
+               };
 
                let channel_keys_id = Readable::read(reader)?;
                let holder_revocation_basepoint = Readable::read(reader)?;
@@ -2733,7 +2754,9 @@ impl<'a, Signer: Sign, K: KeysInterface<Signer = Signer>> ReadableArgs<&'a K>
                let waiting_threshold_conf_len: u64 = Readable::read(reader)?;
                let mut onchain_events_awaiting_threshold_conf = Vec::with_capacity(cmp::min(waiting_threshold_conf_len as usize, MAX_ALLOC_SIZE / 128));
                for _ in 0..waiting_threshold_conf_len {
-                       onchain_events_awaiting_threshold_conf.push(Readable::read(reader)?);
+                       if let Some(val) = MaybeReadable::read(reader)? {
+                               onchain_events_awaiting_threshold_conf.push(val);
+                       }
                }
 
                let outputs_to_watch_len: u64 = Readable::read(reader)?;
@@ -2832,6 +2855,7 @@ mod tests {
        use ln::{PaymentPreimage, PaymentHash};
        use ln::chan_utils;
        use ln::chan_utils::{HTLCOutputInCommitment, ChannelPublicKeys, ChannelTransactionParameters, HolderCommitmentTransaction, CounterpartyChannelTransactionParameters};
+       use ln::script::ShutdownScript;
        use util::test_utils::{TestLogger, TestBroadcaster, TestFeeEstimator};
        use bitcoin::secp256k1::key::{SecretKey,PublicKey};
        use bitcoin::secp256k1::Secp256k1;
@@ -2925,9 +2949,10 @@ mod tests {
                };
                // Prune with one old state and a holder commitment tx holding a few overlaps with the
                // old state.
+               let shutdown_pubkey = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
                let best_block = BestBlock::from_genesis(Network::Testnet);
                let monitor = ChannelMonitor::new(Secp256k1::new(), keys,
-                                                 &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap()), 0, &Script::new(),
+                                                 Some(ShutdownScript::new_p2wpkh_from_pubkey(shutdown_pubkey).into_inner()), 0, &Script::new(),
                                                  (OutPoint { txid: Txid::from_slice(&[43; 32]).unwrap(), index: 0 }, Script::new()),
                                                  &channel_parameters,
                                                  Script::new(), 46, 0,