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
14 use crate::chain::ChannelMonitorUpdateStatus;
15 use crate::chain::keysinterface::NodeSigner;
16 use crate::ln::channelmanager::{ChannelManager, MIN_CLTV_EXPIRY_DELTA, PaymentId};
17 use crate::routing::gossip::RoutingFees;
18 use crate::routing::router::{PaymentParameters, RouteHint, RouteHintHop};
19 use crate::ln::features::ChannelTypeFeatures;
21 use crate::ln::msgs::{ChannelMessageHandler, RoutingMessageHandler, ChannelUpdate, ErrorAction};
22 use crate::ln::wire::Encode;
23 use crate::util::events::{ClosureReason, Event, HTLCDestination, MessageSendEvent, MessageSendEventsProvider};
24 use crate::util::config::UserConfig;
25 use crate::util::ser::Writeable;
26 use crate::util::test_utils;
28 use crate::prelude::*;
29 use core::default::Default;
31 use crate::ln::functional_test_utils::*;
33 use bitcoin::blockdata::constants::genesis_block;
34 use bitcoin::network::constants::Network;
37 fn test_priv_forwarding_rejection() {
38 // If we have a private channel with outbound liquidity, and
39 // UserConfig::accept_forwards_to_priv_channels is set to false, we should reject any attempts
40 // to forward through that channel.
41 let chanmon_cfgs = create_chanmon_cfgs(3);
42 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
43 let mut no_announce_cfg = test_default_channel_config();
44 no_announce_cfg.accept_forwards_to_priv_channels = false;
45 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, Some(no_announce_cfg), None]);
46 let persister: test_utils::TestPersister;
47 let new_chain_monitor: test_utils::TestChainMonitor;
48 let nodes_1_deserialized: ChannelManager<&test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestKeysInterface, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestRouter, &test_utils::TestLogger>;
49 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
51 let chan_id_1 = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 500_000_000).2;
52 let chan_id_2 = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 500_000_000).0.channel_id;
54 // We should always be able to forward through nodes[1] as long as its out through a public
56 send_payment(&nodes[2], &[&nodes[1], &nodes[0]], 10_000);
58 // ... however, if we send to nodes[2], we will have to pass the private channel from nodes[1]
59 // to nodes[2], which should be rejected:
60 let route_hint = RouteHint(vec![RouteHintHop {
61 src_node_id: nodes[1].node.get_our_node_id(),
62 short_channel_id: nodes[2].node.list_channels()[0].short_channel_id.unwrap(),
63 fees: RoutingFees { base_msat: 1000, proportional_millionths: 0 },
64 cltv_expiry_delta: MIN_CLTV_EXPIRY_DELTA,
65 htlc_minimum_msat: None,
66 htlc_maximum_msat: None,
68 let last_hops = vec![route_hint];
69 let payment_params = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id(), TEST_FINAL_CLTV)
70 .with_features(nodes[2].node.invoice_features())
71 .with_route_hints(last_hops);
72 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);
74 nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
75 check_added_monitors!(nodes[0], 1);
76 let payment_event = SendEvent::from_event(nodes[0].node.get_and_clear_pending_msg_events().remove(0));
77 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
78 commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false, true);
80 let htlc_fail_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
81 assert!(htlc_fail_updates.update_add_htlcs.is_empty());
82 assert_eq!(htlc_fail_updates.update_fail_htlcs.len(), 1);
83 assert!(htlc_fail_updates.update_fail_malformed_htlcs.is_empty());
84 assert!(htlc_fail_updates.update_fee.is_none());
86 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &htlc_fail_updates.update_fail_htlcs[0]);
87 commitment_signed_dance!(nodes[0], nodes[1], htlc_fail_updates.commitment_signed, true, true);
88 expect_payment_failed_with_update!(nodes[0], our_payment_hash, false, nodes[2].node.list_channels()[0].short_channel_id.unwrap(), true);
90 // Now disconnect nodes[1] from its peers and restart with accept_forwards_to_priv_channels set
91 // to true. Sadly there is currently no way to change it at runtime.
93 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
94 nodes[2].node.peer_disconnected(&nodes[1].node.get_our_node_id());
96 let nodes_1_serialized = nodes[1].node.encode();
97 let monitor_a_serialized = get_monitor!(nodes[1], chan_id_1).encode();
98 let monitor_b_serialized = get_monitor!(nodes[1], chan_id_2).encode();
100 no_announce_cfg.accept_forwards_to_priv_channels = true;
101 reload_node!(nodes[1], no_announce_cfg, &nodes_1_serialized, &[&monitor_a_serialized, &monitor_b_serialized], persister, new_chain_monitor, nodes_1_deserialized);
103 nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: nodes[1].node.init_features(), remote_network_address: None }, true).unwrap();
104 nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: nodes[0].node.init_features(), remote_network_address: None }, false).unwrap();
105 let as_reestablish = get_chan_reestablish_msgs!(nodes[0], nodes[1]).pop().unwrap();
106 let bs_reestablish = get_chan_reestablish_msgs!(nodes[1], nodes[0]).pop().unwrap();
107 nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &as_reestablish);
108 nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &bs_reestablish);
109 get_event_msg!(nodes[0], MessageSendEvent::SendChannelUpdate, nodes[1].node.get_our_node_id());
110 get_event_msg!(nodes[1], MessageSendEvent::SendChannelUpdate, nodes[0].node.get_our_node_id());
112 nodes[1].node.peer_connected(&nodes[2].node.get_our_node_id(), &msgs::Init { features: nodes[2].node.init_features(), remote_network_address: None }, true).unwrap();
113 nodes[2].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: nodes[1].node.init_features(), remote_network_address: None }, false).unwrap();
114 let bs_reestablish = get_chan_reestablish_msgs!(nodes[1], nodes[2]).pop().unwrap();
115 let cs_reestablish = get_chan_reestablish_msgs!(nodes[2], nodes[1]).pop().unwrap();
116 nodes[2].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &bs_reestablish);
117 nodes[1].node.handle_channel_reestablish(&nodes[2].node.get_our_node_id(), &cs_reestablish);
118 get_event_msg!(nodes[1], MessageSendEvent::SendChannelUpdate, nodes[2].node.get_our_node_id());
119 get_event_msg!(nodes[2], MessageSendEvent::SendChannelUpdate, nodes[1].node.get_our_node_id());
121 nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
122 check_added_monitors!(nodes[0], 1);
123 pass_along_route(&nodes[0], &[&[&nodes[1], &nodes[2]]], 10_000, our_payment_hash, our_payment_secret);
124 claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], our_payment_preimage);
127 fn do_test_1_conf_open(connect_style: ConnectStyle) {
128 // Previously, if the minium_depth config was set to 1, we'd never send a channel_ready. This
129 // tests that we properly send one in that case.
130 let mut alice_config = UserConfig::default();
131 alice_config.channel_handshake_config.minimum_depth = 1;
132 alice_config.channel_handshake_config.announced_channel = true;
133 alice_config.channel_handshake_limits.force_announced_channel_preference = false;
134 let mut bob_config = UserConfig::default();
135 bob_config.channel_handshake_config.minimum_depth = 1;
136 bob_config.channel_handshake_config.announced_channel = true;
137 bob_config.channel_handshake_limits.force_announced_channel_preference = false;
138 let chanmon_cfgs = create_chanmon_cfgs(2);
139 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
140 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(alice_config), Some(bob_config)]);
141 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
142 *nodes[0].connect_style.borrow_mut() = connect_style;
144 let tx = create_chan_between_nodes_with_value_init(&nodes[0], &nodes[1], 100000, 10001);
145 mine_transaction(&nodes[1], &tx);
146 nodes[0].node.handle_channel_ready(&nodes[1].node.get_our_node_id(), &get_event_msg!(nodes[1], MessageSendEvent::SendChannelReady, nodes[0].node.get_our_node_id()));
147 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
149 mine_transaction(&nodes[0], &tx);
150 let as_msg_events = nodes[0].node.get_and_clear_pending_msg_events();
151 assert_eq!(as_msg_events.len(), 2);
152 let as_channel_ready = if let MessageSendEvent::SendChannelReady { ref node_id, ref msg } = as_msg_events[0] {
153 assert_eq!(*node_id, nodes[1].node.get_our_node_id());
155 } else { panic!("Unexpected event"); };
156 if let MessageSendEvent::SendChannelUpdate { ref node_id, msg: _ } = as_msg_events[1] {
157 assert_eq!(*node_id, nodes[1].node.get_our_node_id());
158 } else { panic!("Unexpected event"); }
160 nodes[1].node.handle_channel_ready(&nodes[0].node.get_our_node_id(), &as_channel_ready);
161 expect_channel_ready_event(&nodes[0], &nodes[1].node.get_our_node_id());
162 expect_channel_ready_event(&nodes[1], &nodes[0].node.get_our_node_id());
163 let bs_msg_events = nodes[1].node.get_and_clear_pending_msg_events();
164 assert_eq!(bs_msg_events.len(), 1);
165 if let MessageSendEvent::SendChannelUpdate { ref node_id, msg: _ } = bs_msg_events[0] {
166 assert_eq!(*node_id, nodes[0].node.get_our_node_id());
167 } else { panic!("Unexpected event"); }
169 send_payment(&nodes[0], &[&nodes[1]], 100_000);
171 // After 6 confirmations, as required by the spec, we'll send announcement_signatures and
172 // broadcast the channel_announcement (but not before exactly 6 confirmations).
173 connect_blocks(&nodes[0], 4);
174 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
175 connect_blocks(&nodes[0], 1);
176 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()));
177 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
179 connect_blocks(&nodes[1], 5);
180 let bs_announce_events = nodes[1].node.get_and_clear_pending_msg_events();
181 assert_eq!(bs_announce_events.len(), 2);
182 let bs_announcement_sigs = if let MessageSendEvent::SendAnnouncementSignatures { ref node_id, ref msg } = bs_announce_events[0] {
183 assert_eq!(*node_id, nodes[0].node.get_our_node_id());
185 } else { panic!("Unexpected event"); };
186 let (bs_announcement, bs_update) = if let MessageSendEvent::BroadcastChannelAnnouncement { ref msg, ref update_msg } = bs_announce_events[1] {
187 (msg.clone(), update_msg.clone().unwrap())
188 } else { panic!("Unexpected event"); };
190 nodes[0].node.handle_announcement_signatures(&nodes[1].node.get_our_node_id(), &bs_announcement_sigs);
191 let as_announce_events = nodes[0].node.get_and_clear_pending_msg_events();
192 assert_eq!(as_announce_events.len(), 1);
193 let (announcement, as_update) = if let MessageSendEvent::BroadcastChannelAnnouncement { ref msg, ref update_msg } = as_announce_events[0] {
194 (msg.clone(), update_msg.clone().unwrap())
195 } else { panic!("Unexpected event"); };
196 assert_eq!(announcement, bs_announcement);
199 assert!(node.gossip_sync.handle_channel_announcement(&announcement).unwrap());
200 node.gossip_sync.handle_channel_update(&as_update).unwrap();
201 node.gossip_sync.handle_channel_update(&bs_update).unwrap();
205 fn test_1_conf_open() {
206 do_test_1_conf_open(ConnectStyle::BestBlockFirst);
207 do_test_1_conf_open(ConnectStyle::TransactionsFirst);
208 do_test_1_conf_open(ConnectStyle::FullBlockViaListen);
212 fn test_routed_scid_alias() {
213 // Trivially test sending a payment which is routed through an SCID alias.
214 let chanmon_cfgs = create_chanmon_cfgs(3);
215 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
216 let mut no_announce_cfg = test_default_channel_config();
217 no_announce_cfg.accept_forwards_to_priv_channels = true;
218 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, Some(no_announce_cfg), None]);
219 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
221 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 500_000_000).2;
222 let mut as_channel_ready = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 500_000_000).0;
224 let last_hop = nodes[2].node.list_usable_channels();
225 let hop_hints = vec![RouteHint(vec![RouteHintHop {
226 src_node_id: nodes[1].node.get_our_node_id(),
227 short_channel_id: last_hop[0].inbound_scid_alias.unwrap(),
229 base_msat: last_hop[0].counterparty.forwarding_info.as_ref().unwrap().fee_base_msat,
230 proportional_millionths: last_hop[0].counterparty.forwarding_info.as_ref().unwrap().fee_proportional_millionths,
232 cltv_expiry_delta: last_hop[0].counterparty.forwarding_info.as_ref().unwrap().cltv_expiry_delta,
233 htlc_maximum_msat: None,
234 htlc_minimum_msat: None,
236 let payment_params = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id(), 42)
237 .with_features(nodes[2].node.invoice_features())
238 .with_route_hints(hop_hints);
239 let (route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], payment_params, 100_000, 42);
240 assert_eq!(route.paths[0][1].short_channel_id, last_hop[0].inbound_scid_alias.unwrap());
241 nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret), PaymentId(payment_hash.0)).unwrap();
242 check_added_monitors!(nodes[0], 1);
244 pass_along_route(&nodes[0], &[&[&nodes[1], &nodes[2]]], 100_000, payment_hash, payment_secret);
245 claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], payment_preimage);
247 // Now test that if a peer sends us a second channel_ready after the channel is operational we
248 // will use the new alias.
249 as_channel_ready.short_channel_id_alias = Some(0xdeadbeef);
250 nodes[2].node.handle_channel_ready(&nodes[1].node.get_our_node_id(), &as_channel_ready);
251 // Note that we always respond to a channel_ready with a channel_update. Not a lot of reason
252 // to bother updating that code, so just drop the message here.
253 get_event_msg!(nodes[2], MessageSendEvent::SendChannelUpdate, nodes[1].node.get_our_node_id());
254 let updated_channel_info = nodes[2].node.list_usable_channels();
255 assert_eq!(updated_channel_info.len(), 1);
256 assert_eq!(updated_channel_info[0].inbound_scid_alias.unwrap(), 0xdeadbeef);
257 // Note that because we never send a duplicate channel_ready we can't send a payment through
258 // the 0xdeadbeef SCID alias.
262 fn test_scid_privacy_on_pub_channel() {
263 // Tests rejecting the scid_privacy feature for public channels and that we don't ever try to
265 let chanmon_cfgs = create_chanmon_cfgs(2);
266 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
267 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
268 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
270 let mut scid_privacy_cfg = test_default_channel_config();
271 scid_privacy_cfg.channel_handshake_config.announced_channel = true;
272 scid_privacy_cfg.channel_handshake_config.negotiate_scid_privacy = true;
273 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, Some(scid_privacy_cfg)).unwrap();
274 let mut open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
276 assert!(!open_channel.channel_type.as_ref().unwrap().supports_scid_privacy()); // we ignore `negotiate_scid_privacy` on pub channels
277 open_channel.channel_type.as_mut().unwrap().set_scid_privacy_required();
278 assert_eq!(open_channel.channel_flags & 1, 1); // The `announce_channel` bit is set.
280 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel);
281 let err = get_err_msg(&nodes[1], &nodes[0].node.get_our_node_id());
282 assert_eq!(err.data, "SCID Alias/Privacy Channel Type cannot be set on a public channel");
286 fn test_scid_privacy_negotiation() {
287 // Tests of the negotiation of SCID alias and falling back to non-SCID-alias if our
288 // counterparty doesn't support it.
289 let chanmon_cfgs = create_chanmon_cfgs(2);
290 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
291 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
292 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
294 let mut scid_privacy_cfg = test_default_channel_config();
295 scid_privacy_cfg.channel_handshake_config.announced_channel = false;
296 scid_privacy_cfg.channel_handshake_config.negotiate_scid_privacy = true;
297 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, Some(scid_privacy_cfg)).unwrap();
299 let init_open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
300 assert!(init_open_channel.channel_type.as_ref().unwrap().supports_scid_privacy());
301 assert!(nodes[0].node.list_channels()[0].channel_type.is_none()); // channel_type is none until counterparty accepts
303 // now simulate nodes[1] responding with an Error message, indicating it doesn't understand
305 nodes[0].node.handle_error(&nodes[1].node.get_our_node_id(), &msgs::ErrorMessage {
306 channel_id: init_open_channel.temporary_channel_id,
307 data: "Yo, no SCID aliases, no privacy here!".to_string()
309 assert!(nodes[0].node.list_channels()[0].channel_type.is_none()); // channel_type is none until counterparty accepts
311 let second_open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
312 assert!(!second_open_channel.channel_type.as_ref().unwrap().supports_scid_privacy());
313 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &second_open_channel);
314 nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id()));
316 let events = nodes[0].node.get_and_clear_pending_events();
317 assert_eq!(events.len(), 1);
319 Event::FundingGenerationReady { .. } => {},
320 _ => panic!("Unexpected event"),
323 assert!(!nodes[0].node.list_channels()[0].channel_type.as_ref().unwrap().supports_scid_privacy());
324 assert!(!nodes[1].node.list_channels()[0].channel_type.as_ref().unwrap().supports_scid_privacy());
328 fn test_inbound_scid_privacy() {
329 // Tests accepting channels with the scid_privacy feature and rejecting forwards using the
330 // channel's real SCID as required by the channel feature.
331 let chanmon_cfgs = create_chanmon_cfgs(3);
332 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
333 let mut accept_forward_cfg = test_default_channel_config();
334 accept_forward_cfg.accept_forwards_to_priv_channels = true;
335 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, Some(accept_forward_cfg), None]);
336 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
338 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
340 let mut no_announce_cfg = test_default_channel_config();
341 no_announce_cfg.channel_handshake_config.announced_channel = false;
342 no_announce_cfg.channel_handshake_config.negotiate_scid_privacy = true;
343 nodes[1].node.create_channel(nodes[2].node.get_our_node_id(), 100_000, 10_000, 42, Some(no_announce_cfg)).unwrap();
344 let mut open_channel = get_event_msg!(nodes[1], MessageSendEvent::SendOpenChannel, nodes[2].node.get_our_node_id());
346 assert!(open_channel.channel_type.as_ref().unwrap().requires_scid_privacy());
348 nodes[2].node.handle_open_channel(&nodes[1].node.get_our_node_id(), &open_channel);
349 let accept_channel = get_event_msg!(nodes[2], MessageSendEvent::SendAcceptChannel, nodes[1].node.get_our_node_id());
350 nodes[1].node.handle_accept_channel(&nodes[2].node.get_our_node_id(), &accept_channel);
352 let (temporary_channel_id, tx, _) = create_funding_transaction(&nodes[1], &nodes[2].node.get_our_node_id(), 100_000, 42);
353 nodes[1].node.funding_transaction_generated(&temporary_channel_id, &nodes[2].node.get_our_node_id(), tx.clone()).unwrap();
354 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()));
355 check_added_monitors!(nodes[2], 1);
357 let cs_funding_signed = get_event_msg!(nodes[2], MessageSendEvent::SendFundingSigned, nodes[1].node.get_our_node_id());
358 nodes[1].node.handle_funding_signed(&nodes[2].node.get_our_node_id(), &cs_funding_signed);
359 check_added_monitors!(nodes[1], 1);
361 let conf_height = core::cmp::max(nodes[1].best_block_info().1 + 1, nodes[2].best_block_info().1 + 1);
362 confirm_transaction_at(&nodes[1], &tx, conf_height);
363 connect_blocks(&nodes[1], CHAN_CONFIRM_DEPTH - 1);
364 confirm_transaction_at(&nodes[2], &tx, conf_height);
365 connect_blocks(&nodes[2], CHAN_CONFIRM_DEPTH - 1);
366 let bs_channel_ready = get_event_msg!(nodes[1], MessageSendEvent::SendChannelReady, nodes[2].node.get_our_node_id());
367 nodes[1].node.handle_channel_ready(&nodes[2].node.get_our_node_id(), &get_event_msg!(nodes[2], MessageSendEvent::SendChannelReady, nodes[1].node.get_our_node_id()));
368 expect_channel_ready_event(&nodes[1], &nodes[2].node.get_our_node_id());
369 let bs_update = get_event_msg!(nodes[1], MessageSendEvent::SendChannelUpdate, nodes[2].node.get_our_node_id());
370 nodes[2].node.handle_channel_ready(&nodes[1].node.get_our_node_id(), &bs_channel_ready);
371 expect_channel_ready_event(&nodes[2], &nodes[1].node.get_our_node_id());
372 let cs_update = get_event_msg!(nodes[2], MessageSendEvent::SendChannelUpdate, nodes[1].node.get_our_node_id());
374 nodes[1].node.handle_channel_update(&nodes[2].node.get_our_node_id(), &cs_update);
375 nodes[2].node.handle_channel_update(&nodes[1].node.get_our_node_id(), &bs_update);
377 // Now we can pay just fine using the SCID alias nodes[2] gave to nodes[1]...
379 let last_hop = nodes[2].node.list_usable_channels();
380 let mut hop_hints = vec![RouteHint(vec![RouteHintHop {
381 src_node_id: nodes[1].node.get_our_node_id(),
382 short_channel_id: last_hop[0].inbound_scid_alias.unwrap(),
384 base_msat: last_hop[0].counterparty.forwarding_info.as_ref().unwrap().fee_base_msat,
385 proportional_millionths: last_hop[0].counterparty.forwarding_info.as_ref().unwrap().fee_proportional_millionths,
387 cltv_expiry_delta: last_hop[0].counterparty.forwarding_info.as_ref().unwrap().cltv_expiry_delta,
388 htlc_maximum_msat: None,
389 htlc_minimum_msat: None,
391 let payment_params = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id(), 42)
392 .with_features(nodes[2].node.invoice_features())
393 .with_route_hints(hop_hints.clone());
394 let (route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], payment_params, 100_000, 42);
395 assert_eq!(route.paths[0][1].short_channel_id, last_hop[0].inbound_scid_alias.unwrap());
396 nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret), PaymentId(payment_hash.0)).unwrap();
397 check_added_monitors!(nodes[0], 1);
399 pass_along_route(&nodes[0], &[&[&nodes[1], &nodes[2]]], 100_000, payment_hash, payment_secret);
400 claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], payment_preimage);
402 // ... but if we try to pay using the real SCID, nodes[1] will just tell us they don't know
403 // what channel we're talking about.
404 hop_hints[0].0[0].short_channel_id = last_hop[0].short_channel_id.unwrap();
406 let payment_params_2 = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id(), 42)
407 .with_features(nodes[2].node.invoice_features())
408 .with_route_hints(hop_hints);
409 let (route_2, payment_hash_2, _, payment_secret_2) = get_route_and_payment_hash!(nodes[0], nodes[2], payment_params_2, 100_000, 42);
410 assert_eq!(route_2.paths[0][1].short_channel_id, last_hop[0].short_channel_id.unwrap());
411 nodes[0].node.send_payment(&route_2, payment_hash_2, &Some(payment_secret_2), PaymentId(payment_hash_2.0)).unwrap();
412 check_added_monitors!(nodes[0], 1);
414 let payment_event = SendEvent::from_node(&nodes[0]);
415 assert_eq!(nodes[1].node.get_our_node_id(), payment_event.node_id);
416 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
417 commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, true, true);
419 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);
421 let mut updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
422 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
423 commitment_signed_dance!(nodes[0], nodes[1], updates.commitment_signed, false);
425 expect_payment_failed_conditions(&nodes[0], payment_hash_2, false,
426 PaymentFailedConditions::new().blamed_scid(last_hop[0].short_channel_id.unwrap())
427 .blamed_chan_closed(true).expected_htlc_error_data(0x4000|10, &[0; 0]));
431 fn test_scid_alias_returned() {
432 // Tests that when we fail an HTLC (in this case due to attempting to forward more than the
433 // channel's available balance) we use the correct (in this case the aliased) SCID in the
434 // channel_update which is returned in the onion to the sender.
435 let chanmon_cfgs = create_chanmon_cfgs(3);
436 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
437 let mut accept_forward_cfg = test_default_channel_config();
438 accept_forward_cfg.accept_forwards_to_priv_channels = true;
439 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, Some(accept_forward_cfg), None]);
440 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
442 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 0);
443 let chan = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000, 0);
445 let last_hop = nodes[2].node.list_usable_channels();
446 let mut hop_hints = vec![RouteHint(vec![RouteHintHop {
447 src_node_id: nodes[1].node.get_our_node_id(),
448 short_channel_id: last_hop[0].inbound_scid_alias.unwrap(),
450 base_msat: last_hop[0].counterparty.forwarding_info.as_ref().unwrap().fee_base_msat,
451 proportional_millionths: last_hop[0].counterparty.forwarding_info.as_ref().unwrap().fee_proportional_millionths,
453 cltv_expiry_delta: last_hop[0].counterparty.forwarding_info.as_ref().unwrap().cltv_expiry_delta,
454 htlc_maximum_msat: None,
455 htlc_minimum_msat: None,
457 let payment_params = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id(), 42)
458 .with_features(nodes[2].node.invoice_features())
459 .with_route_hints(hop_hints);
460 let (mut route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], payment_params, 10_000, 42);
461 assert_eq!(route.paths[0][1].short_channel_id, nodes[2].node.list_usable_channels()[0].inbound_scid_alias.unwrap());
463 route.paths[0][1].fee_msat = 10_000_000; // Overshoot the last channel's value
465 // Route the HTLC through to the destination.
466 nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret), PaymentId(payment_hash.0)).unwrap();
467 check_added_monitors!(nodes[0], 1);
468 let as_updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
469 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &as_updates.update_add_htlcs[0]);
470 commitment_signed_dance!(nodes[1], nodes[0], &as_updates.commitment_signed, false, true);
472 expect_pending_htlcs_forwardable!(nodes[1]);
473 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], vec![HTLCDestination::NextHopChannel { node_id: Some(nodes[2].node.get_our_node_id()), channel_id: chan.0.channel_id }]);
474 check_added_monitors!(nodes[1], 1);
476 let bs_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
477 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &bs_updates.update_fail_htlcs[0]);
478 commitment_signed_dance!(nodes[0], nodes[1], bs_updates.commitment_signed, false, true);
480 // Build the expected channel update
481 let contents = msgs::UnsignedChannelUpdate {
482 chain_hash: genesis_block(Network::Testnet).header.block_hash(),
483 short_channel_id: last_hop[0].inbound_scid_alias.unwrap(),
486 cltv_expiry_delta: accept_forward_cfg.channel_config.cltv_expiry_delta,
487 htlc_minimum_msat: 1_000,
488 htlc_maximum_msat: 1_000_000, // Defaults to 10% of the channel value
489 fee_base_msat: last_hop[0].counterparty.forwarding_info.as_ref().unwrap().fee_base_msat,
490 fee_proportional_millionths: last_hop[0].counterparty.forwarding_info.as_ref().unwrap().fee_proportional_millionths,
491 excess_data: Vec::new(),
493 let signature = nodes[1].keys_manager.sign_gossip_message(msgs::UnsignedGossipMessage::ChannelUpdate(&contents)).unwrap();
494 let msg = msgs::ChannelUpdate { signature, contents };
496 let mut err_data = Vec::new();
497 err_data.extend_from_slice(&(msg.serialized_length() as u16 + 2).to_be_bytes());
498 err_data.extend_from_slice(&ChannelUpdate::TYPE.to_be_bytes());
499 err_data.extend_from_slice(&msg.encode());
501 expect_payment_failed_conditions(&nodes[0], payment_hash, false,
502 PaymentFailedConditions::new().blamed_scid(last_hop[0].inbound_scid_alias.unwrap())
503 .blamed_chan_closed(false).expected_htlc_error_data(0x1000|7, &err_data));
505 route.paths[0][1].fee_msat = 10_000; // Reset to the correct payment amount
506 route.paths[0][0].fee_msat = 0; // But set fee paid to the middle hop to 0
508 // Route the HTLC through to the destination.
509 nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret), PaymentId(payment_hash.0)).unwrap();
510 check_added_monitors!(nodes[0], 1);
511 let as_updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
512 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &as_updates.update_add_htlcs[0]);
513 commitment_signed_dance!(nodes[1], nodes[0], &as_updates.commitment_signed, false, true);
515 let bs_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
516 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &bs_updates.update_fail_htlcs[0]);
517 commitment_signed_dance!(nodes[0], nodes[1], bs_updates.commitment_signed, false, true);
519 let mut err_data = Vec::new();
520 err_data.extend_from_slice(&10_000u64.to_be_bytes());
521 err_data.extend_from_slice(&(msg.serialized_length() as u16 + 2).to_be_bytes());
522 err_data.extend_from_slice(&ChannelUpdate::TYPE.to_be_bytes());
523 err_data.extend_from_slice(&msg.encode());
524 expect_payment_failed_conditions(&nodes[0], payment_hash, false,
525 PaymentFailedConditions::new().blamed_scid(last_hop[0].inbound_scid_alias.unwrap())
526 .blamed_chan_closed(false).expected_htlc_error_data(0x1000|12, &err_data));
530 fn test_simple_0conf_channel() {
531 // If our peer tells us they will accept our channel with 0 confs, and we funded the channel,
532 // we should trust the funding won't be double-spent (assuming `trust_own_funding_0conf` is
534 // Further, if we `accept_inbound_channel_from_trusted_peer_0conf`, `channel_ready` messages
535 // should fly immediately and the channel should be available for use as soon as they are
538 let chanmon_cfgs = create_chanmon_cfgs(2);
539 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
540 let mut chan_config = test_default_channel_config();
541 chan_config.manually_accept_inbound_channels = true;
543 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(chan_config)]);
544 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
546 open_zero_conf_channel(&nodes[0], &nodes[1], None);
548 send_payment(&nodes[0], &[&nodes[1]], 100_000);
552 fn test_0conf_channel_with_async_monitor() {
553 // Test that we properly send out channel_ready in (both inbound- and outbound-) zero-conf
554 // channels if ChannelMonitor updates return a `TemporaryFailure` during the initial channel
557 let chanmon_cfgs = create_chanmon_cfgs(3);
558 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
559 let mut chan_config = test_default_channel_config();
560 chan_config.manually_accept_inbound_channels = true;
561 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, Some(chan_config), None]);
562 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
564 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 0);
566 chan_config.channel_handshake_config.announced_channel = false;
567 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, Some(chan_config)).unwrap();
568 let open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
570 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel);
571 let events = nodes[1].node.get_and_clear_pending_events();
572 assert_eq!(events.len(), 1);
574 Event::OpenChannelRequest { temporary_channel_id, .. } => {
575 nodes[1].node.accept_inbound_channel_from_trusted_peer_0conf(&temporary_channel_id, &nodes[0].node.get_our_node_id(), 0).unwrap();
577 _ => panic!("Unexpected event"),
580 let mut accept_channel = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
581 assert_eq!(accept_channel.minimum_depth, 0);
582 nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &accept_channel);
584 let (temporary_channel_id, tx, funding_output) = create_funding_transaction(&nodes[0], &nodes[1].node.get_our_node_id(), 100000, 42);
585 nodes[0].node.funding_transaction_generated(&temporary_channel_id, &nodes[1].node.get_our_node_id(), tx.clone()).unwrap();
586 let funding_created = get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, nodes[1].node.get_our_node_id());
588 chanmon_cfgs[1].persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
589 nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &funding_created);
590 check_added_monitors!(nodes[1], 1);
591 assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
593 let channel_id = funding_output.to_channel_id();
594 nodes[1].chain_monitor.complete_sole_pending_chan_update(&channel_id);
596 let bs_signed_locked = nodes[1].node.get_and_clear_pending_msg_events();
597 assert_eq!(bs_signed_locked.len(), 2);
598 chanmon_cfgs[0].persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
600 match &bs_signed_locked[0] {
601 MessageSendEvent::SendFundingSigned { node_id, msg } => {
602 assert_eq!(*node_id, nodes[0].node.get_our_node_id());
603 nodes[0].node.handle_funding_signed(&nodes[1].node.get_our_node_id(), &msg);
604 check_added_monitors!(nodes[0], 1);
606 _ => panic!("Unexpected event"),
608 match &bs_signed_locked[1] {
609 MessageSendEvent::SendChannelReady { node_id, msg } => {
610 assert_eq!(*node_id, nodes[0].node.get_our_node_id());
611 nodes[0].node.handle_channel_ready(&nodes[1].node.get_our_node_id(), &msg);
613 _ => panic!("Unexpected event"),
616 assert!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().is_empty());
618 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
619 nodes[0].chain_monitor.complete_sole_pending_chan_update(&channel_id);
620 let as_locked_update = nodes[0].node.get_and_clear_pending_msg_events();
622 // Note that the funding transaction is actually released when
623 // get_and_clear_pending_msg_events, above, checks for monitor events.
624 assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 1);
625 assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0)[0], tx);
627 match &as_locked_update[0] {
628 MessageSendEvent::SendChannelReady { node_id, msg } => {
629 assert_eq!(*node_id, nodes[1].node.get_our_node_id());
630 nodes[1].node.handle_channel_ready(&nodes[0].node.get_our_node_id(), &msg);
632 _ => panic!("Unexpected event"),
634 expect_channel_ready_event(&nodes[0], &nodes[1].node.get_our_node_id());
635 expect_channel_ready_event(&nodes[1], &nodes[0].node.get_our_node_id());
637 let bs_channel_update = get_event_msg!(nodes[1], MessageSendEvent::SendChannelUpdate, nodes[0].node.get_our_node_id());
639 let as_channel_update = match &as_locked_update[1] {
640 MessageSendEvent::SendChannelUpdate { node_id, msg } => {
641 assert_eq!(*node_id, nodes[1].node.get_our_node_id());
644 _ => panic!("Unexpected event"),
647 chanmon_cfgs[0].persister.set_update_ret(ChannelMonitorUpdateStatus::Completed);
648 chanmon_cfgs[1].persister.set_update_ret(ChannelMonitorUpdateStatus::Completed);
650 nodes[0].node.handle_channel_update(&nodes[1].node.get_our_node_id(), &bs_channel_update);
651 nodes[1].node.handle_channel_update(&nodes[0].node.get_our_node_id(), &as_channel_update);
653 assert_eq!(nodes[0].node.list_usable_channels().len(), 1);
654 assert_eq!(nodes[1].node.list_usable_channels().len(), 2);
656 send_payment(&nodes[0], &[&nodes[1]], 100_000);
658 // Now that we have useful channels, try sending a payment where the we hit a temporary monitor
659 // failure before we've ever confirmed the funding transaction. This previously caused a panic.
660 let (route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], 1_000_000);
662 nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret), PaymentId(payment_hash.0)).unwrap();
663 check_added_monitors!(nodes[0], 1);
665 let as_send = SendEvent::from_node(&nodes[0]);
666 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &as_send.msgs[0]);
667 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_send.commitment_msg);
668 check_added_monitors!(nodes[1], 1);
670 let (bs_raa, bs_commitment_signed) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
671 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_raa);
672 check_added_monitors!(nodes[0], 1);
674 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_commitment_signed);
675 check_added_monitors!(nodes[0], 1);
677 chanmon_cfgs[1].persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
678 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id()));
679 check_added_monitors!(nodes[1], 1);
680 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
682 chanmon_cfgs[1].persister.set_update_ret(ChannelMonitorUpdateStatus::Completed);
683 let (outpoint, _, latest_update) = nodes[1].chain_monitor.latest_monitor_update_id.lock().unwrap().get(&bs_raa.channel_id).unwrap().clone();
684 nodes[1].chain_monitor.chain_monitor.channel_monitor_updated(outpoint, latest_update).unwrap();
685 check_added_monitors!(nodes[1], 0);
686 expect_pending_htlcs_forwardable!(nodes[1]);
687 check_added_monitors!(nodes[1], 1);
689 let bs_send = SendEvent::from_node(&nodes[1]);
690 nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &bs_send.msgs[0]);
691 commitment_signed_dance!(nodes[2], nodes[1], bs_send.commitment_msg, false);
692 expect_pending_htlcs_forwardable!(nodes[2]);
693 expect_payment_claimable!(nodes[2], payment_hash, payment_secret, 1_000_000);
694 claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], payment_preimage);
696 confirm_transaction(&nodes[0], &tx);
697 confirm_transaction(&nodes[1], &tx);
699 send_payment(&nodes[0], &[&nodes[1]], 100_000);
703 fn test_0conf_close_no_early_chan_update() {
704 // Tests that even with a public channel 0conf channel, we don't generate a channel_update on
706 let chanmon_cfgs = create_chanmon_cfgs(2);
707 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
708 let mut chan_config = test_default_channel_config();
709 chan_config.manually_accept_inbound_channels = true;
711 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(chan_config)]);
712 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
714 // This is the default but we force it on anyway
715 chan_config.channel_handshake_config.announced_channel = true;
716 open_zero_conf_channel(&nodes[0], &nodes[1], Some(chan_config));
718 // We can use the channel immediately, but won't generate a channel_update until we get confs
719 send_payment(&nodes[0], &[&nodes[1]], 100_000);
721 nodes[0].node.force_close_all_channels_broadcasting_latest_txn();
722 check_added_monitors!(nodes[0], 1);
723 check_closed_event!(&nodes[0], 1, ClosureReason::HolderForceClosed);
724 let _ = get_err_msg(&nodes[0], &nodes[1].node.get_our_node_id());
728 fn test_public_0conf_channel() {
729 // Tests that we will announce a public channel (after confirmation) even if its 0conf.
730 let chanmon_cfgs = create_chanmon_cfgs(2);
731 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
732 let mut chan_config = test_default_channel_config();
733 chan_config.manually_accept_inbound_channels = true;
735 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(chan_config)]);
736 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
738 // This is the default but we force it on anyway
739 chan_config.channel_handshake_config.announced_channel = true;
740 let (tx, ..) = open_zero_conf_channel(&nodes[0], &nodes[1], Some(chan_config));
742 // We can use the channel immediately, but we can't announce it until we get 6+ confirmations
743 send_payment(&nodes[0], &[&nodes[1]], 100_000);
745 let scid = confirm_transaction(&nodes[0], &tx);
746 let as_announcement_sigs = get_event_msg!(nodes[0], MessageSendEvent::SendAnnouncementSignatures, nodes[1].node.get_our_node_id());
747 assert_eq!(confirm_transaction(&nodes[1], &tx), scid);
748 let bs_announcement_sigs = get_event_msg!(nodes[1], MessageSendEvent::SendAnnouncementSignatures, nodes[0].node.get_our_node_id());
750 nodes[1].node.handle_announcement_signatures(&nodes[0].node.get_our_node_id(), &as_announcement_sigs);
751 nodes[0].node.handle_announcement_signatures(&nodes[1].node.get_our_node_id(), &bs_announcement_sigs);
753 let bs_announcement = nodes[1].node.get_and_clear_pending_msg_events();
754 assert_eq!(bs_announcement.len(), 1);
757 match bs_announcement[0] {
758 MessageSendEvent::BroadcastChannelAnnouncement { ref msg, ref update_msg } => {
759 announcement = msg.clone();
760 bs_update = update_msg.clone().unwrap();
762 _ => panic!("Unexpected event"),
765 let as_announcement = nodes[0].node.get_and_clear_pending_msg_events();
766 assert_eq!(as_announcement.len(), 1);
767 match as_announcement[0] {
768 MessageSendEvent::BroadcastChannelAnnouncement { ref msg, ref update_msg } => {
769 assert!(announcement == *msg);
770 let update_msg = update_msg.as_ref().unwrap();
771 assert_eq!(update_msg.contents.short_channel_id, scid);
772 assert_eq!(update_msg.contents.short_channel_id, announcement.contents.short_channel_id);
773 assert_eq!(update_msg.contents.short_channel_id, bs_update.contents.short_channel_id);
775 _ => panic!("Unexpected event"),
780 fn test_0conf_channel_reorg() {
781 // If we accept a 0conf channel, which is then confirmed, but then changes SCID in a reorg, we
782 // have to make sure we handle this correctly (or, currently, just force-close the channel).
784 let chanmon_cfgs = create_chanmon_cfgs(2);
785 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
786 let mut chan_config = test_default_channel_config();
787 chan_config.manually_accept_inbound_channels = true;
789 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(chan_config)]);
790 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
792 // This is the default but we force it on anyway
793 chan_config.channel_handshake_config.announced_channel = true;
794 let (tx, ..) = open_zero_conf_channel(&nodes[0], &nodes[1], Some(chan_config));
796 // We can use the channel immediately, but we can't announce it until we get 6+ confirmations
797 send_payment(&nodes[0], &[&nodes[1]], 100_000);
799 mine_transaction(&nodes[0], &tx);
800 mine_transaction(&nodes[1], &tx);
802 // Send a payment using the channel's real SCID, which will be public in a few blocks once we
803 // can generate a channel_announcement.
804 let real_scid = nodes[0].node.list_usable_channels()[0].short_channel_id.unwrap();
805 assert_eq!(nodes[1].node.list_usable_channels()[0].short_channel_id.unwrap(), real_scid);
807 let (mut route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 10_000);
808 assert_eq!(route.paths[0][0].short_channel_id, real_scid);
809 send_along_route_with_secret(&nodes[0], route, &[&[&nodes[1]]], 10_000, payment_hash, payment_secret);
810 claim_payment(&nodes[0], &[&nodes[1]], payment_preimage);
812 disconnect_blocks(&nodes[0], 1);
813 disconnect_blocks(&nodes[1], 1);
815 // At this point the channel no longer has an SCID again. In the future we should likely
816 // support simply un-setting the SCID and waiting until the channel gets re-confirmed, but for
817 // now we force-close the channel here.
818 check_closed_event!(&nodes[0], 1, ClosureReason::ProcessingError {
819 err: "Funding transaction was un-confirmed. Locked at 0 confs, now have 0 confs.".to_owned()
821 check_closed_broadcast!(nodes[0], true);
822 check_closed_event!(&nodes[1], 1, ClosureReason::ProcessingError {
823 err: "Funding transaction was un-confirmed. Locked at 0 confs, now have 0 confs.".to_owned()
825 check_closed_broadcast!(nodes[1], true);
829 fn test_zero_conf_accept_reject() {
830 let mut channel_type_features = ChannelTypeFeatures::only_static_remote_key();
831 channel_type_features.set_zero_conf_required();
833 // 1. Check we reject zero conf channels by default
834 let chanmon_cfgs = create_chanmon_cfgs(2);
835 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
836 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
837 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
839 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None).unwrap();
840 let mut open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
842 open_channel_msg.channel_type = Some(channel_type_features.clone());
844 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel_msg);
846 let msg_events = nodes[1].node.get_and_clear_pending_msg_events();
847 match msg_events[0] {
848 MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg, .. }, .. } => {
849 assert_eq!(msg.data, "No zero confirmation channels accepted".to_owned());
854 // 2. Check we can manually accept zero conf channels via the right method
855 let mut manually_accept_conf = UserConfig::default();
856 manually_accept_conf.manually_accept_inbound_channels = true;
858 let chanmon_cfgs = create_chanmon_cfgs(2);
859 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
860 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs,
861 &[None, Some(manually_accept_conf.clone())]);
862 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
864 // 2.1 First try the non-0conf method to manually accept
865 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42,
866 Some(manually_accept_conf)).unwrap();
867 let mut open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel,
868 nodes[1].node.get_our_node_id());
870 open_channel_msg.channel_type = Some(channel_type_features.clone());
872 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel_msg);
874 // Assert that `nodes[1]` has no `MessageSendEvent::SendAcceptChannel` in the `msg_events`.
875 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
877 let events = nodes[1].node.get_and_clear_pending_events();
880 Event::OpenChannelRequest { temporary_channel_id, .. } => {
881 // Assert we fail to accept via the non-0conf method
882 assert!(nodes[1].node.accept_inbound_channel(&temporary_channel_id,
883 &nodes[0].node.get_our_node_id(), 0).is_err());
888 let msg_events = nodes[1].node.get_and_clear_pending_msg_events();
889 match msg_events[0] {
890 MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg, .. }, .. } => {
891 assert_eq!(msg.data, "No zero confirmation channels accepted".to_owned());
896 // 2.2 Try again with the 0conf method to manually accept
897 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42,
898 Some(manually_accept_conf)).unwrap();
899 let mut open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel,
900 nodes[1].node.get_our_node_id());
902 open_channel_msg.channel_type = Some(channel_type_features);
904 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel_msg);
906 let events = nodes[1].node.get_and_clear_pending_events();
909 Event::OpenChannelRequest { temporary_channel_id, .. } => {
910 // Assert we can accept via the 0conf method
911 assert!(nodes[1].node.accept_inbound_channel_from_trusted_peer_0conf(
912 &temporary_channel_id, &nodes[0].node.get_our_node_id(), 0).is_ok());
917 // Check we would send accept
918 let msg_events = nodes[1].node.get_and_clear_pending_msg_events();
919 match msg_events[0] {
920 MessageSendEvent::SendAcceptChannel { .. } => {},
926 fn test_connect_before_funding() {
927 // Tests for a particularly dumb explicit panic that existed prior to 0.0.111 for 0conf
928 // channels. If we received a block while awaiting funding for 0-conf channels we'd hit an
929 // explicit panic when deciding if we should broadcast our channel_ready message.
930 let chanmon_cfgs = create_chanmon_cfgs(2);
931 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
933 let mut manually_accept_conf = test_default_channel_config();
934 manually_accept_conf.manually_accept_inbound_channels = true;
936 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(manually_accept_conf)]);
937 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
939 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100_000, 10_001, 42, None).unwrap();
940 let open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
942 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel);
943 let events = nodes[1].node.get_and_clear_pending_events();
944 assert_eq!(events.len(), 1);
946 Event::OpenChannelRequest { temporary_channel_id, .. } => {
947 nodes[1].node.accept_inbound_channel_from_trusted_peer_0conf(&temporary_channel_id, &nodes[0].node.get_our_node_id(), 0).unwrap();
949 _ => panic!("Unexpected event"),
952 let mut accept_channel = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
953 assert_eq!(accept_channel.minimum_depth, 0);
954 nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &accept_channel);
956 let events = nodes[0].node.get_and_clear_pending_events();
957 assert_eq!(events.len(), 1);
959 Event::FundingGenerationReady { .. } => {},
960 _ => panic!("Unexpected event"),
963 connect_blocks(&nodes[0], 1);
964 connect_blocks(&nodes[1], 1);