Skip to content

Commit a82a5f7

Browse files
authored
Merge pull request #196 from yuntai/shutdown-apify
Raise APIError from close_channel
2 parents 84953fc + 18ce6c8 commit a82a5f7

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

src/ln/channel.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2879,18 +2879,23 @@ impl Channel {
28792879

28802880
/// Begins the shutdown process, getting a message for the remote peer and returning all
28812881
/// holding cell HTLCs for payment failure.
2882-
pub fn get_shutdown(&mut self) -> Result<(msgs::Shutdown, Vec<(HTLCSource, [u8; 32])>), HandleError> {
2882+
pub fn get_shutdown(&mut self) -> Result<(msgs::Shutdown, Vec<(HTLCSource, [u8; 32])>), APIError> {
28832883
for htlc in self.pending_outbound_htlcs.iter() {
28842884
if htlc.state == OutboundHTLCState::LocalAnnounced {
2885-
return Err(HandleError{err: "Cannot begin shutdown with pending HTLCs, call send_commitment first", action: None});
2885+
return Err(APIError::APIMisuseError{err: "Cannot begin shutdown with pending HTLCs. Process pending events first"});
28862886
}
28872887
}
28882888
if self.channel_state & BOTH_SIDES_SHUTDOWN_MASK != 0 {
2889-
return Err(HandleError{err: "Shutdown already in progress", action: None});
2889+
if (self.channel_state & ChannelState::LocalShutdownSent as u32) == ChannelState::LocalShutdownSent as u32 {
2890+
return Err(APIError::APIMisuseError{err: "Shutdown already in progress"});
2891+
}
2892+
else if (self.channel_state & ChannelState::RemoteShutdownSent as u32) == ChannelState::RemoteShutdownSent as u32 {
2893+
return Err(APIError::ChannelUnavailable{err: "Shutdown already in progress by remote"});
2894+
}
28902895
}
28912896
assert_eq!(self.channel_state & ChannelState::ShutdownComplete as u32, 0);
28922897
if self.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
2893-
return Err(HandleError{err: "Cannot begin shutdown while peer is disconnected, maybe force-close instead?", action: None});
2898+
return Err(APIError::ChannelUnavailable{err: "Cannot begin shutdown while peer is disconnected, maybe force-close instead?"});
28942899
}
28952900

28962901
let our_closing_script = self.get_closing_scriptpubkey();

src/ln/channelmanager.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ impl ChannelManager {
467467
/// pending HTLCs, the channel will be closed on chain.
468468
///
469469
/// May generate a SendShutdown event on success, which should be relayed.
470-
pub fn close_channel(&self, channel_id: &[u8; 32]) -> Result<(), HandleError> {
470+
pub fn close_channel(&self, channel_id: &[u8; 32]) -> Result<(), APIError> {
471471
let (mut res, node_id, chan_option) = {
472472
let mut channel_state_lock = self.channel_state.lock().unwrap();
473473
let channel_state = channel_state_lock.borrow_parts();
@@ -481,7 +481,7 @@ impl ChannelManager {
481481
(res, chan_entry.get().get_their_node_id(), Some(chan_entry.remove_entry().1))
482482
} else { (res, chan_entry.get().get_their_node_id(), None) }
483483
},
484-
hash_map::Entry::Vacant(_) => return Err(HandleError{err: "No such channel", action: None})
484+
hash_map::Entry::Vacant(_) => return Err(APIError::ChannelUnavailable{err: "No such channel"})
485485
}
486486
};
487487
for htlc_source in res.1.drain(..) {

src/util/errors.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ pub enum APIError {
2626
/// A human-readable error message
2727
err: &'static str
2828
},
29+
30+
31+
/// We were unable to complete the request since channel is disconnected or
32+
/// shutdown in progress initiated by remote
33+
ChannelUnavailable {
34+
/// A human-readable error message
35+
err: &'static str
36+
}
2937
}
3038

3139
impl fmt::Debug for APIError {
@@ -34,6 +42,7 @@ impl fmt::Debug for APIError {
3442
APIError::APIMisuseError {ref err} => f.write_str(err),
3543
APIError::FeeRateTooHigh {ref err, ref feerate} => write!(f, "{} feerate: {}", err, feerate),
3644
APIError::RouteError {ref err} => f.write_str(err),
45+
APIError::ChannelUnavailable {ref err} => f.write_str(err),
3746
}
3847
}
3948
}

0 commit comments

Comments
 (0)