Clean up `TestKeysInterface` random bytes override interface
[rust-lightning] / lightning / src / ln / functional_tests.rs
index fc527ddce954fe0dc6dcb9ec693b741ee8424270..00db1f5ca552bd1942807a9b0f03c5ab53d173dc 100644 (file)
@@ -6001,7 +6001,7 @@ fn bolt2_open_channel_sending_node_checks_part1() { //This test needs to be on i
        let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
        //Force duplicate channel ids
        for node in nodes.iter() {
-               *node.keys_manager.override_channel_id_priv.lock().unwrap() = Some([0; 32]);
+               *node.keys_manager.override_random_bytes.lock().unwrap() = Some([0; 32]);
        }
 
        // BOLT #2 spec: Sending node must ensure temporary_channel_id is unique from any other channel ID with the same peer.
@@ -6010,9 +6010,10 @@ fn bolt2_open_channel_sending_node_checks_part1() { //This test needs to be on i
        nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), channel_value_satoshis, push_msat, 42, None).unwrap();
        let node0_to_1_send_open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
        nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), InitFeatures::known(), &node0_to_1_send_open_channel);
+       get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
 
        //Create a second channel with a channel_id collision
-       assert!(nodes[0].node.create_channel(nodes[0].node.get_our_node_id(), channel_value_satoshis, push_msat, 42, None).is_err());
+       assert!(nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), channel_value_satoshis, push_msat, 42, None).is_err());
 }
 
 #[test]
@@ -8190,6 +8191,43 @@ fn test_bump_txn_sanitize_tracking_maps() {
        }
 }
 
