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::{MessageSendEvent, MessageSendEventsProvider, ClosureReason};
15 use crate::ln::channelmanager::{self, PaymentSendFailure, PaymentId, RecipientOnionFields, ChannelShutdownState, ChannelDetails};
16 use crate::routing::router::{PaymentParameters, get_route, RouteParameters};
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, ..) = 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 shutdown_on_unfunded_channel() {
258 // Test receiving a shutdown prior to 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);
264 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 1_000_000, 100_000, 0, None).unwrap();
265 let open_chan = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
267 // Create a dummy P2WPKH script
268 let script = Builder::new().push_int(0)
269 .push_slice(&[0; 20])
272 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &msgs::Shutdown {
273 channel_id: open_chan.temporary_channel_id, scriptpubkey: script,
275 check_closed_event!(nodes[0], 1, ClosureReason::CounterpartyCoopClosedUnfundedChannel, [nodes[1].node.get_our_node_id()], 1_000_000);
279 fn expect_channel_shutdown_state_with_force_closure() {
280 // Test sending a shutdown prior to channel_ready after funding generation
281 let chanmon_cfgs = create_chanmon_cfgs(2);
282 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
283 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
284 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
285 let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
287 expect_channel_shutdown_state!(nodes[0], chan_1.2, ChannelShutdownState::NotShuttingDown);
288 expect_channel_shutdown_state!(nodes[1], chan_1.2, ChannelShutdownState::NotShuttingDown);
290 nodes[1].node.force_close_broadcasting_latest_txn(&chan_1.2, &nodes[0].node.get_our_node_id()).unwrap();
291 check_closed_broadcast!(nodes[1], true);
292 check_added_monitors!(nodes[1], 1);
294 expect_channel_shutdown_state!(nodes[0], chan_1.2, ChannelShutdownState::NotShuttingDown);
295 assert!(nodes[1].node.list_channels().is_empty());
297 let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
298 assert_eq!(node_txn.len(), 1);
299 check_spends!(node_txn[0], chan_1.3);
300 mine_transaction(&nodes[0], &node_txn[0]);
301 check_added_monitors!(nodes[0], 1);
303 assert!(nodes[0].node.list_channels().is_empty());
304 assert!(nodes[1].node.list_channels().is_empty());
305 check_closed_broadcast!(nodes[0], true);
306 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
307 check_closed_event!(nodes[1], 1, ClosureReason::HolderForceClosed, [nodes[0].node.get_our_node_id()], 100000);
311 fn updates_shutdown_wait() {
312 // Test sending a shutdown with outstanding updates pending
313 let chanmon_cfgs = create_chanmon_cfgs(3);
314 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
315 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
316 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
317 let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
318 let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
319 let logger = test_utils::TestLogger::new();
320 let scorer = test_utils::TestScorer::new();
321 let keys_manager = test_utils::TestKeysInterface::new(&[0u8; 32], Network::Testnet);
322 let random_seed_bytes = keys_manager.get_secure_random_bytes();
324 let (payment_preimage_0, payment_hash_0, ..) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 100_000);
326 nodes[0].node.close_channel(&chan_1.2, &nodes[1].node.get_our_node_id()).unwrap();
327 let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
328 nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_shutdown);
329 let node_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
330 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_shutdown);
332 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
333 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
335 let (_, payment_hash, payment_secret) = get_payment_preimage_hash!(nodes[0]);
337 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();
338 let route_params = RouteParameters::from_payment_params_and_value(payment_params_1, 100_000);
339 let route_1 = get_route(&nodes[0].node.get_our_node_id(), &route_params,
340 &nodes[0].network_graph.read_only(), None, &logger, &scorer, &Default::default(), &random_seed_bytes).unwrap();
341 let payment_params_2 = PaymentParameters::from_node_id(nodes[0].node.get_our_node_id(),
342 TEST_FINAL_CLTV).with_bolt11_features(nodes[0].node.invoice_features()).unwrap();
343 let route_params = RouteParameters::from_payment_params_and_value(payment_params_2, 100_000);
344 let route_2 = get_route(&nodes[1].node.get_our_node_id(), &route_params,
345 &nodes[1].network_graph.read_only(), None, &logger, &scorer, &Default::default(), &random_seed_bytes).unwrap();
346 unwrap_send_err!(nodes[0].node.send_payment_with_route(&route_1, payment_hash,
347 RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)
348 ), true, APIError::ChannelUnavailable {..}, {});
349 unwrap_send_err!(nodes[1].node.send_payment_with_route(&route_2, payment_hash,
350 RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)
351 ), true, APIError::ChannelUnavailable {..}, {});
353 nodes[2].node.claim_funds(payment_preimage_0);
354 check_added_monitors!(nodes[2], 1);
355 expect_payment_claimed!(nodes[2], payment_hash_0, 100_000);
357 let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
358 assert!(updates.update_add_htlcs.is_empty());
359 assert!(updates.update_fail_htlcs.is_empty());
360 assert!(updates.update_fail_malformed_htlcs.is_empty());
361 assert!(updates.update_fee.is_none());
362 assert_eq!(updates.update_fulfill_htlcs.len(), 1);
363 nodes[1].node.handle_update_fulfill_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fulfill_htlcs[0]);
364 expect_payment_forwarded!(nodes[1], nodes[0], nodes[2], Some(1000), false, false);
365 check_added_monitors!(nodes[1], 1);
366 let updates_2 = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
367 commitment_signed_dance!(nodes[1], nodes[2], updates.commitment_signed, false);
369 assert!(updates_2.update_add_htlcs.is_empty());
370 assert!(updates_2.update_fail_htlcs.is_empty());
371 assert!(updates_2.update_fail_malformed_htlcs.is_empty());
372 assert!(updates_2.update_fee.is_none());
373 assert_eq!(updates_2.update_fulfill_htlcs.len(), 1);
374 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &updates_2.update_fulfill_htlcs[0]);
375 commitment_signed_dance!(nodes[0], nodes[1], updates_2.commitment_signed, false, true);
376 expect_payment_sent!(nodes[0], payment_preimage_0);
378 let node_0_closing_signed = get_event_msg!(nodes[0], MessageSendEvent::SendClosingSigned, nodes[1].node.get_our_node_id());
379 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed);
380 let node_1_closing_signed = get_event_msg!(nodes[1], MessageSendEvent::SendClosingSigned, nodes[0].node.get_our_node_id());
381 nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed);
382 let (_, node_0_2nd_closing_signed) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
383 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_2nd_closing_signed.unwrap());
384 let (_, node_1_none) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
385 assert!(node_1_none.is_none());
386 check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
387 check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure, [nodes[0].node.get_our_node_id()], 100000);
389 assert!(nodes[0].node.list_channels().is_empty());
391 assert_eq!(nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 1);
392 nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
393 close_channel(&nodes[1], &nodes[2], &chan_2.2, chan_2.3, true);
394 assert!(nodes[1].node.list_channels().is_empty());
395 assert!(nodes[2].node.list_channels().is_empty());
396 check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure, [nodes[2].node.get_our_node_id()], 100000);
397 check_closed_event!(nodes[2], 1, ClosureReason::CooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
401 fn htlc_fail_async_shutdown() {
402 // Test HTLCs fail if shutdown starts even if messages are delivered out-of-order
403 let chanmon_cfgs = create_chanmon_cfgs(3);
404 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
405 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
406 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
407 let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
408 let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
410 let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], 100000);
411 nodes[0].node.send_payment_with_route(&route, our_payment_hash,
412 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
413 check_added_monitors!(nodes[0], 1);
414 let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
415 assert_eq!(updates.update_add_htlcs.len(), 1);
416 assert!(updates.update_fulfill_htlcs.is_empty());
417 assert!(updates.update_fail_htlcs.is_empty());
418 assert!(updates.update_fail_malformed_htlcs.is_empty());
419 assert!(updates.update_fee.is_none());
421 nodes[1].node.close_channel(&chan_1.2, &nodes[0].node.get_our_node_id()).unwrap();
422 let node_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
423 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_shutdown);
424 let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
426 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
427 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &updates.commitment_signed);
428 check_added_monitors!(nodes[1], 1);
429 nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_shutdown);
430 commitment_signed_dance!(nodes[1], nodes[0], (), false, true, false, false);
432 let updates_2 = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
433 assert!(updates_2.update_add_htlcs.is_empty());
434 assert!(updates_2.update_fulfill_htlcs.is_empty());
435 assert_eq!(updates_2.update_fail_htlcs.len(), 1);
436 assert!(updates_2.update_fail_malformed_htlcs.is_empty());
437 assert!(updates_2.update_fee.is_none());
439 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &updates_2.update_fail_htlcs[0]);
440 commitment_signed_dance!(nodes[0], nodes[1], updates_2.commitment_signed, false, true);
442 expect_payment_failed_with_update!(nodes[0], our_payment_hash, false, chan_2.0.contents.short_channel_id, true);
444 let msg_events = nodes[0].node.get_and_clear_pending_msg_events();
445 assert_eq!(msg_events.len(), 1);
446 let node_0_closing_signed = match msg_events[0] {
447 MessageSendEvent::SendClosingSigned { ref node_id, ref msg } => {
448 assert_eq!(*node_id, nodes[1].node.get_our_node_id());
451 _ => panic!("Unexpected event"),
454 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
455 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed);
456 let node_1_closing_signed = get_event_msg!(nodes[1], MessageSendEvent::SendClosingSigned, nodes[0].node.get_our_node_id());
457 nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed);
458 let (_, node_0_2nd_closing_signed) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
459 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_2nd_closing_signed.unwrap());
460 let (_, node_1_none) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
461 assert!(node_1_none.is_none());
463 assert!(nodes[0].node.list_channels().is_empty());
465 assert_eq!(nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 1);
466 nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
467 close_channel(&nodes[1], &nodes[2], &chan_2.2, chan_2.3, true);
468 assert!(nodes[1].node.list_channels().is_empty());
469 assert!(nodes[2].node.list_channels().is_empty());
470 check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
471 check_closed_event!(nodes[1], 2, ClosureReason::CooperativeClosure, [nodes[0].node.get_our_node_id(), nodes[2].node.get_our_node_id()], 100000);
472 check_closed_event!(nodes[2], 1, ClosureReason::CooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
475 fn do_test_shutdown_rebroadcast(recv_count: u8) {
476 // Test that shutdown/closing_signed is re-sent on reconnect with a variable number of
477 // messages delivered prior to disconnect
478 let chanmon_cfgs = create_chanmon_cfgs(3);
479 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
480 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
481 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
482 let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
483 let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
485 let (payment_preimage, payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 100_000);
487 nodes[1].node.close_channel(&chan_1.2, &nodes[0].node.get_our_node_id()).unwrap();
488 let node_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
490 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_shutdown);
491 let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
493 nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_shutdown);
497 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
498 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
500 nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init {
501 features: nodes[1].node.init_features(), networks: None, remote_network_address: None
503 let node_0_reestablish = get_chan_reestablish_msgs!(nodes[0], nodes[1]).pop().unwrap();
504 nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
505 features: nodes[0].node.init_features(), networks: None, remote_network_address: None
507 let node_1_reestablish = get_chan_reestablish_msgs!(nodes[1], nodes[0]).pop().unwrap();
509 nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &node_0_reestablish);
510 let node_1_2nd_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
511 assert!(node_1_shutdown == node_1_2nd_shutdown);
513 nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &node_1_reestablish);
514 let node_0_2nd_shutdown = if recv_count > 0 {
515 let node_0_2nd_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
516 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_2nd_shutdown);
519 let node_0_chan_update = get_event_msg!(nodes[0], MessageSendEvent::SendChannelUpdate, nodes[1].node.get_our_node_id());
520 assert_eq!(node_0_chan_update.contents.flags & 2, 0); // "disabled" flag must not be set as we just reconnected.
521 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_2nd_shutdown);
522 get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id())
524 nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_2nd_shutdown);
526 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
527 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
529 nodes[2].node.claim_funds(payment_preimage);
530 check_added_monitors!(nodes[2], 1);
531 expect_payment_claimed!(nodes[2], payment_hash, 100_000);
533 let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
534 assert!(updates.update_add_htlcs.is_empty());
535 assert!(updates.update_fail_htlcs.is_empty());
536 assert!(updates.update_fail_malformed_htlcs.is_empty());
537 assert!(updates.update_fee.is_none());
538 assert_eq!(updates.update_fulfill_htlcs.len(), 1);
539 nodes[1].node.handle_update_fulfill_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fulfill_htlcs[0]);
540 expect_payment_forwarded!(nodes[1], nodes[0], nodes[2], Some(1000), false, false);
541 check_added_monitors!(nodes[1], 1);
542 let updates_2 = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
543 commitment_signed_dance!(nodes[1], nodes[2], updates.commitment_signed, false);
545 assert!(updates_2.update_add_htlcs.is_empty());
546 assert!(updates_2.update_fail_htlcs.is_empty());
547 assert!(updates_2.update_fail_malformed_htlcs.is_empty());
548 assert!(updates_2.update_fee.is_none());
549 assert_eq!(updates_2.update_fulfill_htlcs.len(), 1);
550 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &updates_2.update_fulfill_htlcs[0]);
551 commitment_signed_dance!(nodes[0], nodes[1], updates_2.commitment_signed, false, true);
552 expect_payment_sent!(nodes[0], payment_preimage);
554 let node_0_closing_signed = get_event_msg!(nodes[0], MessageSendEvent::SendClosingSigned, nodes[1].node.get_our_node_id());
556 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed);
557 let node_1_closing_signed = get_event_msg!(nodes[1], MessageSendEvent::SendClosingSigned, nodes[0].node.get_our_node_id());
558 nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed);
559 let (_, node_0_2nd_closing_signed) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
560 assert!(node_0_2nd_closing_signed.is_some());
563 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
564 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
566 nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
567 features: nodes[0].node.init_features(), networks: None, remote_network_address: None
569 let node_1_2nd_reestablish = get_chan_reestablish_msgs!(nodes[1], nodes[0]).pop().unwrap();
570 nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init {
571 features: nodes[1].node.init_features(), networks: None, remote_network_address: None
574 // If all closing_signeds weren't delivered we can just resume where we left off...
575 let node_0_2nd_reestablish = get_chan_reestablish_msgs!(nodes[0], nodes[1]).pop().unwrap();
577 nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &node_1_2nd_reestablish);
578 let node_0_msgs = nodes[0].node.get_and_clear_pending_msg_events();
579 assert_eq!(node_0_msgs.len(), 2);
580 let node_0_2nd_closing_signed = match node_0_msgs[1] {
581 MessageSendEvent::SendClosingSigned { ref msg, .. } => {
582 assert_eq!(node_0_closing_signed, *msg);
588 let node_0_3rd_shutdown = match node_0_msgs[0] {
589 MessageSendEvent::SendShutdown { ref msg, .. } => {
590 assert_eq!(node_0_2nd_shutdown, *msg);
595 assert!(node_0_2nd_shutdown == node_0_3rd_shutdown);
597 nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &node_0_2nd_reestablish);
598 let node_1_3rd_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
599 assert!(node_1_3rd_shutdown == node_1_2nd_shutdown);
601 nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_3rd_shutdown);
602 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
604 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_3rd_shutdown);
606 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_2nd_closing_signed);
607 let node_1_closing_signed = get_event_msg!(nodes[1], MessageSendEvent::SendClosingSigned, nodes[0].node.get_our_node_id());
608 nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed);
609 let (_, node_0_2nd_closing_signed) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
610 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_2nd_closing_signed.unwrap());
611 let (_, node_1_none) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
612 assert!(node_1_none.is_none());
613 check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure, [nodes[0].node.get_our_node_id()], 100000);
615 // If one node, however, received + responded with an identical closing_signed we end
616 // up erroring and node[0] will try to broadcast its own latest commitment transaction.
617 // There isn't really anything better we can do simply, but in the future we might
618 // explore storing a set of recently-closed channels that got disconnected during
619 // closing_signed and avoiding broadcasting local commitment txn for some timeout to
620 // give our counterparty enough time to (potentially) broadcast a cooperative closing
622 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
624 nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &node_1_2nd_reestablish);
625 let msg_events = nodes[0].node.get_and_clear_pending_msg_events();
626 assert_eq!(msg_events.len(), 1);
627 if let MessageSendEvent::HandleError { ref action, .. } = msg_events[0] {
629 &ErrorAction::SendErrorMessage { ref msg } => {
630 nodes[1].node.handle_error(&nodes[0].node.get_our_node_id(), &msg);
631 assert_eq!(msg.channel_id, chan_1.2);
633 _ => panic!("Unexpected event!"),
635 } else { panic!("Needed SendErrorMessage close"); }
637 // get_closing_signed_broadcast usually eats the BroadcastChannelUpdate for us and
638 // checks it, but in this case nodes[1] didn't ever get a chance to receive a
639 // closing_signed so we do it ourselves
640 check_closed_broadcast!(nodes[1], false);
641 check_added_monitors!(nodes[1], 1);
642 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())) }
643 , [nodes[0].node.get_our_node_id()], 100000);
646 assert!(nodes[0].node.list_channels().is_empty());
648 assert_eq!(nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 1);
649 nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
650 close_channel(&nodes[1], &nodes[2], &chan_2.2, chan_2.3, true);
651 assert!(nodes[1].node.list_channels().is_empty());
652 assert!(nodes[2].node.list_channels().is_empty());
653 check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
654 check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure, [nodes[2].node.get_our_node_id()], 100000);
655 check_closed_event!(nodes[2], 1, ClosureReason::CooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
659 fn test_shutdown_rebroadcast() {
660 do_test_shutdown_rebroadcast(0);
661 do_test_shutdown_rebroadcast(1);
662 do_test_shutdown_rebroadcast(2);
666 fn test_upfront_shutdown_script() {
667 // BOLT 2 : Option upfront shutdown script, if peer commit its closing_script at channel opening
668 // enforce it at shutdown message
670 let mut config = UserConfig::default();
671 config.channel_handshake_config.announced_channel = true;
672 config.channel_handshake_limits.force_announced_channel_preference = false;
673 config.channel_handshake_config.commit_upfront_shutdown_pubkey = false;
674 let user_cfgs = [None, Some(config), None];
675 let chanmon_cfgs = create_chanmon_cfgs(3);
676 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
677 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &user_cfgs);
678 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
680 // We test that in case of peer committing upfront to a script, if it changes at closing, we refuse to sign
681 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 2, 1000000, 1000000);
682 nodes[0].node.close_channel(&OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id(), &nodes[2].node.get_our_node_id()).unwrap();
683 let node_0_orig_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[2].node.get_our_node_id());
684 let mut node_0_shutdown = node_0_orig_shutdown.clone();
685 node_0_shutdown.scriptpubkey = Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script().to_p2sh();
686 // Test we enforce upfront_scriptpbukey if by providing a different one at closing that we warn
687 // the peer and ignore the message.
688 nodes[2].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_shutdown);
689 assert!(regex::Regex::new(r"Got shutdown request with a scriptpubkey \([A-Fa-f0-9]+\) which did not match their previous scriptpubkey.")
690 .unwrap().is_match(&check_warn_msg!(nodes[2], nodes[0].node.get_our_node_id(), chan.2)));
691 // This allows nodes[2] to retry the shutdown message, which should get a response:
692 nodes[2].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_orig_shutdown);
693 get_event_msg!(nodes[2], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
695 // We test that in case of peer committing upfront to a script, if it doesn't change at closing, we sign
696 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 2, 1000000, 1000000);
697 nodes[0].node.close_channel(&OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id(), &nodes[2].node.get_our_node_id()).unwrap();
698 let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[2].node.get_our_node_id());
699 // We test that in case of peer committing upfront to a script, if it oesn't change at closing, we sign
700 nodes[2].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_shutdown);
701 let events = nodes[2].node.get_and_clear_pending_msg_events();
702 assert_eq!(events.len(), 1);
704 MessageSendEvent::SendShutdown { node_id, .. } => { assert_eq!(node_id, nodes[0].node.get_our_node_id()) }
705 _ => panic!("Unexpected event"),
708 // We test that if case of peer non-signaling we don't enforce committed script at channel opening
709 *nodes[0].override_init_features.borrow_mut() = Some(nodes[0].node.init_features().clear_upfront_shutdown_script());
710 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 1000000);
711 nodes[0].node.close_channel(&OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id(), &nodes[1].node.get_our_node_id()).unwrap();
712 let node_1_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
713 nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_1_shutdown);
714 check_added_monitors!(nodes[1], 1);
715 let events = nodes[1].node.get_and_clear_pending_msg_events();
716 assert_eq!(events.len(), 1);
718 MessageSendEvent::SendShutdown { node_id, .. } => { assert_eq!(node_id, nodes[0].node.get_our_node_id()) }
719 _ => panic!("Unexpected event"),
722 // We test that if user opt-out, we provide a zero-length script at channel opening and we are able to close
723 // channel smoothly, opt-out is from channel initiator here
724 *nodes[0].override_init_features.borrow_mut() = None;
725 let chan = create_announced_chan_between_nodes_with_value(&nodes, 1, 0, 1000000, 1000000);
726 nodes[1].node.close_channel(&OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id(), &nodes[0].node.get_our_node_id()).unwrap();
727 check_added_monitors!(nodes[1], 1);
728 let node_0_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
729 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_0_shutdown);
730 let events = nodes[0].node.get_and_clear_pending_msg_events();
731 assert_eq!(events.len(), 1);
733 MessageSendEvent::SendShutdown { node_id, .. } => { assert_eq!(node_id, nodes[1].node.get_our_node_id()) }
734 _ => panic!("Unexpected event"),
737 //// We test that if user opt-out, we provide a zero-length script at channel opening and we are able to close
738 //// channel smoothly
739 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 1000000);
740 nodes[1].node.close_channel(&OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id(), &nodes[0].node.get_our_node_id()).unwrap();
741 check_added_monitors!(nodes[1], 1);
742 let node_0_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
743 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_0_shutdown);
744 let events = nodes[0].node.get_and_clear_pending_msg_events();
745 assert_eq!(events.len(), 2);
747 MessageSendEvent::SendShutdown { node_id, .. } => { assert_eq!(node_id, nodes[1].node.get_our_node_id()) }
748 _ => panic!("Unexpected event"),
751 MessageSendEvent::SendClosingSigned { node_id, .. } => { assert_eq!(node_id, nodes[1].node.get_our_node_id()) }
752 _ => panic!("Unexpected event"),
757 fn test_unsupported_anysegwit_upfront_shutdown_script() {
758 let chanmon_cfgs = create_chanmon_cfgs(2);
759 let mut node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
760 // Clear shutdown_anysegwit on initiator
761 *node_cfgs[0].override_init_features.borrow_mut() = Some(channelmanager::provided_init_features(&test_default_channel_config()).clear_shutdown_anysegwit());
762 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
763 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
765 // Use a non-v0 segwit script supported by option_shutdown_anysegwit
766 let anysegwit_shutdown_script = Builder::new()
768 .push_slice(&[0, 40])
771 // Check script when handling an open_channel message
772 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None).unwrap();
773 let mut open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
774 open_channel.shutdown_scriptpubkey = Some(anysegwit_shutdown_script.clone());
775 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel);
777 let events = nodes[1].node.get_and_clear_pending_msg_events();
778 assert_eq!(events.len(), 1);
780 MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg }, node_id } => {
781 assert_eq!(node_id, nodes[0].node.get_our_node_id());
782 assert_eq!(msg.data, "Peer is signaling upfront_shutdown but has provided an unacceptable scriptpubkey format: Script(OP_PUSHNUM_16 OP_PUSHBYTES_2 0028)");
784 _ => panic!("Unexpected event"),
787 let chanmon_cfgs = create_chanmon_cfgs(2);
788 let mut node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
789 // Clear shutdown_anysegwit on responder
790 *node_cfgs[1].override_init_features.borrow_mut() = Some(channelmanager::provided_init_features(&test_default_channel_config()).clear_shutdown_anysegwit());
791 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
792 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
794 // Check script when handling an accept_channel message
795 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None).unwrap();
796 let open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
797 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel);
798 let mut accept_channel = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
799 accept_channel.shutdown_scriptpubkey = Some(anysegwit_shutdown_script.clone());
800 nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &accept_channel);
802 let events = nodes[0].node.get_and_clear_pending_msg_events();
803 assert_eq!(events.len(), 1);
805 MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg }, node_id } => {
806 assert_eq!(node_id, nodes[1].node.get_our_node_id());
807 assert_eq!(msg.data, "Peer is signaling upfront_shutdown but has provided an unacceptable scriptpubkey format: Script(OP_PUSHNUM_16 OP_PUSHBYTES_2 0028)");
809 _ => panic!("Unexpected event"),
811 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() }
812 , [nodes[1].node.get_our_node_id()], 100000);
816 fn test_invalid_upfront_shutdown_script() {
817 let chanmon_cfgs = create_chanmon_cfgs(2);
818 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
819 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
820 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
822 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None).unwrap();
824 // Use a segwit v0 script with an unsupported witness program
825 let mut open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
826 open_channel.shutdown_scriptpubkey = Some(Builder::new().push_int(0)
829 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel);
831 let events = nodes[1].node.get_and_clear_pending_msg_events();
832 assert_eq!(events.len(), 1);
834 MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg }, node_id } => {
835 assert_eq!(node_id, nodes[0].node.get_our_node_id());
836 assert_eq!(msg.data, "Peer is signaling upfront_shutdown but has provided an unacceptable scriptpubkey format: Script(OP_0 OP_PUSHBYTES_2 0000)");
838 _ => panic!("Unexpected event"),
843 fn test_segwit_v0_shutdown_script() {
844 let mut config = UserConfig::default();
845 config.channel_handshake_config.announced_channel = true;
846 config.channel_handshake_limits.force_announced_channel_preference = false;
847 config.channel_handshake_config.commit_upfront_shutdown_pubkey = false;
848 let user_cfgs = [None, Some(config), None];
849 let chanmon_cfgs = create_chanmon_cfgs(3);
850 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
851 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &user_cfgs);
852 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
854 let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
855 nodes[1].node.close_channel(&OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id(), &nodes[0].node.get_our_node_id()).unwrap();
856 check_added_monitors!(nodes[1], 1);
858 // Use a segwit v0 script supported even without option_shutdown_anysegwit
859 let mut node_0_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
860 node_0_shutdown.scriptpubkey = Builder::new().push_int(0)
861 .push_slice(&[0; 20])
863 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_0_shutdown);
865 let events = nodes[0].node.get_and_clear_pending_msg_events();
866 assert_eq!(events.len(), 2);
868 MessageSendEvent::SendShutdown { node_id, .. } => { assert_eq!(node_id, nodes[1].node.get_our_node_id()) }
869 _ => panic!("Unexpected event"),
872 MessageSendEvent::SendClosingSigned { node_id, .. } => { assert_eq!(node_id, nodes[1].node.get_our_node_id()) }
873 _ => panic!("Unexpected event"),
878 fn test_anysegwit_shutdown_script() {
879 let mut config = UserConfig::default();
880 config.channel_handshake_config.announced_channel = true;
881 config.channel_handshake_limits.force_announced_channel_preference = false;
882 config.channel_handshake_config.commit_upfront_shutdown_pubkey = false;
883 let user_cfgs = [None, Some(config), None];
884 let chanmon_cfgs = create_chanmon_cfgs(3);
885 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
886 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &user_cfgs);
887 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
889 let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
890 nodes[1].node.close_channel(&OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id(), &nodes[0].node.get_our_node_id()).unwrap();
891 check_added_monitors!(nodes[1], 1);
893 // Use a non-v0 segwit script supported by option_shutdown_anysegwit
894 let mut node_0_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
895 node_0_shutdown.scriptpubkey = Builder::new().push_int(16)
898 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_0_shutdown);
900 let events = nodes[0].node.get_and_clear_pending_msg_events();
901 assert_eq!(events.len(), 2);
903 MessageSendEvent::SendShutdown { node_id, .. } => { assert_eq!(node_id, nodes[1].node.get_our_node_id()) }
904 _ => panic!("Unexpected event"),
907 MessageSendEvent::SendClosingSigned { node_id, .. } => { assert_eq!(node_id, nodes[1].node.get_our_node_id()) }
908 _ => panic!("Unexpected event"),
913 fn test_unsupported_anysegwit_shutdown_script() {
914 let mut config = UserConfig::default();
915 config.channel_handshake_config.announced_channel = true;
916 config.channel_handshake_limits.force_announced_channel_preference = false;
917 config.channel_handshake_config.commit_upfront_shutdown_pubkey = false;
918 let user_cfgs = [None, Some(config), None];
919 let chanmon_cfgs = create_chanmon_cfgs(3);
920 let mut node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
921 *node_cfgs[0].override_init_features.borrow_mut() = Some(channelmanager::provided_init_features(&config).clear_shutdown_anysegwit());
922 *node_cfgs[1].override_init_features.borrow_mut() = Some(channelmanager::provided_init_features(&config).clear_shutdown_anysegwit());
923 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &user_cfgs);
924 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
926 // Check that using an unsupported shutdown script fails and a supported one succeeds.
927 let supported_shutdown_script = chanmon_cfgs[1].keys_manager.get_shutdown_scriptpubkey().unwrap();
928 let unsupported_shutdown_script =
929 ShutdownScript::new_witness_program(WitnessVersion::V16, &[0, 40]).unwrap();
930 chanmon_cfgs[1].keys_manager
931 .expect(OnGetShutdownScriptpubkey { returns: unsupported_shutdown_script.clone() })
932 .expect(OnGetShutdownScriptpubkey { returns: supported_shutdown_script });
934 let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
935 match nodes[1].node.close_channel(&OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id(), &nodes[0].node.get_our_node_id()) {
936 Err(APIError::IncompatibleShutdownScript { script }) => {
937 assert_eq!(script.into_inner(), unsupported_shutdown_script.clone().into_inner());
939 Err(e) => panic!("Unexpected error: {:?}", e),
940 Ok(_) => panic!("Expected error"),
942 nodes[1].node.close_channel(&OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id(), &nodes[0].node.get_our_node_id()).unwrap();
943 check_added_monitors!(nodes[1], 1);
945 // Use a non-v0 segwit script unsupported without option_shutdown_anysegwit
946 let mut node_0_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
947 node_0_shutdown.scriptpubkey = unsupported_shutdown_script.into_inner();
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 (60020028) from remote peer");
955 fn test_invalid_shutdown_script() {
956 let mut config = UserConfig::default();
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 let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
967 nodes[1].node.close_channel(&OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id(), &nodes[0].node.get_our_node_id()).unwrap();
968 check_added_monitors!(nodes[1], 1);
970 // Use a segwit v0 script with an unsupported witness program
971 let mut node_0_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
972 node_0_shutdown.scriptpubkey = Builder::new().push_int(0)
975 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_0_shutdown);
977 assert_eq!(&check_warn_msg!(nodes[0], nodes[1].node.get_our_node_id(), chan.2),
978 "Got a nonstandard scriptpubkey (00020000) from remote peer");
982 fn test_user_shutdown_script() {
983 let mut config = test_default_channel_config();
984 config.channel_handshake_config.announced_channel = true;
985 config.channel_handshake_limits.force_announced_channel_preference = false;
986 config.channel_handshake_config.commit_upfront_shutdown_pubkey = 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.clone()).unwrap();
1000 let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
1001 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();
1002 check_added_monitors!(nodes[1], 1);
1004 let mut node_0_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
1006 assert_eq!(node_0_shutdown.scriptpubkey, script);
1010 fn test_already_set_user_shutdown_script() {
1011 let mut config = test_default_channel_config();
1012 config.channel_handshake_config.announced_channel = true;
1013 config.channel_handshake_limits.force_announced_channel_preference = false;
1014 let user_cfgs = [None, Some(config), None];
1015 let chanmon_cfgs = create_chanmon_cfgs(3);
1016 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
1017 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &user_cfgs);
1018 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
1020 // Segwit v0 script of the form OP_0 <20-byte hash>
1021 let script = Builder::new().push_int(0)
1022 .push_slice(&[0; 20])
1025 let shutdown_script = ShutdownScript::try_from(script).unwrap();
1027 let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
1028 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));
1030 assert_eq!(result, Err(APIError::APIMisuseError { err: "Cannot override shutdown script for a channel with one already set".to_string() }));
1033 #[derive(PartialEq)]
1040 fn do_test_closing_signed_reinit_timeout(timeout_step: TimeoutStep) {
1041 // The range-based closing signed negotiation allows the funder to restart the process with a
1042 // new range if the previous range did not overlap. This allows implementations to request user
1043 // intervention allowing users to enter a new fee range. We do not implement the sending side
1044 // of this, instead opting to allow users to enter an explicit "willing to pay up to X to avoid
1045 // force-closing" value and relying on that instead.
1047 // Here we run test the fundee side of that restart mechanism, implementing the funder side of
1049 let chanmon_cfgs = create_chanmon_cfgs(2);
1050 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1051 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1052 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1053 let chan_id = create_announced_chan_between_nodes(&nodes, 0, 1).2;
1055 send_payment(&nodes[0], &[&nodes[1]], 8_000_000);
1057 nodes[0].node.close_channel(&chan_id, &nodes[1].node.get_our_node_id()).unwrap();
1058 let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
1059 nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_shutdown);
1060 let node_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
1061 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_shutdown);
1064 // Now we set nodes[1] to require a relatively high feerate for closing. This should result
1065 // in it rejecting nodes[0]'s initial closing_signed, giving nodes[0] a chance to try
1067 let mut feerate_lock = chanmon_cfgs[1].fee_estimator.sat_per_kw.lock().unwrap();
1068 *feerate_lock *= 10;
1071 let mut node_0_closing_signed = get_event_msg!(nodes[0], MessageSendEvent::SendClosingSigned, nodes[1].node.get_our_node_id());
1072 // nodes[0] should use a "reasonable" feerate, well under the 10 sat/vByte that nodes[1] thinks
1073 // is the current prevailing feerate.
1074 assert!(node_0_closing_signed.fee_satoshis <= 500);
1076 if timeout_step != TimeoutStep::AfterShutdown {
1077 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed);
1078 assert!(check_warn_msg!(nodes[1], nodes[0].node.get_our_node_id(), chan_id)
1079 .starts_with("Unable to come to consensus about closing feerate"));
1081 // Now deliver a mutated closing_signed indicating a higher acceptable fee range, which
1082 // nodes[1] should happily accept and respond to.
1083 node_0_closing_signed.fee_range.as_mut().unwrap().max_fee_satoshis *= 10;
1085 let mut node_0_per_peer_lock;
1086 let mut node_0_peer_state_lock;
1087 get_channel_ref!(nodes[0], nodes[1], node_0_per_peer_lock, node_0_peer_state_lock, chan_id).context_mut().closing_fee_limits.as_mut().unwrap().1 *= 10;
1089 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed);
1090 let node_1_closing_signed = get_event_msg!(nodes[1], MessageSendEvent::SendClosingSigned, nodes[0].node.get_our_node_id());
1091 nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed);
1092 let node_0_2nd_closing_signed = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
1093 if timeout_step == TimeoutStep::NoTimeout {
1094 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_2nd_closing_signed.1.unwrap());
1095 check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure, [nodes[0].node.get_our_node_id()], 100000);
1097 check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
1100 if timeout_step != TimeoutStep::NoTimeout {
1101 assert!(nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().is_empty());
1103 assert_eq!(nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 1);
1106 nodes[1].node.timer_tick_occurred();
1107 nodes[1].node.timer_tick_occurred();
1109 let txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
1110 assert_eq!(txn.len(), 1);
1111 assert_eq!(txn[0].output.len(), 2);
1113 if timeout_step != TimeoutStep::NoTimeout {
1114 assert!((txn[0].output[0].script_pubkey.is_v0_p2wpkh() &&
1115 txn[0].output[1].script_pubkey.is_v0_p2wsh()) ||
1116 (txn[0].output[1].script_pubkey.is_v0_p2wpkh() &&
1117 txn[0].output[0].script_pubkey.is_v0_p2wsh()));
1118 check_closed_broadcast!(nodes[1], true);
1119 check_added_monitors!(nodes[1], 1);
1120 check_closed_event!(nodes[1], 1, ClosureReason::ProcessingError { err: "closing_signed negotiation failed to finish within two timer ticks".to_string() }
1121 , [nodes[0].node.get_our_node_id()], 100000);
1123 assert!(txn[0].output[0].script_pubkey.is_v0_p2wpkh());
1124 assert!(txn[0].output[1].script_pubkey.is_v0_p2wpkh());
1126 let events = nodes[1].node.get_and_clear_pending_msg_events();
1127 assert_eq!(events.len(), 1);
1129 MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
1130 assert_eq!(msg.contents.flags & 2, 2);
1132 _ => panic!("Unexpected event"),
1138 fn test_closing_signed_reinit_timeout() {
1139 do_test_closing_signed_reinit_timeout(TimeoutStep::AfterShutdown);
1140 do_test_closing_signed_reinit_timeout(TimeoutStep::AfterClosingSigned);
1141 do_test_closing_signed_reinit_timeout(TimeoutStep::NoTimeout);
1144 fn do_simple_legacy_shutdown_test(high_initiator_fee: bool) {
1145 // A simpe test of the legacy shutdown fee negotiation logic.
1146 let chanmon_cfgs = create_chanmon_cfgs(2);
1147 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1148 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1149 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1151 let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
1153 if high_initiator_fee {
1154 // If high_initiator_fee is set, set nodes[0]'s feerate significantly higher. This
1155 // shouldn't impact the flow at all given nodes[1] will happily accept the higher fee.
1156 let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
1157 *feerate_lock *= 10;
1160 nodes[0].node.close_channel(&OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id(), &nodes[1].node.get_our_node_id()).unwrap();
1161 let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
1162 nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_shutdown);
1163 let node_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
1164 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_shutdown);
1166 let mut node_0_closing_signed = get_event_msg!(nodes[0], MessageSendEvent::SendClosingSigned, nodes[1].node.get_our_node_id());
1167 node_0_closing_signed.fee_range = None;
1168 if high_initiator_fee {
1169 assert!(node_0_closing_signed.fee_satoshis > 500);
1171 assert!(node_0_closing_signed.fee_satoshis < 500);
1174 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed);
1175 let (_, mut node_1_closing_signed) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
1176 node_1_closing_signed.as_mut().unwrap().fee_range = None;
1178 nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed.unwrap());
1179 let (_, node_0_none) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
1180 assert!(node_0_none.is_none());
1181 check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
1182 check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure, [nodes[0].node.get_our_node_id()], 100000);
1186 fn simple_legacy_shutdown_test() {
1187 do_simple_legacy_shutdown_test(false);
1188 do_simple_legacy_shutdown_test(true);
1192 fn simple_target_feerate_shutdown() {
1193 // Simple test of target in `close_channel_with_target_feerate`.
1194 let chanmon_cfgs = create_chanmon_cfgs(2);
1195 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1196 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1197 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1199 let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
1200 let chan_id = OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id();
1202 nodes[0].node.close_channel_with_feerate_and_script(&chan_id, &nodes[1].node.get_our_node_id(), Some(253 * 10), None).unwrap();
1203 let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
1204 nodes[1].node.close_channel_with_feerate_and_script(&chan_id, &nodes[0].node.get_our_node_id(), Some(253 * 5), None).unwrap();
1205 let node_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
1207 nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_shutdown);
1208 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_shutdown);
1210 let node_0_closing_signed = get_event_msg!(nodes[0], MessageSendEvent::SendClosingSigned, nodes[1].node.get_our_node_id());
1211 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed);
1212 let (_, node_1_closing_signed_opt) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
1213 let node_1_closing_signed = node_1_closing_signed_opt.unwrap();
1215 // nodes[1] was passed a target which was larger than the current channel feerate, which it
1216 // should ignore in favor of the channel fee, as there is no use demanding a minimum higher
1217 // than what will be paid on a force-close transaction. Note that we have to consider rounding,
1218 // so only check that we're within 10 sats.
1219 assert!(node_0_closing_signed.fee_range.as_ref().unwrap().min_fee_satoshis >=
1220 node_1_closing_signed.fee_range.as_ref().unwrap().min_fee_satoshis * 10 - 5);
1221 assert!(node_0_closing_signed.fee_range.as_ref().unwrap().min_fee_satoshis <=
1222 node_1_closing_signed.fee_range.as_ref().unwrap().min_fee_satoshis * 10 + 5);
1224 // Further, because nodes[0]'s target fee is larger than the `Normal` fee estimation plus our
1225 // force-closure-avoidance buffer, min should equal max, and the nodes[1]-selected fee should
1226 // be the nodes[0] only available fee.
1227 assert_eq!(node_0_closing_signed.fee_range.as_ref().unwrap().min_fee_satoshis,
1228 node_0_closing_signed.fee_range.as_ref().unwrap().max_fee_satoshis);
1229 assert_eq!(node_0_closing_signed.fee_range.as_ref().unwrap().min_fee_satoshis,
1230 node_0_closing_signed.fee_satoshis);
1231 assert_eq!(node_0_closing_signed.fee_satoshis, node_1_closing_signed.fee_satoshis);
1233 nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed);
1234 let (_, node_0_none) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
1235 assert!(node_0_none.is_none());
1236 check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
1237 check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure, [nodes[0].node.get_our_node_id()], 100000);