Skip to content

Commit 1ebb04f

Browse files
committed
fixup! Expose API to update a channel's ChannelConfig
1 parent a7f8dd4 commit 1ebb04f

File tree

2 files changed

+72
-26
lines changed

2 files changed

+72
-26
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2972,9 +2972,12 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
29722972
continue;
29732973
}
29742974
if let Ok(msg) = self.get_channel_update_for_broadcast(channel) {
2975-
channel_state.pending_msg_events.push(
2976-
events::MessageSendEvent::BroadcastChannelUpdate { msg },
2977-
);
2975+
channel_state.pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate { msg });
2976+
} else if let Ok(msg) = self.get_channel_update_for_unicast(channel) {
2977+
channel_state.pending_msg_events.push(events::MessageSendEvent::SendChannelUpdate {
2978+
node_id: channel.get_counterparty_node_id(),
2979+
msg,
2980+
});
29782981
}
29792982
}
29802983
}

lightning/src/ln/onion_route_tests.rs

Lines changed: 66 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -593,43 +593,83 @@ fn test_onion_failure() {
593593
}, true, Some(23), None, None);
594594
}
595595

596-
#[test]
597-
fn test_onion_failure_stale_channel_update() {
596+
fn do_test_onion_failure_stale_channel_update(announced_channel: bool) {
598597
// Create a network of three nodes and two channels connecting them. We'll be updating the
599598
// HTLC relay policy of the second channel, causing forwarding failures at the first hop.
599+
let mut config = UserConfig::default();
600+
config.channel_handshake_config.announced_channel = announced_channel;
601+
config.channel_handshake_limits.force_announced_channel_preference = false;
602+
config.accept_forwards_to_priv_channels = !announced_channel;
600603
let chanmon_cfgs = create_chanmon_cfgs(3);
601604
let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
602-
let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
605+
let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, Some(config), None]);
603606
let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
604607

605-
let other_channel = create_announced_chan_between_nodes(
606-
&nodes, 0, 1, InitFeatures::known(), InitFeatures::known(),
607-
);
608-
let channel_to_update = create_announced_chan_between_nodes(
609-
&nodes, 1, 2, InitFeatures::known(), InitFeatures::known(),
608+
let other_channel = create_chan_between_nodes(
609+
&nodes[0], &nodes[1], InitFeatures::known(), InitFeatures::known(),
610610
);
611+
let channel_to_update = if announced_channel {
612+
let channel = create_announced_chan_between_nodes(
613+
&nodes, 1, 2, InitFeatures::known(), InitFeatures::known(),
614+
);
615+
(channel.2, channel.0.contents.short_channel_id)
616+
} else {
617+
let channel = create_unannounced_chan_between_nodes_with_value(
618+
&nodes, 1, 2, 100000, 10001, InitFeatures::known(), InitFeatures::known(),
619+
);
620+
(channel.0.channel_id, channel.0.short_channel_id_alias.unwrap())
621+
};
611622
let channel_to_update_counterparty = &nodes[2].node.get_our_node_id();
612623

613624
let default_config = ChannelConfig::default();
614625

615-
// A test payment should succeed as the HTLC relay paramters have not been changed yet.
626+
// A test payment should succeed as the ChannelConfig has not been changed yet.
616627
const PAYMENT_AMT: u64 = 40000;
617-
send_payment(&nodes[0], &[&nodes[1], &nodes[2]], PAYMENT_AMT);
628+
let (route, payment_hash, payment_preimage, payment_secret) = if announced_channel {
629+
get_route_and_payment_hash!(nodes[0], nodes[2], PAYMENT_AMT)
630+
} else {
631+
let hop_hints = vec![RouteHint(vec![RouteHintHop {
632+
src_node_id: nodes[1].node.get_our_node_id(),
633+
short_channel_id: channel_to_update.1,
634+
fees: RoutingFees {
635+
base_msat: default_config.forwarding_fee_base_msat,
636+
proportional_millionths: default_config.forwarding_fee_proportional_millionths,
637+
},
638+
cltv_expiry_delta: default_config.cltv_expiry_delta,
639+
htlc_maximum_msat: None,
640+
htlc_minimum_msat: None,
641+
}])];
642+
let payment_params = PaymentParameters::from_node_id(*channel_to_update_counterparty)
643+
.with_features(InvoiceFeatures::known())
644+
.with_route_hints(hop_hints);
645+
get_route_and_payment_hash!(nodes[0], nodes[2], payment_params, PAYMENT_AMT, TEST_FINAL_CLTV)
646+
};
647+
send_along_route_with_secret(&nodes[0], route.clone(), &[&[&nodes[1], &nodes[2]]], PAYMENT_AMT,
648+
payment_hash, payment_secret);
649+
claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], payment_preimage);
618650

