Skip to content

Commit 5ced282

Browse files
committed
Encapsulate HTLCFailReason to not expose struct variants
Now that `HTLCFailReason` is opaque and in `onion_utils`, we should encapsulate it so that `ChannelManager` can no longer directly access its inner fields.
1 parent dabae73 commit 5ced282

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

lightning/src/ln/onion_utils.rs

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,10 @@ pub(super) fn process_onion_failure<T: secp256k1::Signing, L: Deref>(secp_ctx: &
593593
}
594594

595595
#[derive(Clone)] // See Channel::revoke_and_ack for why, tl;dr: Rust bug
596-
pub(super) enum HTLCFailReason {
596+
pub(super) struct HTLCFailReason(HTLCFailReasonRepr);
597+
598+
#[derive(Clone)] // See Channel::revoke_and_ack for why, tl;dr: Rust bug
599+
enum HTLCFailReasonRepr {
597600
LightningError {
598601
err: msgs::OnionErrorPacket,
599602
},
@@ -605,18 +608,29 @@ pub(super) enum HTLCFailReason {
605608

606609
impl core::fmt::Debug for HTLCFailReason {
607610
fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
608-
match self {
609-
HTLCFailReason::Reason { ref failure_code, .. } => {
611+
match self.0 {
612+
HTLCFailReasonRepr::Reason { ref failure_code, .. } => {
610613
write!(f, "HTLC error code {}", failure_code)
611614
},
612-
HTLCFailReason::LightningError { .. } => {
615+
HTLCFailReasonRepr::LightningError { .. } => {
613616
write!(f, "pre-built LightningError")
614617
}
615618
}
616619
}
617620
}
618621

619-
impl_writeable_tlv_based_enum!(HTLCFailReason,
622+
impl Writeable for HTLCFailReason {
623+
fn write<W: crate::util::ser::Writer>(&self, writer: &mut W) -> Result<(), crate::io::Error> {
624+
self.0.write(writer)
625+
}
626+
}
627+
impl Readable for HTLCFailReason {
628+
fn read<R: crate::io::Read>(reader: &mut R) -> Result<Self, crate::ln::msgs::DecodeError> {
629+
Ok(Self(Readable::read(reader)?))
630+
}
631+
}
632+
633+
impl_writeable_tlv_based_enum!(HTLCFailReasonRepr,
620634
(0, LightningError) => {
621635
(0, err, required),
622636
},
@@ -628,20 +642,20 @@ impl_writeable_tlv_based_enum!(HTLCFailReason,
628642

629643
impl HTLCFailReason {
630644
pub(super) fn reason(failure_code: u16, data: Vec<u8>) -> Self {
631-
Self::Reason { failure_code, data }
645+
Self(HTLCFailReasonRepr::Reason { failure_code, data })
632646
}
633647

634648
pub(super) fn from_failure_code(failure_code: u16) -> Self {
635-
Self::Reason { failure_code, data: Vec::new() }
649+
Self(HTLCFailReasonRepr::Reason { failure_code, data: Vec::new() })
636650
}
637651

638652
pub(super) fn from_msg(msg: &msgs::UpdateFailHTLC) -> Self {
639-
Self::LightningError { err: msg.reason.clone() }
653+
Self(HTLCFailReasonRepr::LightningError { err: msg.reason.clone() })
640654
}
641655

642656
pub(super) fn get_encrypted_failure_packet(&self, incoming_packet_shared_secret: &[u8; 32], phantom_shared_secret: &Option<[u8; 32]>) -> msgs::OnionErrorPacket {
643-
match self {
644-
HTLCFailReason::Reason { ref failure_code, ref data } => {
657+
match self.0 {
658+
HTLCFailReasonRepr::Reason { ref failure_code, ref data } => {
645659
if let Some(phantom_ss) = phantom_shared_secret {
646660
let phantom_packet = build_failure_packet(phantom_ss, *failure_code, &data[..]).encode();
647661
let encrypted_phantom_packet = encrypt_failure_packet(phantom_ss, &phantom_packet);
@@ -651,18 +665,18 @@ impl HTLCFailReason {
651665
encrypt_failure_packet(incoming_packet_shared_secret, &packet)
652666
}
653667
},
654-
HTLCFailReason::LightningError { err } => {
668+
HTLCFailReasonRepr::LightningError { ref err } => {
655669
encrypt_failure_packet(incoming_packet_shared_secret, &err.data)
656670
}
657671
}
658672
}
659673

660674
pub(super) fn decode_onion_failure<T: secp256k1::Signing, L: Deref>(&self, secp_ctx: &Secp256k1<T>, logger: &L, htlc_source: &HTLCSource) -> (Option<crate::routing::gossip::NetworkUpdate>, Option<u64>, bool, Option<u16>, Option<Vec<u8>>) where L::Target: Logger {
661-
match self {
662-
HTLCFailReason::LightningError { ref err } => {
675+
match self.0 {
676+
HTLCFailReasonRepr::LightningError { ref err } => {
663677
process_onion_failure(secp_ctx, logger, &htlc_source, err.data.clone())
664678
},
665-
HTLCFailReason::Reason { ref failure_code, ref data, .. } => {
679+
HTLCFailReasonRepr::Reason { ref failure_code, ref data, .. } => {
666680
// we get a fail_malformed_htlc from the first hop
667681
// TODO: We'd like to generate a NetworkUpdate for temporary
668682
// failures here, but that would be insufficient as find_route

0 commit comments

Comments
 (0)