Merge pull request #2966 from G8XSU/2647-distribute
[rust-lightning] / lightning / src / ln / async_signer_tests.rs
1 // This file is Copyright its original authors, visible in version control
2 // history.
3 //
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
8 // licenses.
9
10 //! Tests for asynchronous signing. These tests verify that the channel state machine behaves
11 //! properly with a signer implementation that asynchronously derives signatures.
12
13 use bitcoin::{Transaction, TxOut, TxIn, Amount};
14 use bitcoin::blockdata::locktime::absolute::LockTime;
15 use bitcoin::transaction::Version;
16
17 use crate::chain::channelmonitor::LATENCY_GRACE_PERIOD_BLOCKS;
18 use crate::events::bump_transaction::WalletSource;
19 use crate::events::{Event, MessageSendEvent, MessageSendEventsProvider, ClosureReason};
20 use crate::ln::functional_test_utils::*;
21 use crate::ln::msgs::ChannelMessageHandler;
22 use crate::ln::channelmanager::{PaymentId, RecipientOnionFields};
23 use crate::util::test_channel_signer::SignerOp;
24
25 #[test]
26 fn test_async_commitment_signature_for_funding_created() {
27         // Simulate acquiring the signature for `funding_created` asynchronously.
28         let chanmon_cfgs = create_chanmon_cfgs(2);
29         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
30         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
31         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
32
33         nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None, None).unwrap();
34
35         // nodes[0] --- open_channel --> nodes[1]
36         let mut open_chan_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
37         nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_chan_msg);
38
39         // nodes[0] <-- accept_channel --- nodes[1]
40         nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id()));
41
42         // nodes[0] --- funding_created --> nodes[1]
43         //
44         // But! Let's make node[0]'s signer be unavailable: we should *not* broadcast a funding_created
45         // message...
46         let (temporary_channel_id, tx, _) = create_funding_transaction(&nodes[0], &nodes[1].node.get_our_node_id(), 100000, 42);
47         nodes[0].disable_channel_signer_op(&nodes[1].node.get_our_node_id(), &temporary_channel_id, SignerOp::SignCounterpartyCommitment);
48         nodes[0].node.funding_transaction_generated(&temporary_channel_id, &nodes[1].node.get_our_node_id(), tx.clone()).unwrap();
49         check_added_monitors(&nodes[0], 0);
50
51         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
52
53         // Now re-enable the signer and simulate a retry. The temporary_channel_id won't work anymore so
54         // we have to dig out the real channel ID.
55         let chan_id = {
56                 let channels = nodes[0].node.list_channels();
57                 assert_eq!(channels.len(), 1, "expected one channel, not {}", channels.len());
58                 channels[0].channel_id
59         };
60
61         nodes[0].enable_channel_signer_op(&nodes[1].node.get_our_node_id(), &chan_id, SignerOp::SignCounterpartyCommitment);
62         nodes[0].node.signer_unblocked(Some((nodes[1].node.get_our_node_id(), chan_id)));
63
64         let mut funding_created_msg = get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, nodes[1].node.get_our_node_id());
65         nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &funding_created_msg);
66         check_added_monitors(&nodes[1], 1);
67         expect_channel_pending_event(&nodes[1], &nodes[0].node.get_our_node_id());
68
69         // nodes[0] <-- funding_signed --- nodes[1]
70         let funding_signed_msg = get_event_msg!(nodes[1], MessageSendEvent::SendFundingSigned, nodes[0].node.get_our_node_id());
71         nodes[0].node.handle_funding_signed(&nodes[1].node.get_our_node_id(), &funding_signed_msg);
72         check_added_monitors(&nodes[0], 1);
73         expect_channel_pending_event(&nodes[0], &nodes[1].node.get_our_node_id());
74 }
75
76 #[test]
77 fn test_async_commitment_signature_for_funding_signed() {
78         // Simulate acquiring the signature for `funding_signed` asynchronously.
79         let chanmon_cfgs = create_chanmon_cfgs(2);
80         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
81         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
82         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
83
84         nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None, None).unwrap();
85
86         // nodes[0] --- open_channel --> nodes[1]
87         let mut open_chan_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
88         nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_chan_msg);
89
90         // nodes[0] <-- accept_channel --- nodes[1]
91         nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id()));
92
93         // nodes[0] --- funding_created --> nodes[1]
94         let (temporary_channel_id, tx, _) = create_funding_transaction(&nodes[0], &nodes[1].node.get_our_node_id(), 100000, 42);
95         nodes[0].node.funding_transaction_generated(&temporary_channel_id, &nodes[1].node.get_our_node_id(), tx.clone()).unwrap();
96         check_added_monitors(&nodes[0], 0);
97
98         let mut funding_created_msg = get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, nodes[1].node.get_our_node_id());
99
100         // Now let's make node[1]'s signer be unavailable while handling the `funding_created`. It should
101         // *not* broadcast a `funding_signed`...
102         nodes[1].disable_channel_signer_op(&nodes[0].node.get_our_node_id(), &temporary_channel_id, SignerOp::SignCounterpartyCommitment);
103         nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &funding_created_msg);
104         check_added_monitors(&nodes[1], 1);
105
106         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
107
108         // Now re-enable the signer and simulate a retry. The temporary_channel_id won't work anymore so
109         // we have to dig out the real channel ID.
110         let chan_id = {
111                 let channels = nodes[0].node.list_channels();
112                 assert_eq!(channels.len(), 1, "expected one channel, not {}", channels.len());
113                 channels[0].channel_id
114         };
115         nodes[1].enable_channel_signer_op(&nodes[0].node.get_our_node_id(), &chan_id, SignerOp::SignCounterpartyCommitment);
116         nodes[1].node.signer_unblocked(Some((nodes[0].node.get_our_node_id(), chan_id)));
117
118         expect_channel_pending_event(&nodes[1], &nodes[0].node.get_our_node_id());
119
120         // nodes[0] <-- funding_signed --- nodes[1]
121         let funding_signed_msg = get_event_msg!(nodes[1], MessageSendEvent::SendFundingSigned, nodes[0].node.get_our_node_id());
122         nodes[0].node.handle_funding_signed(&nodes[1].node.get_our_node_id(), &funding_signed_msg);
123         check_added_monitors(&nodes[0], 1);
124         expect_channel_pending_event(&nodes[0], &nodes[1].node.get_our_node_id());
125 }
126
127 #[test]
128 fn test_async_commitment_signature_for_commitment_signed() {
129         let chanmon_cfgs = create_chanmon_cfgs(2);
130         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
131         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
132         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
133         let (_, _, chan_id, _) = create_announced_chan_between_nodes(&nodes, 0, 1);
134
135         // Send a payment.
136         let src = &nodes[0];
137         let dst = &nodes[1];
138         let (route, our_payment_hash, _our_payment_preimage, our_payment_secret) = get_route_and_payment_hash!(src, dst, 8000000);
139         src.node.send_payment_with_route(&route, our_payment_hash,
140                 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
141         check_added_monitors!(src, 1);
142
143         // Pass the payment along the route.
144         let payment_event = {
145                 let mut events = src.node.get_and_clear_pending_msg_events();
146                 assert_eq!(events.len(), 1);
147                 SendEvent::from_event(events.remove(0))
148         };
149         assert_eq!(payment_event.node_id, dst.node.get_our_node_id());
150         assert_eq!(payment_event.msgs.len(), 1);
151
152         dst.node.handle_update_add_htlc(&src.node.get_our_node_id(), &payment_event.msgs[0]);
153
154         // Mark dst's signer as unavailable and handle src's commitment_signed: while dst won't yet have a
155         // `commitment_signed` of its own to offer, it should publish a `revoke_and_ack`.
156         dst.disable_channel_signer_op(&src.node.get_our_node_id(), &chan_id, SignerOp::SignCounterpartyCommitment);
157         dst.node.handle_commitment_signed(&src.node.get_our_node_id(), &payment_event.commitment_msg);
158         check_added_monitors(dst, 1);
159
160         get_event_msg!(dst, MessageSendEvent::SendRevokeAndACK, src.node.get_our_node_id());
161
162         // Mark dst's signer as available and retry: we now expect to see dst's `commitment_signed`.
163         dst.enable_channel_signer_op(&src.node.get_our_node_id(), &chan_id, SignerOp::SignCounterpartyCommitment);
164         dst.node.signer_unblocked(Some((src.node.get_our_node_id(), chan_id)));
165
166         let events = dst.node.get_and_clear_pending_msg_events();
167         assert_eq!(events.len(), 1, "expected one message, got {}", events.len());
168         if let MessageSendEvent::UpdateHTLCs { ref node_id, .. } = events[0] {
169                 assert_eq!(node_id, &src.node.get_our_node_id());
170         } else {
171                 panic!("expected UpdateHTLCs message, not {:?}", events[0]);
172         };
173 }
174
175 #[test]
176 fn test_async_commitment_signature_for_funding_signed_0conf() {
177         // Simulate acquiring the signature for `funding_signed` asynchronously for a zero-conf channel.
178         let mut manually_accept_config = test_default_channel_config();
179         manually_accept_config.manually_accept_inbound_channels = true;
180
181         let chanmon_cfgs = create_chanmon_cfgs(2);
182         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
183         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(manually_accept_config)]);
184         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
185
186         // nodes[0] --- open_channel --> nodes[1]
187         nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None, None).unwrap();
188         let open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
189
190         nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel);
191
192         {
193                 let events = nodes[1].node.get_and_clear_pending_events();
194                 assert_eq!(events.len(), 1, "Expected one event, got {}", events.len());
195                 match &events[0] {
196                         Event::OpenChannelRequest { temporary_channel_id, .. } => {
197                                 nodes[1].node.accept_inbound_channel_from_trusted_peer_0conf(
198                                         temporary_channel_id, &nodes[0].node.get_our_node_id(), 0)
199                                         .expect("Unable to accept inbound zero-conf channel");
200                         },
201                         ev => panic!("Expected OpenChannelRequest, not {:?}", ev)
202                 }
203         }
204
205         // nodes[0] <-- accept_channel --- nodes[1]
206         let accept_channel = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
207         assert_eq!(accept_channel.common_fields.minimum_depth, 0, "Expected minimum depth of 0");
208         nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &accept_channel);
209
210         // nodes[0] --- funding_created --> nodes[1]
211         let (temporary_channel_id, tx, _) = create_funding_transaction(&nodes[0], &nodes[1].node.get_our_node_id(), 100000, 42);
212         nodes[0].node.funding_transaction_generated(&temporary_channel_id, &nodes[1].node.get_our_node_id(), tx.clone()).unwrap();
213         check_added_monitors(&nodes[0], 0);
214
215         let mut funding_created_msg = get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, nodes[1].node.get_our_node_id());
216
217         // Now let's make node[1]'s signer be unavailable while handling the `funding_created`. It should
218         // *not* broadcast a `funding_signed`...
219         nodes[1].disable_channel_signer_op(&nodes[0].node.get_our_node_id(), &temporary_channel_id, SignerOp::SignCounterpartyCommitment);
220         nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &funding_created_msg);
221         check_added_monitors(&nodes[1], 1);
222
223         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
224
225         // Now re-enable the signer and simulate a retry. The temporary_channel_id won't work anymore so
226         // we have to dig out the real channel ID.
227         let chan_id = {
228                 let channels = nodes[0].node.list_channels();
229                 assert_eq!(channels.len(), 1, "expected one channel, not {}", channels.len());
230                 channels[0].channel_id
231         };
232
233         // At this point, we basically expect the channel to open like a normal zero-conf channel.
234         nodes[1].enable_channel_signer_op(&nodes[0].node.get_our_node_id(), &chan_id, SignerOp::SignCounterpartyCommitment);
235         nodes[1].node.signer_unblocked(Some((nodes[0].node.get_our_node_id(), chan_id)));
236
237         let (funding_signed, channel_ready_1) = {
238                 let events = nodes[1].node.get_and_clear_pending_msg_events();
239                 assert_eq!(events.len(), 2);
240                 let funding_signed = match &events[0] {
241                         MessageSendEvent::SendFundingSigned { msg, .. } => msg.clone(),
242                         ev => panic!("Expected SendFundingSigned, not {:?}", ev)
243                 };
244                 let channel_ready = match &events[1] {
245                         MessageSendEvent::SendChannelReady { msg, .. } => msg.clone(),
246                         ev => panic!("Expected SendChannelReady, not {:?}", ev)
247                 };
248                 (funding_signed, channel_ready)
249         };
250
251         nodes[0].node.handle_funding_signed(&nodes[1].node.get_our_node_id(), &funding_signed);
252         expect_channel_pending_event(&nodes[0], &nodes[1].node.get_our_node_id());
253         expect_channel_pending_event(&nodes[1], &nodes[0].node.get_our_node_id());
254         check_added_monitors(&nodes[0], 1);
255
256         let channel_ready_0 = get_event_msg!(nodes[0], MessageSendEvent::SendChannelReady, nodes[1].node.get_our_node_id());
257
258         nodes[0].node.handle_channel_ready(&nodes[1].node.get_our_node_id(), &channel_ready_1);
259         expect_channel_ready_event(&nodes[0], &nodes[1].node.get_our_node_id());
260
261         nodes[1].node.handle_channel_ready(&nodes[0].node.get_our_node_id(), &channel_ready_0);
262         expect_channel_ready_event(&nodes[1], &nodes[0].node.get_our_node_id());
263
264         let channel_update_0 = get_event_msg!(nodes[0], MessageSendEvent::SendChannelUpdate, nodes[1].node.get_our_node_id());
265         let channel_update_1 = get_event_msg!(nodes[1], MessageSendEvent::SendChannelUpdate, nodes[0].node.get_our_node_id());
266
267         nodes[0].node.handle_channel_update(&nodes[1].node.get_our_node_id(), &channel_update_1);
268         nodes[1].node.handle_channel_update(&nodes[0].node.get_our_node_id(), &channel_update_0);
269
270         assert_eq!(nodes[0].node.list_usable_channels().len(), 1);
271         assert_eq!(nodes[1].node.list_usable_channels().len(), 1);
272 }
273
274 #[test]
275 fn test_async_commitment_signature_for_peer_disconnect() {
276         let chanmon_cfgs = create_chanmon_cfgs(2);
277         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
278         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
279         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
280         let (_, _, chan_id, _) = create_announced_chan_between_nodes(&nodes, 0, 1);
281
282         // Send a payment.
283         let src = &nodes[0];
284         let dst = &nodes[1];
285         let (route, our_payment_hash, _our_payment_preimage, our_payment_secret) = get_route_and_payment_hash!(src, dst, 8000000);
286         src.node.send_payment_with_route(&route, our_payment_hash,
287                 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
288         check_added_monitors!(src, 1);
289
290         // Pass the payment along the route.
291         let payment_event = {
292                 let mut events = src.node.get_and_clear_pending_msg_events();
293                 assert_eq!(events.len(), 1);
294                 SendEvent::from_event(events.remove(0))
295         };
296         assert_eq!(payment_event.node_id, dst.node.get_our_node_id());
297         assert_eq!(payment_event.msgs.len(), 1);
298
299         dst.node.handle_update_add_htlc(&src.node.get_our_node_id(), &payment_event.msgs[0]);
300
301         // Mark dst's signer as unavailable and handle src's commitment_signed: while dst won't yet have a
302         // `commitment_signed` of its own to offer, it should publish a `revoke_and_ack`.
303         dst.disable_channel_signer_op(&src.node.get_our_node_id(), &chan_id, SignerOp::SignCounterpartyCommitment);
304         dst.node.handle_commitment_signed(&src.node.get_our_node_id(), &payment_event.commitment_msg);
305         check_added_monitors(dst, 1);
306
307         get_event_msg!(dst, MessageSendEvent::SendRevokeAndACK, src.node.get_our_node_id());
308
309         // Now disconnect and reconnect the peers.
310         src.node.peer_disconnected(&dst.node.get_our_node_id());
311         dst.node.peer_disconnected(&src.node.get_our_node_id());
312         let mut reconnect_args = ReconnectArgs::new(&nodes[0], &nodes[1]);
313         reconnect_args.send_channel_ready = (false, false);
314         reconnect_args.pending_raa = (true, false);
315         reconnect_nodes(reconnect_args);
316
317         // Mark dst's signer as available and retry: we now expect to see dst's `commitment_signed`.
318         dst.enable_channel_signer_op(&src.node.get_our_node_id(), &chan_id, SignerOp::SignCounterpartyCommitment);
319         dst.node.signer_unblocked(Some((src.node.get_our_node_id(), chan_id)));
320
321         {
322                 let events = dst.node.get_and_clear_pending_msg_events();
323                 assert_eq!(events.len(), 1, "expected one message, got {}", events.len());
324                 if let MessageSendEvent::UpdateHTLCs { ref node_id, .. } = events[0] {
325                         assert_eq!(node_id, &src.node.get_our_node_id());
326                 } else {
327                         panic!("expected UpdateHTLCs message, not {:?}", events[0]);
328                 };
329         }
330 }
331
332 fn do_test_async_holder_signatures(anchors: bool, remote_commitment: bool) {
333         // Ensures that we can obtain holder signatures for commitment and HTLC transactions
334         // asynchronously by allowing their retrieval to fail and retrying via
335         // `ChannelMonitor::signer_unblocked`.
336         let mut config = test_default_channel_config();
337         if anchors {
338                 config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
339                 config.manually_accept_inbound_channels = true;
340         }
341
342         let chanmon_cfgs = create_chanmon_cfgs(2);
343         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
344         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(config), Some(config)]);
345         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
346
347         let closing_node = if remote_commitment { &nodes[1] } else { &nodes[0] };
348         let coinbase_tx = Transaction {
349                 version: Version::TWO,
350                 lock_time: LockTime::ZERO,
351                 input: vec![TxIn { ..Default::default() }],
352                 output: vec![
353                         TxOut {
354                                 value: Amount::ONE_BTC,
355                                 script_pubkey: closing_node.wallet_source.get_change_script().unwrap(),
356                         },
357                 ],
358         };
359         if anchors {
360                 *nodes[0].fee_estimator.sat_per_kw.lock().unwrap() *= 2;
361                 *nodes[1].fee_estimator.sat_per_kw.lock().unwrap() *= 2;
362                 closing_node.wallet_source.add_utxo(bitcoin::OutPoint { txid: coinbase_tx.txid(), vout: 0 }, coinbase_tx.output[0].value);
363         }
364
365         // Route an HTLC and set the signer as unavailable.
366         let (_, _, chan_id, funding_tx) = create_announced_chan_between_nodes(&nodes, 0, 1);
367         route_payment(&nodes[0], &[&nodes[1]], 1_000_000);
368         let error_message = "Channel force-closed";
369
370
371         if remote_commitment {
372                 // Make the counterparty broadcast its latest commitment.
373                 nodes[1].node.force_close_broadcasting_latest_txn(&chan_id, &nodes[0].node.get_our_node_id(), error_message.to_string()).unwrap();
374                 check_added_monitors(&nodes[1], 1);
375                 check_closed_broadcast(&nodes[1], 1, true);
376                 check_closed_event(&nodes[1], 1, ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(true) }, false, &[nodes[0].node.get_our_node_id()], 100_000);
377         } else {
378                 nodes[0].disable_channel_signer_op(&nodes[1].node.get_our_node_id(), &chan_id, SignerOp::SignHolderCommitment);
379                 nodes[0].disable_channel_signer_op(&nodes[1].node.get_our_node_id(), &chan_id, SignerOp::SignHolderHtlcTransaction);
380                 // We'll connect blocks until the sender has to go onchain to time out the HTLC.
381                 connect_blocks(&nodes[0], TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS + 1);
382
383                 // No transaction should be broadcast since the signer is not available yet.
384                 assert!(nodes[0].tx_broadcaster.txn_broadcast().is_empty());
385                 assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
386
387                 // Mark it as available now, we should see the signed commitment transaction.
388                 nodes[0].enable_channel_signer_op(&nodes[1].node.get_our_node_id(), &chan_id, SignerOp::SignHolderCommitment);
389                 nodes[0].enable_channel_signer_op(&nodes[1].node.get_our_node_id(), &chan_id, SignerOp::SignHolderHtlcTransaction);
390                 get_monitor!(nodes[0], chan_id).signer_unblocked(nodes[0].tx_broadcaster, nodes[0].fee_estimator, &nodes[0].logger);
391         }
392
393         let commitment_tx = {
394                 let mut txn = closing_node.tx_broadcaster.txn_broadcast();
395                 if anchors || remote_commitment {
396                         assert_eq!(txn.len(), 1);
397                         check_spends!(txn[0], funding_tx);
398                         txn.remove(0)
399                 } else {
400                         assert_eq!(txn.len(), 2);
401                         if txn[0].input[0].previous_output.txid == funding_tx.txid() {
402                                 check_spends!(txn[0], funding_tx);
403                                 check_spends!(txn[1], txn[0]);
404                                 txn.remove(0)
405                         } else {
406                                 check_spends!(txn[1], funding_tx);
407                                 check_spends!(txn[0], txn[1]);
408                                 txn.remove(1)
409                         }
410                 }
411         };
412
413         // Mark it as unavailable again to now test the HTLC transaction. We'll mine the commitment such
414         // that the HTLC transaction is retried.
415         let sign_htlc_op = if remote_commitment {
416                 SignerOp::SignCounterpartyHtlcTransaction
417         } else {
418                 SignerOp::SignHolderHtlcTransaction
419         };
420         nodes[0].disable_channel_signer_op(&nodes[1].node.get_our_node_id(), &chan_id, SignerOp::SignHolderCommitment);
421         nodes[0].disable_channel_signer_op(&nodes[1].node.get_our_node_id(), &chan_id, sign_htlc_op);
422         mine_transaction(&nodes[0], &commitment_tx);
423
424         check_added_monitors(&nodes[0], 1);
425         check_closed_broadcast(&nodes[0], 1, true);
426         check_closed_event(&nodes[0], 1, ClosureReason::CommitmentTxConfirmed, false, &[nodes[1].node.get_our_node_id()], 100_000);
427
428         // If the counterparty broadcast its latest commitment, we need to mine enough blocks for the
429         // HTLC timeout.
430         if remote_commitment {
431                 connect_blocks(&nodes[0], TEST_FINAL_CLTV);
432         }
433
434         // No HTLC transaction should be broadcast as the signer is not available yet.
435         if anchors && !remote_commitment {
436                 handle_bump_htlc_event(&nodes[0], 1);
437         }
438         let txn = nodes[0].tx_broadcaster.txn_broadcast();
439         assert!(txn.is_empty(), "expected no transaction to be broadcast, got {:?}", txn);
440
441         // Mark it as available now, we should see the signed HTLC transaction.
442         nodes[0].enable_channel_signer_op(&nodes[1].node.get_our_node_id(), &chan_id, SignerOp::SignHolderCommitment);
443         nodes[0].enable_channel_signer_op(&nodes[1].node.get_our_node_id(), &chan_id, sign_htlc_op);
444         get_monitor!(nodes[0], chan_id).signer_unblocked(nodes[0].tx_broadcaster, nodes[0].fee_estimator, &nodes[0].logger);
445
446         if anchors && !remote_commitment {
447                 handle_bump_htlc_event(&nodes[0], 1);
448         }
449         {
450                 let txn = nodes[0].tx_broadcaster.txn_broadcast();
451                 assert_eq!(txn.len(), 1);
452                 check_spends!(txn[0], commitment_tx, coinbase_tx);
453         }
454 }
455
456 #[test]
457 fn test_async_holder_signatures_no_anchors() {
458         do_test_async_holder_signatures(false, false);
459 }
460
461 #[test]
462 fn test_async_holder_signatures_remote_commitment_no_anchors() {
463         do_test_async_holder_signatures(false, true);
464 }
465
466 #[test]
467 fn test_async_holder_signatures_anchors() {
468         do_test_async_holder_signatures(true, false);
469 }
470
471 #[test]
472 fn test_async_holder_signatures_remote_commitment_anchors() {
473         do_test_async_holder_signatures(true, true);
474 }