@@ -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
}
0 commit comments