Skip to content

Commit 503d13b

Browse files
committed
ln/refactor: macro to impl From<u16> for LocalHTLCFailureReason
De-duplicate use of u16 failure codes by using a macro that will match against each variant's failure_code instead.
1 parent eeddeb1 commit 503d13b

File tree

1 file changed

+46
-51
lines changed

1 file changed

+46
-51
lines changed

lightning/src/ln/onion_utils.rs

+46-51
Original file line numberDiff line numberDiff line change
@@ -1461,6 +1461,9 @@ const UPDATE: u16 = 0x1000;
14611461
/// [`Self::FeeInsufficient`] is a direct representation of its underlying BOLT04 error code.
14621462
/// [`Self::PrivateChannelForward`] provides additional information that is not provided by its
14631463
/// BOLT04 error code.
1464+
//
1465+
// Note that variants that directly represent BOLT04 error codes must implement conversion from u16
1466+
// values using [`impl_from_u16_for_htlc_reason`]
14641467
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
14651468
pub enum LocalHTLCFailureReason {
14661469
/// There has been a temporary processing failure on the node which may resolve on retry.
@@ -1692,57 +1695,49 @@ impl LocalHTLCFailureReason {
16921695
}
16931696
}
16941697

1695-
impl From<u16> for LocalHTLCFailureReason {
1696-
fn from(value: u16) -> Self {
1697-
if value == (NODE | 2) {
1698-
LocalHTLCFailureReason::TemporaryNodeFailure
1699-
} else if value == (PERM | NODE | 2) {
1700-
LocalHTLCFailureReason::PermanentNodeFailure
1701-
} else if value == (PERM | NODE | 3) {
1702-
LocalHTLCFailureReason::RequiredNodeFeature
1703-
} else if value == (BADONION | PERM | 4) {
1704-
LocalHTLCFailureReason::InvalidOnionVersion
1705-
} else if value == (BADONION | PERM | 5) {
1706-
LocalHTLCFailureReason::InvalidOnionHMAC
1707-
} else if value == (BADONION | PERM | 6) {
1708-
LocalHTLCFailureReason::InvalidOnionKey
1709-
} else if value == (UPDATE | 7) {
1710-
LocalHTLCFailureReason::TemporaryChannelFailure
1711-
} else if value == (PERM | 8) {
1712-
LocalHTLCFailureReason::PermanentChannelFailure
1713-
} else if value == (PERM | 9) {
1714-
LocalHTLCFailureReason::RequiredChannelFeature
1715-
} else if value == (PERM | 10) {
1716-
LocalHTLCFailureReason::UnknownNextPeer
1717-
} else if value == (UPDATE | 11) {
1718-
LocalHTLCFailureReason::AmountBelowMinimum
1719-
} else if value == (UPDATE | 12) {
1720-
LocalHTLCFailureReason::FeeInsufficient
1721-
} else if value == (UPDATE | 13) {
1722-
LocalHTLCFailureReason::IncorrectCLTVExpiry
1723-
} else if value == (UPDATE | 14) {
1724-
LocalHTLCFailureReason::CLTVExpiryTooSoon
1725-
} else if value == (PERM | 15) {
1726-
LocalHTLCFailureReason::IncorrectPaymentDetails
1727-
} else if value == 18 {
1728-
LocalHTLCFailureReason::FinalIncorrectCLTVExpiry
1729-
} else if value == 19 {
1730-
LocalHTLCFailureReason::FinalIncorrectHTLCAmount
1731-
} else if value == (UPDATE | 20) {
1732-
LocalHTLCFailureReason::ChannelDisabled
1733-
} else if value == 21 {
1734-
LocalHTLCFailureReason::CLTVExpiryTooFar
1735-
} else if value == (PERM | 22) {
1736-
LocalHTLCFailureReason::InvalidOnionPayload
1737-
} else if value == 23 {
1738-
LocalHTLCFailureReason::MPPTimeout
1739-
} else if value == (BADONION | PERM | 24) {
1740-
LocalHTLCFailureReason::InvalidOnionBlinding
1741-
} else {
1742-
LocalHTLCFailureReason::UnknownFailureCode { code: value }
1743-
}
1744-
}
1745-
}
1698+
macro_rules! impl_from_u16_for_htlc_reason {
1699+
($enum:ident, [$($variant:ident),* $(,)?]) => {
1700+
impl From<u16> for $enum {
1701+
fn from(value: u16) -> Self {
1702+
$(
1703+
if value == $enum::$variant.failure_code() {
1704+
return $enum::$variant;
1705+
}
1706+
)*
1707+
$enum::UnknownFailureCode { code: value }
1708+
}
1709+
}
1710+
};
1711+
}
1712+
1713+
// Error codes that represent BOLT04 error codes must be included here.
1714+
impl_from_u16_for_htlc_reason!(
1715+
LocalHTLCFailureReason,
1716+
[
1717+
TemporaryNodeFailure,
1718+
PermanentNodeFailure,
1719+
RequiredNodeFeature,
1720+
InvalidOnionVersion,
1721+
InvalidOnionHMAC,
1722+
InvalidOnionKey,
1723+
TemporaryChannelFailure,
1724+
PermanentChannelFailure,
1725+
RequiredChannelFeature,
1726+
UnknownNextPeer,
1727+
AmountBelowMinimum,
1728+
FeeInsufficient,
1729+
IncorrectCLTVExpiry,
1730+
CLTVExpiryTooSoon,
1731+
IncorrectPaymentDetails,
1732+
FinalIncorrectCLTVExpiry,
1733+
FinalIncorrectHTLCAmount,
1734+
ChannelDisabled,
1735+
CLTVExpiryTooFar,
1736+
InvalidOnionPayload,
1737+
MPPTimeout,
1738+
InvalidOnionBlinding,
1739+
]
1740+
);
17461741

17471742
impl fmt::Display for LocalHTLCFailureReason {
17481743
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {

0 commit comments

Comments
 (0)