Merge pull request #2101 from TheBlueMatt/2023-03-one-less-sig
[rust-lightning] / lightning / src / ln / monitor_tests.rs
index 3bd50293ff24aa2bdf36f1935590d8341213cbca..5bd2e87ba5c15ca3f1ef998bafc15b92e5c88c68 100644 (file)
@@ -16,6 +16,9 @@ use crate::chain::channelmonitor::LATENCY_GRACE_PERIOD_BLOCKS;
 use crate::chain::channelmonitor::{ANTI_REORG_DELAY, Balance};
 use crate::chain::transaction::OutPoint;
 use crate::chain::chaininterface::LowerBoundedFeeEstimator;
+#[cfg(anchors)]
+use crate::events::bump_transaction::BumpTransactionEvent;
+use crate::events::{Event, MessageSendEvent, MessageSendEventsProvider, ClosureReason, HTLCDestination};
 use crate::ln::channel;
 #[cfg(anchors)]
 use crate::ln::chan_utils;
@@ -28,9 +31,6 @@ use crate::util::config::UserConfig;
 #[cfg(anchors)]
 use crate::util::crypto::sign;
 #[cfg(anchors)]
-use crate::util::events::BumpTransactionEvent;
-use crate::util::events::{Event, MessageSendEvent, MessageSendEventsProvider, ClosureReason, HTLCDestination};
-#[cfg(anchors)]
 use crate::util::ser::Writeable;
 #[cfg(anchors)]
 use crate::util::test_utils;
