Skip to content

Commit 4ca5bcf

Browse files
committed
Update PendingHTLCStatus to hold malformed HTLC error messages
1 parent 7d6aab7 commit 4ca5bcf

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

src/ln/channel.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crypto::hkdf::{hkdf_extract,hkdf_expand};
1616
use ln::msgs;
1717
use ln::msgs::{ErrorAction, HandleError, MsgEncodable};
1818
use ln::channelmonitor::ChannelMonitor;
19-
use ln::channelmanager::{PendingHTLCStatus, PendingForwardHTLCInfo, HTLCFailReason};
19+
use ln::channelmanager::{PendingHTLCStatus, PendingForwardHTLCInfo, HTLCFailReason, HTLCFailureMsg};
2020
use ln::chan_utils::{TxCreationKeys,HTLCOutputInCommitment,HTLC_SUCCESS_TX_WEIGHT,HTLC_TIMEOUT_TX_WEIGHT};
2121
use ln::chan_utils;
2222
use chain::chaininterface::{FeeEstimator,ConfirmationTarget};
@@ -1681,7 +1681,8 @@ impl Channel {
16811681

16821682
let mut to_forward_infos = Vec::new();
16831683
let mut revoked_htlcs = Vec::new();
1684-
let mut failed_htlcs = Vec::new();
1684+
let mut update_fail_htlcs = Vec::new();
1685+
let mut update_fail_malformed_htlcs = Vec::new();
16851686
let mut require_commitment = false;
16861687
let mut value_to_self_msat_diff: i64 = 0;
16871688
// We really shouldnt have two passes here, but retain gives a non-mutable ref (Rust bug)
@@ -1709,7 +1710,10 @@ impl Channel {
17091710
PendingHTLCStatus::Fail(fail_msg) => {
17101711
htlc.state = HTLCState::LocalRemoved;
17111712
require_commitment = true;
1712-
failed_htlcs.push(fail_msg);
1713+
match fail_msg {
1714+
HTLCFailureMsg::Relay(msg) => update_fail_htlcs.push(msg),
1715+
HTLCFailureMsg::Malformed(msg) => update_fail_malformed_htlcs.push(msg),
1716+
}
17131717
},
17141718
PendingHTLCStatus::Forward(forward_info) => {
17151719
to_forward_infos.push(forward_info);
@@ -1728,10 +1732,14 @@ impl Channel {
17281732

17291733
match self.free_holding_cell_htlcs()? {
17301734
Some(mut commitment_update) => {
1731-
commitment_update.0.update_fail_htlcs.reserve(failed_htlcs.len());
1732-
for fail_msg in failed_htlcs.drain(..) {
1735+
commitment_update.0.update_fail_htlcs.reserve(update_fail_htlcs.len());
1736+
for fail_msg in update_fail_htlcs.drain(..) {
17331737
commitment_update.0.update_fail_htlcs.push(fail_msg);
17341738
}
1739+
commitment_update.0.update_fail_malformed_htlcs.reserve(update_fail_malformed_htlcs.len());
1740+
for fail_msg in update_fail_malformed_htlcs.drain(..) {
1741+
commitment_update.0.update_fail_malformed_htlcs.push(fail_msg);
1742+
}
17351743
Ok((Some(commitment_update.0), to_forward_infos, revoked_htlcs, commitment_update.1))
17361744
},
17371745
None => {
@@ -1740,8 +1748,8 @@ impl Channel {
17401748
Ok((Some(msgs::CommitmentUpdate {
17411749
update_add_htlcs: Vec::new(),
17421750
update_fulfill_htlcs: Vec::new(),
1743-
update_fail_htlcs: failed_htlcs,
1744-
update_fail_malformed_htlcs: Vec::new(),
1751+
update_fail_htlcs,
1752+
update_fail_malformed_htlcs,
17451753
commitment_signed
17461754
}), to_forward_infos, revoked_htlcs, monitor_update))
17471755
} else {

src/ln/channelmanager.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,17 @@ mod channel_held_info {
5050
pub(super) outgoing_cltv_value: u32,
5151
}
5252

53+
#[derive(Clone)] // See Channel::revoke_and_ack for why, tl;dr: Rust bug
54+
pub enum HTLCFailureMsg {
55+
Relay(msgs::UpdateFailHTLC),
56+
Malformed(msgs::UpdateFailMalformedHTLC),
57+
}
58+
5359
/// Stores whether we can't forward an HTLC or relevant forwarding info
5460
#[derive(Clone)] // See Channel::revoke_and_ack for why, tl;dr: Rust bug
5561
pub enum PendingHTLCStatus {
5662
Forward(PendingForwardHTLCInfo),
57-
Fail(msgs::UpdateFailHTLC),
63+
Fail(HTLCFailureMsg),
5864
}
5965

6066
#[cfg(feature = "fuzztarget")]
@@ -699,11 +705,11 @@ impl ChannelManager {
699705
if channel_state.is_none() {
700706
channel_state = Some(self.channel_state.lock().unwrap());
701707
}
702-
return (PendingHTLCStatus::Fail(msgs::UpdateFailHTLC {
708+
return (PendingHTLCStatus::Fail(HTLCFailureMsg::Relay(msgs::UpdateFailHTLC {
703709
channel_id: msg.channel_id,
704710
htlc_id: msg.htlc_id,
705711
reason: ChannelManager::build_first_hop_failure_packet(&shared_secret, $err_code, $data),
706-
}), shared_secret, channel_state.unwrap());
712+
})), shared_secret, channel_state.unwrap());
707713
}
708714
}
709715
}
@@ -1726,11 +1732,11 @@ impl ChannelMessageHandler for ChannelManager {
17261732
}
17271733
if !acceptable_cycle {
17281734
log_info!(self, "Failed to accept incoming HTLC: Payment looped through us twice");
1729-
pending_forward_info = PendingHTLCStatus::Fail(msgs::UpdateFailHTLC {
1735+
pending_forward_info = PendingHTLCStatus::Fail(HTLCFailureMsg::Relay(msgs::UpdateFailHTLC {
17301736
channel_id: msg.channel_id,
17311737
htlc_id: msg.htlc_id,
17321738
reason: ChannelManager::build_first_hop_failure_packet(&shared_secret, 0x4000 | 0x2000 | 2, &[0;0]),
1733-
});
1739+
}));
17341740
} else {
17351741
will_forward = true;
17361742
}

0 commit comments

Comments
 (0)