Skip to content

Commit 8d686d8

Browse files
Implement writeable for APIError
1 parent 34f8c39 commit 8d686d8

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2420,10 +2420,10 @@ where
24202420
let session_priv = SecretKey::from_slice(&session_priv_bytes[..]).expect("RNG is busted");
24212421

24222422
let onion_keys = onion_utils::construct_onion_keys(&self.secp_ctx, &path, &session_priv)
2423-
.map_err(|_| APIError::InvalidRoute{err: "Pubkey along hop was maliciously selected"})?;
2423+
.map_err(|_| APIError::InvalidRoute{err: "Pubkey along hop was maliciously selected".to_owned()})?;
24242424
let (onion_payloads, htlc_msat, htlc_cltv) = onion_utils::build_onion_payloads(path, total_value, payment_secret, cur_height, keysend_preimage)?;
24252425
if onion_utils::route_size_insane(&onion_payloads) {
2426-
return Err(APIError::InvalidRoute{err: "Route size too large considering onion data"});
2426+
return Err(APIError::InvalidRoute{err: "Route size too large considering onion data".to_owned()});
24272427
}
24282428
let onion_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, prng_seed, payment_hash);
24292429

lightning/src/ln/onion_utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,11 @@ pub(super) fn build_onion_payloads(path: &Vec<RouteHop>, total_msat: u64, paymen
182182
});
183183
cur_value_msat += hop.fee_msat;
184184
if cur_value_msat >= 21000000 * 100000000 * 1000 {
185-
return Err(APIError::InvalidRoute{err: "Channel fees overflowed?"});
185+
return Err(APIError::InvalidRoute{err: "Channel fees overflowed?".to_owned()});
186186
}
187187
cur_cltv += hop.cltv_expiry_delta as u32;
188188
if cur_cltv >= 500000000 {
189-
return Err(APIError::InvalidRoute{err: "Channel CLTV overflowed?"});
189+
return Err(APIError::InvalidRoute{err: "Channel CLTV overflowed?".to_owned()});
190190
}
191191
last_short_channel_id = hop.short_channel_id;
192192
}

lightning/src/ln/outbound_payment.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -895,22 +895,22 @@ impl OutboundPayments {
895895
u32, PaymentId, &Option<PaymentPreimage>, [u8; 32]) -> Result<(), APIError>
896896
{
897897
if route.paths.len() < 1 {
898-
return Err(PaymentSendFailure::ParameterError(APIError::InvalidRoute{err: "There must be at least one path to send over"}));
898+
return Err(PaymentSendFailure::ParameterError(APIError::InvalidRoute{err: "There must be at least one path to send over".to_owned()}));
899899
}
900900
if payment_secret.is_none() && route.paths.len() > 1 {
901-
return Err(PaymentSendFailure::ParameterError(APIError::APIMisuseError{err: "Payment secret is required for multi-path payments".to_string()}));
901+
return Err(PaymentSendFailure::ParameterError(APIError::APIMisuseError{err: "Payment secret is required for multi-path payments".to_owned()}));
902902
}
903903
let mut total_value = 0;
904904
let our_node_id = node_signer.get_node_id(Recipient::Node).unwrap(); // TODO no unwrap
905905
let mut path_errs = Vec::with_capacity(route.paths.len());
906906
'path_check: for path in route.paths.iter() {
907907
if path.len() < 1 || path.len() > 20 {
908-
path_errs.push(Err(APIError::InvalidRoute{err: "Path didn't go anywhere/had bogus size"}));
908+
path_errs.push(Err(APIError::InvalidRoute{err: "Path didn't go anywhere/had bogus size".to_owned()}));
909909
continue 'path_check;
910910
}
911911
for (idx, hop) in path.iter().enumerate() {
912912
if idx != path.len() - 1 && hop.pubkey == our_node_id {
913-
path_errs.push(Err(APIError::InvalidRoute{err: "Path went through us but wasn't a simple rebalance loop to us"}));
913+
path_errs.push(Err(APIError::InvalidRoute{err: "Path went through us but wasn't a simple rebalance loop to us".to_owned()}));
914914
continue 'path_check;
915915
}
916916
}

lightning/src/util/errors.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub enum APIError {
3737
/// too-many-hops, etc).
3838
InvalidRoute {
3939
/// A human-readable error message
40-
err: &'static str
40+
err: String
4141
},
4242
/// We were unable to complete the request as the Channel required to do so is unable to
4343
/// complete the request (or was not found). This can take many forms, including disconnected
@@ -84,6 +84,18 @@ impl fmt::Debug for APIError {
8484
}
8585
}
8686

87+
impl_writeable_tlv_based_enum_upgradable!(APIError,
88+
(0, APIMisuseError) => { (0, err, required), },
89+
(2, FeeRateTooHigh) => {
90+
(0, err, required),
91+
(2, feerate, required),
92+
},
93+
(4, InvalidRoute) => { (0, err, required), },
94+
(6, ChannelUnavailable) => { (0, err, required), },
95+
(8, MonitorUpdateInProgress) => {},
96+
(10, IncompatibleShutdownScript) => { (0, script, required), },
97+
);
98+
8799
#[inline]
88100
pub(crate) fn get_onion_debug_field(error_code: u16) -> (&'static str, usize) {
89101
match error_code & 0xff {

0 commit comments

Comments
 (0)