Merge pull request #280 from TheBlueMatt/2018-12-no-to-remote-revoked-htlcs
[rust-lightning] / src / ln / functional_tests.rs
index 3a8e3b8619d94bc2eba717fc0f754b2ac4bebf9d..4baaf64d814b484d2e201a3e8cd8a89c7c21d99b 100644 (file)
@@ -2951,7 +2951,7 @@ fn test_simple_commitment_revoked_fail_backward() {
        }
 }
 
-fn do_test_commitment_revoked_fail_backward_exhaustive(deliver_bs_raa: bool) {
+fn do_test_commitment_revoked_fail_backward_exhaustive(deliver_bs_raa: bool, use_dust: bool, no_to_remote: bool) {
        // Test that if our counterparty broadcasts a revoked commitment transaction we fail all
        // pending HTLCs on that channel backwards even if the HTLCs aren't present in our latest
        // commitment transaction anymore.
@@ -2973,15 +2973,22 @@ fn do_test_commitment_revoked_fail_backward_exhaustive(deliver_bs_raa: bool) {
        create_announced_chan_between_nodes(&nodes, 0, 1);
        let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
 
-       let (payment_preimage, _payment_hash) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 3000000);
+       let (payment_preimage, _payment_hash) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], if no_to_remote { 10_000 } else { 3_000_000 });
        // Get the will-be-revoked local txn from nodes[2]
        let revoked_local_txn = nodes[2].node.channel_state.lock().unwrap().by_id.get(&chan_2.2).unwrap().last_local_commitment_txn.clone();
+       assert_eq!(revoked_local_txn[0].output.len(), if no_to_remote { 1 } else { 2 });
        // Revoke the old state
        claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], payment_preimage);
 
-       let (_, first_payment_hash) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 3000000);
-       let (_, second_payment_hash) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 3000000);
-       let (_, third_payment_hash) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 3000000);
+       let value = if use_dust {
+               // The dust limit applied to HTLC outputs considers the fee of the HTLC transaction as
+               // well, so HTLCs at exactly the dust limit will not be included in commitment txn.
+               nodes[2].node.channel_state.lock().unwrap().by_id.get(&chan_2.2).unwrap().our_dust_limit_satoshis * 1000
+       } else { 3000000 };
+
+       let (_, first_payment_hash) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], value);
+       let (_, second_payment_hash) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], value);
+       let (_, third_payment_hash) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], value);
 
        assert!(nodes[2].node.fail_htlc_backwards(&first_payment_hash, 0));
        expect_pending_htlcs_forwardable!(nodes[2]);
@@ -3043,8 +3050,8 @@ fn do_test_commitment_revoked_fail_backward_exhaustive(deliver_bs_raa: bool) {
 
        if deliver_bs_raa {
                nodes[1].node.handle_revoke_and_ack(&nodes[2].node.get_our_node_id(), &bs_raa).unwrap();
-               // One monitor for the new revocation preimage, one as we generate a commitment for
-               // nodes[0] to fail first_payment_hash backwards.
+               // One monitor for the new revocation preimage, no second on as we won't generate a new
+               // commitment transaction for nodes[0] until process_pending_htlc_forwards().
                check_added_monitors!(nodes[1], 1);
                let events = nodes[1].node.get_and_clear_pending_events();
                assert_eq!(events.len(), 1);
@@ -3152,9 +3159,19 @@ fn do_test_commitment_revoked_fail_backward_exhaustive(deliver_bs_raa: bool) {
 }
 
 #[test]
-fn test_commitment_revoked_fail_backward_exhaustive() {
-       do_test_commitment_revoked_fail_backward_exhaustive(false);
-       do_test_commitment_revoked_fail_backward_exhaustive(true);
+fn test_commitment_revoked_fail_backward_exhaustive_a() {
+       do_test_commitment_revoked_fail_backward_exhaustive(false, true, false);
+       do_test_commitment_revoked_fail_backward_exhaustive(true, true, false);
+       do_test_commitment_revoked_fail_backward_exhaustive(false, false, false);
+       do_test_commitment_revoked_fail_backward_exhaustive(true, false, false);
+}
+
+#[test]
+fn test_commitment_revoked_fail_backward_exhaustive_b() {
+       do_test_commitment_revoked_fail_backward_exhaustive(false, true, true);
+       do_test_commitment_revoked_fail_backward_exhaustive(true, true, true);
+       do_test_commitment_revoked_fail_backward_exhaustive(false, false, true);
+       do_test_commitment_revoked_fail_backward_exhaustive(true, false, true);
 }
 
 #[test]