@@ -1775,7 +1775,7 @@ fn test_yield_anchors_events() {
        let mut htlc_txs = Vec::with_capacity(2);
        for event in holder_events {
                match event {
-                       Event::BumpTransaction(BumpTransactionEvent::HTLCResolution { htlc_descriptors, .. }) => {
+                       Event::BumpTransaction(BumpTransactionEvent::HTLCResolution { htlc_descriptors, tx_lock_time, .. }) => {
                                assert_eq!(htlc_descriptors.len(), 1);
                                let htlc_descriptor = &htlc_descriptors[0];
                                let signer = nodes[0].keys_manager.derive_channel_keys(
@@ -1784,11 +1784,7 @@ fn test_yield_anchors_events() {
                                let per_commitment_point = signer.get_per_commitment_point(htlc_descriptor.per_commitment_number, &secp);
                                let mut htlc_tx = Transaction {
                                        version: 2,
-                                       lock_time: if htlc_descriptor.htlc.offered {
-                                               PackedLockTime(htlc_descriptor.htlc.cltv_expiry)
-                                       } else {
-                                               PackedLockTime::ZERO
-                                       },
+                                       lock_time: tx_lock_time,
                                        input: vec![
                                                htlc_descriptor.unsigned_tx_input(), // HTLC input
                                                TxIn { ..Default::default() } // Fee input
@@ -1859,15 +1855,18 @@ fn test_anchors_aggregated_revoked_htlc_tx() {
        let chan_a = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 20_000_000);
        let chan_b = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 20_000_000);
 
+       // Serialize Bob with the initial state of both channels, which we'll use later.
+       let bob_serialized = nodes[1].node.encode();
+
        // Route two payments for each channel from Alice to Bob to lock in the HTLCs.
        let payment_a = route_payment(&nodes[0], &[&nodes[1]], 50_000_000);
        let payment_b = route_payment(&nodes[0], &[&nodes[1]], 50_000_000);
        let payment_c = route_payment(&nodes[0], &[&nodes[1]], 50_000_000);
        let payment_d = route_payment(&nodes[0], &[&nodes[1]], 50_000_000);
 
-       // Serialize Bob with the HTLCs locked in. We'll restart Bob later on with the state at this
-       // point such that he broadcasts a revoked commitment transaction.
-       let bob_serialized = nodes[1].node.encode();
+       // Serialize Bob's monitors with the HTLCs locked in. We'll restart Bob later on with the state
+       // at this point such that he broadcasts a revoked commitment transaction with the HTLCs
+       // present.
        let bob_serialized_monitor_a = get_monitor!(nodes[1], chan_a.2).encode();
        let bob_serialized_monitor_b = get_monitor!(nodes[1], chan_b.2).encode();
 
@@ -1897,30 +1896,26 @@ fn test_anchors_aggregated_revoked_htlc_tx() {
                }
        }
 
-       // Bob force closes by broadcasting his revoked state for each channel.
-       nodes[1].node.force_close_broadcasting_latest_txn(&chan_a.2, &nodes[0].node.get_our_node_id()).unwrap();
-       check_added_monitors(&nodes[1], 1);
-       check_closed_broadcast(&nodes[1], 1, true);
-       check_closed_event!(&nodes[1], 1, ClosureReason::HolderForceClosed);
-       let revoked_commitment_a = {
-               let mut txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
-               assert_eq!(txn.len(), 1);
-               let revoked_commitment = txn.pop().unwrap();
-               assert_eq!(revoked_commitment.output.len(), 6); // 2 HTLC outputs + 1 to_self output + 1 to_remote output + 2 anchor outputs
-               check_spends!(revoked_commitment, chan_a.3);
-               revoked_commitment
-       };
-       nodes[1].node.force_close_broadcasting_latest_txn(&chan_b.2, &nodes[0].node.get_our_node_id()).unwrap();
-       check_added_monitors(&nodes[1], 1);
-       check_closed_broadcast(&nodes[1], 1, true);
-       check_closed_event!(&nodes[1], 1, ClosureReason::HolderForceClosed);
-       let revoked_commitment_b = {
-               let mut txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
-               assert_eq!(txn.len(), 1);
-               let revoked_commitment = txn.pop().unwrap();
-               assert_eq!(revoked_commitment.output.len(), 6); // 2 HTLC outputs + 1 to_self output + 1 to_remote output + 2 anchor outputs
-               check_spends!(revoked_commitment, chan_b.3);
-               revoked_commitment
+       // Bob force closes by restarting with the outdated state, prompting the ChannelMonitors to
+       // broadcast the latest commitment transaction known to them, which in our case is the one with
+       // the HTLCs still pending.
+       nodes[1].node.timer_tick_occurred();
+       check_added_monitors(&nodes[1], 2);
+       check_closed_event!(&nodes[1], 2, ClosureReason::OutdatedChannelManager);
+       let (revoked_commitment_a, revoked_commitment_b) = {
+               let txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
+               assert_eq!(txn.len(), 2);
+               assert_eq!(txn[0].output.len(), 6); // 2 HTLC outputs + 1 to_self output + 1 to_remote output + 2 anchor outputs
+               assert_eq!(txn[1].output.len(), 6); // 2 HTLC outputs + 1 to_self output + 1 to_remote output + 2 anchor outputs
+               if txn[0].input[0].previous_output.txid == chan_a.3.txid() {
+                       check_spends!(&txn[0], &chan_a.3);
+                       check_spends!(&txn[1], &chan_b.3);
+                       (txn[0].clone(), txn[1].clone())
+               } else {
+                       check_spends!(&txn[1], &chan_a.3);
+                       check_spends!(&txn[0], &chan_b.3);
+                       (txn[1].clone(), txn[0].clone())
+               }
        };
 
        // Bob should now receive two events to bump his revoked commitment transaction fees.
@@ -2064,7 +2059,7 @@ fn test_anchors_aggregated_revoked_htlc_tx() {
                };
                let mut descriptors = Vec::with_capacity(4);
                for event in events {
-                       if let Event::BumpTransaction(BumpTransactionEvent::HTLCResolution { mut htlc_descriptors, .. }) = event {
+                       if let Event::BumpTransaction(BumpTransactionEvent::HTLCResolution { mut htlc_descriptors, tx_lock_time, .. }) = event {
                                assert_eq!(htlc_descriptors.len(), 2);
                                for htlc_descriptor in &htlc_descriptors {
                                        assert!(!htlc_descriptor.htlc.offered);
@@ -2076,6 +2071,7 @@ fn test_anchors_aggregated_revoked_htlc_tx() {
                                        htlc_tx.output.push(htlc_descriptor.tx_output(&per_commitment_point, &secp));
                                }
                                descriptors.append(&mut htlc_descriptors);
+                               htlc_tx.lock_time = tx_lock_time;
                        } else {
                                panic!("Unexpected event");
                        }