10
10
//! Tests for asynchronous signing. These tests verify that the channel state machine behaves
11
11
//! properly with a signer implementation that asynchronously derives signatures.
12
12
13
+ use bitcoin:: key:: Secp256k1 ;
13
14
use bitcoin:: { Transaction , TxOut , TxIn , Amount } ;
14
15
use bitcoin:: blockdata:: locktime:: absolute:: LockTime ;
15
16
use bitcoin:: transaction:: Version ;
@@ -18,10 +19,13 @@ use crate::chain::channelmonitor::LATENCY_GRACE_PERIOD_BLOCKS;
18
19
use crate :: chain:: ChannelMonitorUpdateStatus ;
19
20
use crate :: events:: bump_transaction:: WalletSource ;
20
21
use crate :: events:: { ClosureReason , Event , MessageSendEvent , MessageSendEventsProvider } ;
22
+ use crate :: ln:: chan_utils:: ClosingTransaction ;
21
23
use crate :: ln:: { functional_test_utils:: * , msgs} ;
22
24
use crate :: ln:: channel_state:: { ChannelDetails , ChannelShutdownState } ;
23
25
use crate :: ln:: msgs:: ChannelMessageHandler ;
24
26
use crate :: ln:: channelmanager:: { PaymentId , RAACommitmentOrder , RecipientOnionFields } ;
27
+ use crate :: sign:: ecdsa:: EcdsaChannelSigner ;
28
+ use crate :: sign:: SignerProvider ;
25
29
use crate :: util:: test_channel_signer:: SignerOp ;
26
30
27
31
#[ test]
@@ -699,6 +703,11 @@ fn test_async_holder_signatures_remote_commitment_anchors() {
699
703
700
704
#[ test]
701
705
fn test_closing_signed ( ) {
706
+ do_test_closing_signed ( false ) ;
707
+ do_test_closing_signed ( true ) ;
708
+ }
709
+
710
+ fn do_test_closing_signed ( extra_closing_signed : bool ) {
702
711
// Based off of `expect_channel_shutdown_state`.
703
712
// Test that we can asynchronously sign closing transactions.
704
713
let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
@@ -746,6 +755,40 @@ fn test_closing_signed() {
746
755
nodes[ 0 ] . disable_channel_signer_op ( & nodes[ 1 ] . node . get_our_node_id ( ) , & chan_1. 2 , SignerOp :: SignClosingTransaction ) ;
747
756
nodes[ 0 ] . node . handle_closing_signed ( & nodes[ 1 ] . node . get_our_node_id ( ) , & node_1_closing_signed) ;
748
757
nodes[ 0 ] . enable_channel_signer_op ( & nodes[ 1 ] . node . get_our_node_id ( ) , & chan_1. 2 , SignerOp :: SignClosingTransaction ) ;
758
+
759
+ if extra_closing_signed {
760
+ let node_1_closing_signed_2_bad = {
761
+ let mut node_1_closing_signed_2 = node_1_closing_signed. clone ( ) ;
762
+ //node_1_closing_signed_2.signature.
763
+ let holder_script = nodes[ 0 ] . keys_manager . get_shutdown_scriptpubkey ( ) . unwrap ( ) ;
764
+ let counterparty_script = nodes[ 1 ] . keys_manager . get_shutdown_scriptpubkey ( ) . unwrap ( ) ;
765
+ let funding_outpoint = bitcoin:: OutPoint { txid : chan_1. 3 . txid ( ) , vout : 0 } ;
766
+ let closing_tx_2 = ClosingTransaction :: new ( 50000 , 0 , holder_script. into ( ) ,
767
+ counterparty_script. into ( ) , funding_outpoint) ;
768
+
769
+ let per_peer_state = nodes[ 1 ] . node . per_peer_state . read ( ) . unwrap ( ) ;
770
+ let mut chan_lock = per_peer_state. get ( & nodes[ 0 ] . node . get_our_node_id ( ) ) . unwrap ( ) . lock ( ) . unwrap ( ) ;
771
+ let chan = chan_lock. channel_by_id . get_mut ( & chan_1. 2 ) . map ( |phase| phase. context_mut ( ) ) . unwrap ( ) ;
772
+
773
+ let signer = chan. get_mut_signer ( ) . as_mut_ecdsa ( ) . unwrap ( ) ;
774
+ let signature = signer. sign_closing_transaction ( & closing_tx_2, & Secp256k1 :: new ( ) ) . unwrap ( ) ;
775
+ node_1_closing_signed_2. signature = signature;
776
+ node_1_closing_signed_2
777
+ } ;
778
+ nodes[ 0 ] . node . handle_closing_signed ( & nodes[ 1 ] . node . get_our_node_id ( ) , & node_1_closing_signed_2_bad) ;
779
+
780
+ let events = nodes[ 0 ] . node . get_and_clear_pending_msg_events ( ) ;
781
+ assert_eq ! ( events. len( ) , 1 ) ;
782
+ match events[ 0 ] {
783
+ MessageSendEvent :: HandleError {
784
+ action : msgs:: ErrorAction :: SendWarningMessage { .. } , ref node_id
785
+ } => {
786
+ assert_eq ! ( node_id, & nodes[ 1 ] . node. get_our_node_id( ) ) ;
787
+ } ,
788
+ _ => panic ! ( "Unexpected event: {:?}" , events[ 0 ] ) ,
789
+ } ;
790
+ }
791
+
749
792
nodes[ 0 ] . node . signer_unblocked ( None ) ;
750
793
let ( _, node_0_2nd_closing_signed) = get_closing_signed_broadcast ! ( nodes[ 0 ] . node, nodes[ 1 ] . node. get_our_node_id( ) ) ;
751
794
0 commit comments