lightning 0.2.2

A Complete Bitcoin Lightning Library in Rust. Handles the core functionality of the Lightning Network, allowing clients to implement custom wallet, chain interactions, storage and network logic without enforcing a specific runtime.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
use crate::chain::ChannelMonitorUpdateStatus;
use crate::events::{Event, HTLCHandlingFailureType};
use crate::ln::channel::DISCONNECT_PEER_AWAITING_RESPONSE_TICKS;
use crate::ln::channelmanager::PaymentId;
use crate::ln::channelmanager::RecipientOnionFields;
use crate::ln::functional_test_utils::*;
use crate::ln::msgs;
use crate::ln::msgs::{BaseMessageHandler, ChannelMessageHandler, ErrorAction, MessageSendEvent};
use crate::util::errors::APIError;
use crate::util::ser::Writeable;
use crate::util::test_channel_signer::SignerOp;

#[test]
fn test_quiescence_tie() {
	// Test that both nodes proposing quiescence at the same time results in the channel funder
	// becoming the quiescence initiator.
	let chanmon_cfgs = create_chanmon_cfgs(2);
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
	let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
	let chan_id = create_announced_chan_between_nodes(&nodes, 0, 1).2;

	nodes[0].node.maybe_propose_quiescence(&nodes[1].node.get_our_node_id(), &chan_id).unwrap();
	nodes[1].node.maybe_propose_quiescence(&nodes[0].node.get_our_node_id(), &chan_id).unwrap();

	let stfu_node_0 =
		get_event_msg!(nodes[0], MessageSendEvent::SendStfu, nodes[1].node.get_our_node_id());
	nodes[1].node.handle_stfu(nodes[0].node.get_our_node_id(), &stfu_node_0);

	let stfu_node_1 =
		get_event_msg!(nodes[1], MessageSendEvent::SendStfu, nodes[0].node.get_our_node_id());
	nodes[0].node.handle_stfu(nodes[1].node.get_our_node_id(), &stfu_node_1);

	assert!(stfu_node_0.initiator && stfu_node_1.initiator);

	assert!(nodes[0].node.exit_quiescence(&nodes[1].node.get_our_node_id(), &chan_id).unwrap());
	assert!(nodes[1].node.exit_quiescence(&nodes[0].node.get_our_node_id(), &chan_id).unwrap());
}

#[test]
fn test_quiescence_shutdown_ignored() {
	// Test that a shutdown sent/received during quiescence is ignored.
	let chanmon_cfgs = create_chanmon_cfgs(2);
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
	let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
	let chan_id = create_announced_chan_between_nodes(&nodes, 0, 1).2;

	nodes[0].node.maybe_propose_quiescence(&nodes[1].node.get_our_node_id(), &chan_id).unwrap();
	let _ = get_event_msg!(nodes[0], MessageSendEvent::SendStfu, nodes[1].node.get_our_node_id());

	if let Err(e) = nodes[0].node.close_channel(&chan_id, &nodes[1].node.get_our_node_id()) {
		assert_eq!(
			e,
			APIError::APIMisuseError { err: "Cannot begin shutdown while quiescent".to_owned() }
		);
	} else {
		panic!("Expected shutdown to be ignored while quiescent");
	}

	nodes[1].node.close_channel(&chan_id, &nodes[0].node.get_our_node_id()).unwrap();
	let shutdown =
		get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());

	nodes[0].node.handle_shutdown(nodes[1].node.get_our_node_id(), &shutdown);
	let msg_events = nodes[0].node.get_and_clear_pending_msg_events();
	match msg_events[0] {
		MessageSendEvent::HandleError {
			action: ErrorAction::DisconnectPeerWithWarning { ref msg, .. },
			..
		} => {
			assert_eq!(msg.data, "Got shutdown request while quiescent".to_owned());
		},
		_ => panic!(),
	}
}

#[test]
fn test_allow_shutdown_while_awaiting_quiescence() {
	allow_shutdown_while_awaiting_quiescence(false);
	allow_shutdown_while_awaiting_quiescence(true);
}

