Skip to content

Commit 07c5b99

Browse files
committed
Make types available for fuzzing
Preparatory commit that exposes types that are related to onion failure processing to the fuzzing targets.
1 parent 830ffa0 commit 07c5b99

File tree

4 files changed

+71
-52
lines changed

4 files changed

+71
-52
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -409,27 +409,6 @@ pub enum BlindedFailure {
409409
FromBlindedNode,
410410
}
411411

412-
/// Tracks the inbound corresponding to an outbound HTLC
413-
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
414-
pub(crate) struct HTLCPreviousHopData {
415-
// Note that this may be an outbound SCID alias for the associated channel.
416-
short_channel_id: u64,
417-
user_channel_id: Option<u128>,
418-
htlc_id: u64,
419-
incoming_packet_shared_secret: [u8; 32],
420-
phantom_shared_secret: Option<[u8; 32]>,
421-
blinded_failure: Option<BlindedFailure>,
422-
channel_id: ChannelId,
423-
424-
// These fields are consumed by `claim_funds_from_hop()` when updating a force-closed backwards
425-
// channel with a preimage provided by the forward channel.
426-
outpoint: OutPoint,
427-
counterparty_node_id: Option<PublicKey>,
428-
/// Used to preserve our backwards channel by failing back in case an HTLC claim in the forward
429-
/// channel remains unconfirmed for too long.
430-
cltv_expiry: Option<u32>,
431-
}
432-
433412
#[derive(PartialEq, Eq)]
434413
enum OnionPayload {
435414
/// Indicates this incoming onion payload is for the purpose of paying an invoice.
@@ -674,21 +653,50 @@ impl_writeable_tlv_based_enum!(SentHTLCId,
674653
},
675654
);
676655

