Skip to content

Commit 4b24135

Browse files
authored
Merge pull request #2128 from valentinewallace/2023-03-route-blinding-groundwork
Route blinding groundwork
2 parents 100fdbb + c9d3544 commit 4b24135

13 files changed

+289
-269
lines changed

fuzz/src/bin/gen_target.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ GEN_TEST refund_deser
2020
GEN_TEST router
2121
GEN_TEST zbase32
2222
GEN_TEST indexedmap
23+
GEN_TEST onion_hop_data
2324

2425
GEN_TEST msg_accept_channel msg_targets::
2526
GEN_TEST msg_announcement_signatures msg_targets::
@@ -51,7 +52,6 @@ GEN_TEST msg_update_add_htlc msg_targets::
5152
GEN_TEST msg_error_message msg_targets::
5253
GEN_TEST msg_channel_update msg_targets::
5354

54-
GEN_TEST msg_onion_hop_data msg_targets::
5555
GEN_TEST msg_ping msg_targets::
5656
GEN_TEST msg_pong msg_targets::
5757

fuzz/src/bin/msg_onion_hop_data_target.rs renamed to fuzz/src/bin/onion_hop_data_target.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
compile_error!("Fuzz targets need cfg=fuzzing");
1717

1818
extern crate lightning_fuzz;
19-
use lightning_fuzz::msg_targets::msg_onion_hop_data::*;
19+
use lightning_fuzz::onion_hop_data::*;
2020

2121
#[cfg(feature = "afl")]
2222
#[macro_use] extern crate afl;
2323
#[cfg(feature = "afl")]
2424
fn main() {
2525
fuzz!(|data| {
26-
msg_onion_hop_data_run(data.as_ptr(), data.len());
26+
onion_hop_data_run(data.as_ptr(), data.len());
2727
});
2828
}
2929

