@@ -185,16 +185,21 @@ impl CustomOnionMessageHandler for TestCustomMessageHandler {
185185 Some ( expected_msg) => assert_eq ! ( expected_msg, msg) ,
186186 None => panic ! ( "Unexpected message: {:?}" , msg) ,
187187 }
188- let response_option = match msg {
189- TestCustomMessage :: Request => Some ( TestCustomMessage :: Response ) ,
190- TestCustomMessage :: Response => None ,
191- TestCustomMessage :: ResponseA => Some ( TestCustomMessage :: ResponseB ) ,
192- TestCustomMessage :: ResponseB => Some ( TestCustomMessage :: ResponseA ) ,
188+ let ( response_option, add_reply_path ) = match msg {
189+ TestCustomMessage :: Request => ( Some ( TestCustomMessage :: Response ) , false ) ,
190+ TestCustomMessage :: Response => ( None , false ) ,
191+ TestCustomMessage :: ResponseA => ( Some ( TestCustomMessage :: ResponseB ) , true ) ,
192+ TestCustomMessage :: ResponseB => ( Some ( TestCustomMessage :: ResponseA ) , true ) ,
193193 } ;
194- if let ( Some ( response) , Some ( responder) ) = ( response_option, responder) {
195- responder. respond ( response)
196- } else {
197- ResponseInstruction :: NoResponse
194+ match ( response_option, responder) {
195+ ( Some ( response) , Some ( responder) ) => {
196+ if add_reply_path {
197+ responder. respond_with_reply_path ( response)
198+ } else {
199+ responder. respond ( response)
200+ }
201+ }
202+ _ => ResponseInstruction :: NoResponse ,
198203 }
199204 }
200205 fn read_custom_message < R : io:: Read > ( & self , message_type : u64 , buffer : & mut R ) -> Result < Option < Self :: CustomMessage > , DecodeError > where Self : Sized {
@@ -456,6 +461,67 @@ fn async_response_over_one_blinded_hop() {
456461 pass_along_path ( & nodes) ;
457462}
458463
464+ fn do_test_async_response_with_reply_path_over_one_blinded_hop ( reply_path_succeed : bool ) {
465+ // Simulate an asynchronous interaction between two nodes, Alice and Bob.
466+
467+ // 1. Set up the network with two nodes: Alice and Bob.
468+ let mut nodes = create_nodes ( 2 ) ;
469+ let alice = & nodes[ 0 ] ;
470+ let bob = & nodes[ 1 ] ;
471+
472+ // 2. Define the message sent from Bob to Alice.
473+ let message = TestCustomMessage :: ResponseA ;
474+ let path_id = Some ( [ 2 ; 32 ] ) ;
475+
476+ // 3. Simulate the creation of a Blinded Reply path provided by Bob.
477+ let secp_ctx = Secp256k1 :: new ( ) ;
478+ let reply_path = BlindedPath :: new_for_message ( & [ bob. node_id ] , & * bob. entropy_source , & secp_ctx) . unwrap ( ) ;
479+
480+ if reply_path_succeed {
481+ // Add a channel so that nodes are announced to each other.
482+ // This will allow creating the reply path.
483+ add_channel_to_graph ( alice, bob, & secp_ctx, 24 ) ;
484+ }
485+
486+ // 4. Create a responder using the reply path for Alice.
487+ let responder = Some ( Responder :: new ( reply_path, path_id) ) ;
488+
489+ // 5. Expect Alice to receive the message and create a response instruction for it.
490+ alice. custom_message_handler . expect_message ( message. clone ( ) ) ;
491+ let response_instruction = alice. custom_message_handler . handle_custom_message ( message, responder) ;
492+
493+ if !reply_path_succeed {
494+ // 6. Simulate Alice attempting to asynchronously respond back to Bob
495+ // but failing to create a reply path.
496+ assert_eq ! (
497+ alice. messenger. handle_onion_message_response( response_instruction) ,
498+ Err ( SendError :: PathNotFound ) ,
499+ ) ;
500+ return ;
501+ }
502+
503+ // 6. Simulate Alice asynchronously responding back to Bob with a response.
504+ assert_eq ! (
505+ alice. messenger. handle_onion_message_response( response_instruction) ,
506+ Ok ( Some ( SendSuccess :: Buffered ) ) ,
507+ ) ;
508+
509+ // 7. Expect Bob to receive the response and handle it accordingly.
510+ bob. custom_message_handler . expect_message ( TestCustomMessage :: ResponseB ) ;
511+ pass_along_path ( & nodes) ;
512+
513+ // 8. Expect Alice to receive Bob's response through the created reply path and handle it.
514+ alice. custom_message_handler . expect_message ( TestCustomMessage :: ResponseA ) ;
515+ nodes. reverse ( ) ;
516+ pass_along_path ( & nodes) ;
517+ }
518+
519+ #[ test]
520+ fn async_response_with_reply_path_over_one_blinded_hop ( ) {
521+ do_test_async_response_with_reply_path_over_one_blinded_hop ( true ) ;
522+ do_test_async_response_with_reply_path_over_one_blinded_hop ( false ) ;
523+ }
524+
459525#[ test]
460526fn too_big_packet_error ( ) {
461527 // Make sure we error as expected if a packet is too big to send.
0 commit comments