+#[test]
+fn test_pending_claimed_htlc_no_balance_underflow() {
+       // Tests that if we have a pending outbound HTLC as well as a claimed-but-not-fully-removed
+       // HTLC we will not underflow when we call `Channel::get_balance_msat()`.
+       let chanmon_cfgs = create_chanmon_cfgs(2);
+       let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
+       let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
+       let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
+       create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100_000, 0, InitFeatures::known(), InitFeatures::known());
+
+       let payment_preimage = route_payment(&nodes[0], &[&nodes[1]], 1_010_000).0;
+       nodes[1].node.claim_funds(payment_preimage);
+       check_added_monitors!(nodes[1], 1);
+       let fulfill_ev = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
+
+       nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &fulfill_ev.update_fulfill_htlcs[0]);
+       expect_payment_sent_without_paths!(nodes[0], payment_preimage);
+       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &fulfill_ev.commitment_signed);
+       check_added_monitors!(nodes[0], 1);
+       let (_raa, _cs) = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
+
+       // At this point nodes[1] has received 1,010k msat (10k msat more than their reserve) and can
+       // send an HTLC back (though it will go in the holding cell). Send an HTLC back and check we
+       // can get our balance.
+
+       // Get a route from nodes[1] to nodes[0] by getting a route going the other way and then flip
+       // the public key of the only hop. This works around ChannelDetails not showing the
+       // almost-claimed HTLC as available balance.
+       let (mut route, _, _, _) = get_route_and_payment_hash!(nodes[0], nodes[1], 10_000);
+       route.payment_params = None; // This is all wrong, but unnecessary
+       route.paths[0][0].pubkey = nodes[0].node.get_our_node_id();
+       let (_, payment_hash_2, payment_secret_2) = get_payment_preimage_hash!(nodes[0]);
+       nodes[1].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2)).unwrap();
+
+       assert_eq!(nodes[1].node.list_channels()[0].balance_msat, 1_000_000);
+}
+
 #[test]
 fn test_channel_conf_timeout() {
        // Tests that, for inbound channels, we give up on them if the funding transaction does not
@@ -8263,6 +8301,207 @@ fn test_override_0msat_htlc_minimum() {
        assert_eq!(res.htlc_minimum_msat, 1);
 }
 
+#[test]
+fn test_manually_accept_inbound_channel_request() {
+       let mut manually_accept_conf = UserConfig::default();
+       manually_accept_conf.manually_accept_inbound_channels = true;
+       let chanmon_cfgs = create_chanmon_cfgs(2);
+       let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
+       let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(manually_accept_conf.clone())]);
+       let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
+
+       nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, Some(manually_accept_conf)).unwrap();
+       let res = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
+
+       nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), InitFeatures::known(), &res);
+
+       // Assert that `nodes[1]` has no `MessageSendEvent::SendAcceptChannel` in `msg_events` before
+       // accepting the inbound channel request.
+       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
+
+       let events = nodes[1].node.get_and_clear_pending_events();
+       match events[0] {
+               Event::OpenChannelRequest { temporary_channel_id, .. } => {
+                       nodes[1].node.accept_inbound_channel(&temporary_channel_id).unwrap();
+               }
+               _ => panic!("Unexpected event"),
+       }
+
+       let accept_msg_ev = nodes[1].node.get_and_clear_pending_msg_events();
+       assert_eq!(accept_msg_ev.len(), 1);
+
+       match accept_msg_ev[0] {
+               MessageSendEvent::SendAcceptChannel { ref node_id, .. } => {
+                       assert_eq!(*node_id, nodes[0].node.get_our_node_id());
+               }
+               _ => panic!("Unexpected event"),
+       }
+}
+
+#[test]
+fn test_manually_reject_inbound_channel_request() {
+       let mut manually_accept_conf = UserConfig::default();
+       manually_accept_conf.manually_accept_inbound_channels = true;
+       let chanmon_cfgs = create_chanmon_cfgs(2);
+       let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
+       let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(manually_accept_conf.clone())]);
+       let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
+
+       nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, Some(manually_accept_conf)).unwrap();
+       let res = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
+
+       nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), InitFeatures::known(), &res);
+
+       // Assert that `nodes[1]` has no `MessageSendEvent::SendAcceptChannel` in `msg_events` before
+       // rejecting the inbound channel request.
+       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
+
+       let events = nodes[1].node.get_and_clear_pending_events();
+       match events[0] {
+               Event::OpenChannelRequest { temporary_channel_id, .. } => {
+                       nodes[1].node.force_close_channel(&temporary_channel_id).unwrap();
+               }
+               _ => panic!("Unexpected event"),
+       }
+
+       let close_msg_ev = nodes[1].node.get_and_clear_pending_msg_events();
+       assert_eq!(close_msg_ev.len(), 1);
+
+       match close_msg_ev[0] {
+               MessageSendEvent::HandleError { ref node_id, .. } => {
+                       assert_eq!(*node_id, nodes[0].node.get_our_node_id());
+               }
+               _ => panic!("Unexpected event"),
+       }
+       check_closed_event!(nodes[1], 1, ClosureReason::HolderForceClosed);
+}
+
+#[test]
+fn test_reject_funding_before_inbound_channel_accepted() {
+       // This tests that when `UserConfig::manually_accept_inbound_channels` is set to true, inbound
+       // channels must to be manually accepted through `ChannelManager::accept_inbound_channel` by
+       // the node operator before the counterparty sends a `FundingCreated` message. If a
+       // `FundingCreated` message is received before the channel is accepted, it should be rejected
+       // and the channel should be closed.
+       let mut manually_accept_conf = UserConfig::default();
+       manually_accept_conf.manually_accept_inbound_channels = true;
+       let chanmon_cfgs = create_chanmon_cfgs(2);
+       let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
+       let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(manually_accept_conf.clone())]);
+       let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
+
+       nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, Some(manually_accept_conf)).unwrap();
+       let res = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
+       let temp_channel_id = res.temporary_channel_id;
+
+       nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), InitFeatures::known(), &res);
+
+       // Assert that `nodes[1]` has no `MessageSendEvent::SendAcceptChannel` in the `msg_events`.
+       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
+
+       // Clear the `Event::OpenChannelRequest` event without responding to the request.
+       nodes[1].node.get_and_clear_pending_events();
+
+       // Get the `AcceptChannel` message of `nodes[1]` without calling
+       // `ChannelManager::accept_inbound_channel`, which generates a
+       // `MessageSendEvent::SendAcceptChannel` event. The message is passed to `nodes[0]`
+       // `handle_accept_channel`, which is required in order for `create_funding_transaction` to
+       // succeed when `nodes[0]` is passed to it.
+       {
+               let mut lock;
+               let channel = get_channel_ref!(&nodes[1], lock, temp_channel_id);
+               let accept_chan_msg = channel.get_accept_channel_message();
+               nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), InitFeatures::known(), &accept_chan_msg);
+       }
+
+       let (temporary_channel_id, tx, _) = create_funding_transaction(&nodes[0], 100000, 42);
+
+       nodes[0].node.funding_transaction_generated(&temporary_channel_id, tx.clone()).unwrap();
+       let funding_created_msg = get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, nodes[1].node.get_our_node_id());
+
+       // The `funding_created_msg` should be rejected by `nodes[1]` as it hasn't accepted the channel
+       nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &funding_created_msg);
+
+       let close_msg_ev = nodes[1].node.get_and_clear_pending_msg_events();
+       assert_eq!(close_msg_ev.len(), 1);
+
+       let expected_err = "FundingCreated message received before the channel was accepted";
+       match close_msg_ev[0] {
+               MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg }, ref node_id, } => {
+                       assert_eq!(msg.channel_id, temp_channel_id);
+                       assert_eq!(*node_id, nodes[0].node.get_our_node_id());
+                       assert_eq!(msg.data, expected_err);
+               }
+               _ => panic!("Unexpected event"),
+       }
+
+       check_closed_event!(nodes[1], 1, ClosureReason::ProcessingError { err: expected_err.to_string() });
+}
+
+#[test]
+fn test_can_not_accept_inbound_channel_twice() {
+       let mut manually_accept_conf = UserConfig::default();
+       manually_accept_conf.manually_accept_inbound_channels = true;
+       let chanmon_cfgs = create_chanmon_cfgs(2);
+       let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
+       let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(manually_accept_conf.clone())]);
+       let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
+
+       nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, Some(manually_accept_conf)).unwrap();
+       let res = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
+
+       nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), InitFeatures::known(), &res);
+
+       // Assert that `nodes[1]` has no `MessageSendEvent::SendAcceptChannel` in `msg_events` before
+       // accepting the inbound channel request.
+       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
+
+       let events = nodes[1].node.get_and_clear_pending_events();
+       match events[0] {
+               Event::OpenChannelRequest { temporary_channel_id, .. } => {
+                       nodes[1].node.accept_inbound_channel(&temporary_channel_id).unwrap();
+                       let api_res = nodes[1].node.accept_inbound_channel(&temporary_channel_id);
+                       match api_res {
+                               Err(APIError::APIMisuseError { err }) => {
+                                       assert_eq!(err, "The channel isn't currently awaiting to be accepted.");
+                               },
+                               Ok(_) => panic!("Channel shouldn't be possible to be accepted twice"),
+                               Err(_) => panic!("Unexpected Error"),
+                       }
+               }
+               _ => panic!("Unexpected event"),
+       }
+
+       // Ensure that the channel wasn't closed after attempting to accept it twice.
+       let accept_msg_ev = nodes[1].node.get_and_clear_pending_msg_events();
+       assert_eq!(accept_msg_ev.len(), 1);
+
+       match accept_msg_ev[0] {
+               MessageSendEvent::SendAcceptChannel { ref node_id, .. } => {
+                       assert_eq!(*node_id, nodes[0].node.get_our_node_id());
+               }
+               _ => panic!("Unexpected event"),
+       }
+}
+
+#[test]
+fn test_can_not_accept_unknown_inbound_channel() {
+       let chanmon_cfg = create_chanmon_cfgs(1);
+       let node_cfg = create_node_cfgs(1, &chanmon_cfg);
+       let node_chanmgr = create_node_chanmgrs(1, &node_cfg, &[None]);
+       let node = create_network(1, &node_cfg, &node_chanmgr)[0].node;
+
+       let unknown_channel_id = [0; 32];
+       let api_res = node.accept_inbound_channel(&unknown_channel_id);
+       match api_res {
+               Err(APIError::ChannelUnavailable { err }) => {
+                       assert_eq!(err, "Can't accept a channel that doesn't exist");
+               },
+               Ok(_) => panic!("It shouldn't be possible to accept an unkown channel"),
+               Err(_) => panic!("Unexpected Error"),
+       }
+}
+
 #[test]
 fn test_simple_mpp() {
        // Simple test of sending a multi-path payment.
@@ -9363,6 +9602,77 @@ fn test_forwardable_regen() {
        claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], payment_preimage_2);
 }
 
