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