@@ -26,7 +26,7 @@ use ln::channel::{Channel, ChannelError, ChannelKeys};
2626use ln:: channelmonitor:: { ChannelMonitorUpdateErr , ManyChannelMonitor } ;
2727use ln:: router:: { Route , RouteHop } ;
2828use ln:: msgs;
29- use ln:: msgs:: { ChannelMessageHandler , HandleError , RAACommitmentOrder } ;
29+ use ln:: msgs:: { ChannelMessageHandler , HandleError } ;
3030use util:: { byte_utils, events, internal_traits, rng} ;
3131use util:: sha2:: Sha256 ;
3232use util:: ser:: { Readable , Writeable } ;
@@ -229,6 +229,18 @@ struct HTLCForwardInfo {
229229 forward_info : PendingForwardHTLCInfo ,
230230}
231231
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+
232244struct ChannelHolder {
233245 by_id : HashMap < [ u8 ; 32 ] , Channel > ,
234246 short_to_id : HashMap < u64 , [ u8 ; 32 ] > ,
@@ -2112,28 +2124,58 @@ impl ChannelManager {
21122124 Ok ( ( ) )
21132125 }
21142126
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 ! ( ) ;
21222141 }
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+ } ) ;
21292155 }
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+ }
21372179 }
21382180
21392181 /// Begin Update fee process. Allowed only on an outbound channel.
@@ -2400,7 +2442,7 @@ impl ChannelMessageHandler for ChannelManager {
24002442 handle_error ! ( self , self . internal_announcement_signatures( their_node_id, msg) , their_node_id)
24012443 }
24022444
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 > {
24042446 handle_error ! ( self , self . internal_channel_reestablish( their_node_id, msg) , their_node_id)
24052447 }
24062448
@@ -2498,7 +2540,7 @@ mod tests {
24982540 use chain:: chaininterface;
24992541 use chain:: transaction:: OutPoint ;
25002542 use chain:: chaininterface:: ChainListener ;
2501- use ln:: channelmanager:: { ChannelManager , OnionKeys } ;
2543+ use ln:: channelmanager:: { ChannelManager , OnionKeys , RAACommitmentOrder } ;
25022544 use ln:: channelmonitor:: ChannelMonitorUpdateErr ;
25032545 use ln:: router:: { Route , RouteHop , Router } ;
25042546 use ln:: msgs;
@@ -4969,6 +5011,61 @@ mod tests {
49695011 assert_eq ! ( channel_state. short_to_id. len( ) , 0 ) ;
49705012 }
49715013
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+
49725069 /// pending_htlc_adds includes both the holding cell and in-flight update_add_htlcs, whereas
49735070 /// for claims/fails they are separated out.
49745071 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 {
49775074
49785075 let mut resp_1 = Vec :: new ( ) ;
49795076 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) ) ;
49815079 }
49825080 if pending_cell_htlc_claims. 0 != 0 || pending_cell_htlc_fails. 0 != 0 {
49835081 check_added_monitors ! ( node_b, 1 ) ;
@@ -4987,7 +5085,8 @@ mod tests {
49875085
49885086 let mut resp_2 = Vec :: new ( ) ;
49895087 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) ) ;
49915090 }
49925091 if pending_cell_htlc_claims. 1 != 0 || pending_cell_htlc_fails. 1 != 0 {
49935092 check_added_monitors ! ( node_a, 1 ) ;
@@ -5013,7 +5112,7 @@ mod tests {
50135112 assert ! ( chan_msgs. 0 . is_none( ) ) ;
50145113 }
50155114 if pending_raa. 0 {
5016- assert ! ( chan_msgs. 3 == msgs :: RAACommitmentOrder :: RevokeAndACKFirst ) ;
5115+ assert ! ( chan_msgs. 3 == RAACommitmentOrder :: RevokeAndACKFirst ) ;
50175116 node_a. node . handle_revoke_and_ack ( & node_b. node . get_our_node_id ( ) , & chan_msgs. 1 . unwrap ( ) ) . unwrap ( ) ;
50185117 assert ! ( node_a. node. get_and_clear_pending_msg_events( ) . is_empty( ) ) ;
50195118 check_added_monitors ! ( node_a, 1 ) ;
@@ -5070,7 +5169,7 @@ mod tests {
50705169 assert ! ( chan_msgs. 0 . is_none( ) ) ;
50715170 }
50725171 if pending_raa. 1 {
5073- assert ! ( chan_msgs. 3 == msgs :: RAACommitmentOrder :: RevokeAndACKFirst ) ;
5172+ assert ! ( chan_msgs. 3 == RAACommitmentOrder :: RevokeAndACKFirst ) ;
50745173 node_b. node . handle_revoke_and_ack ( & node_a. node . get_our_node_id ( ) , & chan_msgs. 1 . unwrap ( ) ) . unwrap ( ) ;
50755174 assert ! ( node_b. node. get_and_clear_pending_msg_events( ) . is_empty( ) ) ;
50765175 check_added_monitors ! ( node_b, 1 ) ;
@@ -5473,16 +5572,18 @@ mod tests {
54735572 let reestablish_2 = nodes[ 1 ] . node . peer_connected ( & nodes[ 0 ] . node . get_our_node_id ( ) ) ;
54745573 assert_eq ! ( reestablish_2. len( ) , 1 ) ;
54755574
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 ] ) ;
54785579
54795580 assert ! ( as_resp. 0 . is_none( ) ) ;
54805581 assert ! ( bs_resp. 0 . is_none( ) ) ;
54815582
54825583 assert ! ( bs_resp. 1 . is_none( ) ) ;
54835584 assert ! ( bs_resp. 2 . is_none( ) ) ;
54845585
5485- assert ! ( as_resp. 3 == msgs :: RAACommitmentOrder :: CommitmentFirst ) ;
5586+ assert ! ( as_resp. 3 == RAACommitmentOrder :: CommitmentFirst ) ;
54865587
54875588 assert_eq ! ( as_resp. 2 . as_ref( ) . unwrap( ) . update_add_htlcs. len( ) , 1 ) ;
54885589 assert ! ( as_resp. 2 . as_ref( ) . unwrap( ) . update_fulfill_htlcs. is_empty( ) ) ;
@@ -5759,8 +5860,10 @@ mod tests {
57595860 let reestablish_2 = nodes[ 1 ] . node. peer_connected( & nodes[ 0 ] . node. get_our_node_id( ) ) ;
57605861 assert_eq!( reestablish_2. len( ) , 1 ) ;
57615862
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 ] ) ;
57645867
57655868 assert!( as_resp. 0 . is_none( ) ) ;
57665869 assert!( bs_resp. 0 . is_none( ) ) ;
@@ -5777,10 +5880,12 @@ mod tests {
57775880 let reestablish_2 = nodes[ 1 ] . node . peer_connected ( & nodes[ 0 ] . node . get_our_node_id ( ) ) ;
57785881 assert_eq ! ( reestablish_2. len( ) , 1 ) ;
57795882
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 ( ) ;
57815884 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 ( ) ;
57835887 check_added_monitors ! ( nodes[ 1 ] , 0 ) ;
5888+ let mut bs_resp = handle_chan_reestablish_msgs ! ( nodes[ 1 ] , nodes[ 0 ] ) ;
57845889
57855890 assert ! ( as_resp. 0 . is_none( ) ) ;
57865891 assert ! ( bs_resp. 0 . is_none( ) ) ;
@@ -5791,7 +5896,7 @@ mod tests {
57915896
57925897 assert ! ( as_resp. 1 . is_some( ) ) ;
57935898 assert ! ( as_resp. 2 . is_some( ) ) ;
5794- assert ! ( as_resp. 3 == msgs :: RAACommitmentOrder :: CommitmentFirst ) ;
5899+ assert ! ( as_resp. 3 == RAACommitmentOrder :: CommitmentFirst ) ;
57955900 } else {
57965901 assert ! ( bs_resp. 2 . as_ref( ) . unwrap( ) . update_add_htlcs. is_empty( ) ) ;
57975902 assert ! ( bs_resp. 2 . as_ref( ) . unwrap( ) . update_fail_htlcs. is_empty( ) ) ;
@@ -5900,7 +6005,7 @@ mod tests {
59006005 assert ! ( as_resp. 2 . unwrap( ) == as_commitment_update) ;
59016006 assert ! ( bs_resp. 2 . is_none( ) ) ;
59026007
5903- assert ! ( as_resp. 3 == msgs :: RAACommitmentOrder :: RevokeAndACKFirst ) ;
6008+ assert ! ( as_resp. 3 == RAACommitmentOrder :: RevokeAndACKFirst ) ;
59046009 }
59056010
59066011 handle_initial_raa ! ( ) ;
@@ -5926,7 +6031,7 @@ mod tests {
59266031 assert ! ( as_resp. 2 . is_none( ) ) ;
59276032 assert ! ( bs_resp. 2 . unwrap( ) == bs_second_commitment_update) ;
59286033
5929- assert ! ( bs_resp. 3 == msgs :: RAACommitmentOrder :: RevokeAndACKFirst ) ;
6034+ assert ! ( bs_resp. 3 == RAACommitmentOrder :: RevokeAndACKFirst ) ;
59306035 }
59316036
59326037 handle_bs_raa ! ( ) ;
0 commit comments