@@ -744,6 +744,83 @@ macro_rules! maybe_break_monitor_err {
744
744
}
745
745
}
746
746
747
+ macro_rules! handle_chan_restoration_locked {
748
+ ( $self: expr, $channel_lock: expr, $channel_state: expr, $channel_entry: expr,
749
+ $raa: expr, $commitment_update: expr, $order: expr,
750
+ $pending_forwards: expr, $pending_failures: expr, $broadcast_safe: expr, $funding_locked: expr) => { {
751
+ let mut htlc_forwards = Vec :: new( ) ;
752
+ let mut htlc_failures = Vec :: new( ) ;
753
+ let mut pending_events = Vec :: new( ) ;
754
+
755
+ {
756
+ if !$pending_forwards. is_empty( ) {
757
+ htlc_forwards. push( ( $channel_entry. get( ) . get_short_channel_id( ) . expect( "We can't have pending forwards before funding confirmation" ) ,
758
+ $channel_entry. get( ) . get_funding_txo( ) . unwrap( ) , $pending_forwards) ) ;
759
+ }
760
+ htlc_failures. append( & mut $pending_failures) ;
761
+
762
+ macro_rules! handle_cs { ( ) => {
763
+ if let Some ( update) = $commitment_update {
764
+ $channel_state. pending_msg_events. push( events:: MessageSendEvent :: UpdateHTLCs {
765
+ node_id: $channel_entry. get( ) . get_counterparty_node_id( ) ,
766
+ updates: update,
767
+ } ) ;
768
+ }
769
+ } }
770
+ macro_rules! handle_raa { ( ) => {
771
+ if let Some ( revoke_and_ack) = $raa {
772
+ $channel_state. pending_msg_events. push( events:: MessageSendEvent :: SendRevokeAndACK {
773
+ node_id: $channel_entry. get( ) . get_counterparty_node_id( ) ,
774
+ msg: revoke_and_ack,
775
+ } ) ;
776
+ }
777
+ } }
778
+ match $order {
779
+ RAACommitmentOrder :: CommitmentFirst => {
780
+ handle_cs!( ) ;
781
+ handle_raa!( ) ;
782
+ } ,
783
+ RAACommitmentOrder :: RevokeAndACKFirst => {
784
+ handle_raa!( ) ;
785
+ handle_cs!( ) ;
786
+ } ,
787
+ }
788
+ if $broadcast_safe {
789
+ pending_events. push( events:: Event :: FundingBroadcastSafe {
790
+ funding_txo: $channel_entry. get( ) . get_funding_txo( ) . unwrap( ) ,
791
+ user_channel_id: $channel_entry. get( ) . get_user_id( ) ,
792
+ } ) ;
793
+ }
794
+ if let Some ( msg) = $funding_locked {
795
+ $channel_state. pending_msg_events. push( events:: MessageSendEvent :: SendFundingLocked {
796
+ node_id: $channel_entry. get( ) . get_counterparty_node_id( ) ,
797
+ msg,
798
+ } ) ;
799
+ if let Some ( announcement_sigs) = $self. get_announcement_sigs( $channel_entry. get( ) ) {
800
+ $channel_state. pending_msg_events. push( events:: MessageSendEvent :: SendAnnouncementSignatures {
801
+ node_id: $channel_entry. get( ) . get_counterparty_node_id( ) ,
802
+ msg: announcement_sigs,
803
+ } ) ;
804
+ }
805
+ $channel_state. short_to_id. insert( $channel_entry. get( ) . get_short_channel_id( ) . unwrap( ) , $channel_entry. get( ) . channel_id( ) ) ;
806
+ }
807
+ }
808
+ ( htlc_forwards, htlc_failures, pending_events)
809
+ } }
810
+ }
811
+
812
+ macro_rules! post_handle_chan_restoration {
813
+ ( $self: expr, $locked_res: expr) => { {
814
+ let ( mut htlc_forwards, mut htlc_failures, mut pending_events) = $locked_res;
815
+ $self. pending_events. lock( ) . unwrap( ) . append( & mut pending_events) ;
816
+
817
+ for failure in htlc_failures. drain( ..) {
818
+ $self. fail_htlc_backwards_internal( $self. channel_state. lock( ) . unwrap( ) , failure. 0 , & failure. 1 , failure. 2 ) ;
819
+ }
820
+ $self. forward_htlcs( & mut htlc_forwards[ ..] ) ;
821
+ } }
822
+ }
823
+
747
824
impl < Signer : Sign , M : Deref , T : Deref , K : Deref , F : Deref , L : Deref > ChannelManager < Signer , M , T , K , F , L >
748
825
where M :: Target : chain:: Watch < Signer > ,
749
826
T :: Target : BroadcasterInterface ,
@@ -2262,82 +2339,21 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
2262
2339
pub fn channel_monitor_updated ( & self , funding_txo : & OutPoint , highest_applied_update_id : u64 ) {
2263
2340
let _persistence_guard = PersistenceNotifierGuard :: new ( & self . total_consistency_lock , & self . persistence_notifier ) ;
2264
2341
2265
- let mut htlc_forwards = Vec :: new ( ) ;
2266
- let mut htlc_failures = Vec :: new ( ) ;
2267
- let mut pending_events = Vec :: new ( ) ;
2268
-
2269
- {
2342
+ let chan_restoration_res = {
2270
2343
let mut channel_lock = self . channel_state . lock ( ) . unwrap ( ) ;
2271
2344
let channel_state = & mut * channel_lock;
2272
- let short_to_id = & mut channel_state. short_to_id ;
2273
- let pending_msg_events = & mut channel_state. pending_msg_events ;
2274
- let channel = match channel_state. by_id . get_mut ( & funding_txo. to_channel_id ( ) ) {
2275
- Some ( chan) => chan,
2276
- None => return ,
2345
+ let mut channel = match channel_state. by_id . entry ( funding_txo. to_channel_id ( ) ) {
2346
+ hash_map:: Entry :: Occupied ( chan) => chan,
2347
+ hash_map:: Entry :: Vacant ( _) => return ,
2277
2348
} ;
2278
- if !channel. is_awaiting_monitor_update ( ) || channel. get_latest_monitor_update_id ( ) != highest_applied_update_id {
2349
+ if !channel. get ( ) . is_awaiting_monitor_update ( ) || channel. get ( ) . get_latest_monitor_update_id ( ) != highest_applied_update_id {
2279
2350
return ;
2280
2351
}
2281
2352
2282
- let ( raa, commitment_update, order, pending_forwards, mut pending_failures, needs_broadcast_safe, funding_locked) = channel. monitor_updating_restored ( & self . logger ) ;
2283
- if !pending_forwards. is_empty ( ) {
2284
- htlc_forwards. push ( ( channel. get_short_channel_id ( ) . expect ( "We can't have pending forwards before funding confirmation" ) , funding_txo. clone ( ) , pending_forwards) ) ;
2285
- }
2286
- htlc_failures. append ( & mut pending_failures) ;
2287
-
2288
- macro_rules! handle_cs { ( ) => {
2289
- if let Some ( update) = commitment_update {
2290
- pending_msg_events. push( events:: MessageSendEvent :: UpdateHTLCs {
2291
- node_id: channel. get_counterparty_node_id( ) ,
2292
- updates: update,
2293
- } ) ;
2294
- }
2295
- } }
2296
- macro_rules! handle_raa { ( ) => {
2297
- if let Some ( revoke_and_ack) = raa {
2298
- pending_msg_events. push( events:: MessageSendEvent :: SendRevokeAndACK {
2299
- node_id: channel. get_counterparty_node_id( ) ,
2300
- msg: revoke_and_ack,
2301
- } ) ;
2302
- }
2303
- } }
2304
- match order {
2305
- RAACommitmentOrder :: CommitmentFirst => {
2306
- handle_cs ! ( ) ;
2307
- handle_raa ! ( ) ;
2308
- } ,
2309
- RAACommitmentOrder :: RevokeAndACKFirst => {
2310
- handle_raa ! ( ) ;
2311
- handle_cs ! ( ) ;
2312
- } ,
2313
- }
2314
- if needs_broadcast_safe {
2315
- pending_events. push ( events:: Event :: FundingBroadcastSafe {
2316
- funding_txo : channel. get_funding_txo ( ) . unwrap ( ) ,
2317
- user_channel_id : channel. get_user_id ( ) ,
2318
- } ) ;
2319
- }
2320
- if let Some ( msg) = funding_locked {
2321
- pending_msg_events. push ( events:: MessageSendEvent :: SendFundingLocked {
2322
- node_id : channel. get_counterparty_node_id ( ) ,
2323
- msg,
2324
- } ) ;
2325
- if let Some ( announcement_sigs) = self . get_announcement_sigs ( channel) {
2326
- pending_msg_events. push ( events:: MessageSendEvent :: SendAnnouncementSignatures {
2327
- node_id : channel. get_counterparty_node_id ( ) ,
2328
- msg : announcement_sigs,
2329
- } ) ;
2330
- }
2331
- short_to_id. insert ( channel. get_short_channel_id ( ) . unwrap ( ) , channel. channel_id ( ) ) ;
2332
- }
2333
- }
2334
-
2335
- self . pending_events . lock ( ) . unwrap ( ) . append ( & mut pending_events) ;
2336
-
2337
- for failure in htlc_failures. drain ( ..) {
2338
- self . fail_htlc_backwards_internal ( self . channel_state . lock ( ) . unwrap ( ) , failure. 0 , & failure. 1 , failure. 2 ) ;
2339
- }
2340
- self . forward_htlcs ( & mut htlc_forwards[ ..] ) ;
2353
+ let ( raa, commitment_update, order, pending_forwards, mut pending_failures, needs_broadcast_safe, funding_locked) = channel. get_mut ( ) . monitor_updating_restored ( & self . logger ) ;
2354
+ handle_chan_restoration_locked ! ( self , channel_lock, channel_state, channel, raa, commitment_update, order, pending_forwards, pending_failures, needs_broadcast_safe, funding_locked)
2355
+ } ;
2356
+ post_handle_chan_restoration ! ( self , chan_restoration_res) ;
2341
2357
}
2342
2358
2343
2359
fn internal_open_channel ( & self , counterparty_node_id : & PublicKey , their_features : InitFeatures , msg : & msgs:: OpenChannel ) -> Result < ( ) , MsgHandleErrInternal > {
0 commit comments