fn allow_shutdown_while_awaiting_quiescence(local_shutdown: bool) {
	// Test that a shutdown sent/received while we're still awaiting quiescence (stfu has not been
	// sent yet) is honored and the channel is closed cooperatively.
	let chanmon_cfgs = create_chanmon_cfgs(2);
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
	let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
	let chan_id = create_announced_chan_between_nodes(&nodes, 0, 1).2;

	let local_node = &nodes[0];
	let remote_node = &nodes[1];
	let local_node_id = local_node.node.get_our_node_id();
	let remote_node_id = remote_node.node.get_our_node_id();

	let payment_amount = 1_000_000;
	let (route, payment_hash, _, payment_secret) =
		get_route_and_payment_hash!(local_node, remote_node, payment_amount);
	let onion = RecipientOnionFields::secret_only(payment_secret);
	let payment_id = PaymentId(payment_hash.0);
	local_node.node.send_payment_with_route(route, payment_hash, onion, payment_id).unwrap();
	check_added_monitors!(local_node, 1);

	// Attempt to send an HTLC, but don't fully commit it yet.
	let update_add = get_htlc_update_msgs!(local_node, remote_node_id);
	remote_node.node.handle_update_add_htlc(local_node_id, &update_add.update_add_htlcs[0]);
	remote_node
		.node
		.handle_commitment_signed_batch_test(local_node_id, &update_add.commitment_signed);
	let (revoke_and_ack, commit_sig) = get_revoke_commit_msgs!(remote_node, local_node_id);
	local_node.node.handle_revoke_and_ack(remote_node_id, &revoke_and_ack);
	check_added_monitors(local_node, 1);

	// Request the local node to propose quiescence, and immediately try to close the channel. Since
	// we haven't sent `stfu` yet as the state machine is pending, we should forget about our
	// quiescence attempt.
	local_node.node.maybe_propose_quiescence(&remote_node_id, &chan_id).unwrap();
	assert!(local_node.node.get_and_clear_pending_msg_events().is_empty());

	let (closer_node, closee_node) =
		if local_shutdown { (local_node, remote_node) } else { (remote_node, local_node) };
	let closer_node_id = closer_node.node.get_our_node_id();
	let closee_node_id = closee_node.node.get_our_node_id();

	closer_node.node.close_channel(&chan_id, &closee_node_id).unwrap();
	check_added_monitors(&remote_node, 1);
	let shutdown_initiator =
		get_event_msg!(closer_node, MessageSendEvent::SendShutdown, closee_node_id);
	closee_node.node.handle_shutdown(closer_node_id, &shutdown_initiator);
	let shutdown_responder =
		get_event_msg!(closee_node, MessageSendEvent::SendShutdown, closer_node_id);
	closer_node.node.handle_shutdown(closee_node_id, &shutdown_responder);

	// Continue exchanging messages until the HTLC is irrevocably committed and eventually failed
	// back as we are shutting down.
	local_node.node.handle_commitment_signed_batch_test(remote_node_id, &commit_sig);
	check_added_monitors(local_node, 1);

	let last_revoke_and_ack =
		get_event_msg!(local_node, MessageSendEvent::SendRevokeAndACK, remote_node_id);
	remote_node.node.handle_revoke_and_ack(local_node_id, &last_revoke_and_ack);
	check_added_monitors(remote_node, 1);
	expect_and_process_pending_htlcs(remote_node, false);
	expect_htlc_handling_failed_destinations!(
		remote_node.node.get_and_clear_pending_events(),
		&[HTLCHandlingFailureType::Receive { payment_hash }]
	);
	check_added_monitors(remote_node, 1);

	let update_fail = get_htlc_update_msgs!(remote_node, local_node_id);
	local_node.node.handle_update_fail_htlc(remote_node_id, &update_fail.update_fail_htlcs[0]);
	local_node
		.node
		.handle_commitment_signed_batch_test(remote_node_id, &update_fail.commitment_signed);

	let (revoke_and_ack, commit_sig) = get_revoke_commit_msgs!(local_node, remote_node_id);
	remote_node.node.handle_revoke_and_ack(local_node_id, &revoke_and_ack);
	check_added_monitors(remote_node, 1);
	remote_node.node.handle_commitment_signed_batch_test(local_node_id, &commit_sig);
	check_added_monitors(remote_node, 1);

	let last_revoke_and_ack =
		get_event_msg!(remote_node, MessageSendEvent::SendRevokeAndACK, local_node_id);
	local_node.node.handle_revoke_and_ack(remote_node_id, &last_revoke_and_ack);

	expect_payment_failed_conditions(
		local_node,
		payment_hash,
		true,
		PaymentFailedConditions::new(),
	);

	// Now that the state machine is no longer pending, and `closing_signed` is ready to be sent,
	// make sure we're still not waiting for the quiescence handshake to complete.
	// Note that we never actually reached full quiescence here.
	assert!(!local_node.node.exit_quiescence(&remote_node_id, &chan_id).unwrap());

	let _ = get_event_msg!(local_node, MessageSendEvent::SendClosingSigned, remote_node_id);
	check_added_monitors(local_node, 2); // One for the last revoke_and_ack, another for closing_signed
}

