Skip to content

Commit d105761

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 f89ba52 commit d105761

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

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

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

0 commit comments

Comments
 (0)