@@ -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 , _peers : Vec < PublicKey > , _destination : Destination
52
+ & self , _sender : PublicKey , _peers : Vec < 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
@@ -63,7 +66,7 @@ impl OffersMessageHandler for TestOffersMessageHandler {
63
66
}
64
67
}
65
68
66
- #[ derive( Clone ) ]
69
+ #[ derive( Clone , Debug , PartialEq ) ]
67
70
enum TestCustomMessage {
68
71
Request ,
69
72
Response ,
@@ -93,12 +96,16 @@ impl Writeable for TestCustomMessage {
93
96
}
94
97
95
98
struct TestCustomMessageHandler {
96
- num_messages_expected : AtomicU16 ,
99
+ expected_messages : Mutex < VecDeque < TestCustomMessage > > ,
97
100
}
98
101
99
102
impl TestCustomMessageHandler {
100
103
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) ;
102
109
}
103
110
}
104
111
@@ -109,14 +116,18 @@ impl Drop for TestCustomMessageHandler {
109
116
return ;
110
117
}
111
118
}
112
- assert_eq ! ( self . num_messages_expected . load ( Ordering :: SeqCst ) , 0 ) ;
119
+ assert ! ( self . expected_messages . lock ( ) . unwrap ( ) . is_empty ( ) ) ;
113
120
}
114
121
}
115
122
116
123
impl CustomOnionMessageHandler for TestCustomMessageHandler {
117
124
type CustomMessage = TestCustomMessage ;
118
125
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
+
120
131
match msg {
121
132
TestCustomMessage :: Request => Some ( TestCustomMessage :: Response ) ,
122
133
TestCustomMessage :: Response => None ,
@@ -155,7 +166,6 @@ fn create_nodes(num_messengers: u8) -> Vec<MessengerNode> {
155
166
offers_message_handler, custom_message_handler. clone ( )
156
167
) ,
157
168
custom_message_handler,
158
- logger,
159
169
} ) ;
160
170
}
161
171
for idx in 0 ..num_messengers - 1 {
@@ -170,7 +180,6 @@ fn create_nodes(num_messengers: u8) -> Vec<MessengerNode> {
170
180
}
171
181
172
182
fn pass_along_path ( path : & Vec < MessengerNode > ) {
173
- path[ path. len ( ) - 1 ] . custom_message_handler . num_messages_expected . fetch_add ( 1 , Ordering :: SeqCst ) ;
174
183
let mut prev_node = & path[ 0 ] ;
175
184
for node in path. into_iter ( ) . skip ( 1 ) {
176
185
let events = prev_node. messenger . release_pending_msgs ( ) ;
@@ -194,6 +203,7 @@ fn one_hop() {
194
203
destination : Destination :: Node ( nodes[ 1 ] . get_node_pk ( ) ) ,
195
204
} ;
196
205
nodes[ 0 ] . messenger . send_onion_message ( path, test_msg, None ) . unwrap ( ) ;
206
+ nodes[ 1 ] . custom_message_handler . expect_message ( TestCustomMessage :: Response ) ;
197
207
pass_along_path ( & nodes) ;
198
208
}
199
209
@@ -207,6 +217,7 @@ fn two_unblinded_hops() {
207
217
destination : Destination :: Node ( nodes[ 2 ] . get_node_pk ( ) ) ,
208
218
} ;
209
219
nodes[ 0 ] . messenger . send_onion_message ( path, test_msg, None ) . unwrap ( ) ;
220
+ nodes[ 2 ] . custom_message_handler . expect_message ( TestCustomMessage :: Response ) ;
210
221
pass_along_path ( & nodes) ;
211
222
}
212
223
@@ -223,6 +234,7 @@ fn two_unblinded_two_blinded() {
223
234
} ;
224
235
225
236
nodes[ 0 ] . messenger . send_onion_message ( path, test_msg, None ) . unwrap ( ) ;
237
+ nodes[ 4 ] . custom_message_handler . expect_message ( TestCustomMessage :: Response ) ;
226
238
pass_along_path ( & nodes) ;
227
239
}
228
240
@@ -239,6 +251,7 @@ fn three_blinded_hops() {
239
251
} ;
240
252
241
253
nodes[ 0 ] . messenger . send_onion_message ( path, test_msg, None ) . unwrap ( ) ;
254
+ nodes[ 3 ] . custom_message_handler . expect_message ( TestCustomMessage :: Response ) ;
242
255
pass_along_path ( & nodes) ;
243
256
}
244
257
@@ -273,6 +286,7 @@ fn we_are_intro_node() {
273
286
} ;
274
287
275
288
nodes[ 0 ] . messenger . send_onion_message ( path, OnionMessageContents :: Custom ( test_msg. clone ( ) ) , None ) . unwrap ( ) ;
289
+ nodes[ 2 ] . custom_message_handler . expect_message ( TestCustomMessage :: Response ) ;
276
290
pass_along_path ( & nodes) ;
277
291
278
292
// Try with a two-hop blinded path where we are the introduction node.
@@ -282,6 +296,7 @@ fn we_are_intro_node() {
282
296
destination : Destination :: BlindedPath ( blinded_path) ,
283
297
} ;
284
298
nodes[ 0 ] . messenger . send_onion_message ( path, OnionMessageContents :: Custom ( test_msg) , None ) . unwrap ( ) ;
299
+ nodes[ 1 ] . custom_message_handler . expect_message ( TestCustomMessage :: Response ) ;
285
300
nodes. remove ( 2 ) ;
286
301
pass_along_path ( & nodes) ;
287
302
}
@@ -317,8 +332,8 @@ fn invalid_blinded_path_error() {
317
332
318
333
#[ test]
319
334
fn 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 ;
322
337
let secp_ctx = Secp256k1 :: new ( ) ;
323
338
324
339
// Destination::Node
@@ -328,11 +343,12 @@ fn reply_path() {
328
343
} ;
329
344
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 ( ) ;
330
345
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 ) ;
331
347
pass_along_path ( & nodes) ;
332
348
// 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 ) ;
336
352
337
353
// Destination::BlindedPath
338
354
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() {
343
359
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 ( ) ;
344
360
345
361
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 ( ) ;
346
368
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 ) ;
350
369
}
351
370
352
371
#[ test]
@@ -377,7 +396,7 @@ fn invalid_custom_message_type() {
377
396
#[ test]
378
397
fn peer_buffer_full ( ) {
379
398
let nodes = create_nodes ( 2 ) ;
380
- let test_msg = TestCustomMessage :: Response ;
399
+ let test_msg = TestCustomMessage :: Request ;
381
400
let path = OnionMessagePath {
382
401
intermediate_nodes : vec ! [ ] ,
383
402
destination : Destination :: Node ( nodes[ 1 ] . get_node_pk ( ) ) ,
@@ -407,5 +426,6 @@ fn many_hops() {
407
426
destination : Destination :: Node ( nodes[ num_nodes-1 ] . get_node_pk ( ) ) ,
408
427
} ;
409
428
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 ) ;
410
430
pass_along_path ( & nodes) ;
411
431
}
0 commit comments