Skip to content

Commit 8699058

Browse files
valentinewallaceyellowred
authored andcommitted
Add test coverage for serialization of malformed HTLCs.
in Channel and ChannelManager.
1 parent c299765 commit 8699058

File tree

3 files changed

+85
-9
lines changed

3 files changed

+85
-9
lines changed

lightning/src/ln/channel.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8288,6 +8288,7 @@ mod tests {
82888288
use bitcoin::blockdata::transaction::{Transaction, TxOut};
82898289
use bitcoin::blockdata::opcodes;
82908290
use bitcoin::network::constants::Network;
8291+
use crate::ln::onion_utils::INVALID_ONION_BLINDING;
82918292
use crate::ln::{PaymentHash, PaymentPreimage};
82928293
use crate::ln::channel_keys::{RevocationKey, RevocationBasepoint};
82938294
use crate::ln::channelmanager::{self, HTLCSource, PaymentId};
@@ -8824,8 +8825,9 @@ mod tests {
88248825
}
88258826

88268827
#[test]
8827-
fn blinding_point_skimmed_fee_ser() {
8828-
// Ensure that channel blinding points and skimmed fees are (de)serialized properly.
8828+
fn blinding_point_skimmed_fee_malformed_ser() {
8829+
// Ensure that channel blinding points, skimmed fees, and malformed HTLCs are (de)serialized
8830+
// properly.
88298831
let feeest = LowerBoundedFeeEstimator::new(&TestFeeEstimator{fee_est: 15000});
88308832
let secp_ctx = Secp256k1::new();
88318833
let seed = [42; 32];
@@ -8890,13 +8892,19 @@ mod tests {
88908892
payment_preimage: PaymentPreimage([42; 32]),
88918893
htlc_id: 0,
88928894
};
8893-
let mut holding_cell_htlc_updates = Vec::with_capacity(10);
8894-
for i in 0..10 {
8895-
if i % 3 == 0 {
8895+
let dummy_holding_cell_failed_htlc = |htlc_id| HTLCUpdateAwaitingACK::FailHTLC {
8896+
htlc_id, err_packet: msgs::OnionErrorPacket { data: vec![42] }
8897+
};
8898+
let dummy_holding_cell_malformed_htlc = |htlc_id| HTLCUpdateAwaitingACK::FailMalformedHTLC {
8899+
htlc_id, failure_code: INVALID_ONION_BLINDING, sha256_of_onion: [0; 32],
8900+
};
8901+
let mut holding_cell_htlc_updates = Vec::with_capacity(12);
8902+
for i in 0..12 {
8903+
if i % 5 == 0 {
88968904
holding_cell_htlc_updates.push(dummy_holding_cell_add_htlc.clone());
8897-
} else if i % 3 == 1 {
8905+
} else if i % 5 == 1 {
88988906
holding_cell_htlc_updates.push(dummy_holding_cell_claim_htlc.clone());
8899-
} else {
8907+
} else if i % 5 == 2 {
89008908
let mut dummy_add = dummy_holding_cell_add_htlc.clone();
89018909
if let HTLCUpdateAwaitingACK::AddHTLC {
89028910
ref mut blinding_point, ref mut skimmed_fee_msat, ..
@@ -8905,6 +8913,10 @@ mod tests {
89058913
*skimmed_fee_msat = Some(42);
89068914
} else { panic!() }
89078915
holding_cell_htlc_updates.push(dummy_add);
8916+
} else if i % 5 == 3 {
8917+
holding_cell_htlc_updates.push(dummy_holding_cell_malformed_htlc(i as u64));
8918+
} else {
8919+
holding_cell_htlc_updates.push(dummy_holding_cell_failed_htlc(i as u64));
89088920
}
89098921
}
89108922
chan.context.holding_cell_htlc_updates = holding_cell_htlc_updates.clone();

lightning/src/ln/channelmanager.rs

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ use crate::ln::script::ShutdownScript;
111111

112112
/// Information about where a received HTLC('s onion) has indicated the HTLC should go.
113113
#[derive(Clone)] // See Channel::revoke_and_ack for why, tl;dr: Rust bug
114+
#[cfg_attr(test, derive(Debug, PartialEq))]
114115
pub enum PendingHTLCRouting {
115116
/// An HTLC which should be forwarded on to another node.
116117
Forward {
@@ -189,7 +190,7 @@ pub enum PendingHTLCRouting {
189190
}
190191

191192
/// Information used to forward or fail this HTLC that is being forwarded within a blinded path.
192-
#[derive(Clone, Copy, Hash, PartialEq, Eq)]
193+
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
193194
pub struct BlindedForward {
194195
/// The `blinding_point` that was set in the inbound [`msgs::UpdateAddHTLC`], or in the inbound
195196
/// onion payload if we're the introduction node. Useful for calculating the next hop's
@@ -213,6 +214,7 @@ impl PendingHTLCRouting {
213214
/// Information about an incoming HTLC, including the [`PendingHTLCRouting`] describing where it
214215
/// should go next.
215216
#[derive(Clone)] // See Channel::revoke_and_ack for why, tl;dr: Rust bug
217+
#[cfg_attr(test, derive(Debug, PartialEq))]
216218
pub struct PendingHTLCInfo {
217219
/// Further routing details based on whether the HTLC is being forwarded or received.
218220
pub routing: PendingHTLCRouting,
@@ -267,6 +269,7 @@ pub(super) enum PendingHTLCStatus {
267269
Fail(HTLCFailureMsg),
268270
}
269271

272+
#[cfg_attr(test, derive(Clone, Debug, PartialEq))]
270273
pub(super) struct PendingAddHTLCInfo {
271274
pub(super) forward_info: PendingHTLCInfo,
272275

@@ -282,6 +285,7 @@ pub(super) struct PendingAddHTLCInfo {
282285
prev_user_channel_id: u128,
283286
}
284287

288+
#[cfg_attr(test, derive(Clone, Debug, PartialEq))]
285289
pub(super) enum HTLCForwardInfo {
286290
AddHTLC(PendingAddHTLCInfo),
287291
FailHTLC {
@@ -11053,12 +11057,14 @@ mod tests {
1105311057
use crate::events::{Event, HTLCDestination, MessageSendEvent, MessageSendEventsProvider, ClosureReason};
1105411058
use crate::ln::{PaymentPreimage, PaymentHash, PaymentSecret};
1105511059
use crate::ln::ChannelId;
11056-
use crate::ln::channelmanager::{create_recv_pending_htlc_info, inbound_payment, PaymentId, PaymentSendFailure, RecipientOnionFields, InterceptId};
11060+
use crate::ln::channelmanager::{create_recv_pending_htlc_info, HTLCForwardInfo, inbound_payment, PaymentId, PaymentSendFailure, RecipientOnionFields, InterceptId};
1105711061
use crate::ln::functional_test_utils::*;
1105811062
use crate::ln::msgs::{self, ErrorAction};
1105911063
use crate::ln::msgs::ChannelMessageHandler;
11064+
use crate::prelude::*;
1106011065
use crate::routing::router::{PaymentParameters, RouteParameters, find_route};
1106111066
use crate::util::errors::APIError;
11067+
use crate::util::ser::Writeable;
1106211068
use crate::util::test_utils;
1106311069
use crate::util::config::{ChannelConfig, ChannelConfigUpdate};
1106411070
use crate::sign::EntropySource;
@@ -12333,6 +12339,63 @@ mod tests {
1233312339
check_spends!(txn[0], funding_tx);
1233412340
}
1233512341
}
12342+
12343+
#[test]
12344+
fn test_malformed_forward_htlcs_ser() {
12345+
// Ensure that `HTLCForwardInfo::FailMalformedHTLC`s are (de)serialized properly.
12346+
let chanmon_cfg = create_chanmon_cfgs(1);
12347+
let node_cfg = create_node_cfgs(1, &chanmon_cfg);
12348+
let persister;
12349+
let chain_monitor;
12350+
let chanmgrs = create_node_chanmgrs(1, &node_cfg, &[None]);
12351+
let deserialized_chanmgr;
12352+
let mut nodes = create_network(1, &node_cfg, &chanmgrs);
12353+
12354+
let dummy_failed_htlc = |htlc_id| {
12355+
HTLCForwardInfo::FailHTLC { htlc_id, err_packet: msgs::OnionErrorPacket { data: vec![42] }, }
12356+
};
12357+
let dummy_malformed_htlc = |htlc_id| {
12358+
HTLCForwardInfo::FailMalformedHTLC { htlc_id, failure_code: 0x4000, sha256_of_onion: [0; 32] }
12359+
};
12360+
12361+
let dummy_htlcs_1: Vec<HTLCForwardInfo> = (1..10).map(|htlc_id| {
12362+
if htlc_id % 2 == 0 {
12363+
dummy_failed_htlc(htlc_id)
12364+
} else {
12365+
dummy_malformed_htlc(htlc_id)
12366+
}
12367+
}).collect();
12368+
12369+
let dummy_htlcs_2: Vec<HTLCForwardInfo> = (1..10).map(|htlc_id| {
12370+
if htlc_id % 2 == 1 {
12371+
dummy_failed_htlc(htlc_id)
12372+
} else {
12373+
dummy_malformed_htlc(htlc_id)
12374+
}
12375+
}).collect();
12376+
12377+
12378+
let (scid_1, scid_2) = (42, 43);
12379+
let mut forward_htlcs = HashMap::new();
12380+
forward_htlcs.insert(scid_1, dummy_htlcs_1.clone());
12381+
forward_htlcs.insert(scid_2, dummy_htlcs_2.clone());
12382+
12383+
let mut chanmgr_fwd_htlcs = nodes[0].node.forward_htlcs.lock().unwrap();
12384+
*chanmgr_fwd_htlcs = forward_htlcs.clone();
12385+
core::mem::drop(chanmgr_fwd_htlcs);
12386+
12387+
reload_node!(nodes[0], nodes[0].node.encode(), &[], persister, chain_monitor, deserialized_chanmgr);
12388+
12389+
let mut deserialized_fwd_htlcs = nodes[0].node.forward_htlcs.lock().unwrap();
12390+
for scid in [scid_1, scid_2].iter() {
12391+
let deserialized_htlcs = deserialized_fwd_htlcs.remove(scid).unwrap();
12392+
assert_eq!(forward_htlcs.remove(scid).unwrap(), deserialized_htlcs);
12393+
}
12394+
assert!(deserialized_fwd_htlcs.is_empty());
12395+
core::mem::drop(deserialized_fwd_htlcs);
12396+
12397+
expect_pending_htlcs_forwardable!(nodes[0]);
12398+
}
1233612399
}
1233712400

1233812401
#[cfg(ldk_bench)]

lightning/src/ln/msgs.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1678,6 +1678,7 @@ mod fuzzy_internal_msgs {
16781678
// These types aren't intended to be pub, but are exposed for direct fuzzing (as we deserialize
16791679
// them from untrusted input):
16801680
#[derive(Clone)]
1681+
#[cfg_attr(test, derive(Debug, PartialEq))]
16811682
pub struct FinalOnionHopData {
16821683
pub payment_secret: PaymentSecret,
16831684
/// The total value, in msat, of the payment as received by the ultimate recipient.

0 commit comments

Comments
 (0)