1 // This file is Copyright its original authors, visible in version control
4 // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5 // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7 // You may not use this file except in accordance with one or both of these
10 //! Tests that test ChannelManager behavior with fewer confirmations required than the default and
11 //! other behavior that exists only on private channels or with a semi-trusted counterparty (eg
15 use chain::channelmonitor::ChannelMonitor;
16 use chain::keysinterface::{Recipient, KeysInterface};
17 use ln::channelmanager::{ChannelManager, ChannelManagerReadArgs, MIN_CLTV_EXPIRY_DELTA};
18 use routing::network_graph::RoutingFees;
19 use routing::router::{PaymentParameters, RouteHint, RouteHintHop};
20 use ln::features::{InitFeatures, InvoiceFeatures};
22 use ln::msgs::{ChannelMessageHandler, RoutingMessageHandler, OptionalField};
23 use util::enforcing_trait_impls::EnforcingSigner;
24 use util::events::{Event, MessageSendEvent, MessageSendEventsProvider};
25 use util::config::UserConfig;
26 use util::ser::{Writeable, ReadableArgs};
30 use core::default::Default;
32 use ln::functional_test_utils::*;
34 use bitcoin::blockdata::constants::genesis_block;
35 use bitcoin::hash_types::BlockHash;
36 use bitcoin::hashes::Hash;
37 use bitcoin::hashes::sha256d::Hash as Sha256dHash;
38 use bitcoin::network::constants::Network;
39 use bitcoin::secp256k1::Secp256k1;
42 fn test_priv_forwarding_rejection() {
43 // If we have a private channel with outbound liquidity, and
44 // UserConfig::accept_forwards_to_priv_channels is set to false, we should reject any attempts
45 // to forward through that channel.
46 let chanmon_cfgs = create_chanmon_cfgs(3);
47 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
48 let mut no_announce_cfg = test_default_channel_config();
49 no_announce_cfg.accept_forwards_to_priv_channels = false;
50 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, Some(no_announce_cfg), None]);
51 let persister: test_utils::TestPersister;
52 let new_chain_monitor: test_utils::TestChainMonitor;
53 let nodes_1_deserialized: ChannelManager<EnforcingSigner, &test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>;
54 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
56 let chan_id_1 = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 500_000_000, InitFeatures::known(), InitFeatures::known()).2;
57 let chan_id_2 = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 500_000_000, InitFeatures::known(), InitFeatures::known()).0.channel_id;
59 // We should always be able to forward through nodes[1] as long as its out through a public
61 send_payment(&nodes[2], &[&nodes[1], &nodes[0]], 10_000);
63 // ... however, if we send to nodes[2], we will have to pass the private channel from nodes[1]
64 // to nodes[2], which should be rejected:
65 let route_hint = RouteHint(vec![RouteHintHop {
66 src_node_id: nodes[1].node.get_our_node_id(),
67 short_channel_id: nodes[2].node.list_channels()[0].short_channel_id.unwrap(),
68 fees: RoutingFees { base_msat: 1000, proportional_millionths: 0 },
69 cltv_expiry_delta: MIN_CLTV_EXPIRY_DELTA,
70 htlc_minimum_msat: None,
71 htlc_maximum_msat: None,
73 let last_hops = vec![route_hint];
74 let payment_params = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id())
75 .with_features(InvoiceFeatures::known())
76 .with_route_hints(last_hops);
77 let (route, our_payment_hash, our_payment_preimage, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], payment_params, 10_000, TEST_FINAL_CLTV);
79 nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).unwrap();
80 check_added_monitors!(nodes[0], 1);
81 let payment_event = SendEvent::from_event(nodes[0].node.get_and_clear_pending_msg_events().remove(0));
82 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
83 commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false, true);
85 let htlc_fail_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
86 assert!(htlc_fail_updates.update_add_htlcs.is_empty());
87 assert_eq!(htlc_fail_updates.update_fail_htlcs.len(), 1);
88 assert!(htlc_fail_updates.update_fail_malformed_htlcs.is_empty());
89 assert!(htlc_fail_updates.update_fee.is_none());
91 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &htlc_fail_updates.update_fail_htlcs[0]);
92 commitment_signed_dance!(nodes[0], nodes[1], htlc_fail_updates.commitment_signed, true, true);
93 expect_payment_failed_with_update!(nodes[0], our_payment_hash, false, nodes[2].node.list_channels()[0].short_channel_id.unwrap(), true);
95 // Now disconnect nodes[1] from its peers and restart with accept_forwards_to_priv_channels set
96 // to true. Sadly there is currently no way to change it at runtime.
98 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
99 nodes[2].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
101 let nodes_1_serialized = nodes[1].node.encode();
102 let mut monitor_a_serialized = test_utils::TestVecWriter(Vec::new());
103 let mut monitor_b_serialized = test_utils::TestVecWriter(Vec::new());
104 get_monitor!(nodes[1], chan_id_1).write(&mut monitor_a_serialized).unwrap();
105 get_monitor!(nodes[1], chan_id_2).write(&mut monitor_b_serialized).unwrap();
107 persister = test_utils::TestPersister::new();
108 let keys_manager = &chanmon_cfgs[1].keys_manager;
109 new_chain_monitor = test_utils::TestChainMonitor::new(Some(nodes[1].chain_source), nodes[1].tx_broadcaster.clone(), nodes[1].logger, node_cfgs[1].fee_estimator, &persister, keys_manager);
110 nodes[1].chain_monitor = &new_chain_monitor;
112 let mut monitor_a_read = &monitor_a_serialized.0[..];
113 let mut monitor_b_read = &monitor_b_serialized.0[..];
114 let (_, mut monitor_a) = <(BlockHash, ChannelMonitor<EnforcingSigner>)>::read(&mut monitor_a_read, keys_manager).unwrap();
115 let (_, mut monitor_b) = <(BlockHash, ChannelMonitor<EnforcingSigner>)>::read(&mut monitor_b_read, keys_manager).unwrap();
116 assert!(monitor_a_read.is_empty());
117 assert!(monitor_b_read.is_empty());
119 no_announce_cfg.accept_forwards_to_priv_channels = true;
121 let mut nodes_1_read = &nodes_1_serialized[..];
122 let (_, nodes_1_deserialized_tmp) = {
123 let mut channel_monitors = HashMap::new();
124 channel_monitors.insert(monitor_a.get_funding_txo().0, &mut monitor_a);
125 channel_monitors.insert(monitor_b.get_funding_txo().0, &mut monitor_b);
126 <(BlockHash, ChannelManager<EnforcingSigner, &test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>)>::read(&mut nodes_1_read, ChannelManagerReadArgs {
127 default_config: no_announce_cfg,
129 fee_estimator: node_cfgs[1].fee_estimator,
130 chain_monitor: nodes[1].chain_monitor,
131 tx_broadcaster: nodes[1].tx_broadcaster.clone(),
132 logger: nodes[1].logger,
136 assert!(nodes_1_read.is_empty());
137 nodes_1_deserialized = nodes_1_deserialized_tmp;
139 assert!(nodes[1].chain_monitor.watch_channel(monitor_a.get_funding_txo().0, monitor_a).is_ok());
140 assert!(nodes[1].chain_monitor.watch_channel(monitor_b.get_funding_txo().0, monitor_b).is_ok());
141 check_added_monitors!(nodes[1], 2);
142 nodes[1].node = &nodes_1_deserialized;
144 nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: InitFeatures::known(), remote_network_address: None });
145 nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty(), remote_network_address: None });
146 let as_reestablish = get_event_msg!(nodes[0], MessageSendEvent::SendChannelReestablish, nodes[1].node.get_our_node_id());
147 let bs_reestablish = get_event_msg!(nodes[1], MessageSendEvent::SendChannelReestablish, nodes[0].node.get_our_node_id());
148 nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &as_reestablish);
149 nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &bs_reestablish);
150 get_event_msg!(nodes[0], MessageSendEvent::SendChannelUpdate, nodes[1].node.get_our_node_id());
151 get_event_msg!(nodes[1], MessageSendEvent::SendChannelUpdate, nodes[0].node.get_our_node_id());
153 nodes[1].node.peer_connected(&nodes[2].node.get_our_node_id(), &msgs::Init { features: InitFeatures::known(), remote_network_address: None });
154 nodes[2].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty(), remote_network_address: None });
155 let bs_reestablish = get_event_msg!(nodes[1], MessageSendEvent::SendChannelReestablish, nodes[2].node.get_our_node_id());
156 let cs_reestablish = get_event_msg!(nodes[2], MessageSendEvent::SendChannelReestablish, nodes[1].node.get_our_node_id());
157 nodes[2].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &bs_reestablish);
158 nodes[1].node.handle_channel_reestablish(&nodes[2].node.get_our_node_id(), &cs_reestablish);
159 get_event_msg!(nodes[1], MessageSendEvent::SendChannelUpdate, nodes[2].node.get_our_node_id());
160 get_event_msg!(nodes[2], MessageSendEvent::SendChannelUpdate, nodes[1].node.get_our_node_id());
162 nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).unwrap();
163 check_added_monitors!(nodes[0], 1);
164 pass_along_route(&nodes[0], &[&[&nodes[1], &nodes[2]]], 10_000, our_payment_hash, our_payment_secret);
165 claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], our_payment_preimage);
168 fn do_test_1_conf_open(connect_style: ConnectStyle) {
169 // Previously, if the minium_depth config was set to 1, we'd never send a funding_locked. This
170 // tests that we properly send one in that case.
171 let mut alice_config = UserConfig::default();
172 alice_config.own_channel_config.minimum_depth = 1;
173 alice_config.channel_options.announced_channel = true;
174 alice_config.peer_channel_config_limits.force_announced_channel_preference = false;
175 let mut bob_config = UserConfig::default();
176 bob_config.own_channel_config.minimum_depth = 1;
177 bob_config.channel_options.announced_channel = true;
178 bob_config.peer_channel_config_limits.force_announced_channel_preference = false;
179 let chanmon_cfgs = create_chanmon_cfgs(2);
180 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
181 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(alice_config), Some(bob_config)]);
182 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
183 *nodes[0].connect_style.borrow_mut() = connect_style;
185 let tx = create_chan_between_nodes_with_value_init(&nodes[0], &nodes[1], 100000, 10001, InitFeatures::known(), InitFeatures::known());
186 mine_transaction(&nodes[1], &tx);
187 nodes[0].node.handle_funding_locked(&nodes[1].node.get_our_node_id(), &get_event_msg!(nodes[1], MessageSendEvent::SendFundingLocked, nodes[0].node.get_our_node_id()));
188 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
190 mine_transaction(&nodes[0], &tx);
191 let as_msg_events = nodes[0].node.get_and_clear_pending_msg_events();
192 assert_eq!(as_msg_events.len(), 2);
193 let as_funding_locked = if let MessageSendEvent::SendFundingLocked { ref node_id, ref msg } = as_msg_events[0] {
194 assert_eq!(*node_id, nodes[1].node.get_our_node_id());
196 } else { panic!("Unexpected event"); };
197 if let MessageSendEvent::SendChannelUpdate { ref node_id, msg: _ } = as_msg_events[1] {
198 assert_eq!(*node_id, nodes[1].node.get_our_node_id());
199 } else { panic!("Unexpected event"); }
201 nodes[1].node.handle_funding_locked(&nodes[0].node.get_our_node_id(), &as_funding_locked);
202 let bs_msg_events = nodes[1].node.get_and_clear_pending_msg_events();
203 assert_eq!(bs_msg_events.len(), 1);
204 if let MessageSendEvent::SendChannelUpdate { ref node_id, msg: _ } = bs_msg_events[0] {
205 assert_eq!(*node_id, nodes[0].node.get_our_node_id());
206 } else { panic!("Unexpected event"); }
208 send_payment(&nodes[0], &[&nodes[1]], 100_000);
210 // After 6 confirmations, as required by the spec, we'll send announcement_signatures and
211 // broadcast the channel_announcement (but not before exactly 6 confirmations).
212 connect_blocks(&nodes[0], 4);
213 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
214 connect_blocks(&nodes[0], 1);
215 nodes[1].node.handle_announcement_signatures(&nodes[0].node.get_our_node_id(), &get_event_msg!(nodes[0], MessageSendEvent::SendAnnouncementSignatures, nodes[1].node.get_our_node_id()));
216 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
218 connect_blocks(&nodes[1], 5);
219 let bs_announce_events = nodes[1].node.get_and_clear_pending_msg_events();
220 assert_eq!(bs_announce_events.len(), 2);
221 let bs_announcement_sigs = if let MessageSendEvent::SendAnnouncementSignatures { ref node_id, ref msg } = bs_announce_events[0] {
222 assert_eq!(*node_id, nodes[0].node.get_our_node_id());
224 } else { panic!("Unexpected event"); };
225 let (bs_announcement, bs_update) = if let MessageSendEvent::BroadcastChannelAnnouncement { ref msg, ref update_msg } = bs_announce_events[1] {
226 (msg.clone(), update_msg.clone())
227 } else { panic!("Unexpected event"); };
229 nodes[0].node.handle_announcement_signatures(&nodes[1].node.get_our_node_id(), &bs_announcement_sigs);
230 let as_announce_events = nodes[0].node.get_and_clear_pending_msg_events();
231 assert_eq!(as_announce_events.len(), 1);
232 let (announcement, as_update) = if let MessageSendEvent::BroadcastChannelAnnouncement { ref msg, ref update_msg } = as_announce_events[0] {
233 (msg.clone(), update_msg.clone())
234 } else { panic!("Unexpected event"); };
235 assert_eq!(announcement, bs_announcement);
238 assert!(node.net_graph_msg_handler.handle_channel_announcement(&announcement).unwrap());
239 node.net_graph_msg_handler.handle_channel_update(&as_update).unwrap();
240 node.net_graph_msg_handler.handle_channel_update(&bs_update).unwrap();
244 fn test_1_conf_open() {
245 do_test_1_conf_open(ConnectStyle::BestBlockFirst);
246 do_test_1_conf_open(ConnectStyle::TransactionsFirst);
247 do_test_1_conf_open(ConnectStyle::FullBlockViaListen);
251 fn test_routed_scid_alias() {
252 // Trivially test sending a payment which is routed through an SCID alias.
253 let chanmon_cfgs = create_chanmon_cfgs(3);
254 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
255 let mut no_announce_cfg = test_default_channel_config();
256 no_announce_cfg.accept_forwards_to_priv_channels = true;
257 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, Some(no_announce_cfg), None]);
258 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
260 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 500_000_000, InitFeatures::known(), InitFeatures::known()).2;
261 let mut as_funding_locked = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 500_000_000, InitFeatures::known(), InitFeatures::known()).0;
263 let last_hop = nodes[2].node.list_usable_channels();
264 let hop_hints = vec![RouteHint(vec![RouteHintHop {
265 src_node_id: nodes[1].node.get_our_node_id(),
266 short_channel_id: last_hop[0].inbound_scid_alias.unwrap(),
268 base_msat: last_hop[0].counterparty.forwarding_info.as_ref().unwrap().fee_base_msat,
269 proportional_millionths: last_hop[0].counterparty.forwarding_info.as_ref().unwrap().fee_proportional_millionths,
271 cltv_expiry_delta: last_hop[0].counterparty.forwarding_info.as_ref().unwrap().cltv_expiry_delta,
272 htlc_maximum_msat: None,
273 htlc_minimum_msat: None,
275 let payment_params = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id())
276 .with_features(InvoiceFeatures::known())
277 .with_route_hints(hop_hints);
278 let (route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], payment_params, 100_000, 42);
279 assert_eq!(route.paths[0][1].short_channel_id, last_hop[0].inbound_scid_alias.unwrap());
280 nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret)).unwrap();
281 check_added_monitors!(nodes[0], 1);
283 pass_along_route(&nodes[0], &[&[&nodes[1], &nodes[2]]], 100_000, payment_hash, payment_secret);
284 claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], payment_preimage);
286 // Now test that if a peer sends us a second funding_locked after the channel is operational we
287 // will use the new alias.
288 as_funding_locked.short_channel_id_alias = Some(0xdeadbeef);
289 nodes[2].node.handle_funding_locked(&nodes[1].node.get_our_node_id(), &as_funding_locked);
290 // Note that we always respond to a funding_locked with a channel_update. Not a lot of reason
291 // to bother updating that code, so just drop the message here.
292 get_event_msg!(nodes[2], MessageSendEvent::SendChannelUpdate, nodes[1].node.get_our_node_id());
293 let updated_channel_info = nodes[2].node.list_usable_channels();
294 assert_eq!(updated_channel_info.len(), 1);
295 assert_eq!(updated_channel_info[0].inbound_scid_alias.unwrap(), 0xdeadbeef);
296 // Note that because we never send a duplicate funding_locked we can't send a payment through
297 // the 0xdeadbeef SCID alias.
301 fn test_scid_privacy_on_pub_channel() {
302 // Tests rejecting the scid_privacy feature for public channels and that we don't ever try to
304 let chanmon_cfgs = create_chanmon_cfgs(2);
305 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
306 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
307 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
309 let mut scid_privacy_cfg = test_default_channel_config();
310 scid_privacy_cfg.channel_options.announced_channel = true;
311 scid_privacy_cfg.own_channel_config.negotiate_scid_privacy = true;
312 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, Some(scid_privacy_cfg)).unwrap();
313 let mut open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
315 assert!(!open_channel.channel_type.as_ref().unwrap().supports_scid_privacy()); // we ignore `negotiate_scid_privacy` on pub channels
316 open_channel.channel_type.as_mut().unwrap().set_scid_privacy_required();
317 assert_eq!(open_channel.channel_flags & 1, 1); // The `announce_channel` bit is set.
319 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), InitFeatures::known(), &open_channel);
320 let err = get_err_msg!(nodes[1], nodes[0].node.get_our_node_id());
321 assert_eq!(err.data, "SCID Alias/Privacy Channel Type cannot be set on a public channel");
325 fn test_scid_privacy_negotiation() {
326 // Tests of the negotiation of SCID alias and falling back to non-SCID-alias if our
327 // counterparty doesn't support it.
328 let chanmon_cfgs = create_chanmon_cfgs(2);
329 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
330 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
331 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
333 let mut scid_privacy_cfg = test_default_channel_config();
334 scid_privacy_cfg.channel_options.announced_channel = false;
335 scid_privacy_cfg.own_channel_config.negotiate_scid_privacy = true;
336 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, Some(scid_privacy_cfg)).unwrap();
338 let init_open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
339 assert!(init_open_channel.channel_type.as_ref().unwrap().supports_scid_privacy());
340 assert!(nodes[0].node.list_channels()[0].channel_type.is_none()); // channel_type is none until counterparty accepts
342 // now simulate nodes[1] responding with an Error message, indicating it doesn't understand
344 nodes[0].node.handle_error(&nodes[1].node.get_our_node_id(), &msgs::ErrorMessage {
345 channel_id: init_open_channel.temporary_channel_id,
346 data: "Yo, no SCID aliases, no privacy here!".to_string()
348 assert!(nodes[0].node.list_channels()[0].channel_type.is_none()); // channel_type is none until counterparty accepts
350 let second_open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
351 assert!(!second_open_channel.channel_type.as_ref().unwrap().supports_scid_privacy());
352 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), InitFeatures::known(), &second_open_channel);
353 nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), InitFeatures::known(), &get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id()));
355 let events = nodes[0].node.get_and_clear_pending_events();
356 assert_eq!(events.len(), 1);
358 Event::FundingGenerationReady { .. } => {},
359 _ => panic!("Unexpected event"),
362 assert!(!nodes[0].node.list_channels()[0].channel_type.as_ref().unwrap().supports_scid_privacy());
363 assert!(!nodes[1].node.list_channels()[0].channel_type.as_ref().unwrap().supports_scid_privacy());
367 fn test_inbound_scid_privacy() {
368 // Tests accepting channels with the scid_privacy feature and rejecting forwards using the
369 // channel's real SCID as required by the channel feature.
370 let chanmon_cfgs = create_chanmon_cfgs(3);
371 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
372 let mut accept_forward_cfg = test_default_channel_config();
373 accept_forward_cfg.accept_forwards_to_priv_channels = true;
374 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, Some(accept_forward_cfg), None]);
375 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
377 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0, InitFeatures::known(), InitFeatures::known());
379 let mut no_announce_cfg = test_default_channel_config();
380 no_announce_cfg.channel_options.announced_channel = false;
381 no_announce_cfg.own_channel_config.negotiate_scid_privacy = true;
382 nodes[1].node.create_channel(nodes[2].node.get_our_node_id(), 100_000, 10_000, 42, Some(no_announce_cfg)).unwrap();
383 let mut open_channel = get_event_msg!(nodes[1], MessageSendEvent::SendOpenChannel, nodes[2].node.get_our_node_id());
385 assert!(open_channel.channel_type.as_ref().unwrap().requires_scid_privacy());
387 nodes[2].node.handle_open_channel(&nodes[1].node.get_our_node_id(), InitFeatures::known(), &open_channel);
388 let accept_channel = get_event_msg!(nodes[2], MessageSendEvent::SendAcceptChannel, nodes[1].node.get_our_node_id());
389 nodes[1].node.handle_accept_channel(&nodes[2].node.get_our_node_id(), InitFeatures::known(), &accept_channel);
391 let (temporary_channel_id, tx, _) = create_funding_transaction(&nodes[1], 100_000, 42);
392 nodes[1].node.funding_transaction_generated(&temporary_channel_id, tx.clone()).unwrap();
393 nodes[2].node.handle_funding_created(&nodes[1].node.get_our_node_id(), &get_event_msg!(nodes[1], MessageSendEvent::SendFundingCreated, nodes[2].node.get_our_node_id()));
394 check_added_monitors!(nodes[2], 1);
396 let cs_funding_signed = get_event_msg!(nodes[2], MessageSendEvent::SendFundingSigned, nodes[1].node.get_our_node_id());
397 nodes[1].node.handle_funding_signed(&nodes[2].node.get_our_node_id(), &cs_funding_signed);
398 check_added_monitors!(nodes[1], 1);
400 let conf_height = core::cmp::max(nodes[1].best_block_info().1 + 1, nodes[2].best_block_info().1 + 1);
401 confirm_transaction_at(&nodes[1], &tx, conf_height);
402 connect_blocks(&nodes[1], CHAN_CONFIRM_DEPTH - 1);
403 confirm_transaction_at(&nodes[2], &tx, conf_height);
404 connect_blocks(&nodes[2], CHAN_CONFIRM_DEPTH - 1);
405 let bs_funding_locked = get_event_msg!(nodes[1], MessageSendEvent::SendFundingLocked, nodes[2].node.get_our_node_id());
406 nodes[1].node.handle_funding_locked(&nodes[2].node.get_our_node_id(), &get_event_msg!(nodes[2], MessageSendEvent::SendFundingLocked, nodes[1].node.get_our_node_id()));
407 let bs_update = get_event_msg!(nodes[1], MessageSendEvent::SendChannelUpdate, nodes[2].node.get_our_node_id());
408 nodes[2].node.handle_funding_locked(&nodes[1].node.get_our_node_id(), &bs_funding_locked);
409 let cs_update = get_event_msg!(nodes[2], MessageSendEvent::SendChannelUpdate, nodes[1].node.get_our_node_id());
411 nodes[1].node.handle_channel_update(&nodes[2].node.get_our_node_id(), &cs_update);
412 nodes[2].node.handle_channel_update(&nodes[1].node.get_our_node_id(), &bs_update);
414 // Now we can pay just fine using the SCID alias nodes[2] gave to nodes[1]...
416 let last_hop = nodes[2].node.list_usable_channels();
417 let mut hop_hints = vec![RouteHint(vec![RouteHintHop {
418 src_node_id: nodes[1].node.get_our_node_id(),
419 short_channel_id: last_hop[0].inbound_scid_alias.unwrap(),
421 base_msat: last_hop[0].counterparty.forwarding_info.as_ref().unwrap().fee_base_msat,
422 proportional_millionths: last_hop[0].counterparty.forwarding_info.as_ref().unwrap().fee_proportional_millionths,
424 cltv_expiry_delta: last_hop[0].counterparty.forwarding_info.as_ref().unwrap().cltv_expiry_delta,
425 htlc_maximum_msat: None,
426 htlc_minimum_msat: None,
428 let payment_params = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id())
429 .with_features(InvoiceFeatures::known())
430 .with_route_hints(hop_hints.clone());
431 let (route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], payment_params, 100_000, 42);
432 assert_eq!(route.paths[0][1].short_channel_id, last_hop[0].inbound_scid_alias.unwrap());
433 nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret)).unwrap();
434 check_added_monitors!(nodes[0], 1);
436 pass_along_route(&nodes[0], &[&[&nodes[1], &nodes[2]]], 100_000, payment_hash, payment_secret);
437 claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], payment_preimage);
439 // ... but if we try to pay using the real SCID, nodes[1] will just tell us they don't know
440 // what channel we're talking about.
441 hop_hints[0].0[0].short_channel_id = last_hop[0].short_channel_id.unwrap();
443 let payment_params_2 = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id())
444 .with_features(InvoiceFeatures::known())
445 .with_route_hints(hop_hints);
446 let (route_2, payment_hash_2, _, payment_secret_2) = get_route_and_payment_hash!(nodes[0], nodes[2], payment_params_2, 100_000, 42);
447 assert_eq!(route_2.paths[0][1].short_channel_id, last_hop[0].short_channel_id.unwrap());
448 nodes[0].node.send_payment(&route_2, payment_hash_2, &Some(payment_secret_2)).unwrap();
449 check_added_monitors!(nodes[0], 1);
451 let payment_event = SendEvent::from_node(&nodes[0]);
452 assert_eq!(nodes[1].node.get_our_node_id(), payment_event.node_id);
453 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
454 commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, true, true);
456 nodes[1].logger.assert_log_regex("lightning::ln::channelmanager".to_string(), regex::Regex::new(r"Refusing to forward over real channel SCID as our counterparty requested").unwrap(), 1);
458 let mut updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
459 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
460 commitment_signed_dance!(nodes[0], nodes[1], updates.commitment_signed, false);
462 expect_payment_failed_conditions!(nodes[0], payment_hash_2, false,
463 PaymentFailedConditions::new().blamed_scid(last_hop[0].short_channel_id.unwrap())
464 .blamed_chan_closed(true).expected_htlc_error_data(0x4000|10, &[0; 0]));
468 fn test_scid_alias_returned() {
469 // Tests that when we fail an HTLC (in this case due to attempting to forward more than the
470 // channel's available balance) we use the correct (in this case the aliased) SCID in the
471 // channel_update which is returned in the onion to the sender.
472 let chanmon_cfgs = create_chanmon_cfgs(3);
473 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
474 let mut accept_forward_cfg = test_default_channel_config();
475 accept_forward_cfg.accept_forwards_to_priv_channels = true;
476 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, Some(accept_forward_cfg), None]);
477 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
479 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 0, InitFeatures::known(), InitFeatures::known());
480 create_unannounced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000, 0, InitFeatures::known(), InitFeatures::known());
482 let last_hop = nodes[2].node.list_usable_channels();
483 let mut hop_hints = vec![RouteHint(vec![RouteHintHop {
484 src_node_id: nodes[1].node.get_our_node_id(),
485 short_channel_id: last_hop[0].inbound_scid_alias.unwrap(),
487 base_msat: last_hop[0].counterparty.forwarding_info.as_ref().unwrap().fee_base_msat,
488 proportional_millionths: last_hop[0].counterparty.forwarding_info.as_ref().unwrap().fee_proportional_millionths,
490 cltv_expiry_delta: last_hop[0].counterparty.forwarding_info.as_ref().unwrap().cltv_expiry_delta,
491 htlc_maximum_msat: None,
492 htlc_minimum_msat: None,
494 let payment_params = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id())
495 .with_features(InvoiceFeatures::known())
496 .with_route_hints(hop_hints);
497 let (mut route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], payment_params, 10_000, 42);
498 assert_eq!(route.paths[0][1].short_channel_id, nodes[2].node.list_usable_channels()[0].inbound_scid_alias.unwrap());
500 route.paths[0][1].fee_msat = 10_000_000; // Overshoot the last channel's value
502 // Route the HTLC through to the destination.
503 nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret)).unwrap();
504 check_added_monitors!(nodes[0], 1);
505 let as_updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
506 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &as_updates.update_add_htlcs[0]);
507 commitment_signed_dance!(nodes[1], nodes[0], &as_updates.commitment_signed, false, true);
509 expect_pending_htlcs_forwardable!(nodes[1]);
510 expect_pending_htlcs_forwardable!(nodes[1]);
511 check_added_monitors!(nodes[1], 1);
513 let bs_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
514 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &bs_updates.update_fail_htlcs[0]);
515 commitment_signed_dance!(nodes[0], nodes[1], bs_updates.commitment_signed, false, true);
517 // Build the expected channel update
518 let contents = msgs::UnsignedChannelUpdate {
519 chain_hash: genesis_block(Network::Testnet).header.block_hash(),
520 short_channel_id: last_hop[0].inbound_scid_alias.unwrap(),
523 cltv_expiry_delta: accept_forward_cfg.channel_options.cltv_expiry_delta,
524 htlc_minimum_msat: 1_000,
525 htlc_maximum_msat: OptionalField::Present(1_000_000), // Defaults to 10% of the channel value
526 fee_base_msat: last_hop[0].counterparty.forwarding_info.as_ref().unwrap().fee_base_msat,
527 fee_proportional_millionths: last_hop[0].counterparty.forwarding_info.as_ref().unwrap().fee_proportional_millionths,
528 excess_data: Vec::new(),
530 let msg_hash = Sha256dHash::hash(&contents.encode()[..]);
531 let signature = Secp256k1::new().sign_ecdsa(&hash_to_message!(&msg_hash[..]), &nodes[1].keys_manager.get_node_secret(Recipient::Node).unwrap());
532 let msg = msgs::ChannelUpdate { signature, contents };
534 expect_payment_failed_conditions!(nodes[0], payment_hash, false,
535 PaymentFailedConditions::new().blamed_scid(last_hop[0].inbound_scid_alias.unwrap())
536 .blamed_chan_closed(false).expected_htlc_error_data(0x1000|7, &msg.encode_with_len()));
538 route.paths[0][1].fee_msat = 10_000; // Reset to the correct payment amount
539 route.paths[0][0].fee_msat = 0; // But set fee paid to the middle hop to 0
541 // Route the HTLC through to the destination.
542 nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret)).unwrap();
543 check_added_monitors!(nodes[0], 1);
544 let as_updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
545 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &as_updates.update_add_htlcs[0]);
546 commitment_signed_dance!(nodes[1], nodes[0], &as_updates.commitment_signed, false, true);
548 let bs_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
549 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &bs_updates.update_fail_htlcs[0]);
550 commitment_signed_dance!(nodes[0], nodes[1], bs_updates.commitment_signed, false, true);
552 let mut err_data = Vec::new();
553 err_data.extend_from_slice(&10_000u64.to_be_bytes());
554 err_data.extend_from_slice(&msg.encode_with_len());
555 expect_payment_failed_conditions!(nodes[0], payment_hash, false,
556 PaymentFailedConditions::new().blamed_scid(last_hop[0].inbound_scid_alias.unwrap())
557 .blamed_chan_closed(false).expected_htlc_error_data(0x1000|12, &err_data));