@@ -20,10 +20,11 @@ use crate::util::test_utils;
20
20
use bitcoin:: network:: constants:: Network ;
21
21
use bitcoin:: secp256k1:: { PublicKey , Secp256k1 } ;
22
22
23
- use core:: sync:: atomic:: { AtomicU16 , Ordering } ;
24
23
use crate :: io;
25
24
use crate :: io_extras:: read_to_end;
26
- use crate :: sync:: Arc ;
25
+ use crate :: sync:: { Arc , Mutex } ;
26
+
27
+ use crate :: prelude:: * ;
27
28
28
29
struct MessengerNode {
29
30
keys_manager : Arc < test_utils:: TestKeysInterface > ,
@@ -36,7 +37,6 @@ struct MessengerNode {
36
37
Arc < TestCustomMessageHandler >
37
38
> ,
38
39
custom_message_handler : Arc < TestCustomMessageHandler > ,
39
- logger : Arc < test_utils:: TestLogger > ,
40
40
}
41
41
42
42
impl MessengerNode {
@@ -49,9 +49,12 @@ struct TestMessageRouter {}
49
49
50
50
impl MessageRouter for TestMessageRouter {
51
51
fn find_path (
52
- & self , _sender : & PublicKey , _destination : Destination
52
+ & self , _sender : & PublicKey , destination : Destination
53
53
) -> Result < OnionMessagePath , ( ) > {
54
- todo ! ( )
54
+ Ok ( OnionMessagePath {
55
+ intermediate_nodes : vec ! [ ] ,
56
+ destination,
57
+ } )
55
58
}
56
59
}
57
60
@@ -69,7 +72,7 @@ impl ResponseErrorHandler for TestOffersMessageHandler {
69
72
}
70
73
}
71
74
72
- #[ derive( Clone ) ]
75
+ #[ derive( Clone , Debug , PartialEq ) ]
73
76
enum TestCustomMessage {
74
77
Request ,
75
78
Response ,
@@ -99,12 +102,16 @@ impl Writeable for TestCustomMessage {
99
102
}
100
103
101
104
struct TestCustomMessageHandler {
102
- num_messages_expected : AtomicU16 ,
105
+ expected_messages : Mutex < VecDeque < TestCustomMessage > > ,
103
106
}
104
107
105
108
impl TestCustomMessageHandler {
106
109
fn new ( ) -> Self {
107
- Self { num_messages_expected : AtomicU16 :: new ( 0 ) }
110
+ Self { expected_messages : Mutex :: new ( VecDeque :: new ( ) ) }
111
+ }
112
+
113
+ fn expect_message ( & self , message : TestCustomMessage ) {
114
+ self . expected_messages . lock ( ) . unwrap ( ) . push_back ( message) ;
108
115
}
109
116
}
110
117
@@ -115,14 +122,18 @@ impl Drop for TestCustomMessageHandler {
115
122
return ;
116
123
}
117
124
}
118
- assert_eq ! ( self . num_messages_expected . load ( Ordering :: SeqCst ) , 0 ) ;
125
+ assert ! ( self . expected_messages . lock ( ) . unwrap ( ) . is_empty ( ) ) ;
119
126
}
120
127
}
121
128
122
129
impl CustomOnionMessageHandler for TestCustomMessageHandler {
123
130
type CustomMessage = TestCustomMessage ;
124
131
fn handle_custom_message ( & self , msg : Self :: CustomMessage ) -> Option < Self :: CustomMessage > {
125
- self . num_messages_expected . fetch_sub ( 1 , Ordering :: SeqCst ) ;
132
+ match self . expected_messages . lock ( ) . unwrap ( ) . pop_front ( ) {
133
+ Some ( expected_msg) => assert_eq ! ( expected_msg, msg) ,
134
+ None => panic ! ( "Unexpected message: {:?}" , msg) ,
135
+ }
136
+
126
137
match msg {
127
138
TestCustomMessage :: Request => Some ( TestCustomMessage :: Response ) ,
128
139
TestCustomMessage :: Response => None ,
@@ -167,7 +178,6 @@ fn create_nodes(num_messengers: u8) -> Vec<MessengerNode> {
167
178
offers_message_handler, custom_message_handler. clone ( )
168
179
) ,
169
180
custom_message_handler,
170
- logger,
171
181
} ) ;
172
182
}
173
183
for idx in 0 ..num_messengers - 1 {
@@ -182,7 +192,6 @@ fn create_nodes(num_messengers: u8) -> Vec<MessengerNode> {
182
192
}
183
193
184
194
fn pass_along_path ( path : & Vec < MessengerNode > ) {
185
- path[ path. len ( ) - 1 ] . custom_message_handler . num_messages_expected . fetch_add ( 1 , Ordering :: SeqCst ) ;
186
195
let mut prev_node = & path[ 0 ] ;
187
196
for node in path. into_iter ( ) . skip ( 1 ) {
188
197
let events = prev_node. messenger . release_pending_msgs ( ) ;
@@ -206,6 +215,7 @@ fn one_hop() {
206
215
destination : Destination :: Node ( nodes[ 1 ] . get_node_pk ( ) ) ,
207
216
} ;
208
217
nodes[ 0 ] . messenger . send_onion_message ( path, test_msg, None ) . unwrap ( ) ;
218
+ nodes[ 1 ] . custom_message_handler . expect_message ( TestCustomMessage :: Response ) ;
209
219
pass_along_path ( & nodes) ;
210
220
}
211
221
@@ -219,6 +229,7 @@ fn two_unblinded_hops() {
219
229
destination : Destination :: Node ( nodes[ 2 ] . get_node_pk ( ) ) ,
220
230
} ;
221
231
nodes[ 0 ] . messenger . send_onion_message ( path, test_msg, None ) . unwrap ( ) ;
232
+ nodes[ 2 ] . custom_message_handler . expect_message ( TestCustomMessage :: Response ) ;
222
233
pass_along_path ( & nodes) ;
223
234
}
224
235
@@ -235,6 +246,7 @@ fn two_unblinded_two_blinded() {
235
246
} ;
236
247
237
248
nodes[ 0 ] . messenger . send_onion_message ( path, test_msg, None ) . unwrap ( ) ;
249
+ nodes[ 4 ] . custom_message_handler . expect_message ( TestCustomMessage :: Response ) ;
238
250
pass_along_path ( & nodes) ;
239
251
}
240
252
@@ -251,6 +263,7 @@ fn three_blinded_hops() {
251
263
} ;
252
264
253
265
nodes[ 0 ] . messenger . send_onion_message ( path, test_msg, None ) . unwrap ( ) ;
266
+ nodes[ 3 ] . custom_message_handler . expect_message ( TestCustomMessage :: Response ) ;
254
267
pass_along_path ( & nodes) ;
255
268
}
256
269
@@ -285,6 +298,7 @@ fn we_are_intro_node() {
285
298
} ;
286
299
287
300
nodes[ 0 ] . messenger . send_onion_message ( path, OnionMessageContents :: Custom ( test_msg. clone ( ) ) , None ) . unwrap ( ) ;
301
+ nodes[ 2 ] . custom_message_handler . expect_message ( TestCustomMessage :: Response ) ;
288
302
pass_along_path ( & nodes) ;
289
303
290
304
// Try with a two-hop blinded path where we are the introduction node.
@@ -294,6 +308,7 @@ fn we_are_intro_node() {
294
308
destination : Destination :: BlindedPath ( blinded_path) ,
295
309
} ;
296
310
nodes[ 0 ] . messenger . send_onion_message ( path, OnionMessageContents :: Custom ( test_msg) , None ) . unwrap ( ) ;
311
+ nodes[ 1 ] . custom_message_handler . expect_message ( TestCustomMessage :: Response ) ;
297
312
nodes. remove ( 2 ) ;
298
313
pass_along_path ( & nodes) ;
299
314
}
@@ -329,8 +344,8 @@ fn invalid_blinded_path_error() {
329
344
330
345
#[ test]
331
346
fn reply_path ( ) {
332
- let nodes = create_nodes ( 4 ) ;
333
- let test_msg = TestCustomMessage :: Response ;
347
+ let mut nodes = create_nodes ( 4 ) ;
348
+ let test_msg = TestCustomMessage :: Request ;
334
349
let secp_ctx = Secp256k1 :: new ( ) ;
335
350
336
351
// Destination::Node
@@ -340,11 +355,12 @@ fn reply_path() {
340
355
} ;
341
356
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 ( ) ;
342
357
nodes[ 0 ] . messenger . send_onion_message ( path, OnionMessageContents :: Custom ( test_msg. clone ( ) ) , Some ( reply_path) ) . unwrap ( ) ;
358
+ nodes[ 3 ] . custom_message_handler . expect_message ( TestCustomMessage :: Request ) ;
343
359
pass_along_path ( & nodes) ;
344
360
// Make sure the last node successfully decoded the reply path.
345
- nodes[ 3 ] . logger . assert_log_contains (
346
- "lightning::onion_message::messenger" ,
347
- & format ! ( "Received an onion message with path_id None and a reply_path" ) , 1 ) ;
361
+ nodes[ 0 ] . custom_message_handler . expect_message ( TestCustomMessage :: Response ) ;
362
+ nodes . reverse ( ) ;
363
+ pass_along_path ( & nodes ) ;
348
364
349
365
// Destination::BlindedPath
350
366
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 ( ) ;
@@ -355,10 +371,13 @@ fn reply_path() {
355
371
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 ( ) ;
356
372
357
373
nodes[ 0 ] . messenger . send_onion_message ( path, OnionMessageContents :: Custom ( test_msg) , Some ( reply_path) ) . unwrap ( ) ;
374
+ nodes[ 3 ] . custom_message_handler . expect_message ( TestCustomMessage :: Request ) ;
375
+ pass_along_path ( & nodes) ;
376
+
377
+ // Make sure the last node successfully decoded the reply path.
378
+ nodes[ 0 ] . custom_message_handler . expect_message ( TestCustomMessage :: Response ) ;
379
+ nodes. reverse ( ) ;
358
380
pass_along_path ( & nodes) ;
359
- nodes[ 3 ] . logger . assert_log_contains (
360
- "lightning::onion_message::messenger" ,
361
- & format ! ( "Received an onion message with path_id None and a reply_path" ) , 2 ) ;
362
381
}
363
382
364
383
#[ test]
@@ -389,7 +408,7 @@ fn invalid_custom_message_type() {
389
408
#[ test]
390
409
fn peer_buffer_full ( ) {
391
410
let nodes = create_nodes ( 2 ) ;
392
- let test_msg = TestCustomMessage :: Response ;
411
+ let test_msg = TestCustomMessage :: Request ;
393
412
let path = OnionMessagePath {
394
413
intermediate_nodes : vec ! [ ] ,
395
414
destination : Destination :: Node ( nodes[ 1 ] . get_node_pk ( ) ) ,
@@ -419,5 +438,6 @@ fn many_hops() {
419
438
destination : Destination :: Node ( nodes[ num_nodes-1 ] . get_node_pk ( ) ) ,
420
439
} ;
421
440
nodes[ 0 ] . messenger . send_onion_message ( path, OnionMessageContents :: Custom ( test_msg) , None ) . unwrap ( ) ;
441
+ nodes[ num_nodes-1 ] . custom_message_handler . expect_message ( TestCustomMessage :: Response ) ;
422
442
pass_along_path ( & nodes) ;
423
443
}
0 commit comments