@@ -7543,6 +7543,12 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
75437543mod tests {
75447544 use bitcoin:: hashes:: Hash ;
75457545 use bitcoin:: hashes:: sha256:: Hash as Sha256 ;
7546+ use bitcoin:: hashes:: hex:: FromHex ;
7547+ use bitcoin:: secp256k1:: { PublicKey , Secp256k1 , SecretKey } ;
7548+ use bitcoin:: secp256k1:: ecdsa:: Signature ;
7549+ use bitcoin:: secp256k1:: ffi:: Signature as FFISignature ;
7550+ use bitcoin:: blockdata:: script:: Script ;
7551+ use bitcoin:: Txid ;
75467552 use core:: time:: Duration ;
75477553 use core:: sync:: atomic:: Ordering ;
75487554 use ln:: { PaymentPreimage , PaymentHash , PaymentSecret } ;
@@ -7551,11 +7557,12 @@ mod tests {
75517557 use ln:: features:: InitFeatures ;
75527558 use ln:: functional_test_utils:: * ;
75537559 use ln:: msgs;
7554- use ln:: msgs:: ChannelMessageHandler ;
7560+ use ln:: msgs:: { ChannelMessageHandler , OptionalField } ;
75557561 use routing:: router:: { PaymentParameters , RouteParameters , find_route} ;
75567562 use util:: errors:: APIError ;
75577563 use util:: events:: { Event , MessageSendEvent , MessageSendEventsProvider , ClosureReason } ;
75587564 use util:: test_utils;
7565+ use util:: config:: ChannelConfig ;
75597566 use chain:: keysinterface:: KeysInterface ;
75607567
75617568 #[ cfg( feature = "std" ) ]
@@ -8156,6 +8163,213 @@ mod tests {
81568163 check_closed_event ! ( nodes[ 0 ] , 1 , ClosureReason :: CooperativeClosure ) ;
81578164 check_closed_event ! ( nodes[ 1 ] , 1 , ClosureReason :: CooperativeClosure ) ;
81588165 }
8166+
8167+ 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 ] ) {
8168+ let close_msg_ev = node. node . get_and_clear_pending_msg_events ( ) ;
8169+ let expected_error_str = format ! ( "Can't find a peer with a node_id matching the passed counterparty_node_id {}" , closing_node_id) ;
8170+ match close_msg_ev[ 0 ] {
8171+ MessageSendEvent :: HandleError {
8172+ ref node_id, action : msgs:: ErrorAction :: SendErrorMessage {
8173+ msg : msgs:: ErrorMessage { ref channel_id, ref data }
8174+ }
8175+ } => {
8176+ assert_eq ! ( * node_id, closing_node_id) ;
8177+ assert_eq ! ( * data, expected_error_str) ;
8178+ assert_eq ! ( * channel_id, closed_channel_id) ;
8179+ }
8180+ _ => panic ! ( "Unexpected event" ) ,
8181+ }
8182+ }
8183+
8184+ fn check_unkown_peer_error < T > ( res_err : Result < T , APIError > , expected_public_key : PublicKey ) {
8185+ match res_err {
8186+ Err ( APIError :: APIMisuseError { err } ) => {
8187+ assert_eq ! ( err, format!( "Can't find a peer with a node_id matching the passed counterparty_node_id {}" , expected_public_key) ) ;
8188+ } ,
8189+ Ok ( _) => panic ! ( "Unexpected Ok" ) ,
8190+ Err ( _) => panic ! ( "Unexpected Error" ) ,
8191+ }
8192+ }
8193+
8194+ #[ test]
8195+ fn test_api_calls_with_unkown_counterparty_node ( ) {
8196+ // Tests that our API functions and message handlers that expects a `counterparty_node_id`
8197+ // as input, behaves as expected if the `counterparty_node_id` is an unkown peer in the
8198+ // `ChannelManager::per_peer_state` map.
8199+ let chanmon_cfg = create_chanmon_cfgs ( 2 ) ;
8200+ let node_cfg = create_node_cfgs ( 2 , & chanmon_cfg) ;
8201+ let node_chanmgr = create_node_chanmgrs ( 2 , & node_cfg, & [ None , None ] ) ;
8202+ let nodes = create_network ( 2 , & node_cfg, & node_chanmgr) ;
8203+
8204+ // Boilerplate code to produce `open_channel` and `accept_channel` msgs more densly than
8205+ // creating dummy ones.
8206+ nodes[ 0 ] . node . create_channel ( nodes[ 1 ] . node . get_our_node_id ( ) , 1_000_000 , 500_000_000 , 42 , None ) . unwrap ( ) ;
8207+ let open_channel_msg = get_event_msg ! ( nodes[ 0 ] , MessageSendEvent :: SendOpenChannel , nodes[ 1 ] . node. get_our_node_id( ) ) ;
8208+ nodes[ 1 ] . node . handle_open_channel ( & nodes[ 0 ] . node . get_our_node_id ( ) , InitFeatures :: known ( ) , & open_channel_msg) ;
8209+ let accept_channel_msg = get_event_msg ! ( nodes[ 1 ] , MessageSendEvent :: SendAcceptChannel , nodes[ 0 ] . node. get_our_node_id( ) ) ;
8210+
8211+ // Dummy values
8212+ let channel_id = [ 4 ; 32 ] ;
8213+ let signature = Signature :: from ( unsafe { FFISignature :: new ( ) } ) ;
8214+ let unkown_public_key = PublicKey :: from_secret_key ( & Secp256k1 :: signing_only ( ) , & SecretKey :: from_slice ( & [ 42 ; 32 ] ) . unwrap ( ) ) ;
8215+
8216+ // Dummy msgs
8217+ let funding_created_msg = msgs:: FundingCreated {
8218+ temporary_channel_id : open_channel_msg. temporary_channel_id ,
8219+ funding_txid : Txid :: from_hex ( "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" ) . unwrap ( ) ,
8220+ funding_output_index : 0 ,
8221+ signature : signature,
8222+ } ;
8223+
8224+ let funding_signed_msg = msgs:: FundingSigned {
8225+ channel_id : channel_id,
8226+ signature : signature,
8227+ } ;
8228+
8229+ let channel_ready_msg = msgs:: ChannelReady {
8230+ channel_id : channel_id,
8231+ next_per_commitment_point : unkown_public_key,
8232+ short_channel_id_alias : None ,
8233+ } ;
8234+
8235+ let announcement_signatures_msg = msgs:: AnnouncementSignatures {
8236+ channel_id : channel_id,
8237+ short_channel_id : 0 ,
8238+ node_signature : signature,
8239+ bitcoin_signature : signature,
8240+ } ;
8241+
8242+ let channel_reestablish_msg = msgs:: ChannelReestablish {
8243+ channel_id : channel_id,
8244+ next_local_commitment_number : 0 ,
8245+ next_remote_commitment_number : 0 ,
8246+ data_loss_protect : OptionalField :: Absent ,
8247+ } ;
8248+
8249+ let closing_signed_msg = msgs:: ClosingSigned {
8250+ channel_id : channel_id,
8251+ fee_satoshis : 1000 ,
8252+ signature : signature,
8253+ fee_range : None ,
8254+ } ;
8255+
8256+ let shutdown_msg = msgs:: Shutdown {
8257+ channel_id : channel_id,
8258+ scriptpubkey : Script :: new ( ) ,
8259+ } ;
8260+
8261+ let onion_routing_packet = msgs:: OnionPacket {
8262+ version : 255 ,
8263+ public_key : Ok ( unkown_public_key) ,
8264+ hop_data : [ 1 ; 20 * 65 ] ,
8265+ hmac : [ 2 ; 32 ]
8266+ } ;
8267+
8268+ let update_add_htlc_msg = msgs:: UpdateAddHTLC {
8269+ channel_id : channel_id,
8270+ htlc_id : 0 ,
8271+ amount_msat : 1000000 ,
8272+ payment_hash : PaymentHash ( [ 1 ; 32 ] ) ,
8273+ cltv_expiry : 821716 ,
8274+ onion_routing_packet
8275+ } ;
8276+
8277+ let commitment_signed_msg = msgs:: CommitmentSigned {
8278+ channel_id : channel_id,
8279+ signature : signature,
8280+ htlc_signatures : Vec :: new ( ) ,
8281+ } ;
8282+
8283+ let update_fee_msg = msgs:: UpdateFee {
8284+ channel_id : channel_id,
8285+ feerate_per_kw : 1000 ,
8286+ } ;
8287+
8288+ let malformed_update_msg = msgs:: UpdateFailMalformedHTLC {
8289+ channel_id : channel_id,
8290+ htlc_id : 0 ,
8291+ sha256_of_onion : [ 1 ; 32 ] ,
8292+ failure_code : 0x8000 ,
8293+ } ;
8294+
8295+ let fulfill_update_msg = msgs:: UpdateFulfillHTLC {
8296+ channel_id : channel_id,
8297+ htlc_id : 0 ,
8298+ payment_preimage : PaymentPreimage ( [ 1 ; 32 ] ) ,
8299+ } ;
8300+
8301+ let fail_update_msg = msgs:: UpdateFailHTLC {
8302+ channel_id : channel_id,
8303+ htlc_id : 0 ,
8304+ reason : msgs:: OnionErrorPacket { data : Vec :: new ( ) } ,
8305+ } ;
8306+
8307+ let revoke_and_ack_msg = msgs:: RevokeAndACK {
8308+ channel_id : channel_id,
8309+ per_commitment_secret : [ 1 ; 32 ] ,
8310+ next_per_commitment_point : unkown_public_key,
8311+ } ;
8312+
8313+ // Test the API functions and message handlers.
8314+ check_unkown_peer_error ( nodes[ 0 ] . node . create_channel ( unkown_public_key, 1_000_000 , 500_000_000 , 42 , None ) , unkown_public_key) ;
8315+
8316+ nodes[ 1 ] . node . handle_open_channel ( & unkown_public_key, InitFeatures :: known ( ) , & open_channel_msg) ;
8317+ check_unkown_peer_msg_event ( & nodes[ 1 ] , unkown_public_key, open_channel_msg. temporary_channel_id ) ;
8318+
8319+ nodes[ 0 ] . node . handle_accept_channel ( & unkown_public_key, InitFeatures :: known ( ) , & accept_channel_msg) ;
8320+ check_unkown_peer_msg_event ( & nodes[ 0 ] , unkown_public_key, open_channel_msg. temporary_channel_id ) ;
8321+
8322+ check_unkown_peer_error ( nodes[ 0 ] . node . accept_inbound_channel ( & open_channel_msg. temporary_channel_id , & unkown_public_key, 42 ) , unkown_public_key) ;
8323+ nodes[ 1 ] . node . handle_funding_created ( & unkown_public_key, & funding_created_msg) ;
8324+ check_unkown_peer_msg_event ( & nodes[ 1 ] , unkown_public_key, open_channel_msg. temporary_channel_id ) ;
8325+
8326+ nodes[ 0 ] . node . handle_funding_signed ( & unkown_public_key, & funding_signed_msg) ;
8327+ check_unkown_peer_msg_event ( & nodes[ 0 ] , unkown_public_key, channel_id) ;
8328+
8329+ nodes[ 0 ] . node . handle_channel_ready ( & unkown_public_key, & channel_ready_msg) ;
8330+ check_unkown_peer_msg_event ( & nodes[ 0 ] , unkown_public_key, channel_id) ;
8331+
8332+ nodes[ 1 ] . node . handle_announcement_signatures ( & unkown_public_key, & announcement_signatures_msg) ;
8333+ check_unkown_peer_msg_event ( & nodes[ 1 ] , unkown_public_key, channel_id) ;
8334+
8335+ check_unkown_peer_error ( nodes[ 0 ] . node . close_channel ( & channel_id, & unkown_public_key) , unkown_public_key) ;
8336+
8337+ check_unkown_peer_error ( nodes[ 0 ] . node . force_close_broadcasting_latest_txn ( & channel_id, & unkown_public_key) , unkown_public_key) ;
8338+
8339+ check_unkown_peer_error ( nodes[ 0 ] . node . force_close_without_broadcasting_txn ( & channel_id, & unkown_public_key) , unkown_public_key) ;
8340+
8341+ check_unkown_peer_error ( nodes[ 0 ] . node . update_channel_config ( & unkown_public_key, & [ channel_id] , & ChannelConfig :: default ( ) ) , unkown_public_key) ;
8342+
8343+ nodes[ 0 ] . node . handle_shutdown ( & unkown_public_key, & InitFeatures :: known ( ) , & shutdown_msg) ;
8344+ check_unkown_peer_msg_event ( & nodes[ 0 ] , unkown_public_key, channel_id) ;
8345+
8346+ nodes[ 1 ] . node . handle_closing_signed ( & unkown_public_key, & closing_signed_msg) ;
8347+ check_unkown_peer_msg_event ( & nodes[ 1 ] , unkown_public_key, channel_id) ;
8348+
8349+ nodes[ 0 ] . node . handle_channel_reestablish ( & unkown_public_key, & channel_reestablish_msg) ;
8350+ check_unkown_peer_msg_event ( & nodes[ 0 ] , unkown_public_key, channel_id) ;
8351+
8352+ nodes[ 1 ] . node . handle_update_add_htlc ( & unkown_public_key, & update_add_htlc_msg) ;
8353+ check_unkown_peer_msg_event ( & nodes[ 1 ] , unkown_public_key, channel_id) ;
8354+
8355+ nodes[ 1 ] . node . handle_commitment_signed ( & unkown_public_key, & commitment_signed_msg) ;
8356+ check_unkown_peer_msg_event ( & nodes[ 1 ] , unkown_public_key, channel_id) ;
8357+
8358+ nodes[ 1 ] . node . handle_update_fail_malformed_htlc ( & unkown_public_key, & malformed_update_msg) ;
8359+ check_unkown_peer_msg_event ( & nodes[ 1 ] , unkown_public_key, channel_id) ;
8360+
8361+ nodes[ 1 ] . node . handle_update_fail_htlc ( & unkown_public_key, & fail_update_msg) ;
8362+ check_unkown_peer_msg_event ( & nodes[ 1 ] , unkown_public_key, channel_id) ;
8363+
8364+ nodes[ 1 ] . node . handle_update_fulfill_htlc ( & unkown_public_key, & fulfill_update_msg) ;
8365+ check_unkown_peer_msg_event ( & nodes[ 1 ] , unkown_public_key, channel_id) ;
8366+
8367+ nodes[ 1 ] . node . handle_revoke_and_ack ( & unkown_public_key, & revoke_and_ack_msg) ;
8368+ check_unkown_peer_msg_event ( & nodes[ 1 ] , unkown_public_key, channel_id) ;
8369+
8370+ nodes[ 1 ] . node . handle_update_fee ( & unkown_public_key, & update_fee_msg) ;
8371+ check_unkown_peer_msg_event ( & nodes[ 1 ] , unkown_public_key, channel_id) ;
8372+ }
81598373}
81608374
81618375#[ cfg( all( any( test, feature = "_test_utils" ) , feature = "_bench_unstable" ) ) ]
0 commit comments