@@ -26,7 +26,7 @@ use ln::channel::{Channel, ChannelError, ChannelKeys};
26
26
use ln:: channelmonitor:: { ChannelMonitorUpdateErr , ManyChannelMonitor } ;
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 } ;
@@ -229,6 +229,18 @@ struct HTLCForwardInfo {
229
229
forward_info : PendingForwardHTLCInfo ,
230
230
}
231
231
232
+ /// For events which result in both a RevokeAndACK and a CommitmentUpdate, by default they should
233
+ /// be sent in the order they appear in the return value, however sometimes the order needs to be
234
+ /// variable at runtime (eg Channel::channel_reestablish needs to re-send messages in the order
235
+ /// they were originally sent). In those cases, this enum is also returned.
236
+ #[ derive( Clone , PartialEq ) ]
237
+ pub ( super ) enum RAACommitmentOrder {
238
+ /// Send the CommitmentUpdate messages first
239
+ CommitmentFirst ,
240
+ /// Send the RevokeAndACK message first
241
+ RevokeAndACKFirst ,
242
+ }
243
+
232
244
struct ChannelHolder {
233
245
by_id : HashMap < [ u8 ; 32 ] , Channel > ,
234
246
short_to_id : HashMap < u64 , [ u8 ; 32 ] > ,
@@ -2112,28 +2124,58 @@ impl ChannelManager {
2112
2124
Ok ( ( ) )
2113
2125
}
2114
2126
2115
- fn internal_channel_reestablish ( & self , their_node_id : & PublicKey , msg : & msgs:: ChannelReestablish ) -> Result < ( Option < msgs:: FundingLocked > , Option < msgs:: RevokeAndACK > , Option < msgs:: CommitmentUpdate > , RAACommitmentOrder ) , MsgHandleErrInternal > {
2116
- let res = {
2117
- let mut channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
2118
- match channel_state. by_id . get_mut ( & msg. channel_id ) {
2119
- Some ( chan) => {
2120
- if chan. get_their_node_id ( ) != * their_node_id {
2121
- return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Got a message for a channel from the wrong node!" , msg. channel_id ) ) ;
2127
+ fn internal_channel_reestablish ( & self , their_node_id : & PublicKey , msg : & msgs:: ChannelReestablish ) -> Result < ( ) , MsgHandleErrInternal > {
2128
+ let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
2129
+ let channel_state = channel_state_lock. borrow_parts ( ) ;
2130
+
2131
+ match channel_state. by_id . get_mut ( & msg. channel_id ) {
2132
+ Some ( chan) => {
2133
+ if chan. get_their_node_id ( ) != * their_node_id {
2134
+ return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Got a message for a channel from the wrong node!" , msg. channel_id ) ) ;
2135
+ }
2136
+ let ( funding_locked, revoke_and_ack, commitment_update, channel_monitor, order) = chan. channel_reestablish ( msg)
2137
+ . map_err ( |e| MsgHandleErrInternal :: from_chan_maybe_close ( e, msg. channel_id ) ) ?;
2138
+ if let Some ( monitor) = channel_monitor {
2139
+ if let Err ( _e) = self . monitor . add_update_monitor ( monitor. get_funding_txo ( ) . unwrap ( ) , monitor) {
2140
+ unimplemented ! ( ) ;
2122
2141
}
2123
- let ( funding_locked, revoke_and_ack, commitment_update, channel_monitor, order) = chan. channel_reestablish ( msg)
2124
- . map_err ( |e| MsgHandleErrInternal :: from_chan_maybe_close ( e, msg. channel_id ) ) ?;
2125
- if let Some ( monitor) = channel_monitor {
2126
- if let Err ( _e) = self . monitor . add_update_monitor ( monitor. get_funding_txo ( ) . unwrap ( ) , monitor) {
2127
- unimplemented ! ( ) ;
2128
- }
2142
+ }
2143
+ if let Some ( msg) = funding_locked {
2144
+ channel_state. pending_msg_events . push ( events:: MessageSendEvent :: SendFundingLocked {
2145
+ node_id : their_node_id. clone ( ) ,
2146
+ msg
2147
+ } ) ;
2148
+ }
2149
+ macro_rules! send_raa { ( ) => {
2150
+ if let Some ( msg) = revoke_and_ack {
2151
+ channel_state. pending_msg_events. push( events:: MessageSendEvent :: SendRevokeAndACK {
2152
+ node_id: their_node_id. clone( ) ,
2153
+ msg
2154
+ } ) ;
2129
2155
}
2130
- Ok ( ( funding_locked, revoke_and_ack, commitment_update, order) )
2131
- } ,
2132
- None => return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Failed to find corresponding channel" , msg. channel_id ) )
2133
- }
2134
- } ;
2135
-
2136
- res
2156
+ } }
2157
+ macro_rules! send_cu { ( ) => {
2158
+ if let Some ( updates) = commitment_update {
2159
+ channel_state. pending_msg_events. push( events:: MessageSendEvent :: UpdateHTLCs {
2160
+ node_id: their_node_id. clone( ) ,
2161
+ updates
2162
+ } ) ;
2163
+ }
2164
+ } }
2165
+ match order {
2166
+ RAACommitmentOrder :: RevokeAndACKFirst => {
2167
+ send_raa ! ( ) ;
2168
+ send_cu ! ( ) ;
2169
+ } ,
2170
+ RAACommitmentOrder :: CommitmentFirst => {
2171
+ send_cu ! ( ) ;
2172
+ send_raa ! ( ) ;
2173
+ } ,
2174
+ }
2175
+ Ok ( ( ) )
2176
+ } ,
2177
+ None => Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Failed to find corresponding channel" , msg. channel_id ) )
2178
+ }
2137
2179
}
2138
2180
2139
2181
/// Begin Update fee process. Allowed only on an outbound channel.
@@ -2400,7 +2442,7 @@ impl ChannelMessageHandler for ChannelManager {
2400
2442
handle_error ! ( self , self . internal_announcement_signatures( their_node_id, msg) , their_node_id)
2401
2443
}
2402
2444
2403
- fn handle_channel_reestablish ( & self , their_node_id : & PublicKey , msg : & msgs:: ChannelReestablish ) -> Result < ( Option < msgs :: FundingLocked > , Option < msgs :: RevokeAndACK > , Option < msgs :: CommitmentUpdate > , RAACommitmentOrder ) , HandleError > {
2445
+ fn handle_channel_reestablish ( & self , their_node_id : & PublicKey , msg : & msgs:: ChannelReestablish ) -> Result < ( ) , HandleError > {
2404
2446
handle_error ! ( self , self . internal_channel_reestablish( their_node_id, msg) , their_node_id)
2405
2447
}
2406
2448
@@ -2498,7 +2540,7 @@ mod tests {
2498
2540
use chain:: chaininterface;
2499
2541
use chain:: transaction:: OutPoint ;
2500
2542
use chain:: chaininterface:: ChainListener ;
2501
- use ln:: channelmanager:: { ChannelManager , OnionKeys } ;
2543
+ use ln:: channelmanager:: { ChannelManager , OnionKeys , RAACommitmentOrder } ;
2502
2544
use ln:: channelmonitor:: ChannelMonitorUpdateErr ;
2503
2545
use ln:: router:: { Route , RouteHop , Router } ;
2504
2546
use ln:: msgs;
@@ -4969,6 +5011,61 @@ mod tests {
4969
5011
assert_eq ! ( channel_state. short_to_id. len( ) , 0 ) ;
4970
5012
}
4971
5013
5014
+ macro_rules! handle_chan_reestablish_msgs {
5015
+ ( $src_node: expr, $dst_node: expr) => {
5016
+ {
5017
+ let msg_events = $src_node. node. get_and_clear_pending_msg_events( ) ;
5018
+ let mut idx = 0 ;
5019
+ let funding_locked = if let Some ( & MessageSendEvent :: SendFundingLocked { ref node_id, ref msg } ) = msg_events. get( 0 ) {
5020
+ idx += 1 ;
5021
+ assert_eq!( * node_id, $dst_node. node. get_our_node_id( ) ) ;
5022
+ Some ( msg. clone( ) )
5023
+ } else {
5024
+ None
5025
+ } ;
5026
+
5027
+ let mut revoke_and_ack = None ;
5028
+ let mut commitment_update = None ;
5029
+ let order = if let Some ( ev) = msg_events. get( idx) {
5030
+ idx += 1 ;
5031
+ match ev {
5032
+ & MessageSendEvent :: SendRevokeAndACK { ref node_id, ref msg } => {
5033
+ assert_eq!( * node_id, $dst_node. node. get_our_node_id( ) ) ;
5034
+ revoke_and_ack = Some ( msg. clone( ) ) ;
5035
+ RAACommitmentOrder :: RevokeAndACKFirst
5036
+ } ,
5037
+ & MessageSendEvent :: UpdateHTLCs { ref node_id, ref updates } => {
5038
+ assert_eq!( * node_id, $dst_node. node. get_our_node_id( ) ) ;
5039
+ commitment_update = Some ( updates. clone( ) ) ;
5040
+ RAACommitmentOrder :: CommitmentFirst
5041
+ } ,
5042
+ _ => panic!( "Unexpected event" ) ,
5043
+ }
5044
+ } else {
5045
+ RAACommitmentOrder :: CommitmentFirst
5046
+ } ;
5047
+
5048
+ if let Some ( ev) = msg_events. get( idx) {
5049
+ match ev {
5050
+ & MessageSendEvent :: SendRevokeAndACK { ref node_id, ref msg } => {
5051
+ assert_eq!( * node_id, $dst_node. node. get_our_node_id( ) ) ;
5052
+ assert!( revoke_and_ack. is_none( ) ) ;
5053
+ revoke_and_ack = Some ( msg. clone( ) ) ;
5054
+ } ,
5055
+ & MessageSendEvent :: UpdateHTLCs { ref node_id, ref updates } => {
5056
+ assert_eq!( * node_id, $dst_node. node. get_our_node_id( ) ) ;
5057
+ assert!( commitment_update. is_none( ) ) ;
5058
+ commitment_update = Some ( updates. clone( ) ) ;
5059
+ } ,
5060
+ _ => panic!( "Unexpected event" ) ,
5061
+ }
5062
+ }
5063
+
5064
+ ( funding_locked, revoke_and_ack, commitment_update, order)
5065
+ }
5066
+ }
5067
+ }
5068
+
4972
5069
/// pending_htlc_adds includes both the holding cell and in-flight update_add_htlcs, whereas
4973
5070
/// for claims/fails they are separated out.
4974
5071
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 ) ) {
@@ -4977,7 +5074,8 @@ mod tests {
4977
5074
4978
5075
let mut resp_1 = Vec :: new ( ) ;
4979
5076
for msg in reestablish_1 {
4980
- resp_1. push ( node_b. node . handle_channel_reestablish ( & node_a. node . get_our_node_id ( ) , & msg) . unwrap ( ) ) ;
5077
+ node_b. node . handle_channel_reestablish ( & node_a. node . get_our_node_id ( ) , & msg) . unwrap ( ) ;
5078
+ resp_1. push ( handle_chan_reestablish_msgs ! ( node_b, node_a) ) ;
4981
5079
}
4982
5080
if pending_cell_htlc_claims. 0 != 0 || pending_cell_htlc_fails. 0 != 0 {
4983
5081
check_added_monitors ! ( node_b, 1 ) ;
@@ -4987,7 +5085,8 @@ mod tests {
4987
5085
4988
5086
let mut resp_2 = Vec :: new ( ) ;
4989
5087
for msg in reestablish_2 {
4990
- resp_2. push ( node_a. node . handle_channel_reestablish ( & node_b. node . get_our_node_id ( ) , & msg) . unwrap ( ) ) ;
5088
+ node_a. node . handle_channel_reestablish ( & node_b. node . get_our_node_id ( ) , & msg) . unwrap ( ) ;
5089
+ resp_2. push ( handle_chan_reestablish_msgs ! ( node_a, node_b) ) ;
4991
5090
}
4992
5091
if pending_cell_htlc_claims. 1 != 0 || pending_cell_htlc_fails. 1 != 0 {
4993
5092
check_added_monitors ! ( node_a, 1 ) ;
@@ -5013,7 +5112,7 @@ mod tests {
5013
5112
assert ! ( chan_msgs. 0 . is_none( ) ) ;
5014
5113
}
5015
5114
if pending_raa. 0 {
5016
- assert ! ( chan_msgs. 3 == msgs :: RAACommitmentOrder :: RevokeAndACKFirst ) ;
5115
+ assert ! ( chan_msgs. 3 == RAACommitmentOrder :: RevokeAndACKFirst ) ;
5017
5116
node_a. node . handle_revoke_and_ack ( & node_b. node . get_our_node_id ( ) , & chan_msgs. 1 . unwrap ( ) ) . unwrap ( ) ;
5018
5117
assert ! ( node_a. node. get_and_clear_pending_msg_events( ) . is_empty( ) ) ;
5019
5118
check_added_monitors ! ( node_a, 1 ) ;
@@ -5070,7 +5169,7 @@ mod tests {
5070
5169
assert ! ( chan_msgs. 0 . is_none( ) ) ;
5071
5170
}
5072
5171
if pending_raa. 1 {
5073
- assert ! ( chan_msgs. 3 == msgs :: RAACommitmentOrder :: RevokeAndACKFirst ) ;
5172
+ assert ! ( chan_msgs. 3 == RAACommitmentOrder :: RevokeAndACKFirst ) ;
5074
5173
node_b. node . handle_revoke_and_ack ( & node_a. node . get_our_node_id ( ) , & chan_msgs. 1 . unwrap ( ) ) . unwrap ( ) ;
5075
5174
assert ! ( node_b. node. get_and_clear_pending_msg_events( ) . is_empty( ) ) ;
5076
5175
check_added_monitors ! ( node_b, 1 ) ;
@@ -5473,16 +5572,18 @@ mod tests {
5473
5572
let reestablish_2 = nodes[ 1 ] . node . peer_connected ( & nodes[ 0 ] . node . get_our_node_id ( ) ) ;
5474
5573
assert_eq ! ( reestablish_2. len( ) , 1 ) ;
5475
5574
5476
- let as_resp = nodes[ 0 ] . node . handle_channel_reestablish ( & nodes[ 1 ] . node . get_our_node_id ( ) , & reestablish_2[ 0 ] ) . unwrap ( ) ;
5477
- let bs_resp = nodes[ 1 ] . node . handle_channel_reestablish ( & nodes[ 0 ] . node . get_our_node_id ( ) , & reestablish_1[ 0 ] ) . unwrap ( ) ;
5575
+ nodes[ 0 ] . node . handle_channel_reestablish ( & nodes[ 1 ] . node . get_our_node_id ( ) , & reestablish_2[ 0 ] ) . unwrap ( ) ;
5576
+ let as_resp = handle_chan_reestablish_msgs ! ( nodes[ 0 ] , nodes[ 1 ] ) ;
5577
+ nodes[ 1 ] . node . handle_channel_reestablish ( & nodes[ 0 ] . node . get_our_node_id ( ) , & reestablish_1[ 0 ] ) . unwrap ( ) ;
5578
+ let bs_resp = handle_chan_reestablish_msgs ! ( nodes[ 1 ] , nodes[ 0 ] ) ;
5478
5579
5479
5580
assert ! ( as_resp. 0 . is_none( ) ) ;
5480
5581
assert ! ( bs_resp. 0 . is_none( ) ) ;
5481
5582
5482
5583
assert ! ( bs_resp. 1 . is_none( ) ) ;
5483
5584
assert ! ( bs_resp. 2 . is_none( ) ) ;
5484
5585
5485
- assert ! ( as_resp. 3 == msgs :: RAACommitmentOrder :: CommitmentFirst ) ;
5586
+ assert ! ( as_resp. 3 == RAACommitmentOrder :: CommitmentFirst ) ;
5486
5587
5487
5588
assert_eq ! ( as_resp. 2 . as_ref( ) . unwrap( ) . update_add_htlcs. len( ) , 1 ) ;
5488
5589
assert ! ( as_resp. 2 . as_ref( ) . unwrap( ) . update_fulfill_htlcs. is_empty( ) ) ;
@@ -5759,8 +5860,10 @@ mod tests {
5759
5860
let reestablish_2 = nodes[ 1 ] . node. peer_connected( & nodes[ 0 ] . node. get_our_node_id( ) ) ;
5760
5861
assert_eq!( reestablish_2. len( ) , 1 ) ;
5761
5862
5762
- let as_resp = nodes[ 0 ] . node. handle_channel_reestablish( & nodes[ 1 ] . node. get_our_node_id( ) , & reestablish_2[ 0 ] ) . unwrap( ) ;
5763
- let bs_resp = nodes[ 1 ] . node. handle_channel_reestablish( & nodes[ 0 ] . node. get_our_node_id( ) , & reestablish_1[ 0 ] ) . unwrap( ) ;
5863
+ nodes[ 0 ] . node. handle_channel_reestablish( & nodes[ 1 ] . node. get_our_node_id( ) , & reestablish_2[ 0 ] ) . unwrap( ) ;
5864
+ let as_resp = handle_chan_reestablish_msgs!( nodes[ 0 ] , nodes[ 1 ] ) ;
5865
+ nodes[ 1 ] . node. handle_channel_reestablish( & nodes[ 0 ] . node. get_our_node_id( ) , & reestablish_1[ 0 ] ) . unwrap( ) ;
5866
+ let bs_resp = handle_chan_reestablish_msgs!( nodes[ 1 ] , nodes[ 0 ] ) ;
5764
5867
5765
5868
assert!( as_resp. 0 . is_none( ) ) ;
5766
5869
assert!( bs_resp. 0 . is_none( ) ) ;
@@ -5777,10 +5880,12 @@ mod tests {
5777
5880
let reestablish_2 = nodes[ 1 ] . node . peer_connected ( & nodes[ 0 ] . node . get_our_node_id ( ) ) ;
5778
5881
assert_eq ! ( reestablish_2. len( ) , 1 ) ;
5779
5882
5780
- let mut as_resp = nodes[ 0 ] . node . handle_channel_reestablish ( & nodes[ 1 ] . node . get_our_node_id ( ) , & reestablish_2[ 0 ] ) . unwrap ( ) ;
5883
+ nodes[ 0 ] . node . handle_channel_reestablish ( & nodes[ 1 ] . node . get_our_node_id ( ) , & reestablish_2[ 0 ] ) . unwrap ( ) ;
5781
5884
check_added_monitors ! ( nodes[ 0 ] , 0 ) ;
5782
- let mut bs_resp = nodes[ 1 ] . node . handle_channel_reestablish ( & nodes[ 0 ] . node . get_our_node_id ( ) , & reestablish_1[ 0 ] ) . unwrap ( ) ;
5885
+ let mut as_resp = handle_chan_reestablish_msgs ! ( nodes[ 0 ] , nodes[ 1 ] ) ;
5886
+ nodes[ 1 ] . node . handle_channel_reestablish ( & nodes[ 0 ] . node . get_our_node_id ( ) , & reestablish_1[ 0 ] ) . unwrap ( ) ;
5783
5887
check_added_monitors ! ( nodes[ 1 ] , 0 ) ;
5888
+ let mut bs_resp = handle_chan_reestablish_msgs ! ( nodes[ 1 ] , nodes[ 0 ] ) ;
5784
5889
5785
5890
assert ! ( as_resp. 0 . is_none( ) ) ;
5786
5891
assert ! ( bs_resp. 0 . is_none( ) ) ;
@@ -5791,7 +5896,7 @@ mod tests {
5791
5896
5792
5897
assert ! ( as_resp. 1 . is_some( ) ) ;
5793
5898
assert ! ( as_resp. 2 . is_some( ) ) ;
5794
- assert ! ( as_resp. 3 == msgs :: RAACommitmentOrder :: CommitmentFirst ) ;
5899
+ assert ! ( as_resp. 3 == RAACommitmentOrder :: CommitmentFirst ) ;
5795
5900
} else {
5796
5901
assert ! ( bs_resp. 2 . as_ref( ) . unwrap( ) . update_add_htlcs. is_empty( ) ) ;
5797
5902
assert ! ( bs_resp. 2 . as_ref( ) . unwrap( ) . update_fail_htlcs. is_empty( ) ) ;
@@ -5900,7 +6005,7 @@ mod tests {
5900
6005
assert ! ( as_resp. 2 . unwrap( ) == as_commitment_update) ;
5901
6006
assert ! ( bs_resp. 2 . is_none( ) ) ;
5902
6007
5903
- assert ! ( as_resp. 3 == msgs :: RAACommitmentOrder :: RevokeAndACKFirst ) ;
6008
+ assert ! ( as_resp. 3 == RAACommitmentOrder :: RevokeAndACKFirst ) ;
5904
6009
}
5905
6010
5906
6011
handle_initial_raa ! ( ) ;
@@ -5926,7 +6031,7 @@ mod tests {
5926
6031
assert ! ( as_resp. 2 . is_none( ) ) ;
5927
6032
assert ! ( bs_resp. 2 . unwrap( ) == bs_second_commitment_update) ;
5928
6033
5929
- assert ! ( bs_resp. 3 == msgs :: RAACommitmentOrder :: RevokeAndACKFirst ) ;
6034
+ assert ! ( bs_resp. 3 == RAACommitmentOrder :: RevokeAndACKFirst ) ;
5930
6035
}
5931
6036
5932
6037
handle_bs_raa ! ( ) ;
0 commit comments