@@ -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 {
@@ -51,7 +51,7 @@ impl MessageRouter for TestMessageRouter {
51
51
fn find_route (
52
52
& self , _sender : & PublicKey , _destination : & Destination
53
53
) -> Result < Vec < PublicKey > , ( ) > {
54
- todo ! ( )
54
+ Ok ( vec ! [ ] )
55
55
}
56
56
}
57
57
@@ -69,7 +69,7 @@ impl ResponseErrorHandler for TestOffersMessageHandler {
69
69
}
70
70
}
71
71
72
- #[ derive( Clone ) ]
72
+ #[ derive( Clone , Debug , PartialEq ) ]
73
73
enum TestCustomMessage {
74
74
Request ,
75
75
Response ,
@@ -99,12 +99,16 @@ impl Writeable for TestCustomMessage {
99
99
}
100
100
101
101
struct TestCustomMessageHandler {
102
- num_messages_expected : AtomicU16 ,
102
+ expected_messages : Mutex < VecDeque < TestCustomMessage > > ,
103
103
}
104
104
105
105
impl TestCustomMessageHandler {
106
106
fn new ( ) -> Self {
107
- Self { num_messages_expected : AtomicU16 :: new ( 0 ) }
107
+ Self { expected_messages : Mutex :: new ( VecDeque :: new ( ) ) }
108
+ }
109
+
110
+ fn expect_message ( & self , message : TestCustomMessage ) {
111
+ self . expected_messages . lock ( ) . unwrap ( ) . push_back ( message) ;
108
112
}
109
113
}
110
114
@@ -115,14 +119,18 @@ impl Drop for TestCustomMessageHandler {
115
119
return ;
116
120
}
117
121
}
118
- assert_eq ! ( self . num_messages_expected . load ( Ordering :: SeqCst ) , 0 ) ;
122
+ assert ! ( self . expected_messages . lock ( ) . unwrap ( ) . is_empty ( ) ) ;
119
123
}
120
124
}
121
125
122
126
impl CustomOnionMessageHandler for TestCustomMessageHandler {
123
127
type CustomMessage = TestCustomMessage ;
124
128
fn handle_custom_message ( & self , msg : Self :: CustomMessage ) -> Option < Self :: CustomMessage > {
125
- self . num_messages_expected . fetch_sub ( 1 , Ordering :: SeqCst ) ;
129
+ match self . expected_messages . lock ( ) . unwrap ( ) . pop_front ( ) {
130
+ Some ( expected_msg) => assert_eq ! ( expected_msg, msg) ,
131
+ None => panic ! ( "Unexpected message: {:?}" , msg) ,
132
+ }
133
+
126
134
match msg {
127
135
TestCustomMessage :: Request => Some ( TestCustomMessage :: Response ) ,
128
136
TestCustomMessage :: Response => None ,
@@ -167,7 +175,6 @@ fn create_nodes(num_messengers: u8) -> Vec<MessengerNode> {
167
175
offers_message_handler, custom_message_handler. clone ( )
168
176
) ,
169
177
custom_message_handler,
170
- logger,
171
178
} ) ;
172
179
}
173
180
for idx in 0 ..num_messengers - 1 {
@@ -182,7 +189,6 @@ fn create_nodes(num_messengers: u8) -> Vec<MessengerNode> {
182
189
}
183
190
184
191
fn pass_along_path ( path : & Vec < MessengerNode > ) {
185
- path[ path. len ( ) - 1 ] . custom_message_handler . num_messages_expected . fetch_add ( 1 , Ordering :: SeqCst ) ;
186
192
let mut prev_node = & path[ 0 ] ;
187
193
for node in path. into_iter ( ) . skip ( 1 ) {
188
194
let events = prev_node. messenger . release_pending_msgs ( ) ;
@@ -202,6 +208,7 @@ fn one_hop() {
202
208
let test_msg = OnionMessageContents :: Custom ( TestCustomMessage :: Response ) ;
203
209
204
210
nodes[ 0 ] . messenger . send_onion_message ( & [ ] , Destination :: Node ( nodes[ 1 ] . get_node_pk ( ) ) , test_msg, None ) . unwrap ( ) ;
211
+ nodes[ 1 ] . custom_message_handler . expect_message ( TestCustomMessage :: Response ) ;
205
212
pass_along_path ( & nodes) ;
206
213
}
207
214
@@ -211,6 +218,7 @@ fn two_unblinded_hops() {
211
218
let test_msg = OnionMessageContents :: Custom ( TestCustomMessage :: Response ) ;
212
219
213
220
nodes[ 0 ] . messenger . send_onion_message ( & [ nodes[ 1 ] . get_node_pk ( ) ] , Destination :: Node ( nodes[ 2 ] . get_node_pk ( ) ) , test_msg, None ) . unwrap ( ) ;
221
+ nodes[ 2 ] . custom_message_handler . expect_message ( TestCustomMessage :: Response ) ;
214
222
pass_along_path ( & nodes) ;
215
223
}
216
224
@@ -223,6 +231,7 @@ fn two_unblinded_two_blinded() {
223
231
let blinded_path = BlindedPath :: new_for_message ( & [ nodes[ 3 ] . get_node_pk ( ) , nodes[ 4 ] . get_node_pk ( ) ] , & * nodes[ 4 ] . keys_manager , & secp_ctx) . unwrap ( ) ;
224
232
225
233
nodes[ 0 ] . messenger . send_onion_message ( & [ nodes[ 1 ] . get_node_pk ( ) , nodes[ 2 ] . get_node_pk ( ) ] , Destination :: BlindedPath ( blinded_path) , test_msg, None ) . unwrap ( ) ;
234
+ nodes[ 4 ] . custom_message_handler . expect_message ( TestCustomMessage :: Response ) ;
226
235
pass_along_path ( & nodes) ;
227
236
}
228
237
@@ -235,6 +244,7 @@ fn three_blinded_hops() {
235
244
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 ( ) ;
236
245
237
246
nodes[ 0 ] . messenger . send_onion_message ( & [ ] , Destination :: BlindedPath ( blinded_path) , test_msg, None ) . unwrap ( ) ;
247
+ nodes[ 3 ] . custom_message_handler . expect_message ( TestCustomMessage :: Response ) ;
238
248
pass_along_path ( & nodes) ;
239
249
}
240
250
@@ -261,11 +271,13 @@ fn we_are_intro_node() {
261
271
let blinded_path = BlindedPath :: new_for_message ( & [ nodes[ 0 ] . get_node_pk ( ) , nodes[ 1 ] . get_node_pk ( ) , nodes[ 2 ] . get_node_pk ( ) ] , & * nodes[ 2 ] . keys_manager , & secp_ctx) . unwrap ( ) ;
262
272
263
273
nodes[ 0 ] . messenger . send_onion_message ( & [ ] , Destination :: BlindedPath ( blinded_path) , OnionMessageContents :: Custom ( test_msg. clone ( ) ) , None ) . unwrap ( ) ;
274
+ nodes[ 2 ] . custom_message_handler . expect_message ( TestCustomMessage :: Response ) ;
264
275
pass_along_path ( & nodes) ;
265
276
266
277
// Try with a two-hop blinded path where we are the introduction node.
267
278
let blinded_path = BlindedPath :: new_for_message ( & [ nodes[ 0 ] . get_node_pk ( ) , nodes[ 1 ] . get_node_pk ( ) ] , & * nodes[ 1 ] . keys_manager , & secp_ctx) . unwrap ( ) ;
268
279
nodes[ 0 ] . messenger . send_onion_message ( & [ ] , Destination :: BlindedPath ( blinded_path) , OnionMessageContents :: Custom ( test_msg) , None ) . unwrap ( ) ;
280
+ nodes[ 1 ] . custom_message_handler . expect_message ( TestCustomMessage :: Response ) ;
269
281
nodes. remove ( 2 ) ;
270
282
pass_along_path ( & nodes) ;
271
283
}
@@ -293,28 +305,32 @@ fn invalid_blinded_path_error() {
293
305
294
306
#[ test]
295
307
fn reply_path ( ) {
296
- let nodes = create_nodes ( 4 ) ;
297
- let test_msg = TestCustomMessage :: Response ;
308
+ let mut nodes = create_nodes ( 4 ) ;
309
+ let test_msg = TestCustomMessage :: Request ;
298
310
let secp_ctx = Secp256k1 :: new ( ) ;
299
311
300
312
// Destination::Node
301
313
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 ( ) ;
302
314
nodes[ 0 ] . messenger . send_onion_message ( & [ nodes[ 1 ] . get_node_pk ( ) , nodes[ 2 ] . get_node_pk ( ) ] , Destination :: Node ( nodes[ 3 ] . get_node_pk ( ) ) , OnionMessageContents :: Custom ( test_msg. clone ( ) ) , Some ( reply_path) ) . unwrap ( ) ;
315
+ nodes[ 3 ] . custom_message_handler . expect_message ( TestCustomMessage :: Request ) ;
303
316
pass_along_path ( & nodes) ;
304
317
// Make sure the last node successfully decoded the reply path.
305
- nodes[ 3 ] . logger . assert_log_contains (
306
- "lightning::onion_message::messenger" ,
307
- & format ! ( "Received an onion message with path_id None and a reply_path" ) , 1 ) ;
318
+ nodes[ 0 ] . custom_message_handler . expect_message ( TestCustomMessage :: Response ) ;
319
+ nodes . reverse ( ) ;
320
+ pass_along_path ( & nodes ) ;
308
321
309
322
// Destination::BlindedPath
310
323
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 ( ) ;
311
324
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 ( ) ;
312
325
313
326
nodes[ 0 ] . messenger . send_onion_message ( & [ ] , Destination :: BlindedPath ( blinded_path) , OnionMessageContents :: Custom ( test_msg) , Some ( reply_path) ) . unwrap ( ) ;
327
+ nodes[ 3 ] . custom_message_handler . expect_message ( TestCustomMessage :: Request ) ;
328
+ pass_along_path ( & nodes) ;
329
+
330
+ // Make sure the last node successfully decoded the reply path.
331
+ nodes[ 0 ] . custom_message_handler . expect_message ( TestCustomMessage :: Response ) ;
332
+ nodes. reverse ( ) ;
314
333
pass_along_path ( & nodes) ;
315
- nodes[ 3 ] . logger . assert_log_contains (
316
- "lightning::onion_message::messenger" ,
317
- & format ! ( "Received an onion message with path_id None and a reply_path" ) , 2 ) ;
318
334
}
319
335
320
336
#[ test]
@@ -341,7 +357,7 @@ fn invalid_custom_message_type() {
341
357
#[ test]
342
358
fn peer_buffer_full ( ) {
343
359
let nodes = create_nodes ( 2 ) ;
344
- let test_msg = TestCustomMessage :: Response ;
360
+ let test_msg = TestCustomMessage :: Request ;
345
361
for _ in 0 ..188 { // Based on MAX_PER_PEER_BUFFER_SIZE in OnionMessenger
346
362
nodes[ 0 ] . messenger . send_onion_message ( & [ ] , Destination :: Node ( nodes[ 1 ] . get_node_pk ( ) ) , OnionMessageContents :: Custom ( test_msg. clone ( ) ) , None ) . unwrap ( ) ;
347
363
}
@@ -363,5 +379,6 @@ fn many_hops() {
363
379
}
364
380
365
381
nodes[ 0 ] . messenger . send_onion_message ( & intermediates, Destination :: Node ( nodes[ num_nodes-1 ] . get_node_pk ( ) ) , OnionMessageContents :: Custom ( test_msg) , None ) . unwrap ( ) ;
382
+ nodes[ num_nodes-1 ] . custom_message_handler . expect_message ( TestCustomMessage :: Response ) ;
366
383
pass_along_path ( & nodes) ;
367
384
}
0 commit comments