@@ -59,7 +59,7 @@ use crate::routing::gossip::NodeId;
59
59
use crate::routing::router::{BlindedTail, InFlightHtlcs, Path, Payee, PaymentParameters, RouteParameters, RouteParametersConfig, Router, FixedRouter, Route};
60
60
use 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};
61
61
use crate::ln::msgs;
62
- use crate::ln::onion_utils;
62
+ use crate::ln::onion_utils::{self, ATTRIBUTION_DATA_LEN} ;
63
63
use crate::ln::onion_utils::{HTLCFailReason, INVALID_ONION_BLINDING};
64
64
use crate::ln::msgs::{BaseMessageHandler, ChannelMessageHandler, CommitmentUpdate, DecodeError, LightningError, MessageSendEvent};
65
65
#[cfg(test)]
@@ -4433,6 +4433,7 @@ where
4433
4433
channel_id: msg.channel_id,
4434
4434
htlc_id: msg.htlc_id,
4435
4435
reason: failure.data.clone(),
4436
+ attribution_data: failure.attribution_data,
4436
4437
})
4437
4438
}
4438
4439
@@ -4458,10 +4459,12 @@ where
4458
4459
}
4459
4460
let failure = HTLCFailReason::reason($err_code, $data.to_vec())
4460
4461
.get_encrypted_failure_packet(&shared_secret, &None);
4462
+
4461
4463
return PendingHTLCStatus::Fail(HTLCFailureMsg::Relay(msgs::UpdateFailHTLC {
4462
4464
channel_id: msg.channel_id,
4463
4465
htlc_id: msg.htlc_id,
4464
4466
reason: failure.data,
4467
+ attribution_data: failure.attribution_data,
4465
4468
}));
4466
4469
}
4467
4470
}
@@ -12852,11 +12855,15 @@ impl_writeable_tlv_based!(PendingHTLCInfo, {
12852
12855
impl Writeable for HTLCFailureMsg {
12853
12856
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
12854
12857
match self {
12855
- HTLCFailureMsg::Relay(msgs::UpdateFailHTLC { channel_id, htlc_id, reason }) => {
12858
+ HTLCFailureMsg::Relay(msgs::UpdateFailHTLC { channel_id, htlc_id, reason, attribution_data }) => {
12856
12859
0u8.write(writer)?;
12857
12860
channel_id.write(writer)?;
12858
12861
htlc_id.write(writer)?;
12859
12862
reason.write(writer)?;
12863
+
12864
+ // This code will only ever be hit for legacy data that is re-serialized. It isn't necessary to try
12865
+ // writing out attribution data, because it can never be present.
12866
+ debug_assert!(attribution_data.is_none());
12860
12867
},
12861
12868
HTLCFailureMsg::Malformed(msgs::UpdateFailMalformedHTLC {
12862
12869
channel_id, htlc_id, sha256_of_onion, failure_code
@@ -12881,6 +12888,7 @@ impl Readable for HTLCFailureMsg {
12881
12888
channel_id: Readable::read(reader)?,
12882
12889
htlc_id: Readable::read(reader)?,
12883
12890
reason: Readable::read(reader)?,
12891
+ attribution_data: None,
12884
12892
}))
12885
12893
},
12886
12894
1 => {
@@ -13111,6 +13119,7 @@ impl Writeable for HTLCForwardInfo {
13111
13119
write_tlv_fields!(w, {
13112
13120
(0, htlc_id, required),
13113
13121
(2, err_packet.data, required),
13122
+ (5, err_packet.attribution_data, option),
13114
13123
});
13115
13124
},
13116
13125
Self::FailMalformedHTLC { htlc_id, failure_code, sha256_of_onion } => {
@@ -13141,8 +13150,12 @@ impl Readable for HTLCForwardInfo {
13141
13150
(1, malformed_htlc_failure_code, option),
13142
13151
(2, err_packet, required),
13143
13152
(3, sha256_of_onion, option),
13153
+ (5, attribution_data, option),
13144
13154
});
13145
13155
if let Some(failure_code) = malformed_htlc_failure_code {
13156
+ if attribution_data.is_some() {
13157
+ return Err(DecodeError::InvalidValue);
13158
+ }
13146
13159
Self::FailMalformedHTLC {
13147
13160
htlc_id: _init_tlv_based_struct_field!(htlc_id, required),
13148
13161
failure_code,
@@ -13153,6 +13166,7 @@ impl Readable for HTLCForwardInfo {
13153
13166
htlc_id: _init_tlv_based_struct_field!(htlc_id, required),
13154
13167
err_packet: crate::ln::msgs::OnionErrorPacket {
13155
13168
data: _init_tlv_based_struct_field!(err_packet, required),
13169
+ attribution_data: _init_tlv_based_struct_field!(attribution_data, option),
13156
13170
},
13157
13171
}
13158
13172
}
@@ -14852,6 +14866,7 @@ mod tests {
14852
14866
use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
14853
14867
use core::sync::atomic::Ordering;
14854
14868
use crate::events::{Event, HTLCDestination, ClosureReason};
14869
+ use crate::ln::onion_utils::ATTRIBUTION_DATA_LEN;
14855
14870
use crate::ln::types::ChannelId;
14856
14871
use crate::types::payment::{PaymentPreimage, PaymentHash, PaymentSecret};
14857
14872
use crate::ln::channelmanager::{RAACommitmentOrder, create_recv_pending_htlc_info, inbound_payment, ChannelConfigOverrides, HTLCForwardInfo, InterceptId, PaymentId, RecipientOnionFields};
@@ -15325,6 +15340,80 @@ mod tests {
15325
15340
nodes[1].logger.assert_log_contains("lightning::ln::channelmanager", "Payment preimage didn't match payment hash", 1);
15326
15341
}
15327
15342
15343
+ #[test]
15344
+ fn test_htlc_localremoved_persistence() {
15345
+ let chanmon_cfgs: Vec<TestChanMonCfg> = create_chanmon_cfgs(2);
15346
+ let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
15347
+
15348
+ let persister;
15349
+ let chain_monitor;
15350
+ let deserialized_chanmgr;
15351
+
15352
+ // Send a keysend payment that fails because of a preimage mismatch.
15353
+ let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
15354
+ let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
15355
+
15356
+ let payer_pubkey = nodes[0].node.get_our_node_id();
15357
+ let payee_pubkey = nodes[1].node.get_our_node_id();
15358
+
15359
+ let _chan = create_chan_between_nodes(&nodes[0], &nodes[1]);
15360
+ let route_params = RouteParameters::from_payment_params_and_value(
15361
+ PaymentParameters::for_keysend(payee_pubkey, 40, false), 10_000);
15362
+ let network_graph = nodes[0].network_graph;
15363
+ let first_hops = nodes[0].node.list_usable_channels();
15364
+ let scorer = test_utils::TestScorer::new();
15365
+ let random_seed_bytes = chanmon_cfgs[1].keys_manager.get_secure_random_bytes();
15366
+ let route = find_route(
15367
+ &payer_pubkey, &route_params, &network_graph, Some(&first_hops.iter().collect::<Vec<_>>()),
15368
+ nodes[0].logger, &scorer, &Default::default(), &random_seed_bytes
15369
+ ).unwrap();
15370
+
15371
+ let test_preimage = PaymentPreimage([42; 32]);
15372
+ let mismatch_payment_hash = PaymentHash([43; 32]);
15373
+ let session_privs = nodes[0].node.test_add_new_pending_payment(mismatch_payment_hash,
15374
+ RecipientOnionFields::spontaneous_empty(), PaymentId(mismatch_payment_hash.0), &route).unwrap();
15375
+ nodes[0].node.test_send_payment_internal(&route, mismatch_payment_hash,
15376
+ RecipientOnionFields::spontaneous_empty(), Some(test_preimage), PaymentId(mismatch_payment_hash.0), None, session_privs).unwrap();
15377
+ check_added_monitors!(nodes[0], 1);
15378
+
15379
+ let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
15380
+ nodes[1].node.handle_update_add_htlc(nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
15381
+ commitment_signed_dance!(nodes[1], nodes[0], &updates.commitment_signed, false);
15382
+ expect_pending_htlcs_forwardable!(nodes[1]);
15383
+ expect_htlc_handling_failed_destinations!(nodes[1].node.get_and_clear_pending_events(), &[HTLCDestination::FailedPayment { payment_hash: mismatch_payment_hash }]);
15384
+ check_added_monitors(&nodes[1], 1);
15385
+
15386
+ // Save the update_fail_htlc message for later comparison.
15387
+ let msgs = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
15388
+ let htlc_fail_msg = msgs.update_fail_htlcs[0].clone();
15389
+
15390
+ // Reload node.
15391
+ nodes[0].node.peer_disconnected(nodes[1].node.get_our_node_id());
15392
+ nodes[1].node.peer_disconnected(nodes[0].node.get_our_node_id());
15393
+
15394
+ let monitor_encoded = get_monitor!(nodes[1], _chan.3).encode();
15395
+ reload_node!(nodes[1], nodes[1].node.encode(), &[&monitor_encoded], persister, chain_monitor, deserialized_chanmgr);
15396
+
15397
+ nodes[0].node.peer_connected(nodes[1].node.get_our_node_id(), &msgs::Init {
15398
+ features: nodes[1].node.init_features(), networks: None, remote_network_address: None
15399
+ }, true).unwrap();
15400
+ let reestablish_1 = get_chan_reestablish_msgs!(nodes[0], nodes[1]);
15401
+ assert_eq!(reestablish_1.len(), 1);
15402
+ nodes[1].node.peer_connected(nodes[0].node.get_our_node_id(), &msgs::Init {
15403
+ features: nodes[0].node.init_features(), networks: None, remote_network_address: None
15404
+ }, false).unwrap();
15405
+ let reestablish_2 = get_chan_reestablish_msgs!(nodes[1], nodes[0]);
15406
+ assert_eq!(reestablish_2.len(), 1);
15407
+ nodes[0].node.handle_channel_reestablish(nodes[1].node.get_our_node_id(), &reestablish_2[0]);
15408
+ handle_chan_reestablish_msgs!(nodes[0], nodes[1]);
15409
+ nodes[1].node.handle_channel_reestablish(nodes[0].node.get_our_node_id(), &reestablish_1[0]);
15410
+
15411
+ // Assert that same failure message is resent after reload.
15412
+ let msgs = handle_chan_reestablish_msgs!(nodes[1], nodes[0]);
15413
+ let htlc_fail_msg_after_reload = msgs.2.unwrap().update_fail_htlcs[0].clone();
15414
+ assert_eq!(htlc_fail_msg, htlc_fail_msg_after_reload);
15415
+ }
15416
+
15328
15417
#[test]
15329
15418
fn test_multi_hop_missing_secret() {
15330
15419
let chanmon_cfgs = create_chanmon_cfgs(4);
@@ -16283,7 +16372,7 @@ mod tests {
16283
16372
let mut nodes = create_network(1, &node_cfg, &chanmgrs);
16284
16373
16285
16374
let dummy_failed_htlc = |htlc_id| {
16286
- HTLCForwardInfo::FailHTLC { htlc_id, err_packet: msgs::OnionErrorPacket { data: vec![42] } }
16375
+ HTLCForwardInfo::FailHTLC { htlc_id, err_packet: msgs::OnionErrorPacket { data: vec![42], attribution_data: Some([0; ATTRIBUTION_DATA_LEN]) } }
16287
16376
};
16288
16377
let dummy_malformed_htlc = |htlc_id| {
16289
16378
HTLCForwardInfo::FailMalformedHTLC { htlc_id, failure_code: 0x4000, sha256_of_onion: [0; 32] }
0 commit comments