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 of our shutdown and closing_signed negotiation logic.
12 use crate::chain::keysinterface::{EntropySource, SignerProvider};
13 use crate::chain::transaction::OutPoint;
14 use crate::events::{Event, MessageSendEvent, MessageSendEventsProvider, ClosureReason};
15 use crate::ln::channelmanager::{self, PaymentSendFailure, PaymentId, RecipientOnionFields};
16 use crate::routing::router::{PaymentParameters, get_route};
18 use crate::ln::msgs::{ChannelMessageHandler, ErrorAction};
19 use crate::ln::script::ShutdownScript;
20 use crate::util::test_utils;
21 use crate::util::test_utils::OnGetShutdownScriptpubkey;
22 use crate::util::errors::APIError;
23 use crate::util::config::UserConfig;
24 use crate::util::string::UntrustedString;
26 use bitcoin::blockdata::script::Builder;
27 use bitcoin::blockdata::opcodes;
28 use bitcoin::network::constants::Network;
29 use bitcoin::util::address::WitnessVersion;
33 use core::default::Default;
35 use crate::ln::functional_test_utils::*;
36 use crate::ln::msgs::OptionalField::Present;
39 fn pre_funding_lock_shutdown_test() {
40 // Test sending a shutdown prior to channel_ready after funding generation
41 let chanmon_cfgs = create_chanmon_cfgs(2);
42 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
43 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
44 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
45 let tx = create_chan_between_nodes_with_value_init(&nodes[0], &nodes[1], 8000000, 0);
46 mine_transaction(&nodes[0], &tx);
47 mine_transaction(&nodes[1], &tx);
49 nodes[0].node.close_channel(&OutPoint { txid: tx.txid(), index: 0 }.to_channel_id(), &nodes[1].node.get_our_node_id()).unwrap();
50 let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
51 nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_shutdown);
52 let node_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
53 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_shutdown);
55 let node_0_closing_signed = get_event_msg!(nodes[0], MessageSendEvent::SendClosingSigned, nodes[1].node.get_our_node_id());
56 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed);
57 let node_1_closing_signed = get_event_msg!(nodes[1], MessageSendEvent::SendClosingSigned, nodes[0].node.get_our_node_id());
58 nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed);
59 let (_, node_0_2nd_closing_signed) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
60 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_2nd_closing_signed.unwrap());
61 let (_, node_1_none) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
62 assert!(node_1_none.is_none());
64 assert!(nodes[0].node.list_channels().is_empty());
65 assert!(nodes[1].node.list_channels().is_empty());
66 check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure);
67 check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure);
71 fn updates_shutdown_wait() {
72 // Test sending a shutdown with outstanding updates pending
73 let chanmon_cfgs = create_chanmon_cfgs(3);
74 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
75 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
76 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
77 let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
78 let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
79 let logger = test_utils::TestLogger::new();
80 let scorer = test_utils::TestScorer::new();
81 let keys_manager = test_utils::TestKeysInterface::new(&[0u8; 32], Network::Testnet);
82 let random_seed_bytes = keys_manager.get_secure_random_bytes();
84 let (payment_preimage_0, payment_hash_0, _) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 100_000);
86 nodes[0].node.close_channel(&chan_1.2, &nodes[1].node.get_our_node_id()).unwrap();
87 let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
88 nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_shutdown);
89 let node_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
90 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_shutdown);
92 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
93 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
95 let (_, payment_hash, payment_secret) = get_payment_preimage_hash!(nodes[0]);
97 let payment_params_1 = PaymentParameters::from_node_id(nodes[1].node.get_our_node_id(), TEST_FINAL_CLTV).with_features(nodes[1].node.invoice_features());
98 let route_1 = get_route(&nodes[0].node.get_our_node_id(), &payment_params_1, &nodes[0].network_graph.read_only(), None, 100000, TEST_FINAL_CLTV, &logger, &scorer, &random_seed_bytes).unwrap();
99 let payment_params_2 = PaymentParameters::from_node_id(nodes[0].node.get_our_node_id(), TEST_FINAL_CLTV).with_features(nodes[0].node.invoice_features());
100 let route_2 = get_route(&nodes[1].node.get_our_node_id(), &payment_params_2, &nodes[1].network_graph.read_only(), None, 100000, TEST_FINAL_CLTV, &logger, &scorer, &random_seed_bytes).unwrap();
101 unwrap_send_err!(nodes[0].node.send_payment_with_route(&route_1, payment_hash,
102 RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)
103 ), true, APIError::ChannelUnavailable {..}, {});
104 unwrap_send_err!(nodes[1].node.send_payment_with_route(&route_2, payment_hash,
105 RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)
106 ), true, APIError::ChannelUnavailable {..}, {});
108 nodes[2].node.claim_funds(payment_preimage_0);
109 check_added_monitors!(nodes[2], 1);
110 expect_payment_claimed!(nodes[2], payment_hash_0, 100_000);
112 let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
113 assert!(updates.update_add_htlcs.is_empty());
114 assert!(updates.update_fail_htlcs.is_empty());
115 assert!(updates.update_fail_malformed_htlcs.is_empty());
116 assert!(updates.update_fee.is_none());
117 assert_eq!(updates.update_fulfill_htlcs.len(), 1);
118 nodes[1].node.handle_update_fulfill_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fulfill_htlcs[0]);
119 expect_payment_forwarded!(nodes[1], nodes[0], nodes[2], Some(1000), false, false);
120 check_added_monitors!(nodes[1], 1);
121 let updates_2 = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
122 commitment_signed_dance!(nodes[1], nodes[2], updates.commitment_signed, false);
124 assert!(updates_2.update_add_htlcs.is_empty());
125 assert!(updates_2.update_fail_htlcs.is_empty());
126 assert!(updates_2.update_fail_malformed_htlcs.is_empty());
127 assert!(updates_2.update_fee.is_none());
128 assert_eq!(updates_2.update_fulfill_htlcs.len(), 1);
129 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &updates_2.update_fulfill_htlcs[0]);
130 commitment_signed_dance!(nodes[0], nodes[1], updates_2.commitment_signed, false, true);
131 expect_payment_sent!(nodes[0], payment_preimage_0);
133 let node_0_closing_signed = get_event_msg!(nodes[0], MessageSendEvent::SendClosingSigned, nodes[1].node.get_our_node_id());
134 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed);
135 let node_1_closing_signed = get_event_msg!(nodes[1], MessageSendEvent::SendClosingSigned, nodes[0].node.get_our_node_id());
136 nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed);
137 let (_, node_0_2nd_closing_signed) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
138 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_2nd_closing_signed.unwrap());
139 let (_, node_1_none) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
140 assert!(node_1_none.is_none());
141 check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure);
142 check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure);
144 assert!(nodes[0].node.list_channels().is_empty());
146 assert_eq!(nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 1);
147 nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
148 close_channel(&nodes[1], &nodes[2], &chan_2.2, chan_2.3, true);
149 assert!(nodes[1].node.list_channels().is_empty());
150 assert!(nodes[2].node.list_channels().is_empty());
151 check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure);
152 check_closed_event!(nodes[2], 1, ClosureReason::CooperativeClosure);
156 fn htlc_fail_async_shutdown() {
157 // Test HTLCs fail if shutdown starts even if messages are delivered out-of-order
158 let chanmon_cfgs = create_chanmon_cfgs(3);
159 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
160 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
161 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
162 let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
163 let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
165 let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], 100000);
166 nodes[0].node.send_payment_with_route(&route, our_payment_hash,
167 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
168 check_added_monitors!(nodes[0], 1);
169 let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
170 assert_eq!(updates.update_add_htlcs.len(), 1);
171 assert!(updates.update_fulfill_htlcs.is_empty());
172 assert!(updates.update_fail_htlcs.is_empty());
173 assert!(updates.update_fail_malformed_htlcs.is_empty());
174 assert!(updates.update_fee.is_none());
176 nodes[1].node.close_channel(&chan_1.2, &nodes[0].node.get_our_node_id()).unwrap();
177 let node_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
178 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_shutdown);
179 let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
181 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
182 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &updates.commitment_signed);
183 check_added_monitors!(nodes[1], 1);
184 nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_shutdown);
185 commitment_signed_dance!(nodes[1], nodes[0], (), false, true, false);
187 let updates_2 = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
188 assert!(updates_2.update_add_htlcs.is_empty());
189 assert!(updates_2.update_fulfill_htlcs.is_empty());
190 assert_eq!(updates_2.update_fail_htlcs.len(), 1);
191 assert!(updates_2.update_fail_malformed_htlcs.is_empty());
192 assert!(updates_2.update_fee.is_none());
194 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &updates_2.update_fail_htlcs[0]);
195 commitment_signed_dance!(nodes[0], nodes[1], updates_2.commitment_signed, false, true);
197 expect_payment_failed_with_update!(nodes[0], our_payment_hash, false, chan_2.0.contents.short_channel_id, true);
199 let msg_events = nodes[0].node.get_and_clear_pending_msg_events();
200 assert_eq!(msg_events.len(), 1);
201 let node_0_closing_signed = match msg_events[0] {
202 MessageSendEvent::SendClosingSigned { ref node_id, ref msg } => {
203 assert_eq!(*node_id, nodes[1].node.get_our_node_id());
206 _ => panic!("Unexpected event"),
209 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
210 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed);
211 let node_1_closing_signed = get_event_msg!(nodes[1], MessageSendEvent::SendClosingSigned, nodes[0].node.get_our_node_id());
212 nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed);
213 let (_, node_0_2nd_closing_signed) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
214 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_2nd_closing_signed.unwrap());
215 let (_, node_1_none) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
216 assert!(node_1_none.is_none());
218 assert!(nodes[0].node.list_channels().is_empty());
220 assert_eq!(nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 1);
221 nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
222 close_channel(&nodes[1], &nodes[2], &chan_2.2, chan_2.3, true);
223 assert!(nodes[1].node.list_channels().is_empty());
224 assert!(nodes[2].node.list_channels().is_empty());
225 check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure);
226 check_closed_event!(nodes[1], 2, ClosureReason::CooperativeClosure);
227 check_closed_event!(nodes[2], 1, ClosureReason::CooperativeClosure);
230 fn do_test_shutdown_rebroadcast(recv_count: u8) {
231 // Test that shutdown/closing_signed is re-sent on reconnect with a variable number of
232 // messages delivered prior to disconnect
233 let chanmon_cfgs = create_chanmon_cfgs(3);
234 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
235 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
236 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
237 let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
238 let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
240 let (payment_preimage, payment_hash, _) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 100_000);
242 nodes[1].node.close_channel(&chan_1.2, &nodes[0].node.get_our_node_id()).unwrap();
243 let node_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
245 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_shutdown);
246 let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
248 nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_shutdown);
252 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
253 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
255 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();
256 let node_0_reestablish = get_chan_reestablish_msgs!(nodes[0], nodes[1]).pop().unwrap();
257 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();
258 let node_1_reestablish = get_chan_reestablish_msgs!(nodes[1], nodes[0]).pop().unwrap();
260 nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &node_0_reestablish);
261 let node_1_2nd_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
262 assert!(node_1_shutdown == node_1_2nd_shutdown);
264 nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &node_1_reestablish);
265 let node_0_2nd_shutdown = if recv_count > 0 {
266 let node_0_2nd_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
267 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_2nd_shutdown);
270 let node_0_chan_update = get_event_msg!(nodes[0], MessageSendEvent::SendChannelUpdate, nodes[1].node.get_our_node_id());
271 assert_eq!(node_0_chan_update.contents.flags & 2, 0); // "disabled" flag must not be set as we just reconnected.
272 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_2nd_shutdown);
273 get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id())
275 nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_2nd_shutdown);
277 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
278 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
280 nodes[2].node.claim_funds(payment_preimage);
281 check_added_monitors!(nodes[2], 1);
282 expect_payment_claimed!(nodes[2], payment_hash, 100_000);
284 let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
285 assert!(updates.update_add_htlcs.is_empty());
286 assert!(updates.update_fail_htlcs.is_empty());
287 assert!(updates.update_fail_malformed_htlcs.is_empty());
288 assert!(updates.update_fee.is_none());
289 assert_eq!(updates.update_fulfill_htlcs.len(), 1);
290 nodes[1].node.handle_update_fulfill_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fulfill_htlcs[0]);
291 expect_payment_forwarded!(nodes[1], nodes[0], nodes[2], Some(1000), false, false);
292 check_added_monitors!(nodes[1], 1);
293 let updates_2 = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
294 commitment_signed_dance!(nodes[1], nodes[2], updates.commitment_signed, false);
296 assert!(updates_2.update_add_htlcs.is_empty());
297 assert!(updates_2.update_fail_htlcs.is_empty());
298 assert!(updates_2.update_fail_malformed_htlcs.is_empty());
299 assert!(updates_2.update_fee.is_none());
300 assert_eq!(updates_2.update_fulfill_htlcs.len(), 1);
301 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &updates_2.update_fulfill_htlcs[0]);
302 commitment_signed_dance!(nodes[0], nodes[1], updates_2.commitment_signed, false, true);
303 expect_payment_sent!(nodes[0], payment_preimage);
305 let node_0_closing_signed = get_event_msg!(nodes[0], MessageSendEvent::SendClosingSigned, nodes[1].node.get_our_node_id());
307 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed);
308 let node_1_closing_signed = get_event_msg!(nodes[1], MessageSendEvent::SendClosingSigned, nodes[0].node.get_our_node_id());
309 nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed);
310 let (_, node_0_2nd_closing_signed) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
311 assert!(node_0_2nd_closing_signed.is_some());
314 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
315 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
317 nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: nodes[0].node.init_features(), remote_network_address: None }, true).unwrap();
318 let node_1_2nd_reestablish = get_chan_reestablish_msgs!(nodes[1], nodes[0]).pop().unwrap();
319 nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: nodes[1].node.init_features(), remote_network_address: None }, false).unwrap();
321 // If all closing_signeds weren't delivered we can just resume where we left off...
322 let node_0_2nd_reestablish = get_chan_reestablish_msgs!(nodes[0], nodes[1]).pop().unwrap();
324 nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &node_1_2nd_reestablish);
325 let node_0_msgs = nodes[0].node.get_and_clear_pending_msg_events();
326 assert_eq!(node_0_msgs.len(), 2);
327 let node_0_2nd_closing_signed = match node_0_msgs[1] {
328 MessageSendEvent::SendClosingSigned { ref msg, .. } => {
329 assert_eq!(node_0_closing_signed, *msg);
335 let node_0_3rd_shutdown = match node_0_msgs[0] {
336 MessageSendEvent::SendShutdown { ref msg, .. } => {
337 assert_eq!(node_0_2nd_shutdown, *msg);
342 assert!(node_0_2nd_shutdown == node_0_3rd_shutdown);
344 nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &node_0_2nd_reestablish);
345 let node_1_3rd_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
346 assert!(node_1_3rd_shutdown == node_1_2nd_shutdown);
348 nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_3rd_shutdown);
349 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
351 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_3rd_shutdown);
353 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_2nd_closing_signed);
354 let node_1_closing_signed = get_event_msg!(nodes[1], MessageSendEvent::SendClosingSigned, nodes[0].node.get_our_node_id());
355 nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed);
356 let (_, node_0_2nd_closing_signed) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
357 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_2nd_closing_signed.unwrap());
358 let (_, node_1_none) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
359 assert!(node_1_none.is_none());
360 check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure);
362 // If one node, however, received + responded with an identical closing_signed we end
363 // up erroring and node[0] will try to broadcast its own latest commitment transaction.
364 // There isn't really anything better we can do simply, but in the future we might
365 // explore storing a set of recently-closed channels that got disconnected during
366 // closing_signed and avoiding broadcasting local commitment txn for some timeout to
367 // give our counterparty enough time to (potentially) broadcast a cooperative closing
369 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
371 nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &node_1_2nd_reestablish);
372 let msg_events = nodes[0].node.get_and_clear_pending_msg_events();
373 assert_eq!(msg_events.len(), 1);
374 if let MessageSendEvent::HandleError { ref action, .. } = msg_events[0] {
376 &ErrorAction::SendErrorMessage { ref msg } => {
377 nodes[1].node.handle_error(&nodes[0].node.get_our_node_id(), &msg);
378 assert_eq!(msg.channel_id, chan_1.2);
380 _ => panic!("Unexpected event!"),
382 } else { panic!("Needed SendErrorMessage close"); }
384 // get_closing_signed_broadcast usually eats the BroadcastChannelUpdate for us and
385 // checks it, but in this case nodes[1] didn't ever get a chance to receive a
386 // closing_signed so we do it ourselves
387 check_closed_broadcast!(nodes[1], false);
388 check_added_monitors!(nodes[1], 1);
389 check_closed_event!(nodes[1], 1, ClosureReason::CounterpartyForceClosed { peer_msg: UntrustedString(format!("Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}", &nodes[1].node.get_our_node_id())) });
392 assert!(nodes[0].node.list_channels().is_empty());
394 assert_eq!(nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 1);
395 nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
396 close_channel(&nodes[1], &nodes[2], &chan_2.2, chan_2.3, true);
397 assert!(nodes[1].node.list_channels().is_empty());
398 assert!(nodes[2].node.list_channels().is_empty());
399 check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure);
400 check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure);
401 check_closed_event!(nodes[2], 1, ClosureReason::CooperativeClosure);
405 fn test_shutdown_rebroadcast() {
406 do_test_shutdown_rebroadcast(0);
407 do_test_shutdown_rebroadcast(1);
408 do_test_shutdown_rebroadcast(2);
412 fn test_upfront_shutdown_script() {
413 // BOLT 2 : Option upfront shutdown script, if peer commit its closing_script at channel opening
414 // enforce it at shutdown message
416 let mut config = UserConfig::default();
417 config.channel_handshake_config.announced_channel = true;
418 config.channel_handshake_limits.force_announced_channel_preference = false;
419 config.channel_handshake_config.commit_upfront_shutdown_pubkey = false;
420 let user_cfgs = [None, Some(config), None];
421 let chanmon_cfgs = create_chanmon_cfgs(3);
422 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
423 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &user_cfgs);
424 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
426 // We test that in case of peer committing upfront to a script, if it changes at closing, we refuse to sign
427 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 2, 1000000, 1000000);
428 nodes[0].node.close_channel(&OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id(), &nodes[2].node.get_our_node_id()).unwrap();
429 let node_0_orig_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[2].node.get_our_node_id());
430 let mut node_0_shutdown = node_0_orig_shutdown.clone();
431 node_0_shutdown.scriptpubkey = Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script().to_p2sh();
432 // Test we enforce upfront_scriptpbukey if by providing a different one at closing that we warn
433 // the peer and ignore the message.
434 nodes[2].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_shutdown);
435 assert!(regex::Regex::new(r"Got shutdown request with a scriptpubkey \([A-Fa-f0-9]+\) which did not match their previous scriptpubkey.")
436 .unwrap().is_match(&check_warn_msg!(nodes[2], nodes[0].node.get_our_node_id(), chan.2)));
437 // This allows nodes[2] to retry the shutdown message, which should get a response:
438 nodes[2].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_orig_shutdown);
439 get_event_msg!(nodes[2], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
441 // We test that in case of peer committing upfront to a script, if it doesn't change at closing, we sign
442 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 2, 1000000, 1000000);
443 nodes[0].node.close_channel(&OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id(), &nodes[2].node.get_our_node_id()).unwrap();
444 let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[2].node.get_our_node_id());
445 // We test that in case of peer committing upfront to a script, if it oesn't change at closing, we sign
446 nodes[2].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_shutdown);
447 let events = nodes[2].node.get_and_clear_pending_msg_events();
448 assert_eq!(events.len(), 1);
450 MessageSendEvent::SendShutdown { node_id, .. } => { assert_eq!(node_id, nodes[0].node.get_our_node_id()) }
451 _ => panic!("Unexpected event"),
454 // We test that if case of peer non-signaling we don't enforce committed script at channel opening
455 *nodes[0].override_init_features.borrow_mut() = Some(nodes[0].node.init_features().clear_upfront_shutdown_script());
456 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 1000000);
457 nodes[0].node.close_channel(&OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id(), &nodes[1].node.get_our_node_id()).unwrap();
458 let node_1_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
459 nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_1_shutdown);
460 check_added_monitors!(nodes[1], 1);
461 let events = nodes[1].node.get_and_clear_pending_msg_events();
462 assert_eq!(events.len(), 1);
464 MessageSendEvent::SendShutdown { node_id, .. } => { assert_eq!(node_id, nodes[0].node.get_our_node_id()) }
465 _ => panic!("Unexpected event"),
468 // We test that if user opt-out, we provide a zero-length script at channel opening and we are able to close
469 // channel smoothly, opt-out is from channel initiator here
470 *nodes[0].override_init_features.borrow_mut() = None;
471 let chan = create_announced_chan_between_nodes_with_value(&nodes, 1, 0, 1000000, 1000000);
472 nodes[1].node.close_channel(&OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id(), &nodes[0].node.get_our_node_id()).unwrap();
473 check_added_monitors!(nodes[1], 1);
474 let node_0_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
475 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_0_shutdown);
476 let events = nodes[0].node.get_and_clear_pending_msg_events();
477 assert_eq!(events.len(), 1);
479 MessageSendEvent::SendShutdown { node_id, .. } => { assert_eq!(node_id, nodes[1].node.get_our_node_id()) }
480 _ => panic!("Unexpected event"),
483 //// We test that if user opt-out, we provide a zero-length script at channel opening and we are able to close
484 //// channel smoothly
485 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 1000000);
486 nodes[1].node.close_channel(&OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id(), &nodes[0].node.get_our_node_id()).unwrap();
487 check_added_monitors!(nodes[1], 1);
488 let node_0_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
489 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_0_shutdown);
490 let events = nodes[0].node.get_and_clear_pending_msg_events();
491 assert_eq!(events.len(), 2);
493 MessageSendEvent::SendShutdown { node_id, .. } => { assert_eq!(node_id, nodes[1].node.get_our_node_id()) }
494 _ => panic!("Unexpected event"),
497 MessageSendEvent::SendClosingSigned { node_id, .. } => { assert_eq!(node_id, nodes[1].node.get_our_node_id()) }
498 _ => panic!("Unexpected event"),
503 fn test_unsupported_anysegwit_upfront_shutdown_script() {
504 let chanmon_cfgs = create_chanmon_cfgs(2);
505 let mut node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
506 // Clear shutdown_anysegwit on initiator
507 *node_cfgs[0].override_init_features.borrow_mut() = Some(channelmanager::provided_init_features(&test_default_channel_config()).clear_shutdown_anysegwit());
508 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
509 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
511 // Use a non-v0 segwit script supported by option_shutdown_anysegwit
512 let anysegwit_shutdown_script = Builder::new()
514 .push_slice(&[0, 40])
517 // Check script when handling an open_channel message
518 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None).unwrap();
519 let mut open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
520 open_channel.shutdown_scriptpubkey = Present(anysegwit_shutdown_script.clone());
521 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel);
523 let events = nodes[1].node.get_and_clear_pending_msg_events();
524 assert_eq!(events.len(), 1);
526 MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg }, node_id } => {
527 assert_eq!(node_id, nodes[0].node.get_our_node_id());
528 assert_eq!(msg.data, "Peer is signaling upfront_shutdown but has provided an unacceptable scriptpubkey format: Script(OP_PUSHNUM_16 OP_PUSHBYTES_2 0028)");
530 _ => panic!("Unexpected event"),
533 let chanmon_cfgs = create_chanmon_cfgs(2);
534 let mut node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
535 // Clear shutdown_anysegwit on responder
536 *node_cfgs[1].override_init_features.borrow_mut() = Some(channelmanager::provided_init_features(&test_default_channel_config()).clear_shutdown_anysegwit());
537 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
538 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
540 // Check script when handling an accept_channel message
541 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None).unwrap();
542 let open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
543 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel);
544 let mut accept_channel = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
545 accept_channel.shutdown_scriptpubkey = Present(anysegwit_shutdown_script.clone());
546 nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &accept_channel);
548 let events = nodes[0].node.get_and_clear_pending_msg_events();
549 assert_eq!(events.len(), 1);
551 MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg }, node_id } => {
552 assert_eq!(node_id, nodes[1].node.get_our_node_id());
553 assert_eq!(msg.data, "Peer is signaling upfront_shutdown but has provided an unacceptable scriptpubkey format: Script(OP_PUSHNUM_16 OP_PUSHBYTES_2 0028)");
555 _ => panic!("Unexpected event"),
557 check_closed_event!(nodes[0], 1, ClosureReason::ProcessingError { err: "Peer is signaling upfront_shutdown but has provided an unacceptable scriptpubkey format: Script(OP_PUSHNUM_16 OP_PUSHBYTES_2 0028)".to_string() });
561 fn test_invalid_upfront_shutdown_script() {
562 let chanmon_cfgs = create_chanmon_cfgs(2);
563 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
564 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
565 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
567 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None).unwrap();
569 // Use a segwit v0 script with an unsupported witness program
570 let mut open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
571 open_channel.shutdown_scriptpubkey = Present(Builder::new().push_int(0)
574 nodes[0].node.handle_open_channel(&nodes[1].node.get_our_node_id(), &open_channel);
576 let events = nodes[0].node.get_and_clear_pending_msg_events();
577 assert_eq!(events.len(), 1);
579 MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg }, node_id } => {
580 assert_eq!(node_id, nodes[1].node.get_our_node_id());
581 assert_eq!(msg.data, "Peer is signaling upfront_shutdown but has provided an unacceptable scriptpubkey format: Script(OP_0 OP_PUSHBYTES_2 0000)");
583 _ => panic!("Unexpected event"),
588 fn test_segwit_v0_shutdown_script() {
589 let mut config = UserConfig::default();
590 config.channel_handshake_config.announced_channel = true;
591 config.channel_handshake_limits.force_announced_channel_preference = false;
592 config.channel_handshake_config.commit_upfront_shutdown_pubkey = false;
593 let user_cfgs = [None, Some(config), None];
594 let chanmon_cfgs = create_chanmon_cfgs(3);
595 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
596 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &user_cfgs);
597 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
599 let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
600 nodes[1].node.close_channel(&OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id(), &nodes[0].node.get_our_node_id()).unwrap();
601 check_added_monitors!(nodes[1], 1);
603 // Use a segwit v0 script supported even without option_shutdown_anysegwit
604 let mut node_0_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
605 node_0_shutdown.scriptpubkey = Builder::new().push_int(0)
606 .push_slice(&[0; 20])
608 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_0_shutdown);
610 let events = nodes[0].node.get_and_clear_pending_msg_events();
611 assert_eq!(events.len(), 2);
613 MessageSendEvent::SendShutdown { node_id, .. } => { assert_eq!(node_id, nodes[1].node.get_our_node_id()) }
614 _ => panic!("Unexpected event"),
617 MessageSendEvent::SendClosingSigned { node_id, .. } => { assert_eq!(node_id, nodes[1].node.get_our_node_id()) }
618 _ => panic!("Unexpected event"),
623 fn test_anysegwit_shutdown_script() {
624 let mut config = UserConfig::default();
625 config.channel_handshake_config.announced_channel = true;
626 config.channel_handshake_limits.force_announced_channel_preference = false;
627 config.channel_handshake_config.commit_upfront_shutdown_pubkey = false;
628 let user_cfgs = [None, Some(config), None];
629 let chanmon_cfgs = create_chanmon_cfgs(3);
630 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
631 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &user_cfgs);
632 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
634 let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
635 nodes[1].node.close_channel(&OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id(), &nodes[0].node.get_our_node_id()).unwrap();
636 check_added_monitors!(nodes[1], 1);
638 // Use a non-v0 segwit script supported by option_shutdown_anysegwit
639 let mut node_0_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
640 node_0_shutdown.scriptpubkey = Builder::new().push_int(16)
643 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_0_shutdown);
645 let events = nodes[0].node.get_and_clear_pending_msg_events();
646 assert_eq!(events.len(), 2);
648 MessageSendEvent::SendShutdown { node_id, .. } => { assert_eq!(node_id, nodes[1].node.get_our_node_id()) }
649 _ => panic!("Unexpected event"),
652 MessageSendEvent::SendClosingSigned { node_id, .. } => { assert_eq!(node_id, nodes[1].node.get_our_node_id()) }
653 _ => panic!("Unexpected event"),
658 fn test_unsupported_anysegwit_shutdown_script() {
659 let mut config = UserConfig::default();
660 config.channel_handshake_config.announced_channel = true;
661 config.channel_handshake_limits.force_announced_channel_preference = false;
662 config.channel_handshake_config.commit_upfront_shutdown_pubkey = false;
663 let user_cfgs = [None, Some(config), None];
664 let chanmon_cfgs = create_chanmon_cfgs(3);
665 let mut node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
666 *node_cfgs[0].override_init_features.borrow_mut() = Some(channelmanager::provided_init_features(&config).clear_shutdown_anysegwit());
667 *node_cfgs[1].override_init_features.borrow_mut() = Some(channelmanager::provided_init_features(&config).clear_shutdown_anysegwit());
668 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &user_cfgs);
669 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
671 // Check that using an unsupported shutdown script fails and a supported one succeeds.
672 let supported_shutdown_script = chanmon_cfgs[1].keys_manager.get_shutdown_scriptpubkey().unwrap();
673 let unsupported_shutdown_script =
674 ShutdownScript::new_witness_program(WitnessVersion::V16, &[0, 40]).unwrap();
675 chanmon_cfgs[1].keys_manager
676 .expect(OnGetShutdownScriptpubkey { returns: unsupported_shutdown_script.clone() })
677 .expect(OnGetShutdownScriptpubkey { returns: supported_shutdown_script });
679 let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
680 match nodes[1].node.close_channel(&OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id(), &nodes[0].node.get_our_node_id()) {
681 Err(APIError::IncompatibleShutdownScript { script }) => {
682 assert_eq!(script.into_inner(), unsupported_shutdown_script.clone().into_inner());
684 Err(e) => panic!("Unexpected error: {:?}", e),
685 Ok(_) => panic!("Expected error"),
687 nodes[1].node.close_channel(&OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id(), &nodes[0].node.get_our_node_id()).unwrap();
688 check_added_monitors!(nodes[1], 1);
690 // Use a non-v0 segwit script unsupported without option_shutdown_anysegwit
691 let mut node_0_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
692 node_0_shutdown.scriptpubkey = unsupported_shutdown_script.into_inner();
693 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_0_shutdown);
695 assert_eq!(&check_warn_msg!(nodes[0], nodes[1].node.get_our_node_id(), chan.2),
696 "Got a nonstandard scriptpubkey (60020028) from remote peer");
700 fn test_invalid_shutdown_script() {
701 let mut config = UserConfig::default();
702 config.channel_handshake_config.announced_channel = true;
703 config.channel_handshake_limits.force_announced_channel_preference = false;
704 config.channel_handshake_config.commit_upfront_shutdown_pubkey = false;
705 let user_cfgs = [None, Some(config), None];
706 let chanmon_cfgs = create_chanmon_cfgs(3);
707 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
708 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &user_cfgs);
709 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
711 let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
712 nodes[1].node.close_channel(&OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id(), &nodes[0].node.get_our_node_id()).unwrap();
713 check_added_monitors!(nodes[1], 1);
715 // Use a segwit v0 script with an unsupported witness program
716 let mut node_0_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
717 node_0_shutdown.scriptpubkey = Builder::new().push_int(0)
720 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_0_shutdown);
722 assert_eq!(&check_warn_msg!(nodes[0], nodes[1].node.get_our_node_id(), chan.2),
723 "Got a nonstandard scriptpubkey (00020000) from remote peer");
733 fn do_test_closing_signed_reinit_timeout(timeout_step: TimeoutStep) {
734 // The range-based closing signed negotiation allows the funder to restart the process with a
735 // new range if the previous range did not overlap. This allows implementations to request user
736 // intervention allowing users to enter a new fee range. We do not implement the sending side
737 // of this, instead opting to allow users to enter an explicit "willing to pay up to X to avoid
738 // force-closing" value and relying on that instead.
740 // Here we run test the fundee side of that restart mechanism, implementing the funder side of
742 let chanmon_cfgs = create_chanmon_cfgs(2);
743 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
744 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
745 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
746 let chan_id = create_announced_chan_between_nodes(&nodes, 0, 1).2;
748 send_payment(&nodes[0], &[&nodes[1]], 8_000_000);
750 nodes[0].node.close_channel(&chan_id, &nodes[1].node.get_our_node_id()).unwrap();
751 let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
752 nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_shutdown);
753 let node_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
754 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_shutdown);
757 // Now we set nodes[1] to require a relatively high feerate for closing. This should result
758 // in it rejecting nodes[0]'s initial closing_signed, giving nodes[0] a chance to try
760 let mut feerate_lock = chanmon_cfgs[1].fee_estimator.sat_per_kw.lock().unwrap();
764 let mut node_0_closing_signed = get_event_msg!(nodes[0], MessageSendEvent::SendClosingSigned, nodes[1].node.get_our_node_id());
765 // nodes[0] should use a "reasonable" feerate, well under the 10 sat/vByte that nodes[1] thinks
766 // is the current prevailing feerate.
767 assert!(node_0_closing_signed.fee_satoshis <= 500);
769 if timeout_step != TimeoutStep::AfterShutdown {
770 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed);
771 assert!(check_warn_msg!(nodes[1], nodes[0].node.get_our_node_id(), chan_id)
772 .starts_with("Unable to come to consensus about closing feerate"));
774 // Now deliver a mutated closing_signed indicating a higher acceptable fee range, which
775 // nodes[1] should happily accept and respond to.
776 node_0_closing_signed.fee_range.as_mut().unwrap().max_fee_satoshis *= 10;
778 let mut node_0_per_peer_lock;
779 let mut node_0_peer_state_lock;
780 get_channel_ref!(nodes[0], nodes[1], node_0_per_peer_lock, node_0_peer_state_lock, chan_id).closing_fee_limits.as_mut().unwrap().1 *= 10;
782 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed);
783 let node_1_closing_signed = get_event_msg!(nodes[1], MessageSendEvent::SendClosingSigned, nodes[0].node.get_our_node_id());
784 nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed);
785 let node_0_2nd_closing_signed = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
786 if timeout_step == TimeoutStep::NoTimeout {
787 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_2nd_closing_signed.1.unwrap());
788 check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure);
790 check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure);
793 if timeout_step != TimeoutStep::NoTimeout {
794 assert!(nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().is_empty());
796 assert_eq!(nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 1);
799 nodes[1].node.timer_tick_occurred();
800 nodes[1].node.timer_tick_occurred();
802 let txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
803 assert_eq!(txn.len(), 1);
804 assert_eq!(txn[0].output.len(), 2);
806 if timeout_step != TimeoutStep::NoTimeout {
807 assert!((txn[0].output[0].script_pubkey.is_v0_p2wpkh() &&
808 txn[0].output[1].script_pubkey.is_v0_p2wsh()) ||
809 (txn[0].output[1].script_pubkey.is_v0_p2wpkh() &&
810 txn[0].output[0].script_pubkey.is_v0_p2wsh()));
811 check_closed_broadcast!(nodes[1], true);
812 check_added_monitors!(nodes[1], 1);
813 check_closed_event!(nodes[1], 1, ClosureReason::ProcessingError { err: "closing_signed negotiation failed to finish within two timer ticks".to_string() });
815 assert!(txn[0].output[0].script_pubkey.is_v0_p2wpkh());
816 assert!(txn[0].output[1].script_pubkey.is_v0_p2wpkh());
818 let events = nodes[1].node.get_and_clear_pending_msg_events();
819 assert_eq!(events.len(), 1);
821 MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
822 assert_eq!(msg.contents.flags & 2, 2);
824 _ => panic!("Unexpected event"),
830 fn test_closing_signed_reinit_timeout() {
831 do_test_closing_signed_reinit_timeout(TimeoutStep::AfterShutdown);
832 do_test_closing_signed_reinit_timeout(TimeoutStep::AfterClosingSigned);
833 do_test_closing_signed_reinit_timeout(TimeoutStep::NoTimeout);
836 fn do_simple_legacy_shutdown_test(high_initiator_fee: bool) {
837 // A simpe test of the legacy shutdown fee negotiation logic.
838 let chanmon_cfgs = create_chanmon_cfgs(2);
839 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
840 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
841 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
843 let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
845 if high_initiator_fee {
846 // If high_initiator_fee is set, set nodes[0]'s feerate significantly higher. This
847 // shouldn't impact the flow at all given nodes[1] will happily accept the higher fee.
848 let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
852 nodes[0].node.close_channel(&OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id(), &nodes[1].node.get_our_node_id()).unwrap();
853 let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
854 nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_shutdown);
855 let node_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
856 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_shutdown);
858 let mut node_0_closing_signed = get_event_msg!(nodes[0], MessageSendEvent::SendClosingSigned, nodes[1].node.get_our_node_id());
859 node_0_closing_signed.fee_range = None;
860 if high_initiator_fee {
861 assert!(node_0_closing_signed.fee_satoshis > 500);
863 assert!(node_0_closing_signed.fee_satoshis < 500);
866 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed);
867 let (_, mut node_1_closing_signed) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
868 node_1_closing_signed.as_mut().unwrap().fee_range = None;
870 nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed.unwrap());
871 let (_, node_0_none) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
872 assert!(node_0_none.is_none());
873 check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure);
874 check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure);
878 fn simple_legacy_shutdown_test() {
879 do_simple_legacy_shutdown_test(false);
880 do_simple_legacy_shutdown_test(true);
884 fn simple_target_feerate_shutdown() {
885 // Simple test of target in `close_channel_with_target_feerate`.
886 let chanmon_cfgs = create_chanmon_cfgs(2);
887 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
888 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
889 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
891 let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
892 let chan_id = OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id();
894 nodes[0].node.close_channel_with_target_feerate(&chan_id, &nodes[1].node.get_our_node_id(), 253 * 10).unwrap();
895 let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
896 nodes[1].node.close_channel_with_target_feerate(&chan_id, &nodes[0].node.get_our_node_id(), 253 * 5).unwrap();
897 let node_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
899 nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_shutdown);
900 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_shutdown);
902 let node_0_closing_signed = get_event_msg!(nodes[0], MessageSendEvent::SendClosingSigned, nodes[1].node.get_our_node_id());
903 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed);
904 let (_, node_1_closing_signed_opt) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
905 let node_1_closing_signed = node_1_closing_signed_opt.unwrap();
907 // nodes[1] was passed a target which was larger than the current channel feerate, which it
908 // should ignore in favor of the channel fee, as there is no use demanding a minimum higher
909 // than what will be paid on a force-close transaction. Note that we have to consider rounding,
910 // so only check that we're within 10 sats.
911 assert!(node_0_closing_signed.fee_range.as_ref().unwrap().min_fee_satoshis >=
912 node_1_closing_signed.fee_range.as_ref().unwrap().min_fee_satoshis * 10 - 5);
913 assert!(node_0_closing_signed.fee_range.as_ref().unwrap().min_fee_satoshis <=
914 node_1_closing_signed.fee_range.as_ref().unwrap().min_fee_satoshis * 10 + 5);
916 // Further, because nodes[0]'s target fee is larger than the `Normal` fee estimation plus our
917 // force-closure-avoidance buffer, min should equal max, and the nodes[1]-selected fee should
918 // be the nodes[0] only available fee.
919 assert_eq!(node_0_closing_signed.fee_range.as_ref().unwrap().min_fee_satoshis,
920 node_0_closing_signed.fee_range.as_ref().unwrap().max_fee_satoshis);
921 assert_eq!(node_0_closing_signed.fee_range.as_ref().unwrap().min_fee_satoshis,
922 node_0_closing_signed.fee_satoshis);
923 assert_eq!(node_0_closing_signed.fee_satoshis, node_1_closing_signed.fee_satoshis);
925 nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed);
926 let (_, node_0_none) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
927 assert!(node_0_none.is_none());
928 check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure);
929 check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure);