@@ -1781,6 +1781,111 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref> ChannelManager<ChanSigner, M,
1781
1781
PublicKey :: from_secret_key ( & self . secp_ctx , & self . our_network_key )
1782
1782
}
1783
1783
1784
+ /// Restores a single, given channel to normal operation after a
1785
+ /// ChannelMonitorUpdateErr::TemporaryFailure was returned from a channel monitor update
1786
+ /// operation.
1787
+ ///
1788
+ /// All ChannelMonitor updates up to and including highest_applied_update_id must have been
1789
+ /// fully committed in every copy of the given channels' ChannelMonitors.
1790
+ ///
1791
+ /// Note that there is no effect to calling with a highest_applied_update_id other than the
1792
+ /// current latest ChannelMonitorUpdate and one call to this function after multiple
1793
+ /// ChannelMonitorUpdateErr::TemporaryFailures is fine. The highest_applied_update_id field
1794
+ /// exists largely only to prevent races between this and concurrent update_monitor calls.
1795
+ ///
1796
+ /// Thus, the anticipated use is, at a high level:
1797
+ /// 1) You register a ManyChannelMonitor with this ChannelManager.
1798
+ /// 2) it stores each update to disk, and begins updating any remote (eg watchtower) copies of
1799
+ /// said ChannelMonitors as it can, returning ChannelMonitorUpdateErr::TemporaryFailures
1800
+ /// any time it cannot do so instantly,
1801
+ /// 3) once all remote copies are updated, you call this function with the update_id that
1802
+ /// completed, and once it is the latest the Channel will be re-enabled.
1803
+ pub fn channel_monitor_updated ( & self , funding_txo : & OutPoint , highest_applied_update_id : u64 ) {
1804
+ let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
1805
+
1806
+ let mut close_results = Vec :: new ( ) ;
1807
+ let mut htlc_forwards = Vec :: new ( ) ;
1808
+ let mut htlc_failures = Vec :: new ( ) ;
1809
+ let mut pending_events = Vec :: new ( ) ;
1810
+
1811
+ {
1812
+ let mut channel_lock = self . channel_state . lock ( ) . unwrap ( ) ;
1813
+ let channel_state = & mut * channel_lock;
1814
+ let short_to_id = & mut channel_state. short_to_id ;
1815
+ let pending_msg_events = & mut channel_state. pending_msg_events ;
1816
+ let channel = match channel_state. by_id . get_mut ( & funding_txo. to_channel_id ( ) ) {
1817
+ Some ( chan) => chan,
1818
+ None => return ,
1819
+ } ;
1820
+ if !channel. is_awaiting_monitor_update ( ) || channel. get_latest_monitor_update_id ( ) != highest_applied_update_id {
1821
+ return ;
1822
+ }
1823
+
1824
+ let ( raa, commitment_update, order, pending_forwards, mut pending_failures, needs_broadcast_safe, funding_locked) = channel. monitor_updating_restored ( ) ;
1825
+ if !pending_forwards. is_empty ( ) {
1826
+ htlc_forwards. push ( ( channel. get_short_channel_id ( ) . expect ( "We can't have pending forwards before funding confirmation" ) , pending_forwards) ) ;
1827
+ }
1828
+ htlc_failures. append ( & mut pending_failures) ;
1829
+
1830
+ macro_rules! handle_cs { ( ) => {
1831
+ if let Some ( update) = commitment_update {
1832
+ pending_msg_events. push( events:: MessageSendEvent :: UpdateHTLCs {
1833
+ node_id: channel. get_their_node_id( ) ,
1834
+ updates: update,
1835
+ } ) ;
1836
+ }
1837
+ } }
1838
+ macro_rules! handle_raa { ( ) => {
1839
+ if let Some ( revoke_and_ack) = raa {
1840
+ pending_msg_events. push( events:: MessageSendEvent :: SendRevokeAndACK {
1841
+ node_id: channel. get_their_node_id( ) ,
1842
+ msg: revoke_and_ack,
1843
+ } ) ;
1844
+ }
1845
+ } }
1846
+ match order {
1847
+ RAACommitmentOrder :: CommitmentFirst => {
1848
+ handle_cs ! ( ) ;
1849
+ handle_raa ! ( ) ;
1850
+ } ,
1851
+ RAACommitmentOrder :: RevokeAndACKFirst => {
1852
+ handle_raa ! ( ) ;
1853
+ handle_cs ! ( ) ;
1854
+ } ,
1855
+ }
1856
+ if needs_broadcast_safe {
1857
+ pending_events. push ( events:: Event :: FundingBroadcastSafe {
1858
+ funding_txo : channel. get_funding_txo ( ) . unwrap ( ) ,
1859
+ user_channel_id : channel. get_user_id ( ) ,
1860
+ } ) ;
1861
+ }
1862
+ if let Some ( msg) = funding_locked {
1863
+ pending_msg_events. push ( events:: MessageSendEvent :: SendFundingLocked {
1864
+ node_id : channel. get_their_node_id ( ) ,
1865
+ msg,
1866
+ } ) ;
1867
+ if let Some ( announcement_sigs) = self . get_announcement_sigs ( channel) {
1868
+ pending_msg_events. push ( events:: MessageSendEvent :: SendAnnouncementSignatures {
1869
+ node_id : channel. get_their_node_id ( ) ,
1870
+ msg : announcement_sigs,
1871
+ } ) ;
1872
+ }
1873
+ short_to_id. insert ( channel. get_short_channel_id ( ) . unwrap ( ) , channel. channel_id ( ) ) ;
1874
+ }
1875
+ }
1876
+
1877
+ self . pending_events . lock ( ) . unwrap ( ) . append ( & mut pending_events) ;
1878
+
1879
+ for failure in htlc_failures. drain ( ..) {
1880
+ self . fail_htlc_backwards_internal ( self . channel_state . lock ( ) . unwrap ( ) , failure. 0 , & failure. 1 , failure. 2 ) ;
1881
+ }
1882
+ self . forward_htlcs ( & mut htlc_forwards[ ..] ) ;
1883
+
1884
+ for res in close_results. drain ( ..) {
1885
+ self . finish_force_close_channel ( res) ;
1886
+ }
1887
+ }
1888
+
1784
1889
/// Used to restore channels to normal operation after a
1785
1890
/// ChannelMonitorUpdateErr::TemporaryFailure was returned from a channel monitor update
1786
1891
/// operation.
@@ -3351,7 +3456,8 @@ impl<'a, R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>, M: Deref, T
3351
3456
if let Some ( ref mut monitor) = args. channel_monitors . get_mut ( & funding_txo) {
3352
3457
if channel. get_cur_local_commitment_transaction_number ( ) != monitor. get_cur_local_commitment_number ( ) ||
3353
3458
channel. get_revoked_remote_commitment_transaction_number ( ) != monitor. get_min_seen_secret ( ) ||
3354
- channel. get_cur_remote_commitment_transaction_number ( ) != monitor. get_cur_remote_commitment_number ( ) {
3459
+ channel. get_cur_remote_commitment_transaction_number ( ) != monitor. get_cur_remote_commitment_number ( ) ||
3460
+ channel. get_latest_monitor_update_id ( ) != monitor. get_latest_update_id ( ) {
3355
3461
let mut force_close_res = channel. force_shutdown ( ) ;
3356
3462
force_close_res. 0 = monitor. get_latest_local_commitment_txn ( ) ;
3357
3463
closed_channels. push ( force_close_res) ;
0 commit comments