+#[test]
+fn test_dup_htlc_second_fail_panic() {
+       // Previously, if we received two HTLCs back-to-back, where the second overran the expected
+       // value for the payment, we'd fail back both HTLCs after generating a `PaymentReceived` event.
+       // Then, if the user failed the second payment, they'd hit a "tried to fail an already failed
+       // HTLC" debug panic. This tests for this behavior, checking that only one HTLC is auto-failed.
+       let chanmon_cfgs = create_chanmon_cfgs(2);
+       let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
+       let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
+       let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
+
+       let _chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001, InitFeatures::known(), InitFeatures::known());
+
+       let payment_params = PaymentParameters::from_node_id(nodes[1].node.get_our_node_id())
+               .with_features(InvoiceFeatures::known());
+       let scorer = test_utils::TestScorer::with_penalty(0);
+       let route = get_route(
+               &nodes[0].node.get_our_node_id(), &payment_params, &nodes[0].network_graph,
+               Some(&nodes[0].node.list_usable_channels().iter().collect::<Vec<_>>()),
+               10_000, TEST_FINAL_CLTV, nodes[0].logger, &scorer).unwrap();
+
+       let (_, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(&nodes[1]);
+
+       {
+               nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).unwrap();
+               check_added_monitors!(nodes[0], 1);
+               let mut events = nodes[0].node.get_and_clear_pending_msg_events();
+               assert_eq!(events.len(), 1);
+               let mut payment_event = SendEvent::from_event(events.pop().unwrap());
+               nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
+               commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
+       }
+       expect_pending_htlcs_forwardable!(nodes[1]);
+       expect_payment_received!(nodes[1], our_payment_hash, our_payment_secret, 10_000);
+
+       {
+               nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).unwrap();
+               check_added_monitors!(nodes[0], 1);
+               let mut events = nodes[0].node.get_and_clear_pending_msg_events();
+               assert_eq!(events.len(), 1);
+               let mut payment_event = SendEvent::from_event(events.pop().unwrap());
+               nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
+               commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
+               // At this point, nodes[1] would notice it has too much value for the payment. It will
+               // assume the second is a privacy attack (no longer particularly relevant
+               // post-payment_secrets) and fail back the new HTLC. Previously, it'd also have failed back
+               // the first HTLC delivered above.
+       }
+
+       // Now we go fail back the first HTLC from the user end.
+       expect_pending_htlcs_forwardable_ignore!(nodes[1]);
+       nodes[1].node.process_pending_htlc_forwards();
+       nodes[1].node.fail_htlc_backwards(&our_payment_hash);
+
+       expect_pending_htlcs_forwardable_ignore!(nodes[1]);
+       nodes[1].node.process_pending_htlc_forwards();
+
+       check_added_monitors!(nodes[1], 1);
+       let fail_updates_1 = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
+       assert_eq!(fail_updates_1.update_fail_htlcs.len(), 2);
+
+       nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &fail_updates_1.update_fail_htlcs[0]);
+       nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &fail_updates_1.update_fail_htlcs[1]);
+       commitment_signed_dance!(nodes[0], nodes[1], fail_updates_1.commitment_signed, false);
+
+       let failure_events = nodes[0].node.get_and_clear_pending_events();
+       assert_eq!(failure_events.len(), 2);
+       if let Event::PaymentPathFailed { .. } = failure_events[0] {} else { panic!(); }
+       if let Event::PaymentPathFailed { .. } = failure_events[1] {} else { panic!(); }
+}
+
 #[test]
 fn test_keysend_payments_to_public_node() {
        let chanmon_cfgs = create_chanmon_cfgs(2);