@@ -26,7 +26,7 @@ use ln::channel::{Channel, ChannelError};
26
26
use ln:: channelmonitor:: { ChannelMonitorUpdateErr , ManyChannelMonitor , CLTV_CLAIM_BUFFER , HTLC_FAIL_TIMEOUT_BLOCKS } ;
27
27
use ln:: router:: { Route , RouteHop } ;
28
28
use ln:: msgs;
29
- use ln:: msgs:: { ChannelMessageHandler , HandleError , RAACommitmentOrder } ;
29
+ use ln:: msgs:: { ChannelMessageHandler , HandleError } ;
30
30
use chain:: keysinterface:: KeysInterface ;
31
31
use util:: { byte_utils, events, internal_traits, rng} ;
32
32
use util:: sha2:: Sha256 ;
@@ -244,6 +244,18 @@ struct HTLCForwardInfo {
244
244
forward_info : PendingForwardHTLCInfo ,
245
245
}
246
246
247
+ /// For events which result in both a RevokeAndACK and a CommitmentUpdate, by default they should
248
+ /// be sent in the order they appear in the return value, however sometimes the order needs to be
249
+ /// variable at runtime (eg Channel::channel_reestablish needs to re-send messages in the order
250
+ /// they were originally sent). In those cases, this enum is also returned.
251
+ #[ derive( Clone , PartialEq ) ]
252
+ pub ( super ) enum RAACommitmentOrder {
253
+ /// Send the CommitmentUpdate messages first
254
+ CommitmentFirst ,
255
+ /// Send the RevokeAndACK message first
256
+ RevokeAndACKFirst ,
257
+ }
258
+
247
259
struct ChannelHolder {
248
260
by_id : HashMap < [ u8 ; 32 ] , Channel > ,
249
261
short_to_id : HashMap < u64 , [ u8 ; 32 ] > ,
@@ -2287,28 +2299,58 @@ impl ChannelManager {
2287
2299
Ok ( ( ) )
2288
2300
}
2289
2301
2290
- fn internal_channel_reestablish ( & self , their_node_id : & PublicKey , msg : & msgs:: ChannelReestablish ) -> Result < ( Option < msgs:: FundingLocked > , Option < msgs:: RevokeAndACK > , Option < msgs:: CommitmentUpdate > , RAACommitmentOrder ) , MsgHandleErrInternal > {
2291
- let res = {
2292
- let mut channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
2293
- match channel_state. by_id . get_mut ( & msg. channel_id ) {
2294
- Some ( chan) => {
2295
- if chan. get_their_node_id ( ) != * their_node_id {
2296
- return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Got a message for a channel from the wrong node!" , msg. channel_id ) ) ;
2302
+ fn internal_channel_reestablish ( & self , their_node_id : & PublicKey , msg : & msgs:: ChannelReestablish ) -> Result < ( ) , MsgHandleErrInternal > {
2303
+ let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
2304
+ let channel_state = channel_state_lock. borrow_parts ( ) ;
2305
+
2306
+ match channel_state. by_id . get_mut ( & msg. channel_id ) {
2307
+ Some ( chan) => {
2308
+ if chan. get_their_node_id ( ) != * their_node_id {
2309
+ return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Got a message for a channel from the wrong node!" , msg. channel_id ) ) ;
2310
+ }
2311
+ let ( funding_locked, revoke_and_ack, commitment_update, channel_monitor, order) = chan. channel_reestablish ( msg)
2312
+ . map_err ( |e| MsgHandleErrInternal :: from_chan_maybe_close ( e, msg. channel_id ) ) ?;
2313
+ if let Some ( monitor) = channel_monitor {
2314
+ if let Err ( _e) = self . monitor . add_update_monitor ( monitor. get_funding_txo ( ) . unwrap ( ) , monitor) {
2315
+ unimplemented ! ( ) ;
2297
2316
}
2298
- let ( funding_locked, revoke_and_ack, commitment_update, channel_monitor, order) = chan. channel_reestablish ( msg)
2299
- . map_err ( |e| MsgHandleErrInternal :: from_chan_maybe_close ( e, msg. channel_id ) ) ?;
2300
- if let Some ( monitor) = channel_monitor {
2301
- if let Err ( _e) = self . monitor . add_update_monitor ( monitor. get_funding_txo ( ) . unwrap ( ) , monitor) {
2302
- unimplemented ! ( ) ;
2303
- }
2317
+ }
2318
+ if let Some ( msg) = funding_locked {
2319
+ channel_state. pending_msg_events . push ( events:: MessageSendEvent :: SendFundingLocked {
2320
+ node_id : their_node_id. clone ( ) ,
2321
+ msg
2322
+ } ) ;
2323
+ }
2324
+ macro_rules! send_raa { ( ) => {
2325
+ if let Some ( msg) = revoke_and_ack {
2326
+ channel_state. pending_msg_events. push( events:: MessageSendEvent :: SendRevokeAndACK {
2327
+ node_id: their_node_id. clone( ) ,
2328
+ msg
2329
+ } ) ;
2304
2330
}
2305
- Ok ( ( funding_locked, revoke_and_ack, commitment_update, order) )
2306
- } ,
2307
- None => return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Failed to find corresponding channel" , msg. channel_id ) )
2308
- }
2309
- } ;
2310
-
2311
- res
2331
+ } }
2332
+ macro_rules! send_cu { ( ) => {
2333
+ if let Some ( updates) = commitment_update {
2334
+ channel_state. pending_msg_events. push( events:: MessageSendEvent :: UpdateHTLCs {
2335
+ node_id: their_node_id. clone( ) ,
2336
+ updates
2337
+ } ) ;
2338
+ }
2339
+ } }
2340
+ match order {
2341
+ RAACommitmentOrder :: RevokeAndACKFirst => {
2342
+ send_raa ! ( ) ;
2343
+ send_cu ! ( ) ;
2344
+ } ,
2345
+ RAACommitmentOrder :: CommitmentFirst => {
2346
+ send_cu ! ( ) ;
2347
+ send_raa ! ( ) ;
2348
+ } ,
2349
+ }
2350
+ Ok ( ( ) )
2351
+ } ,
2352
+ None => Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Failed to find corresponding channel" , msg. channel_id ) )
2353
+ }
2312
2354
}
2313
2355
2314
2356
/// Begin Update fee process. Allowed only on an outbound channel.
@@ -2575,7 +2617,7 @@ impl ChannelMessageHandler for ChannelManager {
2575
2617
handle_error ! ( self , self . internal_announcement_signatures( their_node_id, msg) , their_node_id)
2576
2618
}
2577
2619
2578
- fn handle_channel_reestablish ( & self , their_node_id : & PublicKey , msg : & msgs:: ChannelReestablish ) -> Result < ( Option < msgs :: FundingLocked > , Option < msgs :: RevokeAndACK > , Option < msgs :: CommitmentUpdate > , RAACommitmentOrder ) , HandleError > {
2620
+ fn handle_channel_reestablish ( & self , their_node_id : & PublicKey , msg : & msgs:: ChannelReestablish ) -> Result < ( ) , HandleError > {
2579
2621
handle_error ! ( self , self . internal_channel_reestablish( their_node_id, msg) , their_node_id)
2580
2622
}
2581
2623
@@ -2675,7 +2717,7 @@ mod tests {
2675
2717
use chain:: chaininterface:: ChainListener ;
2676
2718
use chain:: keysinterface:: KeysInterface ;
2677
2719
use chain:: keysinterface;
2678
- use ln:: channelmanager:: { ChannelManager , OnionKeys , PaymentFailReason } ;
2720
+ use ln:: channelmanager:: { ChannelManager , OnionKeys , PaymentFailReason , RAACommitmentOrder } ;
2679
2721
use ln:: channelmonitor:: { ChannelMonitorUpdateErr , CLTV_CLAIM_BUFFER , HTLC_FAIL_TIMEOUT_BLOCKS } ;
2680
2722
use ln:: router:: { Route , RouteHop , Router } ;
2681
2723
use ln:: msgs;
@@ -5155,6 +5197,61 @@ mod tests {
5155
5197
assert_eq ! ( channel_state. short_to_id. len( ) , 0 ) ;
5156
5198
}
5157
5199
5200
+ macro_rules! handle_chan_reestablish_msgs {
5201
+ ( $src_node: expr, $dst_node: expr) => {
5202
+ {
5203
+ let msg_events = $src_node. node. get_and_clear_pending_msg_events( ) ;
5204
+ let mut idx = 0 ;
5205
+ let funding_locked = if let Some ( & MessageSendEvent :: SendFundingLocked { ref node_id, ref msg } ) = msg_events. get( 0 ) {
5206
+ idx += 1 ;
5207
+ assert_eq!( * node_id, $dst_node. node. get_our_node_id( ) ) ;
5208
+ Some ( msg. clone( ) )
5209
+ } else {
5210
+ None
5211
+ } ;
5212
+
5213
+ let mut revoke_and_ack = None ;
5214
+ let mut commitment_update = None ;
5215
+ let order = if let Some ( ev) = msg_events. get( idx) {
5216
+ idx += 1 ;
5217
+ match ev {
5218
+ & MessageSendEvent :: SendRevokeAndACK { ref node_id, ref msg } => {
5219
+ assert_eq!( * node_id, $dst_node. node. get_our_node_id( ) ) ;
5220
+ revoke_and_ack = Some ( msg. clone( ) ) ;
5221
+ RAACommitmentOrder :: RevokeAndACKFirst
5222
+ } ,
5223
+ & MessageSendEvent :: UpdateHTLCs { ref node_id, ref updates } => {
5224
+ assert_eq!( * node_id, $dst_node. node. get_our_node_id( ) ) ;
5225
+ commitment_update = Some ( updates. clone( ) ) ;
5226
+ RAACommitmentOrder :: CommitmentFirst
5227
+ } ,
5228
+ _ => panic!( "Unexpected event" ) ,
5229
+ }
5230
+ } else {
5231
+ RAACommitmentOrder :: CommitmentFirst
5232
+ } ;
5233
+
5234
+ if let Some ( ev) = msg_events. get( idx) {
5235
+ match ev {
5236
+ & MessageSendEvent :: SendRevokeAndACK { ref node_id, ref msg } => {
5237
+ assert_eq!( * node_id, $dst_node. node. get_our_node_id( ) ) ;
5238
+ assert!( revoke_and_ack. is_none( ) ) ;
5239
+ revoke_and_ack = Some ( msg. clone( ) ) ;
5240
+ } ,
5241
+ & MessageSendEvent :: UpdateHTLCs { ref node_id, ref updates } => {
5242
+ assert_eq!( * node_id, $dst_node. node. get_our_node_id( ) ) ;
5243
+ assert!( commitment_update. is_none( ) ) ;
5244
+ commitment_update = Some ( updates. clone( ) ) ;
5245
+ } ,
5246
+ _ => panic!( "Unexpected event" ) ,
5247
+ }
5248
+ }
5249
+
5250
+ ( funding_locked, revoke_and_ack, commitment_update, order)
5251
+ }
5252
+ }
5253
+ }
5254
+
5158
5255
/// pending_htlc_adds includes both the holding cell and in-flight update_add_htlcs, whereas
5159
5256
/// for claims/fails they are separated out.
5160
5257
fn reconnect_nodes ( node_a : & Node , node_b : & Node , pre_all_htlcs : bool , pending_htlc_adds : ( i64 , i64 ) , pending_htlc_claims : ( usize , usize ) , pending_cell_htlc_claims : ( usize , usize ) , pending_cell_htlc_fails : ( usize , usize ) , pending_raa : ( bool , bool ) ) {
@@ -5163,7 +5260,8 @@ mod tests {
5163
5260
5164
5261
let mut resp_1 = Vec :: new ( ) ;
5165
5262
for msg in reestablish_1 {
5166
- resp_1. push ( node_b. node . handle_channel_reestablish ( & node_a. node . get_our_node_id ( ) , & msg) . unwrap ( ) ) ;
5263
+ node_b. node . handle_channel_reestablish ( & node_a. node . get_our_node_id ( ) , & msg) . unwrap ( ) ;
5264
+ resp_1. push ( handle_chan_reestablish_msgs ! ( node_b, node_a) ) ;
5167
5265
}
5168
5266
if pending_cell_htlc_claims. 0 != 0 || pending_cell_htlc_fails. 0 != 0 {
5169
5267
check_added_monitors ! ( node_b, 1 ) ;
@@ -5173,7 +5271,8 @@ mod tests {
5173
5271
5174
5272
let mut resp_2 = Vec :: new ( ) ;
5175
5273
for msg in reestablish_2 {
5176
- resp_2. push ( node_a. node . handle_channel_reestablish ( & node_b. node . get_our_node_id ( ) , & msg) . unwrap ( ) ) ;
5274
+ node_a. node . handle_channel_reestablish ( & node_b. node . get_our_node_id ( ) , & msg) . unwrap ( ) ;
5275
+ resp_2. push ( handle_chan_reestablish_msgs ! ( node_a, node_b) ) ;
5177
5276
}
5178
5277
if pending_cell_htlc_claims. 1 != 0 || pending_cell_htlc_fails. 1 != 0 {
5179
5278
check_added_monitors ! ( node_a, 1 ) ;
@@ -5199,7 +5298,7 @@ mod tests {
5199
5298
assert ! ( chan_msgs. 0 . is_none( ) ) ;
5200
5299
}
5201
5300
if pending_raa. 0 {
5202
- assert ! ( chan_msgs. 3 == msgs :: RAACommitmentOrder :: RevokeAndACKFirst ) ;
5301
+ assert ! ( chan_msgs. 3 == RAACommitmentOrder :: RevokeAndACKFirst ) ;
5203
5302
node_a. node . handle_revoke_and_ack ( & node_b. node . get_our_node_id ( ) , & chan_msgs. 1 . unwrap ( ) ) . unwrap ( ) ;
5204
5303
assert ! ( node_a. node. get_and_clear_pending_msg_events( ) . is_empty( ) ) ;
5205
5304
check_added_monitors ! ( node_a, 1 ) ;
@@ -5256,7 +5355,7 @@ mod tests {
5256
5355
assert ! ( chan_msgs. 0 . is_none( ) ) ;
5257
5356
}
5258
5357
if pending_raa. 1 {
5259
- assert ! ( chan_msgs. 3 == msgs :: RAACommitmentOrder :: RevokeAndACKFirst ) ;
5358
+ assert ! ( chan_msgs. 3 == RAACommitmentOrder :: RevokeAndACKFirst ) ;
5260
5359
node_b. node . handle_revoke_and_ack ( & node_a. node . get_our_node_id ( ) , & chan_msgs. 1 . unwrap ( ) ) . unwrap ( ) ;
5261
5360
assert ! ( node_b. node. get_and_clear_pending_msg_events( ) . is_empty( ) ) ;
5262
5361
check_added_monitors ! ( node_b, 1 ) ;
@@ -5660,16 +5759,18 @@ mod tests {
5660
5759
let reestablish_2 = nodes[ 1 ] . node . peer_connected ( & nodes[ 0 ] . node . get_our_node_id ( ) ) ;
5661
5760
assert_eq ! ( reestablish_2. len( ) , 1 ) ;
5662
5761
5663
- let as_resp = nodes[ 0 ] . node . handle_channel_reestablish ( & nodes[ 1 ] . node . get_our_node_id ( ) , & reestablish_2[ 0 ] ) . unwrap ( ) ;
5664
- let bs_resp = nodes[ 1 ] . node . handle_channel_reestablish ( & nodes[ 0 ] . node . get_our_node_id ( ) , & reestablish_1[ 0 ] ) . unwrap ( ) ;
5762
+ nodes[ 0 ] . node . handle_channel_reestablish ( & nodes[ 1 ] . node . get_our_node_id ( ) , & reestablish_2[ 0 ] ) . unwrap ( ) ;
5763
+ let as_resp = handle_chan_reestablish_msgs ! ( nodes[ 0 ] , nodes[ 1 ] ) ;
5764
+ nodes[ 1 ] . node . handle_channel_reestablish ( & nodes[ 0 ] . node . get_our_node_id ( ) , & reestablish_1[ 0 ] ) . unwrap ( ) ;
5765
+ let bs_resp = handle_chan_reestablish_msgs ! ( nodes[ 1 ] , nodes[ 0 ] ) ;
5665
5766
5666
5767
assert ! ( as_resp. 0 . is_none( ) ) ;
5667
5768
assert ! ( bs_resp. 0 . is_none( ) ) ;
5668
5769
5669
5770
assert ! ( bs_resp. 1 . is_none( ) ) ;
5670
5771
assert ! ( bs_resp. 2 . is_none( ) ) ;
5671
5772
5672
- assert ! ( as_resp. 3 == msgs :: RAACommitmentOrder :: CommitmentFirst ) ;
5773
+ assert ! ( as_resp. 3 == RAACommitmentOrder :: CommitmentFirst ) ;
5673
5774
5674
5775
assert_eq ! ( as_resp. 2 . as_ref( ) . unwrap( ) . update_add_htlcs. len( ) , 1 ) ;
5675
5776
assert ! ( as_resp. 2 . as_ref( ) . unwrap( ) . update_fulfill_htlcs. is_empty( ) ) ;
@@ -5946,8 +6047,10 @@ mod tests {
5946
6047
let reestablish_2 = nodes[ 1 ] . node. peer_connected( & nodes[ 0 ] . node. get_our_node_id( ) ) ;
5947
6048
assert_eq!( reestablish_2. len( ) , 1 ) ;
5948
6049
5949
- let as_resp = nodes[ 0 ] . node. handle_channel_reestablish( & nodes[ 1 ] . node. get_our_node_id( ) , & reestablish_2[ 0 ] ) . unwrap( ) ;
5950
- let bs_resp = nodes[ 1 ] . node. handle_channel_reestablish( & nodes[ 0 ] . node. get_our_node_id( ) , & reestablish_1[ 0 ] ) . unwrap( ) ;
6050
+ nodes[ 0 ] . node. handle_channel_reestablish( & nodes[ 1 ] . node. get_our_node_id( ) , & reestablish_2[ 0 ] ) . unwrap( ) ;
6051
+ let as_resp = handle_chan_reestablish_msgs!( nodes[ 0 ] , nodes[ 1 ] ) ;
6052
+ nodes[ 1 ] . node. handle_channel_reestablish( & nodes[ 0 ] . node. get_our_node_id( ) , & reestablish_1[ 0 ] ) . unwrap( ) ;
6053
+ let bs_resp = handle_chan_reestablish_msgs!( nodes[ 1 ] , nodes[ 0 ] ) ;
5951
6054
5952
6055
assert!( as_resp. 0 . is_none( ) ) ;
5953
6056
assert!( bs_resp. 0 . is_none( ) ) ;
@@ -5964,10 +6067,12 @@ mod tests {
5964
6067
let reestablish_2 = nodes[ 1 ] . node . peer_connected ( & nodes[ 0 ] . node . get_our_node_id ( ) ) ;
5965
6068
assert_eq ! ( reestablish_2. len( ) , 1 ) ;
5966
6069
5967
- let mut as_resp = nodes[ 0 ] . node . handle_channel_reestablish ( & nodes[ 1 ] . node . get_our_node_id ( ) , & reestablish_2[ 0 ] ) . unwrap ( ) ;
6070
+ nodes[ 0 ] . node . handle_channel_reestablish ( & nodes[ 1 ] . node . get_our_node_id ( ) , & reestablish_2[ 0 ] ) . unwrap ( ) ;
5968
6071
check_added_monitors ! ( nodes[ 0 ] , 0 ) ;
5969
- let mut bs_resp = nodes[ 1 ] . node . handle_channel_reestablish ( & nodes[ 0 ] . node . get_our_node_id ( ) , & reestablish_1[ 0 ] ) . unwrap ( ) ;
6072
+ let mut as_resp = handle_chan_reestablish_msgs ! ( nodes[ 0 ] , nodes[ 1 ] ) ;
6073
+ nodes[ 1 ] . node . handle_channel_reestablish ( & nodes[ 0 ] . node . get_our_node_id ( ) , & reestablish_1[ 0 ] ) . unwrap ( ) ;
5970
6074
check_added_monitors ! ( nodes[ 1 ] , 0 ) ;
6075
+ let mut bs_resp = handle_chan_reestablish_msgs ! ( nodes[ 1 ] , nodes[ 0 ] ) ;
5971
6076
5972
6077
assert ! ( as_resp. 0 . is_none( ) ) ;
5973
6078
assert ! ( bs_resp. 0 . is_none( ) ) ;
@@ -5978,7 +6083,7 @@ mod tests {
5978
6083
5979
6084
assert ! ( as_resp. 1 . is_some( ) ) ;
5980
6085
assert ! ( as_resp. 2 . is_some( ) ) ;
5981
- assert ! ( as_resp. 3 == msgs :: RAACommitmentOrder :: CommitmentFirst ) ;
6086
+ assert ! ( as_resp. 3 == RAACommitmentOrder :: CommitmentFirst ) ;
5982
6087
} else {
5983
6088
assert ! ( bs_resp. 2 . as_ref( ) . unwrap( ) . update_add_htlcs. is_empty( ) ) ;
5984
6089
assert ! ( bs_resp. 2 . as_ref( ) . unwrap( ) . update_fail_htlcs. is_empty( ) ) ;
@@ -6087,7 +6192,7 @@ mod tests {
6087
6192
assert ! ( as_resp. 2 . unwrap( ) == as_commitment_update) ;
6088
6193
assert ! ( bs_resp. 2 . is_none( ) ) ;
6089
6194
6090
- assert ! ( as_resp. 3 == msgs :: RAACommitmentOrder :: RevokeAndACKFirst ) ;
6195
+ assert ! ( as_resp. 3 == RAACommitmentOrder :: RevokeAndACKFirst ) ;
6091
6196
}
6092
6197
6093
6198
handle_initial_raa ! ( ) ;
@@ -6113,7 +6218,7 @@ mod tests {
6113
6218
assert ! ( as_resp. 2 . is_none( ) ) ;
6114
6219
assert ! ( bs_resp. 2 . unwrap( ) == bs_second_commitment_update) ;
6115
6220
6116
- assert ! ( bs_resp. 3 == msgs :: RAACommitmentOrder :: RevokeAndACKFirst ) ;
6221
+ assert ! ( bs_resp. 3 == RAACommitmentOrder :: RevokeAndACKFirst ) ;
6117
6222
}
6118
6223
6119
6224
handle_bs_raa ! ( ) ;
0 commit comments