11
11
12
12
use crate :: blinded_path:: BlindedPath ;
13
13
use crate :: events:: { Event , EventsProvider } ;
14
- use crate :: ln:: features:: InitFeatures ;
15
- use crate :: ln:: msgs:: { self , DecodeError , OnionMessageHandler , SocketAddress } ;
14
+ use crate :: ln:: features:: { ChannelFeatures , InitFeatures } ;
15
+ use crate :: ln:: msgs:: { self , DecodeError , OnionMessageHandler } ;
16
+ use crate :: routing:: gossip:: { NetworkGraph , P2PGossipSync } ;
17
+ use crate :: routing:: test_utils:: { add_channel, add_or_update_node} ;
16
18
use crate :: sign:: { NodeSigner , Recipient } ;
17
19
use crate :: util:: ser:: { FixedLengthReader , LengthReadable , Writeable , Writer } ;
18
20
use crate :: util:: test_utils;
19
- use super :: messenger:: { CustomOnionMessageHandler , Destination , MessageRouter , OnionMessagePath , OnionMessenger , PendingOnionMessage , SendError } ;
21
+ use super :: messenger:: { CustomOnionMessageHandler , DefaultMessageRouter , Destination , OnionMessagePath , OnionMessenger , PendingOnionMessage , SendError } ;
20
22
use super :: offers:: { OffersMessage , OffersMessageHandler } ;
21
23
use super :: packet:: { OnionMessageContents , Packet } ;
22
24
23
25
use bitcoin:: network:: constants:: Network ;
24
26
use bitcoin:: hashes:: hex:: FromHex ;
25
- use bitcoin:: secp256k1:: { PublicKey , Secp256k1 , SecretKey , self } ;
27
+ use bitcoin:: secp256k1:: { All , PublicKey , Secp256k1 , SecretKey } ;
26
28
27
29
use crate :: io;
28
30
use crate :: io_extras:: read_to_end;
29
31
use crate :: sync:: { Arc , Mutex } ;
30
32
33
+ use core:: ops:: Deref ;
34
+
31
35
use crate :: prelude:: * ;
32
36
33
37
struct MessengerNode {
34
38
node_id : PublicKey ,
39
+ privkey : SecretKey ,
35
40
entropy_source : Arc < test_utils:: TestKeysInterface > ,
36
41
messenger : OnionMessenger <
37
42
Arc < test_utils:: TestKeysInterface > ,
38
43
Arc < test_utils:: TestNodeSigner > ,
39
44
Arc < test_utils:: TestLogger > ,
40
- Arc < TestMessageRouter > ,
45
+ Arc < DefaultMessageRouter <
46
+ Arc < NetworkGraph < Arc < test_utils:: TestLogger > > > ,
47
+ Arc < test_utils:: TestLogger > ,
48
+ Arc < test_utils:: TestKeysInterface >
49
+ > > ,
41
50
Arc < TestOffersMessageHandler > ,
42
51
Arc < TestCustomMessageHandler >
43
52
> ,
44
53
custom_message_handler : Arc < TestCustomMessageHandler > ,
45
- }
46
-
47
- struct TestMessageRouter { }
48
-
49
- impl MessageRouter for TestMessageRouter {
50
- fn find_path (
51
- & self , _sender : PublicKey , _peers : Vec < PublicKey > , destination : Destination
52
- ) -> Result < OnionMessagePath , ( ) > {
53
- Ok ( OnionMessagePath {
54
- intermediate_nodes : vec ! [ ] ,
55
- destination,
56
- first_node_addresses :
57
- Some ( vec ! [ SocketAddress :: TcpIpV4 { addr: [ 127 , 0 , 0 , 1 ] , port: 1000 } ] ) ,
58
- } )
59
- }
60
-
61
- fn create_blinded_paths <
62
- T : secp256k1:: Signing + secp256k1:: Verification
63
- > (
64
- & self , _recipient : PublicKey , _peers : Vec < PublicKey > , _secp_ctx : & Secp256k1 < T > ,
65
- ) -> Result < Vec < BlindedPath > , ( ) > {
66
- unreachable ! ( )
67
- }
54
+ gossip_sync : Arc < P2PGossipSync <
55
+ Arc < NetworkGraph < Arc < test_utils:: TestLogger > > > ,
56
+ Arc < test_utils:: TestChainSource > ,
57
+ Arc < test_utils:: TestLogger >
58
+ > >
68
59
}
69
60
70
61
struct TestOffersMessageHandler { }
@@ -171,24 +162,34 @@ fn create_nodes(num_messengers: u8) -> Vec<MessengerNode> {
171
162
}
172
163
173
164
fn create_nodes_using_secrets ( secrets : Vec < SecretKey > ) -> Vec < MessengerNode > {
165
+ let gossip_logger = Arc :: new ( test_utils:: TestLogger :: with_id ( "gossip" . to_string ( ) ) ) ;
166
+ let network_graph = Arc :: new ( NetworkGraph :: new ( Network :: Testnet , gossip_logger. clone ( ) ) ) ;
167
+ let gossip_sync = Arc :: new (
168
+ P2PGossipSync :: new ( network_graph. clone ( ) , None , gossip_logger)
169
+ ) ;
170
+
174
171
let mut nodes = Vec :: new ( ) ;
175
172
for ( i, secret_key) in secrets. into_iter ( ) . enumerate ( ) {
176
173
let logger = Arc :: new ( test_utils:: TestLogger :: with_id ( format ! ( "node {}" , i) ) ) ;
177
174
let seed = [ i as u8 ; 32 ] ;
178
175
let entropy_source = Arc :: new ( test_utils:: TestKeysInterface :: new ( & seed, Network :: Testnet ) ) ;
179
176
let node_signer = Arc :: new ( test_utils:: TestNodeSigner :: new ( secret_key) ) ;
180
177
181
- let message_router = Arc :: new ( TestMessageRouter { } ) ;
178
+ let message_router = Arc :: new (
179
+ DefaultMessageRouter :: new ( network_graph. clone ( ) , entropy_source. clone ( ) )
180
+ ) ;
182
181
let offers_message_handler = Arc :: new ( TestOffersMessageHandler { } ) ;
183
182
let custom_message_handler = Arc :: new ( TestCustomMessageHandler :: new ( ) ) ;
184
183
nodes. push ( MessengerNode {
184
+ privkey : secret_key,
185
185
node_id : node_signer. get_node_id ( Recipient :: Node ) . unwrap ( ) ,
186
186
entropy_source : entropy_source. clone ( ) ,
187
187
messenger : OnionMessenger :: new (
188
188
entropy_source, node_signer, logger. clone ( ) , message_router,
189
189
offers_message_handler, custom_message_handler. clone ( )
190
190
) ,
191
191
custom_message_handler,
192
+ gossip_sync : gossip_sync. clone ( ) ,
192
193
} ) ;
193
194
}
194
195
for i in 0 ..nodes. len ( ) - 1 {
@@ -216,6 +217,20 @@ fn release_events(node: &MessengerNode) -> Vec<Event> {
216
217
events. into_inner ( )
217
218
}
218
219
220
+ fn add_channel_to_graph (
221
+ node_a : & MessengerNode , node_b : & MessengerNode , secp_ctx : & Secp256k1 < All > , short_channel_id : u64
222
+ ) {
223
+ let gossip_sync = node_a. gossip_sync . deref ( ) ;
224
+ let privkey_a = & node_a. privkey ;
225
+ let privkey_b = & node_b. privkey ;
226
+ let channel_features = ChannelFeatures :: empty ( ) ;
227
+ let node_features_a = node_a. messenger . provided_node_features ( ) ;
228
+ let node_features_b = node_b. messenger . provided_node_features ( ) ;
229
+ add_channel ( gossip_sync, secp_ctx, privkey_a, privkey_b, channel_features, short_channel_id) ;
230
+ add_or_update_node ( gossip_sync, secp_ctx, privkey_a, node_features_a, 1 ) ;
231
+ add_or_update_node ( gossip_sync, secp_ctx, privkey_b, node_features_b, 1 ) ;
232
+ }
233
+
219
234
fn pass_along_path ( path : & Vec < MessengerNode > ) {
220
235
let mut prev_node = & path[ 0 ] ;
221
236
for node in path. into_iter ( ) . skip ( 1 ) {
@@ -235,12 +250,8 @@ fn one_unblinded_hop() {
235
250
let nodes = create_nodes ( 2 ) ;
236
251
let test_msg = TestCustomMessage :: Response ;
237
252
238
- let path = OnionMessagePath {
239
- intermediate_nodes : vec ! [ ] ,
240
- destination : Destination :: Node ( nodes[ 1 ] . node_id ) ,
241
- first_node_addresses : None ,
242
- } ;
243
- nodes[ 0 ] . messenger . send_onion_message_using_path ( path, test_msg, None ) . unwrap ( ) ;
253
+ let destination = Destination :: Node ( nodes[ 1 ] . node_id ) ;
254
+ nodes[ 0 ] . messenger . send_onion_message ( test_msg, destination, None ) . unwrap ( ) ;
244
255
nodes[ 1 ] . custom_message_handler . expect_message ( TestCustomMessage :: Response ) ;
245
256
pass_along_path ( & nodes) ;
246
257
}
@@ -255,6 +266,7 @@ fn two_unblinded_hops() {
255
266
destination : Destination :: Node ( nodes[ 2 ] . node_id ) ,
256
267
first_node_addresses : None ,
257
268
} ;
269
+
258
270
nodes[ 0 ] . messenger . send_onion_message_using_path ( path, test_msg, None ) . unwrap ( ) ;
259
271
nodes[ 2 ] . custom_message_handler . expect_message ( TestCustomMessage :: Response ) ;
260
272
pass_along_path ( & nodes) ;
@@ -267,12 +279,8 @@ fn one_blinded_hop() {
267
279
268
280
let secp_ctx = Secp256k1 :: new ( ) ;
269
281
let blinded_path = BlindedPath :: new_for_message ( & [ nodes[ 1 ] . node_id ] , & * nodes[ 1 ] . entropy_source , & secp_ctx) . unwrap ( ) ;
270
- let path = OnionMessagePath {
271
- intermediate_nodes : vec ! [ ] ,
272
- destination : Destination :: BlindedPath ( blinded_path) ,
273
- first_node_addresses : None ,
274
- } ;
275
- nodes[ 0 ] . messenger . send_onion_message_using_path ( path, test_msg, None ) . unwrap ( ) ;
282
+ let destination = Destination :: BlindedPath ( blinded_path) ;
283
+ nodes[ 0 ] . messenger . send_onion_message ( test_msg, destination, None ) . unwrap ( ) ;
276
284
nodes[ 1 ] . custom_message_handler . expect_message ( TestCustomMessage :: Response ) ;
277
285
pass_along_path ( & nodes) ;
278
286
}
@@ -302,13 +310,9 @@ fn three_blinded_hops() {
302
310
303
311
let secp_ctx = Secp256k1 :: new ( ) ;
304
312
let blinded_path = BlindedPath :: new_for_message ( & [ nodes[ 1 ] . node_id , nodes[ 2 ] . node_id , nodes[ 3 ] . node_id ] , & * nodes[ 3 ] . entropy_source , & secp_ctx) . unwrap ( ) ;
305
- let path = OnionMessagePath {
306
- intermediate_nodes : vec ! [ ] ,
307
- destination : Destination :: BlindedPath ( blinded_path) ,
308
- first_node_addresses : None ,
309
- } ;
313
+ let destination = Destination :: BlindedPath ( blinded_path) ;
310
314
311
- nodes[ 0 ] . messenger . send_onion_message_using_path ( path , test_msg , None ) . unwrap ( ) ;
315
+ nodes[ 0 ] . messenger . send_onion_message ( test_msg , destination , None ) . unwrap ( ) ;
312
316
nodes[ 3 ] . custom_message_handler . expect_message ( TestCustomMessage :: Response ) ;
313
317
pass_along_path ( & nodes) ;
314
318
}
@@ -339,24 +343,16 @@ fn we_are_intro_node() {
339
343
340
344
let secp_ctx = Secp256k1 :: new ( ) ;
341
345
let blinded_path = BlindedPath :: new_for_message ( & [ nodes[ 0 ] . node_id , nodes[ 1 ] . node_id , nodes[ 2 ] . node_id ] , & * nodes[ 2 ] . entropy_source , & secp_ctx) . unwrap ( ) ;
342
- let path = OnionMessagePath {
343
- intermediate_nodes : vec ! [ ] ,
344
- destination : Destination :: BlindedPath ( blinded_path) ,
345
- first_node_addresses : None ,
346
- } ;
346
+ let destination = Destination :: BlindedPath ( blinded_path) ;
347
347
348
- nodes[ 0 ] . messenger . send_onion_message_using_path ( path , test_msg. clone ( ) , None ) . unwrap ( ) ;
348
+ nodes[ 0 ] . messenger . send_onion_message ( test_msg. clone ( ) , destination , None ) . unwrap ( ) ;
349
349
nodes[ 2 ] . custom_message_handler . expect_message ( TestCustomMessage :: Response ) ;
350
350
pass_along_path ( & nodes) ;
351
351
352
352
// Try with a two-hop blinded path where we are the introduction node.
353
353
let blinded_path = BlindedPath :: new_for_message ( & [ nodes[ 0 ] . node_id , nodes[ 1 ] . node_id ] , & * nodes[ 1 ] . entropy_source , & secp_ctx) . unwrap ( ) ;
354
- let path = OnionMessagePath {
355
- intermediate_nodes : vec ! [ ] ,
356
- destination : Destination :: BlindedPath ( blinded_path) ,
357
- first_node_addresses : None ,
358
- } ;
359
- nodes[ 0 ] . messenger . send_onion_message_using_path ( path, test_msg, None ) . unwrap ( ) ;
354
+ let destination = Destination :: BlindedPath ( blinded_path) ;
355
+ nodes[ 0 ] . messenger . send_onion_message ( test_msg, destination, None ) . unwrap ( ) ;
360
356
nodes[ 1 ] . custom_message_handler . expect_message ( TestCustomMessage :: Response ) ;
361
357
nodes. remove ( 2 ) ;
362
358
pass_along_path ( & nodes) ;
@@ -372,12 +368,8 @@ fn invalid_blinded_path_error() {
372
368
let secp_ctx = Secp256k1 :: new ( ) ;
373
369
let mut blinded_path = BlindedPath :: new_for_message ( & [ nodes[ 1 ] . node_id , nodes[ 2 ] . node_id ] , & * nodes[ 2 ] . entropy_source , & secp_ctx) . unwrap ( ) ;
374
370
blinded_path. blinded_hops . clear ( ) ;
375
- let path = OnionMessagePath {
376
- intermediate_nodes : vec ! [ ] ,
377
- destination : Destination :: BlindedPath ( blinded_path) ,
378
- first_node_addresses : None ,
379
- } ;
380
- let err = nodes[ 0 ] . messenger . send_onion_message_using_path ( path, test_msg. clone ( ) , None ) . unwrap_err ( ) ;
371
+ let destination = Destination :: BlindedPath ( blinded_path) ;
372
+ let err = nodes[ 0 ] . messenger . send_onion_message ( test_msg, destination, None ) . unwrap_err ( ) ;
381
373
assert_eq ! ( err, SendError :: TooFewBlindedHops ) ;
382
374
}
383
375
@@ -404,14 +396,10 @@ fn reply_path() {
404
396
405
397
// Destination::BlindedPath
406
398
let blinded_path = BlindedPath :: new_for_message ( & [ nodes[ 1 ] . node_id , nodes[ 2 ] . node_id , nodes[ 3 ] . node_id ] , & * nodes[ 3 ] . entropy_source , & secp_ctx) . unwrap ( ) ;
407
- let path = OnionMessagePath {
408
- intermediate_nodes : vec ! [ ] ,
409
- destination : Destination :: BlindedPath ( blinded_path) ,
410
- first_node_addresses : None ,
411
- } ;
399
+ let destination = Destination :: BlindedPath ( blinded_path) ;
412
400
let reply_path = BlindedPath :: new_for_message ( & [ nodes[ 2 ] . node_id , nodes[ 1 ] . node_id , nodes[ 0 ] . node_id ] , & * nodes[ 0 ] . entropy_source , & secp_ctx) . unwrap ( ) ;
413
401
414
- nodes[ 0 ] . messenger . send_onion_message_using_path ( path , test_msg , Some ( reply_path) ) . unwrap ( ) ;
402
+ nodes[ 0 ] . messenger . send_onion_message ( test_msg , destination , Some ( reply_path) ) . unwrap ( ) ;
415
403
nodes[ 3 ] . custom_message_handler . expect_message ( TestCustomMessage :: Request ) ;
416
404
pass_along_path ( & nodes) ;
417
405
@@ -439,28 +427,20 @@ fn invalid_custom_message_type() {
439
427
}
440
428
441
429
let test_msg = InvalidCustomMessage { } ;
442
- let path = OnionMessagePath {
443
- intermediate_nodes : vec ! [ ] ,
444
- destination : Destination :: Node ( nodes[ 1 ] . node_id ) ,
445
- first_node_addresses : None ,
446
- } ;
447
- let err = nodes[ 0 ] . messenger . send_onion_message_using_path ( path, test_msg, None ) . unwrap_err ( ) ;
430
+ let destination = Destination :: Node ( nodes[ 1 ] . node_id ) ;
431
+ let err = nodes[ 0 ] . messenger . send_onion_message ( test_msg, destination, None ) . unwrap_err ( ) ;
448
432
assert_eq ! ( err, SendError :: InvalidMessage ) ;
449
433
}
450
434
451
435
#[ test]
452
436
fn peer_buffer_full ( ) {
453
437
let nodes = create_nodes ( 2 ) ;
454
438
let test_msg = TestCustomMessage :: Request ;
455
- let path = OnionMessagePath {
456
- intermediate_nodes : vec ! [ ] ,
457
- destination : Destination :: Node ( nodes[ 1 ] . node_id ) ,
458
- first_node_addresses : None ,
459
- } ;
439
+ let destination = Destination :: Node ( nodes[ 1 ] . node_id ) ;
460
440
for _ in 0 ..188 { // Based on MAX_PER_PEER_BUFFER_SIZE in OnionMessenger
461
- nodes[ 0 ] . messenger . send_onion_message_using_path ( path . clone ( ) , test_msg . clone ( ) , None ) . unwrap ( ) ;
441
+ nodes[ 0 ] . messenger . send_onion_message ( test_msg . clone ( ) , destination . clone ( ) , None ) . unwrap ( ) ;
462
442
}
463
- let err = nodes[ 0 ] . messenger . send_onion_message_using_path ( path , test_msg , None ) . unwrap_err ( ) ;
443
+ let err = nodes[ 0 ] . messenger . send_onion_message ( test_msg , destination , None ) . unwrap_err ( ) ;
464
444
assert_eq ! ( err, SendError :: BufferFull ) ;
465
445
}
466
446
@@ -492,6 +472,8 @@ fn requests_peer_connection_for_buffered_messages() {
492
472
let nodes = create_nodes ( 3 ) ;
493
473
let message = TestCustomMessage :: Request ;
494
474
let secp_ctx = Secp256k1 :: new ( ) ;
475
+ add_channel_to_graph ( & nodes[ 0 ] , & nodes[ 1 ] , & secp_ctx, 42 ) ;
476
+
495
477
let blinded_path = BlindedPath :: new_for_message (
496
478
& [ nodes[ 1 ] . node_id , nodes[ 2 ] . node_id ] , & * nodes[ 0 ] . entropy_source , & secp_ctx
497
479
) . unwrap ( ) ;
@@ -527,6 +509,8 @@ fn drops_buffered_messages_waiting_for_peer_connection() {
527
509
let nodes = create_nodes ( 3 ) ;
528
510
let message = TestCustomMessage :: Request ;
529
511
let secp_ctx = Secp256k1 :: new ( ) ;
512
+ add_channel_to_graph ( & nodes[ 0 ] , & nodes[ 1 ] , & secp_ctx, 42 ) ;
513
+
530
514
let blinded_path = BlindedPath :: new_for_message (
531
515
& [ nodes[ 1 ] . node_id , nodes[ 2 ] . node_id ] , & * nodes[ 0 ] . entropy_source , & secp_ctx
532
516
) . unwrap ( ) ;
0 commit comments