@@ -1775,6 +1775,103 @@ impl<ChanSigner: ChannelKeys, M: Deref> ChannelManager<ChanSigner, M> where M::T
1775
1775
PublicKey :: from_secret_key ( & self . secp_ctx , & self . our_network_key )
1776
1776
}
1777
1777
1778
+ /// Restores a single, given channel to normal operation after a
1779
+ /// ChannelMonitorUpdateErr::TemporaryFailure was returned from a channel monitor update
1780
+ /// operation.
1781
+ ///
1782
+ /// All ChannelMonitor updates up to and including highest_applied_update_id must have been
1783
+ /// fully committed in every copy of the given channels' ChannelMonitors.
1784
+ ///
1785
+ /// Note that there is no effect to calling with a highest_applied_update_id other than the
1786
+ /// current latest ChannelMonitorUpdate and one call to this function after multiple
1787
+ /// ChannelMonitorUpdateErr::TemporaryFailures is fine. The highest_applied_update_id field
1788
+ /// exists largely only to prevent races between this and concurrent update_monitor calls.
1789
+ pub fn channel_monitor_updated ( & self , funding_txo : & OutPoint , highest_applied_update_id : u64 ) {
1790
+ let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
1791
+
1792
+ let mut close_results = Vec :: new ( ) ;
1793
+ let mut htlc_forwards = Vec :: new ( ) ;
1794
+ let mut htlc_failures = Vec :: new ( ) ;
1795
+ let mut pending_events = Vec :: new ( ) ;
1796
+
1797
+ {
1798
+ let mut channel_lock = self . channel_state . lock ( ) . unwrap ( ) ;
1799
+ let channel_state = & mut * channel_lock;
1800
+ let short_to_id = & mut channel_state. short_to_id ;
1801
+ let pending_msg_events = & mut channel_state. pending_msg_events ;
1802
+ let channel = match channel_state. by_id . get_mut ( & funding_txo. to_channel_id ( ) ) {
1803
+ Some ( chan) => chan,
1804
+ None => return ,
1805
+ } ;
1806
+ if !channel. is_awaiting_monitor_update ( ) || channel. get_latest_monitor_update_id ( ) != highest_applied_update_id {
1807
+ return ;
1808
+ }
1809
+
1810
+ let ( raa, commitment_update, order, pending_forwards, mut pending_failures, needs_broadcast_safe, funding_locked) = channel. monitor_updating_restored ( ) ;
1811
+ if !pending_forwards. is_empty ( ) {
1812
+ htlc_forwards. push ( ( channel. get_short_channel_id ( ) . expect ( "We can't have pending forwards before funding confirmation" ) , pending_forwards) ) ;
1813
+ }
1814
+ htlc_failures. append ( & mut pending_failures) ;
1815
+
1816
+ macro_rules! handle_cs { ( ) => {
1817
+ if let Some ( update) = commitment_update {
1818
+ pending_msg_events. push( events:: MessageSendEvent :: UpdateHTLCs {
1819
+ node_id: channel. get_their_node_id( ) ,
1820
+ updates: update,
1821
+ } ) ;
1822
+ }
1823
+ } }
1824
+ macro_rules! handle_raa { ( ) => {
1825
+ if let Some ( revoke_and_ack) = raa {
1826
+ pending_msg_events. push( events:: MessageSendEvent :: SendRevokeAndACK {
1827
+ node_id: channel. get_their_node_id( ) ,
1828
+ msg: revoke_and_ack,
1829
+ } ) ;
1830
+ }
1831
+ } }
1832
+ match order {
1833
+ RAACommitmentOrder :: CommitmentFirst => {
1834
+ handle_cs ! ( ) ;
1835
+ handle_raa ! ( ) ;
1836
+ } ,
1837
+ RAACommitmentOrder :: RevokeAndACKFirst => {
1838
+ handle_raa ! ( ) ;
1839
+ handle_cs ! ( ) ;
1840
+ } ,
1841
+ }
1842
+ if needs_broadcast_safe {
1843
+ pending_events. push ( events:: Event :: FundingBroadcastSafe {
1844
+ funding_txo : channel. get_funding_txo ( ) . unwrap ( ) ,
1845
+ user_channel_id : channel. get_user_id ( ) ,
1846
+ } ) ;
1847
+ }
1848
+ if let Some ( msg) = funding_locked {
1849
+ pending_msg_events. push ( events:: MessageSendEvent :: SendFundingLocked {
1850
+ node_id : channel. get_their_node_id ( ) ,
1851
+ msg,
1852
+ } ) ;
1853
+ if let Some ( announcement_sigs) = self . get_announcement_sigs ( channel) {
1854
+ pending_msg_events. push ( events:: MessageSendEvent :: SendAnnouncementSignatures {
1855
+ node_id : channel. get_their_node_id ( ) ,
1856
+ msg : announcement_sigs,
1857
+ } ) ;
1858
+ }
1859
+ short_to_id. insert ( channel. get_short_channel_id ( ) . unwrap ( ) , channel. channel_id ( ) ) ;
1860
+ }
1861
+ }
1862
+
1863
+ self . pending_events . lock ( ) . unwrap ( ) . append ( & mut pending_events) ;
1864
+
1865
+ for failure in htlc_failures. drain ( ..) {
1866
+ self . fail_htlc_backwards_internal ( self . channel_state . lock ( ) . unwrap ( ) , failure. 0 , & failure. 1 , failure. 2 ) ;
1867
+ }
1868
+ self . forward_htlcs ( & mut htlc_forwards[ ..] ) ;
1869
+
1870
+ for res in close_results. drain ( ..) {
1871
+ self . finish_force_close_channel ( res) ;
1872
+ }
1873
+ }
1874
+
1778
1875
/// Used to restore channels to normal operation after a
1779
1876
/// ChannelMonitorUpdateErr::TemporaryFailure was returned from a channel monitor update
1780
1877
/// operation.
@@ -3323,7 +3420,8 @@ impl<'a, R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>, M: Deref> R
3323
3420
if let Some ( ref mut monitor) = args. channel_monitors . get_mut ( & funding_txo) {
3324
3421
if channel. get_cur_local_commitment_transaction_number ( ) != monitor. get_cur_local_commitment_number ( ) ||
3325
3422
channel. get_revoked_remote_commitment_transaction_number ( ) != monitor. get_min_seen_secret ( ) ||
3326
- channel. get_cur_remote_commitment_transaction_number ( ) != monitor. get_cur_remote_commitment_number ( ) {
3423
+ channel. get_cur_remote_commitment_transaction_number ( ) != monitor. get_cur_remote_commitment_number ( ) ||
3424
+ channel. get_latest_monitor_update_id ( ) != monitor. get_latest_update_id ( ) {
3327
3425
let mut force_close_res = channel. force_shutdown ( ) ;
3328
3426
force_close_res. 0 = monitor. get_latest_local_commitment_txn ( ) ;
3329
3427
closed_channels. push ( force_close_res) ;
0 commit comments