#[test]
fn test_quiescence_waits_for_async_signer_and_monitor_update() {
	// Test that quiescence:
	//   a) considers an async signer when determining whether a pending channel update exists
	//   b) waits until pending monitor updates complete to send `stfu`/become quiescent
	let chanmon_cfgs = create_chanmon_cfgs(2);
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
	let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
	let chan_id = create_announced_chan_between_nodes(&nodes, 0, 1).2;

	let node_id_0 = nodes[0].node.get_our_node_id();
	let node_id_1 = nodes[1].node.get_our_node_id();

	let payment_amount = 1_000_000;
	let (preimage, payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1]], payment_amount);
	nodes[1].node.claim_funds(preimage);
	check_added_monitors(&nodes[1], 1);
	expect_payment_claimed!(&nodes[1], payment_hash, payment_amount);

	let mut update = get_htlc_update_msgs!(&nodes[1], node_id_0);
	nodes[0].node.handle_update_fulfill_htlc(node_id_1, update.update_fulfill_htlcs.remove(0));
	nodes[0].node.handle_commitment_signed_batch_test(node_id_1, &update.commitment_signed);
	check_added_monitors(&nodes[0], 1);

	// While settling back the payment, propose quiescence from nodes[1]. We won't see its `stfu` go
	// out yet as the `update_fulfill` is still pending on both sides.
	nodes[1].node.maybe_propose_quiescence(&node_id_0, &chan_id).unwrap();

	// Disable releasing commitment secrets on nodes[1], to hold back their `stfu` until the
	// `revoke_and_ack` goes out, and drive the state machine forward.
	nodes[1].disable_channel_signer_op(&node_id_0, &chan_id, SignerOp::ReleaseCommitmentSecret);

	let (revoke_and_ack, commit_sig) = get_revoke_commit_msgs!(&nodes[0], node_id_1);
	nodes[1].node.handle_revoke_and_ack(node_id_0, &revoke_and_ack);
	check_added_monitors(&nodes[1], 1);
	nodes[1].node.handle_commitment_signed_batch_test(node_id_0, &commit_sig);
	check_added_monitors(&nodes[1], 1);

	assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());

	// Resume the signer. We should now expect to see both messages.
	nodes[1].enable_channel_signer_op(&node_id_0, &chan_id, SignerOp::ReleaseCommitmentSecret);
	nodes[1].node.signer_unblocked(Some((node_id_0, chan_id)));

	macro_rules! find_msg {
		($events: expr, $msg: ident) => {{
			$events
				.iter()
				.find_map(|event| {
					if let MessageSendEvent::$msg { ref msg, .. } = event {
						Some(msg)
					} else {
						None
					}
				})
				.unwrap()
		}};
	}
	let msg_events = nodes[1].node.get_and_clear_pending_msg_events();
	assert_eq!(msg_events.len(), 2);
	let revoke_and_ack = find_msg!(msg_events, SendRevokeAndACK);
	let stfu = find_msg!(msg_events, SendStfu);

	// While handling the last `revoke_and_ack` on nodes[0], we'll hold the monitor update. We
	// cannot become quiescent until it completes.
	chanmon_cfgs[0].persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
	nodes[0].node.handle_revoke_and_ack(node_id_1, &revoke_and_ack);

	nodes[0].node.handle_stfu(node_id_1, &stfu);
	assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());

	// We have two updates pending:
	{
		let test_chain_mon = &nodes[0].chain_monitor;
		let (_, latest_update) =
			test_chain_mon.latest_monitor_update_id.lock().unwrap().get(&chan_id).unwrap().clone();
		let chain_monitor = &nodes[0].chain_monitor.chain_monitor;
		// One for the latest commitment transaction update from the last `revoke_and_ack`
		chain_monitor.channel_monitor_updated(chan_id, latest_update).unwrap();
		expect_payment_sent(&nodes[0], preimage, None, false, true);

		let (_, new_latest_update) =
			test_chain_mon.latest_monitor_update_id.lock().unwrap().get(&chan_id).unwrap().clone();
		assert_eq!(new_latest_update, latest_update + 1);
		// One for the commitment secret update from the last `revoke_and_ack`
		chain_monitor.channel_monitor_updated(chan_id, new_latest_update).unwrap();
		// Once that update completes, we'll get the `PaymentPathSuccessful` event
		let events = nodes[0].node.get_and_clear_pending_events();
		assert_eq!(events.len(), 1);
		if let Event::PaymentPathSuccessful { .. } = &events[0] {
		} else {
			panic!("{events:?}");
		}
	}

	// With the updates completed, we can now become quiescent.
	let stfu = get_event_msg!(&nodes[0], MessageSendEvent::SendStfu, node_id_1);
	nodes[1].node.handle_stfu(node_id_0, &stfu);

	assert!(nodes[0].node.exit_quiescence(&node_id_1, &chan_id).unwrap());
	assert!(nodes[1].node.exit_quiescence(&node_id_0, &chan_id).unwrap());

	// After exiting quiescence, we should be able to resume payments from nodes[0].
	send_payment(&nodes[0], &[&nodes[1]], payment_amount);
}

