@@ -26,7 +26,7 @@ use ln::channel::{Channel, ChannelError, ChannelKeys};
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 util:: { byte_utils, events, internal_traits, rng} ;
31
31
use util:: sha2:: Sha256 ;
32
32
use util:: ser:: { Readable , Writeable } ;
@@ -233,6 +233,18 @@ struct HTLCForwardInfo {
233
233
forward_info : PendingForwardHTLCInfo ,
234
234
}
235
235
236
+ /// For events which result in both a RevokeAndACK and a CommitmentUpdate, by default they should
237
+ /// be sent in the order they appear in the return value, however sometimes the order needs to be
238
+ /// variable at runtime (eg Channel::channel_reestablish needs to re-send messages in the order
239
+ /// they were originally sent). In those cases, this enum is also returned.
240
+ #[ derive( Clone , PartialEq ) ]
241
+ pub ( super ) enum RAACommitmentOrder {
242
+ /// Send the CommitmentUpdate messages first
243
+ CommitmentFirst ,
244
+ /// Send the RevokeAndACK message first
245
+ RevokeAndACKFirst ,
246
+ }
247
+
236
248
struct ChannelHolder {
237
249
by_id : HashMap < [ u8 ; 32 ] , Channel > ,
238
250
short_to_id : HashMap < u64 , [ u8 ; 32 ] > ,
@@ -2314,28 +2326,58 @@ impl ChannelManager {
2314
2326
Ok ( ( ) )
2315
2327
}
2316
2328
2317
- fn internal_channel_reestablish ( & self , their_node_id : & PublicKey , msg : & msgs:: ChannelReestablish ) -> Result < ( Option < msgs:: FundingLocked > , Option < msgs:: RevokeAndACK > , Option < msgs:: CommitmentUpdate > , RAACommitmentOrder ) , MsgHandleErrInternal > {
2318
- let res = {
2319
- let mut channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
2320
- match channel_state. by_id . get_mut ( & msg. channel_id ) {
2321
- Some ( chan) => {
2322
- if chan. get_their_node_id ( ) != * their_node_id {
2323
- return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Got a message for a channel from the wrong node!" , msg. channel_id ) ) ;
2329
+ fn internal_channel_reestablish ( & self , their_node_id : & PublicKey , msg : & msgs:: ChannelReestablish ) -> Result < ( ) , MsgHandleErrInternal > {
2330
+ let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
2331
+ let channel_state = channel_state_lock. borrow_parts ( ) ;
2332
+
2333
+ match channel_state. by_id . get_mut ( & msg. channel_id ) {
2334
+ Some ( chan) => {
2335
+ if chan. get_their_node_id ( ) != * their_node_id {
2336
+ return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Got a message for a channel from the wrong node!" , msg. channel_id ) ) ;
2337
+ }
2338
+ let ( funding_locked, revoke_and_ack, commitment_update, channel_monitor, order) = chan. channel_reestablish ( msg)
2339
+ . map_err ( |e| MsgHandleErrInternal :: from_chan_maybe_close ( e, msg. channel_id ) ) ?;
2340
+ if let Some ( monitor) = channel_monitor {
2341
+ if let Err ( _e) = self . monitor . add_update_monitor ( monitor. get_funding_txo ( ) . unwrap ( ) , monitor) {
2342
+ unimplemented ! ( ) ;
2324
2343
}
2325
- let ( funding_locked, revoke_and_ack, commitment_update, channel_monitor, order) = chan. channel_reestablish ( msg)
2326
- . map_err ( |e| MsgHandleErrInternal :: from_chan_maybe_close ( e, msg. channel_id ) ) ?;
2327
- if let Some ( monitor) = channel_monitor {
2328
- if let Err ( _e) = self . monitor . add_update_monitor ( monitor. get_funding_txo ( ) . unwrap ( ) , monitor) {
2329
- unimplemented ! ( ) ;
2330
- }
2344
+ }
2345
+ if let Some ( msg) = funding_locked {
2346
+ channel_state. pending_msg_events . push ( events:: MessageSendEvent :: SendFundingLocked {
2347
+ node_id : their_node_id. clone ( ) ,
2348
+ msg
2349
+ } ) ;
2350
+ }
2351
+ macro_rules! send_raa { ( ) => {
2352
+ if let Some ( msg) = revoke_and_ack {
2353
+ channel_state. pending_msg_events. push( events:: MessageSendEvent :: SendRevokeAndACK {
2354
+ node_id: their_node_id. clone( ) ,
2355
+ msg
2356
+ } ) ;
2331
2357
}
2332
- Ok ( ( funding_locked, revoke_and_ack, commitment_update, order) )
2333
- } ,
2334
- None => return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Failed to find corresponding channel" , msg. channel_id ) )
2335
- }
2336
- } ;
2337
-
2338
- res
2358
+ } }
2359
+ macro_rules! send_cu { ( ) => {
2360
+ if let Some ( updates) = commitment_update {
2361
+ channel_state. pending_msg_events. push( events:: MessageSendEvent :: UpdateHTLCs {
2362
+ node_id: their_node_id. clone( ) ,
2363
+ updates
2364
+ } ) ;
2365
+ }
2366
+ } }
2367
+ match order {
2368
+ RAACommitmentOrder :: RevokeAndACKFirst => {
2369
+ send_raa ! ( ) ;
2370
+ send_cu ! ( ) ;
2371
+ } ,
2372
+ RAACommitmentOrder :: CommitmentFirst => {
2373
+ send_cu ! ( ) ;
2374
+ send_raa ! ( ) ;
2375
+ } ,
2376
+ }
2377
+ Ok ( ( ) )
2378
+ } ,
2379
+ None => Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Failed to find corresponding channel" , msg. channel_id ) )
2380
+ }
2339
2381
}
2340
2382
2341
2383
/// Begin Update fee process. Allowed only on an outbound channel.
@@ -2602,7 +2644,7 @@ impl ChannelMessageHandler for ChannelManager {
2602
2644
handle_error ! ( self , self . internal_announcement_signatures( their_node_id, msg) , their_node_id)
2603
2645
}
2604
2646
2605
- fn handle_channel_reestablish ( & self , their_node_id : & PublicKey , msg : & msgs:: ChannelReestablish ) -> Result < ( Option < msgs :: FundingLocked > , Option < msgs :: RevokeAndACK > , Option < msgs :: CommitmentUpdate > , RAACommitmentOrder ) , HandleError > {
2647
+ fn handle_channel_reestablish ( & self , their_node_id : & PublicKey , msg : & msgs:: ChannelReestablish ) -> Result < ( ) , HandleError > {
2606
2648
handle_error ! ( self , self . internal_channel_reestablish( their_node_id, msg) , their_node_id)
2607
2649
}
2608
2650
@@ -2700,7 +2742,7 @@ mod tests {
2700
2742
use chain:: chaininterface;
2701
2743
use chain:: transaction:: OutPoint ;
2702
2744
use chain:: chaininterface:: ChainListener ;
2703
- use ln:: channelmanager:: { ChannelManager , OnionKeys } ;
2745
+ use ln:: channelmanager:: { ChannelManager , OnionKeys , RAACommitmentOrder } ;
2704
2746
use ln:: channelmonitor:: { ChannelMonitorUpdateErr , CLTV_CLAIM_BUFFER , HTLC_FAIL_TIMEOUT_BLOCKS } ;
2705
2747
use ln:: router:: { Route , RouteHop , Router } ;
2706
2748
use ln:: msgs;
@@ -5182,6 +5224,61 @@ mod tests {
5182
5224
assert_eq ! ( channel_state. short_to_id. len( ) , 0 ) ;
5183
5225
}
5184
5226
5227
+ macro_rules! handle_chan_reestablish_msgs {
5228
+ ( $src_node: expr, $dst_node: expr) => {
5229
+ {
5230
+ let msg_events = $src_node. node. get_and_clear_pending_msg_events( ) ;
5231
+ let mut idx = 0 ;
5232
+ let funding_locked = if let Some ( & MessageSendEvent :: SendFundingLocked { ref node_id, ref msg } ) = msg_events. get( 0 ) {
5233
+ idx += 1 ;
5234
+ assert_eq!( * node_id, $dst_node. node. get_our_node_id( ) ) ;
5235
+ Some ( msg. clone( ) )
5236
+ } else {
5237
+ None
5238
+ } ;
5239
+
5240
+ let mut revoke_and_ack = None ;
5241
+ let mut commitment_update = None ;
5242
+ let order = if let Some ( ev) = msg_events. get( idx) {
5243
+ idx += 1 ;
5244
+ match ev {
5245
+ & MessageSendEvent :: SendRevokeAndACK { ref node_id, ref msg } => {
5246
+ assert_eq!( * node_id, $dst_node. node. get_our_node_id( ) ) ;
5247
+ revoke_and_ack = Some ( msg. clone( ) ) ;
5248
+ RAACommitmentOrder :: RevokeAndACKFirst
5249
+ } ,
5250
+ & MessageSendEvent :: UpdateHTLCs { ref node_id, ref updates } => {
5251
+ assert_eq!( * node_id, $dst_node. node. get_our_node_id( ) ) ;
5252
+ commitment_update = Some ( updates. clone( ) ) ;
5253
+ RAACommitmentOrder :: CommitmentFirst
5254
+ } ,
5255
+ _ => panic!( "Unexpected event" ) ,
5256
+ }
5257
+ } else {
5258
+ RAACommitmentOrder :: CommitmentFirst
5259
+ } ;
5260
+
5261
+ if let Some ( ev) = msg_events. get( idx) {
5262
+ match ev {
5263
+ & MessageSendEvent :: SendRevokeAndACK { ref node_id, ref msg } => {
5264
+ assert_eq!( * node_id, $dst_node. node. get_our_node_id( ) ) ;
5265
+ assert!( revoke_and_ack. is_none( ) ) ;
5266
+ revoke_and_ack = Some ( msg. clone( ) ) ;
5267
+ } ,
5268
+ & MessageSendEvent :: UpdateHTLCs { ref node_id, ref updates } => {
5269
+ assert_eq!( * node_id, $dst_node. node. get_our_node_id( ) ) ;
5270
+ assert!( commitment_update. is_none( ) ) ;
5271
+ commitment_update = Some ( updates. clone( ) ) ;
5272
+ } ,
5273
+ _ => panic!( "Unexpected event" ) ,
5274
+ }
5275
+ }
5276
+
5277
+ ( funding_locked, revoke_and_ack, commitment_update, order)
5278
+ }
5279
+ }
5280
+ }
5281
+
5185
5282
/// pending_htlc_adds includes both the holding cell and in-flight update_add_htlcs, whereas
5186
5283
/// for claims/fails they are separated out.
5187
5284
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 ) ) {
@@ -5190,7 +5287,8 @@ mod tests {
5190
5287
5191
5288
let mut resp_1 = Vec :: new ( ) ;
5192
5289
for msg in reestablish_1 {
5193
- resp_1. push ( node_b. node . handle_channel_reestablish ( & node_a. node . get_our_node_id ( ) , & msg) . unwrap ( ) ) ;
5290
+ node_b. node . handle_channel_reestablish ( & node_a. node . get_our_node_id ( ) , & msg) . unwrap ( ) ;
5291
+ resp_1. push ( handle_chan_reestablish_msgs ! ( node_b, node_a) ) ;
5194
5292
}
5195
5293
if pending_cell_htlc_claims. 0 != 0 || pending_cell_htlc_fails. 0 != 0 {
5196
5294
check_added_monitors ! ( node_b, 1 ) ;
@@ -5200,7 +5298,8 @@ mod tests {
5200
5298
5201
5299
let mut resp_2 = Vec :: new ( ) ;
5202
5300
for msg in reestablish_2 {
5203
- resp_2. push ( node_a. node . handle_channel_reestablish ( & node_b. node . get_our_node_id ( ) , & msg) . unwrap ( ) ) ;
5301
+ node_a. node . handle_channel_reestablish ( & node_b. node . get_our_node_id ( ) , & msg) . unwrap ( ) ;
5302
+ resp_2. push ( handle_chan_reestablish_msgs ! ( node_a, node_b) ) ;
5204
5303
}
5205
5304
if pending_cell_htlc_claims. 1 != 0 || pending_cell_htlc_fails. 1 != 0 {
5206
5305
check_added_monitors ! ( node_a, 1 ) ;
@@ -5226,7 +5325,7 @@ mod tests {
5226
5325
assert ! ( chan_msgs. 0 . is_none( ) ) ;
5227
5326
}
5228
5327
if pending_raa. 0 {
5229
- assert ! ( chan_msgs. 3 == msgs :: RAACommitmentOrder :: RevokeAndACKFirst ) ;
5328
+ assert ! ( chan_msgs. 3 == RAACommitmentOrder :: RevokeAndACKFirst ) ;
5230
5329
node_a. node . handle_revoke_and_ack ( & node_b. node . get_our_node_id ( ) , & chan_msgs. 1 . unwrap ( ) ) . unwrap ( ) ;
5231
5330
assert ! ( node_a. node. get_and_clear_pending_msg_events( ) . is_empty( ) ) ;
5232
5331
check_added_monitors ! ( node_a, 1 ) ;
@@ -5283,7 +5382,7 @@ mod tests {
5283
5382
assert ! ( chan_msgs. 0 . is_none( ) ) ;
5284
5383
}
5285
5384
if pending_raa. 1 {
5286
- assert ! ( chan_msgs. 3 == msgs :: RAACommitmentOrder :: RevokeAndACKFirst ) ;
5385
+ assert ! ( chan_msgs. 3 == RAACommitmentOrder :: RevokeAndACKFirst ) ;
5287
5386
node_b. node . handle_revoke_and_ack ( & node_a. node . get_our_node_id ( ) , & chan_msgs. 1 . unwrap ( ) ) . unwrap ( ) ;
5288
5387
assert ! ( node_b. node. get_and_clear_pending_msg_events( ) . is_empty( ) ) ;
5289
5388
check_added_monitors ! ( node_b, 1 ) ;
@@ -5687,16 +5786,18 @@ mod tests {
5687
5786
let reestablish_2 = nodes[ 1 ] . node . peer_connected ( & nodes[ 0 ] . node . get_our_node_id ( ) ) ;
5688
5787
assert_eq ! ( reestablish_2. len( ) , 1 ) ;
5689
5788
5690
- let as_resp = nodes[ 0 ] . node . handle_channel_reestablish ( & nodes[ 1 ] . node . get_our_node_id ( ) , & reestablish_2[ 0 ] ) . unwrap ( ) ;
5691
- let bs_resp = nodes[ 1 ] . node . handle_channel_reestablish ( & nodes[ 0 ] . node . get_our_node_id ( ) , & reestablish_1[ 0 ] ) . unwrap ( ) ;
5789
+ nodes[ 0 ] . node . handle_channel_reestablish ( & nodes[ 1 ] . node . get_our_node_id ( ) , & reestablish_2[ 0 ] ) . unwrap ( ) ;
5790
+ let as_resp = handle_chan_reestablish_msgs ! ( nodes[ 0 ] , nodes[ 1 ] ) ;
5791
+ nodes[ 1 ] . node . handle_channel_reestablish ( & nodes[ 0 ] . node . get_our_node_id ( ) , & reestablish_1[ 0 ] ) . unwrap ( ) ;
5792
+ let bs_resp = handle_chan_reestablish_msgs ! ( nodes[ 1 ] , nodes[ 0 ] ) ;
5692
5793
5693
5794
assert ! ( as_resp. 0 . is_none( ) ) ;
5694
5795
assert ! ( bs_resp. 0 . is_none( ) ) ;
5695
5796
5696
5797
assert ! ( bs_resp. 1 . is_none( ) ) ;
5697
5798
assert ! ( bs_resp. 2 . is_none( ) ) ;
5698
5799
5699
- assert ! ( as_resp. 3 == msgs :: RAACommitmentOrder :: CommitmentFirst ) ;
5800
+ assert ! ( as_resp. 3 == RAACommitmentOrder :: CommitmentFirst ) ;
5700
5801
5701
5802
assert_eq ! ( as_resp. 2 . as_ref( ) . unwrap( ) . update_add_htlcs. len( ) , 1 ) ;
5702
5803
assert ! ( as_resp. 2 . as_ref( ) . unwrap( ) . update_fulfill_htlcs. is_empty( ) ) ;
@@ -5973,8 +6074,10 @@ mod tests {
5973
6074
let reestablish_2 = nodes[ 1 ] . node. peer_connected( & nodes[ 0 ] . node. get_our_node_id( ) ) ;
5974
6075
assert_eq!( reestablish_2. len( ) , 1 ) ;
5975
6076
5976
- let as_resp = nodes[ 0 ] . node. handle_channel_reestablish( & nodes[ 1 ] . node. get_our_node_id( ) , & reestablish_2[ 0 ] ) . unwrap( ) ;
5977
- let bs_resp = nodes[ 1 ] . node. handle_channel_reestablish( & nodes[ 0 ] . node. get_our_node_id( ) , & reestablish_1[ 0 ] ) . unwrap( ) ;
6077
+ nodes[ 0 ] . node. handle_channel_reestablish( & nodes[ 1 ] . node. get_our_node_id( ) , & reestablish_2[ 0 ] ) . unwrap( ) ;
6078
+ let as_resp = handle_chan_reestablish_msgs!( nodes[ 0 ] , nodes[ 1 ] ) ;
6079
+ nodes[ 1 ] . node. handle_channel_reestablish( & nodes[ 0 ] . node. get_our_node_id( ) , & reestablish_1[ 0 ] ) . unwrap( ) ;
6080
+ let bs_resp = handle_chan_reestablish_msgs!( nodes[ 1 ] , nodes[ 0 ] ) ;
5978
6081
5979
6082
assert!( as_resp. 0 . is_none( ) ) ;
5980
6083
assert!( bs_resp. 0 . is_none( ) ) ;
@@ -5991,10 +6094,12 @@ mod tests {
5991
6094
let reestablish_2 = nodes[ 1 ] . node . peer_connected ( & nodes[ 0 ] . node . get_our_node_id ( ) ) ;
5992
6095
assert_eq ! ( reestablish_2. len( ) , 1 ) ;
5993
6096
5994
- let mut as_resp = nodes[ 0 ] . node . handle_channel_reestablish ( & nodes[ 1 ] . node . get_our_node_id ( ) , & reestablish_2[ 0 ] ) . unwrap ( ) ;
6097
+ nodes[ 0 ] . node . handle_channel_reestablish ( & nodes[ 1 ] . node . get_our_node_id ( ) , & reestablish_2[ 0 ] ) . unwrap ( ) ;
5995
6098
check_added_monitors ! ( nodes[ 0 ] , 0 ) ;
5996
- let mut bs_resp = nodes[ 1 ] . node . handle_channel_reestablish ( & nodes[ 0 ] . node . get_our_node_id ( ) , & reestablish_1[ 0 ] ) . unwrap ( ) ;
6099
+ let mut as_resp = handle_chan_reestablish_msgs ! ( nodes[ 0 ] , nodes[ 1 ] ) ;
6100
+ nodes[ 1 ] . node . handle_channel_reestablish ( & nodes[ 0 ] . node . get_our_node_id ( ) , & reestablish_1[ 0 ] ) . unwrap ( ) ;
5997
6101
check_added_monitors ! ( nodes[ 1 ] , 0 ) ;
6102
+ let mut bs_resp = handle_chan_reestablish_msgs ! ( nodes[ 1 ] , nodes[ 0 ] ) ;
5998
6103
5999
6104
assert ! ( as_resp. 0 . is_none( ) ) ;
6000
6105
assert ! ( bs_resp. 0 . is_none( ) ) ;
@@ -6005,7 +6110,7 @@ mod tests {
6005
6110
6006
6111
assert ! ( as_resp. 1 . is_some( ) ) ;
6007
6112
assert ! ( as_resp. 2 . is_some( ) ) ;
6008
- assert ! ( as_resp. 3 == msgs :: RAACommitmentOrder :: CommitmentFirst ) ;
6113
+ assert ! ( as_resp. 3 == RAACommitmentOrder :: CommitmentFirst ) ;
6009
6114
} else {
6010
6115
assert ! ( bs_resp. 2 . as_ref( ) . unwrap( ) . update_add_htlcs. is_empty( ) ) ;
6011
6116
assert ! ( bs_resp. 2 . as_ref( ) . unwrap( ) . update_fail_htlcs. is_empty( ) ) ;
@@ -6114,7 +6219,7 @@ mod tests {
6114
6219
assert ! ( as_resp. 2 . unwrap( ) == as_commitment_update) ;
6115
6220
assert ! ( bs_resp. 2 . is_none( ) ) ;
6116
6221
6117
- assert ! ( as_resp. 3 == msgs :: RAACommitmentOrder :: RevokeAndACKFirst ) ;
6222
+ assert ! ( as_resp. 3 == RAACommitmentOrder :: RevokeAndACKFirst ) ;
6118
6223
}
6119
6224
6120
6225
handle_initial_raa ! ( ) ;
@@ -6140,7 +6245,7 @@ mod tests {
6140
6245
assert ! ( as_resp. 2 . is_none( ) ) ;
6141
6246
assert ! ( bs_resp. 2 . unwrap( ) == bs_second_commitment_update) ;
6142
6247
6143
- assert ! ( bs_resp. 3 == msgs :: RAACommitmentOrder :: RevokeAndACKFirst ) ;
6248
+ assert ! ( bs_resp. 3 == RAACommitmentOrder :: RevokeAndACKFirst ) ;
6144
6249
}
6145
6250
6146
6251
handle_bs_raa ! ( ) ;
0 commit comments