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