Skip to content

Commit e6a3c23

Browse files
dunxenTheBlueMatt
authored andcommitted
Add test for malformed update error with NODE bit set
Some tweaks by Matt Corallo <git@bluematt.me>
1 parent 5e07c60 commit e6a3c23

File tree

1 file changed

+80
-1
lines changed

1 file changed

+80
-1
lines changed

lightning/src/ln/functional_tests.rs

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use ln::channelmanager::{ChannelManager, ChannelManagerReadArgs, PaymentId, RAAC
2424
use ln::channel::{Channel, ChannelError};
2525
use ln::{chan_utils, onion_utils};
2626
use ln::chan_utils::{htlc_success_tx_weight, htlc_timeout_tx_weight, HTLCOutputInCommitment};
27-
use routing::gossip::NetworkGraph;
27+
use routing::gossip::{NetworkGraph, NetworkUpdate};
2828
use routing::router::{PaymentParameters, Route, RouteHop, RouteParameters, find_route, get_route};
2929
use ln::features::{ChannelFeatures, InitFeatures, InvoiceFeatures, NodeFeatures};
3030
use ln::msgs;
@@ -7181,6 +7181,85 @@ fn test_update_fulfill_htlc_bolt2_after_malformed_htlc_message_must_forward_upda
71817181
check_added_monitors!(nodes[1], 1);
71827182
}
71837183

7184+
#[test]
7185+
fn test_channel_failed_after_message_with_badonion_node_perm_bits_set() {
7186+
let chanmon_cfgs = create_chanmon_cfgs(3);
7187+
let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
7188+
let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
7189+
let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
7190+
create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
7191+
let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known());
7192+
7193+
let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], 100_000);
7194+
7195+
// First hop
7196+
let mut payment_event = {
7197+
nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).unwrap();
7198+
check_added_monitors!(nodes[0], 1);
7199+
SendEvent::from_node(&nodes[0])
7200+
};
7201+
7202+
nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
7203+
commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
7204+
expect_pending_htlcs_forwardable!(nodes[1]);
7205+
check_added_monitors!(nodes[1], 1);
7206+
payment_event = SendEvent::from_node(&nodes[1]);
7207+
assert_eq!(payment_event.msgs.len(), 1);
7208+
7209+
// Second Hop
7210+
payment_event.msgs[0].onion_routing_packet.version = 1; // Trigger an invalid_onion_version error
7211+
nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event.msgs[0]);
7212+
check_added_monitors!(nodes[2], 0);
7213+
commitment_signed_dance!(nodes[2], nodes[1], payment_event.commitment_msg, false, true);
7214+
7215+
let events_3 = nodes[2].node.get_and_clear_pending_msg_events();
7216+
assert_eq!(events_3.len(), 1);
7217+
match events_3[0] {
7218+
MessageSendEvent::UpdateHTLCs { ref updates, .. } => {
7219+
let mut update_msg = updates.update_fail_malformed_htlcs[0].clone();
7220+
// Set the NODE bit (BADONION and PERM already set in invalid_onion_version error)
7221+
update_msg.failure_code |= 0x2000;
7222+
7223+
nodes[1].node.handle_update_fail_malformed_htlc(&nodes[2].node.get_our_node_id(), &update_msg);
7224+
commitment_signed_dance!(nodes[1], nodes[2], updates.commitment_signed, false, true);
7225+
},
7226+
_ => panic!("Unexpected event"),
7227+
}
7228+
7229+
expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1],
7230+
vec![HTLCDestination::NextHopChannel {
7231+
node_id: Some(nodes[2].node.get_our_node_id()), channel_id: chan_2.2 }]);
7232+
let events_4 = nodes[1].node.get_and_clear_pending_msg_events();
7233+
assert_eq!(events_4.len(), 1);
7234+
check_added_monitors!(nodes[1], 1);
7235+
7236+
match events_4[0] {
7237+
MessageSendEvent::UpdateHTLCs { ref updates, .. } => {
7238+
nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
7239+
commitment_signed_dance!(nodes[0], nodes[1], updates.commitment_signed, false, true);
7240+
},
7241+
_ => panic!("Unexpected event"),
7242+
}
7243+
7244+
let events_5 = nodes[0].node.get_and_clear_pending_events();
7245+
assert_eq!(events_5.len(), 1);
7246+
7247+
// Expect a PaymentPathFailed event with a ChannelFailure network update for the channel between
7248+
// the node originating the error to its next hop.
7249+
match events_5[0] {
7250+
Event::PaymentPathFailed { network_update:
7251+
Some(NetworkUpdate::ChannelFailure { short_channel_id, is_permanent }), error_code, ..
7252+
} => {
7253+
assert_eq!(short_channel_id, chan_2.0.contents.short_channel_id);
7254+
assert!(is_permanent);
7255+
assert_eq!(error_code, Some(0x8000|0x4000|0x2000|4));
7256+
},
7257+
_ => panic!("Unexpected event"),
7258+
}
7259+
7260+
// TODO: Test actual removal of channel from NetworkGraph when it's implemented.
7261+
}
7262+
71847263
fn do_test_failure_delay_dust_htlc_local_commitment(announce_latest: bool) {
71857264
// Dust-HTLC failure updates must be delayed until failure-trigger tx (in this case local commitment) reach ANTI_REORG_DELAY
71867265
// We can have at most two valid local commitment tx, so both cases must be covered, and both txs must be checked to get them all as

0 commit comments

Comments
 (0)