Skip to content

Commit 018a467

Browse files
committed
Add tests for handling htlc fail channel updates
1 parent b4464b5 commit 018a467

File tree

1 file changed

+105
-1
lines changed

1 file changed

+105
-1
lines changed

lightning/src/ln/router.rs

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1058,7 +1058,7 @@ mod tests {
10581058
use ln::router::{Router,NodeInfo,NetworkMap,ChannelInfo,DirectionalChannelInfo,RouteHint};
10591059
use ln::features::{ChannelFeatures, InitFeatures, NodeFeatures};
10601060
use ln::msgs::{ErrorAction, LightningError, RoutingMessageHandler, UnsignedNodeAnnouncement, NodeAnnouncement,
1061-
UnsignedChannelAnnouncement, ChannelAnnouncement, UnsignedChannelUpdate, ChannelUpdate};
1061+
UnsignedChannelAnnouncement, ChannelAnnouncement, UnsignedChannelUpdate, ChannelUpdate, HTLCFailChannelUpdate};
10621062
use util::test_utils;
10631063
use util::test_utils::TestVecWriter;
10641064
use util::logger::Logger;
@@ -2229,4 +2229,108 @@ mod tests {
22292229
};
22302230

22312231
}
2232+
2233+
#[test]
2234+
fn handling_htlc_fail_channel_update() {
2235+
let (secp_ctx, our_id, router) = create_router();
2236+
let secret_key = &SecretKey::from_slice(&hex::decode("0202020202020202020202020202020202020202020202020202020202020202").unwrap()[..]).unwrap();
2237+
let public_key = PublicKey::from_secret_key(&secp_ctx, secret_key);
2238+
2239+
let channel_id = 0;
2240+
let channel_key = NetworkMap::get_key(channel_id, genesis_block(Network::Testnet).header.bitcoin_hash());
2241+
let channel_time = 100;
2242+
2243+
{
2244+
let mut network = router.network_map.write().unwrap();
2245+
assert_eq!(network.channels.len(), 0);
2246+
network.channels.insert(channel_key, ChannelInfo {
2247+
features: ChannelFeatures::empty(),
2248+
one_to_two: DirectionalChannelInfo {
2249+
src_node_id: public_key.clone(),
2250+
last_update: channel_time,
2251+
enabled: false,
2252+
cltv_expiry_delta: u16::max_value(),
2253+
htlc_minimum_msat: 0,
2254+
fee_base_msat: u32::max_value(),
2255+
fee_proportional_millionths: u32::max_value(),
2256+
last_update_message: None,
2257+
}, two_to_one: DirectionalChannelInfo {
2258+
src_node_id: our_id.clone(),
2259+
last_update: channel_time,
2260+
enabled: true,
2261+
cltv_expiry_delta: 0,
2262+
htlc_minimum_msat: 0,
2263+
fee_base_msat: u32::max_value(),
2264+
fee_proportional_millionths: 0,
2265+
last_update_message: None,
2266+
},
2267+
announcement_message: None,
2268+
});
2269+
assert_eq!(network.channels.len(), 1);
2270+
}
2271+
2272+
let channel_close_msg = HTLCFailChannelUpdate::ChannelClosed {
2273+
short_channel_id: channel_id,
2274+
is_permanent: false
2275+
};
2276+
2277+
router.handle_htlc_fail_channel_update(&channel_close_msg);
2278+
2279+
{
2280+
// Non-permanent closing just disables a channel
2281+
let mut network = router.network_map.write().unwrap();
2282+
match network.channels.get_mut(&channel_key) {
2283+
None => panic!(),
2284+
Some(channel_info) => {
2285+
assert!(!channel_info.one_to_two.enabled);
2286+
assert!(!channel_info.two_to_one.enabled);
2287+
}
2288+
}
2289+
}
2290+
2291+
let channel_close_msg = HTLCFailChannelUpdate::ChannelClosed {
2292+
short_channel_id: channel_id,
2293+
is_permanent: true
2294+
};
2295+
2296+
// This triggers a panic on the local node because the channel is pointing
2297+
// to an unknown node. Should this be handled?
2298+
// It also panics when a node has a channel to itself, even if node is in the db. I know it's handled
2299+
// earlier, but maybe it's good idea to add an extra assert in (remove_channel_in_nodes)?
2300+
// router.handle_htlc_fail_channel_update(&channel_close_msg);
2301+
2302+
{
2303+
let mut network = router.network_map.write().unwrap();
2304+
network.nodes.insert(public_key.clone(), NodeInfo {
2305+
channels: vec!(channel_key),
2306+
lowest_inbound_channel_fee_base_msat: u32::max_value(),
2307+
lowest_inbound_channel_fee_proportional_millionths: 0,
2308+
features: NodeFeatures::empty(),
2309+
last_update: Some(1),
2310+
rgb: [0; 3],
2311+
alias: [0; 32],
2312+
addresses: Vec::new(),
2313+
announcement_message: None,
2314+
});
2315+
}
2316+
2317+
router.handle_htlc_fail_channel_update(&channel_close_msg);
2318+
2319+
{
2320+
// Permanent closing deletes a channel
2321+
let network = router.network_map.write().unwrap();
2322+
assert_eq!(network.channels.len(), 0);
2323+
// Node is also deleted because there are no associated channels
2324+
assert_eq!(network.nodes.len(), 0);
2325+
}
2326+
2327+
// This message is not implemented yet: causes panic.
2328+
// let node_failure_msg = HTLCFailChannelUpdate::NodeFailure {
2329+
// node_id: public_key,
2330+
// is_permanent: false
2331+
// };
2332+
// router.handle_htlc_fail_channel_update(&node_failure_msg);
2333+
}
2334+
2335+
22322336
}

0 commit comments

Comments
 (0)