@@ -20,10 +20,11 @@ use crate::util::test_utils;
2020use bitcoin:: network:: constants:: Network ;
2121use bitcoin:: secp256k1:: { PublicKey , Secp256k1 } ;
2222
23- use core:: sync:: atomic:: { AtomicU16 , Ordering } ;
2423use crate :: io;
2524use crate :: io_extras:: read_to_end;
26- use crate :: sync:: Arc ;
25+ use crate :: sync:: { Arc , Mutex } ;
26+
27+ use crate :: prelude:: * ;
2728
2829struct MessengerNode {
2930 keys_manager : Arc < test_utils:: TestKeysInterface > ,
@@ -36,7 +37,6 @@ struct MessengerNode {
3637 Arc < TestCustomMessageHandler >
3738 > ,
3839 custom_message_handler : Arc < TestCustomMessageHandler > ,
39- logger : Arc < test_utils:: TestLogger > ,
4040}
4141
4242impl MessengerNode {
@@ -49,9 +49,12 @@ struct TestMessageRouter {}
4949
5050impl MessageRouter for TestMessageRouter {
5151 fn find_path (
52- & self , _sender : PublicKey , _peers : Vec < PublicKey > , _destination : Destination
52+ & self , _sender : PublicKey , _peers : Vec < PublicKey > , destination : Destination
5353 ) -> Result < OnionMessagePath , ( ) > {
54- todo ! ( )
54+ Ok ( OnionMessagePath {
55+ intermediate_nodes : vec ! [ ] ,
56+ destination,
57+ } )
5558 }
5659}
5760
@@ -63,7 +66,7 @@ impl OffersMessageHandler for TestOffersMessageHandler {
6366 }
6467}
6568
66- #[ derive( Clone ) ]
69+ #[ derive( Clone , Debug , PartialEq ) ]
6770enum TestCustomMessage {
6871 Request ,
6972 Response ,
@@ -93,12 +96,16 @@ impl Writeable for TestCustomMessage {
9396}
9497
9598struct TestCustomMessageHandler {
96- num_messages_expected : AtomicU16 ,
99+ expected_messages : Mutex < VecDeque < TestCustomMessage > > ,
97100}
98101
99102impl TestCustomMessageHandler {
100103 fn new ( ) -> Self {
101- Self { num_messages_expected : AtomicU16 :: new ( 0 ) }
104+ Self { expected_messages : Mutex :: new ( VecDeque :: new ( ) ) }
105+ }
106+
107+ fn expect_message ( & self , message : TestCustomMessage ) {
108+ self . expected_messages . lock ( ) . unwrap ( ) . push_back ( message) ;
102109 }
103110}
104111
@@ -109,14 +116,18 @@ impl Drop for TestCustomMessageHandler {
109116 return ;
110117 }
111118 }
112- assert_eq ! ( self . num_messages_expected . load ( Ordering :: SeqCst ) , 0 ) ;
119+ assert ! ( self . expected_messages . lock ( ) . unwrap ( ) . is_empty ( ) ) ;
113120 }
114121}
115122
116123impl CustomOnionMessageHandler for TestCustomMessageHandler {
117124 type CustomMessage = TestCustomMessage ;
118125 fn handle_custom_message ( & self , msg : Self :: CustomMessage ) -> Option < Self :: CustomMessage > {
119- self . num_messages_expected . fetch_sub ( 1 , Ordering :: SeqCst ) ;
126+ match self . expected_messages . lock ( ) . unwrap ( ) . pop_front ( ) {
127+ Some ( expected_msg) => assert_eq ! ( expected_msg, msg) ,
128+ None => panic ! ( "Unexpected message: {:?}" , msg) ,
129+ }
130+
120131 match msg {
121132 TestCustomMessage :: Request => Some ( TestCustomMessage :: Response ) ,
122133 TestCustomMessage :: Response => None ,
@@ -155,7 +166,6 @@ fn create_nodes(num_messengers: u8) -> Vec<MessengerNode> {
155166 offers_message_handler, custom_message_handler. clone ( )
156167 ) ,
157168 custom_message_handler,
158- logger,
159169 } ) ;
160170 }
161171 for idx in 0 ..num_messengers - 1 {
@@ -170,7 +180,6 @@ fn create_nodes(num_messengers: u8) -> Vec<MessengerNode> {
170180}
171181
172182fn pass_along_path ( path : & Vec < MessengerNode > ) {
173- path[ path. len ( ) - 1 ] . custom_message_handler . num_messages_expected . fetch_add ( 1 , Ordering :: SeqCst ) ;
174183 let mut prev_node = & path[ 0 ] ;
175184 for node in path. into_iter ( ) . skip ( 1 ) {
176185 let events = prev_node. messenger . release_pending_msgs ( ) ;
@@ -194,6 +203,7 @@ fn one_hop() {
194203 destination : Destination :: Node ( nodes[ 1 ] . get_node_pk ( ) ) ,
195204 } ;
196205 nodes[ 0 ] . messenger . send_onion_message ( path, test_msg, None ) . unwrap ( ) ;
206+ nodes[ 1 ] . custom_message_handler . expect_message ( TestCustomMessage :: Response ) ;
197207 pass_along_path ( & nodes) ;
198208}
199209
@@ -207,6 +217,7 @@ fn two_unblinded_hops() {
207217 destination : Destination :: Node ( nodes[ 2 ] . get_node_pk ( ) ) ,
208218 } ;
209219 nodes[ 0 ] . messenger . send_onion_message ( path, test_msg, None ) . unwrap ( ) ;
220+ nodes[ 2 ] . custom_message_handler . expect_message ( TestCustomMessage :: Response ) ;
210221 pass_along_path ( & nodes) ;
211222}
212223
@@ -223,6 +234,7 @@ fn two_unblinded_two_blinded() {
223234 } ;
224235
225236 nodes[ 0 ] . messenger . send_onion_message ( path, test_msg, None ) . unwrap ( ) ;
237+ nodes[ 4 ] . custom_message_handler . expect_message ( TestCustomMessage :: Response ) ;
226238 pass_along_path ( & nodes) ;
227239}
228240
@@ -239,6 +251,7 @@ fn three_blinded_hops() {
239251 } ;
240252
241253 nodes[ 0 ] . messenger . send_onion_message ( path, test_msg, None ) . unwrap ( ) ;
254+ nodes[ 3 ] . custom_message_handler . expect_message ( TestCustomMessage :: Response ) ;
242255 pass_along_path ( & nodes) ;
243256}
244257
@@ -273,6 +286,7 @@ fn we_are_intro_node() {
273286 } ;
274287
275288 nodes[ 0 ] . messenger . send_onion_message ( path, OnionMessageContents :: Custom ( test_msg. clone ( ) ) , None ) . unwrap ( ) ;
289+ nodes[ 2 ] . custom_message_handler . expect_message ( TestCustomMessage :: Response ) ;
276290 pass_along_path ( & nodes) ;
277291
278292 // Try with a two-hop blinded path where we are the introduction node.
@@ -282,6 +296,7 @@ fn we_are_intro_node() {
282296 destination : Destination :: BlindedPath ( blinded_path) ,
283297 } ;
284298 nodes[ 0 ] . messenger . send_onion_message ( path, OnionMessageContents :: Custom ( test_msg) , None ) . unwrap ( ) ;
299+ nodes[ 1 ] . custom_message_handler . expect_message ( TestCustomMessage :: Response ) ;
285300 nodes. remove ( 2 ) ;
286301 pass_along_path ( & nodes) ;
287302}
@@ -317,8 +332,8 @@ fn invalid_blinded_path_error() {
317332
318333#[ test]
319334fn reply_path ( ) {
320- let nodes = create_nodes ( 4 ) ;
321- let test_msg = TestCustomMessage :: Response ;
335+ let mut nodes = create_nodes ( 4 ) ;
336+ let test_msg = TestCustomMessage :: Request ;
322337 let secp_ctx = Secp256k1 :: new ( ) ;
323338
324339 // Destination::Node
@@ -328,11 +343,12 @@ fn reply_path() {
328343 } ;
329344 let reply_path = BlindedPath :: new_for_message ( & [ nodes[ 2 ] . get_node_pk ( ) , nodes[ 1 ] . get_node_pk ( ) , nodes[ 0 ] . get_node_pk ( ) ] , & * nodes[ 0 ] . keys_manager , & secp_ctx) . unwrap ( ) ;
330345 nodes[ 0 ] . messenger . send_onion_message ( path, OnionMessageContents :: Custom ( test_msg. clone ( ) ) , Some ( reply_path) ) . unwrap ( ) ;
346+ nodes[ 3 ] . custom_message_handler . expect_message ( TestCustomMessage :: Request ) ;
331347 pass_along_path ( & nodes) ;
332348 // Make sure the last node successfully decoded the reply path.
333- nodes[ 3 ] . logger . assert_log_contains (
334- "lightning::onion_message::messenger" ,
335- & format ! ( "Received an onion message with path_id None and a reply_path" ) , 1 ) ;
349+ nodes[ 0 ] . custom_message_handler . expect_message ( TestCustomMessage :: Response ) ;
350+ nodes . reverse ( ) ;
351+ pass_along_path ( & nodes ) ;
336352
337353 // Destination::BlindedPath
338354 let blinded_path = BlindedPath :: new_for_message ( & [ nodes[ 1 ] . get_node_pk ( ) , nodes[ 2 ] . get_node_pk ( ) , nodes[ 3 ] . get_node_pk ( ) ] , & * nodes[ 3 ] . keys_manager , & secp_ctx) . unwrap ( ) ;
@@ -343,10 +359,13 @@ fn reply_path() {
343359 let reply_path = BlindedPath :: new_for_message ( & [ nodes[ 2 ] . get_node_pk ( ) , nodes[ 1 ] . get_node_pk ( ) , nodes[ 0 ] . get_node_pk ( ) ] , & * nodes[ 0 ] . keys_manager , & secp_ctx) . unwrap ( ) ;
344360
345361 nodes[ 0 ] . messenger . send_onion_message ( path, OnionMessageContents :: Custom ( test_msg) , Some ( reply_path) ) . unwrap ( ) ;
362+ nodes[ 3 ] . custom_message_handler . expect_message ( TestCustomMessage :: Request ) ;
363+ pass_along_path ( & nodes) ;
364+
365+ // Make sure the last node successfully decoded the reply path.
366+ nodes[ 0 ] . custom_message_handler . expect_message ( TestCustomMessage :: Response ) ;
367+ nodes. reverse ( ) ;
346368 pass_along_path ( & nodes) ;
347- nodes[ 3 ] . logger . assert_log_contains (
348- "lightning::onion_message::messenger" ,
349- & format ! ( "Received an onion message with path_id None and a reply_path" ) , 2 ) ;
350369}
351370
352371#[ test]
@@ -377,7 +396,7 @@ fn invalid_custom_message_type() {
377396#[ test]
378397fn peer_buffer_full ( ) {
379398 let nodes = create_nodes ( 2 ) ;
380- let test_msg = TestCustomMessage :: Response ;
399+ let test_msg = TestCustomMessage :: Request ;
381400 let path = OnionMessagePath {
382401 intermediate_nodes : vec ! [ ] ,
383402 destination : Destination :: Node ( nodes[ 1 ] . get_node_pk ( ) ) ,
@@ -407,5 +426,6 @@ fn many_hops() {
407426 destination : Destination :: Node ( nodes[ num_nodes-1 ] . get_node_pk ( ) ) ,
408427 } ;
409428 nodes[ 0 ] . messenger . send_onion_message ( path, OnionMessageContents :: Custom ( test_msg) , None ) . unwrap ( ) ;
429+ nodes[ num_nodes-1 ] . custom_message_handler . expect_message ( TestCustomMessage :: Response ) ;
410430 pass_along_path ( & nodes) ;
411431}
0 commit comments