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