Skip to content

Commit 032f202

Browse files
committed
Properly handle ChannelError::Close results in update_fee.
Best reviewed with -b
1 parent 9fdc349 commit 032f202

File tree

1 file changed

+49
-36
lines changed

1 file changed

+49
-36
lines changed

src/ln/channelmanager.rs

Lines changed: 49 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2570,49 +2570,62 @@ impl ChannelManager {
25702570
#[doc(hidden)]
25712571
pub fn update_fee(&self, channel_id: [u8;32], feerate_per_kw: u64) -> Result<(), APIError> {
25722572
let _ = self.total_consistency_lock.read().unwrap();
2573-
let mut channel_state_lock = self.channel_state.lock().unwrap();
2574-
let channel_state = channel_state_lock.borrow_parts();
2573+
let their_node_id;
2574+
let err: Result<(), _> = loop {
2575+
let mut channel_state_lock = self.channel_state.lock().unwrap();
2576+
let channel_state = channel_state_lock.borrow_parts();
25752577

2576-
match channel_state.by_id.get_mut(&channel_id) {
2577-
None => return Err(APIError::APIMisuseError{err: "Failed to find corresponding channel"}),
2578-
Some(chan) => {
2579-
if !chan.is_outbound() {
2580-
return Err(APIError::APIMisuseError{err: "update_fee cannot be sent for an inbound channel"});
2581-
}
2582-
if chan.is_awaiting_monitor_update() {
2583-
return Err(APIError::MonitorUpdateFailed);
2584-
}
2585-
if !chan.is_live() {
2586-
return Err(APIError::ChannelUnavailable{err: "Channel is either not yet fully established or peer is currently disconnected"});
2587-
}
2588-
if let Some((update_fee, commitment_signed, chan_monitor)) = chan.send_update_fee_and_commit(feerate_per_kw)
2589-
.map_err(|e| match e {
2590-
ChannelError::Ignore(err) => APIError::APIMisuseError{err},
2591-
ChannelError::Close(err) => {
2592-
// TODO: We need to close the channel here, but for that to be safe we have
2593-
// to do all channel closure inside the channel_state lock which is a
2594-
// somewhat-larger refactor, so we leave that for later.
2595-
APIError::APIMisuseError{err}
2578+
match channel_state.by_id.entry(channel_id) {
2579+
hash_map::Entry::Vacant(_) => return Err(APIError::APIMisuseError{err: "Failed to find corresponding channel"}),
2580+
hash_map::Entry::Occupied(mut chan) => {
2581+
if !chan.get().is_outbound() {
2582+
return Err(APIError::APIMisuseError{err: "update_fee cannot be sent for an inbound channel"});
2583+
}
2584+
if chan.get().is_awaiting_monitor_update() {
2585+
return Err(APIError::MonitorUpdateFailed);
2586+
}
2587+
if !chan.get().is_live() {
2588+
return Err(APIError::ChannelUnavailable{err: "Channel is either not yet fully established or peer is currently disconnected"});
2589+
}
2590+
their_node_id = chan.get().get_their_node_id();
2591+
if let Some((update_fee, commitment_signed, chan_monitor)) =
2592+
break_chan_entry!(self, chan.get_mut().send_update_fee_and_commit(feerate_per_kw), channel_state, chan)
2593+
{
2594+
if let Err(_e) = self.monitor.add_update_monitor(chan_monitor.get_funding_txo().unwrap(), chan_monitor) {
2595+
unimplemented!();
2596+
}
2597+
channel_state.pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
2598+
node_id: chan.get().get_their_node_id(),
2599+
updates: msgs::CommitmentUpdate {
2600+
update_add_htlcs: Vec::new(),
2601+
update_fulfill_htlcs: Vec::new(),
2602+
update_fail_htlcs: Vec::new(),
2603+
update_fail_malformed_htlcs: Vec::new(),
2604+
update_fee: Some(update_fee),
2605+
commitment_signed,
25962606
},
2597-
})? {
2598-
if let Err(_e) = self.monitor.add_update_monitor(chan_monitor.get_funding_txo().unwrap(), chan_monitor) {
2599-
unimplemented!();
2607+
});
26002608
}
2601-
channel_state.pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
2602-
node_id: chan.get_their_node_id(),
2603-
updates: msgs::CommitmentUpdate {
2604-
update_add_htlcs: Vec::new(),
2605-
update_fulfill_htlcs: Vec::new(),
2606-
update_fail_htlcs: Vec::new(),
2607-
update_fail_malformed_htlcs: Vec::new(),
2608-
update_fee: Some(update_fee),
2609-
commitment_signed,
2610-
},
2609+
},
2610+
}
2611+
return Ok(())
2612+
};
2613+
2614+
match handle_error!(self, err, their_node_id) {
2615+
Ok(_) => unreachable!(),
2616+
Err(e) => {
2617+
if let Some(msgs::ErrorAction::IgnoreError) = e.action {
2618+
} else {
2619+
log_error!(self, "Got bad keys: {}!", e.err);
2620+
let mut channel_state = self.channel_state.lock().unwrap();
2621+
channel_state.pending_msg_events.push(events::MessageSendEvent::HandleError {
2622+
node_id: their_node_id,
2623+
action: e.action,
26112624
});
26122625
}
2626+
Err(APIError::APIMisuseError { err: e.err })
26132627
},
26142628
}
2615-
Ok(())
26162629
}
26172630
}
26182631

0 commit comments

Comments
 (0)