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, ChannelUpdate};
24 use util::enforcing_trait_impls::EnforcingSigner;
25 use util::events::{Event, MessageSendEvent, MessageSendEventsProvider};
26 use util::config::UserConfig;
27 use util::ser::{Writeable, ReadableArgs};
31 use core::default::Default;
33 use ln::functional_test_utils::*;
35 use bitcoin::blockdata::constants::genesis_block;
36 use bitcoin::hash_types::BlockHash;
37 use bitcoin::hashes::Hash;
38 use bitcoin::hashes::sha256d::Hash as Sha256dHash;
39 use bitcoin::network::constants::Network;
40 use bitcoin::secp256k1::Secp256k1;
43 fn test_priv_forwarding_rejection() {
44 // If we have a private channel with outbound liquidity, and
45 // UserConfig::accept_forwards_to_priv_channels is set to false, we should reject any attempts
46 // to forward through that channel.
47 let chanmon_cfgs = create_chanmon_cfgs(3);
48 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
49 let mut no_announce_cfg = test_default_channel_config();
50 no_announce_cfg.accept_forwards_to_priv_channels = false;
51 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, Some(no_announce_cfg), None]);
52 let persister: test_utils::TestPersister;
53 let new_chain_monitor: test_utils::TestChainMonitor;
54 let nodes_1_deserialized: ChannelManager<EnforcingSigner, &test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>;
55 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
57 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;
58 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;
60 // We should always be able to forward through nodes[1] as long as its out through a public
62 send_payment(&nodes[2], &[&nodes[1], &nodes[0]], 10_000);
64 // ... however, if we send to nodes[2], we will have to pass the private channel from nodes[1]
65 // to nodes[2], which should be rejected:
66 let route_hint = RouteHint(vec![RouteHintHop {
67 src_node_id: nodes[1].node.get_our_node_id(),
68 short_channel_id: nodes[2].node.list_channels()[0].short_channel_id.unwrap(),
69 fees: RoutingFees { base_msat: 1000, proportional_millionths: 0 },
70 cltv_expiry_delta: MIN_CLTV_EXPIRY_DELTA,
71 htlc_minimum_msat: None,
72 htlc_maximum_msat: None,
74 let last_hops = vec![route_hint];
75 let payment_params = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id())
76 .with_features(InvoiceFeatures::known())
77 .with_route_hints(last_hops);
78 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);
80 nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).unwrap();
81 check_added_monitors!(nodes[0], 1);
82 let payment_event = SendEvent::from_event(nodes[0].node.get_and_clear_pending_msg_events().remove(0));
83 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
84 commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false, true);
86 let htlc_fail_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
87 assert!(htlc_fail_updates.update_add_htlcs.is_empty());
88 assert_eq!(htlc_fail_updates.update_fail_htlcs.len(), 1);
89 assert!(htlc_fail_updates.update_fail_malformed_htlcs.is_empty());
90 assert!(htlc_fail_updates.update_fee.is_none());
92 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &htlc_fail_updates.update_fail_htlcs[0]);
93 commitment_signed_dance!(nodes[0], nodes[1], htlc_fail_updates.commitment_signed, true, true);
94 expect_payment_failed_with_update!(nodes[0], our_payment_hash, false, nodes[2].node.list_channels()[0].short_channel_id.unwrap(), true);
96 // Now disconnect nodes[1] from its peers and restart with accept_forwards_to_priv_channels set
97 // to true. Sadly there is currently no way to change it at runtime.
99 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
100 nodes[2].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
102 let nodes_1_serialized = nodes[1].node.encode();
103 let mut monitor_a_serialized = test_utils::TestVecWriter(Vec::new());
104 let mut monitor_b_serialized = test_utils::TestVecWriter(Vec::new());
105 get_monitor!(nodes[1], chan_id_1).write(&mut monitor_a_serialized).unwrap();
106 get_monitor!(nodes[1], chan_id_2).write(&mut monitor_b_serialized).unwrap();
108 persister = test_utils::TestPersister::new();
109 let keys_manager = &chanmon_cfgs[1].keys_manager;
110 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);
111 nodes[1].chain_monitor = &new_chain_monitor;
113 let mut monitor_a_read = &monitor_a_serialized.0[..];
114 let mut monitor_b_read = &monitor_b_serialized.0[..];
115 let (_, mut monitor_a) = <(BlockHash, ChannelMonitor<EnforcingSigner>)>::read(&mut monitor_a_read, keys_manager).unwrap();
116 let (_, mut monitor_b) = <(BlockHash, ChannelMonitor<EnforcingSigner>)>::read(&mut monitor_b_read, keys_manager).unwrap();
117 assert!(monitor_a_read.is_empty());
118 assert!(monitor_b_read.is_empty());
120 no_announce_cfg.accept_forwards_to_priv_channels = true;
122 let mut nodes_1_read = &nodes_1_serialized[..];
123 let (_, nodes_1_deserialized_tmp) = {
124 let mut channel_monitors = HashMap::new();
125 channel_monitors.insert(monitor_a.get_funding_txo().0, &mut monitor_a);
126 channel_monitors.insert(monitor_b.get_funding_txo().0, &mut monitor_b);
127 <(BlockHash, ChannelManager<EnforcingSigner, &test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>)>::read(&mut nodes_1_read, ChannelManagerReadArgs {
128 default_config: no_announce_cfg,
130 fee_estimator: node_cfgs[1].fee_estimator,
131 chain_monitor: nodes[1].chain_monitor,
132 tx_broadcaster: nodes[1].tx_broadcaster.clone(),
133 logger: nodes[1].logger,
137 assert!(nodes_1_read.is_empty());
138 nodes_1_deserialized = nodes_1_deserialized_tmp;
140 assert!(nodes[1].chain_monitor.watch_channel(monitor_a.get_funding_txo().0, monitor_a).is_ok());
141 assert!(nodes[1].chain_monitor.watch_channel(monitor_b.get_funding_txo().0, monitor_b).is_ok());
142 check_added_monitors!(nodes[1], 2);
143 nodes[1].node = &nodes_1_deserialized;
145 nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: InitFeatures::known(), remote_network_address: None });
146 nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty(), remote_network_address: None });
147 let as_reestablish = get_event_msg!(nodes[0], MessageSendEvent::SendChannelReestablish, nodes[1].node.get_our_node_id());
148 let bs_reestablish = get_event_msg!(nodes[1], MessageSendEvent::SendChannelReestablish, nodes[0].node.get_our_node_id());
149 nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &as_reestablish);
150 nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &bs_reestablish);
151 get_event_msg!(nodes[0], MessageSendEvent::SendChannelUpdate, nodes[1].node.get_our_node_id());
152 get_event_msg!(nodes[1], MessageSendEvent::SendChannelUpdate, nodes[0].node.get_our_node_id());
154 nodes[1].node.peer_connected(&nodes[2].node.get_our_node_id(), &msgs::Init { features: InitFeatures::known(), remote_network_address: None });
155 nodes[2].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty(), remote_network_address: None });
156 let bs_reestablish = get_event_msg!(nodes[1], MessageSendEvent::SendChannelReestablish, nodes[2].node.get_our_node_id());
157 let cs_reestablish = get_event_msg!(nodes[2], MessageSendEvent::SendChannelReestablish, nodes[1].node.get_our_node_id());
158 nodes[2].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &bs_reestablish);
159 nodes[1].node.handle_channel_reestablish(&nodes[2].node.get_our_node_id(), &cs_reestablish);
160 get_event_msg!(nodes[1], MessageSendEvent::SendChannelUpdate, nodes[2].node.get_our_node_id());
161 get_event_msg!(nodes[2], MessageSendEvent::SendChannelUpdate, nodes[1].node.get_our_node_id());
163 nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).unwrap();
164 check_added_monitors!(nodes[0], 1);
165 pass_along_route(&nodes[0], &[&[&nodes[1], &nodes[2]]], 10_000, our_payment_hash, our_payment_secret);
166 claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], our_payment_preimage);
169 fn do_test_1_conf_open(connect_style: ConnectStyle) {
170 // Previously, if the minium_depth config was set to 1, we'd never send a funding_locked. This
171 // tests that we properly send one in that case.
172 let mut alice_config = UserConfig::default();
173 alice_config.own_channel_config.minimum_depth = 1;
174 alice_config.channel_options.announced_channel = true;
175 alice_config.peer_channel_config_limits.force_announced_channel_preference = false;
176 let mut bob_config = UserConfig::default();
177 bob_config.own_channel_config.minimum_depth = 1;
178 bob_config.channel_options.announced_channel = true;
179 bob_config.peer_channel_config_limits.force_announced_channel_preference = false;
180 let chanmon_cfgs = create_chanmon_cfgs(2);
181 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
182 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(alice_config), Some(bob_config)]);
183 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
184 *nodes[0].connect_style.borrow_mut() = connect_style;
186 let tx = create_chan_between_nodes_with_value_init(&nodes[0], &nodes[1], 100000, 10001, InitFeatures::known(), InitFeatures::known());
187 mine_transaction(&nodes[1], &tx);
188 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()));
189 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
191 mine_transaction(&nodes[0], &tx);
192 let as_msg_events = nodes[0].node.get_and_clear_pending_msg_events();
193 assert_eq!(as_msg_events.len(), 2);
194 let as_funding_locked = if let MessageSendEvent::SendFundingLocked { ref node_id, ref msg } = as_msg_events[0] {
195 assert_eq!(*node_id, nodes[1].node.get_our_node_id());
197 } else { panic!("Unexpected event"); };
198 if let MessageSendEvent::SendChannelUpdate { ref node_id, msg: _ } = as_msg_events[1] {
199 assert_eq!(*node_id, nodes[1].node.get_our_node_id());
200 } else { panic!("Unexpected event"); }
202 nodes[1].node.handle_funding_locked(&nodes[0].node.get_our_node_id(), &as_funding_locked);
203 let bs_msg_events = nodes[1].node.get_and_clear_pending_msg_events();
204 assert_eq!(bs_msg_events.len(), 1);
205 if let MessageSendEvent::SendChannelUpdate { ref node_id, msg: _ } = bs_msg_events[0] {
206 assert_eq!(*node_id, nodes[0].node.get_our_node_id());
207 } else { panic!("Unexpected event"); }
209 send_payment(&nodes[0], &[&nodes[1]], 100_000);
211 // After 6 confirmations, as required by the spec, we'll send announcement_signatures and
212 // broadcast the channel_announcement (but not before exactly 6 confirmations).
213 connect_blocks(&nodes[0], 4);
214 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
215 connect_blocks(&nodes[0], 1);
216 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()));
217 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
219 connect_blocks(&nodes[1], 5);
220 let bs_announce_events = nodes[1].node.get_and_clear_pending_msg_events();
221 assert_eq!(bs_announce_events.len(), 2);
222 let bs_announcement_sigs = if let MessageSendEvent::SendAnnouncementSignatures { ref node_id, ref msg } = bs_announce_events[0] {
223 assert_eq!(*node_id, nodes[0].node.get_our_node_id());
225 } else { panic!("Unexpected event"); };
226 let (bs_announcement, bs_update) = if let MessageSendEvent::BroadcastChannelAnnouncement { ref msg, ref update_msg } = bs_announce_events[1] {
227 (msg.clone(), update_msg.clone())
228 } else { panic!("Unexpected event"); };
230 nodes[0].node.handle_announcement_signatures(&nodes[1].node.get_our_node_id(), &bs_announcement_sigs);
231 let as_announce_events = nodes[0].node.get_and_clear_pending_msg_events();
232 assert_eq!(as_announce_events.len(), 1);
233 let (announcement, as_update) = if let MessageSendEvent::BroadcastChannelAnnouncement { ref msg, ref update_msg } = as_announce_events[0] {
234 (msg.clone(), update_msg.clone())
235 } else { panic!("Unexpected event"); };
236 assert_eq!(announcement, bs_announcement);
239 assert!(node.net_graph_msg_handler.handle_channel_announcement(&announcement).unwrap());
240 node.net_graph_msg_handler.handle_channel_update(&as_update).unwrap();
241 node.net_graph_msg_handler.handle_channel_update(&bs_update).unwrap();
245 fn test_1_conf_open() {
246 do_test_1_conf_open(ConnectStyle::BestBlockFirst);
247 do_test_1_conf_open(ConnectStyle::TransactionsFirst);
248 do_test_1_conf_open(ConnectStyle::FullBlockViaListen);
252 fn test_routed_scid_alias() {
253 // Trivially test sending a payment which is routed through an SCID alias.
254 let chanmon_cfgs = create_chanmon_cfgs(3);
255 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
256 let mut no_announce_cfg = test_default_channel_config();
257 no_announce_cfg.accept_forwards_to_priv_channels = true;
258 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, Some(no_announce_cfg), None]);
259 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
261 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 500_000_000, InitFeatures::known(), InitFeatures::known()).2;
262 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;
264 let last_hop = nodes[2].node.list_usable_channels();
265 let hop_hints = vec![RouteHint(vec![RouteHintHop {
266 src_node_id: nodes[1].node.get_our_node_id(),
267 short_channel_id: last_hop[0].inbound_scid_alias.unwrap(),
269 base_msat: last_hop[0].counterparty.forwarding_info.as_ref().unwrap().fee_base_msat,
270 proportional_millionths: last_hop[0].counterparty.forwarding_info.as_ref().unwrap().fee_proportional_millionths,
272 cltv_expiry_delta: last_hop[0].counterparty.forwarding_info.as_ref().unwrap().cltv_expiry_delta,
273 htlc_maximum_msat: None,
274 htlc_minimum_msat: None,
276 let payment_params = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id())
277 .with_features(InvoiceFeatures::known())
278 .with_route_hints(hop_hints);
279 let (route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], payment_params, 100_000, 42);
280 assert_eq!(route.paths[0][1].short_channel_id, last_hop[0].inbound_scid_alias.unwrap());
281 nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret)).unwrap();
282 check_added_monitors!(nodes[0], 1);
284 pass_along_route(&nodes[0], &[&[&nodes[1], &nodes[2]]], 100_000, payment_hash, payment_secret);
285 claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], payment_preimage);
287 // Now test that if a peer sends us a second funding_locked after the channel is operational we
288 // will use the new alias.
289 as_funding_locked.short_channel_id_alias = Some(0xdeadbeef);
290 nodes[2].node.handle_funding_locked(&nodes[1].node.get_our_node_id(), &as_funding_locked);
291 // Note that we always respond to a funding_locked with a channel_update. Not a lot of reason
292 // to bother updating that code, so just drop the message here.
293 get_event_msg!(nodes[2], MessageSendEvent::SendChannelUpdate, nodes[1].node.get_our_node_id());
294 let updated_channel_info = nodes[2].node.list_usable_channels();
295 assert_eq!(updated_channel_info.len(), 1);
296 assert_eq!(updated_channel_info[0].inbound_scid_alias.unwrap(), 0xdeadbeef);
297 // Note that because we never send a duplicate funding_locked we can't send a payment through
298 // the 0xdeadbeef SCID alias.
302 fn test_scid_privacy_on_pub_channel() {
303 // Tests rejecting the scid_privacy feature for public channels and that we don't ever try to
305 let chanmon_cfgs = create_chanmon_cfgs(2);
306 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
307 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
308 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
310 let mut scid_privacy_cfg = test_default_channel_config();
311 scid_privacy_cfg.channel_options.announced_channel = true;
312 scid_privacy_cfg.own_channel_config.negotiate_scid_privacy = true;
313 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, Some(scid_privacy_cfg)).unwrap();
314 let mut open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
316 assert!(!open_channel.channel_type.as_ref().unwrap().supports_scid_privacy()); // we ignore `negotiate_scid_privacy` on pub channels
317 open_channel.channel_type.as_mut().unwrap().set_scid_privacy_required();
318 assert_eq!(open_channel.channel_flags & 1, 1); // The `announce_channel` bit is set.
320 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), InitFeatures::known(), &open_channel);
321 let err = get_err_msg!(nodes[1], nodes[0].node.get_our_node_id());
322 assert_eq!(err.data, "SCID Alias/Privacy Channel Type cannot be set on a public channel");
326 fn test_scid_privacy_negotiation() {
327 // Tests of the negotiation of SCID alias and falling back to non-SCID-alias if our
328 // counterparty doesn't support it.
329 let chanmon_cfgs = create_chanmon_cfgs(2);
330 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
331 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
332 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
334 let mut scid_privacy_cfg = test_default_channel_config();
335 scid_privacy_cfg.channel_options.announced_channel = false;
336 scid_privacy_cfg.own_channel_config.negotiate_scid_privacy = true;
337 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, Some(scid_privacy_cfg)).unwrap();
339 let init_open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
340 assert!(init_open_channel.channel_type.as_ref().unwrap().supports_scid_privacy());
341 assert!(nodes[0].node.list_channels()[0].channel_type.is_none()); // channel_type is none until counterparty accepts
343 // now simulate nodes[1] responding with an Error message, indicating it doesn't understand
345 nodes[0].node.handle_error(&nodes[1].node.get_our_node_id(), &msgs::ErrorMessage {
346 channel_id: init_open_channel.temporary_channel_id,
347 data: "Yo, no SCID aliases, no privacy here!".to_string()
349 assert!(nodes[0].node.list_channels()[0].channel_type.is_none()); // channel_type is none until counterparty accepts
351 let second_open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
352 assert!(!second_open_channel.channel_type.as_ref().unwrap().supports_scid_privacy());
353 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), InitFeatures::known(), &second_open_channel);
354 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()));
356 let events = nodes[0].node.get_and_clear_pending_events();
357 assert_eq!(events.len(), 1);
359 Event::FundingGenerationReady { .. } => {},
360 _ => panic!("Unexpected event"),
363 assert!(!nodes[0].node.list_channels()[0].channel_type.as_ref().unwrap().supports_scid_privacy());
364 assert!(!nodes[1].node.list_channels()[0].channel_type.as_ref().unwrap().supports_scid_privacy());
368 fn test_inbound_scid_privacy() {
369 // Tests accepting channels with the scid_privacy feature and rejecting forwards using the
370 // channel's real SCID as required by the channel feature.
371 let chanmon_cfgs = create_chanmon_cfgs(3);
372 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
373 let mut accept_forward_cfg = test_default_channel_config();
374 accept_forward_cfg.accept_forwards_to_priv_channels = true;
375 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, Some(accept_forward_cfg), None]);
376 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
378 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0, InitFeatures::known(), InitFeatures::known());
380 let mut no_announce_cfg = test_default_channel_config();
381 no_announce_cfg.channel_options.announced_channel = false;
382 no_announce_cfg.own_channel_config.negotiate_scid_privacy = true;
383 nodes[1].node.create_channel(nodes[2].node.get_our_node_id(), 100_000, 10_000, 42, Some(no_announce_cfg)).unwrap();
384 let mut open_channel = get_event_msg!(nodes[1], MessageSendEvent::SendOpenChannel, nodes[2].node.get_our_node_id());
386 assert!(open_channel.channel_type.as_ref().unwrap().requires_scid_privacy());
388 nodes[2].node.handle_open_channel(&nodes[1].node.get_our_node_id(), InitFeatures::known(), &open_channel);
389 let accept_channel = get_event_msg!(nodes[2], MessageSendEvent::SendAcceptChannel, nodes[1].node.get_our_node_id());
390 nodes[1].node.handle_accept_channel(&nodes[2].node.get_our_node_id(), InitFeatures::known(), &accept_channel);
392 let (temporary_channel_id, tx, _) = create_funding_transaction(&nodes[1], 100_000, 42);
393 nodes[1].node.funding_transaction_generated(&temporary_channel_id, tx.clone()).unwrap();
394 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()));
395 check_added_monitors!(nodes[2], 1);
397 let cs_funding_signed = get_event_msg!(nodes[2], MessageSendEvent::SendFundingSigned, nodes[1].node.get_our_node_id());
398 nodes[1].node.handle_funding_signed(&nodes[2].node.get_our_node_id(), &cs_funding_signed);
399 check_added_monitors!(nodes[1], 1);
401 let conf_height = core::cmp::max(nodes[1].best_block_info().1 + 1, nodes[2].best_block_info().1 + 1);
402 confirm_transaction_at(&nodes[1], &tx, conf_height);
403 connect_blocks(&nodes[1], CHAN_CONFIRM_DEPTH - 1);
404 confirm_transaction_at(&nodes[2], &tx, conf_height);
405 connect_blocks(&nodes[2], CHAN_CONFIRM_DEPTH - 1);
406 let bs_funding_locked = get_event_msg!(nodes[1], MessageSendEvent::SendFundingLocked, nodes[2].node.get_our_node_id());
407 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()));
408 let bs_update = get_event_msg!(nodes[1], MessageSendEvent::SendChannelUpdate, nodes[2].node.get_our_node_id());
409 nodes[2].node.handle_funding_locked(&nodes[1].node.get_our_node_id(), &bs_funding_locked);
410 let cs_update = get_event_msg!(nodes[2], MessageSendEvent::SendChannelUpdate, nodes[1].node.get_our_node_id());
412 nodes[1].node.handle_channel_update(&nodes[2].node.get_our_node_id(), &cs_update);
413 nodes[2].node.handle_channel_update(&nodes[1].node.get_our_node_id(), &bs_update);
415 // Now we can pay just fine using the SCID alias nodes[2] gave to nodes[1]...
417 let last_hop = nodes[2].node.list_usable_channels();
418 let mut hop_hints = vec![RouteHint(vec![RouteHintHop {
419 src_node_id: nodes[1].node.get_our_node_id(),
420 short_channel_id: last_hop[0].inbound_scid_alias.unwrap(),
422 base_msat: last_hop[0].counterparty.forwarding_info.as_ref().unwrap().fee_base_msat,
423 proportional_millionths: last_hop[0].counterparty.forwarding_info.as_ref().unwrap().fee_proportional_millionths,
425 cltv_expiry_delta: last_hop[0].counterparty.forwarding_info.as_ref().unwrap().cltv_expiry_delta,
426 htlc_maximum_msat: None,
427 htlc_minimum_msat: None,
429 let payment_params = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id())
430 .with_features(InvoiceFeatures::known())
431 .with_route_hints(hop_hints.clone());
432 let (route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], payment_params, 100_000, 42);
433 assert_eq!(route.paths[0][1].short_channel_id, last_hop[0].inbound_scid_alias.unwrap());
434 nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret)).unwrap();
435 check_added_monitors!(nodes[0], 1);
437 pass_along_route(&nodes[0], &[&[&nodes[1], &nodes[2]]], 100_000, payment_hash, payment_secret);
438 claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], payment_preimage);
440 // ... but if we try to pay using the real SCID, nodes[1] will just tell us they don't know
441 // what channel we're talking about.
442 hop_hints[0].0[0].short_channel_id = last_hop[0].short_channel_id.unwrap();
444 let payment_params_2 = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id())
445 .with_features(InvoiceFeatures::known())
446 .with_route_hints(hop_hints);
447 let (route_2, payment_hash_2, _, payment_secret_2) = get_route_and_payment_hash!(nodes[0], nodes[2], payment_params_2, 100_000, 42);
448 assert_eq!(route_2.paths[0][1].short_channel_id, last_hop[0].short_channel_id.unwrap());
449 nodes[0].node.send_payment(&route_2, payment_hash_2, &Some(payment_secret_2)).unwrap();
450 check_added_monitors!(nodes[0], 1);
452 let payment_event = SendEvent::from_node(&nodes[0]);
453 assert_eq!(nodes[1].node.get_our_node_id(), payment_event.node_id);
454 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
455 commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, true, true);
457 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);
459 let mut updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
460 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
461 commitment_signed_dance!(nodes[0], nodes[1], updates.commitment_signed, false);
463 expect_payment_failed_conditions!(nodes[0], payment_hash_2, false,
464 PaymentFailedConditions::new().blamed_scid(last_hop[0].short_channel_id.unwrap())
465 .blamed_chan_closed(true).expected_htlc_error_data(0x4000|10, &[0; 0]));
469 fn test_scid_alias_returned() {
470 // Tests that when we fail an HTLC (in this case due to attempting to forward more than the
471 // channel's available balance) we use the correct (in this case the aliased) SCID in the
472 // channel_update which is returned in the onion to the sender.
473 let chanmon_cfgs = create_chanmon_cfgs(3);
474 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
475 let mut accept_forward_cfg = test_default_channel_config();
476 accept_forward_cfg.accept_forwards_to_priv_channels = true;
477 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, Some(accept_forward_cfg), None]);
478 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
480 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 0, InitFeatures::known(), InitFeatures::known());
481 create_unannounced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000, 0, InitFeatures::known(), InitFeatures::known());
483 let last_hop = nodes[2].node.list_usable_channels();
484 let mut hop_hints = vec![RouteHint(vec![RouteHintHop {
485 src_node_id: nodes[1].node.get_our_node_id(),
486 short_channel_id: last_hop[0].inbound_scid_alias.unwrap(),
488 base_msat: last_hop[0].counterparty.forwarding_info.as_ref().unwrap().fee_base_msat,
489 proportional_millionths: last_hop[0].counterparty.forwarding_info.as_ref().unwrap().fee_proportional_millionths,
491 cltv_expiry_delta: last_hop[0].counterparty.forwarding_info.as_ref().unwrap().cltv_expiry_delta,
492 htlc_maximum_msat: None,
493 htlc_minimum_msat: None,
495 let payment_params = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id())
496 .with_features(InvoiceFeatures::known())
497 .with_route_hints(hop_hints);
498 let (mut route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], payment_params, 10_000, 42);
499 assert_eq!(route.paths[0][1].short_channel_id, nodes[2].node.list_usable_channels()[0].inbound_scid_alias.unwrap());
501 route.paths[0][1].fee_msat = 10_000_000; // Overshoot the last channel's value
503 // Route the HTLC through to the destination.
504 nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret)).unwrap();
505 check_added_monitors!(nodes[0], 1);
506 let as_updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
507 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &as_updates.update_add_htlcs[0]);
508 commitment_signed_dance!(nodes[1], nodes[0], &as_updates.commitment_signed, false, true);
510 expect_pending_htlcs_forwardable!(nodes[1]);
511 expect_pending_htlcs_forwardable!(nodes[1]);
512 check_added_monitors!(nodes[1], 1);
514 let bs_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
515 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &bs_updates.update_fail_htlcs[0]);
516 commitment_signed_dance!(nodes[0], nodes[1], bs_updates.commitment_signed, false, true);
518 // Build the expected channel update
519 let contents = msgs::UnsignedChannelUpdate {
520 chain_hash: genesis_block(Network::Testnet).header.block_hash(),
521 short_channel_id: last_hop[0].inbound_scid_alias.unwrap(),
524 cltv_expiry_delta: accept_forward_cfg.channel_options.cltv_expiry_delta,
525 htlc_minimum_msat: 1_000,
526 htlc_maximum_msat: OptionalField::Present(1_000_000), // Defaults to 10% of the channel value
527 fee_base_msat: last_hop[0].counterparty.forwarding_info.as_ref().unwrap().fee_base_msat,
528 fee_proportional_millionths: last_hop[0].counterparty.forwarding_info.as_ref().unwrap().fee_proportional_millionths,
529 excess_data: Vec::new(),
531 let msg_hash = Sha256dHash::hash(&contents.encode()[..]);
532 let signature = Secp256k1::new().sign_ecdsa(&hash_to_message!(&msg_hash[..]), &nodes[1].keys_manager.get_node_secret(Recipient::Node).unwrap());
533 let msg = msgs::ChannelUpdate { signature, contents };
535 let mut err_data = Vec::new();
536 err_data.extend_from_slice(&(msg.serialized_length() as u16 + 2).to_be_bytes());
537 err_data.extend_from_slice(&ChannelUpdate::TYPE.to_be_bytes());
538 err_data.extend_from_slice(&msg.encode());
540 expect_payment_failed_conditions!(nodes[0], payment_hash, false,
541 PaymentFailedConditions::new().blamed_scid(last_hop[0].inbound_scid_alias.unwrap())
542 .blamed_chan_closed(false).expected_htlc_error_data(0x1000|7, &err_data));
544 route.paths[0][1].fee_msat = 10_000; // Reset to the correct payment amount
545 route.paths[0][0].fee_msat = 0; // But set fee paid to the middle hop to 0
547 // Route the HTLC through to the destination.
548 nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret)).unwrap();
549 check_added_monitors!(nodes[0], 1);
550 let as_updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
551 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &as_updates.update_add_htlcs[0]);
552 commitment_signed_dance!(nodes[1], nodes[0], &as_updates.commitment_signed, false, true);
554 let bs_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
555 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &bs_updates.update_fail_htlcs[0]);
556 commitment_signed_dance!(nodes[0], nodes[1], bs_updates.commitment_signed, false, true);
558 let mut err_data = Vec::new();
559 err_data.extend_from_slice(&10_000u64.to_be_bytes());
560 err_data.extend_from_slice(&(msg.serialized_length() as u16 + 2).to_be_bytes());
561 err_data.extend_from_slice(&ChannelUpdate::TYPE.to_be_bytes());
562 err_data.extend_from_slice(&msg.encode());
563 expect_payment_failed_conditions!(nodes[0], payment_hash, false,
564 PaymentFailedConditions::new().blamed_scid(last_hop[0].inbound_scid_alias.unwrap())
565 .blamed_chan_closed(false).expected_htlc_error_data(0x1000|12, &err_data));