lightning/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
12use crate::ln::script::ShutdownScript;
13
14#[allow(unused_imports)]
15use crate::prelude::*;
16
17use 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)]
22pub 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::types::features::InitFeatures
68	IncompatibleShutdownScript {
69		/// The incompatible shutdown script.
70		script: ShutdownScript,
71	},
72}
73
74impl 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 } => {
79				write!(f, "{} feerate: {}", err, feerate)
80			},
81			APIError::InvalidRoute { ref err } => write!(f, "Invalid route provided: {}", err),
82			APIError::ChannelUnavailable { ref err } => write!(f, "Channel unavailable: {}", err),
83			APIError::MonitorUpdateInProgress => f.write_str(
84				"Client indicated a channel monitor update is in progress but not yet complete",
85			),
86			APIError::IncompatibleShutdownScript { ref script } => {
87				write!(f, "Provided a scriptpubkey format not accepted by peer: {}", script)
88			},
89		}
90	}
91}
92
93impl_writeable_tlv_based_enum_upgradable!(APIError,
94	(0, APIMisuseError) => { (0, err, required), },
95	(2, FeeRateTooHigh) => {
96		(0, err, required),
97		(2, feerate, required),
98	},
99	(4, InvalidRoute) => { (0, err, required), },
100	(6, ChannelUnavailable) => { (0, err, required), },
101	(8, MonitorUpdateInProgress) => {},
102	(10, IncompatibleShutdownScript) => { (0, script, required), },
103);
104
105#[inline]
106pub(crate) fn get_onion_debug_field(error_code: u16) -> (&'static str, usize) {
107	match error_code & 0xff {
108		4 | 5 | 6 => ("sha256_of_onion", 32),
109		11 | 12 => ("htlc_msat", 8),
110		13 | 18 => ("cltv_expiry", 4),
111		19 => ("incoming_htlc_msat", 8),
112		20 => ("flags", 2),
113		_ => ("", 0),
114	}
115}
116
117#[inline]
118pub(crate) fn get_onion_error_description(error_code: u16) -> (&'static str, &'static str) {
119	const BADONION: u16 = 0x8000;
120	const PERM: u16 = 0x4000;
121	const NODE: u16 = 0x2000;
122	const UPDATE: u16 = 0x1000;
123	match error_code {
124		_c if _c == PERM|1 => ("The realm byte was not understood by the processing node", "invalid_realm"),
125		_c if _c == NODE|2 => ("Node indicated temporary node failure", "temporary_node_failure"),
126		_c if _c == PERM|NODE|2 => ("Node indicated permanent node failure", "permanent_node_failure"),
127		_c if _c == PERM|NODE|3 => ("Node indicated the required node feature is missing in the onion", "required_node_feature_missing"),
128		_c if _c == BADONION|PERM|4 => ("Node indicated the version by is not understood", "invalid_onion_version"),
129		_c if _c == BADONION|PERM|5  => ("Node indicated the HMAC of the onion is incorrect", "invalid_onion_hmac"),
130		_c if _c == BADONION|PERM|6 => ("Node indicated the ephemeral public keys is not parseable", "invalid_onion_key"),
131		_c if _c == UPDATE|7 => ("Node indicated the outgoing channel is unable to handle the HTLC temporarily", "temporary_channel_failure"),
132		_c if _c == PERM|8 => ("Node indicated the outgoing channel is unable to handle the HTLC peramanently", "permanent_channel_failure"),
133		_c if _c == PERM|9 => ("Node indicated the required feature for the outgoing channel is not satisfied", "required_channel_feature_missing"),
134		_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"),
135		_c if _c == UPDATE|11 => ("Node indicated the HTLC amount was below the required minmum for the outbound channel", "amount_below_minimum"),
136		_c if _c == UPDATE|12 => ("Node indicated the fee amount does not meet the required level", "fee_insufficient"),
137		_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"),
138		_c if _c == UPDATE|14 => ("Node indicated the CLTV expiry too close to the current block height for safe handling", "expiry_too_soon"),
139		_c if _c == PERM|15 => ("The final node indicated the payment hash is unknown or amount is incorrect", "incorrect_or_unknown_payment_details"),
140		_c if _c == PERM|16 => ("The final node indicated the payment amount is incorrect", "incorrect_payment_amount"),
141		_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"),
142		_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"),
143		_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"),
144		_c if _c == UPDATE|20 => ("Node indicated the outbound channel has been disabled", "channel_disabled"),
145		_c if _c == 21 => ("Node indicated the CLTV expiry in the HTLC is too far in the future", "expiry_too_far"),
146		_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"),
147		_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"),
148		_ => ("Unknown", ""),
149	}
150}