@@ -1314,6 +1314,8 @@ mod tests {
1314
1314
use util:: events:: { Event , EventHandler , MessageSendEvent , MessageSendEventsProvider } ;
1315
1315
use util:: scid_utils:: scid_from_parts;
1316
1316
1317
+ use super :: STALE_CHANNEL_UPDATE_AGE_LIMIT_SECS ;
1318
+
1317
1319
use bitcoin:: hashes:: sha256d:: Hash as Sha256dHash ;
1318
1320
use bitcoin:: hashes:: Hash ;
1319
1321
use bitcoin:: network:: constants:: Network ;
@@ -1716,7 +1718,8 @@ mod tests {
1716
1718
} ;
1717
1719
}
1718
1720
1719
- fn do_handling_network_update ( remove_by_timeout : bool ) {
1721
+ #[ test]
1722
+ fn handling_network_update ( ) {
1720
1723
let logger = test_utils:: TestLogger :: new ( ) ;
1721
1724
let chain_source = Arc :: new ( test_utils:: TestChainSource :: new ( Network :: Testnet ) ) ;
1722
1725
let genesis_hash = genesis_block ( Network :: Testnet ) . header . block_hash ( ) ;
@@ -1795,42 +1798,22 @@ mod tests {
1795
1798
} ;
1796
1799
}
1797
1800
1798
- if remove_by_timeout {
1799
- network_graph. remove_stale_channels_with_time ( 100 + STALE_CHANNEL_UPDATE_AGE_LIMIT_SECS ) ;
1800
- assert_eq ! ( network_graph. read_only( ) . channels( ) . len( ) , 1 ) ;
1801
- assert_eq ! ( network_graph. read_only( ) . nodes( ) . len( ) , 2 ) ;
1802
-
1803
- network_graph. remove_stale_channels_with_time ( 101 + STALE_CHANNEL_UPDATE_AGE_LIMIT_SECS ) ;
1804
- #[ cfg( feature = "std" ) ]
1805
- {
1806
- // In std mode, a further check is performed before fully removing the channel -
1807
- // the channel_announcement must have been received at least two weeks ago. We
1808
- // fudge that here by indicating the time has jumped two weeks.
1809
- assert_eq ! ( network_graph. read_only( ) . channels( ) . len( ) , 1 ) ;
1810
- assert_eq ! ( network_graph. read_only( ) . nodes( ) . len( ) , 2 ) ;
1811
-
1812
- use std:: time:: { SystemTime , UNIX_EPOCH } ;
1813
- let announcement_time = SystemTime :: now ( ) . duration_since ( UNIX_EPOCH ) . expect ( "Time must be > 1970" ) . as_secs ( ) ;
1814
- network_graph. remove_stale_channels_with_time ( announcement_time + 1 + STALE_CHANNEL_UPDATE_AGE_LIMIT_SECS ) ;
1815
- }
1816
- } else {
1817
- // Permanent closing deletes a channel
1818
- net_graph_msg_handler. handle_event ( & Event :: PaymentPathFailed {
1819
- payment_id : None ,
1820
- payment_hash : PaymentHash ( [ 0 ; 32 ] ) ,
1821
- rejected_by_dest : false ,
1822
- all_paths_failed : true ,
1823
- path : vec ! [ ] ,
1824
- network_update : Some ( NetworkUpdate :: ChannelClosed {
1825
- short_channel_id,
1826
- is_permanent : true ,
1827
- } ) ,
1828
- short_channel_id : None ,
1829
- retry : None ,
1830
- error_code : None ,
1831
- error_data : None ,
1832
- } ) ;
1833
- }
1801
+ // Permanent closing deletes a channel
1802
+ net_graph_msg_handler. handle_event ( & Event :: PaymentPathFailed {
1803
+ payment_id : None ,
1804
+ payment_hash : PaymentHash ( [ 0 ; 32 ] ) ,
1805
+ rejected_by_dest : false ,
1806
+ all_paths_failed : true ,
1807
+ path : vec ! [ ] ,
1808
+ network_update : Some ( NetworkUpdate :: ChannelClosed {
1809
+ short_channel_id,
1810
+ is_permanent : true ,
1811
+ } ) ,
1812
+ short_channel_id : None ,
1813
+ retry : None ,
1814
+ error_code : None ,
1815
+ error_data : None ,
1816
+ } ) ;
1834
1817
1835
1818
assert_eq ! ( network_graph. read_only( ) . channels( ) . len( ) , 0 ) ;
1836
1819
// Nodes are also deleted because there are no associated channels anymore
@@ -1839,9 +1822,52 @@ mod tests {
1839
1822
}
1840
1823
1841
1824
#[ test]
1842
- fn handling_network_update ( ) {
1843
- do_handling_network_update ( true ) ;
1844
- do_handling_network_update ( false ) ;
1825
+ fn test_channel_timeouts ( ) {
1826
+ // Test the removal of channels with `remove_stale_channels`.
1827
+ let logger = test_utils:: TestLogger :: new ( ) ;
1828
+ let chain_source = Arc :: new ( test_utils:: TestChainSource :: new ( Network :: Testnet ) ) ;
1829
+ let genesis_hash = genesis_block ( Network :: Testnet ) . header . block_hash ( ) ;
1830
+ let network_graph = NetworkGraph :: new ( genesis_hash) ;
1831
+ let net_graph_msg_handler = NetGraphMsgHandler :: new ( & network_graph, Some ( chain_source. clone ( ) ) , & logger) ;
1832
+ let secp_ctx = Secp256k1 :: new ( ) ;
1833
+
1834
+ let node_1_privkey = & SecretKey :: from_slice ( & [ 42 ; 32 ] ) . unwrap ( ) ;
1835
+ let node_2_privkey = & SecretKey :: from_slice ( & [ 41 ; 32 ] ) . unwrap ( ) ;
1836
+
1837
+ let short_channel_id;
1838
+
1839
+ let valid_channel_announcement = get_signed_channel_announcement ( |_| { } , node_1_privkey, node_2_privkey, & secp_ctx) ;
1840
+ short_channel_id = valid_channel_announcement. contents . short_channel_id ;
1841
+ let chain_source: Option < & test_utils:: TestChainSource > = None ;
1842
+ assert ! ( network_graph. update_channel_from_announcement( & valid_channel_announcement, & chain_source, & secp_ctx) . is_ok( ) ) ;
1843
+ assert ! ( network_graph. read_only( ) . channels( ) . get( & short_channel_id) . is_some( ) ) ;
1844
+
1845
+ let valid_channel_update = get_signed_channel_update ( |_| { } , node_1_privkey, & secp_ctx) ;
1846
+ assert ! ( net_graph_msg_handler. handle_channel_update( & valid_channel_update) . is_ok( ) ) ;
1847
+ assert ! ( network_graph. read_only( ) . channels( ) . get( & short_channel_id) . unwrap( ) . one_to_two. is_some( ) ) ;
1848
+
1849
+ network_graph. remove_stale_channels_with_time ( 100 + STALE_CHANNEL_UPDATE_AGE_LIMIT_SECS ) ;
1850
+ assert_eq ! ( network_graph. read_only( ) . channels( ) . len( ) , 1 ) ;
1851
+ assert_eq ! ( network_graph. read_only( ) . nodes( ) . len( ) , 2 ) ;
1852
+
1853
+ network_graph. remove_stale_channels_with_time ( 101 + STALE_CHANNEL_UPDATE_AGE_LIMIT_SECS ) ;
1854
+ #[ cfg( feature = "std" ) ]
1855
+ {
1856
+ // In std mode, a further check is performed before fully removing the channel -
1857
+ // the channel_announcement must have been received at least two weeks ago. We
1858
+ // fudge that here by indicating the time has jumped two weeks. Note that the
1859
+ // directional channel information will have been removed already..
1860
+ assert_eq ! ( network_graph. read_only( ) . channels( ) . len( ) , 1 ) ;
1861
+ assert_eq ! ( network_graph. read_only( ) . nodes( ) . len( ) , 2 ) ;
1862
+ assert ! ( network_graph. read_only( ) . channels( ) . get( & short_channel_id) . unwrap( ) . one_to_two. is_none( ) ) ;
1863
+
1864
+ use std:: time:: { SystemTime , UNIX_EPOCH } ;
1865
+ let announcement_time = SystemTime :: now ( ) . duration_since ( UNIX_EPOCH ) . expect ( "Time must be > 1970" ) . as_secs ( ) ;
1866
+ network_graph. remove_stale_channels_with_time ( announcement_time + 1 + STALE_CHANNEL_UPDATE_AGE_LIMIT_SECS ) ;
1867
+ }
1868
+
1869
+ assert_eq ! ( network_graph. read_only( ) . channels( ) . len( ) , 0 ) ;
1870
+ assert_eq ! ( network_graph. read_only( ) . nodes( ) . len( ) , 0 ) ;
1845
1871
}
1846
1872
1847
1873
#[ test]
0 commit comments