1111//! properly with a signer implementation that asynchronously derives signatures.
1212
1313use crate :: events:: { MessageSendEvent , MessageSendEventsProvider } ;
14- use crate :: ln:: msgs:: ChannelMessageHandler ;
15-
1614use crate :: ln:: functional_test_utils:: * ;
15+ use crate :: ln:: msgs:: ChannelMessageHandler ;
16+ use crate :: ln:: channelmanager:: { PaymentId , RecipientOnionFields } ;
1717
1818#[ test]
1919fn test_async_commitment_signature_for_funding_created ( ) {
@@ -24,7 +24,7 @@ fn test_async_commitment_signature_for_funding_created() {
2424 let nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
2525
2626 nodes[ 0 ] . node . create_channel ( nodes[ 1 ] . node . get_our_node_id ( ) , 100000 , 10001 , 42 , None ) . unwrap ( ) ;
27-
27+
2828 // nodes[0] --- open_channel --> nodes[1]
2929 let mut open_chan_msg = get_event_msg ! ( nodes[ 0 ] , MessageSendEvent :: SendOpenChannel , nodes[ 1 ] . node. get_our_node_id( ) ) ;
3030 nodes[ 1 ] . node . handle_open_channel ( & nodes[ 0 ] . node . get_our_node_id ( ) , & open_chan_msg) ;
@@ -130,3 +130,62 @@ fn test_async_commitment_signature_for_funding_signed() {
130130 check_added_monitors ( & nodes[ 0 ] , 1 ) ;
131131 expect_channel_pending_event ( & nodes[ 0 ] , & nodes[ 1 ] . node . get_our_node_id ( ) ) ;
132132}
133+
134+ #[ test]
135+ fn test_async_commitment_signature_for_commitment_signed ( ) {
136+ let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
137+ let node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
138+ let node_chanmgrs = create_node_chanmgrs ( 2 , & node_cfgs, & [ None , None ] ) ;
139+ let nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
140+ create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
141+
142+ let chan_id = {
143+ let per_peer_state = nodes[ 0 ] . node . per_peer_state . read ( ) . unwrap ( ) ;
144+ let chan_lock = per_peer_state. get ( & nodes[ 1 ] . node . get_our_node_id ( ) ) . unwrap ( ) . lock ( ) . unwrap ( ) ;
145+ let chan_ids = chan_lock. channel_by_id . keys ( ) . collect :: < Vec < _ > > ( ) ;
146+ let n = chan_ids. len ( ) ;
147+ assert_eq ! ( n, 1 , "expected one channel, not {n}" ) ;
148+ * chan_ids[ 0 ]
149+ } ;
150+
151+ // Send a payment.
152+ let src = & nodes[ 0 ] ;
153+ let dst = & nodes[ 1 ] ;
154+ let ( route, our_payment_hash, _our_payment_preimage, our_payment_secret) = get_route_and_payment_hash ! ( src, dst, 8000000 ) ;
155+ src. node . send_payment_with_route ( & route, our_payment_hash,
156+ RecipientOnionFields :: secret_only ( our_payment_secret) , PaymentId ( our_payment_hash. 0 ) ) . unwrap ( ) ;
157+ check_added_monitors ! ( src, 1 ) ;
158+
159+ // Pass the payment along the route.
160+ let payment_event = {
161+ let mut events = src. node . get_and_clear_pending_msg_events ( ) ;
162+ assert_eq ! ( events. len( ) , 1 ) ;
163+ SendEvent :: from_event ( events. remove ( 0 ) )
164+ } ;
165+ assert_eq ! ( payment_event. node_id, dst. node. get_our_node_id( ) ) ;
166+ assert_eq ! ( payment_event. msgs. len( ) , 1 ) ;
167+
168+ dst. node . handle_update_add_htlc ( & src. node . get_our_node_id ( ) , & payment_event. msgs [ 0 ] ) ;
169+
170+ // Mark dst's signer as unavailable and handle src's commitment_signed: while dst won't yet have a
171+ // `commitment_signed` of its own to offer, it should publish a `revoke_and_ack`.
172+ dst. set_channel_signer_available ( & src. node . get_our_node_id ( ) , & chan_id, false ) ;
173+ dst. node . handle_commitment_signed ( & src. node . get_our_node_id ( ) , & payment_event. commitment_msg ) ;
174+ check_added_monitors ( dst, 1 ) ;
175+
176+ get_event_msg ! ( dst, MessageSendEvent :: SendRevokeAndACK , src. node. get_our_node_id( ) ) ;
177+
178+ // Mark dst's signer as available and retry: we now expect to see dst's `commitment_signed`.
179+ dst. set_channel_signer_available ( & src. node . get_our_node_id ( ) , & chan_id, true ) ;
180+ dst. node . signer_unblocked ( Some ( ( src. node . get_our_node_id ( ) , chan_id) ) ) ;
181+
182+ let events = dst. node . get_and_clear_pending_msg_events ( ) ;
183+ let n = events. len ( ) ;
184+ assert_eq ! ( n, 1 , "expected one message, got {n}" ) ;
185+ if let MessageSendEvent :: UpdateHTLCs { ref node_id, ref updates } = events[ 0 ] {
186+ assert_eq ! ( node_id, & src. node. get_our_node_id( ) ) ;
187+ } else {
188+ panic ! ( "expected UpdateHTLCs message, not {:?}" , events[ 0 ] ) ;
189+ } ;
190+ }
191+
0 commit comments