Implement writeable for APIError
authorValentine Wallace <vwallace@protonmail.com>
Sun, 19 Feb 2023 23:32:29 +0000 (18:32 -0500)
committerValentine Wallace <vwallace@protonmail.com>
Sat, 25 Feb 2023 21:13:42 +0000 (16:13 -0500)
lightning/src/ln/channelmanager.rs
lightning/src/ln/onion_utils.rs
lightning/src/ln/outbound_payment.rs
lightning/src/util/errors.rs

index 99cf4046fd5f85920b6c5bc05c3d8ae3bec7e6e5..1fd70ee6fed0e0ca8765fafc19773fc712ac4b61 100644 (file)
@@ -2420,10 +2420,10 @@ where
                let session_priv = SecretKey::from_slice(&session_priv_bytes[..]).expect("RNG is busted");
 
                let onion_keys = onion_utils::construct_onion_keys(&self.secp_ctx, &path, &session_priv)
-                       .map_err(|_| APIError::InvalidRoute{err: "Pubkey along hop was maliciously selected"})?;
+                       .map_err(|_| APIError::InvalidRoute{err: "Pubkey along hop was maliciously selected".to_owned()})?;
                let (onion_payloads, htlc_msat, htlc_cltv) = onion_utils::build_onion_payloads(path, total_value, payment_secret, cur_height, keysend_preimage)?;
                if onion_utils::route_size_insane(&onion_payloads) {
-                       return Err(APIError::InvalidRoute{err: "Route size too large considering onion data"});
+                       return Err(APIError::InvalidRoute{err: "Route size too large considering onion data".to_owned()});
                }
                let onion_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, prng_seed, payment_hash);
 
index 1abaf5920a51d8476d56b6f65968f725bd1a781a..2916829f259ec58ee110b95fdf14fb16a702a401 100644 (file)
@@ -182,11 +182,11 @@ pub(super) fn build_onion_payloads(path: &Vec<RouteHop>, total_msat: u64, paymen
                });
                cur_value_msat += hop.fee_msat;
                if cur_value_msat >= 21000000 * 100000000 * 1000 {
-                       return Err(APIError::InvalidRoute{err: "Channel fees overflowed?"});
+                       return Err(APIError::InvalidRoute{err: "Channel fees overflowed?".to_owned()});
                }
                cur_cltv += hop.cltv_expiry_delta as u32;
                if cur_cltv >= 500000000 {
-                       return Err(APIError::InvalidRoute{err: "Channel CLTV overflowed?"});
+                       return Err(APIError::InvalidRoute{err: "Channel CLTV overflowed?".to_owned()});
                }
                last_short_channel_id = hop.short_channel_id;
        }
index 16037c7f656954d4aeab22a0c3342fc6dc467268..473e050de60816ed707f5f452228fbeddaa97205 100644 (file)
@@ -895,22 +895,22 @@ impl OutboundPayments {
                   u32, PaymentId, &Option<PaymentPreimage>, [u8; 32]) -> Result<(), APIError>
        {
                if route.paths.len() < 1 {
-                       return Err(PaymentSendFailure::ParameterError(APIError::InvalidRoute{err: "There must be at least one path to send over"}));
+                       return Err(PaymentSendFailure::ParameterError(APIError::InvalidRoute{err: "There must be at least one path to send over".to_owned()}));
                }
                if payment_secret.is_none() && route.paths.len() > 1 {
-                       return Err(PaymentSendFailure::ParameterError(APIError::APIMisuseError{err: "Payment secret is required for multi-path payments".to_string()}));
+                       return Err(PaymentSendFailure::ParameterError(APIError::APIMisuseError{err: "Payment secret is required for multi-path payments".to_owned()}));
                }
                let mut total_value = 0;
                let our_node_id = node_signer.get_node_id(Recipient::Node).unwrap(); // TODO no unwrap
                let mut path_errs = Vec::with_capacity(route.paths.len());
                'path_check: for path in route.paths.iter() {
                        if path.len() < 1 || path.len() > 20 {
-                               path_errs.push(Err(APIError::InvalidRoute{err: "Path didn't go anywhere/had bogus size"}));
+                               path_errs.push(Err(APIError::InvalidRoute{err: "Path didn't go anywhere/had bogus size".to_owned()}));
                                continue 'path_check;
                        }
                        for (idx, hop) in path.iter().enumerate() {
                                if idx != path.len() - 1 && hop.pubkey == our_node_id {
-                                       path_errs.push(Err(APIError::InvalidRoute{err: "Path went through us but wasn't a simple rebalance loop to us"}));
+                                       path_errs.push(Err(APIError::InvalidRoute{err: "Path went through us but wasn't a simple rebalance loop to us".to_owned()}));
                                        continue 'path_check;
                                }
                        }
index b7d562e54ddb7d1bf3166aace8510ea10f62e2ff..aa740044676af69487ac06f928765862bbc7a417 100644 (file)
@@ -37,7 +37,7 @@ pub enum APIError {
        /// too-many-hops, etc).
        InvalidRoute {
                /// A human-readable error message
-               err: &'static str
+               err: String
        },
        /// We were unable to complete the request as the Channel required to do so is unable to
        /// complete the request (or was not found). This can take many forms, including disconnected
@@ -84,6 +84,18 @@ impl fmt::Debug for APIError {
        }
 }
 
+impl_writeable_tlv_based_enum_upgradable!(APIError,
+       (0, APIMisuseError) => { (0, err, required), },
+       (2, FeeRateTooHigh) => {
+               (0, err, required),
+               (2, feerate, required),
+       },
+       (4, InvalidRoute) => { (0, err, required), },
+       (6, ChannelUnavailable) => { (0, err, required), },
+       (8, MonitorUpdateInProgress) => {},
+       (10, IncompatibleShutdownScript) => { (0, script, required), },
+);
+
 #[inline]
 pub(crate) fn get_onion_debug_field(error_code: u16) -> (&'static str, usize) {
        match error_code & 0xff {