@@ -26,11 +26,10 @@ use crypto::digest::Digest;
26
26
use crypto:: symmetriccipher:: SynchronousStreamCipher ;
27
27
use crypto:: chacha20:: ChaCha20 ;
28
28
29
- use std:: sync:: { Mutex , Arc } ;
29
+ use std:: sync:: { Mutex , MutexGuard , Arc } ;
30
30
use std:: collections:: HashMap ;
31
31
use std:: collections:: hash_map;
32
- use std:: ptr;
33
- use std:: mem;
32
+ use std:: { ptr, mem} ;
34
33
use std:: time:: { Instant , Duration } ;
35
34
36
35
/// Stores the info we will need to send when we want to forward an HTLC onwards
@@ -651,11 +650,10 @@ impl ChannelManager {
651
650
652
651
/// Indicates that the preimage for payment_hash is unknown after a PaymentReceived event.
653
652
pub fn fail_htlc_backwards ( & self , payment_hash : & [ u8 ; 32 ] ) -> bool {
654
- self . fail_htlc_backwards_internal ( payment_hash, HTLCFailReason :: Reason { failure_code : 0x4000 | 15 } )
653
+ self . fail_htlc_backwards_internal ( self . channel_state . lock ( ) . unwrap ( ) , payment_hash, HTLCFailReason :: Reason { failure_code : 0x4000 | 15 } )
655
654
}
656
655
657
- fn fail_htlc_backwards_internal ( & self , payment_hash : & [ u8 ; 32 ] , onion_error : HTLCFailReason ) -> bool {
658
- let mut channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
656
+ fn fail_htlc_backwards_internal ( & self , mut channel_state : MutexGuard < ChannelHolder > , payment_hash : & [ u8 ; 32 ] , onion_error : HTLCFailReason ) -> bool {
659
657
let mut pending_htlc = {
660
658
match channel_state. claimable_htlcs . remove ( payment_hash) {
661
659
Some ( pending_htlc) => pending_htlc,
@@ -674,6 +672,7 @@ impl ChannelManager {
674
672
PendingOutboundHTLC :: CycledRoute { .. } => { panic ! ( "WAT" ) ; } ,
675
673
PendingOutboundHTLC :: OutboundRoute { .. } => {
676
674
//TODO: DECRYPT route from OutboundRoute
675
+ mem:: drop ( channel_state) ;
677
676
let mut pending_events = self . pending_events . lock ( ) . unwrap ( ) ;
678
677
pending_events. push ( events:: Event :: PaymentFailed {
679
678
payment_hash : payment_hash. clone ( )
@@ -707,6 +706,7 @@ impl ChannelManager {
707
706
}
708
707
} ;
709
708
709
+ mem:: drop ( channel_state) ;
710
710
let mut pending_events = self . pending_events . lock ( ) . unwrap ( ) ;
711
711
pending_events. push ( events:: Event :: SendFailHTLC {
712
712
node_id,
@@ -1217,36 +1217,34 @@ impl ChannelMessageHandler for ChannelManager {
1217
1217
}
1218
1218
1219
1219
fn handle_update_fail_htlc ( & self , their_node_id : & PublicKey , msg : & msgs:: UpdateFailHTLC ) -> Result < Option < ( Vec < msgs:: UpdateAddHTLC > , msgs:: CommitmentSigned ) > , HandleError > {
1220
- let res = {
1221
- let mut channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
1222
- match channel_state. by_id . get_mut ( & msg. channel_id ) {
1223
- Some ( chan) => {
1224
- if chan. get_their_node_id ( ) != * their_node_id {
1225
- return Err ( HandleError { err : "Got a message for a channel from the wrong node!" , msg : None } )
1226
- }
1227
- chan. update_fail_htlc ( & msg) ?
1228
- } ,
1229
- None => return Err ( HandleError { err : "Failed to find corresponding channel" , msg : None } )
1230
- }
1231
- } ;
1232
- self . fail_htlc_backwards_internal ( & res. 0 , HTLCFailReason :: ErrorPacket { err : & msg. reason } ) ;
1220
+ let mut channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
1221
+ let res;
1222
+ match channel_state. by_id . get_mut ( & msg. channel_id ) {
1223
+ Some ( chan) => {
1224
+ if chan. get_their_node_id ( ) != * their_node_id {
1225
+ return Err ( HandleError { err : "Got a message for a channel from the wrong node!" , msg : None } )
1226
+ }
1227
+ res = chan. update_fail_htlc ( & msg) ?;
1228
+ } ,
1229
+ None => return Err ( HandleError { err : "Failed to find corresponding channel" , msg : None } )
1230
+ }
1231
+ self . fail_htlc_backwards_internal ( channel_state, & res. 0 , HTLCFailReason :: ErrorPacket { err : & msg. reason } ) ;
1233
1232
Ok ( res. 1 )
1234
1233
}
1235
1234
1236
1235
fn handle_update_fail_malformed_htlc ( & self , their_node_id : & PublicKey , msg : & msgs:: UpdateFailMalformedHTLC ) -> Result < Option < ( Vec < msgs:: UpdateAddHTLC > , msgs:: CommitmentSigned ) > , HandleError > {
1237
- let res = {
1238
- let mut channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
1239
- match channel_state. by_id . get_mut ( & msg. channel_id ) {
1240
- Some ( chan) => {
1241
- if chan. get_their_node_id ( ) != * their_node_id {
1242
- return Err ( HandleError { err : "Got a message for a channel from the wrong node!" , msg : None } )
1243
- }
1244
- chan. update_fail_malformed_htlc ( & msg) ?
1245
- } ,
1246
- None => return Err ( HandleError { err : "Failed to find corresponding channel" , msg : None } )
1247
- }
1248
- } ;
1249
- self . fail_htlc_backwards_internal ( & res. 0 , HTLCFailReason :: Reason { failure_code : msg. failure_code } ) ;
1236
+ let mut channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
1237
+ let res;
1238
+ match channel_state. by_id . get_mut ( & msg. channel_id ) {
1239
+ Some ( chan) => {
1240
+ if chan. get_their_node_id ( ) != * their_node_id {
1241
+ return Err ( HandleError { err : "Got a message for a channel from the wrong node!" , msg : None } )
1242
+ }
1243
+ res = chan. update_fail_malformed_htlc ( & msg) ?;
1244
+ } ,
1245
+ None => return Err ( HandleError { err : "Failed to find corresponding channel" , msg : None } )
1246
+ }
1247
+ self . fail_htlc_backwards_internal ( channel_state, & res. 0 , HTLCFailReason :: Reason { failure_code : msg. failure_code } ) ;
1250
1248
Ok ( res. 1 )
1251
1249
}
1252
1250
0 commit comments