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::sign::{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, ChannelShutdownState, ChannelDetails};
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;
34 use std::convert::TryFrom;
36 use crate::ln::functional_test_utils::*;
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, [nodes[1].node.get_our_node_id()], 8000000);
67 check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure, [nodes[0].node.get_our_node_id()], 8000000);
71 fn expect_channel_shutdown_state() {
72 // Test sending a shutdown prior to channel_ready after funding generation
73 let chanmon_cfgs = create_chanmon_cfgs(2);
74 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
75 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
76 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
77 let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
79 expect_channel_shutdown_state!(nodes[0], chan_1.2, ChannelShutdownState::NotShuttingDown);
81 nodes[0].node.close_channel(&chan_1.2, &nodes[1].node.get_our_node_id()).unwrap();
83 expect_channel_shutdown_state!(nodes[0], chan_1.2, ChannelShutdownState::ShutdownInitiated);
84 expect_channel_shutdown_state!(nodes[1], chan_1.2, ChannelShutdownState::NotShuttingDown);
86 let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
87 nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_shutdown);
89 // node1 goes into NegotiatingClosingFee since there are no HTLCs in flight, note that it
90 // doesnt mean that node1 has sent/recved its closing signed message
91 expect_channel_shutdown_state!(nodes[0], chan_1.2, ChannelShutdownState::ShutdownInitiated);
92 expect_channel_shutdown_state!(nodes[1], chan_1.2, ChannelShutdownState::NegotiatingClosingFee);
94 let node_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
95 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_shutdown);
97 expect_channel_shutdown_state!(nodes[0], chan_1.2, ChannelShutdownState::NegotiatingClosingFee);
98 expect_channel_shutdown_state!(nodes[1], chan_1.2, ChannelShutdownState::NegotiatingClosingFee);
100 let node_0_closing_signed = get_event_msg!(nodes[0], MessageSendEvent::SendClosingSigned, nodes[1].node.get_our_node_id());
101 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed);
102 let node_1_closing_signed = get_event_msg!(nodes[1], MessageSendEvent::SendClosingSigned, nodes[0].node.get_our_node_id());
103 nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed);
104 let (_, node_0_2nd_closing_signed) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
105 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_2nd_closing_signed.unwrap());
106 let (_, node_1_none) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
107 assert!(node_1_none.is_none());
109 assert!(nodes[0].node.list_channels().is_empty());
110 assert!(nodes[1].node.list_channels().is_empty());
111 check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
112 check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure, [nodes[0].node.get_our_node_id()], 100000);
116 fn expect_channel_shutdown_state_with_htlc() {
117 // Test sending a shutdown with outstanding updates pending.
118 let chanmon_cfgs = create_chanmon_cfgs(3);
119 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
120 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
121 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
122 let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
123 let _chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
125 let (payment_preimage_0, payment_hash_0, _) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 100_000);
127 expect_channel_shutdown_state!(nodes[0], chan_1.2, ChannelShutdownState::NotShuttingDown);
128 expect_channel_shutdown_state!(nodes[1], chan_1.2, ChannelShutdownState::NotShuttingDown);
130 nodes[0].node.close_channel(&chan_1.2, &nodes[1].node.get_our_node_id()).unwrap();
132 expect_channel_shutdown_state!(nodes[0], chan_1.2, ChannelShutdownState::ShutdownInitiated);
133 expect_channel_shutdown_state!(nodes[1], chan_1.2, ChannelShutdownState::NotShuttingDown);
135 let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
136 nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_shutdown);
138 expect_channel_shutdown_state!(nodes[0], chan_1.2, ChannelShutdownState::ShutdownInitiated);
139 expect_channel_shutdown_state!(nodes[1], chan_1.2, ChannelShutdownState::ResolvingHTLCs);
141 let node_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
142 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_shutdown);
144 expect_channel_shutdown_state!(nodes[0], chan_1.2, ChannelShutdownState::ResolvingHTLCs);
145 expect_channel_shutdown_state!(nodes[1], chan_1.2, ChannelShutdownState::ResolvingHTLCs);
147 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
148 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
150 // Claim Funds on Node2
151 nodes[2].node.claim_funds(payment_preimage_0);
152 check_added_monitors!(nodes[2], 1);
153 expect_payment_claimed!(nodes[2], payment_hash_0, 100_000);
155 // Fulfil HTLCs on node1 and node0
156 let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
157 assert!(updates.update_add_htlcs.is_empty());
158 assert!(updates.update_fail_htlcs.is_empty());
159 assert!(updates.update_fail_malformed_htlcs.is_empty());
160 assert!(updates.update_fee.is_none());
161 assert_eq!(updates.update_fulfill_htlcs.len(), 1);
162 nodes[1].node.handle_update_fulfill_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fulfill_htlcs[0]);
163 expect_payment_forwarded!(nodes[1], nodes[0], nodes[2], Some(1000), false, false);
164 check_added_monitors!(nodes[1], 1);
165 let updates_2 = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
166 commitment_signed_dance!(nodes[1], nodes[2], updates.commitment_signed, false);
168 // Still in "resolvingHTLCs" on chan1 after htlc removed on chan2
169 expect_channel_shutdown_state!(nodes[0], chan_1.2, ChannelShutdownState::ResolvingHTLCs);
170 expect_channel_shutdown_state!(nodes[1], chan_1.2, ChannelShutdownState::ResolvingHTLCs);
172 assert!(updates_2.update_add_htlcs.is_empty());
173 assert!(updates_2.update_fail_htlcs.is_empty());
174 assert!(updates_2.update_fail_malformed_htlcs.is_empty());
175 assert!(updates_2.update_fee.is_none());
176 assert_eq!(updates_2.update_fulfill_htlcs.len(), 1);
177 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &updates_2.update_fulfill_htlcs[0]);
178 commitment_signed_dance!(nodes[0], nodes[1], updates_2.commitment_signed, false, true);
179 expect_payment_sent!(nodes[0], payment_preimage_0);
181 // all htlcs removed, chan1 advances to NegotiatingClosingFee
182 expect_channel_shutdown_state!(nodes[0], chan_1.2, ChannelShutdownState::NegotiatingClosingFee);
183 expect_channel_shutdown_state!(nodes[1], chan_1.2, ChannelShutdownState::NegotiatingClosingFee);
185 // ClosingSignNegotion process
186 let node_0_closing_signed = get_event_msg!(nodes[0], MessageSendEvent::SendClosingSigned, nodes[1].node.get_our_node_id());
187 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed);
188 let node_1_closing_signed = get_event_msg!(nodes[1], MessageSendEvent::SendClosingSigned, nodes[0].node.get_our_node_id());
189 nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed);
190 let (_, node_0_2nd_closing_signed) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
191 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_2nd_closing_signed.unwrap());
192 let (_, node_1_none) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
193 assert!(node_1_none.is_none());
194 check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
195 check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure, [nodes[0].node.get_our_node_id()], 100000);
197 // Shutdown basically removes the channelDetails, testing of shutdowncomplete state unnecessary
198 assert!(nodes[0].node.list_channels().is_empty());
202 fn test_lnd_bug_6039() {
203 // LND sends a nonsense error message any time it gets a shutdown if there are still HTLCs
204 // pending. We currently swallow that error to work around LND's bug #6039. This test emulates
205 // the LND nonsense and ensures we at least kinda handle it.
206 let chanmon_cfgs = create_chanmon_cfgs(2);
207 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
208 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
209 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
210 let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
212 let (payment_preimage, payment_hash, _) = route_payment(&nodes[0], &[&nodes[1]], 100_000);
214 nodes[0].node.close_channel(&chan.2, &nodes[1].node.get_our_node_id()).unwrap();
215 let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
216 nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_shutdown);
218 // Generate an lnd-like error message and check that we respond by simply screaming louder to
219 // see if LND will accept our protocol compliance.
220 let err_msg = msgs::ErrorMessage { channel_id: chan.2, data: "link failed to shutdown".to_string() };
221 nodes[0].node.handle_error(&nodes[1].node.get_our_node_id(), &err_msg);
222 let node_a_responses = nodes[0].node.get_and_clear_pending_msg_events();
223 assert_eq!(node_a_responses[0], MessageSendEvent::SendShutdown {
224 node_id: nodes[1].node.get_our_node_id(),
225 msg: node_0_shutdown,
227 if let MessageSendEvent::HandleError { action: msgs::ErrorAction::SendWarningMessage { .. }, .. }
228 = node_a_responses[1] {} else { panic!(); }
230 let node_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
232 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
233 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
235 claim_payment(&nodes[0], &[&nodes[1]], payment_preimage);
237 // Assume that LND will eventually respond to our Shutdown if we clear all the remaining HTLCs
238 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_shutdown);
240 // ClosingSignNegotion process
241 let node_0_closing_signed = get_event_msg!(nodes[0], MessageSendEvent::SendClosingSigned, nodes[1].node.get_our_node_id());
242 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed);
243 let node_1_closing_signed = get_event_msg!(nodes[1], MessageSendEvent::SendClosingSigned, nodes[0].node.get_our_node_id());
244 nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed);
245 let (_, node_0_2nd_closing_signed) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
246 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_2nd_closing_signed.unwrap());
247 let (_, node_1_none) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
248 assert!(node_1_none.is_none());
249 check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
250 check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure, [nodes[0].node.get_our_node_id()], 100000);
252 // Shutdown basically removes the channelDetails, testing of shutdowncomplete state unnecessary
253 assert!(nodes[0].node.list_channels().is_empty());
257 fn expect_channel_shutdown_state_with_force_closure() {
258 // Test sending a shutdown prior to channel_ready after funding generation
259 let chanmon_cfgs = create_chanmon_cfgs(2);
260 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
261 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
262 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
263 let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
265 expect_channel_shutdown_state!(nodes[0], chan_1.2, ChannelShutdownState::NotShuttingDown);
266 expect_channel_shutdown_state!(nodes[1], chan_1.2, ChannelShutdownState::NotShuttingDown);
268 nodes[1].node.force_close_broadcasting_latest_txn(&chan_1.2, &nodes[0].node.get_our_node_id()).unwrap();
269 check_closed_broadcast!(nodes[1], true);
270 check_added_monitors!(nodes[1], 1);
272 expect_channel_shutdown_state!(nodes[0], chan_1.2, ChannelShutdownState::NotShuttingDown);
273 assert!(nodes[1].node.list_channels().is_empty());
275 let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
276 assert_eq!(node_txn.len(), 1);
277 check_spends!(node_txn[0], chan_1.3);
278 mine_transaction(&nodes[0], &node_txn[0]);
279 check_added_monitors!(nodes[0], 1);
281 assert!(nodes[0].node.list_channels().is_empty());
282 assert!(nodes[1].node.list_channels().is_empty());
283 check_closed_broadcast!(nodes[0], true);
284 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
285 check_closed_event!(nodes[1], 1, ClosureReason::HolderForceClosed, [nodes[0].node.get_our_node_id()], 100000);
289 fn updates_shutdown_wait() {
290 // Test sending a shutdown with outstanding updates pending
291 let chanmon_cfgs = create_chanmon_cfgs(3);
292 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
293 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
294 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
295 let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
296 let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
297 let logger = test_utils::TestLogger::new();
298 let scorer = test_utils::TestScorer::new();
299 let keys_manager = test_utils::TestKeysInterface::new(&[0u8; 32], Network::Testnet);
300 let random_seed_bytes = keys_manager.get_secure_random_bytes();
302 let (payment_preimage_0, payment_hash_0, _) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 100_000);
304 nodes[0].node.close_channel(&chan_1.2, &nodes[1].node.get_our_node_id()).unwrap();
305 let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
306 nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_shutdown);
307 let node_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
308 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_shutdown);
310 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
311 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
313 let (_, payment_hash, payment_secret) = get_payment_preimage_hash!(nodes[0]);
315 let payment_params_1 = PaymentParameters::from_node_id(nodes[1].node.get_our_node_id(), TEST_FINAL_CLTV).with_bolt11_features(nodes[1].node.invoice_features()).unwrap();
316 let route_1 = get_route(&nodes[0].node.get_our_node_id(), &payment_params_1, &nodes[0].network_graph.read_only(), None, 100000, &logger, &scorer, &(), &random_seed_bytes).unwrap();
317 let payment_params_2 = PaymentParameters::from_node_id(nodes[0].node.get_our_node_id(), TEST_FINAL_CLTV).with_bolt11_features(nodes[0].node.invoice_features()).unwrap();
318 let route_2 = get_route(&nodes[1].node.get_our_node_id(), &payment_params_2, &nodes[1].network_graph.read_only(), None, 100000, &logger, &scorer, &(), &random_seed_bytes).unwrap();
319 unwrap_send_err!(nodes[0].node.send_payment_with_route(&route_1, payment_hash,
320 RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)
321 ), true, APIError::ChannelUnavailable {..}, {});
322 unwrap_send_err!(nodes[1].node.send_payment_with_route(&route_2, payment_hash,
323 RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)
324 ), true, APIError::ChannelUnavailable {..}, {});
326 nodes[2].node.claim_funds(payment_preimage_0);
327 check_added_monitors!(nodes[2], 1);
328 expect_payment_claimed!(nodes[2], payment_hash_0, 100_000);
330 let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
331 assert!(updates.update_add_htlcs.is_empty());
332 assert!(updates.update_fail_htlcs.is_empty());
333 assert!(updates.update_fail_malformed_htlcs.is_empty());
334 assert!(updates.update_fee.is_none());
335 assert_eq!(updates.update_fulfill_htlcs.len(), 1);
336 nodes[1].node.handle_update_fulfill_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fulfill_htlcs[0]);
337 expect_payment_forwarded!(nodes[1], nodes[0], nodes[2], Some(1000), false, false);
338 check_added_monitors!(nodes[1], 1);
339 let updates_2 = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
340 commitment_signed_dance!(nodes[1], nodes[2], updates.commitment_signed, false);
342 assert!(updates_2.update_add_htlcs.is_empty());
343 assert!(updates_2.update_fail_htlcs.is_empty());
344 assert!(updates_2.update_fail_malformed_htlcs.is_empty());
345 assert!(updates_2.update_fee.is_none());
346 assert_eq!(updates_2.update_fulfill_htlcs.len(), 1);
347 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &updates_2.update_fulfill_htlcs[0]);
348 commitment_signed_dance!(nodes[0], nodes[1], updates_2.commitment_signed, false, true);
349 expect_payment_sent!(nodes[0], payment_preimage_0);
351 let node_0_closing_signed = get_event_msg!(nodes[0], MessageSendEvent::SendClosingSigned, nodes[1].node.get_our_node_id());
352 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed);
353 let node_1_closing_signed = get_event_msg!(nodes[1], MessageSendEvent::SendClosingSigned, nodes[0].node.get_our_node_id());
354 nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed);
355 let (_, node_0_2nd_closing_signed) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
356 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_2nd_closing_signed.unwrap());
357 let (_, node_1_none) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
358 assert!(node_1_none.is_none());
359 check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
360 check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure, [nodes[0].node.get_our_node_id()], 100000);
362 assert!(nodes[0].node.list_channels().is_empty());
364 assert_eq!(nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 1);
365 nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
366 close_channel(&nodes[1], &nodes[2], &chan_2.2, chan_2.3, true);
367 assert!(nodes[1].node.list_channels().is_empty());
368 assert!(nodes[2].node.list_channels().is_empty());
369 check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure, [nodes[2].node.get_our_node_id()], 100000);
370 check_closed_event!(nodes[2], 1, ClosureReason::CooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
374 fn htlc_fail_async_shutdown() {
375 // Test HTLCs fail if shutdown starts even if messages are delivered out-of-order
376 let chanmon_cfgs = create_chanmon_cfgs(3);
377 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
378 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
379 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
380 let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
381 let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
383 let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], 100000);
384 nodes[0].node.send_payment_with_route(&route, our_payment_hash,
385 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
386 check_added_monitors!(nodes[0], 1);
387 let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
388 assert_eq!(updates.update_add_htlcs.len(), 1);
389 assert!(updates.update_fulfill_htlcs.is_empty());
390 assert!(updates.update_fail_htlcs.is_empty());
391 assert!(updates.update_fail_malformed_htlcs.is_empty());
392 assert!(updates.update_fee.is_none());
394 nodes[1].node.close_channel(&chan_1.2, &nodes[0].node.get_our_node_id()).unwrap();
395 let node_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
396 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_shutdown);
397 let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
399 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
400 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &updates.commitment_signed);
401 check_added_monitors!(nodes[1], 1);
402 nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_shutdown);
403 commitment_signed_dance!(nodes[1], nodes[0], (), false, true, false, false);
405 let updates_2 = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
406 assert!(updates_2.update_add_htlcs.is_empty());
407 assert!(updates_2.update_fulfill_htlcs.is_empty());
408 assert_eq!(updates_2.update_fail_htlcs.len(), 1);
409 assert!(updates_2.update_fail_malformed_htlcs.is_empty());
410 assert!(updates_2.update_fee.is_none());
412 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &updates_2.update_fail_htlcs[0]);
413 commitment_signed_dance!(nodes[0], nodes[1], updates_2.commitment_signed, false, true);
415 expect_payment_failed_with_update!(nodes[0], our_payment_hash, false, chan_2.0.contents.short_channel_id, true);
417 let msg_events = nodes[0].node.get_and_clear_pending_msg_events();
418 assert_eq!(msg_events.len(), 1);
419 let node_0_closing_signed = match msg_events[0] {
420 MessageSendEvent::SendClosingSigned { ref node_id, ref msg } => {
421 assert_eq!(*node_id, nodes[1].node.get_our_node_id());
424 _ => panic!("Unexpected event"),
427 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
428 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed);
429 let node_1_closing_signed = get_event_msg!(nodes[1], MessageSendEvent::SendClosingSigned, nodes[0].node.get_our_node_id());
430 nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed);
431 let (_, node_0_2nd_closing_signed) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
432 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_2nd_closing_signed.unwrap());
433 let (_, node_1_none) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
434 assert!(node_1_none.is_none());
436 assert!(nodes[0].node.list_channels().is_empty());
438 assert_eq!(nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 1);
439 nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
440 close_channel(&nodes[1], &nodes[2], &chan_2.2, chan_2.3, true);
441 assert!(nodes[1].node.list_channels().is_empty());
442 assert!(nodes[2].node.list_channels().is_empty());
443 check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
444 check_closed_event!(nodes[1], 2, ClosureReason::CooperativeClosure, [nodes[0].node.get_our_node_id(), nodes[2].node.get_our_node_id()], 100000);
445 check_closed_event!(nodes[2], 1, ClosureReason::CooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
448 fn do_test_shutdown_rebroadcast(recv_count: u8) {
449 // Test that shutdown/closing_signed is re-sent on reconnect with a variable number of
450 // messages delivered prior to disconnect
451 let chanmon_cfgs = create_chanmon_cfgs(3);
452 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
453 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
454 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
455 let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
456 let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
458 let (payment_preimage, payment_hash, _) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 100_000);
460 nodes[1].node.close_channel(&chan_1.2, &nodes[0].node.get_our_node_id()).unwrap();
461 let node_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
463 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_shutdown);
464 let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
466 nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_shutdown);
470 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
471 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
473 nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init {
474 features: nodes[1].node.init_features(), networks: None, remote_network_address: None
476 let node_0_reestablish = get_chan_reestablish_msgs!(nodes[0], nodes[1]).pop().unwrap();
477 nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
478 features: nodes[0].node.init_features(), networks: None, remote_network_address: None
480 let node_1_reestablish = get_chan_reestablish_msgs!(nodes[1], nodes[0]).pop().unwrap();
482 nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &node_0_reestablish);
483 let node_1_2nd_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
484 assert!(node_1_shutdown == node_1_2nd_shutdown);
486 nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &node_1_reestablish);
487 let node_0_2nd_shutdown = if recv_count > 0 {
488 let node_0_2nd_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
489 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_2nd_shutdown);
492 let node_0_chan_update = get_event_msg!(nodes[0], MessageSendEvent::SendChannelUpdate, nodes[1].node.get_our_node_id());
493 assert_eq!(node_0_chan_update.contents.flags & 2, 0); // "disabled" flag must not be set as we just reconnected.
494 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_2nd_shutdown);
495 get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id())
497 nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_2nd_shutdown);
499 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
500 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
502 nodes[2].node.claim_funds(payment_preimage);
503 check_added_monitors!(nodes[2], 1);
504 expect_payment_claimed!(nodes[2], payment_hash, 100_000);
506 let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
507 assert!(updates.update_add_htlcs.is_empty());
508 assert!(updates.update_fail_htlcs.is_empty());
509 assert!(updates.update_fail_malformed_htlcs.is_empty());
510 assert!(updates.update_fee.is_none());
511 assert_eq!(updates.update_fulfill_htlcs.len(), 1);
512 nodes[1].node.handle_update_fulfill_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fulfill_htlcs[0]);
513 expect_payment_forwarded!(nodes[1], nodes[0], nodes[2], Some(1000), false, false);
514 check_added_monitors!(nodes[1], 1);
515 let updates_2 = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
516 commitment_signed_dance!(nodes[1], nodes[2], updates.commitment_signed, false);
518 assert!(updates_2.update_add_htlcs.is_empty());
519 assert!(updates_2.update_fail_htlcs.is_empty());
520 assert!(updates_2.update_fail_malformed_htlcs.is_empty());
521 assert!(updates_2.update_fee.is_none());
522 assert_eq!(updates_2.update_fulfill_htlcs.len(), 1);
523 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &updates_2.update_fulfill_htlcs[0]);
524 commitment_signed_dance!(nodes[0], nodes[1], updates_2.commitment_signed, false, true);
525 expect_payment_sent!(nodes[0], payment_preimage);
527 let node_0_closing_signed = get_event_msg!(nodes[0], MessageSendEvent::SendClosingSigned, nodes[1].node.get_our_node_id());
529 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed);
530 let node_1_closing_signed = get_event_msg!(nodes[1], MessageSendEvent::SendClosingSigned, nodes[0].node.get_our_node_id());
531 nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed);
532 let (_, node_0_2nd_closing_signed) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
533 assert!(node_0_2nd_closing_signed.is_some());
536 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
537 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
539 nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
540 features: nodes[0].node.init_features(), networks: None, remote_network_address: None
542 let node_1_2nd_reestablish = get_chan_reestablish_msgs!(nodes[1], nodes[0]).pop().unwrap();
543 nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init {
544 features: nodes[1].node.init_features(), networks: None, remote_network_address: None
547 // If all closing_signeds weren't delivered we can just resume where we left off...
548 let node_0_2nd_reestablish = get_chan_reestablish_msgs!(nodes[0], nodes[1]).pop().unwrap();
550 nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &node_1_2nd_reestablish);
551 let node_0_msgs = nodes[0].node.get_and_clear_pending_msg_events();
552 assert_eq!(node_0_msgs.len(), 2);
553 let node_0_2nd_closing_signed = match node_0_msgs[1] {
554 MessageSendEvent::SendClosingSigned { ref msg, .. } => {
555 assert_eq!(node_0_closing_signed, *msg);
561 let node_0_3rd_shutdown = match node_0_msgs[0] {
562 MessageSendEvent::SendShutdown { ref msg, .. } => {
563 assert_eq!(node_0_2nd_shutdown, *msg);
568 assert!(node_0_2nd_shutdown == node_0_3rd_shutdown);
570 nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &node_0_2nd_reestablish);
571 let node_1_3rd_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
572 assert!(node_1_3rd_shutdown == node_1_2nd_shutdown);
574 nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_3rd_shutdown);
575 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
577 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_3rd_shutdown);
579 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_2nd_closing_signed);
580 let node_1_closing_signed = get_event_msg!(nodes[1], MessageSendEvent::SendClosingSigned, nodes[0].node.get_our_node_id());
581 nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed);
582 let (_, node_0_2nd_closing_signed) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
583 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_2nd_closing_signed.unwrap());
584 let (_, node_1_none) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
585 assert!(node_1_none.is_none());
586 check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure, [nodes[0].node.get_our_node_id()], 100000);
588 // If one node, however, received + responded with an identical closing_signed we end
589 // up erroring and node[0] will try to broadcast its own latest commitment transaction.
590 // There isn't really anything better we can do simply, but in the future we might
591 // explore storing a set of recently-closed channels that got disconnected during
592 // closing_signed and avoiding broadcasting local commitment txn for some timeout to
593 // give our counterparty enough time to (potentially) broadcast a cooperative closing
595 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
597 nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &node_1_2nd_reestablish);
598 let msg_events = nodes[0].node.get_and_clear_pending_msg_events();
599 assert_eq!(msg_events.len(), 1);
600 if let MessageSendEvent::HandleError { ref action, .. } = msg_events[0] {
602 &ErrorAction::SendErrorMessage { ref msg } => {
603 nodes[1].node.handle_error(&nodes[0].node.get_our_node_id(), &msg);
604 assert_eq!(msg.channel_id, chan_1.2);
606 _ => panic!("Unexpected event!"),
608 } else { panic!("Needed SendErrorMessage close"); }
610 // get_closing_signed_broadcast usually eats the BroadcastChannelUpdate for us and
611 // checks it, but in this case nodes[1] didn't ever get a chance to receive a
612 // closing_signed so we do it ourselves
613 check_closed_broadcast!(nodes[1], false);
614 check_added_monitors!(nodes[1], 1);
615 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())) }
616 , [nodes[0].node.get_our_node_id()], 100000);
619 assert!(nodes[0].node.list_channels().is_empty());
621 assert_eq!(nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 1);
622 nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
623 close_channel(&nodes[1], &nodes[2], &chan_2.2, chan_2.3, true);
624 assert!(nodes[1].node.list_channels().is_empty());
625 assert!(nodes[2].node.list_channels().is_empty());
626 check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
627 check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure, [nodes[2].node.get_our_node_id()], 100000);
628 check_closed_event!(nodes[2], 1, ClosureReason::CooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
632 fn test_shutdown_rebroadcast() {
633 do_test_shutdown_rebroadcast(0);
634 do_test_shutdown_rebroadcast(1);
635 do_test_shutdown_rebroadcast(2);
639 fn test_upfront_shutdown_script() {
640 // BOLT 2 : Option upfront shutdown script, if peer commit its closing_script at channel opening
641 // enforce it at shutdown message
643 let mut config = UserConfig::default();
644 config.channel_handshake_config.announced_channel = true;
645 config.channel_handshake_limits.force_announced_channel_preference = false;
646 config.channel_handshake_config.commit_upfront_shutdown_pubkey = false;
647 let user_cfgs = [None, Some(config), None];
648 let chanmon_cfgs = create_chanmon_cfgs(3);
649 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
650 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &user_cfgs);
651 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
653 // We test that in case of peer committing upfront to a script, if it changes at closing, we refuse to sign
654 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 2, 1000000, 1000000);
655 nodes[0].node.close_channel(&OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id(), &nodes[2].node.get_our_node_id()).unwrap();
656 let node_0_orig_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[2].node.get_our_node_id());
657 let mut node_0_shutdown = node_0_orig_shutdown.clone();
658 node_0_shutdown.scriptpubkey = Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script().to_p2sh();
659 // Test we enforce upfront_scriptpbukey if by providing a different one at closing that we warn
660 // the peer and ignore the message.
661 nodes[2].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_shutdown);
662 assert!(regex::Regex::new(r"Got shutdown request with a scriptpubkey \([A-Fa-f0-9]+\) which did not match their previous scriptpubkey.")
663 .unwrap().is_match(&check_warn_msg!(nodes[2], nodes[0].node.get_our_node_id(), chan.2)));
664 // This allows nodes[2] to retry the shutdown message, which should get a response:
665 nodes[2].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_orig_shutdown);
666 get_event_msg!(nodes[2], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
668 // We test that in case of peer committing upfront to a script, if it doesn't change at closing, we sign
669 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 2, 1000000, 1000000);
670 nodes[0].node.close_channel(&OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id(), &nodes[2].node.get_our_node_id()).unwrap();
671 let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[2].node.get_our_node_id());
672 // We test that in case of peer committing upfront to a script, if it oesn't change at closing, we sign
673 nodes[2].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_shutdown);
674 let events = nodes[2].node.get_and_clear_pending_msg_events();
675 assert_eq!(events.len(), 1);
677 MessageSendEvent::SendShutdown { node_id, .. } => { assert_eq!(node_id, nodes[0].node.get_our_node_id()) }
678 _ => panic!("Unexpected event"),
681 // We test that if case of peer non-signaling we don't enforce committed script at channel opening
682 *nodes[0].override_init_features.borrow_mut() = Some(nodes[0].node.init_features().clear_upfront_shutdown_script());
683 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 1000000);
684 nodes[0].node.close_channel(&OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id(), &nodes[1].node.get_our_node_id()).unwrap();
685 let node_1_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
686 nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_1_shutdown);
687 check_added_monitors!(nodes[1], 1);
688 let events = nodes[1].node.get_and_clear_pending_msg_events();
689 assert_eq!(events.len(), 1);
691 MessageSendEvent::SendShutdown { node_id, .. } => { assert_eq!(node_id, nodes[0].node.get_our_node_id()) }
692 _ => panic!("Unexpected event"),
695 // We test that if user opt-out, we provide a zero-length script at channel opening and we are able to close
696 // channel smoothly, opt-out is from channel initiator here
697 *nodes[0].override_init_features.borrow_mut() = None;
698 let chan = create_announced_chan_between_nodes_with_value(&nodes, 1, 0, 1000000, 1000000);
699 nodes[1].node.close_channel(&OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id(), &nodes[0].node.get_our_node_id()).unwrap();
700 check_added_monitors!(nodes[1], 1);
701 let node_0_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
702 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_0_shutdown);
703 let events = nodes[0].node.get_and_clear_pending_msg_events();
704 assert_eq!(events.len(), 1);
706 MessageSendEvent::SendShutdown { node_id, .. } => { assert_eq!(node_id, nodes[1].node.get_our_node_id()) }
707 _ => panic!("Unexpected event"),
710 //// We test that if user opt-out, we provide a zero-length script at channel opening and we are able to close
711 //// channel smoothly
712 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 1000000);
713 nodes[1].node.close_channel(&OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id(), &nodes[0].node.get_our_node_id()).unwrap();
714 check_added_monitors!(nodes[1], 1);
715 let node_0_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
716 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_0_shutdown);
717 let events = nodes[0].node.get_and_clear_pending_msg_events();
718 assert_eq!(events.len(), 2);
720 MessageSendEvent::SendShutdown { node_id, .. } => { assert_eq!(node_id, nodes[1].node.get_our_node_id()) }
721 _ => panic!("Unexpected event"),
724 MessageSendEvent::SendClosingSigned { node_id, .. } => { assert_eq!(node_id, nodes[1].node.get_our_node_id()) }
725 _ => panic!("Unexpected event"),
730 fn test_unsupported_anysegwit_upfront_shutdown_script() {
731 let chanmon_cfgs = create_chanmon_cfgs(2);
732 let mut node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
733 // Clear shutdown_anysegwit on initiator
734 *node_cfgs[0].override_init_features.borrow_mut() = Some(channelmanager::provided_init_features(&test_default_channel_config()).clear_shutdown_anysegwit());
735 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
736 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
738 // Use a non-v0 segwit script supported by option_shutdown_anysegwit
739 let anysegwit_shutdown_script = Builder::new()
741 .push_slice(&[0, 40])
744 // Check script when handling an open_channel message
745 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None).unwrap();
746 let mut open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
747 open_channel.shutdown_scriptpubkey = Some(anysegwit_shutdown_script.clone());
748 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel);
750 let events = nodes[1].node.get_and_clear_pending_msg_events();
751 assert_eq!(events.len(), 1);
753 MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg }, node_id } => {
754 assert_eq!(node_id, nodes[0].node.get_our_node_id());
755 assert_eq!(msg.data, "Peer is signaling upfront_shutdown but has provided an unacceptable scriptpubkey format: Script(OP_PUSHNUM_16 OP_PUSHBYTES_2 0028)");
757 _ => panic!("Unexpected event"),
760 let chanmon_cfgs = create_chanmon_cfgs(2);
761 let mut node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
762 // Clear shutdown_anysegwit on responder
763 *node_cfgs[1].override_init_features.borrow_mut() = Some(channelmanager::provided_init_features(&test_default_channel_config()).clear_shutdown_anysegwit());
764 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
765 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
767 // Check script when handling an accept_channel message
768 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None).unwrap();
769 let open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
770 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel);
771 let mut accept_channel = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
772 accept_channel.shutdown_scriptpubkey = Some(anysegwit_shutdown_script.clone());
773 nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &accept_channel);
775 let events = nodes[0].node.get_and_clear_pending_msg_events();
776 assert_eq!(events.len(), 1);
778 MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg }, node_id } => {
779 assert_eq!(node_id, nodes[1].node.get_our_node_id());
780 assert_eq!(msg.data, "Peer is signaling upfront_shutdown but has provided an unacceptable scriptpubkey format: Script(OP_PUSHNUM_16 OP_PUSHBYTES_2 0028)");
782 _ => panic!("Unexpected event"),
784 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() }
785 , [nodes[1].node.get_our_node_id()], 100000);
789 fn test_invalid_upfront_shutdown_script() {
790 let chanmon_cfgs = create_chanmon_cfgs(2);
791 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
792 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
793 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
795 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None).unwrap();
797 // Use a segwit v0 script with an unsupported witness program
798 let mut open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
799 open_channel.shutdown_scriptpubkey = Some(Builder::new().push_int(0)
802 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel);
804 let events = nodes[1].node.get_and_clear_pending_msg_events();
805 assert_eq!(events.len(), 1);
807 MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg }, node_id } => {
808 assert_eq!(node_id, nodes[0].node.get_our_node_id());
809 assert_eq!(msg.data, "Peer is signaling upfront_shutdown but has provided an unacceptable scriptpubkey format: Script(OP_0 OP_PUSHBYTES_2 0000)");
811 _ => panic!("Unexpected event"),
816 fn test_segwit_v0_shutdown_script() {
817 let mut config = UserConfig::default();
818 config.channel_handshake_config.announced_channel = true;
819 config.channel_handshake_limits.force_announced_channel_preference = false;
820 config.channel_handshake_config.commit_upfront_shutdown_pubkey = false;
821 let user_cfgs = [None, Some(config), None];
822 let chanmon_cfgs = create_chanmon_cfgs(3);
823 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
824 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &user_cfgs);
825 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
827 let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
828 nodes[1].node.close_channel(&OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id(), &nodes[0].node.get_our_node_id()).unwrap();
829 check_added_monitors!(nodes[1], 1);
831 // Use a segwit v0 script supported even without option_shutdown_anysegwit
832 let mut node_0_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
833 node_0_shutdown.scriptpubkey = Builder::new().push_int(0)
834 .push_slice(&[0; 20])
836 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_0_shutdown);
838 let events = nodes[0].node.get_and_clear_pending_msg_events();
839 assert_eq!(events.len(), 2);
841 MessageSendEvent::SendShutdown { node_id, .. } => { assert_eq!(node_id, nodes[1].node.get_our_node_id()) }
842 _ => panic!("Unexpected event"),
845 MessageSendEvent::SendClosingSigned { node_id, .. } => { assert_eq!(node_id, nodes[1].node.get_our_node_id()) }
846 _ => panic!("Unexpected event"),
851 fn test_anysegwit_shutdown_script() {
852 let mut config = UserConfig::default();
853 config.channel_handshake_config.announced_channel = true;
854 config.channel_handshake_limits.force_announced_channel_preference = false;
855 config.channel_handshake_config.commit_upfront_shutdown_pubkey = false;
856 let user_cfgs = [None, Some(config), None];
857 let chanmon_cfgs = create_chanmon_cfgs(3);
858 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
859 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &user_cfgs);
860 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
862 let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
863 nodes[1].node.close_channel(&OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id(), &nodes[0].node.get_our_node_id()).unwrap();
864 check_added_monitors!(nodes[1], 1);
866 // Use a non-v0 segwit script supported by option_shutdown_anysegwit
867 let mut node_0_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
868 node_0_shutdown.scriptpubkey = Builder::new().push_int(16)
871 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_0_shutdown);
873 let events = nodes[0].node.get_and_clear_pending_msg_events();
874 assert_eq!(events.len(), 2);
876 MessageSendEvent::SendShutdown { node_id, .. } => { assert_eq!(node_id, nodes[1].node.get_our_node_id()) }
877 _ => panic!("Unexpected event"),
880 MessageSendEvent::SendClosingSigned { node_id, .. } => { assert_eq!(node_id, nodes[1].node.get_our_node_id()) }
881 _ => panic!("Unexpected event"),
886 fn test_unsupported_anysegwit_shutdown_script() {
887 let mut config = UserConfig::default();
888 config.channel_handshake_config.announced_channel = true;
889 config.channel_handshake_limits.force_announced_channel_preference = false;
890 config.channel_handshake_config.commit_upfront_shutdown_pubkey = false;
891 let user_cfgs = [None, Some(config), None];
892 let chanmon_cfgs = create_chanmon_cfgs(3);
893 let mut node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
894 *node_cfgs[0].override_init_features.borrow_mut() = Some(channelmanager::provided_init_features(&config).clear_shutdown_anysegwit());
895 *node_cfgs[1].override_init_features.borrow_mut() = Some(channelmanager::provided_init_features(&config).clear_shutdown_anysegwit());
896 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &user_cfgs);
897 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
899 // Check that using an unsupported shutdown script fails and a supported one succeeds.
900 let supported_shutdown_script = chanmon_cfgs[1].keys_manager.get_shutdown_scriptpubkey().unwrap();
901 let unsupported_shutdown_script =
902 ShutdownScript::new_witness_program(WitnessVersion::V16, &[0, 40]).unwrap();
903 chanmon_cfgs[1].keys_manager
904 .expect(OnGetShutdownScriptpubkey { returns: unsupported_shutdown_script.clone() })
905 .expect(OnGetShutdownScriptpubkey { returns: supported_shutdown_script });
907 let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
908 match nodes[1].node.close_channel(&OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id(), &nodes[0].node.get_our_node_id()) {
909 Err(APIError::IncompatibleShutdownScript { script }) => {
910 assert_eq!(script.into_inner(), unsupported_shutdown_script.clone().into_inner());
912 Err(e) => panic!("Unexpected error: {:?}", e),
913 Ok(_) => panic!("Expected error"),
915 nodes[1].node.close_channel(&OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id(), &nodes[0].node.get_our_node_id()).unwrap();
916 check_added_monitors!(nodes[1], 1);
918 // Use a non-v0 segwit script unsupported without option_shutdown_anysegwit
919 let mut node_0_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
920 node_0_shutdown.scriptpubkey = unsupported_shutdown_script.into_inner();
921 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_0_shutdown);
923 assert_eq!(&check_warn_msg!(nodes[0], nodes[1].node.get_our_node_id(), chan.2),
924 "Got a nonstandard scriptpubkey (60020028) from remote peer");
928 fn test_invalid_shutdown_script() {
929 let mut config = UserConfig::default();
930 config.channel_handshake_config.announced_channel = true;
931 config.channel_handshake_limits.force_announced_channel_preference = false;
932 config.channel_handshake_config.commit_upfront_shutdown_pubkey = false;
933 let user_cfgs = [None, Some(config), None];
934 let chanmon_cfgs = create_chanmon_cfgs(3);
935 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
936 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &user_cfgs);
937 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
939 let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
940 nodes[1].node.close_channel(&OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id(), &nodes[0].node.get_our_node_id()).unwrap();
941 check_added_monitors!(nodes[1], 1);
943 // Use a segwit v0 script with an unsupported witness program
944 let mut node_0_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
945 node_0_shutdown.scriptpubkey = Builder::new().push_int(0)
948 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_0_shutdown);
950 assert_eq!(&check_warn_msg!(nodes[0], nodes[1].node.get_our_node_id(), chan.2),
951 "Got a nonstandard scriptpubkey (00020000) from remote peer");
955 fn test_user_shutdown_script() {
956 let mut config = test_default_channel_config();
957 config.channel_handshake_config.announced_channel = true;
958 config.channel_handshake_limits.force_announced_channel_preference = false;
959 config.channel_handshake_config.commit_upfront_shutdown_pubkey = false;
960 let user_cfgs = [None, Some(config), None];
961 let chanmon_cfgs = create_chanmon_cfgs(3);
962 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
963 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &user_cfgs);
964 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
966 // Segwit v0 script of the form OP_0 <20-byte hash>
967 let script = Builder::new().push_int(0)
968 .push_slice(&[0; 20])
971 let shutdown_script = ShutdownScript::try_from(script.clone()).unwrap();
973 let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
974 nodes[1].node.close_channel_with_feerate_and_script(&OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id(), &nodes[0].node.get_our_node_id(), None, Some(shutdown_script)).unwrap();
975 check_added_monitors!(nodes[1], 1);
977 let mut node_0_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
979 assert_eq!(node_0_shutdown.scriptpubkey, script);
983 fn test_already_set_user_shutdown_script() {
984 let mut config = test_default_channel_config();
985 config.channel_handshake_config.announced_channel = true;
986 config.channel_handshake_limits.force_announced_channel_preference = false;
987 let user_cfgs = [None, Some(config), None];
988 let chanmon_cfgs = create_chanmon_cfgs(3);
989 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
990 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &user_cfgs);
991 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
993 // Segwit v0 script of the form OP_0 <20-byte hash>
994 let script = Builder::new().push_int(0)
995 .push_slice(&[0; 20])
998 let shutdown_script = ShutdownScript::try_from(script).unwrap();
1000 let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
1001 let result = nodes[1].node.close_channel_with_feerate_and_script(&OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id(), &nodes[0].node.get_our_node_id(), None, Some(shutdown_script));
1003 assert_eq!(result, Err(APIError::APIMisuseError { err: "Cannot override shutdown script for a channel with one already set".to_string() }));
1006 #[derive(PartialEq)]
1013 fn do_test_closing_signed_reinit_timeout(timeout_step: TimeoutStep) {
1014 // The range-based closing signed negotiation allows the funder to restart the process with a
1015 // new range if the previous range did not overlap. This allows implementations to request user
1016 // intervention allowing users to enter a new fee range. We do not implement the sending side
1017 // of this, instead opting to allow users to enter an explicit "willing to pay up to X to avoid
1018 // force-closing" value and relying on that instead.
1020 // Here we run test the fundee side of that restart mechanism, implementing the funder side of
1022 let chanmon_cfgs = create_chanmon_cfgs(2);
1023 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1024 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1025 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1026 let chan_id = create_announced_chan_between_nodes(&nodes, 0, 1).2;
1028 send_payment(&nodes[0], &[&nodes[1]], 8_000_000);
1030 nodes[0].node.close_channel(&chan_id, &nodes[1].node.get_our_node_id()).unwrap();
1031 let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
1032 nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_shutdown);
1033 let node_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
1034 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_shutdown);
1037 // Now we set nodes[1] to require a relatively high feerate for closing. This should result
1038 // in it rejecting nodes[0]'s initial closing_signed, giving nodes[0] a chance to try
1040 let mut feerate_lock = chanmon_cfgs[1].fee_estimator.sat_per_kw.lock().unwrap();
1041 *feerate_lock *= 10;
1044 let mut node_0_closing_signed = get_event_msg!(nodes[0], MessageSendEvent::SendClosingSigned, nodes[1].node.get_our_node_id());
1045 // nodes[0] should use a "reasonable" feerate, well under the 10 sat/vByte that nodes[1] thinks
1046 // is the current prevailing feerate.
1047 assert!(node_0_closing_signed.fee_satoshis <= 500);
1049 if timeout_step != TimeoutStep::AfterShutdown {
1050 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed);
1051 assert!(check_warn_msg!(nodes[1], nodes[0].node.get_our_node_id(), chan_id)
1052 .starts_with("Unable to come to consensus about closing feerate"));
1054 // Now deliver a mutated closing_signed indicating a higher acceptable fee range, which
1055 // nodes[1] should happily accept and respond to.
1056 node_0_closing_signed.fee_range.as_mut().unwrap().max_fee_satoshis *= 10;
1058 let mut node_0_per_peer_lock;
1059 let mut node_0_peer_state_lock;
1060 get_channel_ref!(nodes[0], nodes[1], node_0_per_peer_lock, node_0_peer_state_lock, chan_id).context.closing_fee_limits.as_mut().unwrap().1 *= 10;
1062 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed);
1063 let node_1_closing_signed = get_event_msg!(nodes[1], MessageSendEvent::SendClosingSigned, nodes[0].node.get_our_node_id());
1064 nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed);
1065 let node_0_2nd_closing_signed = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
1066 if timeout_step == TimeoutStep::NoTimeout {
1067 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_2nd_closing_signed.1.unwrap());
1068 check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure, [nodes[0].node.get_our_node_id()], 100000);
1070 check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
1073 if timeout_step != TimeoutStep::NoTimeout {
1074 assert!(nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().is_empty());
1076 assert_eq!(nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 1);
1079 nodes[1].node.timer_tick_occurred();
1080 nodes[1].node.timer_tick_occurred();
1082 let txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
1083 assert_eq!(txn.len(), 1);
1084 assert_eq!(txn[0].output.len(), 2);
1086 if timeout_step != TimeoutStep::NoTimeout {
1087 assert!((txn[0].output[0].script_pubkey.is_v0_p2wpkh() &&
1088 txn[0].output[1].script_pubkey.is_v0_p2wsh()) ||
1089 (txn[0].output[1].script_pubkey.is_v0_p2wpkh() &&
1090 txn[0].output[0].script_pubkey.is_v0_p2wsh()));
1091 check_closed_broadcast!(nodes[1], true);
1092 check_added_monitors!(nodes[1], 1);
1093 check_closed_event!(nodes[1], 1, ClosureReason::ProcessingError { err: "closing_signed negotiation failed to finish within two timer ticks".to_string() }
1094 , [nodes[0].node.get_our_node_id()], 100000);
1096 assert!(txn[0].output[0].script_pubkey.is_v0_p2wpkh());
1097 assert!(txn[0].output[1].script_pubkey.is_v0_p2wpkh());
1099 let events = nodes[1].node.get_and_clear_pending_msg_events();
1100 assert_eq!(events.len(), 1);
1102 MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
1103 assert_eq!(msg.contents.flags & 2, 2);
1105 _ => panic!("Unexpected event"),
1111 fn test_closing_signed_reinit_timeout() {
1112 do_test_closing_signed_reinit_timeout(TimeoutStep::AfterShutdown);
1113 do_test_closing_signed_reinit_timeout(TimeoutStep::AfterClosingSigned);
1114 do_test_closing_signed_reinit_timeout(TimeoutStep::NoTimeout);
1117 fn do_simple_legacy_shutdown_test(high_initiator_fee: bool) {
1118 // A simpe test of the legacy shutdown fee negotiation logic.
1119 let chanmon_cfgs = create_chanmon_cfgs(2);
1120 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1121 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1122 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1124 let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
1126 if high_initiator_fee {
1127 // If high_initiator_fee is set, set nodes[0]'s feerate significantly higher. This
1128 // shouldn't impact the flow at all given nodes[1] will happily accept the higher fee.
1129 let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
1130 *feerate_lock *= 10;
1133 nodes[0].node.close_channel(&OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id(), &nodes[1].node.get_our_node_id()).unwrap();
1134 let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
1135 nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_shutdown);
1136 let node_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
1137 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_shutdown);
1139 let mut node_0_closing_signed = get_event_msg!(nodes[0], MessageSendEvent::SendClosingSigned, nodes[1].node.get_our_node_id());
1140 node_0_closing_signed.fee_range = None;
1141 if high_initiator_fee {
1142 assert!(node_0_closing_signed.fee_satoshis > 500);
1144 assert!(node_0_closing_signed.fee_satoshis < 500);
1147 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed);
1148 let (_, mut node_1_closing_signed) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
1149 node_1_closing_signed.as_mut().unwrap().fee_range = None;
1151 nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed.unwrap());
1152 let (_, node_0_none) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
1153 assert!(node_0_none.is_none());
1154 check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
1155 check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure, [nodes[0].node.get_our_node_id()], 100000);
1159 fn simple_legacy_shutdown_test() {
1160 do_simple_legacy_shutdown_test(false);
1161 do_simple_legacy_shutdown_test(true);
1165 fn simple_target_feerate_shutdown() {
1166 // Simple test of target in `close_channel_with_target_feerate`.
1167 let chanmon_cfgs = create_chanmon_cfgs(2);
1168 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1169 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1170 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1172 let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
1173 let chan_id = OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id();
1175 nodes[0].node.close_channel_with_feerate_and_script(&chan_id, &nodes[1].node.get_our_node_id(), Some(253 * 10), None).unwrap();
1176 let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
1177 nodes[1].node.close_channel_with_feerate_and_script(&chan_id, &nodes[0].node.get_our_node_id(), Some(253 * 5), None).unwrap();
1178 let node_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
1180 nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_shutdown);
1181 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_shutdown);
1183 let node_0_closing_signed = get_event_msg!(nodes[0], MessageSendEvent::SendClosingSigned, nodes[1].node.get_our_node_id());
1184 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed);
1185 let (_, node_1_closing_signed_opt) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
1186 let node_1_closing_signed = node_1_closing_signed_opt.unwrap();
1188 // nodes[1] was passed a target which was larger than the current channel feerate, which it
1189 // should ignore in favor of the channel fee, as there is no use demanding a minimum higher
1190 // than what will be paid on a force-close transaction. Note that we have to consider rounding,
1191 // so only check that we're within 10 sats.
1192 assert!(node_0_closing_signed.fee_range.as_ref().unwrap().min_fee_satoshis >=
1193 node_1_closing_signed.fee_range.as_ref().unwrap().min_fee_satoshis * 10 - 5);
1194 assert!(node_0_closing_signed.fee_range.as_ref().unwrap().min_fee_satoshis <=
1195 node_1_closing_signed.fee_range.as_ref().unwrap().min_fee_satoshis * 10 + 5);
1197 // Further, because nodes[0]'s target fee is larger than the `Normal` fee estimation plus our
1198 // force-closure-avoidance buffer, min should equal max, and the nodes[1]-selected fee should
1199 // be the nodes[0] only available fee.
1200 assert_eq!(node_0_closing_signed.fee_range.as_ref().unwrap().min_fee_satoshis,
1201 node_0_closing_signed.fee_range.as_ref().unwrap().max_fee_satoshis);
1202 assert_eq!(node_0_closing_signed.fee_range.as_ref().unwrap().min_fee_satoshis,
1203 node_0_closing_signed.fee_satoshis);
1204 assert_eq!(node_0_closing_signed.fee_satoshis, node_1_closing_signed.fee_satoshis);
1206 nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed);
1207 let (_, node_0_none) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
1208 assert!(node_0_none.is_none());
1209 check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
1210 check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure, [nodes[0].node.get_our_node_id()], 100000);