@@ -185,16 +185,21 @@ impl CustomOnionMessageHandler for TestCustomMessageHandler {
185
185
Some ( expected_msg) => assert_eq ! ( expected_msg, msg) ,
186
186
None => panic ! ( "Unexpected message: {:?}" , msg) ,
187
187
}
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 ) ,
193
193
} ;
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 ,
198
203
}
199
204
}
200
205
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,62 @@ fn async_response_over_one_blinded_hop() {
456
461
pass_along_path ( & nodes) ;
457
462
}
458
463
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
+ let mut nodes = create_nodes ( 2 ) ;
468
+ let alice = & nodes[ 0 ] ;
469
+ let bob = & nodes[ 1 ] ;
470
+
471
+ // Alice receives a message from Bob with a reply path
472
+ let message = TestCustomMessage :: ResponseA ;
473
+ let path_id = Some ( [ 2 ; 32 ] ) ;
474
+
475
+ let secp_ctx = Secp256k1 :: new ( ) ;
476
+ let reply_path = BlindedPath :: new_for_message ( & [ bob. node_id ] , & * bob. entropy_source , & secp_ctx) . unwrap ( ) ;
477
+
478
+ if reply_path_succeed {
479
+ // Add a channel so that nodes are announced to each other.
480
+ // This will allow creating the reply path by Alice to include in the response.
481
+ add_channel_to_graph ( alice, bob, & secp_ctx, 24 ) ;
482
+ }
483
+
484
+ let responder = Some ( Responder :: new ( reply_path, path_id) ) ;
485
+ alice. custom_message_handler . expect_message ( message. clone ( ) ) ;
486
+
487
+ // Alice handles the message reponse, and creates the appropriate ResponseInstruction for it.
488
+ let response_instruction = alice. custom_message_handler . handle_custom_message ( message, responder) ;
489
+
490
+ if !reply_path_succeed {
491
+ // Simulate Alice attempting to asynchronously respond back to Bob
492
+ // but failing to create a reply path.
493
+ assert_eq ! (
494
+ alice. messenger. handle_onion_message_response( response_instruction) ,
495
+ Err ( SendError :: PathNotFound ) ,
496
+ ) ;
497
+ } else {
498
+ // Simulate Alice asynchronously responding back to Bob with a response.
499
+ assert_eq ! (
500
+ alice. messenger. handle_onion_message_response( response_instruction) ,
501
+ Ok ( Some ( SendSuccess :: Buffered ) ) ,
502
+ ) ;
503
+
504
+ bob. custom_message_handler . expect_message ( TestCustomMessage :: ResponseB ) ;
505
+ pass_along_path ( & nodes) ;
506
+
507
+ // Simulate Bob responding back to Alice through the reply path created by her.
508
+ alice. custom_message_handler . expect_message ( TestCustomMessage :: ResponseA ) ;
509
+ nodes. reverse ( ) ;
510
+ pass_along_path ( & nodes) ;
511
+ }
512
+ }
513
+
514
+ #[ test]
515
+ fn async_response_with_reply_path_over_one_blinded_hop ( ) {
516
+ do_test_async_response_with_reply_path_over_one_blinded_hop ( true ) ;
517
+ do_test_async_response_with_reply_path_over_one_blinded_hop ( false ) ;
518
+ }
519
+
459
520
#[ test]
460
521
fn too_big_packet_error ( ) {
461
522
// Make sure we error as expected if a packet is too big to send.
0 commit comments