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