Merge pull request #1504 from TheBlueMatt/2022-05-pub-io
[rust-lightning] / lightning / src / ln / monitor_tests.rs
index af42644dd710164a8c1a633ef54975aa2a238873..492a65537701750f7a97c56ac6cb287da395aac0 100644 (file)
@@ -20,6 +20,7 @@ use util::events::{Event, MessageSendEvent, MessageSendEventsProvider, ClosureRe
 use bitcoin::blockdata::script::Builder;
 use bitcoin::blockdata::opcodes;
 use bitcoin::secp256k1::Secp256k1;
+use bitcoin::Transaction;
 
 use prelude::*;
 
@@ -82,6 +83,17 @@ fn chanmon_fail_from_stale_commitment() {
        expect_payment_failed_with_update!(nodes[0], payment_hash, false, update_a.contents.short_channel_id, true);
 }
 
+fn test_spendable_output<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, spendable_tx: &Transaction) {
+       let mut spendable = node.chain_monitor.chain_monitor.get_and_clear_pending_events();
+       assert_eq!(spendable.len(), 1);
+       if let Event::SpendableOutputs { outputs } = spendable.pop().unwrap() {
+               assert_eq!(outputs.len(), 1);
+               let spend_tx = node.keys_manager.backing.spend_spendable_outputs(&[&outputs[0]], Vec::new(),
+                       Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(), 253, &Secp256k1::new()).unwrap();
+               check_spends!(spend_tx, spendable_tx);
+       } else { panic!(); }
+}
+
 #[test]
 fn chanmon_claim_value_coop_close() {
        // Tests `get_claimable_balances` returns the correct values across a simple cooperative claim.
@@ -155,23 +167,9 @@ fn chanmon_claim_value_coop_close() {
        assert_eq!(Vec::<Balance>::new(),
                nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
 
-       let mut node_a_spendable = nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events();
-       assert_eq!(node_a_spendable.len(), 1);
-       if let Event::SpendableOutputs { outputs } = node_a_spendable.pop().unwrap() {
-               assert_eq!(outputs.len(), 1);
-               let spend_tx = nodes[0].keys_manager.backing.spend_spendable_outputs(&[&outputs[0]], Vec::new(),
-                       Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(), 253, &Secp256k1::new()).unwrap();
-               check_spends!(spend_tx, shutdown_tx[0]);
-       }
+       test_spendable_output(&nodes[0], &shutdown_tx[0]);
+       test_spendable_output(&nodes[1], &shutdown_tx[0]);
 
-       let mut node_b_spendable = nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events();
-       assert_eq!(node_b_spendable.len(), 1);
-       if let Event::SpendableOutputs { outputs } = node_b_spendable.pop().unwrap() {
-               assert_eq!(outputs.len(), 1);
-               let spend_tx = nodes[1].keys_manager.backing.spend_spendable_outputs(&[&outputs[0]], Vec::new(),
-                       Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(), 253, &Secp256k1::new()).unwrap();
-               check_spends!(spend_tx, shutdown_tx[0]);
-       }
        check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure);
        check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure);
 }
