@@ -25,7 +25,7 @@ use chain::transaction::OutPoint;
25
25
use chain:: keysinterface:: { ChannelKeys , KeysInterface } ;
26
26
use util:: transaction_utils;
27
27
use util:: ser:: { Readable , ReadableArgs , Writeable , Writer , WriterWriteAdaptor } ;
28
- use util:: logger:: Logger ;
28
+ use util:: logger:: { Logger , LogHolder } ;
29
29
use util:: errors:: APIError ;
30
30
use util:: config:: { UserConfig , ChannelConfig } ;
31
31
@@ -1992,74 +1992,89 @@ impl Channel {
1992
1992
self . monitor_pending_order = None ;
1993
1993
}
1994
1994
1995
+ log_trace ! ( self , "Updating HTLCs on receipt of RAA..." ) ;
1995
1996
let mut to_forward_infos = Vec :: new ( ) ;
1996
1997
let mut revoked_htlcs = Vec :: new ( ) ;
1997
1998
let mut update_fail_htlcs = Vec :: new ( ) ;
1998
1999
let mut update_fail_malformed_htlcs = Vec :: new ( ) ;
1999
2000
let mut require_commitment = false ;
2000
2001
let mut value_to_self_msat_diff: i64 = 0 ;
2001
- // We really shouldnt have two passes here, but retain gives a non-mutable ref (Rust bug)
2002
- self . pending_inbound_htlcs . retain ( |htlc| {
2003
- if let & InboundHTLCState :: LocalRemoved ( ref reason) = & htlc. state {
2004
- if let & InboundHTLCRemovalReason :: Fulfill ( _) = reason {
2005
- value_to_self_msat_diff += htlc. amount_msat as i64 ;
2006
- }
2007
- false
2008
- } else { true }
2009
- } ) ;
2010
- self . pending_outbound_htlcs . retain ( |htlc| {
2011
- if let OutboundHTLCState :: AwaitingRemovedRemoteRevoke = htlc. state {
2012
- if let Some ( reason) = htlc. fail_reason . clone ( ) { // We really want take() here, but, again, non-mut ref :(
2013
- revoked_htlcs. push ( ( htlc. source . clone ( ) , htlc. payment_hash , reason) ) ;
2014
- } else {
2015
- // They fulfilled, so we sent them money
2016
- value_to_self_msat_diff -= htlc. amount_msat as i64 ;
2017
- }
2018
- false
2019
- } else { true }
2020
- } ) ;
2021
- for htlc in self . pending_inbound_htlcs . iter_mut ( ) {
2022
- let swap = if let & InboundHTLCState :: AwaitingRemoteRevokeToAnnounce ( _) = & htlc. state {
2023
- true
2024
- } else if let & InboundHTLCState :: AwaitingAnnouncedRemoteRevoke ( _) = & htlc. state {
2025
- true
2026
- } else { false } ;
2027
- if swap {
2028
- let mut state = InboundHTLCState :: Committed ;
2029
- mem:: swap ( & mut state, & mut htlc. state ) ;
2030
-
2031
- if let InboundHTLCState :: AwaitingRemoteRevokeToAnnounce ( forward_info) = state {
2032
- htlc. state = InboundHTLCState :: AwaitingAnnouncedRemoteRevoke ( forward_info) ;
2033
- require_commitment = true ;
2034
- } else if let InboundHTLCState :: AwaitingAnnouncedRemoteRevoke ( forward_info) = state {
2035
- match forward_info {
2036
- PendingHTLCStatus :: Fail ( fail_msg) => {
2037
- require_commitment = true ;
2038
- match fail_msg {
2039
- HTLCFailureMsg :: Relay ( msg) => {
2040
- htlc. state = InboundHTLCState :: LocalRemoved ( InboundHTLCRemovalReason :: FailRelay ( msg. reason . clone ( ) ) ) ;
2041
- update_fail_htlcs. push ( msg)
2042
- } ,
2043
- HTLCFailureMsg :: Malformed ( msg) => {
2044
- htlc. state = InboundHTLCState :: LocalRemoved ( InboundHTLCRemovalReason :: FailMalformed ( ( msg. sha256_of_onion , msg. failure_code ) ) ) ;
2045
- update_fail_malformed_htlcs. push ( msg)
2046
- } ,
2002
+
2003
+ {
2004
+ // Take references explicitly so that we can hold multiple references to self.
2005
+ let pending_inbound_htlcs: & mut Vec < _ > = & mut self . pending_inbound_htlcs ;
2006
+ let pending_outbound_htlcs: & mut Vec < _ > = & mut self . pending_outbound_htlcs ;
2007
+ let logger = LogHolder { logger : & self . logger } ;
2008
+
2009
+ // We really shouldnt have two passes here, but retain gives a non-mutable ref (Rust bug)
2010
+ pending_inbound_htlcs. retain ( |htlc| {
2011
+ if let & InboundHTLCState :: LocalRemoved ( ref reason) = & htlc. state {
2012
+ log_trace ! ( logger, " ...removing inbound LocalRemoved {}" , log_bytes!( htlc. payment_hash. 0 ) ) ;
2013
+ if let & InboundHTLCRemovalReason :: Fulfill ( _) = reason {
2014
+ value_to_self_msat_diff += htlc. amount_msat as i64 ;
2015
+ }
2016
+ false
2017
+ } else { true }
2018
+ } ) ;
2019
+ pending_outbound_htlcs. retain ( |htlc| {
2020
+ if let OutboundHTLCState :: AwaitingRemovedRemoteRevoke = htlc. state {
2021
+ log_trace ! ( logger, " ...removing outbound AwaitingRemovedRemoteRevoke {}" , log_bytes!( htlc. payment_hash. 0 ) ) ;
2022
+ if let Some ( reason) = htlc. fail_reason . clone ( ) { // We really want take() here, but, again, non-mut ref :(
2023
+ revoked_htlcs. push ( ( htlc. source . clone ( ) , htlc. payment_hash , reason) ) ;
2024
+ } else {
2025
+ // They fulfilled, so we sent them money
2026
+ value_to_self_msat_diff -= htlc. amount_msat as i64 ;
2027
+ }
2028
+ false
2029
+ } else { true }
2030
+ } ) ;
2031
+ for htlc in pending_inbound_htlcs. iter_mut ( ) {
2032
+ let swap = if let & InboundHTLCState :: AwaitingRemoteRevokeToAnnounce ( _) = & htlc. state {
2033
+ log_trace ! ( logger, " ...promoting inbound AwaitingRemoteRevokeToAnnounce {} to Committed" , log_bytes!( htlc. payment_hash. 0 ) ) ;
2034
+ true
2035
+ } else if let & InboundHTLCState :: AwaitingAnnouncedRemoteRevoke ( _) = & htlc. state {
2036
+ log_trace ! ( logger, " ...promoting inbound AwaitingAnnouncedRemoteRevoke {} to Committed" , log_bytes!( htlc. payment_hash. 0 ) ) ;
2037
+ true
2038
+ } else { false } ;
2039
+ if swap {
2040
+ let mut state = InboundHTLCState :: Committed ;
2041
+ mem:: swap ( & mut state, & mut htlc. state ) ;
2042
+
2043
+ if let InboundHTLCState :: AwaitingRemoteRevokeToAnnounce ( forward_info) = state {
2044
+ htlc. state = InboundHTLCState :: AwaitingAnnouncedRemoteRevoke ( forward_info) ;
2045
+ require_commitment = true ;
2046
+ } else if let InboundHTLCState :: AwaitingAnnouncedRemoteRevoke ( forward_info) = state {
2047
+ match forward_info {
2048
+ PendingHTLCStatus :: Fail ( fail_msg) => {
2049
+ require_commitment = true ;
2050
+ match fail_msg {
2051
+ HTLCFailureMsg :: Relay ( msg) => {
2052
+ htlc. state = InboundHTLCState :: LocalRemoved ( InboundHTLCRemovalReason :: FailRelay ( msg. reason . clone ( ) ) ) ;
2053
+ update_fail_htlcs. push ( msg)
2054
+ } ,
2055
+ HTLCFailureMsg :: Malformed ( msg) => {
2056
+ htlc. state = InboundHTLCState :: LocalRemoved ( InboundHTLCRemovalReason :: FailMalformed ( ( msg. sha256_of_onion , msg. failure_code ) ) ) ;
2057
+ update_fail_malformed_htlcs. push ( msg)
2058
+ } ,
2059
+ }
2060
+ } ,
2061
+ PendingHTLCStatus :: Forward ( forward_info) => {
2062
+ to_forward_infos. push ( ( forward_info, htlc. htlc_id ) ) ;
2063
+ htlc. state = InboundHTLCState :: Committed ;
2047
2064
}
2048
- } ,
2049
- PendingHTLCStatus :: Forward ( forward_info) => {
2050
- to_forward_infos. push ( ( forward_info, htlc. htlc_id ) ) ;
2051
- htlc. state = InboundHTLCState :: Committed ;
2052
2065
}
2053
2066
}
2054
2067
}
2055
2068
}
2056
- }
2057
- for htlc in self . pending_outbound_htlcs . iter_mut ( ) {
2058
- if let OutboundHTLCState :: LocalAnnounced ( _) = htlc. state {
2059
- htlc. state = OutboundHTLCState :: Committed ;
2060
- } else if let OutboundHTLCState :: AwaitingRemoteRevokeToRemove = htlc. state {
2061
- htlc. state = OutboundHTLCState :: AwaitingRemovedRemoteRevoke ;
2062
- require_commitment = true ;
2069
+ for htlc in pending_outbound_htlcs. iter_mut ( ) {
2070
+ if let OutboundHTLCState :: LocalAnnounced ( _) = htlc. state {
2071
+ log_trace ! ( logger, " ...promoting outbound LocalAnnounced {} to Committed" , log_bytes!( htlc. payment_hash. 0 ) ) ;
2072
+ htlc. state = OutboundHTLCState :: Committed ;
2073
+ } else if let OutboundHTLCState :: AwaitingRemoteRevokeToRemove = htlc. state {
2074
+ log_trace ! ( logger, " ...promoting outbound AwaitingRemoteRevokeToRemove {} to AwaitingRemovedRemoteRevoke" , log_bytes!( htlc. payment_hash. 0 ) ) ;
2075
+ htlc. state = OutboundHTLCState :: AwaitingRemovedRemoteRevoke ;
2076
+ require_commitment = true ;
2077
+ }
2063
2078
}
2064
2079
}
2065
2080
self . value_to_self_msat = ( self . value_to_self_msat as i64 + value_to_self_msat_diff) as u64 ;
0 commit comments