Skip to content

Commit b1bafd0

Browse files
committed
Add tests for getting node announcements
1 parent 927379c commit b1bafd0

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

lightning/src/ln/router.rs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2451,5 +2451,89 @@ mod tests {
24512451
assert_eq!(next_announcements.len(), 0);
24522452
}
24532453

2454+
#[test]
2455+
fn getting_next_node_announcements() {
2456+
let (secp_ctx, _, router) = create_router();
2457+
let secret_key = &SecretKey::from_slice(&hex::decode("0202020202020202020202020202020202020202020202020202020202020202").unwrap()[..]).unwrap();
2458+
let public_key = PublicKey::from_secret_key(&secp_ctx, secret_key);
2459+
let secret_key2 = &SecretKey::from_slice(&hex::decode("0202020202020202020202020202020202020202020202020202020202020201").unwrap()[..]).unwrap();
2460+
let public_key2 = PublicKey::from_secret_key(&secp_ctx, secret_key2);
2461+
2462+
let zero_hash = Sha256dHash::hash(&[0; 32]);
2463+
let channel_key = NetworkMap::get_key(0, zero_hash.clone());
2464+
// No nodes yet.
2465+
let next_announcements = router.get_next_node_announcements(None, 10);
2466+
assert_eq!(next_announcements.len(), 0);
2467+
2468+
{
2469+
let mut network = router.network_map.write().unwrap();
2470+
network.nodes.insert(public_key.clone(), NodeInfo {
2471+
channels: vec!(channel_key),
2472+
lowest_inbound_channel_fee_base_msat: u32::max_value(),
2473+
lowest_inbound_channel_fee_proportional_millionths: 0,
2474+
features: NodeFeatures::empty(),
2475+
last_update: Some(1),
2476+
rgb: [0; 3],
2477+
alias: [0; 32],
2478+
addresses: Vec::new(),
2479+
announcement_message: None,
2480+
});
2481+
}
2482+
2483+
// Nodes without announcements are not returned.
2484+
let next_announcements = router.get_next_node_announcements(None, 2);
2485+
assert_eq!(next_announcements.len(), 0);
24542486

2487+
{
2488+
let mut network = router.network_map.write().unwrap();
2489+
2490+
let unsigned_announcement = UnsignedNodeAnnouncement {
2491+
features: NodeFeatures::supported(),
2492+
timestamp: 10,
2493+
node_id: public_key,
2494+
rgb: [0; 3],
2495+
alias: [0; 32],
2496+
addresses: Vec::new(),
2497+
excess_address_data: Vec::new(),
2498+
excess_data: Vec::new(),
2499+
};
2500+
let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
2501+
let valid_announcement = NodeAnnouncement {
2502+
signature: secp_ctx.sign(&msghash, secret_key),
2503+
contents: unsigned_announcement.clone()
2504+
};
2505+
2506+
network.nodes.insert(public_key.clone(), NodeInfo {
2507+
channels: vec!(channel_key),
2508+
lowest_inbound_channel_fee_base_msat: u32::max_value(),
2509+
lowest_inbound_channel_fee_proportional_millionths: 0,
2510+
features: NodeFeatures::empty(),
2511+
last_update: Some(1),
2512+
rgb: [0; 3],
2513+
alias: [0; 32],
2514+
addresses: Vec::new(),
2515+
announcement_message: Some(valid_announcement.clone()),
2516+
});
2517+
2518+
network.nodes.insert(public_key2.clone(), NodeInfo {
2519+
channels: vec!(channel_key),
2520+
lowest_inbound_channel_fee_base_msat: u32::max_value(),
2521+
lowest_inbound_channel_fee_proportional_millionths: 0,
2522+
features: NodeFeatures::empty(),
2523+
last_update: Some(1),
2524+
rgb: [0; 3],
2525+
alias: [0; 32],
2526+
addresses: Vec::new(),
2527+
announcement_message: Some(valid_announcement.clone()),
2528+
});
2529+
2530+
}
2531+
2532+
let next_announcements = router.get_next_node_announcements(None, 3);
2533+
assert_eq!(next_announcements.len(), 2);
2534+
2535+
// Skip the first node.
2536+
let next_announcements = router.get_next_node_announcements(Some(&public_key2), 2);
2537+
assert_eq!(next_announcements.len(), 1);
2538+
}
24552539
}

0 commit comments

Comments
 (0)