#[test]
fn test_quiescence_on_final_revoke_and_ack_pending_monitor_update() {
	// Test that we do not let a pending monitor update for a final `revoke_and_ack` prevent us from
	// entering quiescence. This was caught by the fuzzer, reported as #3805.
	let chanmon_cfgs = create_chanmon_cfgs(2);
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
	let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
	let chan_id = create_announced_chan_between_nodes(&nodes, 0, 1).2;

	let node_id_0 = nodes[0].node.get_our_node_id();
	let node_id_1 = nodes[1].node.get_our_node_id();

	let payment_amount = 1_000_000;
	let (route, payment_hash, _, payment_secret) =
		get_route_and_payment_hash!(&nodes[0], &nodes[1], payment_amount);
	let onion = RecipientOnionFields::secret_only(payment_secret);
	let payment_id = PaymentId(payment_hash.0);
	nodes[0].node.send_payment_with_route(route, payment_hash, onion, payment_id).unwrap();
	check_added_monitors(&nodes[0], 1);

	nodes[1].node.maybe_propose_quiescence(&node_id_0, &chan_id).unwrap();
	let stfu = get_event_msg!(&nodes[1], MessageSendEvent::SendStfu, node_id_0);
	nodes[0].node.handle_stfu(node_id_1, &stfu);

	let update_add = get_htlc_update_msgs!(&nodes[0], node_id_1);
	nodes[1].node.handle_update_add_htlc(node_id_0, &update_add.update_add_htlcs[0]);
	nodes[1].node.handle_commitment_signed_batch_test(node_id_0, &update_add.commitment_signed);
	check_added_monitors(&nodes[1], 1);

	let (revoke_and_ack, commit_sig) = get_revoke_commit_msgs!(&nodes[1], node_id_0);
	nodes[0].node.handle_revoke_and_ack(node_id_1, &revoke_and_ack);
	check_added_monitors(&nodes[0], 1);
	nodes[0].node.handle_commitment_signed_batch_test(node_id_1, &commit_sig);
	check_added_monitors(&nodes[0], 1);

	chanmon_cfgs[1].persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
	let msgs = nodes[0].node.get_and_clear_pending_msg_events();
	if let MessageSendEvent::SendRevokeAndACK { msg, .. } = &msgs[0] {
		nodes[1].node.handle_revoke_and_ack(node_id_0, &msg);
		check_added_monitors(&nodes[1], 1);
	} else {
		panic!();
	}
	if let MessageSendEvent::SendStfu { msg, .. } = &msgs[1] {
		nodes[1].node.handle_stfu(node_id_0, &msg);
	} else {
		panic!();
	}

	assert!(nodes[0].node.exit_quiescence(&node_id_1, &chan_id).unwrap());
	assert!(nodes[1].node.exit_quiescence(&node_id_0, &chan_id).unwrap());
}

#[test]
fn test_quiescence_updates_go_to_holding_cell() {
	quiescence_updates_go_to_holding_cell(false);
	quiescence_updates_go_to_holding_cell(true);
}

