@@ -2377,5 +2377,149 @@ mod tests {
2377
2377
// TODO: Test HTLCFailChannelUpdate::NodeFailure, which is not implemented yet.
2378
2378
}
2379
2379
2380
+ #[ test]
2381
+ fn getting_next_channel_announcements ( ) {
2382
+ let ( secp_ctx, _, router) = create_router ( ) ;
2383
+ let node_1_privkey = & SecretKey :: from_slice ( & [ 42 ; 32 ] ) . unwrap ( ) ;
2384
+ let node_2_privkey = & SecretKey :: from_slice ( & [ 41 ; 32 ] ) . unwrap ( ) ;
2385
+ let node_id_1 = PublicKey :: from_secret_key ( & secp_ctx, node_1_privkey) ;
2386
+ let node_id_2 = PublicKey :: from_secret_key ( & secp_ctx, node_2_privkey) ;
2387
+ let node_1_btckey = & SecretKey :: from_slice ( & [ 40 ; 32 ] ) . unwrap ( ) ;
2388
+ let node_2_btckey = & SecretKey :: from_slice ( & [ 39 ; 32 ] ) . unwrap ( ) ;
2389
+
2390
+ let short_channel_id = 1 ;
2391
+ let chain_hash = genesis_block ( Network :: Testnet ) . header . bitcoin_hash ( ) ;
2392
+ let channel_key = NetworkMap :: get_key ( short_channel_id, chain_hash) ;
2393
+
2394
+ // No channels yet.
2395
+ let next_announcements = router. get_next_channel_announcements ( channel_key, 10 ) ;
2396
+ assert_eq ! ( next_announcements. len( ) , 0 ) ;
2397
+
2398
+ {
2399
+ // Announce a channel we will update
2400
+ let unsigned_announcement = UnsignedChannelAnnouncement {
2401
+ features : ChannelFeatures :: empty ( ) ,
2402
+ chain_hash,
2403
+ short_channel_id,
2404
+ node_id_1,
2405
+ node_id_2,
2406
+ bitcoin_key_1 : PublicKey :: from_secret_key ( & secp_ctx, node_1_btckey) ,
2407
+ bitcoin_key_2 : PublicKey :: from_secret_key ( & secp_ctx, node_2_btckey) ,
2408
+ excess_data : Vec :: new ( ) ,
2409
+ } ;
2410
+
2411
+ let msghash = hash_to_message ! ( & Sha256dHash :: hash( & unsigned_announcement. encode( ) [ ..] ) [ ..] ) ;
2412
+ let valid_channel_announcement = ChannelAnnouncement {
2413
+ node_signature_1 : secp_ctx. sign ( & msghash, node_1_privkey) ,
2414
+ node_signature_2 : secp_ctx. sign ( & msghash, node_2_privkey) ,
2415
+ bitcoin_signature_1 : secp_ctx. sign ( & msghash, node_1_btckey) ,
2416
+ bitcoin_signature_2 : secp_ctx. sign ( & msghash, node_2_btckey) ,
2417
+ contents : unsigned_announcement. clone ( ) ,
2418
+ } ;
2419
+ match router. handle_channel_announcement ( & valid_channel_announcement) {
2420
+ Ok ( _) => ( ) ,
2421
+ Err ( _) => panic ! ( )
2422
+ } ;
2423
+ }
2424
+
2425
+ // No updates.
2426
+ let next_announcements = router. get_next_channel_announcements ( channel_key, 2 ) ;
2427
+ assert_eq ! ( next_announcements. len( ) , 0 ) ;
2380
2428
2429
+ {
2430
+ let unsigned_channel_update = UnsignedChannelUpdate {
2431
+ chain_hash,
2432
+ short_channel_id,
2433
+ timestamp : 100 ,
2434
+ flags : 0 ,
2435
+ cltv_expiry_delta : 144 ,
2436
+ htlc_minimum_msat : 1000000 ,
2437
+ fee_base_msat : 10000 ,
2438
+ fee_proportional_millionths : 20 ,
2439
+ excess_data : Vec :: new ( )
2440
+ } ;
2441
+ let msghash = hash_to_message ! ( & Sha256dHash :: hash( & unsigned_channel_update. encode( ) [ ..] ) [ ..] ) ;
2442
+ let valid_channel_update = ChannelUpdate {
2443
+ signature : secp_ctx. sign ( & msghash, node_1_privkey) ,
2444
+ contents : unsigned_channel_update
2445
+ } ;
2446
+ match router. handle_channel_update ( & valid_channel_update) {
2447
+ Ok ( _) => ( ) ,
2448
+ Err ( _) => panic ! ( )
2449
+ } ;
2450
+ }
2451
+
2452
+ // Updates with empty messages are not returned.
2453
+ let next_announcements = router. get_next_channel_announcements ( channel_key, 2 ) ;
2454
+ assert_eq ! ( next_announcements. len( ) , 0 ) ;
2455
+
2456
+ {
2457
+ // Necessary channel updates
2458
+ let mut unsigned_channel_update = UnsignedChannelUpdate {
2459
+ chain_hash,
2460
+ short_channel_id,
2461
+ timestamp : 101 ,
2462
+ flags : 0 ,
2463
+ cltv_expiry_delta : 144 ,
2464
+ htlc_minimum_msat : 1000000 ,
2465
+ fee_base_msat : 10000 ,
2466
+ fee_proportional_millionths : 20 ,
2467
+ excess_data : Vec :: new ( )
2468
+ } ;
2469
+ let msghash = hash_to_message ! ( & Sha256dHash :: hash( & unsigned_channel_update. encode( ) [ ..] ) [ ..] ) ;
2470
+ let valid_channel_update = ChannelUpdate {
2471
+ signature : secp_ctx. sign ( & msghash, node_1_privkey) ,
2472
+ contents : unsigned_channel_update. clone ( )
2473
+ } ;
2474
+ match router. handle_channel_update ( & valid_channel_update) {
2475
+ Ok ( _) => ( ) ,
2476
+ Err ( _) => panic ! ( )
2477
+ } ;
2478
+
2479
+ unsigned_channel_update. flags = 1 ;
2480
+ let msghash = hash_to_message ! ( & Sha256dHash :: hash( & unsigned_channel_update. encode( ) [ ..] ) [ ..] ) ;
2481
+ let valid_channel_update = ChannelUpdate {
2482
+ signature : secp_ctx. sign ( & msghash, node_2_privkey) ,
2483
+ contents : unsigned_channel_update
2484
+ } ;
2485
+ match router. handle_channel_update ( & valid_channel_update) {
2486
+ Ok ( _) => ( ) ,
2487
+ Err ( _) => panic ! ( )
2488
+ } ;
2489
+ }
2490
+
2491
+ let next_announcements = router. get_next_channel_announcements ( channel_key, 2 ) ;
2492
+ assert_eq ! ( next_announcements. len( ) , 1 ) ;
2493
+
2494
+ // Further starting point have no channels after it
2495
+ let next_announcements = router. get_next_channel_announcements ( channel_key + 1000 , 2 ) ;
2496
+ assert_eq ! ( next_announcements. len( ) , 0 ) ;
2497
+
2498
+ {
2499
+ let unsigned_channel_update = UnsignedChannelUpdate {
2500
+ chain_hash,
2501
+ short_channel_id,
2502
+ timestamp : 102 ,
2503
+ flags : 0 ,
2504
+ cltv_expiry_delta : 144 ,
2505
+ htlc_minimum_msat : 1000000 ,
2506
+ fee_base_msat : 10000 ,
2507
+ fee_proportional_millionths : 20 ,
2508
+ excess_data : [ 1 ; 3 ] . to_vec ( )
2509
+ } ;
2510
+ let msghash = hash_to_message ! ( & Sha256dHash :: hash( & unsigned_channel_update. encode( ) [ ..] ) [ ..] ) ;
2511
+ let valid_channel_update = ChannelUpdate {
2512
+ signature : secp_ctx. sign ( & msghash, node_1_privkey) ,
2513
+ contents : unsigned_channel_update. clone ( )
2514
+ } ;
2515
+ match router. handle_channel_update ( & valid_channel_update) {
2516
+ Ok ( _) => ( ) ,
2517
+ Err ( _) => panic ! ( )
2518
+ } ;
2519
+ }
2520
+
2521
+ // Test that announcements with excess data won't be returned
2522
+ let next_announcements = router. get_next_channel_announcements ( channel_key, 2 ) ;
2523
+ assert_eq ! ( next_announcements. len( ) , 0 ) ;
2524
+ }
2381
2525
}
0 commit comments