@@ -1723,8 +1723,8 @@ impl ChannelManager {
1723
1723
}
1724
1724
}
1725
1725
1726
- fn internal_shutdown ( & self , their_node_id : & PublicKey , msg : & msgs:: Shutdown ) -> Result < ( Option < msgs :: Shutdown > , Option < msgs :: ClosingSigned > ) , MsgHandleErrInternal > {
1727
- let ( mut res , chan_option) = {
1726
+ fn internal_shutdown ( & self , their_node_id : & PublicKey , msg : & msgs:: Shutdown ) -> Result < ( ) , MsgHandleErrInternal > {
1727
+ let ( mut dropped_htlcs , chan_option) = {
1728
1728
let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
1729
1729
let channel_state = channel_state_lock. borrow_parts ( ) ;
1730
1730
@@ -1734,18 +1734,30 @@ impl ChannelManager {
1734
1734
//TODO: here and below MsgHandleErrInternal, #153 case
1735
1735
return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Got a message for a channel from the wrong node!" , msg. channel_id ) ) ;
1736
1736
}
1737
- let res = chan_entry. get_mut ( ) . shutdown ( & * self . fee_estimator , & msg) . map_err ( |e| MsgHandleErrInternal :: from_maybe_close ( e) ) ?;
1737
+ let ( shutdown, closing_signed, dropped_htlcs) = chan_entry. get_mut ( ) . shutdown ( & * self . fee_estimator , & msg) . map_err ( |e| MsgHandleErrInternal :: from_maybe_close ( e) ) ?;
1738
+ if let Some ( msg) = shutdown {
1739
+ channel_state. pending_msg_events . push ( events:: MessageSendEvent :: SendShutdown {
1740
+ node_id : their_node_id. clone ( ) ,
1741
+ msg,
1742
+ } ) ;
1743
+ }
1744
+ if let Some ( msg) = closing_signed {
1745
+ channel_state. pending_msg_events . push ( events:: MessageSendEvent :: SendClosingSigned {
1746
+ node_id : their_node_id. clone ( ) ,
1747
+ msg,
1748
+ } ) ;
1749
+ }
1738
1750
if chan_entry. get ( ) . is_shutdown ( ) {
1739
1751
if let Some ( short_id) = chan_entry. get ( ) . get_short_channel_id ( ) {
1740
1752
channel_state. short_to_id . remove ( & short_id) ;
1741
1753
}
1742
- ( res , Some ( chan_entry. remove_entry ( ) . 1 ) )
1743
- } else { ( res , None ) }
1754
+ ( dropped_htlcs , Some ( chan_entry. remove_entry ( ) . 1 ) )
1755
+ } else { ( dropped_htlcs , None ) }
1744
1756
} ,
1745
1757
hash_map:: Entry :: Vacant ( _) => return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Failed to find corresponding channel" , msg. channel_id ) )
1746
1758
}
1747
1759
} ;
1748
- for htlc_source in res . 2 . drain ( ..) {
1760
+ for htlc_source in dropped_htlcs . drain ( ..) {
1749
1761
// unknown_next_peer...I dunno who that is anymore....
1750
1762
self . fail_htlc_backwards_internal ( self . channel_state . lock ( ) . unwrap ( ) , htlc_source. 0 , & htlc_source. 1 , HTLCFailReason :: Reason { failure_code : 0x4000 | 10 , data : Vec :: new ( ) } ) ;
1751
1763
}
@@ -1757,11 +1769,11 @@ impl ChannelManager {
1757
1769
} ) ;
1758
1770
}
1759
1771
}
1760
- Ok ( ( res . 0 , res . 1 ) )
1772
+ Ok ( ( ) )
1761
1773
}
1762
1774
1763
- fn internal_closing_signed ( & self , their_node_id : & PublicKey , msg : & msgs:: ClosingSigned ) -> Result < Option < msgs :: ClosingSigned > , MsgHandleErrInternal > {
1764
- let ( res , chan_option) = {
1775
+ fn internal_closing_signed ( & self , their_node_id : & PublicKey , msg : & msgs:: ClosingSigned ) -> Result < ( ) , MsgHandleErrInternal > {
1776
+ let ( tx , chan_option) = {
1765
1777
let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
1766
1778
let channel_state = channel_state_lock. borrow_parts ( ) ;
1767
1779
match channel_state. by_id . entry ( msg. channel_id . clone ( ) ) {
@@ -1770,8 +1782,14 @@ impl ChannelManager {
1770
1782
//TODO: here and below MsgHandleErrInternal, #153 case
1771
1783
return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Got a message for a channel from the wrong node!" , msg. channel_id ) ) ;
1772
1784
}
1773
- let res = chan_entry. get_mut ( ) . closing_signed ( & * self . fee_estimator , & msg) . map_err ( |e| MsgHandleErrInternal :: from_maybe_close ( e) ) ?;
1774
- if res. 1 . is_some ( ) {
1785
+ let ( closing_signed, tx) = chan_entry. get_mut ( ) . closing_signed ( & * self . fee_estimator , & msg) . map_err ( |e| MsgHandleErrInternal :: from_maybe_close ( e) ) ?;
1786
+ if let Some ( msg) = closing_signed {
1787
+ channel_state. pending_msg_events . push ( events:: MessageSendEvent :: SendClosingSigned {
1788
+ node_id : their_node_id. clone ( ) ,
1789
+ msg,
1790
+ } ) ;
1791
+ }
1792
+ if tx. is_some ( ) {
1775
1793
// We're done with this channel, we've got a signed closing transaction and
1776
1794
// will send the closing_signed back to the remote peer upon return. This
1777
1795
// also implies there are no pending HTLCs left on the channel, so we can
@@ -1780,13 +1798,13 @@ impl ChannelManager {
1780
1798
if let Some ( short_id) = chan_entry. get ( ) . get_short_channel_id ( ) {
1781
1799
channel_state. short_to_id . remove ( & short_id) ;
1782
1800
}
1783
- ( res , Some ( chan_entry. remove_entry ( ) . 1 ) )
1784
- } else { ( res , None ) }
1801
+ ( tx , Some ( chan_entry. remove_entry ( ) . 1 ) )
1802
+ } else { ( tx , None ) }
1785
1803
} ,
1786
1804
hash_map:: Entry :: Vacant ( _) => return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Failed to find corresponding channel" , msg. channel_id ) )
1787
1805
}
1788
1806
} ;
1789
- if let Some ( broadcast_tx) = res . 1 {
1807
+ if let Some ( broadcast_tx) = tx {
1790
1808
self . tx_broadcaster . broadcast_transaction ( & broadcast_tx) ;
1791
1809
}
1792
1810
if let Some ( chan) = chan_option {
@@ -1797,7 +1815,7 @@ impl ChannelManager {
1797
1815
} ) ;
1798
1816
}
1799
1817
}
1800
- Ok ( res . 0 )
1818
+ Ok ( ( ) )
1801
1819
}
1802
1820
1803
1821
fn internal_update_add_htlc ( & self , their_node_id : & PublicKey , msg : & msgs:: UpdateAddHTLC ) -> Result < ( ) , MsgHandleErrInternal > {
@@ -2320,11 +2338,11 @@ impl ChannelMessageHandler for ChannelManager {
2320
2338
handle_error ! ( self , self . internal_funding_locked( their_node_id, msg) , their_node_id)
2321
2339
}
2322
2340
2323
- fn handle_shutdown ( & self , their_node_id : & PublicKey , msg : & msgs:: Shutdown ) -> Result < ( Option < msgs :: Shutdown > , Option < msgs :: ClosingSigned > ) , HandleError > {
2341
+ fn handle_shutdown ( & self , their_node_id : & PublicKey , msg : & msgs:: Shutdown ) -> Result < ( ) , HandleError > {
2324
2342
handle_error ! ( self , self . internal_shutdown( their_node_id, msg) , their_node_id)
2325
2343
}
2326
2344
2327
- fn handle_closing_signed ( & self , their_node_id : & PublicKey , msg : & msgs:: ClosingSigned ) -> Result < Option < msgs :: ClosingSigned > , HandleError > {
2345
+ fn handle_closing_signed ( & self , their_node_id : & PublicKey , msg : & msgs:: ClosingSigned ) -> Result < ( ) , HandleError > {
2328
2346
handle_error ! ( self , self . internal_closing_signed( their_node_id, msg) , their_node_id)
2329
2347
}
2330
2348
@@ -2847,68 +2865,91 @@ mod tests {
2847
2865
}
2848
2866
2849
2867
fn close_channel ( outbound_node : & Node , inbound_node : & Node , channel_id : & [ u8 ; 32 ] , funding_tx : Transaction , close_inbound_first : bool ) -> ( msgs:: ChannelUpdate , msgs:: ChannelUpdate ) {
2850
- let ( node_a, broadcaster_a) = if close_inbound_first { ( & inbound_node. node , & inbound_node. tx_broadcaster ) } else { ( & outbound_node. node , & outbound_node. tx_broadcaster ) } ;
2868
+ let ( node_a, broadcaster_a, struct_a ) = if close_inbound_first { ( & inbound_node. node , & inbound_node. tx_broadcaster , inbound_node ) } else { ( & outbound_node. node , & outbound_node. tx_broadcaster , outbound_node ) } ;
2851
2869
let ( node_b, broadcaster_b) = if close_inbound_first { ( & outbound_node. node , & outbound_node. tx_broadcaster ) } else { ( & inbound_node. node , & inbound_node. tx_broadcaster ) } ;
2852
2870
let ( tx_a, tx_b) ;
2853
2871
2854
2872
node_a. close_channel ( channel_id) . unwrap ( ) ;
2855
- let events_1 = node_a. get_and_clear_pending_msg_events ( ) ;
2856
- assert_eq ! ( events_1. len( ) , 1 ) ;
2857
- let shutdown_a = match events_1[ 0 ] {
2873
+ node_b. handle_shutdown ( & node_a. get_our_node_id ( ) , & get_event_msg ! ( struct_a, MessageSendEvent :: SendShutdown , node_b. get_our_node_id( ) ) ) . unwrap ( ) ;
2874
+
2875
+ let events_1 = node_b. get_and_clear_pending_msg_events ( ) ;
2876
+ assert ! ( events_1. len( ) >= 1 ) ;
2877
+ let shutdown_b = match events_1[ 0 ] {
2858
2878
MessageSendEvent :: SendShutdown { ref node_id, ref msg } => {
2859
- assert_eq ! ( node_id, & node_b . get_our_node_id( ) ) ;
2879
+ assert_eq ! ( node_id, & node_a . get_our_node_id( ) ) ;
2860
2880
msg. clone ( )
2861
2881
} ,
2862
2882
_ => panic ! ( "Unexpected event" ) ,
2863
2883
} ;
2864
2884
2865
- let ( shutdown_b, mut closing_signed_b) = node_b. handle_shutdown ( & node_a. get_our_node_id ( ) , & shutdown_a) . unwrap ( ) ;
2866
- if !close_inbound_first {
2867
- assert ! ( closing_signed_b. is_none( ) ) ;
2885
+ let closing_signed_b = if !close_inbound_first {
2886
+ assert_eq ! ( events_1. len( ) , 1 ) ;
2887
+ None
2888
+ } else {
2889
+ Some ( match events_1[ 1 ] {
2890
+ MessageSendEvent :: SendClosingSigned { ref node_id, ref msg } => {
2891
+ assert_eq ! ( node_id, & node_a. get_our_node_id( ) ) ;
2892
+ msg. clone ( )
2893
+ } ,
2894
+ _ => panic ! ( "Unexpected event" ) ,
2895
+ } )
2896
+ } ;
2897
+
2898
+ macro_rules! get_closing_signed_broadcast {
2899
+ ( $node: expr, $dest_pubkey: expr) => {
2900
+ {
2901
+ let events = $node. get_and_clear_pending_msg_events( ) ;
2902
+ assert!( events. len( ) == 1 || events. len( ) == 2 ) ;
2903
+ ( match events[ events. len( ) - 1 ] {
2904
+ MessageSendEvent :: BroadcastChannelUpdate { ref msg } => {
2905
+ msg. clone( )
2906
+ } ,
2907
+ _ => panic!( "Unexpected event" ) ,
2908
+ } , if events. len( ) == 2 {
2909
+ match events[ 0 ] {
2910
+ MessageSendEvent :: SendClosingSigned { ref node_id, ref msg } => {
2911
+ assert_eq!( * node_id, $dest_pubkey) ;
2912
+ Some ( msg. clone( ) )
2913
+ } ,
2914
+ _ => panic!( "Unexpected event" ) ,
2915
+ }
2916
+ } else { None } )
2917
+ }
2918
+ }
2868
2919
}
2869
- let ( empty_a , mut closing_signed_a ) = node_a . handle_shutdown ( & node_b . get_our_node_id ( ) , & shutdown_b . unwrap ( ) ) . unwrap ( ) ;
2870
- assert ! ( empty_a . is_none ( ) ) ;
2871
- if close_inbound_first {
2872
- assert ! ( closing_signed_a . is_none ( ) ) ;
2873
- closing_signed_a = node_a. handle_closing_signed ( & node_b. get_our_node_id ( ) , & closing_signed_b. unwrap ( ) ) . unwrap ( ) ;
2920
+
2921
+ node_a . handle_shutdown ( & node_b . get_our_node_id ( ) , & shutdown_b ) . unwrap ( ) ;
2922
+ let ( as_update , bs_update ) = if close_inbound_first {
2923
+ assert ! ( node_a . get_and_clear_pending_msg_events ( ) . is_empty ( ) ) ;
2924
+ node_a. handle_closing_signed ( & node_b. get_our_node_id ( ) , & closing_signed_b. unwrap ( ) ) . unwrap ( ) ;
2874
2925
assert_eq ! ( broadcaster_a. txn_broadcasted. lock( ) . unwrap( ) . len( ) , 1 ) ;
2875
2926
tx_a = broadcaster_a. txn_broadcasted . lock ( ) . unwrap ( ) . remove ( 0 ) ;
2927
+ let ( as_update, closing_signed_a) = get_closing_signed_broadcast ! ( node_a, node_b. get_our_node_id( ) ) ;
2876
2928
2877
- let empty_b = node_b. handle_closing_signed ( & node_a. get_our_node_id ( ) , & closing_signed_a. unwrap ( ) ) . unwrap ( ) ;
2878
- assert ! ( empty_b. is_none( ) ) ;
2929
+ node_b. handle_closing_signed ( & node_a. get_our_node_id ( ) , & closing_signed_a. unwrap ( ) ) . unwrap ( ) ;
2930
+ let ( bs_update, none_b) = get_closing_signed_broadcast ! ( node_b, node_a. get_our_node_id( ) ) ;
2931
+ assert ! ( none_b. is_none( ) ) ;
2879
2932
assert_eq ! ( broadcaster_b. txn_broadcasted. lock( ) . unwrap( ) . len( ) , 1 ) ;
2880
2933
tx_b = broadcaster_b. txn_broadcasted . lock ( ) . unwrap ( ) . remove ( 0 ) ;
2934
+ ( as_update, bs_update)
2881
2935
} else {
2882
- closing_signed_b = node_b. handle_closing_signed ( & node_a. get_our_node_id ( ) , & closing_signed_a. unwrap ( ) ) . unwrap ( ) ;
2936
+ let closing_signed_a = get_event_msg ! ( struct_a, MessageSendEvent :: SendClosingSigned , node_b. get_our_node_id( ) ) ;
2937
+
2938
+ node_b. handle_closing_signed ( & node_a. get_our_node_id ( ) , & closing_signed_a) . unwrap ( ) ;
2883
2939
assert_eq ! ( broadcaster_b. txn_broadcasted. lock( ) . unwrap( ) . len( ) , 1 ) ;
2884
2940
tx_b = broadcaster_b. txn_broadcasted . lock ( ) . unwrap ( ) . remove ( 0 ) ;
2941
+ let ( bs_update, closing_signed_b) = get_closing_signed_broadcast ! ( node_b, node_a. get_our_node_id( ) ) ;
2885
2942
2886
- let empty_a2 = node_a. handle_closing_signed ( & node_b. get_our_node_id ( ) , & closing_signed_b. unwrap ( ) ) . unwrap ( ) ;
2887
- assert ! ( empty_a2. is_none( ) ) ;
2943
+ node_a. handle_closing_signed ( & node_b. get_our_node_id ( ) , & closing_signed_b. unwrap ( ) ) . unwrap ( ) ;
2944
+ let ( as_update, none_a) = get_closing_signed_broadcast ! ( node_a, node_b. get_our_node_id( ) ) ;
2945
+ assert ! ( none_a. is_none( ) ) ;
2888
2946
assert_eq ! ( broadcaster_a. txn_broadcasted. lock( ) . unwrap( ) . len( ) , 1 ) ;
2889
2947
tx_a = broadcaster_a. txn_broadcasted . lock ( ) . unwrap ( ) . remove ( 0 ) ;
2890
- }
2948
+ ( as_update, bs_update)
2949
+ } ;
2891
2950
assert_eq ! ( tx_a, tx_b) ;
2892
2951
check_spends ! ( tx_a, funding_tx) ;
2893
2952
2894
- let events_2 = node_a. get_and_clear_pending_msg_events ( ) ;
2895
- assert_eq ! ( events_2. len( ) , 1 ) ;
2896
- let as_update = match events_2[ 0 ] {
2897
- MessageSendEvent :: BroadcastChannelUpdate { ref msg } => {
2898
- msg. clone ( )
2899
- } ,
2900
- _ => panic ! ( "Unexpected event" ) ,
2901
- } ;
2902
-
2903
- let events_3 = node_b. get_and_clear_pending_msg_events ( ) ;
2904
- assert_eq ! ( events_3. len( ) , 1 ) ;
2905
- let bs_update = match events_3[ 0 ] {
2906
- MessageSendEvent :: BroadcastChannelUpdate { ref msg } => {
2907
- msg. clone ( )
2908
- } ,
2909
- _ => panic ! ( "Unexpected event" ) ,
2910
- } ;
2911
-
2912
2953
( as_update, bs_update)
2913
2954
}
2914
2955
0 commit comments