619651
// Closure to update and retrieve the latest ChannelUpdate.
620652
let update_and_get_channel_update = |config: &ChannelConfig, expect_new_update: bool,
621653
prev_update: Option<&msgs::ChannelUpdate>| -> Option<msgs::ChannelUpdate> {
622654
nodes[1].node.update_channel_config(
623-
channel_to_update_counterparty, &[channel_to_update.2], config,
655+
channel_to_update_counterparty, &[channel_to_update.0], config,
624656
).unwrap();
625657
let events = nodes[1].node.get_and_clear_pending_msg_events();
626658
assert_eq!(events.len(), expect_new_update as usize);
627659
if !expect_new_update {
628660
return None;
629661
}
630662
let new_update = match &events[0] {
631-
MessageSendEvent::BroadcastChannelUpdate { msg, .. } => msg.clone(),
632-
_ => panic!("expected BroadcastChannelUpdate event"),
663+
MessageSendEvent::BroadcastChannelUpdate { msg } => {
664+
assert!(announced_channel);
665+
msg.clone()
666+
},
667+
MessageSendEvent::SendChannelUpdate { node_id, msg } => {
668+
assert_eq!(node_id, channel_to_update_counterparty);
669+
assert!(!announced_channel);
670+
msg.clone()
671+
},
672+
_ => panic!("expected Broadcast/SendChannelUpdate event"),
633673
};
634674
if prev_update.is_some() {
635675
assert!(new_update.contents.timestamp > prev_update.unwrap().contents.timestamp)
@@ -638,13 +678,10 @@ fn test_onion_failure_stale_channel_update() {
638678
};
639679

640680
// We'll be attempting to route payments using the default ChannelUpdate for channels. This will
641-
// lead to onion failures at the first hop once we update the HTLC relay parameters for the
681+
// lead to onion failures at the first hop once we update the ChannelConfig for the
642682
// second hop.
643-
let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(
644-
nodes[0], nodes[2], PAYMENT_AMT
645-
);
646683
let expect_onion_failure = |name: &str, error_code: u16, channel_update: &msgs::ChannelUpdate| {
647-
let short_channel_id = channel_to_update.0.contents.short_channel_id;
684+
let short_channel_id = channel_to_update.1;
648685
let network_update = NetworkUpdate::ChannelUpdateMessage { msg: channel_update.clone() };
649686
run_onion_failure_test(
650687
name, 0, &nodes, &route, &payment_hash, &payment_secret, |_| {}, || {}, true,
@@ -656,15 +693,15 @@ fn test_onion_failure_stale_channel_update() {
656693
let mut invalid_config = default_config.clone();
657694
invalid_config.cltv_expiry_delta = 0;
658695
match nodes[1].node.update_channel_config(
659-
channel_to_update_counterparty, &[channel_to_update.2], &invalid_config,
696+
channel_to_update_counterparty, &[channel_to_update.0], &invalid_config,
660697
) {
661698
Err(APIError::APIMisuseError{ .. }) => {},
662699
_ => panic!("unexpected result applying invalid cltv_expiry_delta"),
663700
}
664701

665702
// Increase the base fee which should trigger a new ChannelUpdate.
666703
let mut config = nodes[1].node.list_usable_channels().iter()
667-
.find(|channel| channel.channel_id == channel_to_update.2).unwrap()
704+
.find(|channel| channel.channel_id == channel_to_update.0).unwrap()
668705
.config.unwrap();
669706
config.forwarding_fee_base_msat = u32::max_value();
670707
let msg = update_and_get_channel_update(&config, true, None).unwrap();
@@ -700,10 +737,10 @@ fn test_onion_failure_stale_channel_update() {
700737
);
701738

702739
let mut chanmon_1 = <(_, ChannelMonitor<_>)>::read(
703-
&mut &get_monitor!(nodes[1], other_channel.2).encode()[..], nodes[1].keys_manager,
740+
&mut &get_monitor!(nodes[1], other_channel.3).encode()[..], nodes[1].keys_manager,
704741
).unwrap().1;
705742
let mut chanmon_2 = <(_, ChannelMonitor<_>)>::read(
706-
&mut &get_monitor!(nodes[1], channel_to_update.2).encode()[..], nodes[1].keys_manager,
743+
&mut &get_monitor!(nodes[1], channel_to_update.0).encode()[..], nodes[1].keys_manager,
707744
).unwrap().1;
708745
let mut channel_monitors = HashMap::new();
709746
channel_monitors.insert(chanmon_1.get_funding_txo().0, &mut chanmon_1);
@@ -721,12 +758,18 @@ fn test_onion_failure_stale_channel_update() {
721758
},
722759
).unwrap().1;
723760
chanmgr.list_channels().iter()
724-
.find(|channel| channel.channel_id == channel_to_update.2).unwrap()
761+
.find(|channel| channel.channel_id == channel_to_update.0).unwrap()
725762
.config.unwrap()
726763
};
727764
assert_eq!(config, config_after_restart);
728765
}
729766

767+
#[test]
768+
fn test_onion_failure_stale_channel_update() {
769+
do_test_onion_failure_stale_channel_update(false);
770+
do_test_onion_failure_stale_channel_update(true);
771+
}
772+
730773
#[test]
731774
fn test_default_to_onion_payload_tlv_format() {
732775
// Tests that we default to creating tlv format onion payloads when no `NodeAnnouncementInfo`

0 commit comments

Comments
 (0)