Skip to content

Commit a46812a

Browse files
committed
Test async sign_closing_transaction
1 parent 30e1827 commit a46812a

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

lightning/src/ln/async_signer_tests.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use crate::chain::channelmonitor::LATENCY_GRACE_PERIOD_BLOCKS;
1818
use crate::events::bump_transaction::WalletSource;
1919
use crate::events::{Event, MessageSendEvent, MessageSendEventsProvider, ClosureReason};
2020
use crate::ln::functional_test_utils::*;
21+
use crate::ln::channel_state::{ChannelDetails, ChannelShutdownState};
2122
use crate::ln::msgs::ChannelMessageHandler;
2223
use crate::ln::channelmanager::{PaymentId, RecipientOnionFields};
2324
use crate::util::test_channel_signer::SignerOp;
@@ -472,3 +473,62 @@ fn test_async_holder_signatures_anchors() {
472473
fn test_async_holder_signatures_remote_commitment_anchors() {
473474
do_test_async_holder_signatures(true, true);
474475
}
476+
477+
#[test]
478+
fn test_closing_signed() {
479+
// Based off of `expect_channel_shutdown_state`.
480+
// Test that we can asynchronously sign closing transactions.
481+
let chanmon_cfgs = create_chanmon_cfgs(2);
482+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
483+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
484+
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
485+
let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
486+
487+
expect_channel_shutdown_state!(nodes[0], chan_1.2, ChannelShutdownState::NotShuttingDown);
488+
489+
nodes[0].node.close_channel(&chan_1.2, &nodes[1].node.get_our_node_id()).unwrap();
490+
491+
expect_channel_shutdown_state!(nodes[0], chan_1.2, ChannelShutdownState::ShutdownInitiated);
492+
expect_channel_shutdown_state!(nodes[1], chan_1.2, ChannelShutdownState::NotShuttingDown);
493+
494+
let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
495+
nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_shutdown);
496+
497+
expect_channel_shutdown_state!(nodes[0], chan_1.2, ChannelShutdownState::ShutdownInitiated);
498+
expect_channel_shutdown_state!(nodes[1], chan_1.2, ChannelShutdownState::NegotiatingClosingFee);
499+
500+
let node_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
501+
nodes[0].disable_channel_signer_op(&nodes[1].node.get_our_node_id(), &chan_1.2, SignerOp::SignClosingTransaction);
502+
nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_shutdown);
503+
504+
expect_channel_shutdown_state!(nodes[0], chan_1.2, ChannelShutdownState::NegotiatingClosingFee);
505+
expect_channel_shutdown_state!(nodes[1], chan_1.2, ChannelShutdownState::NegotiatingClosingFee);
506+
507+
let events = nodes[0].node.get_and_clear_pending_msg_events();
508+
assert!(events.is_empty(), "Expected no events, got {:?}", events);
509+
nodes[0].enable_channel_signer_op(&nodes[1].node.get_our_node_id(), &chan_1.2, SignerOp::SignClosingTransaction);
510+
nodes[0].node.signer_unblocked(None);
511+
512+
let node_0_closing_signed = get_event_msg!(nodes[0], MessageSendEvent::SendClosingSigned, nodes[1].node.get_our_node_id());
513+
nodes[1].disable_channel_signer_op(&nodes[0].node.get_our_node_id(), &chan_1.2, SignerOp::SignClosingTransaction);
514+
nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed);
515+
516+
let events = nodes[1].node.get_and_clear_pending_msg_events();
517+
assert!(events.is_empty(), "Expected no events, got {:?}", events);
518+
nodes[1].enable_channel_signer_op(&nodes[0].node.get_our_node_id(), &chan_1.2, SignerOp::SignClosingTransaction);
519+
nodes[1].node.signer_unblocked(None);
520+
521+
let node_1_closing_signed = get_event_msg!(nodes[1], MessageSendEvent::SendClosingSigned, nodes[0].node.get_our_node_id());
522+
// From here we don't make any new signatures, so there's no need to test blocking the
523+
// signer.
524+
nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed);
525+
let (_, node_0_2nd_closing_signed) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
526+
nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_2nd_closing_signed.unwrap());
527+
let (_, node_1_none) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
528+
assert!(node_1_none.is_none());
529+
530+
assert!(nodes[0].node.list_channels().is_empty());
531+
assert!(nodes[1].node.list_channels().is_empty());
532+
check_closed_event!(nodes[0], 1, ClosureReason::LocallyInitiatedCooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
533+
check_closed_event!(nodes[1], 1, ClosureReason::CounterpartyInitiatedCooperativeClosure, [nodes[0].node.get_our_node_id()], 100000);
534+
}

lightning/src/util/test_channel_signer.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,9 @@ impl EcdsaChannelSigner for TestChannelSigner {
312312
}
313313

314314
fn sign_closing_transaction(&self, closing_tx: &ClosingTransaction, secp_ctx: &Secp256k1<secp256k1::All>) -> Result<Signature, ()> {
315+
if !self.is_signer_available(SignerOp::SignClosingTransaction) {
316+
return Err(());
317+
}
315318
closing_tx.verify(self.inner.funding_outpoint().unwrap().into_bitcoin_outpoint())
316319
.expect("derived different closing transaction");
317320
Ok(self.inner.sign_closing_transaction(closing_tx, secp_ctx).unwrap())

0 commit comments

Comments
 (0)