@@ -203,7 +201,7 @@ fn do_test_claim_value_force_close(prev_commitment_tx: bool) {
        assert_eq!(funding_outpoint.to_channel_id(), chan_id);
 
        // This HTLC is immediately claimed, giving node B the preimage
-       let payment_preimage = route_payment(&nodes[0], &[&nodes[1]], 3_000_000).0;
+       let (payment_preimage, payment_hash, _) = route_payment(&nodes[0], &[&nodes[1]], 3_000_000);
        // This HTLC is allowed to time out, letting A claim it. However, in order to test claimable
        // balances more fully we also give B the preimage for this HTLC.
        let (timeout_payment_preimage, timeout_payment_hash, _) = route_payment(&nodes[0], &[&nodes[1]], 4_000_000);
@@ -236,13 +234,18 @@ fn do_test_claim_value_force_close(prev_commitment_tx: bool) {
 
        nodes[1].node.claim_funds(payment_preimage);
        check_added_monitors!(nodes[1], 1);
+       expect_payment_claimed!(nodes[1], payment_hash, 3_000_000);
+
        let b_htlc_msgs = get_htlc_update_msgs!(&nodes[1], nodes[0].node.get_our_node_id());
        // We claim the dust payment here as well, but it won't impact our claimable balances as its
        // dust and thus doesn't appear on chain at all.
        nodes[1].node.claim_funds(dust_payment_preimage);
        check_added_monitors!(nodes[1], 1);
+       expect_payment_claimed!(nodes[1], dust_payment_hash, 3_000);
+
        nodes[1].node.claim_funds(timeout_payment_preimage);
        check_added_monitors!(nodes[1], 1);
+       expect_payment_claimed!(nodes[1], timeout_payment_hash, 4_000_000);
 
        if prev_commitment_tx {
                // To build a previous commitment transaction, deliver one round of commitment messages.
@@ -384,15 +387,7 @@ fn do_test_claim_value_force_close(prev_commitment_tx: bool) {
                }]),
                sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
 
-       let mut node_a_spendable = nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events();
-       assert_eq!(node_a_spendable.len(), 1);
-       if let Event::SpendableOutputs { outputs } = node_a_spendable.pop().unwrap() {
-               assert_eq!(outputs.len(), 1);
-               let spend_tx = nodes[0].keys_manager.backing.spend_spendable_outputs(&[&outputs[0]], Vec::new(),
-                       Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(), 253, &Secp256k1::new()).unwrap();
-               check_spends!(spend_tx, remote_txn[0]);
-       }
-
+       test_spendable_output(&nodes[0], &remote_txn[0]);
        assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
 
        // After broadcasting the HTLC claim transaction, node A will still consider the HTLC
@@ -449,14 +444,7 @@ fn do_test_claim_value_force_close(prev_commitment_tx: bool) {
                nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
        expect_payment_failed!(nodes[0], timeout_payment_hash, true);
 
-       let mut node_a_spendable = nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events();
-       assert_eq!(node_a_spendable.len(), 1);
-       if let Event::SpendableOutputs { outputs } = node_a_spendable.pop().unwrap() {
-               assert_eq!(outputs.len(), 1);
-               let spend_tx = nodes[0].keys_manager.backing.spend_spendable_outputs(&[&outputs[0]], Vec::new(),
-                       Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(), 253, &Secp256k1::new()).unwrap();
-               check_spends!(spend_tx, a_broadcast_txn[2]);
-       } else { panic!(); }
+       test_spendable_output(&nodes[0], &a_broadcast_txn[2]);
 
        // Node B will no longer consider the HTLC "contentious" after the HTLC claim transaction
        // confirms, and consider it simply "awaiting confirmations". Note that it has to wait for the
@@ -479,15 +467,7 @@ fn do_test_claim_value_force_close(prev_commitment_tx: bool) {
        // After reaching the commitment output CSV, we'll get a SpendableOutputs event for it and have
        // only the HTLCs claimable on node B.
        connect_blocks(&nodes[1], node_b_commitment_claimable - nodes[1].best_block_info().1);
-
-       let mut node_b_spendable = nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events();
-       assert_eq!(node_b_spendable.len(), 1);
-       if let Event::SpendableOutputs { outputs } = node_b_spendable.pop().unwrap() {
-               assert_eq!(outputs.len(), 1);
-               let spend_tx = nodes[1].keys_manager.backing.spend_spendable_outputs(&[&outputs[0]], Vec::new(),
-                       Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(), 253, &Secp256k1::new()).unwrap();
-               check_spends!(spend_tx, remote_txn[0]);
-       }
+       test_spendable_output(&nodes[1], &remote_txn[0]);
 
        assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
                        claimable_amount_satoshis: 3_000,
@@ -501,15 +481,7 @@ fn do_test_claim_value_force_close(prev_commitment_tx: bool) {
        // After reaching the claimed HTLC output CSV, we'll get a SpendableOutptus event for it and
        // have only one HTLC output left spendable.
        connect_blocks(&nodes[1], node_b_htlc_claimable - nodes[1].best_block_info().1);
-
-       let mut node_b_spendable = nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events();
-       assert_eq!(node_b_spendable.len(), 1);
-       if let Event::SpendableOutputs { outputs } = node_b_spendable.pop().unwrap() {
-               assert_eq!(outputs.len(), 1);
-               let spend_tx = nodes[1].keys_manager.backing.spend_spendable_outputs(&[&outputs[0]], Vec::new(),
-                       Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(), 253, &Secp256k1::new()).unwrap();
-               check_spends!(spend_tx, b_broadcast_txn[0]);
-       } else { panic!(); }
+       test_spendable_output(&nodes[1], &b_broadcast_txn[0]);
 
        assert_eq!(vec![Balance::ContentiousClaimable {
                        claimable_amount_satoshis: 4_000,
@@ -580,9 +552,10 @@ fn test_balances_on_local_commitment_htlcs() {
 
        expect_pending_htlcs_forwardable!(nodes[1]);
        expect_payment_received!(nodes[1], payment_hash_2, payment_secret_2, 20_000_000);
-       assert!(nodes[1].node.claim_funds(payment_preimage_2));
+       nodes[1].node.claim_funds(payment_preimage_2);
        get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
        check_added_monitors!(nodes[1], 1);
+       expect_payment_claimed!(nodes[1], payment_hash_2, 20_000_000);
 
        let chan_feerate = get_feerate!(nodes[0], chan_id) as u64;
        let opt_anchors = get_opt_anchors!(nodes[0], chan_id);
@@ -704,25 +677,11 @@ fn test_balances_on_local_commitment_htlcs() {
                        confirmation_height: node_a_htlc_claimable,
                }],
                nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
-       let mut node_a_spendable = nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events();
-       assert_eq!(node_a_spendable.len(), 1);
-       if let Event::SpendableOutputs { outputs } = node_a_spendable.pop().unwrap() {
-               assert_eq!(outputs.len(), 1);
-               let spend_tx = nodes[0].keys_manager.backing.spend_spendable_outputs(&[&outputs[0]], Vec::new(),
-                       Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(), 253, &Secp256k1::new()).unwrap();
-               check_spends!(spend_tx, as_txn[0]);
-       }
+       test_spendable_output(&nodes[0], &as_txn[0]);
 
        // Connect blocks until the HTLC-Timeout's CSV expires, providing us the relevant
        // `SpendableOutputs` event and removing the claimable balance entry.
        connect_blocks(&nodes[0], node_a_htlc_claimable - nodes[0].best_block_info().1);
        assert!(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
-       let mut node_a_spendable = nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events();
-       assert_eq!(node_a_spendable.len(), 1);
-       if let Event::SpendableOutputs { outputs } = node_a_spendable.pop().unwrap() {
-               assert_eq!(outputs.len(), 1);
-               let spend_tx = nodes[0].keys_manager.backing.spend_spendable_outputs(&[&outputs[0]], Vec::new(),
-                       Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(), 253, &Secp256k1::new()).unwrap();
-               check_spends!(spend_tx, as_txn[1]);
-       }
+       test_spendable_output(&nodes[0], &as_txn[1]);
 }