From 934ec75cb2cd687132277f2d44ea1ec5c29226ed Mon Sep 17 00:00:00 2001 From: Alec Chen Date: Wed, 14 Aug 2024 14:45:15 -0700 Subject: [PATCH] f - test coverage for handling closing_signed while waiting on signer --- lightning/src/ln/async_signer_tests.rs | 43 ++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/lightning/src/ln/async_signer_tests.rs b/lightning/src/ln/async_signer_tests.rs index 92de904b7e7..a95fe0cdb71 100644 --- a/lightning/src/ln/async_signer_tests.rs +++ b/lightning/src/ln/async_signer_tests.rs @@ -10,6 +10,7 @@ //! Tests for asynchronous signing. These tests verify that the channel state machine behaves //! properly with a signer implementation that asynchronously derives signatures. +use bitcoin::key::Secp256k1; use bitcoin::{Transaction, TxOut, TxIn, Amount}; use bitcoin::blockdata::locktime::absolute::LockTime; use bitcoin::transaction::Version; @@ -18,10 +19,13 @@ use crate::chain::channelmonitor::LATENCY_GRACE_PERIOD_BLOCKS; use crate::chain::ChannelMonitorUpdateStatus; use crate::events::bump_transaction::WalletSource; use crate::events::{ClosureReason, Event, MessageSendEvent, MessageSendEventsProvider}; +use crate::ln::chan_utils::ClosingTransaction; use crate::ln::{functional_test_utils::*, msgs}; use crate::ln::channel_state::{ChannelDetails, ChannelShutdownState}; use crate::ln::msgs::ChannelMessageHandler; use crate::ln::channelmanager::{PaymentId, RAACommitmentOrder, RecipientOnionFields}; +use crate::sign::ecdsa::EcdsaChannelSigner; +use crate::sign::SignerProvider; use crate::util::test_channel_signer::SignerOp; #[test] @@ -699,6 +703,11 @@ fn test_async_holder_signatures_remote_commitment_anchors() { #[test] fn test_closing_signed() { + do_test_closing_signed(false); + do_test_closing_signed(true); +} + +fn do_test_closing_signed(extra_closing_signed: bool) { // Based off of `expect_channel_shutdown_state`. // Test that we can asynchronously sign closing transactions. let chanmon_cfgs = create_chanmon_cfgs(2); @@ -746,6 +755,40 @@ fn test_closing_signed() { nodes[0].disable_channel_signer_op(&nodes[1].node.get_our_node_id(), &chan_1.2, SignerOp::SignClosingTransaction); nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed); nodes[0].enable_channel_signer_op(&nodes[1].node.get_our_node_id(), &chan_1.2, SignerOp::SignClosingTransaction); + + if extra_closing_signed { + let node_1_closing_signed_2_bad = { + let mut node_1_closing_signed_2 = node_1_closing_signed.clone(); + //node_1_closing_signed_2.signature. + let holder_script = nodes[0].keys_manager.get_shutdown_scriptpubkey().unwrap(); + let counterparty_script = nodes[1].keys_manager.get_shutdown_scriptpubkey().unwrap(); + let funding_outpoint = bitcoin::OutPoint { txid: chan_1.3.txid(), vout: 0 }; + let closing_tx_2 = ClosingTransaction::new(50000, 0, holder_script.into(), + counterparty_script.into(), funding_outpoint); + + let per_peer_state = nodes[1].node.per_peer_state.read().unwrap(); + let mut chan_lock = per_peer_state.get(&nodes[0].node.get_our_node_id()).unwrap().lock().unwrap(); + let chan = chan_lock.channel_by_id.get_mut(&chan_1.2).map(|phase| phase.context_mut()).unwrap(); + + let signer = chan.get_mut_signer().as_mut_ecdsa().unwrap(); + let signature = signer.sign_closing_transaction(&closing_tx_2, &Secp256k1::new()).unwrap(); + node_1_closing_signed_2.signature = signature; + node_1_closing_signed_2 + }; + nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed_2_bad); + + let events = nodes[0].node.get_and_clear_pending_msg_events(); + assert_eq!(events.len(), 1); + match events[0] { + MessageSendEvent::HandleError { + action: msgs::ErrorAction::SendWarningMessage { .. }, ref node_id + } => { + assert_eq!(node_id, &nodes[1].node.get_our_node_id()); + }, + _ => panic!("Unexpected event: {:?}", events[0]), + }; + } + nodes[0].node.signer_unblocked(None); let (_, node_0_2nd_closing_signed) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());