Skip to content

Commit 935ffde

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 5e9ae6b commit 935ffde

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
@@ -1457,6 +1457,9 @@ const UPDATE: u16 = 0x1000;
14571457
/// [`Self::FeeInsufficient`] is a direct representation of its underlying BOLT04 error code.
14581458
/// [`Self::PrivateChannelForward`] provides additional information that is not provided by its
14591459
/// BOLT04 error code.
1460+
//
1461+
// Note that variants that directly represent BOLT04 error codes must implement conversion from u16
1462+
// values using [`impl_from_u16_for_htlc_reason`]
14601463
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
14611464
pub enum LocalHTLCFailureReason {
14621465
/// There has been a temporary processing failure on the node which may resolve on retry.
@@ -1691,57 +1694,49 @@ impl LocalHTLCFailureReason {
16911694
}
16921695
}
16931696

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

17461741
impl_writeable_tlv_based_enum!(LocalHTLCFailureReason,
17471742
(1, TemporaryNodeFailure) => {},

0 commit comments

Comments
 (0)