]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Add handle unkown peer test
authorViktor Tigerström <11711198+ViktorTigerstrom@users.noreply.github.com>
Mon, 13 Jun 2022 19:24:51 +0000 (21:24 +0200)
committerMatt Corallo <git@bluematt.me>
Mon, 9 Jan 2023 19:42:52 +0000 (19:42 +0000)
lightning/src/ln/channelmanager.rs

index 6c406fcf208252c7de592e08ed007fe0585c0396..7c7a6b1211632e8ceb03a1c583af803c956b3896 100644 (file)
@@ -7478,17 +7478,24 @@ where
 mod tests {
        use bitcoin::hashes::Hash;
        use bitcoin::hashes::sha256::Hash as Sha256;
+       use bitcoin::hashes::hex::FromHex;
+       use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
+       use bitcoin::secp256k1::ecdsa::Signature;
+       use bitcoin::secp256k1::ffi::Signature as FFISignature;
+       use bitcoin::blockdata::script::Script;
+       use bitcoin::Txid;
        use core::time::Duration;
        use core::sync::atomic::Ordering;
        use crate::ln::{PaymentPreimage, PaymentHash, PaymentSecret};
-       use crate::ln::channelmanager::{self, inbound_payment, PaymentId, PaymentSendFailure};
+       use crate::ln::channelmanager::{self, inbound_payment, PaymentId, PaymentSendFailure, InterceptId};
        use crate::ln::functional_test_utils::*;
        use crate::ln::msgs;
-       use crate::ln::msgs::ChannelMessageHandler;
+       use crate::ln::msgs::{ChannelMessageHandler, OptionalField};
        use crate::routing::router::{PaymentParameters, RouteParameters, find_route};
        use crate::util::errors::APIError;
        use crate::util::events::{Event, HTLCDestination, MessageSendEvent, MessageSendEventsProvider, ClosureReason};
        use crate::util::test_utils;
+       use crate::util::config::ChannelConfig;
        use crate::chain::keysinterface::{EntropySource, KeysInterface};
 
        #[test]
@@ -8049,6 +8056,226 @@ mod tests {
                check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure);
                check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure);
        }