fn quiescence_updates_go_to_holding_cell(fail_htlc: bool) {
	// Test that any updates made to a channel while quiescent go to the holding cell.
	let chanmon_cfgs = create_chanmon_cfgs(2);
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
	let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
	let chan_id = create_announced_chan_between_nodes(&nodes, 0, 1).2;

	let node_id_0 = nodes[0].node.get_our_node_id();
	let node_id_1 = nodes[1].node.get_our_node_id();

	// Send enough to be able to pay from both directions.
	let payment_amount = 1_000_000;
	send_payment(&nodes[0], &[&nodes[1]], payment_amount * 4);

	// Propose quiescence from nodes[1], and immediately try to send a payment. Since its `stfu` has
	// already gone out first, the outbound HTLC will go into the holding cell.
	nodes[1].node.maybe_propose_quiescence(&node_id_0, &chan_id).unwrap();
	let stfu = get_event_msg!(&nodes[1], MessageSendEvent::SendStfu, node_id_0);

	let (route1, payment_hash1, payment_preimage1, payment_secret1) =
		get_route_and_payment_hash!(&nodes[1], &nodes[0], payment_amount);
	let onion1 = RecipientOnionFields::secret_only(payment_secret1);
	let payment_id1 = PaymentId(payment_hash1.0);
	nodes[1].node.send_payment_with_route(route1, payment_hash1, onion1, payment_id1).unwrap();
	check_added_monitors!(&nodes[1], 0);
	assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());

	// Send a payment in the opposite direction. Since nodes[0] hasn't sent its own `stfu` yet, it's
	// allowed to make updates.
	let (route2, payment_hash2, payment_preimage2, payment_secret2) =
		get_route_and_payment_hash!(&nodes[0], &nodes[1], payment_amount);
	let onion2 = RecipientOnionFields::secret_only(payment_secret2);
	let payment_id2 = PaymentId(payment_hash2.0);
	nodes[0].node.send_payment_with_route(route2, payment_hash2, onion2, payment_id2).unwrap();
	check_added_monitors!(&nodes[0], 1);

	let update_add = get_htlc_update_msgs!(&nodes[0], node_id_1);
	nodes[1].node.handle_update_add_htlc(node_id_0, &update_add.update_add_htlcs[0]);
	commitment_signed_dance!(&nodes[1], &nodes[0], update_add.commitment_signed, false);
	expect_and_process_pending_htlcs(&nodes[1], false);
	expect_payment_claimable!(nodes[1], payment_hash2, payment_secret2, payment_amount);

	// Have nodes[1] attempt to fail/claim nodes[0]'s payment. Since nodes[1] already sent out
	// `stfu`, the `update_fail/fulfill` will go into the holding cell.
	if fail_htlc {
		nodes[1].node.fail_htlc_backwards(&payment_hash2);
		let failed_payment = HTLCHandlingFailureType::Receive { payment_hash: payment_hash2 };
		expect_and_process_pending_htlcs_and_htlc_handling_failed(&nodes[1], &[failed_payment]);
	} else {
		nodes[1].node.claim_funds(payment_preimage2);
		check_added_monitors(&nodes[1], 1);
	}
	assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());

	// Finish the quiescence handshake.
	nodes[0].node.handle_stfu(node_id_1, &stfu);
	let stfu = get_event_msg!(&nodes[0], MessageSendEvent::SendStfu, node_id_1);
	nodes[1].node.handle_stfu(node_id_0, &stfu);

	assert!(nodes[0].node.exit_quiescence(&node_id_1, &chan_id).unwrap());
	assert!(nodes[1].node.exit_quiescence(&node_id_0, &chan_id).unwrap());

	// Now that quiescence is over, nodes are allowed to make updates again. nodes[1] will have its
	// outbound HTLC finally go out, along with the fail/claim of nodes[0]'s payment.
	let mut update = get_htlc_update_msgs!(&nodes[1], node_id_0);
	check_added_monitors(&nodes[1], 1);
	nodes[0].node.handle_update_add_htlc(node_id_1, &update.update_add_htlcs[0]);
	if fail_htlc {
		nodes[0].node.handle_update_fail_htlc(node_id_1, &update.update_fail_htlcs[0]);
	} else {
		expect_payment_claimed!(nodes[1], payment_hash2, payment_amount);
		nodes[0].node.handle_update_fulfill_htlc(node_id_1, update.update_fulfill_htlcs.remove(0));
	}
	commitment_signed_dance!(&nodes[0], &nodes[1], update.commitment_signed, false);

	// The payment from nodes[0] should now be seen as failed/successful.
	let events = nodes[0].node.get_and_clear_pending_events();
	assert_eq!(events.len(), 2);
	if fail_htlc {
		assert!(events.iter().find(|e| matches!(e, Event::PaymentFailed { .. })).is_some());
		assert!(events.iter().find(|e| matches!(e, Event::PaymentPathFailed { .. })).is_some());
	} else {
		assert!(events.iter().find(|e| matches!(e, Event::PaymentSent { .. })).is_some());
		assert!(events.iter().find(|e| matches!(e, Event::PaymentPathSuccessful { .. })).is_some());
		check_added_monitors(&nodes[0], 1);
	}
	nodes[0].node.process_pending_htlc_forwards();
	expect_payment_claimable!(nodes[0], payment_hash1, payment_secret1, payment_amount);

	// Have nodes[0] fail/claim nodes[1]'s payment.
	if fail_htlc {
		nodes[0].node.fail_htlc_backwards(&payment_hash1);
		let failed_payment = HTLCHandlingFailureType::Receive { payment_hash: payment_hash1 };
		expect_and_process_pending_htlcs_and_htlc_handling_failed(&nodes[0], &[failed_payment]);
	} else {
		nodes[0].node.claim_funds(payment_preimage1);
	}
	check_added_monitors(&nodes[0], 1);

	let mut update = get_htlc_update_msgs!(&nodes[0], node_id_1);
	if fail_htlc {
		nodes[1].node.handle_update_fail_htlc(node_id_0, &update.update_fail_htlcs[0]);
	} else {
		expect_payment_claimed!(nodes[0], payment_hash1, payment_amount);
		nodes[1].node.handle_update_fulfill_htlc(node_id_0, update.update_fulfill_htlcs.remove(0));
	}
	commitment_signed_dance!(&nodes[1], &nodes[0], update.commitment_signed, false);

	// The payment from nodes[1] should now be seen as failed/successful.
	if fail_htlc {
		let conditions = PaymentFailedConditions::new();
		expect_payment_failed_conditions(&nodes[1], payment_hash1, true, conditions);
	} else {
		expect_payment_sent(&nodes[1], payment_preimage1, None, true, true);
	}
}

