Skip to content

Commit f4b14ad

Browse files
committed
extend fail htlc msg
1 parent 2084280 commit f4b14ad

File tree

8 files changed

+187
-64
lines changed

8 files changed

+187
-64
lines changed

lightning/src/ln/channel.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4672,7 +4672,7 @@ trait FailHTLCContents {
46724672
impl FailHTLCContents for msgs::OnionErrorPacket {
46734673
type Message = msgs::UpdateFailHTLC;
46744674
fn to_message(self, htlc_id: u64, channel_id: ChannelId) -> Self::Message {
4675-
msgs::UpdateFailHTLC { htlc_id, channel_id, reason: self }
4675+
msgs::UpdateFailHTLC { htlc_id, channel_id, reason: self.data, attribution_data: Some(self.attribution_data) }
46764676
}
46774677
fn to_inbound_htlc_state(self) -> InboundHTLCState {
46784678
InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::FailRelay(self))
@@ -5991,7 +5991,7 @@ impl<SP: Deref> FundedChannel<SP> where
59915991
require_commitment = true;
59925992
match fail_msg {
59935993
HTLCFailureMsg::Relay(msg) => {
5994-
htlc.state = InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::FailRelay(msg.reason.clone()));
5994+
htlc.state = InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::FailRelay((&msg).into()));
59955995
update_fail_htlcs.push(msg)
59965996
},
59975997
HTLCFailureMsg::Malformed(msg) => {
@@ -6699,7 +6699,8 @@ impl<SP: Deref> FundedChannel<SP> where
66996699
update_fail_htlcs.push(msgs::UpdateFailHTLC {
67006700
channel_id: self.context.channel_id(),
67016701
htlc_id: htlc.htlc_id,
6702-
reason: err_packet.clone()
6702+
reason: err_packet.data.clone(),
6703+
attribution_data: Some(err_packet.attribution_data)
67036704
});
67046705
},
67056706
&InboundHTLCRemovalReason::FailMalformed((ref sha256_of_onion, ref failure_code)) => {
@@ -10061,7 +10062,7 @@ impl<SP: Deref> Writeable for FundedChannel<SP> where SP::Target: SignerProvider
1006110062
// `::FailHTLC` variant and write the real malformed error as an optional TLV.
1006210063
malformed_htlcs.push((htlc_id, failure_code, sha256_of_onion));
1006310064

10064-
let dummy_err_packet = msgs::OnionErrorPacket { data: Vec::new() };
10065+
let dummy_err_packet = msgs::OnionErrorPacket { data: Vec::new(), attribution_data: [0; 940] };
1006510066
2u8.write(writer)?;
1006610067
htlc_id.write(writer)?;
1006710068
dummy_err_packet.write(writer)?;
@@ -11500,7 +11501,7 @@ mod tests {
1150011501
htlc_id: 0,
1150111502
};
1150211503
let dummy_holding_cell_failed_htlc = |htlc_id| HTLCUpdateAwaitingACK::FailHTLC {
11503-
htlc_id, err_packet: msgs::OnionErrorPacket { data: vec![42] }
11504+
htlc_id, err_packet: msgs::OnionErrorPacket { data: vec![42], attribution_data: [0; 940] }
1150411505
};
1150511506
let dummy_holding_cell_malformed_htlc = |htlc_id| HTLCUpdateAwaitingACK::FailMalformedHTLC {
1150611507
htlc_id, failure_code: INVALID_ONION_BLINDING, sha256_of_onion: [0; 32],

lightning/src/ln/channelmanager.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4411,11 +4411,13 @@ where
44114411
} else {
44124412
(err_code, &res.0[..])
44134413
};
4414+
let failure = HTLCFailReason::reason(err_code, err_data.to_vec())
4415+
.get_encrypted_failure_packet(shared_secret, &None);
44144416
HTLCFailureMsg::Relay(msgs::UpdateFailHTLC {
44154417
channel_id: msg.channel_id,
44164418
htlc_id: msg.htlc_id,
4417-
reason: HTLCFailReason::reason(err_code, err_data.to_vec())
4418-
.get_encrypted_failure_packet(shared_secret, &None),
4419+
reason: failure.data.clone(),
4420+
attribution_data: Some(failure.attribution_data)
44194421
})
44204422
}
44214423

@@ -4439,11 +4441,14 @@ where
44394441
}
44404442
))
44414443
}
4444+
let failure = HTLCFailReason::reason($err_code, $data.to_vec())
4445+
.get_encrypted_failure_packet(&shared_secret, &None);
4446+
44424447
return PendingHTLCStatus::Fail(HTLCFailureMsg::Relay(msgs::UpdateFailHTLC {
44434448
channel_id: msg.channel_id,
44444449
htlc_id: msg.htlc_id,
4445-
reason: HTLCFailReason::reason($err_code, $data.to_vec())
4446-
.get_encrypted_failure_packet(&shared_secret, &None),
4450+
reason: failure.data,
4451+
attribution_data: Some(failure.attribution_data)
44474452
}));
44484453
}
44494454
}
@@ -5782,7 +5787,7 @@ where
57825787
let failure = match htlc_fail {
57835788
HTLCFailureMsg::Relay(fail_htlc) => HTLCForwardInfo::FailHTLC {
57845789
htlc_id: fail_htlc.htlc_id,
5785-
err_packet: fail_htlc.reason,
5790+
err_packet: (&fail_htlc).into(),
57865791
},
57875792
HTLCFailureMsg::Malformed(fail_malformed_htlc) => HTLCForwardInfo::FailMalformedHTLC {
57885793
htlc_id: fail_malformed_htlc.htlc_id,
@@ -12852,11 +12857,12 @@ impl_writeable_tlv_based!(PendingHTLCInfo, {
1285212857
impl Writeable for HTLCFailureMsg {
1285312858
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
1285412859
match self {
12855-
HTLCFailureMsg::Relay(msgs::UpdateFailHTLC { channel_id, htlc_id, reason }) => {
12860+
HTLCFailureMsg::Relay(msgs::UpdateFailHTLC { channel_id, htlc_id, reason, attribution_data }) => {
1285612861
0u8.write(writer)?;
1285712862
channel_id.write(writer)?;
1285812863
htlc_id.write(writer)?;
1285912864
reason.write(writer)?;
12865+
attribution_data.write(writer)?; // TODO: Make TLV?
1286012866
},
1286112867
HTLCFailureMsg::Malformed(msgs::UpdateFailMalformedHTLC {
1286212868
channel_id, htlc_id, sha256_of_onion, failure_code
@@ -12881,6 +12887,7 @@ impl Readable for HTLCFailureMsg {
1288112887
channel_id: Readable::read(reader)?,
1288212888
htlc_id: Readable::read(reader)?,
1288312889
reason: Readable::read(reader)?,
12890+
attribution_data: Readable::read(reader)?,
1288412891
}))
1288512892
},
1288612893
1 => {
@@ -13118,7 +13125,7 @@ impl Writeable for HTLCForwardInfo {
1311813125
// packet so older versions have something to fail back with, but serialize the real data as
1311913126
// optional TLVs for the benefit of newer versions.
1312013127
FAIL_HTLC_VARIANT_ID.write(w)?;
13121-
let dummy_err_packet = msgs::OnionErrorPacket { data: Vec::new() };
13128+
let dummy_err_packet = msgs::OnionErrorPacket { data: Vec::new(), attribution_data: [0; 940] };
1312213129
write_tlv_fields!(w, {
1312313130
(0, htlc_id, required),
1312413131
(1, failure_code, required),
@@ -15073,8 +15080,8 @@ mod tests {
1507315080
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1507415081

1507515082
create_announced_chan_between_nodes(&nodes, 0, 1);
15076-
15077-
// Since we do not send peer storage, we manually simulate receiving a dummy
15083+
15084+
// Since we do not send peer storage, we manually simulate receiving a dummy
1507815085
// `PeerStorage` from the channel partner.
1507915086
nodes[0].node.handle_peer_storage(nodes[1].node.get_our_node_id(), msgs::PeerStorage{data: vec![0; 100]});
1508015087

@@ -16283,7 +16290,7 @@ mod tests {
1628316290
let mut nodes = create_network(1, &node_cfg, &chanmgrs);
1628416291

1628516292
let dummy_failed_htlc = |htlc_id| {
16286-
HTLCForwardInfo::FailHTLC { htlc_id, err_packet: msgs::OnionErrorPacket { data: vec![42] }, }
16293+
HTLCForwardInfo::FailHTLC { htlc_id, err_packet: msgs::OnionErrorPacket { data: vec![42], attribution_data: [0; 940] } }
1628716294
};
1628816295
let dummy_malformed_htlc = |htlc_id| {
1628916296
HTLCForwardInfo::FailMalformedHTLC { htlc_id, failure_code: 0x4000, sha256_of_onion: [0; 32] }

lightning/src/ln/functional_tests.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7047,7 +7047,8 @@ fn test_update_fulfill_htlc_bolt2_update_fail_htlc_before_commitment() {
70477047
let update_msg = msgs::UpdateFailHTLC{
70487048
channel_id: chan.2,
70497049
htlc_id: 0,
7050-
reason: msgs::OnionErrorPacket { data: Vec::new()},
7050+
reason: Vec::new(),
7051+
attribution_data: Some([0; 940])
70517052
};
70527053

70537054
nodes[0].node.handle_update_fail_htlc(nodes[1].node.get_our_node_id(), &update_msg);

lightning/src/ln/msgs.rs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ pub struct UpdateFulfillHTLC {
729729
/// A [`peer_storage`] message that can be sent to or received from a peer.
730730
///
731731
/// This message is used to distribute backup data to peers.
732-
/// If data is lost or corrupted, users can retrieve it through [`PeerStorageRetrieval`]
732+
/// If data is lost or corrupted, users can retrieve it through [`PeerStorageRetrieval`]
733733
/// to recover critical information, such as channel states, for fund recovery.
734734
///
735735
/// [`peer_storage`] is used to send our own encrypted backup data to a peer.
@@ -744,7 +744,7 @@ pub struct PeerStorage {
744744
/// A [`peer_storage_retrieval`] message that can be sent to or received from a peer.
745745
///
746746
/// This message is sent to peers for whom we store backup data.
747-
/// If we receive this message, it indicates that the peer had stored our backup data.
747+
/// If we receive this message, it indicates that the peer had stored our backup data.
748748
/// This data can be used for fund recovery in case of data loss.
749749
///
750750
/// [`peer_storage_retrieval`] is used to send the most recent backup of the peer.
@@ -765,9 +765,9 @@ pub struct UpdateFailHTLC {
765765
pub channel_id: ChannelId,
766766
/// The HTLC ID
767767
pub htlc_id: u64,
768-
pub(crate) reason: OnionErrorPacket,
768+
pub(crate) reason: Vec<u8>,
769+
pub attribution_data: Option<[u8; 940]>
769770
}
770-
771771
/// An [`update_fail_malformed_htlc`] message to be sent to or received from a peer.
772772
///
773773
/// [`update_fail_malformed_htlc`]: https://github.com/lightning/bolts/blob/master/02-peer-protocol.md#removing-an-htlc-update_fulfill_htlc-update_fail_htlc-and-update_fail_malformed_htlc
@@ -2043,6 +2043,16 @@ pub(crate) struct OnionErrorPacket {
20432043
// This really should be a constant size slice, but the spec lets these things be up to 128KB?
20442044
// (TODO) We limit it in decode to much lower...
20452045
pub(crate) data: Vec<u8>,
2046+
pub(crate) attribution_data: [u8; 940]
2047+
}
2048+
2049+
impl From<&UpdateFailHTLC> for OnionErrorPacket {
2050+
fn from(msg: &UpdateFailHTLC) -> Self {
2051+
OnionErrorPacket {
2052+
data: msg.reason.clone(),
2053+
attribution_data: msg.attribution_data.unwrap(), // TODO: Make safe
2054+
}
2055+
}
20462056
}
20472057

20482058
impl fmt::Display for DecodeError {
@@ -2670,7 +2680,9 @@ impl_writeable_msg!(UpdateFailHTLC, {
26702680
channel_id,
26712681
htlc_id,
26722682
reason
2673-
}, {});
2683+
}, {
2684+
(4333, attribution_data, option)
2685+
});
26742686

26752687
impl_writeable_msg!(UpdateFailMalformedHTLC, {
26762688
channel_id,
@@ -2702,7 +2714,8 @@ impl_writeable_msg!(PeerStorageRetrieval, {
27022714
// serialization format in a way which assumes we know the total serialized length/message end
27032715
// position.
27042716
impl_writeable!(OnionErrorPacket, {
2705-
data
2717+
data,
2718+
attribution_data
27062719
});
27072720

27082721
// Note that this is written as a part of ChannelManager objects, and thus cannot change its
@@ -4450,13 +4463,11 @@ mod tests {
44504463

44514464
#[test]
44524465
fn encoding_update_fail_htlc() {
4453-
let reason = OnionErrorPacket {
4454-
data: [1; 32].to_vec(),
4455-
};
44564466
let update_fail_htlc = msgs::UpdateFailHTLC {
44574467
channel_id: ChannelId::from_bytes([2; 32]),
44584468
htlc_id: 2316138423780173,
4459-
reason
4469+
reason: [1; 32].to_vec(),
4470+
attribution_data: Some([0; 940])
44604471
};
44614472
let encoded_value = update_fail_htlc.encode();
44624473
let target_value = <Vec<u8>>::from_hex("020202020202020202020202020202020202020202020202020202020202020200083a840000034d00200101010101010101010101010101010101010101010101010101010101010101").unwrap();

lightning/src/ln/onion_payment.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ where
289289
).map_err(|e| {
290290
let (err_code, err_data) = match e {
291291
HTLCFailureMsg::Malformed(m) => (m.failure_code, Vec::new()),
292-
HTLCFailureMsg::Relay(r) => (0x4000 | 22, r.reason.data),
292+
HTLCFailureMsg::Relay(r) => (0x4000 | 22, r.reason),
293293
};
294294
let msg = "Failed to decode update add htlc onion";
295295
InboundHTLCErr { msg, err_code, err_data }
@@ -400,11 +400,13 @@ where
400400
}
401401

402402
log_info!(logger, "Failed to accept/forward incoming HTLC: {}", message);
403+
let failure = HTLCFailReason::reason(err_code, data.to_vec())
404+
.get_encrypted_failure_packet(&shared_secret, &None);
403405
return Err(HTLCFailureMsg::Relay(msgs::UpdateFailHTLC {
404406
channel_id: msg.channel_id,
405407
htlc_id: msg.htlc_id,
406-
reason: HTLCFailReason::reason(err_code, data.to_vec())
407-
.get_encrypted_failure_packet(&shared_secret, &None),
408+
reason: failure.data,
409+
attribution_data: Some(failure.attribution_data)
408410
}));
409411
};
410412

0 commit comments

Comments
 (0)