Skip to content

Commit e807e87

Browse files
committed
Add tests for handling node announcements
1 parent b8876a9 commit e807e87

File tree

1 file changed

+72
-1
lines changed

1 file changed

+72
-1
lines changed

lightning/src/ln/router.rs

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1057,7 +1057,7 @@ mod tests {
10571057
use ln::channelmanager;
10581058
use ln::router::{Router,NodeInfo,NetworkMap,ChannelInfo,DirectionalChannelInfo,RouteHint};
10591059
use ln::features::{ChannelFeatures, InitFeatures, NodeFeatures};
1060-
use ln::msgs::{ErrorAction, LightningError, RoutingMessageHandler};
1060+
use ln::msgs::{ErrorAction, LightningError, RoutingMessageHandler, UnsignedNodeAnnouncement, NodeAnnouncement};
10611061
use util::test_utils;
10621062
use util::test_utils::TestVecWriter;
10631063
use util::logger::Logger;
@@ -1864,4 +1864,75 @@ mod tests {
18641864
assert!(router.should_request_full_sync(&node_id));
18651865
assert!(!router.should_request_full_sync(&node_id));
18661866
}
1867+
1868+
#[test]
1869+
fn handling_node_announcements() {
1870+
let (secp_ctx, _, router) = create_router();
1871+
let secret_key = &SecretKey::from_slice(&hex::decode("0202020202020202020202020202020202020202020202020202020202020202").unwrap()[..]).unwrap();
1872+
let node_id = PublicKey::from_secret_key(&secp_ctx, secret_key);
1873+
let zero_hash = Sha256dHash::hash(&[0; 32]);
1874+
let first_announcement_time = 500;
1875+
1876+
let mut unsigned_announcement = UnsignedNodeAnnouncement {
1877+
features: NodeFeatures::supported(),
1878+
timestamp: first_announcement_time + 1,
1879+
node_id,
1880+
rgb: [0; 3],
1881+
alias: [0; 32],
1882+
addresses: Vec::new(),
1883+
excess_address_data: Vec::new(),
1884+
excess_data: Vec::new(),
1885+
};
1886+
let mut msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1887+
let valid_announcement = NodeAnnouncement {
1888+
signature: secp_ctx.sign(&msghash, secret_key),
1889+
contents: unsigned_announcement.clone()
1890+
};
1891+
1892+
match router.handle_node_announcement(&valid_announcement) {
1893+
Ok(_) => panic!(),
1894+
Err(e) => assert_eq!("No existing channels for node_announcement", e.err)
1895+
};
1896+
1897+
{
1898+
let mut network = router.network_map.write().unwrap();
1899+
network.nodes.insert(node_id.clone(), NodeInfo {
1900+
channels: vec!(NetworkMap::get_key(1, zero_hash.clone()), NetworkMap::get_key(3, zero_hash.clone())),
1901+
lowest_inbound_channel_fee_base_msat: 100,
1902+
lowest_inbound_channel_fee_proportional_millionths: 0,
1903+
features: NodeFeatures::supported(),
1904+
last_update: Some(first_announcement_time),
1905+
rgb: [0; 3],
1906+
alias: [0; 32],
1907+
addresses: Vec::new(),
1908+
announcement_message: None,
1909+
});
1910+
}
1911+
1912+
match router.handle_node_announcement(&valid_announcement) {
1913+
Ok(res) => assert!(res),
1914+
Err(e) => panic!("{}", e.err)
1915+
};
1916+
1917+
let fake_msghash = hash_to_message!(&zero_hash);
1918+
match router.handle_node_announcement(
1919+
&NodeAnnouncement {
1920+
signature: secp_ctx.sign(&fake_msghash, secret_key),
1921+
contents: unsigned_announcement.clone()
1922+
}) {
1923+
Ok(_) => panic!(),
1924+
Err(e) => assert_eq!(e.err, "Invalid signature from remote node")
1925+
};
1926+
1927+
unsigned_announcement.timestamp = first_announcement_time - 1;
1928+
msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1929+
let outdated_announcement = NodeAnnouncement {
1930+
signature: secp_ctx.sign(&msghash, secret_key),
1931+
contents: unsigned_announcement.clone()
1932+
};
1933+
match router.handle_node_announcement(&outdated_announcement) {
1934+
Ok(_) => panic!(),
1935+
Err(e) => assert_eq!(e.err, "Update older than last processed update")
1936+
};
1937+
}
18671938
}

0 commit comments

Comments
 (0)