@@ -745,20 +745,30 @@ macro_rules! maybe_break_monitor_err {
745
745
}
746
746
747
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,
748
+ ( $self: ident , $channel_lock: expr, $channel_state: expr, $channel_entry: expr,
749
+ $raa: expr, $commitment_update: expr, $order: expr, $chanmon_update : expr ,
750
750
$pending_forwards: expr, $broadcast_safe: expr, $funding_locked: expr) => { {
751
751
let mut htlc_forwards = None ;
752
752
let mut funding_broadcast_safe = None ;
753
753
let counterparty_node_id = $channel_entry. get( ) . get_counterparty_node_id( ) ;
754
+ let channel_id = $channel_entry. get( ) . channel_id( ) ;
754
755
755
- {
756
+ let res = loop {
756
757
if !$pending_forwards. is_empty( ) {
757
758
htlc_forwards = Some ( ( $channel_entry. get( ) . get_short_channel_id( ) . expect( "We can't have pending forwards before funding confirmation" ) ,
758
759
$channel_entry. get( ) . get_funding_txo( ) . unwrap( ) , $pending_forwards) ) ;
759
760
}
760
761
761
762
macro_rules! handle_cs { ( ) => {
763
+ if let Some ( monitor_update) = $chanmon_update {
764
+ assert!( $order == RAACommitmentOrder :: RevokeAndACKFirst ) ;
765
+ assert!( !$broadcast_safe) ;
766
+ assert!( $funding_locked. is_none( ) ) ;
767
+ assert!( $commitment_update. is_some( ) ) ;
768
+ if let Err ( e) = $self. chain_monitor. update_channel( $channel_entry. get( ) . get_funding_txo( ) . unwrap( ) , monitor_update) {
769
+ break handle_monitor_err!( $self, e, $channel_state, $channel_entry, RAACommitmentOrder :: CommitmentFirst , false , true ) ;
770
+ }
771
+ }
762
772
if let Some ( update) = $commitment_update {
763
773
$channel_state. pending_msg_events. push( events:: MessageSendEvent :: UpdateHTLCs {
764
774
node_id: counterparty_node_id,
@@ -801,21 +811,26 @@ macro_rules! handle_chan_restoration_locked {
801
811
msg: announcement_sigs,
802
812
} ) ;
803
813
}
804
- $channel_state. short_to_id. insert( $channel_entry. get( ) . get_short_channel_id( ) . unwrap( ) , $channel_entry . get ( ) . channel_id( ) ) ;
814
+ $channel_state. short_to_id. insert( $channel_entry. get( ) . get_short_channel_id( ) . unwrap( ) , channel_id) ;
805
815
}
806
- }
807
- ( htlc_forwards, funding_broadcast_safe)
816
+ break Ok ( ( ) ) ;
817
+ } ;
818
+
819
+ ( htlc_forwards, funding_broadcast_safe, res, channel_id, counterparty_node_id)
808
820
} }
809
821
}
810
822
811
823
macro_rules! post_handle_chan_restoration {
812
- ( $self: expr, $locked_res: expr, $pending_failures: expr) => { {
813
- let ( htlc_forwards, funding_broadcast_safe) = $locked_res;
824
+ ( $self: ident, $locked_res: expr, $pending_failures: expr, $forwarding_failures: expr) => { {
825
+ let ( htlc_forwards, funding_broadcast_safe, res, channel_id, counterparty_node_id) = $locked_res;
826
+
827
+ let _ = handle_error!( $self, res, counterparty_node_id) ;
814
828
815
829
if let Some ( ev) = funding_broadcast_safe {
816
830
$self. pending_events. lock( ) . unwrap( ) . push( ev) ;
817
831
}
818
832
833
+ $self. fail_holding_cell_htlcs( $forwarding_failures, channel_id) ;
819
834
for failure in $pending_failures. drain( ..) {
820
835
$self. fail_htlc_backwards_internal( $self. channel_state. lock( ) . unwrap( ) , failure. 0 , & failure. 1 , failure. 2 ) ;
821
836
}
@@ -2332,6 +2347,12 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
2332
2347
/// ChannelMonitorUpdateErr::TemporaryFailures is fine. The highest_applied_update_id field
2333
2348
/// exists largely only to prevent races between this and concurrent update_monitor calls.
2334
2349
///
2350
+ /// In some cases, this may generate a monitor update, resulting in a call to the
2351
+ /// `chain::Watch`'s `update_channel` method for the same channel monitor which is being
2352
+ /// notified of a successful update here. Because of this, please be very careful with
2353
+ /// reentrancy bugs! It is incredibly easy to write an implementation of `update_channel` which
2354
+ /// will take a lock that is also held when calling this method.
2355
+ ///
2335
2356
/// Thus, the anticipated use is, at a high level:
2336
2357
/// 1) You register a chain::Watch with this ChannelManager,
2337
2358
/// 2) it stores each update to disk, and begins updating any remote (eg watchtower) copies of
@@ -2343,7 +2364,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
2343
2364
pub fn channel_monitor_updated ( & self , funding_txo : & OutPoint , highest_applied_update_id : u64 ) {
2344
2365
let _persistence_guard = PersistenceNotifierGuard :: new ( & self . total_consistency_lock , & self . persistence_notifier ) ;
2345
2366
2346
- let ( mut pending_failures, chan_restoration_res) = {
2367
+ let ( mut pending_failures, forwarding_failures , chan_restoration_res) = {
2347
2368
let mut channel_lock = self . channel_state . lock ( ) . unwrap ( ) ;
2348
2369
let channel_state = & mut * channel_lock;
2349
2370
let mut channel = match channel_state. by_id . entry ( funding_txo. to_channel_id ( ) ) {
@@ -2354,10 +2375,10 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
2354
2375
return ;
2355
2376
}
2356
2377
2357
- let ( raa, commitment_update, order, pending_forwards, pending_failures, needs_broadcast_safe, funding_locked) = channel. get_mut ( ) . monitor_updating_restored ( & self . logger ) ;
2358
- ( pending_failures, handle_chan_restoration_locked ! ( self , channel_lock, channel_state, channel, raa, commitment_update, order, pending_forwards, needs_broadcast_safe, funding_locked) )
2378
+ let ( raa, commitment_update, order, chanmon_update , pending_forwards, pending_failures, forwarding_failures , needs_broadcast_safe, funding_locked) = channel. get_mut ( ) . monitor_updating_restored ( & self . logger ) ;
2379
+ ( pending_failures, forwarding_failures , handle_chan_restoration_locked ! ( self , channel_lock, channel_state, channel, raa, commitment_update, order, chanmon_update , pending_forwards, needs_broadcast_safe, funding_locked) )
2359
2380
} ;
2360
- post_handle_chan_restoration ! ( self , chan_restoration_res, pending_failures) ;
2381
+ post_handle_chan_restoration ! ( self , chan_restoration_res, pending_failures, forwarding_failures ) ;
2361
2382
}
2362
2383
2363
2384
fn internal_open_channel ( & self , counterparty_node_id : & PublicKey , their_features : InitFeatures , msg : & msgs:: OpenChannel ) -> Result < ( ) , MsgHandleErrInternal > {
0 commit comments