677-
678-
/// Tracks the inbound corresponding to an outbound HTLC
679-
#[allow(clippy::derive_hash_xor_eq)] // Our Hash is faithful to the data, we just don't have SecretKey::hash
680-
#[derive(Clone, Debug, PartialEq, Eq)]
681-
pub(crate) enum HTLCSource {
682-
PreviousHopData(HTLCPreviousHopData),
683-
OutboundRoute {
684-
path: Path,
685-
session_priv: SecretKey,
686-
/// Technically we can recalculate this from the route, but we cache it here to avoid
687-
/// doing a double-pass on route when we get a failure back
688-
first_hop_htlc_msat: u64,
689-
payment_id: PaymentId,
690-
},
656+
mod fuzzy_channelmanager {
657+
use super::*;
658+
659+
/// Tracks the inbound corresponding to an outbound HTLC
660+
#[allow(clippy::derive_hash_xor_eq)] // Our Hash is faithful to the data, we just don't have SecretKey::hash
661+
#[derive(Clone, Debug, PartialEq, Eq)]
662+
pub enum HTLCSource {
663+
PreviousHopData(HTLCPreviousHopData),
664+
OutboundRoute {
665+
path: Path,
666+
session_priv: SecretKey,
667+
/// Technically we can recalculate this from the route, but we cache it here to avoid
668+
/// doing a double-pass on route when we get a failure back
669+
first_hop_htlc_msat: u64,
670+
payment_id: PaymentId,
671+
},
672+
}
673+
674+
/// Tracks the inbound corresponding to an outbound HTLC
675+
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
676+
pub struct HTLCPreviousHopData {
677+
// Note that this may be an outbound SCID alias for the associated channel.
678+
pub short_channel_id: u64,
679+
pub user_channel_id: Option<u128>,
680+
pub htlc_id: u64,
681+
pub incoming_packet_shared_secret: [u8; 32],
682+
pub phantom_shared_secret: Option<[u8; 32]>,
683+
pub blinded_failure: Option<BlindedFailure>,
684+
pub channel_id: ChannelId,
685+
686+
// These fields are consumed by `claim_funds_from_hop()` when updating a force-closed backwards
687+
// channel with a preimage provided by the forward channel.
688+
pub outpoint: OutPoint,
689+
pub counterparty_node_id: Option<PublicKey>,
690+
/// Used to preserve our backwards channel by failing back in case an HTLC claim in the forward
691+
/// channel remains unconfirmed for too long.
692+
pub cltv_expiry: Option<u32>,
693+
}
691694
}
695+
#[cfg(fuzzing)]
696+
pub use self::fuzzy_channelmanager::*;
697+
#[cfg(not(fuzzing))]
698+
pub(crate) use self::fuzzy_channelmanager::*;
699+
692700
#[allow(clippy::derive_hash_xor_eq)] // Our Hash is faithful to the data, we just don't have SecretKey::hash
693701
impl core::hash::Hash for HTLCSource {
694702
fn hash<H: core::hash::Hasher>(&self, hasher: &mut H) {

lightning/src/ln/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ pub use onion_utils::create_payment_onion;
5151
// without the node parameter being mut. This is incorrect, and thus newer rustcs will complain
5252
// about an unnecessary mut. Thus, we silence the unused_mut warning in two test modules below.
5353

54+
#[cfg(fuzzing)]
55+
pub use onion_utils::process_onion_failure;
56+
5457
#[cfg(test)]
5558
#[allow(unused_mut)]
5659
pub mod bolt11_payment_tests;

lightning/src/ln/msgs.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2241,6 +2241,13 @@ mod fuzzy_internal_msgs {
22412241
pub(crate) failuremsg: Vec<u8>,
22422242
pub(crate) pad: Vec<u8>,
22432243
}
2244+
2245+
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
2246+
pub struct OnionErrorPacket {
2247+
// This really should be a constant size slice, but the spec lets these things be up to 128KB?
2248+
// (TODO) We limit it in decode to much lower...
2249+
pub data: Vec<u8>,
2250+
}
22442251
}
22452252
#[cfg(fuzzing)]
22462253
pub use self::fuzzy_internal_msgs::*;
@@ -2349,13 +2356,6 @@ impl Debug for TrampolineOnionPacket {
23492356
}
23502357
}
23512358

2352-
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
2353-
pub(crate) struct OnionErrorPacket {
2354-
// This really should be a constant size slice, but the spec lets these things be up to 128KB?
2355-
// (TODO) We limit it in decode to much lower...
2356-
pub(crate) data: Vec<u8>,
2357-
}
2358-
23592359
impl From<UpdateFailHTLC> for OnionErrorPacket {
23602360
fn from(msg: UpdateFailHTLC) -> Self {
23612361
OnionErrorPacket {

lightning/src/ln/onion_utils.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -926,18 +926,26 @@ pub(super) fn build_failure_packet(
926926
onion_error_packet
927927
}
928928

929-
pub(crate) struct DecodedOnionFailure {
930-
pub(crate) network_update: Option<NetworkUpdate>,
931-
pub(crate) short_channel_id: Option<u64>,
932-
pub(crate) payment_failed_permanently: bool,
933-
pub(crate) failed_within_blinded_path: bool,
934-
#[cfg(any(test, feature = "_test_utils"))]
935-
pub(crate) onion_error_code: Option<u16>,
936-
#[cfg(any(test, feature = "_test_utils"))]
937-
pub(crate) onion_error_data: Option<Vec<u8>>,
929+
mod fuzzy_onion_utils {
930+
use super::*;
931+
932+
pub struct DecodedOnionFailure {
933+
pub(crate) network_update: Option<NetworkUpdate>,
934+
pub(crate) short_channel_id: Option<u64>,
935+
pub(crate) payment_failed_permanently: bool,
936+
pub(crate) failed_within_blinded_path: bool,
937+
#[cfg(any(test, feature = "_test_utils"))]
938+
pub(crate) onion_error_code: Option<u16>,
939+
#[cfg(any(test, feature = "_test_utils"))]
940+
pub(crate) onion_error_data: Option<Vec<u8>>,
941+
}
938942
}
943+
#[cfg(fuzzing)]
944+
pub use self::fuzzy_onion_utils::*;
945+
#[cfg(not(fuzzing))]
946+
pub(crate) use self::fuzzy_onion_utils::*;
939947

940-
pub(super) fn process_onion_failure<T: secp256k1::Signing, L: Deref>(
948+
pub fn process_onion_failure<T: secp256k1::Signing, L: Deref>(
941949
secp_ctx: &Secp256k1<T>, logger: &L, htlc_source: &HTLCSource,
942950
encrypted_packet: OnionErrorPacket,
943951
) -> DecodedOnionFailure

0 commit comments

Comments
 (0)