@@ -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::{ChannelMessageHandler, CommitmentUpdate, DecodeError, LightningError};
65
65
#[cfg(test)]
@@ -4418,6 +4418,7 @@ where
4418
4418
channel_id: msg.channel_id,
4419
4419
htlc_id: msg.htlc_id,
4420
4420
reason: failure.data.clone(),
4421
+ attribution_data: failure.attribution_data,
4421
4422
})
4422
4423
}
4423
4424
@@ -4443,10 +4444,12 @@ where
4443
4444
}
4444
4445
let failure = HTLCFailReason::reason($err_code, $data.to_vec())
4445
4446
.get_encrypted_failure_packet(&shared_secret, &None);
4447
+
4446
4448
return PendingHTLCStatus::Fail(HTLCFailureMsg::Relay(msgs::UpdateFailHTLC {
4447
4449
channel_id: msg.channel_id,
4448
4450
htlc_id: msg.htlc_id,
4449
4451
reason: failure.data,
4452
+ attribution_data: failure.attribution_data,
4450
4453
}));
4451
4454
}
4452
4455
}
@@ -12837,11 +12840,15 @@ impl_writeable_tlv_based!(PendingHTLCInfo, {
12837
12840
impl Writeable for HTLCFailureMsg {
12838
12841
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
12839
12842
match self {
12840
- HTLCFailureMsg::Relay(msgs::UpdateFailHTLC { channel_id, htlc_id, reason }) => {
12843
+ HTLCFailureMsg::Relay(msgs::UpdateFailHTLC { channel_id, htlc_id, reason, attribution_data }) => {
12841
12844
0u8.write(writer)?;
12842
12845
channel_id.write(writer)?;
12843
12846
htlc_id.write(writer)?;
12844
12847
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());
12845
12852
},
12846
12853
HTLCFailureMsg::Malformed(msgs::UpdateFailMalformedHTLC {
12847
12854
channel_id, htlc_id, sha256_of_onion, failure_code
@@ -12866,6 +12873,7 @@ impl Readable for HTLCFailureMsg {
12866
12873
channel_id: Readable::read(reader)?,
12867
12874
htlc_id: Readable::read(reader)?,
12868
12875
reason: Readable::read(reader)?,
12876
+ attribution_data: None,
12869
12877
}))
12870
12878
},
12871
12879
1 => {
@@ -13096,6 +13104,7 @@ impl Writeable for HTLCForwardInfo {
13096
13104
write_tlv_fields!(w, {
13097
13105
(0, htlc_id, required),
13098
13106
(2, err_packet.data, required),
13107
+ (5, err_packet.attribution_data, option),
13099
13108
});
13100
13109
},
13101
13110
Self::FailMalformedHTLC { htlc_id, failure_code, sha256_of_onion } => {
@@ -13126,8 +13135,12 @@ impl Readable for HTLCForwardInfo {
13126
13135
(1, malformed_htlc_failure_code, option),
13127
13136
(2, err_packet, required),
13128
13137
(3, sha256_of_onion, option),
13138
+ (5, attribution_data, option),
13129
13139
});
13130
13140
if let Some(failure_code) = malformed_htlc_failure_code {
13141
+ if attribution_data.is_some() {
13142
+ return Err(DecodeError::InvalidValue);
13143
+ }
13131
13144
Self::FailMalformedHTLC {
13132
13145
htlc_id: _init_tlv_based_struct_field!(htlc_id, required),
13133
13146
failure_code,
@@ -13138,6 +13151,7 @@ impl Readable for HTLCForwardInfo {
13138
13151
htlc_id: _init_tlv_based_struct_field!(htlc_id, required),
13139
13152
err_packet: crate::ln::msgs::OnionErrorPacket {
13140
13153
data: _init_tlv_based_struct_field!(err_packet, required),
13154
+ attribution_data: _init_tlv_based_struct_field!(attribution_data, option),
13141
13155
},
13142
13156
}
13143
13157
}
@@ -14837,7 +14851,8 @@ mod tests {
14837
14851
use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
14838
14852
use core::sync::atomic::Ordering;
14839
14853
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;
14841
14856
use crate::types::payment::{PaymentPreimage, PaymentHash, PaymentSecret};
14842
14857
use crate::ln::channelmanager::{RAACommitmentOrder, create_recv_pending_htlc_info, inbound_payment, ChannelConfigOverrides, HTLCForwardInfo, InterceptId, PaymentId, RecipientOnionFields};
14843
14858
use crate::ln::functional_test_utils::*;
@@ -15311,6 +15326,80 @@ mod tests {
15311
15326
nodes[1].logger.assert_log_contains("lightning::ln::channelmanager", "Payment preimage didn't match payment hash", 1);
15312
15327
}
15313
15328
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
+
15314
15403
#[test]
15315
15404
fn test_multi_hop_missing_secret() {
15316
15405
let chanmon_cfgs = create_chanmon_cfgs(4);
@@ -16269,7 +16358,7 @@ mod tests {
16269
16358
let mut nodes = create_network(1, &node_cfg, &chanmgrs);
16270
16359
16271
16360
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]) } }
16273
16362
};
16274
16363
let dummy_malformed_htlc = |htlc_id| {
16275
16364
HTLCForwardInfo::FailMalformedHTLC { htlc_id, failure_code: 0x4000, sha256_of_onion: [0; 32] }
0 commit comments