@@ -24,6 +24,7 @@ use chain::transaction::OutPoint;
24
24
use util:: { transaction_utils, rng} ;
25
25
use util:: sha2:: Sha256 ;
26
26
27
+ use std;
27
28
use std:: default:: Default ;
28
29
use std:: { cmp, mem} ;
29
30
use std:: time:: Instant ;
@@ -927,7 +928,7 @@ impl Channel {
927
928
Ok ( our_sig)
928
929
}
929
930
930
- pub fn get_update_fulfill_htlc ( & mut self , payment_preimage_arg : [ u8 ; 32 ] ) -> Result < Option < ( msgs:: UpdateFulfillHTLC , ChannelMonitor ) > , HandleError > {
931
+ fn get_update_fulfill_htlc ( & mut self , payment_preimage_arg : [ u8 ; 32 ] ) -> Result < ( Option < msgs:: UpdateFulfillHTLC > , Option < ChannelMonitor > ) , HandleError > {
931
932
// Either ChannelFunded got set (which means it wont bet unset) or there is no way any
932
933
// caller thought we could have something claimed (cause we wouldn't have accepted in an
933
934
// incoming HTLC anyway). If we got to ShutdownComplete, callers aren't allowed to call us,
@@ -942,13 +943,31 @@ impl Channel {
942
943
let mut payment_hash_calc = [ 0 ; 32 ] ;
943
944
sha. result ( & mut payment_hash_calc) ;
944
945
946
+ let mut pending_idx = std:: usize:: MAX ;
947
+ for ( idx, htlc) in self . pending_htlcs . iter ( ) . enumerate ( ) {
948
+ if !htlc. outbound && htlc. payment_hash == payment_hash_calc {
949
+ if pending_idx != std:: usize:: MAX {
950
+ panic ! ( "Duplicate HTLC payment_hash, you probably re-used payment preimages, NEVER DO THIS!" ) ;
951
+ }
952
+ pending_idx = idx;
953
+ }
954
+ }
955
+ if pending_idx == std:: usize:: MAX {
956
+ return Err ( HandleError { err : "Unable to find a pending HTLC which matched the given payment preimage" , action : None } ) ;
957
+ }
958
+
945
959
// Now update local state:
960
+ //
961
+ // We have to put the payment_preimage in the channel_monitor right away here to ensure we
962
+ // can claim it even if the channel hits the chain before we see their next commitment.
963
+ self . channel_monitor . provide_payment_preimage ( & payment_hash_calc, & payment_preimage_arg) ;
964
+
946
965
if ( self . channel_state & ( ChannelState :: AwaitingRemoteRevoke as u32 ) ) == ( ChannelState :: AwaitingRemoteRevoke as u32 ) {
947
966
for pending_update in self . holding_cell_htlc_updates . iter ( ) {
948
967
match pending_update {
949
968
& HTLCUpdateAwaitingACK :: ClaimHTLC { ref payment_preimage, .. } => {
950
969
if payment_preimage_arg == * payment_preimage {
951
- return Ok ( None ) ;
970
+ return Ok ( ( None , None ) ) ;
952
971
}
953
972
} ,
954
973
& HTLCUpdateAwaitingACK :: FailHTLC { ref payment_hash, .. } => {
@@ -962,49 +981,39 @@ impl Channel {
962
981
self . holding_cell_htlc_updates . push ( HTLCUpdateAwaitingACK :: ClaimHTLC {
963
982
payment_preimage : payment_preimage_arg, payment_hash : payment_hash_calc,
964
983
} ) ;
965
- return Ok ( None ) ;
966
- }
967
-
968
- let mut htlc_id = 0 ;
969
- let mut htlc_amount_msat = 0 ;
970
- for htlc in self . pending_htlcs . iter_mut ( ) {
971
- if !htlc. outbound && htlc. payment_hash == payment_hash_calc {
972
- if htlc_id != 0 {
973
- panic ! ( "Duplicate HTLC payment_hash, you probably re-used payment preimages, NEVER DO THIS!" ) ;
974
- }
975
- htlc_id = htlc. htlc_id ;
976
- htlc_amount_msat += htlc. amount_msat ;
977
- if htlc. state == HTLCState :: Committed {
978
- htlc. state = HTLCState :: LocalRemoved ;
979
- htlc. local_removed_fulfilled = true ;
980
- } else if htlc. state == HTLCState :: RemoteAnnounced {
981
- panic ! ( "Somehow forwarded HTLC prior to remote revocation!" ) ;
982
- } else if htlc. state == HTLCState :: LocalRemoved || htlc. state == HTLCState :: LocalRemovedAwaitingCommitment {
983
- return Err ( HandleError { err : "Unable to find a pending HTLC which matched the given payment preimage" , action : None } ) ;
984
- } else {
985
- panic ! ( "Have an inbound HTLC when not awaiting remote revoke that had a garbage state" ) ;
986
- }
984
+ return Ok ( ( None , Some ( self . channel_monitor . clone ( ) ) ) ) ;
985
+ }
986
+
987
+ let htlc_id = {
988
+ let mut htlc = & mut self . pending_htlcs [ pending_idx] ;
989
+ if htlc. state == HTLCState :: Committed {
990
+ htlc. state = HTLCState :: LocalRemoved ;
991
+ htlc. local_removed_fulfilled = true ;
992
+ } else if htlc. state == HTLCState :: RemoteAnnounced {
993
+ panic ! ( "Somehow forwarded HTLC prior to remote revocation!" ) ;
994
+ } else if htlc. state == HTLCState :: LocalRemoved || htlc. state == HTLCState :: LocalRemovedAwaitingCommitment {
995
+ return Err ( HandleError { err : "Unable to find a pending HTLC which matched the given payment preimage" , action : None } ) ;
996
+ } else {
997
+ panic ! ( "Have an inbound HTLC when not awaiting remote revoke that had a garbage state" ) ;
987
998
}
988
- }
989
- if htlc_amount_msat == 0 {
990
- return Err ( HandleError { err : "Unable to find a pending HTLC which matched the given payment preimage" , action : None } ) ;
991
- }
992
- self . channel_monitor . provide_payment_preimage ( & payment_hash_calc, & payment_preimage_arg) ;
999
+ htlc. htlc_id
1000
+ } ;
993
1001
994
- Ok ( Some ( ( msgs:: UpdateFulfillHTLC {
1002
+ Ok ( ( Some ( msgs:: UpdateFulfillHTLC {
995
1003
channel_id : self . channel_id ( ) ,
996
1004
htlc_id : htlc_id,
997
1005
payment_preimage : payment_preimage_arg,
998
- } , self . channel_monitor . clone ( ) ) ) )
1006
+ } ) , Some ( self . channel_monitor . clone ( ) ) ) )
999
1007
}
1000
1008
1001
- pub fn get_update_fulfill_htlc_and_commit ( & mut self , payment_preimage : [ u8 ; 32 ] ) -> Result < Option < ( msgs:: UpdateFulfillHTLC , msgs:: CommitmentSigned , ChannelMonitor ) > , HandleError > {
1009
+ pub fn get_update_fulfill_htlc_and_commit ( & mut self , payment_preimage : [ u8 ; 32 ] ) -> Result < ( Option < ( msgs:: UpdateFulfillHTLC , msgs:: CommitmentSigned ) > , Option < ChannelMonitor > ) , HandleError > {
1002
1010
match self . get_update_fulfill_htlc ( payment_preimage) ? {
1003
- Some ( update_fulfill_htlc) => {
1011
+ ( Some ( update_fulfill_htlc) , _ ) => {
1004
1012
let ( commitment, monitor_update) = self . send_commitment_no_status_check ( ) ?;
1005
- Ok ( Some ( ( update_fulfill_htlc. 0 , commitment, monitor_update) ) )
1013
+ Ok ( ( Some ( ( update_fulfill_htlc, commitment) ) , Some ( monitor_update) ) )
1006
1014
} ,
1007
- None => Ok ( None )
1015
+ ( None , Some ( channel_monitor) ) => Ok ( ( None , Some ( channel_monitor) ) ) ,
1016
+ ( None , None ) => Ok ( ( None , None ) )
1008
1017
}
1009
1018
}
1010
1019
@@ -1491,7 +1500,7 @@ impl Channel {
1491
1500
} ,
1492
1501
& HTLCUpdateAwaitingACK :: ClaimHTLC { payment_preimage, .. } => {
1493
1502
match self . get_update_fulfill_htlc ( payment_preimage) {
1494
- Ok ( update_fulfill_msg_option) => update_fulfill_htlcs. push ( update_fulfill_msg_option. unwrap ( ) . 0 ) ,
1503
+ Ok ( update_fulfill_msg_option) => update_fulfill_htlcs. push ( update_fulfill_msg_option. 0 . unwrap ( ) ) ,
1495
1504
Err ( e) => {
1496
1505
err = Some ( e) ;
1497
1506
}
@@ -1827,6 +1836,7 @@ impl Channel {
1827
1836
1828
1837
/// Guaranteed to be Some after both FundingLocked messages have been exchanged (and, thus,
1829
1838
/// is_usable() returns true).
1839
+ /// Allowed in any state (including after shutdown)
1830
1840
pub fn get_short_channel_id ( & self ) -> Option < u64 > {
1831
1841
self . short_channel_id
1832
1842
}
@@ -1837,10 +1847,12 @@ impl Channel {
1837
1847
self . channel_monitor . get_funding_txo ( )
1838
1848
}
1839
1849
1850
+ /// Allowed in any state (including after shutdown)
1840
1851
pub fn get_their_node_id ( & self ) -> PublicKey {
1841
1852
self . their_node_id
1842
1853
}
1843
1854
1855
+ /// Allowed in any state (including after shutdown)
1844
1856
pub fn get_our_htlc_minimum_msat ( & self ) -> u64 {
1845
1857
self . our_htlc_minimum_msat
1846
1858
}
@@ -1849,6 +1861,7 @@ impl Channel {
1849
1861
self . channel_value_satoshis
1850
1862
}
1851
1863
1864
+ /// Allowed in any state (including after shutdown)
1852
1865
pub fn get_channel_update_count ( & self ) -> u32 {
1853
1866
self . channel_update_count
1854
1867
}
@@ -1858,6 +1871,7 @@ impl Channel {
1858
1871
}
1859
1872
1860
1873
/// Gets the fee we'd want to charge for adding an HTLC output to this Channel
1874
+ /// Allowed in any state (including after shutdown)
1861
1875
pub fn get_our_fee_base_msat ( & self , fee_estimator : & FeeEstimator ) -> u32 {
1862
1876
// For lack of a better metric, we calculate what it would cost to consolidate the new HTLC
1863
1877
// output value back into a transaction with the regular channel output:
@@ -1877,13 +1891,15 @@ impl Channel {
1877
1891
}
1878
1892
1879
1893
/// Returns true if this channel is fully established and not known to be closing.
1894
+ /// Allowed in any state (including after shutdown)
1880
1895
pub fn is_usable ( & self ) -> bool {
1881
1896
let mask = ChannelState :: ChannelFunded as u32 | BOTH_SIDES_SHUTDOWN_MASK ;
1882
1897
( self . channel_state & mask) == ( ChannelState :: ChannelFunded as u32 )
1883
1898
}
1884
1899
1885
1900
/// Returns true if this channel is currently available for use. This is a superset of
1886
1901
/// is_usable() and considers things like the channel being temporarily disabled.
1902
+ /// Allowed in any state (including after shutdown)
1887
1903
pub fn is_live ( & self ) -> bool {
1888
1904
self . is_usable ( )
1889
1905
}
@@ -2323,14 +2339,39 @@ impl Channel {
2323
2339
}
2324
2340
2325
2341
/// Gets the latest commitment transaction and any dependant transactions for relay (forcing
2326
- /// shutdown of this channel - no more calls into this Channel may be made afterwards.
2327
- pub fn force_shutdown ( & mut self ) -> Vec < Transaction > {
2342
+ /// shutdown of this channel - no more calls into this Channel may be made afterwards except
2343
+ /// those explicitly stated to be allowed after shutdown completes, eg some simple getters).
2344
+ /// Also returns the list of payment_hashes for channels which we can safely fail backwards
2345
+ /// immediately (others we will have to allow to time out).
2346
+ pub fn force_shutdown ( & mut self ) -> ( Vec < Transaction > , Vec < [ u8 ; 32 ] > ) {
2328
2347
assert ! ( self . channel_state != ChannelState :: ShutdownComplete as u32 ) ;
2348
+
2349
+ // We go ahead and "free" any holding cell HTLCs or HTLCs we haven't yet committed to and
2350
+ // return them to fail the payment.
2351
+ let mut dropped_outbound_htlcs = Vec :: with_capacity ( self . holding_cell_htlc_updates . len ( ) ) ;
2352
+ for htlc_update in self . holding_cell_htlc_updates . drain ( ..) {
2353
+ match htlc_update {
2354
+ HTLCUpdateAwaitingACK :: AddHTLC { payment_hash, .. } => {
2355
+ dropped_outbound_htlcs. push ( payment_hash) ;
2356
+ } ,
2357
+ _ => { }
2358
+ }
2359
+ }
2360
+
2361
+ for htlc in self . pending_htlcs . drain ( ..) {
2362
+ if htlc. state == HTLCState :: LocalAnnounced {
2363
+ dropped_outbound_htlcs. push ( htlc. payment_hash ) ;
2364
+ }
2365
+ //TODO: Do something with the remaining HTLCs
2366
+ //(we need to have the ChannelManager monitor them so we can claim the inbound HTLCs
2367
+ //which correspond)
2368
+ }
2369
+
2329
2370
self . channel_state = ChannelState :: ShutdownComplete as u32 ;
2330
2371
self . channel_update_count += 1 ;
2331
2372
let mut res = Vec :: new ( ) ;
2332
2373
mem:: swap ( & mut res, & mut self . last_local_commitment_txn ) ;
2333
- res
2374
+ ( res, dropped_outbound_htlcs )
2334
2375
}
2335
2376
}
2336
2377
0 commit comments