Clean up repeated_id test a bit to test indirectly
[rust-lightning] / src / ln / functional_tests.rs
index 8c0fbded2f9016d0ba47466772aae92fd2448c5b..2bce6ecc58a74defc41353de43ca06ca2ce9abad 100644 (file)
@@ -21,6 +21,7 @@ use util::errors::APIError;
 use util::logger::Logger;
 use util::ser::{Writeable, Writer, ReadableArgs};
 use util::config::UserConfig;
+use util::rng;
 
 use bitcoin::util::hash::{BitcoinHash, Sha256dHash};
 use bitcoin::util::bip143;
@@ -6713,8 +6714,6 @@ fn test_update_add_htlc_bolt2_sender_exceed_max_htlc_value_in_flight() {
 // BOLT 2 Requirements for the Receiver when handling an update_add_htlc message.
 #[test]
 fn test_update_add_htlc_bolt2_receiver_check_amount_received_more_than_min() {
-       use super::msgs::HandleError;
-
        //BOLT2 Requirement: receiving an amount_msat equal to 0, OR less than its own htlc_minimum_msat -> SHOULD fail the channel.
        let mut nodes = create_network(2);
        let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000);
@@ -6731,7 +6730,7 @@ fn test_update_add_htlc_bolt2_receiver_check_amount_received_more_than_min() {
        let mut updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
        updates.update_add_htlcs[0].amount_msat = htlc_minimum_msat-1;
        let err = nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
-       if let Err(HandleError{err, action: _}) = err {
+       if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) = err {
                assert_eq!(err, "Remote side tried to send less than our minimum HTLC value");
        } else {
                assert!(false);
@@ -6742,8 +6741,6 @@ fn test_update_add_htlc_bolt2_receiver_check_amount_received_more_than_min() {
 
 #[test]
 fn test_update_add_htlc_bolt2_receiver_sender_can_afford_amount_sent() {
-       use super::msgs::HandleError;
-
        //BOLT2 Requirement: receiving an amount_msat that the sending node cannot afford at the current feerate_per_kw (while maintaining its channel reserve): SHOULD fail the channel
        let mut nodes = create_network(2);
        let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000);
@@ -6759,7 +6756,7 @@ fn test_update_add_htlc_bolt2_receiver_sender_can_afford_amount_sent() {
        updates.update_add_htlcs[0].amount_msat = 5000000-their_channel_reserve+1;
        let err = nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
 
-       if let Err(HandleError{err, action: _}) = err {
+       if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) = err {
                assert_eq!(err, "Remote HTLC add would put them over their reserve value");
        } else {
                assert!(false);
@@ -6771,9 +6768,7 @@ fn test_update_add_htlc_bolt2_receiver_sender_can_afford_amount_sent() {
 
 #[test]
 fn test_update_add_htlc_bolt2_receiver_check_max_htlc_limit() {
-       use util::rng;
        let secp_ctx = Secp256k1::new();
-       use super::msgs::HandleError;
 
        //BOLT 2 Requirement: if a sending node adds more than its max_accepted_htlcs HTLCs to its local commitment transaction: SHOULD fail the channel
        //BOLT 2 Requirement: MUST allow multiple HTLCs with the same payment_hash.
@@ -6809,7 +6804,7 @@ fn test_update_add_htlc_bolt2_receiver_check_max_htlc_limit() {
        msg.htlc_id = (super::channel::OUR_MAX_HTLCS) as u64;
        let err = nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &msg);
 
-       if let Err(HandleError{err, action: _}) = err {
+       if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) = err {
                assert_eq!(err, "Remote tried to push more than our max accepted HTLCs");
        } else {
                assert!(false);
@@ -6821,8 +6816,6 @@ fn test_update_add_htlc_bolt2_receiver_check_max_htlc_limit() {
 
 #[test]
 fn test_update_add_htlc_bolt2_receiver_check_max_in_flight_msat() {
-       use super::msgs::HandleError;
-
        //OR adds more than its max_htlc_value_in_flight_msat worth of offered HTLCs to its local commitment transaction: SHOULD fail the channel
        let mut nodes = create_network(2);
        let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 1000000);
@@ -6834,7 +6827,7 @@ fn test_update_add_htlc_bolt2_receiver_check_max_in_flight_msat() {
        updates.update_add_htlcs[0].amount_msat = nodes[1].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().their_max_htlc_value_in_flight_msat + 1;
        let err = nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
 
-       if let Err(HandleError{err, action: _}) = err {
+       if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) = err {
                assert_eq!(err,"Remote HTLC add would put them over their max HTLC value in flight");
        } else {
                assert!(false);
@@ -6846,8 +6839,6 @@ fn test_update_add_htlc_bolt2_receiver_check_max_in_flight_msat() {
 
 #[test]
 fn test_update_add_htlc_bolt2_receiver_check_cltv_expiry() {
-       use super::msgs::HandleError;
-
        //BOLT2 Requirement: if sending node sets cltv_expiry to greater or equal to 500000000: SHOULD fail the channel.
        let mut nodes = create_network(2);
        let _chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000);
@@ -6859,7 +6850,7 @@ fn test_update_add_htlc_bolt2_receiver_check_cltv_expiry() {
        updates.update_add_htlcs[0].cltv_expiry = 500000000;
        let err = nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
 
-       if let Err(HandleError{err, action: _}) = err {
+       if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) = err {
                assert_eq!(err,"Remote provided CLTV expiry in seconds instead of block height");
        } else {
                assert!(false);
@@ -6872,15 +6863,16 @@ fn test_update_add_htlc_bolt2_receiver_check_cltv_expiry() {
 #[test]
 fn test_update_add_htlc_bolt2_receiver_check_repeated_id_ignore() {
        //BOLT 2 requirement: if the sender did not previously acknowledge the commitment of that HTLC: MUST ignore a repeated id value after a reconnection.
+       // We test this by first testing that that repeated HTLCs pass commitment signature checks
+       // after disconnect and that non-sequential htlc_ids result in a channel failure.
        let mut nodes = create_network(2);
-       let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000);
-       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 3999999, TEST_FINAL_CLTV).unwrap();
+       create_announced_chan_between_nodes(&nodes, 0, 1);
+       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 1000000, TEST_FINAL_CLTV).unwrap();
        let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
        nodes[0].node.send_payment(route, our_payment_hash).unwrap();
        check_added_monitors!(nodes[0], 1);
        let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
-       let _ = nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
-       assert_eq!(nodes[1].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().next_remote_htlc_id, 1);
+       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]).unwrap();
 
        //Disconnect and Reconnect
        nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
@@ -6892,13 +6884,22 @@ fn test_update_add_htlc_bolt2_receiver_check_repeated_id_ignore() {
        let reestablish_2 = get_chan_reestablish_msgs!(nodes[1], nodes[0]);
        assert_eq!(reestablish_2.len(), 1);
        nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_2[0]).unwrap();
-       let _ = handle_chan_reestablish_msgs!(nodes[0], nodes[1]);
+       handle_chan_reestablish_msgs!(nodes[0], nodes[1]);
        nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &reestablish_1[0]).unwrap();
-       let _ = handle_chan_reestablish_msgs!(nodes[1], nodes[0]);
+       handle_chan_reestablish_msgs!(nodes[1], nodes[0]);
 
        //Resend HTLC
-       let _ = nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
-       assert_eq!(nodes[1].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().next_remote_htlc_id, 1);
+       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]).unwrap();
        assert_eq!(updates.commitment_signed.htlc_signatures.len(), 1);
-}
+       nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &updates.commitment_signed).unwrap();
+
+       let err = nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
+       if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) = err {
+               assert_eq!(err, "Remote skipped HTLC ID");
+       } else {
+               assert!(false);
+       }
 
+       assert!(nodes[1].node.list_channels().is_empty());
+       check_closed_broadcast!(nodes[1]);
+}