@@ -1775,6 +1775,111 @@ 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
+ ///
1790
+ /// Thus, the anticipated use is, at a high level:
1791
+ /// 1) You register a ManyChannelMonitor with this ChannelManager.
1792
+ /// 2) it stores each update to disk, and begins updating any remote (eg watchtower) copies of
1793
+ /// said ChannelMonitors as it can, returning ChannelMonitorUpdateErr::TemporaryFailures
1794
+ /// any time it cannot do so instantly,
1795
+ /// 3) once all remote copies are updated, you call this function with the update_id that
1796
+ /// completed, and once it is the latest the Channel will be re-enabled.
1797
+ pub fn channel_monitor_updated ( & self , funding_txo : & OutPoint , highest_applied_update_id : u64 ) {
1798
+ let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
1799
+
1800
+ let mut close_results = Vec :: new ( ) ;
1801
+ let mut htlc_forwards = Vec :: new ( ) ;
1802
+ let mut htlc_failures = Vec :: new ( ) ;
1803
+ let mut pending_events = Vec :: new ( ) ;
1804
+
1805
+ {
1806
+ let mut channel_lock = self . channel_state . lock ( ) . unwrap ( ) ;
1807
+ let channel_state = & mut * channel_lock;
1808
+ let short_to_id = & mut channel_state. short_to_id ;
1809
+ let pending_msg_events = & mut channel_state. pending_msg_events ;
1810
+ let channel = match channel_state. by_id . get_mut ( & funding_txo. to_channel_id ( ) ) {
1811
+ Some ( chan) => chan,
1812
+ None => return ,
1813
+ } ;
1814
+ if !channel. is_awaiting_monitor_update ( ) || channel. get_latest_monitor_update_id ( ) != highest_applied_update_id {
1815
+ return ;
1816
+ }
1817
+
1818
+ let ( raa, commitment_update, order, pending_forwards, mut pending_failures, needs_broadcast_safe, funding_locked) = channel. monitor_updating_restored ( ) ;
1819
+ if !pending_forwards. is_empty ( ) {
1820
+ htlc_forwards. push ( ( channel. get_short_channel_id ( ) . expect ( "We can't have pending forwards before funding confirmation" ) , pending_forwards) ) ;
1821
+ }
1822
+ htlc_failures. append ( & mut pending_failures) ;
1823
+
1824
+ macro_rules! handle_cs { ( ) => {
1825
+ if let Some ( update) = commitment_update {
1826
+ pending_msg_events. push( events:: MessageSendEvent :: UpdateHTLCs {
1827
+ node_id: channel. get_their_node_id( ) ,
1828
+ updates: update,
1829
+ } ) ;
1830
+ }
1831
+ } }
1832
+ macro_rules! handle_raa { ( ) => {
1833
+ if let Some ( revoke_and_ack) = raa {
1834
+ pending_msg_events. push( events:: MessageSendEvent :: SendRevokeAndACK {
1835
+ node_id: channel. get_their_node_id( ) ,
1836
+ msg: revoke_and_ack,
1837
+ } ) ;
1838
+ }
1839
+ } }
1840
+ match order {
1841
+ RAACommitmentOrder :: CommitmentFirst => {
1842
+ handle_cs ! ( ) ;
1843
+ handle_raa ! ( ) ;
1844
+ } ,
1845
+ RAACommitmentOrder :: RevokeAndACKFirst => {
1846
+ handle_raa ! ( ) ;
1847
+ handle_cs ! ( ) ;
1848
+ } ,
1849
+ }
1850
+ if needs_broadcast_safe {
1851
+ pending_events. push ( events:: Event :: FundingBroadcastSafe {
1852
+ funding_txo : channel. get_funding_txo ( ) . unwrap ( ) ,
1853
+ user_channel_id : channel. get_user_id ( ) ,
1854
+ } ) ;
1855
+ }
1856
+ if let Some ( msg) = funding_locked {
1857
+ pending_msg_events. push ( events:: MessageSendEvent :: SendFundingLocked {
1858
+ node_id : channel. get_their_node_id ( ) ,
1859
+ msg,
1860
+ } ) ;
1861
+ if let Some ( announcement_sigs) = self . get_announcement_sigs ( channel) {
1862
+ pending_msg_events. push ( events:: MessageSendEvent :: SendAnnouncementSignatures {
1863
+ node_id : channel. get_their_node_id ( ) ,
1864
+ msg : announcement_sigs,
1865
+ } ) ;
1866
+ }
1867
+ short_to_id. insert ( channel. get_short_channel_id ( ) . unwrap ( ) , channel. channel_id ( ) ) ;
1868
+ }
1869
+ }
1870
+
1871
+ self . pending_events . lock ( ) . unwrap ( ) . append ( & mut pending_events) ;
1872
+
1873
+ for failure in htlc_failures. drain ( ..) {
1874
+ self . fail_htlc_backwards_internal ( self . channel_state . lock ( ) . unwrap ( ) , failure. 0 , & failure. 1 , failure. 2 ) ;
1875
+ }
1876
+ self . forward_htlcs ( & mut htlc_forwards[ ..] ) ;
1877
+
1878
+ for res in close_results. drain ( ..) {
1879
+ self . finish_force_close_channel ( res) ;
1880
+ }
1881
+ }
1882
+
1778
1883
/// Used to restore channels to normal operation after a
1779
1884
/// ChannelMonitorUpdateErr::TemporaryFailure was returned from a channel monitor update
1780
1885
/// operation.
@@ -3323,7 +3428,8 @@ impl<'a, R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>, M: Deref> R
3323
3428
if let Some ( ref mut monitor) = args. channel_monitors . get_mut ( & funding_txo) {
3324
3429
if channel. get_cur_local_commitment_transaction_number ( ) != monitor. get_cur_local_commitment_number ( ) ||
3325
3430
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 ( ) {
3431
+ channel. get_cur_remote_commitment_transaction_number ( ) != monitor. get_cur_remote_commitment_number ( ) ||
3432
+ channel. get_latest_monitor_update_id ( ) != monitor. get_latest_update_id ( ) {
3327
3433
let mut force_close_res = channel. force_shutdown ( ) ;
3328
3434
force_close_res. 0 = monitor. get_latest_local_commitment_txn ( ) ;
3329
3435
closed_channels. push ( force_close_res) ;
0 commit comments