Skip to content

Commit dba48e0

Browse files
committed
fuzzy exports
1 parent fe4bcd7 commit dba48e0

File tree

6 files changed

+76
-61
lines changed

6 files changed

+76
-61
lines changed

fuzz/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
hfuzz_target
22
target
33
hfuzz_workspace
4+
corpus

fuzz/src/process_onion_failure.rs

+4-9
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
9292

9393
let mut hops = Vec::<RouteHop>::new();
9494
let hop_count = get_slice!(1)[0] as usize;
95-
for i in 0..hop_count {
95+
for _ in 0..hop_count {
9696
hops.push(RouteHop {
9797
pubkey: get_pubkey!(),
9898
node_features: NodeFeatures::empty(),
@@ -108,7 +108,7 @@ fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
108108
true => {
109109
let mut trampoline_hops = Vec::<TrampolineHop>::new();
110110
let trampoline_hop_count = get_slice!(1)[0] as usize;
111-
for i in 0..trampoline_hop_count {
111+
for _ in 0..trampoline_hop_count {
112112
trampoline_hops.push(TrampolineHop {
113113
pubkey: get_pubkey!(),
114114
node_features: NodeFeatures::empty(),
@@ -118,7 +118,7 @@ fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
118118
}
119119
let mut blinded_hops = Vec::<BlindedHop>::new();
120120
let blinded_hop_count = get_slice!(1)[0] as usize;
121-
for i in 0..blinded_hop_count {
121+
for _ in 0..blinded_hop_count {
122122
blinded_hops.push(BlindedHop {
123123
blinded_node_id: get_pubkey!(),
124124
encrypted_payload: get_slice!(get_u8!()).to_vec(),
@@ -147,12 +147,7 @@ fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
147147
let failure_len = u16::from_be_bytes(get_slice!(2).try_into().unwrap());
148148
let encrypted_packet = OnionErrorPacket { data: get_slice!(failure_len as u64).into() };
149149

150-
lightning::ln::onion_utils::process_onion_failure(
151-
&secp_ctx,
152-
&logger,
153-
&htlc_source,
154-
encrypted_packet,
155-
);
150+
lightning::ln::process_onion_failure(&secp_ctx, &logger, &htlc_source, encrypted_packet);
156151
}
157152

158153
/// Method that needs to be added manually, {name}_test

lightning/src/ln/channelmanager.rs

+43-35
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 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

+4-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub mod channel;
3939
#[cfg(not(fuzzing))]
4040
pub(crate) mod channel;
4141

42-
pub mod onion_utils;
42+
pub(crate) mod onion_utils;
4343
mod outbound_payment;
4444
pub mod wire;
4545

@@ -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

+7-7
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 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 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

+17-9
Original file line numberDiff line numberDiff line change
@@ -926,16 +926,24 @@ pub(super) fn build_failure_packet(
926926
onion_error_packet
927927
}
928928

929-
pub 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

940948
pub fn process_onion_failure<T: secp256k1::Signing, L: Deref>(
941949
secp_ctx: &Secp256k1<T>, logger: &L, htlc_source: &HTLCSource,

0 commit comments

Comments
 (0)