@@ -33,7 +33,7 @@ fn main() {
3333
fn main() {
3434
loop {
3535
fuzz!(|data| {
36-
msg_onion_hop_data_run(data.as_ptr(), data.len());
36+
onion_hop_data_run(data.as_ptr(), data.len());
3737
});
3838
}
3939
}
@@ -42,7 +42,7 @@ fn main() {
4242
#[macro_use] extern crate libfuzzer_sys;
4343
#[cfg(feature = "libfuzzer_fuzz")]
4444
fuzz_target!(|data: &[u8]| {
45-
msg_onion_hop_data_run(data.as_ptr(), data.len());
45+
onion_hop_data_run(data.as_ptr(), data.len());
4646
});
4747

4848
#[cfg(feature = "stdin_fuzz")]
@@ -51,7 +51,7 @@ fn main() {
5151

5252
let mut data = Vec::with_capacity(8192);
5353
std::io::stdin().read_to_end(&mut data).unwrap();
54-
msg_onion_hop_data_run(data.as_ptr(), data.len());
54+
onion_hop_data_run(data.as_ptr(), data.len());
5555
}
5656

5757
#[test]
@@ -63,11 +63,11 @@ fn run_test_cases() {
6363
use std::sync::{atomic, Arc};
6464
{
6565
let data: Vec<u8> = vec![0];
66-
msg_onion_hop_data_run(data.as_ptr(), data.len());
66+
onion_hop_data_run(data.as_ptr(), data.len());
6767
}
6868
let mut threads = Vec::new();
6969
let threads_running = Arc::new(atomic::AtomicUsize::new(0));
70-
if let Ok(tests) = fs::read_dir("test_cases/msg_onion_hop_data") {
70+
if let Ok(tests) = fs::read_dir("test_cases/onion_hop_data") {
7171
for test in tests {
7272
let mut data: Vec<u8> = Vec::new();
7373
let path = test.unwrap().path();
@@ -82,7 +82,7 @@ fn run_test_cases() {
8282

8383
let panic_logger = string_logger.clone();
8484
let res = if ::std::panic::catch_unwind(move || {
85-
msg_onion_hop_data_test(&data, panic_logger);
85+
onion_hop_data_test(&data, panic_logger);
8686
}).is_err() {
8787
Some(string_logger.into_string())
8888
} else { None };

fuzz/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,6 @@ pub mod process_network_graph;
2828
pub mod refund_deser;
2929
pub mod router;
3030
pub mod zbase32;
31+
pub mod onion_hop_data;
3132

3233
pub mod msg_targets;

fuzz/src/msg_targets/gen_target.sh

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ GEN_TEST lightning::ln::msgs::ChannelReady test_msg_simple ""
2020
GEN_TEST lightning::ln::msgs::FundingSigned test_msg_simple ""
2121
GEN_TEST lightning::ln::msgs::GossipTimestampFilter test_msg_simple ""
2222
GEN_TEST lightning::ln::msgs::Init test_msg_simple ""
23-
GEN_TEST lightning::ln::msgs::OnionHopData test_msg_simple ""
2423
GEN_TEST lightning::ln::msgs::OpenChannel test_msg_simple ""
2524
GEN_TEST lightning::ln::msgs::Ping test_msg_simple ""
2625
GEN_TEST lightning::ln::msgs::Pong test_msg_simple ""

fuzz/src/msg_targets/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ pub mod msg_channel_ready;
88
pub mod msg_funding_signed;
99
pub mod msg_gossip_timestamp_filter;
1010
pub mod msg_init;
11-
pub mod msg_onion_hop_data;
1211
pub mod msg_open_channel;
1312
pub mod msg_ping;
1413
pub mod msg_pong;

fuzz/src/msg_targets/msg_onion_hop_data.rs renamed to fuzz/src/onion_hop_data.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,19 @@
1010
// This file is auto-generated by gen_target.sh based on msg_target_template.txt
1111
// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
1212

13-
use crate::msg_targets::utils::VecWriter;
1413
use crate::utils::test_logger;
1514

1615
#[inline]
17-
pub fn msg_onion_hop_data_test<Out: test_logger::Output>(data: &[u8], _out: Out) {
18-
test_msg_simple!(lightning::ln::msgs::OnionHopData, data);
16+
pub fn onion_hop_data_test<Out: test_logger::Output>(data: &[u8], _out: Out) {
17+
use lightning::util::ser::Readable;
18+
let mut r = ::std::io::Cursor::new(data);
19+
let _ = <lightning::ln::msgs::InboundOnionPayload as Readable>::read(&mut r);
1920
}
2021

2122
#[no_mangle]
22-
pub extern "C" fn msg_onion_hop_data_run(data: *const u8, datalen: usize) {
23+
pub extern "C" fn onion_hop_data_run(data: *const u8, datalen: usize) {
24+
use lightning::util::ser::Readable;
2325
let data = unsafe { std::slice::from_raw_parts(data, datalen) };
24-
test_msg_simple!(lightning::ln::msgs::OnionHopData, data);
26+
let mut r = ::std::io::Cursor::new(data);
27+
let _ = <lightning::ln::msgs::InboundOnionPayload as Readable>::read(&mut r);
2528
}

fuzz/targets.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ void refund_deser_run(const unsigned char* data, size_t data_len);
1313
void router_run(const unsigned char* data, size_t data_len);
1414
void zbase32_run(const unsigned char* data, size_t data_len);
1515
void indexedmap_run(const unsigned char* data, size_t data_len);
16+
void onion_hop_data_run(const unsigned char* data, size_t data_len);
1617
void msg_accept_channel_run(const unsigned char* data, size_t data_len);
1718
void msg_announcement_signatures_run(const unsigned char* data, size_t data_len);
1819
void msg_channel_reestablish_run(const unsigned char* data, size_t data_len);
@@ -40,7 +41,6 @@ void msg_gossip_timestamp_filter_run(const unsigned char* data, size_t data_len)
4041
void msg_update_add_htlc_run(const unsigned char* data, size_t data_len);
4142
void msg_error_message_run(const unsigned char* data, size_t data_len);
4243
void msg_channel_update_run(const unsigned char* data, size_t data_len);
43-
void msg_onion_hop_data_run(const unsigned char* data, size_t data_len);
4444
void msg_ping_run(const unsigned char* data, size_t data_len);
4545
void msg_pong_run(const unsigned char* data, size_t data_len);
4646
void msg_channel_details_run(const unsigned char* data, size_t data_len);

lightning/src/ln/channel.rs

+13-12
Original file line numberDiff line numberDiff line change
@@ -3119,9 +3119,9 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
31193119

31203120
let mut htlc_updates = Vec::new();
31213121
mem::swap(&mut htlc_updates, &mut self.context.holding_cell_htlc_updates);
3122-
let mut update_add_htlcs = Vec::with_capacity(htlc_updates.len());
3123-
let mut update_fulfill_htlcs = Vec::with_capacity(htlc_updates.len());
3124-
let mut update_fail_htlcs = Vec::with_capacity(htlc_updates.len());
3122+
let mut update_add_count = 0;
3123+
let mut update_fulfill_count = 0;
3124+
let mut update_fail_count = 0;
31253125
let mut htlcs_to_fail = Vec::new();
31263126
for htlc_update in htlc_updates.drain(..) {
31273127
// Note that this *can* fail, though it should be due to rather-rare conditions on
@@ -3137,7 +3137,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
31373137
match self.send_htlc(amount_msat, *payment_hash, cltv_expiry, source.clone(),
31383138
onion_routing_packet.clone(), false, skimmed_fee_msat, fee_estimator, logger)
31393139
{
3140-
Ok(update_add_msg_option) => update_add_htlcs.push(update_add_msg_option.unwrap()),
3140+
Ok(_) => update_add_count += 1,
31413141
Err(e) => {
31423142
match e {
31433143
ChannelError::Ignore(ref msg) => {
@@ -3164,11 +3164,11 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
31643164
// not fail - any in between attempts to claim the HTLC will have resulted
31653165
// in it hitting the holding cell again and we cannot change the state of a
31663166
// holding cell HTLC from fulfill to anything else.
3167-
let (update_fulfill_msg_option, mut additional_monitor_update) =
3168-
if let UpdateFulfillFetch::NewClaim { msg, monitor_update, .. } = self.get_update_fulfill_htlc(htlc_id, *payment_preimage, logger) {
3169-
(msg, monitor_update)
3170-
} else { unreachable!() };
3171-
update_fulfill_htlcs.push(update_fulfill_msg_option.unwrap());
3167+
let mut additional_monitor_update =
3168+
if let UpdateFulfillFetch::NewClaim { monitor_update, .. } =
3169+
self.get_update_fulfill_htlc(htlc_id, *payment_preimage, logger)
3170+
{ monitor_update } else { unreachable!() };
3171+
update_fulfill_count += 1;
31723172
monitor_update.updates.append(&mut additional_monitor_update.updates);
31733173
},
31743174
&HTLCUpdateAwaitingACK::FailHTLC { htlc_id, ref err_packet } => {
@@ -3179,7 +3179,8 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
31793179
// not fail - we should never end up in a state where we double-fail
31803180
// an HTLC or fail-then-claim an HTLC as it indicates we didn't wait
31813181
// for a full revocation before failing.
3182-
update_fail_htlcs.push(update_fail_msg_option.unwrap())
3182+
debug_assert!(update_fail_msg_option.is_some());
3183+
update_fail_count += 1;
31833184
},
31843185
Err(e) => {
31853186
if let ChannelError::Ignore(_) = e {}
@@ -3191,7 +3192,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
31913192
},
31923193
}
31933194
}
3194-
if update_add_htlcs.is_empty() && update_fulfill_htlcs.is_empty() && update_fail_htlcs.is_empty() && self.context.holding_cell_update_fee.is_none() {
3195+
if update_add_count == 0 && update_fulfill_count == 0 && update_fail_count == 0 && self.context.holding_cell_update_fee.is_none() {
31953196
return (None, htlcs_to_fail);
31963197
}
31973198
let update_fee = if let Some(feerate) = self.context.holding_cell_update_fee.take() {
@@ -3208,7 +3209,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
32083209

32093210
log_debug!(logger, "Freeing holding cell in channel {} resulted in {}{} HTLCs added, {} HTLCs fulfilled, and {} HTLCs failed.",
32103211
log_bytes!(self.context.channel_id()), if update_fee.is_some() { "a fee update, " } else { "" },
3211-
update_add_htlcs.len(), update_fulfill_htlcs.len(), update_fail_htlcs.len());
3212+
update_add_count, update_fulfill_count, update_fail_count);
32123213

32133214
self.monitor_updating_paused(false, true, false, Vec::new(), Vec::new(), Vec::new());
32143215
(self.push_ret_blockable_mon_update(monitor_update), htlcs_to_fail)

0 commit comments

Comments
 (0)