Skip to content

Commit 119b703

Browse files
committed
f - test enabling signer before reestablihs, no double send
1 parent cd73d11 commit 119b703

File tree

1 file changed

+58
-14
lines changed

1 file changed

+58
-14
lines changed

lightning/src/ln/async_signer_tests.rs

+58-14
Original file line numberDiff line numberDiff line change
@@ -332,20 +332,33 @@ fn test_async_commitment_signature_for_peer_disconnect() {
332332

333333
#[test]
334334
fn test_async_commitment_signature_ordering_reestablish() {
335-
do_test_async_commitment_signature_ordering(false, false);
335+
do_test_async_commitment_signature_ordering(false, false, false);
336336
}
337337

338338
#[test]
339339
fn test_async_commitment_signature_ordering_monitor_restored() {
340-
do_test_async_commitment_signature_ordering(true, false);
340+
do_test_async_commitment_signature_ordering(true, false, false);
341341
}
342342

343343
#[test]
344-
fn test_async_commitment_signature_ordering_monitor_restored_signer_restored_early() {
345-
do_test_async_commitment_signature_ordering(true, true);
344+
fn test_async_commitment_signature_ordering_monitor_restored_signer_restored_before_completion() {
345+
// This tests that if we were pending a monitor update completion across a restart,
346+
// and needed to send a CS then RAA, that if our signer is unblocked before the monitor
347+
// update completes, then we don't send duplicate messages upon calling `signer_unblocked`
348+
// after the monitor update completes.
349+
do_test_async_commitment_signature_ordering(true, true, false);
346350
}
347351

348-
fn do_test_async_commitment_signature_ordering(monitor_update_failure: bool, enable_signer_before_monitor_completion: bool) {
352+
#[test]
353+
fn test_async_commitment_signature_ordering_signer_restored_before_reestablish() {
354+
// This tests that if we tried to send a commitment_signed, but our signer was blocked,
355+
// if we disconnect, reconnect, unblock the signer, then handle channel_reestablish,
356+
// that we don't send duplicate messages upon calling `signer_unblocked`.
357+
do_test_async_commitment_signature_ordering(false, false, true);
358+
}
359+
360+
fn do_test_async_commitment_signature_ordering(monitor_update_failure: bool,
361+
enable_signer_before_monitor_completion: bool, enable_signer_before_reestablish: bool) {
349362
// Across disconnects we may end up in a situation where we need to send a
350363
// commitment_signed and then revoke_and_ack. We need to make sure that if
351364
// the signer is pending for commitment_signed but not revoke_and_ack, we don't
@@ -365,14 +378,24 @@ fn do_test_async_commitment_signature_ordering(monitor_update_failure: bool, ena
365378

366379
let (payment_preimage_1, payment_hash_1, ..) = route_payment(&nodes[0], &[&nodes[1]], 1_000_000);
367380

381+
if enable_signer_before_reestablish {
382+
// When the node goes to sign the second commitment update, it will set that
383+
// it's pending a commitment update, which will still be set when the peer
384+
// reconnects.
385+
nodes[0].disable_channel_signer_op(&nodes[1].node.get_our_node_id(), &chan_id, SignerOp::SignCounterpartyCommitment);
386+
}
368387
// Start to send the second update_add_htlc + commitment_signed, but don't actually make it
369388
// to the peer.
370389
let (route, payment_hash_2, payment_preimage_2, payment_secret_2) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
371390
nodes[0].node.send_payment_with_route(&route, payment_hash_2,
372391
RecipientOnionFields::secret_only(payment_secret_2), PaymentId(payment_hash_2.0)).unwrap();
373392
check_added_monitors!(nodes[0], 1);
374393

375-
get_htlc_update_msgs(&nodes[0], &nodes[1].node.get_our_node_id());
394+
if enable_signer_before_reestablish {
395+
assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
396+
} else {
397+
get_htlc_update_msgs(&nodes[0], &nodes[1].node.get_our_node_id());
398+
}
376399

377400
// Send back update_fulfill_htlc + commitment_signed for the first payment.
378401
nodes[1].node.claim_funds(payment_preimage_1);
@@ -391,7 +414,7 @@ fn do_test_async_commitment_signature_ordering(monitor_update_failure: bool, ena
391414
chanmon_cfgs[0].persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
392415
}
393416
nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), commitment_signed);
394-
if monitor_update_failure {
417+
if monitor_update_failure || enable_signer_before_reestablish {
395418
assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
396419
} else {
397420
let _ = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
@@ -421,22 +444,38 @@ fn do_test_async_commitment_signature_ordering(monitor_update_failure: bool, ena
421444
// With a fully working signer, here we would send a commitment_signed,
422445
// and then revoke_and_ack. With commitment_signed disabled, since
423446
// our ordering is CS then RAA, we should make sure we don't send the RAA.
424-
nodes[0].disable_channel_signer_op(&nodes[1].node.get_our_node_id(), &chan_id, SignerOp::SignCounterpartyCommitment);
447+
if enable_signer_before_reestablish {
448+
nodes[0].enable_channel_signer_op(&nodes[1].node.get_our_node_id(), &chan_id, SignerOp::SignCounterpartyCommitment);
449+
} else {
450+
nodes[0].disable_channel_signer_op(&nodes[1].node.get_our_node_id(), &chan_id, SignerOp::SignCounterpartyCommitment);
451+
}
425452
nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_2[0]);
426-
let as_resp = handle_chan_reestablish_msgs!(nodes[0], nodes[1]);
427-
assert!(as_resp.0.is_none());
428-
assert!(as_resp.1.is_none());
429-
assert!(as_resp.2.is_none());
430453

431-
let as_resp = if monitor_update_failure && enable_signer_before_monitor_completion {
454+
let as_resp = if enable_signer_before_reestablish {
455+
// We should send the CS + RAA now, and not again on `signer_unblocked`.
456+
let as_resp = handle_chan_reestablish_msgs!(nodes[0], nodes[1]);
457+
458+
nodes[0].node.signer_unblocked(Some((nodes[1].node.get_our_node_id(), chan_id)));
459+
let as_resp_empty = handle_chan_reestablish_msgs!(nodes[0], nodes[1]);
460+
assert!(as_resp_empty.0.is_none());
461+
assert!(as_resp_empty.1.is_none());
462+
assert!(as_resp_empty.2.is_none());
463+
464+
as_resp
465+
} else if monitor_update_failure && enable_signer_before_monitor_completion {
466+
let as_resp = handle_chan_reestablish_msgs!(nodes[0], nodes[1]);
467+
assert!(as_resp.0.is_none());
468+
assert!(as_resp.1.is_none());
469+
assert!(as_resp.2.is_none());
470+
432471
nodes[0].enable_channel_signer_op(&nodes[1].node.get_our_node_id(), &chan_id, SignerOp::SignCounterpartyCommitment);
433472

434473
chanmon_cfgs[0].persister.set_update_ret(ChannelMonitorUpdateStatus::Completed);
435474
let (outpoint, latest_update, _) = nodes[0].chain_monitor.latest_monitor_update_id.lock().unwrap().get(&chan_id).unwrap().clone();
436475
nodes[0].chain_monitor.chain_monitor.force_channel_monitor_updated(outpoint, latest_update);
437476
check_added_monitors!(nodes[0], 0);
438477

439-
// We should send the CS now, and not send it again on `signer_unblocked`.
478+
// We should send the CS + RAA now, and not again on `signer_unblocked`.
440479
let as_resp = handle_chan_reestablish_msgs!(nodes[0], nodes[1]);
441480

442481
nodes[0].node.signer_unblocked(Some((nodes[1].node.get_our_node_id(), chan_id)));
@@ -446,6 +485,11 @@ fn do_test_async_commitment_signature_ordering(monitor_update_failure: bool, ena
446485
assert!(as_resp_empty.2.is_none());
447486
as_resp
448487
} else {
488+
let as_resp = handle_chan_reestablish_msgs!(nodes[0], nodes[1]);
489+
assert!(as_resp.0.is_none());
490+
assert!(as_resp.1.is_none());
491+
assert!(as_resp.2.is_none());
492+
449493
if monitor_update_failure {
450494
chanmon_cfgs[0].persister.set_update_ret(ChannelMonitorUpdateStatus::Completed);
451495
let (outpoint, latest_update, _) = nodes[0].chain_monitor.latest_monitor_update_id.lock().unwrap().get(&chan_id).unwrap().clone();

0 commit comments

Comments
 (0)