@@ -4026,7 +4026,6 @@ where
4026
4026
4027
4027
let per_peer_state = self . per_peer_state . read ( ) . unwrap ( ) ;
4028
4028
let chan_id = prev_hop. outpoint . to_channel_id ( ) ;
4029
-
4030
4029
let counterparty_node_id_opt = match self . short_to_chan_info . read ( ) . unwrap ( ) . get ( & prev_hop. short_channel_id ) {
4031
4030
Some ( ( cp_id, _dup_chan_id) ) => Some ( cp_id. clone ( ) ) ,
4032
4031
None => None
@@ -4038,99 +4037,57 @@ where
4038
4037
)
4039
4038
) . unwrap_or ( None ) ;
4040
4039
4041
- if let Some ( hash_map:: Entry :: Occupied ( mut chan) ) = peer_state_opt. as_mut ( ) . map ( |peer_state| peer_state. channel_by_id . entry ( chan_id) )
4042
- {
4043
- let counterparty_node_id = chan. get ( ) . get_counterparty_node_id ( ) ;
4044
- match chan. get_mut ( ) . get_update_fulfill_htlc_and_commit ( prev_hop. htlc_id , payment_preimage, & self . logger ) {
4045
- Ok ( msgs_monitor_option) => {
4046
- if let UpdateFulfillCommitFetch :: NewClaim { msgs, htlc_value_msat, monitor_update } = msgs_monitor_option {
4047
- match self . chain_monitor . update_channel ( chan. get ( ) . get_funding_txo ( ) . unwrap ( ) , & monitor_update) {
4048
- ChannelMonitorUpdateStatus :: Completed => { } ,
4049
- e => {
4050
- log_given_level ! ( self . logger, if e == ChannelMonitorUpdateStatus :: PermanentFailure { Level :: Error } else { Level :: Debug } ,
4051
- "Failed to update channel monitor with preimage {:?}: {:?}" ,
4052
- payment_preimage, e) ;
4053
- let err = handle_monitor_update_res ! ( self , e, chan, RAACommitmentOrder :: CommitmentFirst , false , msgs. is_some( ) ) . unwrap_err ( ) ;
4054
- mem:: drop ( peer_state_opt) ;
4055
- mem:: drop ( per_peer_state) ;
4056
- self . handle_monitor_update_completion_actions ( completion_action ( Some ( htlc_value_msat) ) ) ;
4057
- return Err ( ( counterparty_node_id, err) ) ;
4058
- }
4059
- }
4060
- if let Some ( ( msg, commitment_signed) ) = msgs {
4061
- log_debug ! ( self . logger, "Claiming funds for HTLC with preimage {} resulted in a commitment_signed for channel {}" ,
4062
- log_bytes!( payment_preimage. 0 ) , log_bytes!( chan. get( ) . channel_id( ) ) ) ;
4063
- peer_state_opt. as_mut ( ) . unwrap ( ) . pending_msg_events . push ( events:: MessageSendEvent :: UpdateHTLCs {
4064
- node_id : counterparty_node_id,
4065
- updates : msgs:: CommitmentUpdate {
4066
- update_add_htlcs : Vec :: new ( ) ,
4067
- update_fulfill_htlcs : vec ! [ msg] ,
4068
- update_fail_htlcs : Vec :: new ( ) ,
4069
- update_fail_malformed_htlcs : Vec :: new ( ) ,
4070
- update_fee : None ,
4071
- commitment_signed,
4072
- }
4073
- } ) ;
4074
- }
4075
- mem:: drop ( peer_state_opt) ;
4076
- mem:: drop ( per_peer_state) ;
4077
- self . handle_monitor_update_completion_actions ( completion_action ( Some ( htlc_value_msat) ) ) ;
4078
- Ok ( ( ) )
4079
- } else {
4080
- Ok ( ( ) )
4081
- }
4082
- } ,
4083
- Err ( ( e, monitor_update) ) => {
4084
- match self . chain_monitor . update_channel ( chan. get ( ) . get_funding_txo ( ) . unwrap ( ) , & monitor_update) {
4085
- ChannelMonitorUpdateStatus :: Completed => { } ,
4086
- e => {
4087
- // TODO: This needs to be handled somehow - if we receive a monitor update
4088
- // with a preimage we *must* somehow manage to propagate it to the upstream
4089
- // channel, or we must have an ability to receive the same update and try
4090
- // again on restart.
4091
- log_given_level ! ( self . logger, if e == ChannelMonitorUpdateStatus :: PermanentFailure { Level :: Error } else { Level :: Info } ,
4092
- "Failed to update channel monitor with preimage {:?} immediately prior to force-close: {:?}" ,
4093
- payment_preimage, e) ;
4094
- } ,
4040
+ if let Some ( mut peer_state_lock) = peer_state_opt. take ( ) {
4041
+ let peer_state = & mut * peer_state_lock;
4042
+ if let hash_map:: Entry :: Occupied ( mut chan) = peer_state. channel_by_id . entry ( chan_id) {
4043
+ let counterparty_node_id = chan. get ( ) . get_counterparty_node_id ( ) ;
4044
+ let fulfill_res = chan. get_mut ( ) . get_update_fulfill_htlc_and_commit ( prev_hop. htlc_id , payment_preimage, & self . logger ) ;
4045
+
4046
+ if let UpdateFulfillCommitFetch :: NewClaim { htlc_value_msat, monitor_update } = fulfill_res {
4047
+ if let Some ( action) = completion_action ( Some ( htlc_value_msat) ) {
4048
+ log_trace ! ( self . logger, "Tracking monitor update completion action for channel {}: {:?}" ,
4049
+ log_bytes!( chan_id) , action) ;
4050
+ peer_state. monitor_update_blocked_actions . entry ( chan_id) . or_insert ( Vec :: new ( ) ) . push ( action) ;
4095
4051
}
4096
- let ( drop, res) = convert_chan_err ! ( self , e, chan. get_mut( ) , & chan_id) ;
4097
- if drop {
4098
- chan. remove_entry ( ) ;
4052
+ let update_id = monitor_update. update_id ;
4053
+ let update_res = self . chain_monitor . update_channel ( prev_hop. outpoint , monitor_update) ;
4054
+ let res = handle_new_monitor_update ! ( self , update_res, update_id, peer_state_lock,
4055
+ peer_state, chan) ;
4056
+ if let Err ( e) = res {
4057
+ // TODO: This is a *critical* error - we probably updated the outbound edge
4058
+ // of the HTLC's monitor with a preimage. We should retry this monitor
4059
+ // update over and over again until morale improves.
4060
+ log_error ! ( self . logger, "Failed to update channel monitor with preimage {:?}" , payment_preimage) ;
4061
+ return Err ( ( counterparty_node_id, e) ) ;
4099
4062
}
4100
- mem:: drop ( peer_state_opt) ;
4101
- mem:: drop ( per_peer_state) ;
4102
- self . handle_monitor_update_completion_actions ( completion_action ( None ) ) ;
4103
- Err ( ( counterparty_node_id, res) )
4104
- } ,
4105
- }
4106
- } else {
4107
- let preimage_update = ChannelMonitorUpdate {
4108
- update_id : CLOSED_CHANNEL_UPDATE_ID ,
4109
- updates : vec ! [ ChannelMonitorUpdateStep :: PaymentPreimage {
4110
- payment_preimage,
4111
- } ] ,
4112
- } ;
4113
- // We update the ChannelMonitor on the backward link, after
4114
- // receiving an `update_fulfill_htlc` from the forward link.
4115
- let update_res = self . chain_monitor . update_channel ( prev_hop. outpoint , & preimage_update) ;
4116
- if update_res != ChannelMonitorUpdateStatus :: Completed {
4117
- // TODO: This needs to be handled somehow - if we receive a monitor update
4118
- // with a preimage we *must* somehow manage to propagate it to the upstream
4119
- // channel, or we must have an ability to receive the same event and try
4120
- // again on restart.
4121
- log_error ! ( self . logger, "Critical error: failed to update channel monitor with preimage {:?}: {:?}" ,
4122
- payment_preimage, update_res) ;
4063
+ }
4064
+ return Ok ( ( ) ) ;
4123
4065
}
4124
- mem:: drop ( peer_state_opt) ;
4125
- mem:: drop ( per_peer_state) ;
4126
- // Note that we do process the completion action here. This totally could be a
4127
- // duplicate claim, but we have no way of knowing without interrogating the
4128
- // `ChannelMonitor` we've provided the above update to. Instead, note that `Event`s are
4129
- // generally always allowed to be duplicative (and it's specifically noted in
4130
- // `PaymentForwarded`).
4131
- self . handle_monitor_update_completion_actions ( completion_action ( None ) ) ;
4132
- Ok ( ( ) )
4133
4066
}
4067
+ let preimage_update = ChannelMonitorUpdate {
4068
+ update_id : CLOSED_CHANNEL_UPDATE_ID ,
4069
+ updates : vec ! [ ChannelMonitorUpdateStep :: PaymentPreimage {
4070
+ payment_preimage,
4071
+ } ] ,
4072
+ } ;
4073
+ // We update the ChannelMonitor on the backward link, after
4074
+ // receiving an `update_fulfill_htlc` from the forward link.
4075
+ let update_res = self . chain_monitor . update_channel ( prev_hop. outpoint , & preimage_update) ;
4076
+ if update_res != ChannelMonitorUpdateStatus :: Completed {
4077
+ // TODO: This needs to be handled somehow - if we receive a monitor update
4078
+ // with a preimage we *must* somehow manage to propagate it to the upstream
4079
+ // channel, or we must have an ability to receive the same event and try
4080
+ // again on restart.
4081
+ log_error ! ( self . logger, "Critical error: failed to update channel monitor with preimage {:?}: {:?}" ,
4082
+ payment_preimage, update_res) ;
4083
+ }
4084
+ // Note that we do process the completion action here. This totally could be a
4085
+ // duplicate claim, but we have no way of knowing without interrogating the
4086
+ // `ChannelMonitor` we've provided the above update to. Instead, note that `Event`s are
4087
+ // generally always allowed to be duplicative (and it's specifically noted in
4088
+ // `PaymentForwarded`).
4089
+ self . handle_monitor_update_completion_actions ( completion_action ( None ) ) ;
4090
+ Ok ( ( ) )
4134
4091
}
4135
4092
4136
4093
fn finalize_claims ( & self , sources : Vec < HTLCSource > ) {
@@ -7044,8 +7001,6 @@ where
7044
7001
// LDK versions prior to 0.0.113 do not know how to read the pending claimed payments
7045
7002
// map. Thus, if there are no entries we skip writing a TLV for it.
7046
7003
pending_claiming_payments = None ;
7047
- } else {
7048
- debug_assert ! ( false , "While we have code to serialize pending_claiming_payments, the map should always be empty until a later PR" ) ;
7049
7004
}
7050
7005
7051
7006
write_tlv_fields ! ( writer, {
0 commit comments