#[test]
fn test_quiescence_timeout() {
	// Test that we'll disconnect if we remain quiescent for `DISCONNECT_PEER_AWAITING_RESPONSE_TICKS`.
	let chanmon_cfgs = create_chanmon_cfgs(2);
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
	let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
	let chan_id = create_announced_chan_between_nodes(&nodes, 0, 1).2;

	let node_id_0 = nodes[0].node.get_our_node_id();
	let node_id_1 = nodes[1].node.get_our_node_id();

	nodes[0].node.maybe_propose_quiescence(&nodes[1].node.get_our_node_id(), &chan_id).unwrap();

	let stfu_initiator = get_event_msg!(nodes[0], MessageSendEvent::SendStfu, node_id_1);
	nodes[1].node.handle_stfu(node_id_0, &stfu_initiator);

	let stfu_responder = get_event_msg!(nodes[1], MessageSendEvent::SendStfu, node_id_0);
	nodes[0].node.handle_stfu(node_id_1, &stfu_responder);

	assert!(stfu_initiator.initiator && !stfu_responder.initiator);

	for _ in 0..DISCONNECT_PEER_AWAITING_RESPONSE_TICKS {
		nodes[0].node.timer_tick_occurred();
		nodes[1].node.timer_tick_occurred();
	}

	let f = |event| {
		if let MessageSendEvent::HandleError { action, .. } = event {
			if let msgs::ErrorAction::DisconnectPeerWithWarning { .. } = action {
				Some(())
			} else {
				None
			}
		} else {
			None
		}
	};
	assert!(nodes[0].node.get_and_clear_pending_msg_events().into_iter().find_map(f).is_some());
	assert!(nodes[1].node.get_and_clear_pending_msg_events().into_iter().find_map(f).is_some());
}

#[test]
fn test_quiescence_timeout_while_waiting_for_counterparty_stfu() {
	// Test that we'll disconnect if the counterparty does not send their stfu within a reasonable
	// time if we've already sent ours.
	let chanmon_cfgs = create_chanmon_cfgs(2);
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
	let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
	let chan_id = create_announced_chan_between_nodes(&nodes, 0, 1).2;

	let node_id_0 = nodes[0].node.get_our_node_id();

	nodes[1].node.maybe_propose_quiescence(&node_id_0, &chan_id).unwrap();
	let _ = get_event_msg!(nodes[1], MessageSendEvent::SendStfu, node_id_0);

	// Route a payment in between to ensure expecting to receive `revoke_and_ack` doesn't override
	// the expectation of receiving `stfu` as well.
	let _ = route_payment(&nodes[0], &[&nodes[1]], 1_000_000);

	for _ in 0..DISCONNECT_PEER_AWAITING_RESPONSE_TICKS {
		nodes[0].node.timer_tick_occurred();
		nodes[1].node.timer_tick_occurred();
	}

	// nodes[0] hasn't received stfu from nodes[1], so it's not enforcing any timeouts.
	assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());

	// nodes[1] didn't receive nodes[0]'s stfu within the timeout so it'll disconnect.
	let f = |&ref event| {
		if let MessageSendEvent::HandleError { action, .. } = event {
			if let msgs::ErrorAction::DisconnectPeerWithWarning { .. } = action {
				Some(())
			} else {
				None
			}
		} else {
			None
		}
	};
	assert!(nodes[1].node.get_and_clear_pending_msg_events().iter().find_map(f).is_some());
}

