1010//! Tests for asynchronous signing. These tests verify that the channel state machine behaves
1111//! properly with a signer implementation that asynchronously derives signatures.
1212
13+ use std:: collections:: HashSet ;
14+
1315use bitcoin:: { Transaction , TxOut , TxIn , Amount } ;
1416use bitcoin:: blockdata:: locktime:: absolute:: LockTime ;
1517use bitcoin:: transaction:: Version ;
@@ -22,6 +24,7 @@ use crate::ln::{functional_test_utils::*, msgs};
2224use crate :: ln:: msgs:: ChannelMessageHandler ;
2325use crate :: ln:: channelmanager:: { PaymentId , RAACommitmentOrder , RecipientOnionFields } ;
2426use crate :: util:: test_channel_signer:: SignerOp ;
27+ use crate :: util:: logger:: Logger ;
2528
2629#[ test]
2730fn test_async_commitment_signature_for_funding_created ( ) {
@@ -127,11 +130,17 @@ fn test_async_commitment_signature_for_funding_signed() {
127130
128131#[ test]
129132fn test_async_commitment_signature_for_commitment_signed ( ) {
130- do_test_async_commitment_signature_for_commitment_signed_revoke_and_ack ( 0 ) ;
131- do_test_async_commitment_signature_for_commitment_signed_revoke_and_ack ( 1 ) ;
133+ for i in 0 ..=8 {
134+ let enable_signer_op_order = vec ! [
135+ SignerOp :: GetPerCommitmentPoint ,
136+ SignerOp :: ReleaseCommitmentSecret ,
137+ SignerOp :: SignCounterpartyCommitment ,
138+ ] . into_iter ( ) . filter ( |& op| i & ( 1 << op as u8 ) != 0 ) . collect ( ) ;
139+ do_test_async_commitment_signature_for_commitment_signed_revoke_and_ack ( enable_signer_op_order) ;
140+ }
132141}
133142
134- fn do_test_async_commitment_signature_for_commitment_signed_revoke_and_ack ( test_case : u8 ) {
143+ fn do_test_async_commitment_signature_for_commitment_signed_revoke_and_ack ( enable_signer_op_order : Vec < SignerOp > ) {
135144 let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
136145 let node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
137146 let node_chanmgrs = create_node_chanmgrs ( 2 , & node_cfgs, & [ None , None ] ) ;
@@ -160,31 +169,33 @@ fn do_test_async_commitment_signature_for_commitment_signed_revoke_and_ack(test_
160169 // Mark dst's signer as unavailable and handle src's commitment_signed: while dst won't yet have a
161170 // `commitment_signed` of its own to offer, it should publish a `revoke_and_ack`.
162171 dst. disable_channel_signer_op ( & src. node . get_our_node_id ( ) , & chan_id, SignerOp :: GetPerCommitmentPoint ) ;
172+ dst. disable_channel_signer_op ( & src. node . get_our_node_id ( ) , & chan_id, SignerOp :: ReleaseCommitmentSecret ) ;
163173 dst. disable_channel_signer_op ( & src. node . get_our_node_id ( ) , & chan_id, SignerOp :: SignCounterpartyCommitment ) ;
164174 dst. node . handle_commitment_signed ( & src. node . get_our_node_id ( ) , & payment_event. commitment_msg ) ;
165175 check_added_monitors ( dst, 1 ) ;
166176
167- if test_case == 0 {
168- // Unblock CS -> no messages should be sent, since we must send RAA first.
169- dst. enable_channel_signer_op ( & src. node . get_our_node_id ( ) , & chan_id, SignerOp :: SignCounterpartyCommitment ) ;
170- dst. node . signer_unblocked ( Some ( ( src. node . get_our_node_id ( ) , chan_id) ) ) ;
171- let events = dst. node . get_and_clear_pending_msg_events ( ) ;
172- assert ! ( events. is_empty( ) , "expected no message, got {}" , events. len( ) ) ;
173-
174- // Unblock revoke_and_ack -> we should send both RAA + CS.
175- dst. enable_channel_signer_op ( & src. node . get_our_node_id ( ) , & chan_id, SignerOp :: GetPerCommitmentPoint ) ;
176- dst. node . signer_unblocked ( Some ( ( src. node . get_our_node_id ( ) , chan_id) ) ) ;
177- get_revoke_commit_msgs ( & dst, & src. node . get_our_node_id ( ) ) ;
178- } else if test_case == 1 {
179- // Unblock revoke_and_ack -> we should send just RAA.
180- dst. enable_channel_signer_op ( & src. node . get_our_node_id ( ) , & chan_id, SignerOp :: GetPerCommitmentPoint ) ;
177+ let mut enabled_signer_ops = HashSet :: new ( ) ;
178+ log_trace ! ( dst. logger, "enable_signer_op_order={:?}" , enable_signer_op_order) ;
179+ for op in enable_signer_op_order {
180+ enabled_signer_ops. insert ( op) ;
181+ dst. enable_channel_signer_op ( & src. node . get_our_node_id ( ) , & chan_id, op) ;
181182 dst. node . signer_unblocked ( Some ( ( src. node . get_our_node_id ( ) , chan_id) ) ) ;
182- get_event_msg ! ( dst, MessageSendEvent :: SendRevokeAndACK , src. node. get_our_node_id( ) ) ;
183183
184- // Unblock commitment signed -> we should send CS.
185- dst. enable_channel_signer_op ( & src. node . get_our_node_id ( ) , & chan_id, SignerOp :: SignCounterpartyCommitment ) ;
186- dst. node . signer_unblocked ( Some ( ( src. node . get_our_node_id ( ) , chan_id) ) ) ;
187- get_htlc_update_msgs ( dst, & src. node . get_our_node_id ( ) ) ;
184+ if enabled_signer_ops. contains ( & SignerOp :: GetPerCommitmentPoint ) && enabled_signer_ops. contains ( & SignerOp :: ReleaseCommitmentSecret ) {
185+ // We are just able to send revoke_and_ack
186+ if op == SignerOp :: GetPerCommitmentPoint || op == SignerOp :: ReleaseCommitmentSecret {
187+ get_event_msg ! ( dst, MessageSendEvent :: SendRevokeAndACK , src. node. get_our_node_id( ) ) ;
188+ }
189+ // We either just sent or previously sent revoke_and_ack
190+ // and now we are able to send commitment_signed
191+ if op == SignerOp :: SignCounterpartyCommitment {
192+ get_htlc_update_msgs ( dst, & src. node . get_our_node_id ( ) ) ;
193+ }
194+ } else {
195+ // We can't send either message until RAA is unblocked
196+ let events = dst. node . get_and_clear_pending_msg_events ( ) ;
197+ assert ! ( events. is_empty( ) , "expected no message, got {}" , events. len( ) ) ;
198+ }
188199 }
189200}
190201
0 commit comments