+
+       fn check_unkown_peer_msg_event<'a, 'b: 'a, 'c: 'b>(node: &Node<'a, 'b, 'c>, closing_node_id: PublicKey, closed_channel_id: [u8; 32]){
+               let close_msg_ev = node.node.get_and_clear_pending_msg_events();
+               let expected_error_str = format!("Can't find a peer matching the passed counterparty node_id {}", closing_node_id);
+               match close_msg_ev[0] {
+                       MessageSendEvent::HandleError {
+                               ref node_id, action: msgs::ErrorAction::SendErrorMessage {
+                                       msg: msgs::ErrorMessage { ref channel_id, ref data }
+                               }
+                       } => {
+                               assert_eq!(*node_id, closing_node_id);
+                               assert_eq!(*data, expected_error_str);
+                               assert_eq!(*channel_id, closed_channel_id);
+                       }
+                       _ => panic!("Unexpected event"),
+               }
+       }
+
+       fn check_not_connected_to_peer_error<T>(res_err: Result<T, APIError>, expected_public_key: PublicKey) {
+               let expected_message = format!("Not connected to node: {}", expected_public_key);
+               check_api_misuse_error_message(expected_message, res_err)
+       }
+
+       fn check_unkown_peer_error<T>(res_err: Result<T, APIError>, expected_public_key: PublicKey) {
+               let expected_message = format!("Can't find a peer matching the passed counterparty node_id {}", expected_public_key);
+               check_api_misuse_error_message(expected_message, res_err)
+       }
+
+       fn check_api_misuse_error_message<T>(expected_err_message: String, res_err: Result<T, APIError>) {
+               match res_err {
+                       Err(APIError::APIMisuseError { err }) => {
+                               assert_eq!(err, expected_err_message);
+                       },
+                       Ok(_) => panic!("Unexpected Ok"),
+                       Err(_) => panic!("Unexpected Error"),
+               }
+       }
+
+       #[test]
+       fn test_api_calls_with_unkown_counterparty_node() {
+               // Tests that our API functions and message handlers that expects a `counterparty_node_id`
+               // as input, behaves as expected if the `counterparty_node_id` is an unkown peer in the
+               // `ChannelManager::per_peer_state` map.
+               let chanmon_cfg = create_chanmon_cfgs(2);
+               let node_cfg = create_node_cfgs(2, &chanmon_cfg);
+               let node_chanmgr = create_node_chanmgrs(2, &node_cfg, &[None, None]);
+               let nodes = create_network(2, &node_cfg, &node_chanmgr);
+
+               // Boilerplate code to produce `open_channel` and `accept_channel` msgs more densly than
+               // creating dummy ones.
+               nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 1_000_000, 500_000_000, 42, None).unwrap();
+               let open_channel_msg = 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(), channelmanager::provided_init_features(), &open_channel_msg);
+               let accept_channel_msg = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
+
+               // Dummy values
+               let channel_id = [4; 32];
+               let signature = Signature::from(unsafe { FFISignature::new() });
+               let unkown_public_key = PublicKey::from_secret_key(&Secp256k1::signing_only(), &SecretKey::from_slice(&[42; 32]).unwrap());
+               let intercept_id = InterceptId([0; 32]);
+
+               // Dummy msgs
+               let funding_created_msg = msgs::FundingCreated {
+                       temporary_channel_id: open_channel_msg.temporary_channel_id,
+                       funding_txid: Txid::from_hex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").unwrap(),
+                       funding_output_index: 0,
+                       signature: signature,
+               };
+
+               let funding_signed_msg = msgs::FundingSigned {
+                       channel_id: channel_id,
+                       signature: signature,
+               };
+
+               let channel_ready_msg = msgs::ChannelReady {
+                       channel_id: channel_id,
+                       next_per_commitment_point: unkown_public_key,
+                       short_channel_id_alias: None,
+               };
+
+               let announcement_signatures_msg = msgs::AnnouncementSignatures {
+                       channel_id: channel_id,
+                       short_channel_id: 0,
+                       node_signature: signature,
+                       bitcoin_signature: signature,
+               };
+
+               let channel_reestablish_msg = msgs::ChannelReestablish {
+                       channel_id: channel_id,
+                       next_local_commitment_number: 0,
+                       next_remote_commitment_number: 0,
+                       data_loss_protect: OptionalField::Absent,
+               };
+
+               let closing_signed_msg = msgs::ClosingSigned {
+                       channel_id: channel_id,
+                       fee_satoshis: 1000,
+                       signature: signature,
+                       fee_range: None,
+               };
+
+               let shutdown_msg = msgs::Shutdown {
+                       channel_id: channel_id,
+                       scriptpubkey: Script::new(),
+               };
+
+               let onion_routing_packet = msgs::OnionPacket {
+                       version: 255,
+                       public_key: Ok(unkown_public_key),
+                       hop_data: [1; 20*65],
+                       hmac: [2; 32]
+               };
+
+               let update_add_htlc_msg = msgs::UpdateAddHTLC {
+                       channel_id: channel_id,
+                       htlc_id: 0,
+                       amount_msat: 1000000,
+                       payment_hash: PaymentHash([1; 32]),
+                       cltv_expiry: 821716,
+                       onion_routing_packet
+               };
+
+               let commitment_signed_msg = msgs::CommitmentSigned {
+                       channel_id: channel_id,
+                       signature: signature,
+                       htlc_signatures: Vec::new(),
+               };
+
+               let update_fee_msg = msgs::UpdateFee {
+                       channel_id: channel_id,
+                       feerate_per_kw: 1000,
+               };
+
+               let malformed_update_msg = msgs::UpdateFailMalformedHTLC{
+                       channel_id: channel_id,
+                       htlc_id: 0,
+                       sha256_of_onion: [1; 32],
+                       failure_code: 0x8000,
+               };
+
+               let fulfill_update_msg = msgs::UpdateFulfillHTLC{
+                       channel_id: channel_id,
+                       htlc_id: 0,
+                       payment_preimage: PaymentPreimage([1; 32]),
+               };
+
+               let fail_update_msg = msgs::UpdateFailHTLC{
+                       channel_id: channel_id,
+                       htlc_id: 0,
+                       reason: msgs::OnionErrorPacket { data: Vec::new()},
+               };
+
+               let revoke_and_ack_msg = msgs::RevokeAndACK {
+                       channel_id: channel_id,
+                       per_commitment_secret: [1; 32],
+                       next_per_commitment_point: unkown_public_key,
+               };
+
+               // Test the API functions and message handlers.
+               check_not_connected_to_peer_error(nodes[0].node.create_channel(unkown_public_key, 1_000_000, 500_000_000, 42, None), unkown_public_key);
+
+               nodes[1].node.handle_open_channel(&unkown_public_key, channelmanager::provided_init_features(), &open_channel_msg);
+               check_unkown_peer_msg_event(&nodes[1], unkown_public_key, open_channel_msg.temporary_channel_id);
+
+               nodes[0].node.handle_accept_channel(&unkown_public_key, channelmanager::provided_init_features(), &accept_channel_msg);
+               check_unkown_peer_msg_event(&nodes[0], unkown_public_key, open_channel_msg.temporary_channel_id);
+
+               check_unkown_peer_error(nodes[0].node.accept_inbound_channel(&open_channel_msg.temporary_channel_id, &unkown_public_key, 42), unkown_public_key);
+               nodes[1].node.handle_funding_created(&unkown_public_key, &funding_created_msg);
+               check_unkown_peer_msg_event(&nodes[1], unkown_public_key, open_channel_msg.temporary_channel_id);
+
+               nodes[0].node.handle_funding_signed(&unkown_public_key, &funding_signed_msg);
+               check_unkown_peer_msg_event(&nodes[0], unkown_public_key, channel_id);
+
+               nodes[0].node.handle_channel_ready(&unkown_public_key, &channel_ready_msg);
+               check_unkown_peer_msg_event(&nodes[0], unkown_public_key, channel_id);
+
+               nodes[1].node.handle_announcement_signatures(&unkown_public_key, &announcement_signatures_msg);
+               check_unkown_peer_msg_event(&nodes[1], unkown_public_key, channel_id);
+
+               check_unkown_peer_error(nodes[0].node.close_channel(&channel_id, &unkown_public_key), unkown_public_key);
+
+               check_unkown_peer_error(nodes[0].node.force_close_broadcasting_latest_txn(&channel_id, &unkown_public_key), unkown_public_key);
+
+               check_unkown_peer_error(nodes[0].node.force_close_without_broadcasting_txn(&channel_id, &unkown_public_key), unkown_public_key);
+
+               check_unkown_peer_error(nodes[0].node.forward_intercepted_htlc(intercept_id, &channel_id, unkown_public_key, 1_000_000), unkown_public_key);
+
+               check_unkown_peer_error(nodes[0].node.update_channel_config(&unkown_public_key, &[channel_id], &ChannelConfig::default()), unkown_public_key);
+
+               nodes[0].node.handle_shutdown(&unkown_public_key, &channelmanager::provided_init_features(), &shutdown_msg);
+               check_unkown_peer_msg_event(&nodes[0], unkown_public_key, channel_id);
+
+               nodes[1].node.handle_closing_signed(&unkown_public_key, &closing_signed_msg);
+               check_unkown_peer_msg_event(&nodes[1], unkown_public_key, channel_id);
+
+               nodes[0].node.handle_channel_reestablish(&unkown_public_key, &channel_reestablish_msg);
+               check_unkown_peer_msg_event(&nodes[0], unkown_public_key, channel_id);
+
+               nodes[1].node.handle_update_add_htlc(&unkown_public_key, &update_add_htlc_msg);
+               check_unkown_peer_msg_event(&nodes[1], unkown_public_key, channel_id);
+
+               nodes[1].node.handle_commitment_signed(&unkown_public_key, &commitment_signed_msg);
+               check_unkown_peer_msg_event(&nodes[1], unkown_public_key, channel_id);
+
+               nodes[1].node.handle_update_fail_malformed_htlc(&unkown_public_key, &malformed_update_msg);
+               check_unkown_peer_msg_event(&nodes[1], unkown_public_key, channel_id);
+
+               nodes[1].node.handle_update_fail_htlc(&unkown_public_key, &fail_update_msg);
+               check_unkown_peer_msg_event(&nodes[1], unkown_public_key, channel_id);
+
+               nodes[1].node.handle_update_fulfill_htlc(&unkown_public_key, &fulfill_update_msg);
+               check_unkown_peer_msg_event(&nodes[1], unkown_public_key, channel_id);
+
+               nodes[1].node.handle_revoke_and_ack(&unkown_public_key, &revoke_and_ack_msg);
+               check_unkown_peer_msg_event(&nodes[1], unkown_public_key, channel_id);
+
+               nodes[1].node.handle_update_fee(&unkown_public_key, &update_fee_msg);
+               check_unkown_peer_msg_event(&nodes[1], unkown_public_key, channel_id);
+       }
 }
 
 #[cfg(all(any(test, feature = "_test_utils"), feature = "_bench_unstable"))]