Skip to content

Commit 0baf72b

Browse files
authored
Merge pull request #211 from TheBlueMatt/2018-10-misc-cleanups
Miscellaneous Cleanups
2 parents 4cb059e + da70db2 commit 0baf72b

File tree

3 files changed

+35
-21
lines changed

3 files changed

+35
-21
lines changed

src/ln/channel.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1960,10 +1960,12 @@ impl Channel {
19601960
if !self.channel_outbound {
19611961
panic!("Cannot send fee from inbound channel");
19621962
}
1963-
19641963
if !self.is_usable() {
19651964
panic!("Cannot update fee until channel is fully established and we haven't started shutting down");
19661965
}
1966+
if !self.is_live() {
1967+
panic!("Cannot update fee while peer is disconnected (ChannelManager should have caught this)");
1968+
}
19671969

19681970
if (self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32)) == (ChannelState::AwaitingRemoteRevoke as u32) {
19691971
self.holding_cell_update_fee = Some(feerate_per_kw);

src/ln/channelmanager.rs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,10 @@ impl ChannelManager {
449449
let channel_state = self.channel_state.lock().unwrap();
450450
let mut res = Vec::with_capacity(channel_state.by_id.len());
451451
for (channel_id, channel) in channel_state.by_id.iter() {
452-
if channel.is_usable() {
452+
// Note we use is_live here instead of usable which leads to somewhat confused
453+
// internal/external nomenclature, but that's ok cause that's probably what the user
454+
// really wanted anyway.
455+
if channel.is_live() {
453456
res.push(ChannelDetails {
454457
channel_id: (*channel_id).clone(),
455458
short_channel_id: channel.get_short_channel_id(),
@@ -997,7 +1000,7 @@ impl ChannelManager {
9971000
};
9981001

9991002
let msg_hash = Sha256dHash::from_data(&unsigned.encode()[..]);
1000-
let sig = self.secp_ctx.sign(&Message::from_slice(&msg_hash[..]).unwrap(), &self.our_network_key); //TODO Can we unwrap here?
1003+
let sig = self.secp_ctx.sign(&Message::from_slice(&msg_hash[..]).unwrap(), &self.our_network_key);
10011004

10021005
Ok(msgs::ChannelUpdate {
10031006
signature: sig,
@@ -1050,7 +1053,7 @@ impl ChannelManager {
10501053
let channel_state = channel_state_lock.borrow_parts();
10511054

10521055
let id = match channel_state.short_to_id.get(&route.hops.first().unwrap().short_channel_id) {
1053-
None => return Err(APIError::RouteError{err: "No channel available with first hop!"}),
1056+
None => return Err(APIError::ChannelUnavailable{err: "No channel available with first hop!"}),
10541057
Some(id) => id.clone(),
10551058
};
10561059

@@ -1060,12 +1063,12 @@ impl ChannelManager {
10601063
return Err(APIError::RouteError{err: "Node ID mismatch on first hop!"});
10611064
}
10621065
if !chan.is_live() {
1063-
return Err(APIError::RouteError{err: "Peer for first hop currently disconnected!"});
1066+
return Err(APIError::ChannelUnavailable{err: "Peer for first hop currently disconnected!"});
10641067
}
10651068
chan.send_htlc_and_commit(htlc_msat, payment_hash.clone(), htlc_cltv, HTLCSource::OutboundRoute {
10661069
route: route.clone(),
10671070
session_priv: session_priv.clone(),
1068-
}, onion_packet).map_err(|he| APIError::RouteError{err: he.err})?
1071+
}, onion_packet).map_err(|he| APIError::ChannelUnavailable{err: he.err})?
10691072
};
10701073

10711074
let first_hop_node_id = route.hops.first().unwrap().pubkey;
@@ -1102,7 +1105,6 @@ impl ChannelManager {
11021105
/// May panic if the funding_txo is duplicative with some other channel (note that this should
11031106
/// be trivially prevented by using unique funding transaction keys per-channel).
11041107
pub fn funding_transaction_generated(&self, temporary_channel_id: &[u8; 32], funding_txo: OutPoint) {
1105-
11061108
macro_rules! add_pending_event {
11071109
($event: expr) => {
11081110
{
@@ -1998,12 +2000,12 @@ impl ChannelManager {
19982000
match channel_state.by_id.get_mut(&channel_id) {
19992001
None => return Err(APIError::APIMisuseError{err: "Failed to find corresponding channel"}),
20002002
Some(chan) => {
2001-
if !chan.is_usable() {
2002-
return Err(APIError::APIMisuseError{err: "Channel is not in usuable state"});
2003-
}
20042003
if !chan.is_outbound() {
20052004
return Err(APIError::APIMisuseError{err: "update_fee cannot be sent for an inbound channel"});
20062005
}
2006+
if !chan.is_live() {
2007+
return Err(APIError::ChannelUnavailable{err: "Channel is either not yet fully established or peer is currently disconnected"});
2008+
}
20072009
if let Some((update_fee, commitment_signed, chan_monitor)) = chan.send_update_fee_and_commit(feerate_per_kw).map_err(|e| APIError::APIMisuseError{err: e.err})? {
20082010
if let Err(_e) = self.monitor.add_update_monitor(chan_monitor.get_funding_txo().unwrap(), chan_monitor) {
20092011
unimplemented!();
@@ -3025,7 +3027,7 @@ mod tests {
30253027

30263028
let err = origin_node.node.send_payment(route, our_payment_hash).err().unwrap();
30273029
match err {
3028-
APIError::RouteError{err} => assert_eq!(err, "Cannot send value that would put us over our max HTLC value in flight"),
3030+
APIError::ChannelUnavailable{err} => assert_eq!(err, "Cannot send value that would put us over our max HTLC value in flight"),
30293031
_ => panic!("Unknown error variants"),
30303032
};
30313033
}
@@ -3989,7 +3991,7 @@ mod tests {
39893991
assert!(route.hops.iter().rev().skip(1).all(|h| h.fee_msat == feemsat));
39903992
let err = nodes[0].node.send_payment(route, our_payment_hash).err().unwrap();
39913993
match err {
3992-
APIError::RouteError{err} => assert_eq!(err, "Cannot send value that would put us over our max HTLC value in flight"),
3994+
APIError::ChannelUnavailable{err} => assert_eq!(err, "Cannot send value that would put us over our max HTLC value in flight"),
39933995
_ => panic!("Unknown error variants"),
39943996
}
39953997
}
@@ -4025,7 +4027,7 @@ mod tests {
40254027
let (route, our_payment_hash, _) = get_route_and_payment_hash!(recv_value + 1);
40264028
let err = nodes[0].node.send_payment(route.clone(), our_payment_hash).err().unwrap();
40274029
match err {
4028-
APIError::RouteError{err} => assert_eq!(err, "Cannot send value that would put us over our reserve value"),
4030+
APIError::ChannelUnavailable{err} => assert_eq!(err, "Cannot send value that would put us over our reserve value"),
40294031
_ => panic!("Unknown error variants"),
40304032
}
40314033
}
@@ -4050,7 +4052,7 @@ mod tests {
40504052
{
40514053
let (route, our_payment_hash, _) = get_route_and_payment_hash!(recv_value_2 + 1);
40524054
match nodes[0].node.send_payment(route, our_payment_hash).err().unwrap() {
4053-
APIError::RouteError{err} => assert_eq!(err, "Cannot send value that would put us over our reserve value"),
4055+
APIError::ChannelUnavailable{err} => assert_eq!(err, "Cannot send value that would put us over our reserve value"),
40544056
_ => panic!("Unknown error variants"),
40554057
}
40564058
}
@@ -4106,7 +4108,7 @@ mod tests {
41064108
{
41074109
let (route, our_payment_hash, _) = get_route_and_payment_hash!(recv_value_22+1);
41084110
match nodes[0].node.send_payment(route, our_payment_hash).err().unwrap() {
4109-
APIError::RouteError{err} => assert_eq!(err, "Cannot send value that would put us over our reserve value"),
4111+
APIError::ChannelUnavailable{err} => assert_eq!(err, "Cannot send value that would put us over our reserve value"),
41104112
_ => panic!("Unknown error variants"),
41114113
}
41124114
}
@@ -4935,6 +4937,10 @@ mod tests {
49354937
_ => panic!("Unexpected event"),
49364938
};
49374939

4940+
nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
4941+
nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
4942+
reconnect_nodes(&nodes[0], &nodes[1], false, (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
4943+
49384944
nodes[1].node.channel_state.lock().unwrap().next_forward = Instant::now();
49394945
nodes[1].node.process_pending_htlc_forwards();
49404946

@@ -5029,6 +5035,10 @@ mod tests {
50295035
reconnect_nodes(&nodes[0], &nodes[1], false, (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
50305036
}
50315037

5038+
nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
5039+
nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
5040+
reconnect_nodes(&nodes[0], &nodes[1], false, (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
5041+
50325042
// Channel should still work fine...
50335043
let payment_preimage_2 = send_along_route(&nodes[0], route, &[&nodes[1]], 1000000).0;
50345044
claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_2);
@@ -5079,6 +5089,9 @@ mod tests {
50795089
_ => panic!("Unexpected event"),
50805090
}
50815091

5092+
reconnect_nodes(&nodes[0], &nodes[1], true, (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
5093+
nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
5094+
nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
50825095
reconnect_nodes(&nodes[0], &nodes[1], true, (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
50835096

50845097
// TODO: We shouldn't need to manually pass list_usable_chanels here once we support

src/util/errors.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,15 @@ pub enum APIError {
2020
/// The feerate which was too high.
2121
feerate: u64
2222
},
23-
24-
/// Invalid route or parameters (cltv_delta, fee, pubkey) was specified
23+
/// A malformed Route was provided (eg overflowed value, node id mismatch, overly-looped route,
24+
/// too-many-hops, etc).
2525
RouteError {
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
29+
/// We were unable to complete the request as the Channel required to do so is unable to
30+
/// complete the request (or was not found). This can take many forms, including disconnected
31+
/// peer, channel at capacity, channel shutting down, etc.
3332
ChannelUnavailable {
3433
/// A human-readable error message
3534
err: &'static str

0 commit comments

Comments
 (0)