@@ -1785,8 +1785,8 @@ impl ChannelManager {
1785
1785
}
1786
1786
}
1787
1787
1788
- fn internal_shutdown ( & self , their_node_id : & PublicKey , msg : & msgs:: Shutdown ) -> Result < ( Option < msgs :: Shutdown > , Option < msgs :: ClosingSigned > ) , MsgHandleErrInternal > {
1789
- let ( mut res , chan_option) = {
1788
+ fn internal_shutdown ( & self , their_node_id : & PublicKey , msg : & msgs:: Shutdown ) -> Result < ( ) , MsgHandleErrInternal > {
1789
+ let ( mut dropped_htlcs , chan_option) = {
1790
1790
let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
1791
1791
let channel_state = channel_state_lock. borrow_parts ( ) ;
1792
1792
@@ -1796,18 +1796,30 @@ impl ChannelManager {
1796
1796
//TODO: here and below MsgHandleErrInternal, #153 case
1797
1797
return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Got a message for a channel from the wrong node!" , msg. channel_id ) ) ;
1798
1798
}
1799
- let res = chan_entry. get_mut ( ) . shutdown ( & * self . fee_estimator , & msg) . map_err ( |e| MsgHandleErrInternal :: from_maybe_close ( e) ) ?;
1799
+ let ( shutdown, closing_signed, dropped_htlcs) = chan_entry. get_mut ( ) . shutdown ( & * self . fee_estimator , & msg) . map_err ( |e| MsgHandleErrInternal :: from_maybe_close ( e) ) ?;
1800
+ if let Some ( msg) = shutdown {
1801
+ channel_state. pending_msg_events . push ( events:: MessageSendEvent :: SendShutdown {
1802
+ node_id : their_node_id. clone ( ) ,
1803
+ msg,
1804
+ } ) ;
1805
+ }
1806
+ if let Some ( msg) = closing_signed {
1807
+ channel_state. pending_msg_events . push ( events:: MessageSendEvent :: SendClosingSigned {
1808
+ node_id : their_node_id. clone ( ) ,
1809
+ msg,
1810
+ } ) ;
1811
+ }
1800
1812
if chan_entry. get ( ) . is_shutdown ( ) {
1801
1813
if let Some ( short_id) = chan_entry. get ( ) . get_short_channel_id ( ) {
1802
1814
channel_state. short_to_id . remove ( & short_id) ;
1803
1815
}
1804
- ( res , Some ( chan_entry. remove_entry ( ) . 1 ) )
1805
- } else { ( res , None ) }
1816
+ ( dropped_htlcs , Some ( chan_entry. remove_entry ( ) . 1 ) )
1817
+ } else { ( dropped_htlcs , None ) }
1806
1818
} ,
1807
1819
hash_map:: Entry :: Vacant ( _) => return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Failed to find corresponding channel" , msg. channel_id ) )
1808
1820
}
1809
1821
} ;
1810
- for htlc_source in res . 2 . drain ( ..) {
1822
+ for htlc_source in dropped_htlcs . drain ( ..) {
1811
1823
// unknown_next_peer...I dunno who that is anymore....
1812
1824
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 ( ) } ) ;
1813
1825
}
@@ -1819,11 +1831,11 @@ impl ChannelManager {
1819
1831
} ) ;
1820
1832
}
1821
1833
}
1822
- Ok ( ( res . 0 , res . 1 ) )
1834
+ Ok ( ( ) )
1823
1835
}
1824
1836
1825
- fn internal_closing_signed ( & self , their_node_id : & PublicKey , msg : & msgs:: ClosingSigned ) -> Result < Option < msgs :: ClosingSigned > , MsgHandleErrInternal > {
1826
- let ( res , chan_option) = {
1837
+ fn internal_closing_signed ( & self , their_node_id : & PublicKey , msg : & msgs:: ClosingSigned ) -> Result < ( ) , MsgHandleErrInternal > {
1838
+ let ( tx , chan_option) = {
1827
1839
let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
1828
1840
let channel_state = channel_state_lock. borrow_parts ( ) ;
1829
1841
match channel_state. by_id . entry ( msg. channel_id . clone ( ) ) {
@@ -1832,8 +1844,14 @@ impl ChannelManager {
1832
1844
//TODO: here and below MsgHandleErrInternal, #153 case
1833
1845
return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Got a message for a channel from the wrong node!" , msg. channel_id ) ) ;
1834
1846
}
1835
- let res = chan_entry. get_mut ( ) . closing_signed ( & * self . fee_estimator , & msg) . map_err ( |e| MsgHandleErrInternal :: from_maybe_close ( e) ) ?;
1836
- if res. 1 . is_some ( ) {
1847
+ let ( closing_signed, tx) = chan_entry. get_mut ( ) . closing_signed ( & * self . fee_estimator , & msg) . map_err ( |e| MsgHandleErrInternal :: from_maybe_close ( e) ) ?;
1848
+ if let Some ( msg) = closing_signed {
1849
+ channel_state. pending_msg_events . push ( events:: MessageSendEvent :: SendClosingSigned {
1850
+ node_id : their_node_id. clone ( ) ,
1851
+ msg,
1852
+ } ) ;
1853
+ }
1854
+ if tx. is_some ( ) {
1837
1855
// We're done with this channel, we've got a signed closing transaction and
1838
1856
// will send the closing_signed back to the remote peer upon return. This
1839
1857
// also implies there are no pending HTLCs left on the channel, so we can
@@ -1842,13 +1860,13 @@ impl ChannelManager {
1842
1860
if let Some ( short_id) = chan_entry. get ( ) . get_short_channel_id ( ) {
1843
1861
channel_state. short_to_id . remove ( & short_id) ;
1844
1862
}
1845
- ( res , Some ( chan_entry. remove_entry ( ) . 1 ) )
1846
- } else { ( res , None ) }
1863
+ ( tx , Some ( chan_entry. remove_entry ( ) . 1 ) )
1864
+ } else { ( tx , None ) }
1847
1865
} ,
1848
1866
hash_map:: Entry :: Vacant ( _) => return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Failed to find corresponding channel" , msg. channel_id ) )
1849
1867
}
1850
1868
} ;
1851
- if let Some ( broadcast_tx) = res . 1 {
1869
+ if let Some ( broadcast_tx) = tx {
1852
1870
self . tx_broadcaster . broadcast_transaction ( & broadcast_tx) ;
1853
1871
}
1854
1872
if let Some ( chan) = chan_option {
@@ -1859,7 +1877,7 @@ impl ChannelManager {
1859
1877
} ) ;
1860
1878
}
1861
1879
}
1862
- Ok ( res . 0 )
1880
+ Ok ( ( ) )
1863
1881
}
1864
1882
1865
1883
fn internal_update_add_htlc ( & self , their_node_id : & PublicKey , msg : & msgs:: UpdateAddHTLC ) -> Result < ( ) , MsgHandleErrInternal > {
@@ -2522,11 +2540,11 @@ impl ChannelMessageHandler for ChannelManager {
2522
2540
handle_error ! ( self , self . internal_funding_locked( their_node_id, msg) , their_node_id)
2523
2541
}
2524
2542
2525
- fn handle_shutdown ( & self , their_node_id : & PublicKey , msg : & msgs:: Shutdown ) -> Result < ( Option < msgs :: Shutdown > , Option < msgs :: ClosingSigned > ) , HandleError > {
2543
+ fn handle_shutdown ( & self , their_node_id : & PublicKey , msg : & msgs:: Shutdown ) -> Result < ( ) , HandleError > {
2526
2544
handle_error ! ( self , self . internal_shutdown( their_node_id, msg) , their_node_id)
2527
2545
}
2528
2546
2529
- fn handle_closing_signed ( & self , their_node_id : & PublicKey , msg : & msgs:: ClosingSigned ) -> Result < Option < msgs :: ClosingSigned > , HandleError > {
2547
+ fn handle_closing_signed ( & self , their_node_id : & PublicKey , msg : & msgs:: ClosingSigned ) -> Result < ( ) , HandleError > {
2530
2548
handle_error ! ( self , self . internal_closing_signed( their_node_id, msg) , their_node_id)
2531
2549
}
2532
2550
@@ -3050,68 +3068,91 @@ mod tests {
3050
3068
}
3051
3069
3052
3070
fn close_channel ( outbound_node : & Node , inbound_node : & Node , channel_id : & [ u8 ; 32 ] , funding_tx : Transaction , close_inbound_first : bool ) -> ( msgs:: ChannelUpdate , msgs:: ChannelUpdate ) {
3053
- let ( node_a, broadcaster_a) = if close_inbound_first { ( & inbound_node. node , & inbound_node. tx_broadcaster ) } else { ( & outbound_node. node , & outbound_node. tx_broadcaster ) } ;
3071
+ 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 ) } ;
3054
3072
let ( node_b, broadcaster_b) = if close_inbound_first { ( & outbound_node. node , & outbound_node. tx_broadcaster ) } else { ( & inbound_node. node , & inbound_node. tx_broadcaster ) } ;
3055
3073
let ( tx_a, tx_b) ;
3056
3074
3057
3075
node_a. close_channel ( channel_id) . unwrap ( ) ;
3058
- let events_1 = node_a. get_and_clear_pending_msg_events ( ) ;
3059
- assert_eq ! ( events_1. len( ) , 1 ) ;
3060
- let shutdown_a = match events_1[ 0 ] {
3076
+ node_b. handle_shutdown ( & node_a. get_our_node_id ( ) , & get_event_msg ! ( struct_a, MessageSendEvent :: SendShutdown , node_b. get_our_node_id( ) ) ) . unwrap ( ) ;
3077
+
3078
+ let events_1 = node_b. get_and_clear_pending_msg_events ( ) ;
3079
+ assert ! ( events_1. len( ) >= 1 ) ;
3080
+ let shutdown_b = match events_1[ 0 ] {
3061
3081
MessageSendEvent :: SendShutdown { ref node_id, ref msg } => {
3062
- assert_eq ! ( node_id, & node_b . get_our_node_id( ) ) ;
3082
+ assert_eq ! ( node_id, & node_a . get_our_node_id( ) ) ;
3063
3083
msg. clone ( )
3064
3084
} ,
3065
3085
_ => panic ! ( "Unexpected event" ) ,
3066
3086
} ;
3067
3087
3068
- let ( shutdown_b, mut closing_signed_b) = node_b. handle_shutdown ( & node_a. get_our_node_id ( ) , & shutdown_a) . unwrap ( ) ;
3069
- if !close_inbound_first {
3070
- assert ! ( closing_signed_b. is_none( ) ) ;
3088
+ let closing_signed_b = if !close_inbound_first {
3089
+ assert_eq ! ( events_1. len( ) , 1 ) ;
3090
+ None
3091
+ } else {
3092
+ Some ( match events_1[ 1 ] {
3093
+ MessageSendEvent :: SendClosingSigned { ref node_id, ref msg } => {
3094
+ assert_eq ! ( node_id, & node_a. get_our_node_id( ) ) ;
3095
+ msg. clone ( )
3096
+ } ,
3097
+ _ => panic ! ( "Unexpected event" ) ,
3098
+ } )
3099
+ } ;
3100
+
3101
+ macro_rules! get_closing_signed_broadcast {
3102
+ ( $node: expr, $dest_pubkey: expr) => {
3103
+ {
3104
+ let events = $node. get_and_clear_pending_msg_events( ) ;
3105
+ assert!( events. len( ) == 1 || events. len( ) == 2 ) ;
3106
+ ( match events[ events. len( ) - 1 ] {
3107
+ MessageSendEvent :: BroadcastChannelUpdate { ref msg } => {
3108
+ msg. clone( )
3109
+ } ,
3110
+ _ => panic!( "Unexpected event" ) ,
3111
+ } , if events. len( ) == 2 {
3112
+ match events[ 0 ] {
3113
+ MessageSendEvent :: SendClosingSigned { ref node_id, ref msg } => {
3114
+ assert_eq!( * node_id, $dest_pubkey) ;
3115
+ Some ( msg. clone( ) )
3116
+ } ,
3117
+ _ => panic!( "Unexpected event" ) ,
3118
+ }
3119
+ } else { None } )
3120
+ }
3121
+ }
3071
3122
}
3072
- let ( empty_a , mut closing_signed_a ) = node_a . handle_shutdown ( & node_b . get_our_node_id ( ) , & shutdown_b . unwrap ( ) ) . unwrap ( ) ;
3073
- assert ! ( empty_a . is_none ( ) ) ;
3074
- if close_inbound_first {
3075
- assert ! ( closing_signed_a . is_none ( ) ) ;
3076
- closing_signed_a = node_a. handle_closing_signed ( & node_b. get_our_node_id ( ) , & closing_signed_b. unwrap ( ) ) . unwrap ( ) ;
3123
+
3124
+ node_a . handle_shutdown ( & node_b . get_our_node_id ( ) , & shutdown_b ) . unwrap ( ) ;
3125
+ let ( as_update , bs_update ) = if close_inbound_first {
3126
+ assert ! ( node_a . get_and_clear_pending_msg_events ( ) . is_empty ( ) ) ;
3127
+ node_a. handle_closing_signed ( & node_b. get_our_node_id ( ) , & closing_signed_b. unwrap ( ) ) . unwrap ( ) ;
3077
3128
assert_eq ! ( broadcaster_a. txn_broadcasted. lock( ) . unwrap( ) . len( ) , 1 ) ;
3078
3129
tx_a = broadcaster_a. txn_broadcasted . lock ( ) . unwrap ( ) . remove ( 0 ) ;
3130
+ let ( as_update, closing_signed_a) = get_closing_signed_broadcast ! ( node_a, node_b. get_our_node_id( ) ) ;
3079
3131
3080
- let empty_b = node_b. handle_closing_signed ( & node_a. get_our_node_id ( ) , & closing_signed_a. unwrap ( ) ) . unwrap ( ) ;
3081
- assert ! ( empty_b. is_none( ) ) ;
3132
+ node_b. handle_closing_signed ( & node_a. get_our_node_id ( ) , & closing_signed_a. unwrap ( ) ) . unwrap ( ) ;
3133
+ let ( bs_update, none_b) = get_closing_signed_broadcast ! ( node_b, node_a. get_our_node_id( ) ) ;
3134
+ assert ! ( none_b. is_none( ) ) ;
3082
3135
assert_eq ! ( broadcaster_b. txn_broadcasted. lock( ) . unwrap( ) . len( ) , 1 ) ;
3083
3136
tx_b = broadcaster_b. txn_broadcasted . lock ( ) . unwrap ( ) . remove ( 0 ) ;
3137
+ ( as_update, bs_update)
3084
3138
} else {
3085
- closing_signed_b = node_b. handle_closing_signed ( & node_a. get_our_node_id ( ) , & closing_signed_a. unwrap ( ) ) . unwrap ( ) ;
3139
+ let closing_signed_a = get_event_msg ! ( struct_a, MessageSendEvent :: SendClosingSigned , node_b. get_our_node_id( ) ) ;
3140
+
3141
+ node_b. handle_closing_signed ( & node_a. get_our_node_id ( ) , & closing_signed_a) . unwrap ( ) ;
3086
3142
assert_eq ! ( broadcaster_b. txn_broadcasted. lock( ) . unwrap( ) . len( ) , 1 ) ;
3087
3143
tx_b = broadcaster_b. txn_broadcasted . lock ( ) . unwrap ( ) . remove ( 0 ) ;
3144
+ let ( bs_update, closing_signed_b) = get_closing_signed_broadcast ! ( node_b, node_a. get_our_node_id( ) ) ;
3088
3145
3089
- let empty_a2 = node_a. handle_closing_signed ( & node_b. get_our_node_id ( ) , & closing_signed_b. unwrap ( ) ) . unwrap ( ) ;
3090
- assert ! ( empty_a2. is_none( ) ) ;
3146
+ node_a. handle_closing_signed ( & node_b. get_our_node_id ( ) , & closing_signed_b. unwrap ( ) ) . unwrap ( ) ;
3147
+ let ( as_update, none_a) = get_closing_signed_broadcast ! ( node_a, node_b. get_our_node_id( ) ) ;
3148
+ assert ! ( none_a. is_none( ) ) ;
3091
3149
assert_eq ! ( broadcaster_a. txn_broadcasted. lock( ) . unwrap( ) . len( ) , 1 ) ;
3092
3150
tx_a = broadcaster_a. txn_broadcasted . lock ( ) . unwrap ( ) . remove ( 0 ) ;
3093
- }
3151
+ ( as_update, bs_update)
3152
+ } ;
3094
3153
assert_eq ! ( tx_a, tx_b) ;
3095
3154
check_spends ! ( tx_a, funding_tx) ;
3096
3155
3097
- let events_2 = node_a. get_and_clear_pending_msg_events ( ) ;
3098
- assert_eq ! ( events_2. len( ) , 1 ) ;
3099
- let as_update = match events_2[ 0 ] {
3100
- MessageSendEvent :: BroadcastChannelUpdate { ref msg } => {
3101
- msg. clone ( )
3102
- } ,
3103
- _ => panic ! ( "Unexpected event" ) ,
3104
- } ;
3105
-
3106
- let events_3 = node_b. get_and_clear_pending_msg_events ( ) ;
3107
- assert_eq ! ( events_3. len( ) , 1 ) ;
3108
- let bs_update = match events_3[ 0 ] {
3109
- MessageSendEvent :: BroadcastChannelUpdate { ref msg } => {
3110
- msg. clone ( )
3111
- } ,
3112
- _ => panic ! ( "Unexpected event" ) ,
3113
- } ;
3114
-
3115
3156
( as_update, bs_update)
3116
3157
}
3117
3158
0 commit comments