#[test]
fn test_quiescence_timeout_while_waiting_for_counterparty_something_fundamental() {
	// Test that we'll disconnect if the counterparty does not send their "something fundamental"
	// within a reasonable time if we've reached quiescence.
	let chanmon_cfgs = create_chanmon_cfgs(2);
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
	let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
	let chan_id = create_announced_chan_between_nodes(&nodes, 0, 1).2;

	let node_id_0 = nodes[0].node.get_our_node_id();
	let node_id_1 = nodes[1].node.get_our_node_id();

	nodes[1].node.maybe_propose_quiescence(&node_id_0, &chan_id).unwrap();
	let stfu = get_event_msg!(nodes[1], MessageSendEvent::SendStfu, node_id_0);

	nodes[0].node.handle_stfu(node_id_1, &stfu);
	let _stfu = get_event_msg!(nodes[0], MessageSendEvent::SendStfu, node_id_1);

	for _ in 0..DISCONNECT_PEER_AWAITING_RESPONSE_TICKS {
		nodes[0].node.timer_tick_occurred();
		nodes[1].node.timer_tick_occurred();
	}

	// Node B didn't receive node A's stfu within the timeout so it'll disconnect.
	let f = |event| {
		if let MessageSendEvent::HandleError { action, .. } = event {
			if let msgs::ErrorAction::DisconnectPeerWithWarning { .. } = action {
				Some(())
			} else {
				None
			}
		} else {
			None
		}
	};
	// At this point, node A is waiting on B to do something fundamental, and node B is waiting on
	// A's stfu that we never delivered. Thus both should disconnect each other.
	assert!(nodes[0].node.get_and_clear_pending_msg_events().into_iter().find_map(&f).is_some());
	assert!(nodes[1].node.get_and_clear_pending_msg_events().into_iter().find_map(&f).is_some());
}

fn do_test_quiescence_during_disconnection(with_pending_claim: bool, propose_disconnected: bool) {
	// Test that we'll start trying for quiescence immediately after reconnection if we're waiting
	// to do some quiescence-required action.
	let chanmon_cfgs = create_chanmon_cfgs(2);
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
	let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
	let chan_id = create_announced_chan_between_nodes(&nodes, 0, 1).2;

	let node_a_id = nodes[0].node.get_our_node_id();
	let node_b_id = nodes[1].node.get_our_node_id();

	// First get both nodes off the starting state so we don't have to deal with channel_ready
	// retransmissions on reconect.
	send_payment(&nodes[0], &[&nodes[1]], 100_000);

	let (preimage, payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1]], 100_000);
	if with_pending_claim {
		// Optionally reconnect with pending quiescence while there's some pending messages to
		// deliver.
		nodes[1].node.claim_funds(preimage);
		check_added_monitors(&nodes[1], 1);
		expect_payment_claimed!(nodes[1], payment_hash, 100_000);
		let _ = get_htlc_update_msgs(&nodes[1], &node_a_id);
	}

	if !propose_disconnected {
		nodes[1].node.maybe_propose_quiescence(&node_a_id, &chan_id).unwrap();
	}

	nodes[0].node.peer_disconnected(node_b_id);
	nodes[1].node.peer_disconnected(node_a_id);

	if propose_disconnected {
		nodes[1].node.maybe_propose_quiescence(&node_a_id, &chan_id).unwrap();
	}

	let init_msg = msgs::Init {
		features: nodes[1].node.init_features(),
		networks: None,
		remote_network_address: None,
	};
	nodes[0].node.peer_connected(node_b_id, &init_msg, true).unwrap();
	nodes[1].node.peer_connected(node_a_id, &init_msg, true).unwrap();

	let reestab_a = get_event_msg!(nodes[0], MessageSendEvent::SendChannelReestablish, node_b_id);
	let reestab_b = get_event_msg!(nodes[1], MessageSendEvent::SendChannelReestablish, node_a_id);

	nodes[0].node.handle_channel_reestablish(node_b_id, &reestab_b);
	get_event_msg!(nodes[0], MessageSendEvent::SendChannelUpdate, node_b_id);

	nodes[1].node.handle_channel_reestablish(node_a_id, &reestab_a);
	let mut bs_msgs = nodes[1].node.get_and_clear_pending_msg_events();
	bs_msgs.retain(|msg| !matches!(msg, MessageSendEvent::SendChannelUpdate { .. }));
	assert_eq!(bs_msgs.len(), 1, "{bs_msgs:?}");
	let stfu = if with_pending_claim {
		// Node B should first re-send its channel update, then try to enter quiescence once that
		// completes...
		let msg = bs_msgs.pop().unwrap();
		if let MessageSendEvent::UpdateHTLCs { mut updates, .. } = msg {
			let fulfill = updates.update_fulfill_htlcs.pop().unwrap();
			nodes[0].node.handle_update_fulfill_htlc(node_b_id, fulfill);
			let cs = updates.commitment_signed;
			nodes[0].node.handle_commitment_signed_batch_test(node_b_id, &cs);
			check_added_monitors(&nodes[0], 1);

			let (raa, cs) = get_revoke_commit_msgs(&nodes[0], &node_b_id);
			nodes[1].node.handle_revoke_and_ack(node_a_id, &raa);
			check_added_monitors(&nodes[1], 1);
			nodes[1].node.handle_commitment_signed_batch_test(node_a_id, &cs);
			check_added_monitors(&nodes[1], 1);

			let mut bs_raa_stfu = nodes[1].node.get_and_clear_pending_msg_events();
			assert_eq!(bs_raa_stfu.len(), 2);
			if let MessageSendEvent::SendRevokeAndACK { msg, .. } = &bs_raa_stfu[0] {
				nodes[0].node.handle_revoke_and_ack(node_b_id, &msg);
				expect_payment_sent!(&nodes[0], preimage);
			} else {
				panic!("Unexpected first message {bs_raa_stfu:?}");
			}

			bs_raa_stfu.pop().unwrap()
		} else {
			panic!("Unexpected message {msg:?}");
		}
	} else {
		bs_msgs.pop().unwrap()
	};
	if let MessageSendEvent::SendStfu { msg, .. } = stfu {
		nodes[0].node.handle_stfu(node_b_id, &msg);
	} else {
		panic!("Unexpected message {stfu:?}");
	}

	let stfu_resp = get_event_msg!(nodes[0], MessageSendEvent::SendStfu, node_b_id);
	nodes[1].node.handle_stfu(node_a_id, &stfu_resp);

	assert!(nodes[0].node.exit_quiescence(&node_b_id, &chan_id).unwrap());
	assert!(nodes[1].node.exit_quiescence(&node_a_id, &chan_id).unwrap());
}

