Skip to content

Commit c8a1460

Browse files
committed
Add tests for handling htlc fail channel updates
1 parent 1da1ffa commit c8a1460

File tree

1 file changed

+91
-1
lines changed

1 file changed

+91
-1
lines changed

lightning/src/ln/router.rs

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1056,7 +1056,7 @@ mod tests {
10561056
use ln::router::{Router,NodeInfo,NetworkMap,ChannelInfo,DirectionalChannelInfo,RouteHint};
10571057
use ln::features::{ChannelFeatures, InitFeatures, NodeFeatures};
10581058
use ln::msgs::{ErrorAction, LightningError, RoutingMessageHandler, UnsignedNodeAnnouncement, NodeAnnouncement,
1059-
UnsignedChannelAnnouncement, ChannelAnnouncement, UnsignedChannelUpdate, ChannelUpdate};
1059+
UnsignedChannelAnnouncement, ChannelAnnouncement, UnsignedChannelUpdate, ChannelUpdate, HTLCFailChannelUpdate};
10601060
use util::test_utils;
10611061
use util::test_utils::TestVecWriter;
10621062
use util::logger::Logger;
@@ -2295,4 +2295,94 @@ mod tests {
22952295
};
22962296

22972297
}
2298+
2299+
#[test]
2300+
fn handling_htlc_fail_channel_update() {
2301+
let (secp_ctx, our_id, router) = create_router();
2302+
let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap();
2303+
let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();
2304+
let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey);
2305+
let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey);
2306+
let node_1_btckey = &SecretKey::from_slice(&[40; 32]).unwrap();
2307+
let node_2_btckey = &SecretKey::from_slice(&[39; 32]).unwrap();
2308+
2309+
let short_channel_id = 0;
2310+
let chain_hash = genesis_block(Network::Testnet).header.bitcoin_hash();
2311+
let channel_key = NetworkMap::get_key(short_channel_id, chain_hash);
2312+
2313+
{
2314+
// There is only local node in the table at the beginning.
2315+
let network = router.network_map.read().unwrap();
2316+
assert_eq!(network.nodes.len(), 1);
2317+
assert_eq!(network.nodes.contains_key(&our_id), true);
2318+
}
2319+
2320+
{
2321+
// Announce a channel we will update
2322+
let unsigned_announcement = UnsignedChannelAnnouncement {
2323+
features: ChannelFeatures::empty(),
2324+
chain_hash,
2325+
short_channel_id,
2326+
node_id_1,
2327+
node_id_2,
2328+
bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, node_1_btckey),
2329+
bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, node_2_btckey),
2330+
excess_data: Vec::new(),
2331+
};
2332+
2333+
let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
2334+
let valid_channel_announcement = ChannelAnnouncement {
2335+
node_signature_1: secp_ctx.sign(&msghash, node_1_privkey),
2336+
node_signature_2: secp_ctx.sign(&msghash, node_2_privkey),
2337+
bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey),
2338+
bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey),
2339+
contents: unsigned_announcement.clone(),
2340+
};
2341+
match router.handle_channel_announcement(&valid_channel_announcement) {
2342+
Ok(_) => (),
2343+
Err(_) => panic!()
2344+
};
2345+
2346+
}
2347+
2348+
let channel_close_msg = HTLCFailChannelUpdate::ChannelClosed {
2349+
short_channel_id,
2350+
is_permanent: false
2351+
};
2352+
2353+
router.handle_htlc_fail_channel_update(&channel_close_msg);
2354+
2355+
{
2356+
// Non-permanent closing just disables a channel
2357+
let network = router.network_map.write().unwrap();
2358+
match network.channels.get(&channel_key) {
2359+
None => panic!(),
2360+
Some(channel_info) => {
2361+
assert!(!channel_info.one_to_two.enabled);
2362+
assert!(!channel_info.two_to_one.enabled);
2363+
}
2364+
}
2365+
}
2366+
2367+
let channel_close_msg = HTLCFailChannelUpdate::ChannelClosed {
2368+
short_channel_id,
2369+
is_permanent: true
2370+
};
2371+
2372+
router.handle_htlc_fail_channel_update(&channel_close_msg);
2373+
2374+
{
2375+
// Permanent closing deletes a channel
2376+
let network = router.network_map.read().unwrap();
2377+
assert_eq!(network.channels.len(), 0);
2378+
// Nodes are also deleted because there are no associated channels anymore
2379+
// Only the local node remains in the table.
2380+
assert_eq!(network.nodes.len(), 1);
2381+
assert_eq!(network.nodes.contains_key(&our_id), true);
2382+
}
2383+
2384+
// TODO: Test HTLCFailChannelUpdate::NodeFailure, which is not implemented yet.
2385+
}
2386+
2387+
22982388
}

0 commit comments

Comments
 (0)