@@ -2699,11 +2699,12 @@ mod tests {
26992699 use crate :: ln:: types:: ChannelId ;
27002700 use crate :: ln:: features:: { InitFeatures , NodeFeatures } ;
27012701 use crate :: ln:: peer_channel_encryptor:: PeerChannelEncryptor ;
2702- use crate :: ln:: peer_handler:: { CustomMessageHandler , PeerManager , MessageHandler , SocketDescriptor , IgnoringMessageHandler , filter_addresses, ErroringMessageHandler , MAX_BUFFER_DRAIN_TICK_INTERVALS_PER_PEER } ;
2702+ use crate :: ln:: peer_handler:: { CustomMessageHandler , OnionMessageHandler , PeerManager , MessageHandler , SocketDescriptor , IgnoringMessageHandler , filter_addresses, ErroringMessageHandler , MAX_BUFFER_DRAIN_TICK_INTERVALS_PER_PEER } ;
27032703 use crate :: ln:: { msgs, wire} ;
27042704 use crate :: ln:: msgs:: { Init , LightningError , SocketAddress } ;
27052705 use crate :: util:: test_utils;
27062706
2707+
27072708 use bitcoin:: Network ;
27082709 use bitcoin:: blockdata:: constants:: ChainHash ;
27092710 use bitcoin:: secp256k1:: { PublicKey , SecretKey } ;
@@ -2780,6 +2781,95 @@ mod tests {
27802781 }
27812782 }
27822783
2784+ struct TestPeerTrackingMessageHandler {
2785+ features : InitFeatures ,
2786+ pub peer_connected_called : Mutex < bool > ,
2787+ pub peer_disconnected_called : Mutex < bool > ,
2788+ }
2789+
2790+ impl TestPeerTrackingMessageHandler {
2791+ pub fn new ( features : InitFeatures ) -> Self {
2792+ Self {
2793+ features,
2794+ peer_connected_called : Mutex :: new ( false ) ,
2795+ peer_disconnected_called : Mutex :: new ( false ) ,
2796+ }
2797+ }
2798+ }
2799+
2800+ impl wire:: CustomMessageReader for TestPeerTrackingMessageHandler {
2801+ type CustomMessage = Infallible ;
2802+ fn read < R : io:: Read > ( & self , _: u16 , _: & mut R ) -> Result < Option < Self :: CustomMessage > , msgs:: DecodeError > {
2803+ Ok ( None )
2804+ }
2805+ }
2806+
2807+ impl CustomMessageHandler for TestPeerTrackingMessageHandler {
2808+ fn handle_custom_message ( & self , _: Infallible , _: & PublicKey ) -> Result < ( ) , LightningError > {
2809+ unreachable ! ( ) ;
2810+ }
2811+
2812+ fn get_and_clear_pending_msg ( & self ) -> Vec < ( PublicKey , Self :: CustomMessage ) > { Vec :: new ( ) }
2813+
2814+ fn peer_disconnected ( & self , _their_node_id : & PublicKey ) {
2815+ let connected = {
2816+ * self . peer_connected_called . lock ( ) . unwrap ( )
2817+ } ;
2818+ assert ! ( connected) ;
2819+
2820+ let mut disconnected = self . peer_disconnected_called . lock ( ) . unwrap ( ) ;
2821+ * disconnected = true ;
2822+ }
2823+
2824+ fn peer_connected ( & self , _their_node_id : & PublicKey , _msg : & Init , _inbound : bool ) -> Result < ( ) , ( ) > {
2825+ let mut connected = self . peer_connected_called . lock ( ) . unwrap ( ) ;
2826+ assert ! ( !* connected) ;
2827+ * connected = true ;
2828+
2829+ let disconnected = {
2830+ * self . peer_disconnected_called . lock ( ) . unwrap ( )
2831+ } ;
2832+ assert ! ( !disconnected) ;
2833+
2834+ Err ( ( ) )
2835+ }
2836+
2837+ fn provided_node_features ( & self ) -> NodeFeatures { NodeFeatures :: empty ( ) }
2838+
2839+ fn provided_init_features ( & self , _: & PublicKey ) -> InitFeatures {
2840+ self . features . clone ( )
2841+ }
2842+ }
2843+
2844+ impl OnionMessageHandler for TestPeerTrackingMessageHandler {
2845+ fn handle_onion_message ( & self , _peer_node_id : & PublicKey , _msg : & msgs:: OnionMessage ) { }
2846+ fn next_onion_message_for_peer ( & self , _peer_node_id : PublicKey ) -> Option < msgs:: OnionMessage > { None }
2847+ fn peer_connected ( & self , _their_node_id : & PublicKey , _init : & Init , _inbound : bool ) -> Result < ( ) , ( ) > {
2848+ let mut connected = self . peer_connected_called . lock ( ) . unwrap ( ) ;
2849+ assert ! ( !* connected) ;
2850+ * connected = true ;
2851+
2852+ let disconnected = {
2853+ * self . peer_disconnected_called . lock ( ) . unwrap ( )
2854+ } ;
2855+ assert ! ( !disconnected) ;
2856+
2857+ Err ( ( ) )
2858+ }
2859+ fn peer_disconnected ( & self , _their_node_id : & PublicKey ) {
2860+ let connected = {
2861+ * self . peer_connected_called . lock ( ) . unwrap ( )
2862+ } ;
2863+ assert ! ( connected) ;
2864+
2865+ let mut disconnected = self . peer_disconnected_called . lock ( ) . unwrap ( ) ;
2866+ * disconnected = true ;
2867+ }
2868+ fn timer_tick_occurred ( & self ) { }
2869+ fn provided_node_features ( & self ) -> NodeFeatures { NodeFeatures :: empty ( ) }
2870+ fn provided_init_features ( & self , _their_node_id : & PublicKey ) -> InitFeatures { self . features . clone ( ) }
2871+ }
2872+
27832873 fn create_peermgr_cfgs ( peer_count : usize ) -> Vec < PeerManagerCfg > {
27842874 let mut cfgs = Vec :: new ( ) ;
27852875 for i in 0 ..peer_count {
@@ -3164,6 +3254,103 @@ mod tests {
31643254 assert_eq ! ( peers[ 0 ] . peers. read( ) . unwrap( ) . len( ) , 0 ) ;
31653255 }
31663256
3257+ #[ test]
3258+ fn test_peer_connected_error_disconnects ( ) {
3259+
3260+ struct PeerTrackingPeerManagerConfig {
3261+ logger : test_utils:: TestLogger ,
3262+ node_signer : test_utils:: TestNodeSigner ,
3263+ chan_handler : test_utils:: TestChannelMessageHandler ,
3264+ route_handler : test_utils:: TestRoutingMessageHandler ,
3265+ onion_message_handler : TestPeerTrackingMessageHandler ,
3266+ custom_message_handler : TestPeerTrackingMessageHandler ,
3267+ }
3268+
3269+ fn create_cfgs ( peers : u8 ) -> Vec < PeerTrackingPeerManagerConfig > {
3270+ let mut cfgs = vec ! [ ] ;
3271+ for i in 0 ..peers {
3272+ let features = {
3273+ let mut feature_bits = vec ! [ 0u8 ; 33 ] ;
3274+ feature_bits[ 32 ] = 0b00000001 ;
3275+ InitFeatures :: from_le_bytes ( feature_bits)
3276+ } ;
3277+ let node_secret = SecretKey :: from_slice ( & [ 42 + i as u8 ; 32 ] ) . unwrap ( ) ;
3278+ cfgs. push ( PeerTrackingPeerManagerConfig {
3279+ logger : test_utils:: TestLogger :: new ( ) ,
3280+ node_signer : test_utils:: TestNodeSigner :: new ( node_secret) ,
3281+ chan_handler : test_utils:: TestChannelMessageHandler :: new ( ChainHash :: using_genesis_block ( Network :: Testnet ) ) ,
3282+ route_handler : test_utils:: TestRoutingMessageHandler :: new ( ) ,
3283+ onion_message_handler : TestPeerTrackingMessageHandler :: new ( features. clone ( ) ) ,
3284+ custom_message_handler : TestPeerTrackingMessageHandler :: new ( features. clone ( ) ) ,
3285+ } ) ;
3286+ }
3287+ cfgs
3288+ }
3289+
3290+ type PeerTrackingPeerManager < ' a > = PeerManager < FileDescriptor , & ' a test_utils:: TestChannelMessageHandler , & ' a test_utils:: TestRoutingMessageHandler , & ' a TestPeerTrackingMessageHandler , & ' a test_utils:: TestLogger , & ' a TestPeerTrackingMessageHandler , & ' a test_utils:: TestNodeSigner > ;
3291+
3292+ fn create_network < ' a > ( peer_count : usize , cfgs : & ' a Vec < PeerTrackingPeerManagerConfig > ) -> Vec < PeerTrackingPeerManager < ' a > > {
3293+ let mut peers = Vec :: new ( ) ;
3294+ for i in 0 ..peer_count {
3295+ let ephemeral_bytes = [ i as u8 ; 32 ] ;
3296+ let msg_handler = MessageHandler {
3297+ chan_handler : & cfgs[ i] . chan_handler , route_handler : & cfgs[ i] . route_handler ,
3298+ onion_message_handler : & cfgs[ i] . onion_message_handler , custom_message_handler : & cfgs[ i] . custom_message_handler
3299+ } ;
3300+ let peer = PeerManager :: new ( msg_handler, 0 , & ephemeral_bytes, & cfgs[ i] . logger , & cfgs[ i] . node_signer ) ;
3301+ peers. push ( peer) ;
3302+ }
3303+
3304+ peers
3305+ }
3306+
3307+ fn try_establish_connection < ' a > ( peer_a : & PeerTrackingPeerManager < ' a > , peer_b : & PeerTrackingPeerManager < ' a > ) -> ( FileDescriptor , FileDescriptor ) {
3308+ let id_a = peer_a. node_signer . get_node_id ( Recipient :: Node ) . unwrap ( ) ;
3309+ let mut fd_a = FileDescriptor {
3310+ fd : 1 , outbound_data : Arc :: new ( Mutex :: new ( Vec :: new ( ) ) ) ,
3311+ disconnect : Arc :: new ( AtomicBool :: new ( false ) ) ,
3312+ } ;
3313+ let addr_a = SocketAddress :: TcpIpV4 { addr : [ 127 , 0 , 0 , 1 ] , port : 1000 } ;
3314+ let mut fd_b = FileDescriptor {
3315+ fd : 1 , outbound_data : Arc :: new ( Mutex :: new ( Vec :: new ( ) ) ) ,
3316+ disconnect : Arc :: new ( AtomicBool :: new ( false ) ) ,
3317+ } ;
3318+ let addr_b = SocketAddress :: TcpIpV4 { addr : [ 127 , 0 , 0 , 1 ] , port : 1001 } ;
3319+ let initial_data = peer_b. new_outbound_connection ( id_a, fd_b. clone ( ) , Some ( addr_a. clone ( ) ) ) . unwrap ( ) ;
3320+ peer_a. new_inbound_connection ( fd_a. clone ( ) , Some ( addr_b. clone ( ) ) ) . unwrap ( ) ;
3321+
3322+ let _res = peer_a. read_event ( & mut fd_a, & initial_data) ;
3323+ peer_a. process_events ( ) ;
3324+
3325+ let a_data = fd_a. outbound_data . lock ( ) . unwrap ( ) . split_off ( 0 ) ;
3326+
3327+ let _res = peer_b. read_event ( & mut fd_b, & a_data) ;
3328+
3329+ peer_b. process_events ( ) ;
3330+ let b_data = fd_b. outbound_data . lock ( ) . unwrap ( ) . split_off ( 0 ) ;
3331+ let _res = peer_a. read_event ( & mut fd_a, & b_data) ;
3332+
3333+ peer_a. process_events ( ) ;
3334+ let a_data = fd_a. outbound_data . lock ( ) . unwrap ( ) . split_off ( 0 ) ;
3335+
3336+ let _res = peer_b. read_event ( & mut fd_b, & a_data) ;
3337+ ( fd_a. clone ( ) , fd_b. clone ( ) )
3338+ }
3339+
3340+ let cfgs = create_cfgs ( 2 ) ;
3341+ let peers = create_network ( 2 , & cfgs) ;
3342+ let ( _sd1, _sd2) = try_establish_connection ( & peers[ 0 ] , & peers[ 1 ] ) ;
3343+
3344+ let cmh_peer_connected_called = cfgs[ 0 ] . custom_message_handler . peer_connected_called . lock ( ) . unwrap ( ) ;
3345+ let cmh_peer_disconnected_called = cfgs[ 0 ] . custom_message_handler . peer_disconnected_called . lock ( ) . unwrap ( ) ;
3346+ let om_peer_connected_called = cfgs[ 0 ] . onion_message_handler . peer_connected_called . lock ( ) . unwrap ( ) ;
3347+ let om_peer_disconnected_called = cfgs[ 0 ] . onion_message_handler . peer_disconnected_called . lock ( ) . unwrap ( ) ;
3348+ assert ! ( * cmh_peer_connected_called) ;
3349+ assert ! ( * cmh_peer_disconnected_called) ;
3350+ assert ! ( * om_peer_connected_called) ;
3351+ assert ! ( * om_peer_disconnected_called) ;
3352+ }
3353+
31673354 #[ test]
31683355 fn test_do_attempt_write_data ( ) {
31693356 // Create 2 peers with custom TestRoutingMessageHandlers and connect them.
0 commit comments