#[test]
fn test_quiescence_during_disconnection() {
	do_test_quiescence_during_disconnection(false, false);
	do_test_quiescence_during_disconnection(true, false);
	do_test_quiescence_during_disconnection(false, true);
	do_test_quiescence_during_disconnection(true, true);
}

#[test]
fn test_quiescence_termination_on_disconnect() {
	do_test_quiescence_termination_on_disconnect(false);
	do_test_quiescence_termination_on_disconnect(true);
}

fn do_test_quiescence_termination_on_disconnect(reload: bool) {
	// Tests that we terminate quiescence on disconnect if there's no quiescence protocol taking place.
	let chanmon_cfgs = create_chanmon_cfgs(2);
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let (persister_0a, persister_1a);
	let (chain_monitor_0a, chain_monitor_1a);
	let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
	let (node_0a, node_1a);
	let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);

	let node_id_0 = nodes[0].node.get_our_node_id();
	let node_id_1 = nodes[1].node.get_our_node_id();

	let (_, _, channel_id, _) = create_announced_chan_between_nodes(&nodes, 0, 1);

	nodes[0].node.maybe_propose_quiescence(&node_id_1, &channel_id).unwrap();

	let stfu = get_event_msg!(nodes[0], MessageSendEvent::SendStfu, node_id_1);
	nodes[1].node.handle_stfu(node_id_0, &stfu);
	let stfu = get_event_msg!(nodes[1], MessageSendEvent::SendStfu, node_id_0);
	nodes[0].node.handle_stfu(node_id_1, &stfu);

	if reload {
		let encoded_monitor_0 = get_monitor!(nodes[0], channel_id).encode();
		reload_node!(
			nodes[0],
			&nodes[0].node.encode(),
			&[&encoded_monitor_0],
			persister_0a,
			chain_monitor_0a,
			node_0a
		);
		let encoded_monitor_1 = get_monitor!(nodes[1], channel_id).encode();
		reload_node!(
			nodes[1],
			&nodes[1].node.encode(),
			&[&encoded_monitor_1],
			persister_1a,
			chain_monitor_1a,
			node_1a
		);
	} else {
		nodes[0].node.peer_disconnected(node_id_1);
		nodes[1].node.peer_disconnected(node_id_0);
	}

	let mut reconnect_args = ReconnectArgs::new(&nodes[0], &nodes[1]);
	reconnect_args.send_channel_ready = (true, true);
	reconnect_args.send_announcement_sigs = (true, true);
	reconnect_nodes(reconnect_args);

	send_payment(&nodes[0], &[&nodes[1]], 1_000_000);
}