@@ -59,7 +59,7 @@ use crate::routing::gossip::NodeId;
5959use crate::routing::router::{BlindedTail, InFlightHtlcs, Path, Payee, PaymentParameters, RouteParameters, RouteParametersConfig, Router, FixedRouter, Route};
6060use crate::ln::onion_payment::{check_incoming_htlc_cltv, create_recv_pending_htlc_info, create_fwd_pending_htlc_info, decode_incoming_update_add_htlc_onion, InboundHTLCErr, NextPacketDetails};
6161use crate::ln::msgs;
62- use crate::ln::onion_utils;
62+ use crate::ln::onion_utils::{self, ATTRIBUTION_DATA_LEN} ;
6363use crate::ln::onion_utils::{HTLCFailReason, INVALID_ONION_BLINDING};
6464use crate::ln::msgs::{ChannelMessageHandler, CommitmentUpdate, DecodeError, LightningError};
6565#[cfg(test)]
@@ -4418,6 +4418,7 @@ where
44184418 channel_id: msg.channel_id,
44194419 htlc_id: msg.htlc_id,
44204420 reason: failure.data.clone(),
4421+ attribution_data: failure.attribution_data,
44214422 })
44224423 }
44234424
@@ -4443,10 +4444,12 @@ where
44434444 }
44444445 let failure = HTLCFailReason::reason($err_code, $data.to_vec())
44454446 .get_encrypted_failure_packet(&shared_secret, &None);
4447+
44464448 return PendingHTLCStatus::Fail(HTLCFailureMsg::Relay(msgs::UpdateFailHTLC {
44474449 channel_id: msg.channel_id,
44484450 htlc_id: msg.htlc_id,
44494451 reason: failure.data,
4452+ attribution_data: failure.attribution_data,
44504453 }));
44514454 }
44524455 }
@@ -12837,11 +12840,15 @@ impl_writeable_tlv_based!(PendingHTLCInfo, {
1283712840impl Writeable for HTLCFailureMsg {
1283812841 fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
1283912842 match self {
12840- HTLCFailureMsg::Relay(msgs::UpdateFailHTLC { channel_id, htlc_id, reason }) => {
12843+ HTLCFailureMsg::Relay(msgs::UpdateFailHTLC { channel_id, htlc_id, reason, attribution_data }) => {
1284112844 0u8.write(writer)?;
1284212845 channel_id.write(writer)?;
1284312846 htlc_id.write(writer)?;
1284412847 reason.write(writer)?;
12848+
12849+ // This code will only ever be hit for legacy data that is re-serialized. It isn't necessary to try
12850+ // writing out attribution data, because it can never be present.
12851+ debug_assert!(attribution_data.is_none());
1284512852 },
1284612853 HTLCFailureMsg::Malformed(msgs::UpdateFailMalformedHTLC {
1284712854 channel_id, htlc_id, sha256_of_onion, failure_code
@@ -12866,6 +12873,7 @@ impl Readable for HTLCFailureMsg {
1286612873 channel_id: Readable::read(reader)?,
1286712874 htlc_id: Readable::read(reader)?,
1286812875 reason: Readable::read(reader)?,
12876+ attribution_data: None,
1286912877 }))
1287012878 },
1287112879 1 => {
@@ -13096,6 +13104,7 @@ impl Writeable for HTLCForwardInfo {
1309613104 write_tlv_fields!(w, {
1309713105 (0, htlc_id, required),
1309813106 (2, err_packet.data, required),
13107+ (5, err_packet.attribution_data, option),
1309913108 });
1310013109 },
1310113110 Self::FailMalformedHTLC { htlc_id, failure_code, sha256_of_onion } => {
@@ -13126,8 +13135,12 @@ impl Readable for HTLCForwardInfo {
1312613135 (1, malformed_htlc_failure_code, option),
1312713136 (2, err_packet, required),
1312813137 (3, sha256_of_onion, option),
13138+ (5, attribution_data, option),
1312913139 });
1313013140 if let Some(failure_code) = malformed_htlc_failure_code {
13141+ if attribution_data.is_some() {
13142+ return Err(DecodeError::InvalidValue);
13143+ }
1313113144 Self::FailMalformedHTLC {
1313213145 htlc_id: _init_tlv_based_struct_field!(htlc_id, required),
1313313146 failure_code,
@@ -13138,6 +13151,7 @@ impl Readable for HTLCForwardInfo {
1313813151 htlc_id: _init_tlv_based_struct_field!(htlc_id, required),
1313913152 err_packet: crate::ln::msgs::OnionErrorPacket {
1314013153 data: _init_tlv_based_struct_field!(err_packet, required),
13154+ attribution_data: _init_tlv_based_struct_field!(attribution_data, option),
1314113155 },
1314213156 }
1314313157 }
@@ -14837,7 +14851,8 @@ mod tests {
1483714851 use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
1483814852 use core::sync::atomic::Ordering;
1483914853 use crate::events::{Event, HTLCDestination, MessageSendEvent, MessageSendEventsProvider, ClosureReason};
14840- use crate::ln::types::ChannelId;
14854+ use crate::ln::onion_utils::ATTRIBUTION_DATA_LEN;
14855+ use crate::ln::types::ChannelId;
1484114856 use crate::types::payment::{PaymentPreimage, PaymentHash, PaymentSecret};
1484214857 use crate::ln::channelmanager::{RAACommitmentOrder, create_recv_pending_htlc_info, inbound_payment, ChannelConfigOverrides, HTLCForwardInfo, InterceptId, PaymentId, RecipientOnionFields};
1484314858 use crate::ln::functional_test_utils::*;
@@ -15311,6 +15326,80 @@ mod tests {
1531115326 nodes[1].logger.assert_log_contains("lightning::ln::channelmanager", "Payment preimage didn't match payment hash", 1);
1531215327 }
1531315328
15329+ #[test]
15330+ fn test_htlc_localremoved_persistence() {
15331+ let chanmon_cfgs: Vec<TestChanMonCfg> = create_chanmon_cfgs(2);
15332+ let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
15333+
15334+ let persister;
15335+ let chain_monitor;
15336+ let deserialized_chanmgr;
15337+
15338+ // Send a keysend payment that fails because of a preimage mismatch.
15339+ let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
15340+ let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
15341+
15342+ let payer_pubkey = nodes[0].node.get_our_node_id();
15343+ let payee_pubkey = nodes[1].node.get_our_node_id();
15344+
15345+ let _chan = create_chan_between_nodes(&nodes[0], &nodes[1]);
15346+ let route_params = RouteParameters::from_payment_params_and_value(
15347+ PaymentParameters::for_keysend(payee_pubkey, 40, false), 10_000);
15348+ let network_graph = nodes[0].network_graph;
15349+ let first_hops = nodes[0].node.list_usable_channels();
15350+ let scorer = test_utils::TestScorer::new();
15351+ let random_seed_bytes = chanmon_cfgs[1].keys_manager.get_secure_random_bytes();
15352+ let route = find_route(
15353+ &payer_pubkey, &route_params, &network_graph, Some(&first_hops.iter().collect::<Vec<_>>()),
15354+ nodes[0].logger, &scorer, &Default::default(), &random_seed_bytes
15355+ ).unwrap();
15356+
15357+ let test_preimage = PaymentPreimage([42; 32]);
15358+ let mismatch_payment_hash = PaymentHash([43; 32]);
15359+ let session_privs = nodes[0].node.test_add_new_pending_payment(mismatch_payment_hash,
15360+ RecipientOnionFields::spontaneous_empty(), PaymentId(mismatch_payment_hash.0), &route).unwrap();
15361+ nodes[0].node.test_send_payment_internal(&route, mismatch_payment_hash,
15362+ RecipientOnionFields::spontaneous_empty(), Some(test_preimage), PaymentId(mismatch_payment_hash.0), None, session_privs).unwrap();
15363+ check_added_monitors!(nodes[0], 1);
15364+
15365+ let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
15366+ nodes[1].node.handle_update_add_htlc(nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
15367+ commitment_signed_dance!(nodes[1], nodes[0], &updates.commitment_signed, false);
15368+ expect_pending_htlcs_forwardable!(nodes[1]);
15369+ expect_htlc_handling_failed_destinations!(nodes[1].node.get_and_clear_pending_events(), &[HTLCDestination::FailedPayment { payment_hash: mismatch_payment_hash }]);
15370+ check_added_monitors(&nodes[1], 1);
15371+
15372+ // Save the update_fail_htlc message for later comparison.
15373+ let msgs = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
15374+ let htlc_fail_msg = msgs.update_fail_htlcs[0].clone();
15375+
15376+ // Reload node.
15377+ nodes[0].node.peer_disconnected(nodes[1].node.get_our_node_id());
15378+ nodes[1].node.peer_disconnected(nodes[0].node.get_our_node_id());
15379+
15380+ let monitor_encoded = get_monitor!(nodes[1], _chan.3).encode();
15381+ reload_node!(nodes[1], nodes[1].node.encode(), &[&monitor_encoded], persister, chain_monitor, deserialized_chanmgr);
15382+
15383+ nodes[0].node.peer_connected(nodes[1].node.get_our_node_id(), &msgs::Init {
15384+ features: nodes[1].node.init_features(), networks: None, remote_network_address: None
15385+ }, true).unwrap();
15386+ let reestablish_1 = get_chan_reestablish_msgs!(nodes[0], nodes[1]);
15387+ assert_eq!(reestablish_1.len(), 1);
15388+ nodes[1].node.peer_connected(nodes[0].node.get_our_node_id(), &msgs::Init {
15389+ features: nodes[0].node.init_features(), networks: None, remote_network_address: None
15390+ }, false).unwrap();
15391+ let reestablish_2 = get_chan_reestablish_msgs!(nodes[1], nodes[0]);
15392+ assert_eq!(reestablish_2.len(), 1);
15393+ nodes[0].node.handle_channel_reestablish(nodes[1].node.get_our_node_id(), &reestablish_2[0]);
15394+ handle_chan_reestablish_msgs!(nodes[0], nodes[1]);
15395+ nodes[1].node.handle_channel_reestablish(nodes[0].node.get_our_node_id(), &reestablish_1[0]);
15396+
15397+ // Assert that same failure message is resent after reload.
15398+ let msgs = handle_chan_reestablish_msgs!(nodes[1], nodes[0]);
15399+ let htlc_fail_msg_after_reload = msgs.2.unwrap().update_fail_htlcs[0].clone();
15400+ assert_eq!(htlc_fail_msg, htlc_fail_msg_after_reload);
15401+ }
15402+
1531415403 #[test]
1531515404 fn test_multi_hop_missing_secret() {
1531615405 let chanmon_cfgs = create_chanmon_cfgs(4);
@@ -16269,7 +16358,7 @@ mod tests {
1626916358 let mut nodes = create_network(1, &node_cfg, &chanmgrs);
1627016359
1627116360 let dummy_failed_htlc = |htlc_id| {
16272- HTLCForwardInfo::FailHTLC { htlc_id, err_packet: msgs::OnionErrorPacket { data: vec![42] } }
16361+ HTLCForwardInfo::FailHTLC { htlc_id, err_packet: msgs::OnionErrorPacket { data: vec![42], attribution_data: Some([0; ATTRIBUTION_DATA_LEN]) } }
1627316362 };
1627416363 let dummy_malformed_htlc = |htlc_id| {
1627516364 HTLCForwardInfo::FailMalformedHTLC { htlc_id, failure_code: 0x4000, sha256_of_onion: [0; 32] }
0 commit comments