735ce044f810b6a4f9aa97e500c1c628cd43ee4d
[rust-lightning] / lightning / src / util / errors.rs
1 // This file is Copyright its original authors, visible in version control
2 // history.
3 //
4 // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5 // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7 // You may not use this file except in accordance with one or both of these
8 // licenses.
9
10 //! Error types live here.
11
12 use crate::ln::script::ShutdownScript;
13
14 #[allow(unused_imports)]
15 use crate::prelude::*;
16
17 use core::fmt;
18
19 /// Indicates an error on the client's part (usually some variant of attempting to use too-low or
20 /// too-high values)
21 #[derive(Clone, PartialEq, Eq)]
22 pub enum APIError {
23         /// Indicates the API was wholly misused (see err for more). Cases where these can be returned
24         /// are documented, but generally indicates some precondition of a function was violated.
25         APIMisuseError {
26                 /// A human-readable error message
27                 err: String
28         },
29         /// Due to a high feerate, we were unable to complete the request.
30         /// For example, this may be returned if the feerate implies we cannot open a channel at the
31         /// requested value, but opening a larger channel would succeed.
32         FeeRateTooHigh {
33                 /// A human-readable error message
34                 err: String,
35                 /// The feerate which was too high.
36                 feerate: u32
37         },
38         /// A malformed Route was provided (eg overflowed value, node id mismatch, overly-looped route,
39         /// too-many-hops, etc).
40         InvalidRoute {
41                 /// A human-readable error message
42                 err: String
43         },
44         /// We were unable to complete the request as the Channel required to do so is unable to
45         /// complete the request (or was not found). This can take many forms, including disconnected
46         /// peer, channel at capacity, channel shutting down, etc.
47         ChannelUnavailable {
48                 /// A human-readable error message
49                 err: String
50         },
51         /// An attempt to call [`chain::Watch::watch_channel`]/[`chain::Watch::update_channel`]
52         /// returned a [`ChannelMonitorUpdateStatus::InProgress`] indicating the persistence of a
53         /// monitor update is awaiting async resolution. Once it resolves the attempted action should
54         /// complete automatically.
55         ///
56         /// [`chain::Watch::watch_channel`]: crate::chain::Watch::watch_channel
57         /// [`chain::Watch::update_channel`]: crate::chain::Watch::update_channel
58         /// [`ChannelMonitorUpdateStatus::InProgress`]: crate::chain::ChannelMonitorUpdateStatus::InProgress
59         MonitorUpdateInProgress,
60         /// [`SignerProvider::get_shutdown_scriptpubkey`] returned a shutdown scriptpubkey incompatible
61         /// with the channel counterparty as negotiated in [`InitFeatures`].
62         ///
63         /// Using a SegWit v0 script should resolve this issue. If you cannot, you won't be able to open
64         /// a channel or cooperatively close one with this peer (and will have to force-close instead).
65         ///
66         /// [`SignerProvider::get_shutdown_scriptpubkey`]: crate::sign::SignerProvider::get_shutdown_scriptpubkey
67         /// [`InitFeatures`]: crate::ln::features::InitFeatures
68         IncompatibleShutdownScript {
69                 /// The incompatible shutdown script.
70                 script: ShutdownScript,
71         },
72 }
73
74 impl fmt::Debug for APIError {
75         fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
76                 match *self {
77                         APIError::APIMisuseError {ref err} => write!(f, "Misuse error: {}", err),
78                         APIError::FeeRateTooHigh {ref err, ref feerate} => write!(f, "{} feerate: {}", err, feerate),
79                         APIError::InvalidRoute {ref err} => write!(f, "Invalid route provided: {}", err),
80                         APIError::ChannelUnavailable {ref err} => write!(f, "Channel unavailable: {}", err),
81                         APIError::MonitorUpdateInProgress => f.write_str("Client indicated a channel monitor update is in progress but not yet complete"),
82                         APIError::IncompatibleShutdownScript { ref script } => {
83                                 write!(f, "Provided a scriptpubkey format not accepted by peer: {}", script)
84                         },
85                 }
86         }
87 }
88
89 impl_writeable_tlv_based_enum_upgradable!(APIError,
90         (0, APIMisuseError) => { (0, err, required), },
91         (2, FeeRateTooHigh) => {
92                 (0, err, required),
93                 (2, feerate, required),
94         },
95         (4, InvalidRoute) => { (0, err, required), },
96         (6, ChannelUnavailable) => { (0, err, required), },
97         (8, MonitorUpdateInProgress) => {},
98         (10, IncompatibleShutdownScript) => { (0, script, required), },
99 );
100
101 #[inline]
102 pub(crate) fn get_onion_debug_field(error_code: u16) -> (&'static str, usize) {
103         match error_code & 0xff {
104                 4|5|6 => ("sha256_of_onion", 32),
105                 11|12 => ("htlc_msat", 8),
106                 13|18 => ("cltv_expiry", 4),
107                 19 => ("incoming_htlc_msat", 8),
108                 20 => ("flags", 2),
109                 _ => ("", 0),
110         }
111 }
112
113 #[inline]
114 pub(crate) fn get_onion_error_description(error_code: u16) -> (&'static str, &'static str) {
115         const BADONION: u16 = 0x8000;
116         const PERM: u16 = 0x4000;
117         const NODE: u16 = 0x2000;
118         const UPDATE: u16 = 0x1000;
119         match error_code {
120                 _c if _c == PERM|1 => ("The realm byte was not understood by the processing node", "invalid_realm"),
121                 _c if _c == NODE|2 => ("Node indicated temporary node failure", "temporary_node_failure"),
122                 _c if _c == PERM|NODE|2 => ("Node indicated permanent node failure", "permanent_node_failure"),
123                 _c if _c == PERM|NODE|3 => ("Node indicated the required node feature is missing in the onion", "required_node_feature_missing"),
124                 _c if _c == BADONION|PERM|4 => ("Node indicated the version by is not understood", "invalid_onion_version"),
125                 _c if _c == BADONION|PERM|5  => ("Node indicated the HMAC of the onion is incorrect", "invalid_onion_hmac"),
126                 _c if _c == BADONION|PERM|6 => ("Node indicated the ephemeral public keys is not parseable", "invalid_onion_key"),
127                 _c if _c == UPDATE|7 => ("Node indicated the outgoing channel is unable to handle the HTLC temporarily", "temporary_channel_failure"),
128                 _c if _c == PERM|8 => ("Node indicated the outgoing channel is unable to handle the HTLC peramanently", "permanent_channel_failure"),
129                 _c if _c == PERM|9 => ("Node indicated the required feature for the outgoing channel is not satisfied", "required_channel_feature_missing"),
130                 _c if _c == PERM|10 => ("Node indicated the outbound channel is not found for the specified short_channel_id in the onion packet", "unknown_next_peer"),
131                 _c if _c == UPDATE|11 => ("Node indicated the HTLC amount was below the required minmum for the outbound channel", "amount_below_minimum"),
132                 _c if _c == UPDATE|12 => ("Node indicated the fee amount does not meet the required level", "fee_insufficient"),
133                 _c if _c == UPDATE|13 => ("Node indicated the cltv_expiry does not comply with the cltv_expiry_delta required by the outgoing channel", "incorrect_cltv_expiry"),
134                 _c if _c == UPDATE|14 => ("Node indicated the CLTV expiry too close to the current block height for safe handling", "expiry_too_soon"),
135                 _c if _c == PERM|15 => ("The final node indicated the payment hash is unknown or amount is incorrect", "incorrect_or_unknown_payment_details"),
136                 _c if _c == PERM|16 => ("The final node indicated the payment amount is incorrect", "incorrect_payment_amount"),
137                 _c if _c == 17 => ("The final node indicated the CLTV expiry is too close to the current block height for safe handling", "final_expiry_too_soon"),
138                 _c if _c == 18 => ("The final node indicated the CLTV expiry in the HTLC does not match the value in the onion", "final_incorrect_cltv_expiry"),
139                 _c if _c == 19 => ("The final node indicated the amount in the HTLC does not match the value in the onion", "final_incorrect_htlc_amount"),
140                 _c if _c == UPDATE|20 => ("Node indicated the outbound channel has been disabled", "channel_disabled"),
141                 _c if _c == 21 => ("Node indicated the CLTV expiry in the HTLC is too far in the future", "expiry_too_far"),
142                 _c if _c == PERM|22 => ("Node indicated that the decrypted onion per-hop payload was not understood by it or is incomplete", "invalid_onion_payload"),
143                 _c if _c == 23 => ("The final node indicated the complete amount of the multi-part payment was not received within a reasonable time", "mpp_timeout"),
144                 _ => ("Unknown", ""),
145         }
146 }