@@ -1986,6 +1986,103 @@ impl<ChanSigner: ChannelKeys, M: Deref> ChannelManager<ChanSigner, M> where M::T
1986
1986
PublicKey :: from_secret_key ( & self . secp_ctx , & self . our_network_key )
1987
1987
}
1988
1988
1989
+ /// Restores a single, given channel to normal operation after a
1990
+ /// ChannelMonitorUpdateErr::TemporaryFailure was returned from a channel monitor update
1991
+ /// operation.
1992
+ ///
1993
+ /// All ChannelMonitor updates up to and including highest_applied_update_id must have been
1994
+ /// fully committed in every copy of the given channels' ChannelMonitors.
1995
+ ///
1996
+ /// Note that there is no effect to calling with a highest_applied_update_id other than the
1997
+ /// current latest ChannelMonitorUpdate and one call to this function after multiple
1998
+ /// ChannelMonitorUpdateErr::TemporaryFailures is fine. The highest_applied_update_id field
1999
+ /// exists largely only to prevent races between this and concurrent update_monitor calls.
2000
+ pub fn channel_monitor_updated ( & self , funding_txo : & OutPoint , highest_applied_update_id : u64 ) {
2001
+ let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
2002
+
2003
+ let mut close_results = Vec :: new ( ) ;
2004
+ let mut htlc_forwards = Vec :: new ( ) ;
2005
+ let mut htlc_failures = Vec :: new ( ) ;
2006
+ let mut pending_events = Vec :: new ( ) ;
2007
+
2008
+ {
2009
+ let mut channel_lock = self . channel_state . lock ( ) . unwrap ( ) ;
2010
+ let channel_state = & mut * channel_lock;
2011
+ let short_to_id = & mut channel_state. short_to_id ;
2012
+ let pending_msg_events = & mut channel_state. pending_msg_events ;
2013
+ let channel = match channel_state. by_id . get_mut ( & funding_txo. to_channel_id ( ) ) {
2014
+ Some ( chan) => chan,
2015
+ None => return ,
2016
+ } ;
2017
+ if channel. get_latest_monitor_update_id ( ) != highest_applied_update_id {
2018
+ return ;
2019
+ }
2020
+
2021
+ let ( raa, commitment_update, order, pending_forwards, mut pending_failures, needs_broadcast_safe, funding_locked) = channel. monitor_updating_restored ( ) ;
2022
+ if !pending_forwards. is_empty ( ) {
2023
+ htlc_forwards. push ( ( channel. get_short_channel_id ( ) . expect ( "We can't have pending forwards before funding confirmation" ) , pending_forwards) ) ;
2024
+ }
2025
+ htlc_failures. append ( & mut pending_failures) ;
2026
+
2027
+ macro_rules! handle_cs { ( ) => {
2028
+ if let Some ( update) = commitment_update {
2029
+ pending_msg_events. push( events:: MessageSendEvent :: UpdateHTLCs {
2030
+ node_id: channel. get_their_node_id( ) ,
2031
+ updates: update,
2032
+ } ) ;
2033
+ }
2034
+ } }
2035
+ macro_rules! handle_raa { ( ) => {
2036
+ if let Some ( revoke_and_ack) = raa {
2037
+ pending_msg_events. push( events:: MessageSendEvent :: SendRevokeAndACK {
2038
+ node_id: channel. get_their_node_id( ) ,
2039
+ msg: revoke_and_ack,
2040
+ } ) ;
2041
+ }
2042
+ } }
2043
+ match order {
2044
+ RAACommitmentOrder :: CommitmentFirst => {
2045
+ handle_cs ! ( ) ;
2046
+ handle_raa ! ( ) ;
2047
+ } ,
2048
+ RAACommitmentOrder :: RevokeAndACKFirst => {
2049
+ handle_raa ! ( ) ;
2050
+ handle_cs ! ( ) ;
2051
+ } ,
2052
+ }
2053
+ if needs_broadcast_safe {
2054
+ pending_events. push ( events:: Event :: FundingBroadcastSafe {
2055
+ funding_txo : channel. get_funding_txo ( ) . unwrap ( ) ,
2056
+ user_channel_id : channel. get_user_id ( ) ,
2057
+ } ) ;
2058
+ }
2059
+ if let Some ( msg) = funding_locked {
2060
+ pending_msg_events. push ( events:: MessageSendEvent :: SendFundingLocked {
2061
+ node_id : channel. get_their_node_id ( ) ,
2062
+ msg,
2063
+ } ) ;
2064
+ if let Some ( announcement_sigs) = self . get_announcement_sigs ( channel) {
2065
+ pending_msg_events. push ( events:: MessageSendEvent :: SendAnnouncementSignatures {
2066
+ node_id : channel. get_their_node_id ( ) ,
2067
+ msg : announcement_sigs,
2068
+ } ) ;
2069
+ }
2070
+ short_to_id. insert ( channel. get_short_channel_id ( ) . unwrap ( ) , channel. channel_id ( ) ) ;
2071
+ }
2072
+ }
2073
+
2074
+ self . pending_events . lock ( ) . unwrap ( ) . append ( & mut pending_events) ;
2075
+
2076
+ for failure in htlc_failures. drain ( ..) {
2077
+ self . fail_htlc_backwards_internal ( self . channel_state . lock ( ) . unwrap ( ) , failure. 0 , & failure. 1 , failure. 2 ) ;
2078
+ }
2079
+ self . forward_htlcs ( & mut htlc_forwards[ ..] ) ;
2080
+
2081
+ for res in close_results. drain ( ..) {
2082
+ self . finish_force_close_channel ( res) ;
2083
+ }
2084
+ }
2085
+
1989
2086
/// Used to restore channels to normal operation after a
1990
2087
/// ChannelMonitorUpdateErr::TemporaryFailure was returned from a channel monitor update
1991
2088
/// operation.
@@ -3532,7 +3629,8 @@ impl<'a, R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>, M: Deref> R
3532
3629
if let Some ( ref mut monitor) = args. channel_monitors . get_mut ( & funding_txo) {
3533
3630
if channel. get_cur_local_commitment_transaction_number ( ) != monitor. get_cur_local_commitment_number ( ) ||
3534
3631
channel. get_revoked_remote_commitment_transaction_number ( ) != monitor. get_min_seen_secret ( ) ||
3535
- channel. get_cur_remote_commitment_transaction_number ( ) != monitor. get_cur_remote_commitment_number ( ) {
3632
+ channel. get_cur_remote_commitment_transaction_number ( ) != monitor. get_cur_remote_commitment_number ( ) ||
3633
+ channel. get_latest_monitor_update_id ( ) != monitor. get_latest_update_id ( ) {
3536
3634
let mut force_close_res = channel. force_shutdown ( ) ;
3537
3635
force_close_res. 0 = monitor. get_latest_local_commitment_txn ( ) ;
3538
3636
closed_channels. push ( force_close_res) ;
0 commit comments