Skip to content

Commit b60ec94

Browse files
committed
f split out new test logic now that new tests are easier to write
1 parent 51d0d12 commit b60ec94

File tree

1 file changed

+66
-40
lines changed

1 file changed

+66
-40
lines changed

lightning/src/routing/network_graph.rs

Lines changed: 66 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,6 +1314,8 @@ mod tests {
13141314
use util::events::{Event, EventHandler, MessageSendEvent, MessageSendEventsProvider};
13151315
use util::scid_utils::scid_from_parts;
13161316

1317+
use super::STALE_CHANNEL_UPDATE_AGE_LIMIT_SECS;
1318+
13171319
use bitcoin::hashes::sha256d::Hash as Sha256dHash;
13181320
use bitcoin::hashes::Hash;
13191321
use bitcoin::network::constants::Network;
@@ -1716,7 +1718,8 @@ mod tests {
17161718
};
17171719
}
17181720

1719-
fn do_handling_network_update(remove_by_timeout: bool) {
1721+
#[test]
1722+
fn handling_network_update() {
17201723
let logger = test_utils::TestLogger::new();
17211724
let chain_source = Arc::new(test_utils::TestChainSource::new(Network::Testnet));
17221725
let genesis_hash = genesis_block(Network::Testnet).header.block_hash();
@@ -1795,42 +1798,22 @@ mod tests {
17951798
};
17961799
}
17971800

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+
});
18341817

18351818
assert_eq!(network_graph.read_only().channels().len(), 0);
18361819
// Nodes are also deleted because there are no associated channels anymore
@@ -1839,9 +1822,52 @@ mod tests {
18391822
}
18401823

18411824
#[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);
18451871
}
18461872

18471873
#[test]

0 commit comments

Comments
 (0)