Skip to content

Commit 25a749e

Browse files
committed
Add persistence test for htlc in the LocalRemoved state
Increase coverage and prepare for attributable failures which are going to extend the update_fail_htlc message with an additional field that needs to be persisted as well.
1 parent 131b35e commit 25a749e

File tree

1 file changed

+73
-2
lines changed

1 file changed

+73
-2
lines changed

lightning/src/ln/reload_tests.rs

+73-2
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@
1212
use crate::chain::{ChannelMonitorUpdateStatus, Watch};
1313
use crate::chain::chaininterface::LowerBoundedFeeEstimator;
1414
use crate::chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdateStep};
15+
use crate::routing::router::{PaymentParameters, RouteParameters};
1516
use crate::sign::EntropySource;
1617
use crate::chain::transaction::OutPoint;
1718
use crate::events::{ClosureReason, Event, HTLCDestination};
18-
use crate::ln::channelmanager::{ChannelManager, ChannelManagerReadArgs, PaymentId, RecipientOnionFields};
19+
use crate::ln::channelmanager::{ChannelManager, ChannelManagerReadArgs, PaymentId, RecipientOnionFields, RAACommitmentOrder};
1920
use crate::ln::msgs;
2021
use crate::ln::types::ChannelId;
2122
use crate::ln::msgs::{BaseMessageHandler, ChannelMessageHandler, RoutingMessageHandler, ErrorAction, MessageSendEvent};
@@ -27,6 +28,7 @@ use crate::util::config::UserConfig;
2728

2829
use bitcoin::hashes::Hash;
2930
use bitcoin::hash_types::BlockHash;
31+
use types::payment::{PaymentHash, PaymentPreimage};
3032

3133
use crate::prelude::*;
3234

@@ -504,7 +506,6 @@ fn test_manager_serialize_deserialize_inconsistent_monitor() {
504506

505507
#[cfg(feature = "std")]
506508
fn do_test_data_loss_protect(reconnect_panicing: bool, substantially_old: bool, not_stale: bool) {
507-
use crate::routing::router::{RouteParameters, PaymentParameters};
508509
use crate::ln::channelmanager::Retry;
509510
use crate::util::string::UntrustedString;
510511
// When we get a data_loss_protect proving we're behind, we immediately panic as the
@@ -1286,3 +1287,73 @@ fn test_reload_partial_funding_batch() {
12861287
// Ensure the channels don't exist anymore.
12871288
assert!(nodes[0].node.list_channels().is_empty());
12881289
}
1290+
1291+
#[test]
1292+
fn test_htlc_localremoved_persistence() {
1293+
// Tests that if we fail an htlc back (update_fail_htlc message) and then restart the node, the node will resend the
1294+
// exact same fail message.
1295+
let chanmon_cfgs: Vec<TestChanMonCfg> = create_chanmon_cfgs(2);
1296+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1297+
1298+
let persister;
1299+
let chain_monitor;
1300+
let deserialized_chanmgr;
1301+
1302+
// Send a keysend payment that fails because of a preimage mismatch.
1303+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1304+
let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1305+
1306+
let payee_pubkey = nodes[1].node.get_our_node_id();
1307+
1308+
let _chan = create_chan_between_nodes(&nodes[0], &nodes[1]);
1309+
let route_params = RouteParameters::from_payment_params_and_value(
1310+
PaymentParameters::for_keysend(payee_pubkey, 40, false), 10_000);
1311+
let route = find_route(
1312+
&nodes[0], &route_params
1313+
).unwrap();
1314+
1315+
let test_preimage = PaymentPreimage([42; 32]);
1316+
let mismatch_payment_hash = PaymentHash([43; 32]);
1317+
let session_privs = nodes[0].node.test_add_new_pending_payment(mismatch_payment_hash,
1318+
RecipientOnionFields::spontaneous_empty(), PaymentId(mismatch_payment_hash.0), &route).unwrap();
1319+
nodes[0].node.test_send_payment_internal(&route, mismatch_payment_hash,
1320+
RecipientOnionFields::spontaneous_empty(), Some(test_preimage), PaymentId(mismatch_payment_hash.0), None, session_privs).unwrap();
1321+
check_added_monitors!(nodes[0], 1);
1322+
1323+
let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
1324+
nodes[1].node.handle_update_add_htlc(nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
1325+
commitment_signed_dance!(nodes[1], nodes[0], &updates.commitment_signed, false);
1326+
expect_pending_htlcs_forwardable!(nodes[1]);
1327+
expect_htlc_handling_failed_destinations!(nodes[1].node.get_and_clear_pending_events(), &[HTLCDestination::FailedPayment { payment_hash: mismatch_payment_hash }]);
1328+
check_added_monitors(&nodes[1], 1);
1329+
1330+
// Save the update_fail_htlc message for later comparison.
1331+
let msgs = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
1332+
let htlc_fail_msg = msgs.update_fail_htlcs[0].clone();
1333+
1334+
// Reload nodes.
1335+
nodes[0].node.peer_disconnected(nodes[1].node.get_our_node_id());
1336+
nodes[1].node.peer_disconnected(nodes[0].node.get_our_node_id());
1337+
1338+
let monitor_encoded = get_monitor!(nodes[1], _chan.3).encode();
1339+
reload_node!(nodes[1], nodes[1].node.encode(), &[&monitor_encoded], persister, chain_monitor, deserialized_chanmgr);
1340+
1341+
nodes[0].node.peer_connected(nodes[1].node.get_our_node_id(), &msgs::Init {
1342+
features: nodes[1].node.init_features(), networks: None, remote_network_address: None
1343+
}, true).unwrap();
1344+
let reestablish_1 = get_chan_reestablish_msgs!(nodes[0], nodes[1]);
1345+
assert_eq!(reestablish_1.len(), 1);
1346+
nodes[1].node.peer_connected(nodes[0].node.get_our_node_id(), &msgs::Init {
1347+
features: nodes[0].node.init_features(), networks: None, remote_network_address: None
1348+
}, false).unwrap();
1349+
let reestablish_2 = get_chan_reestablish_msgs!(nodes[1], nodes[0]);
1350+
assert_eq!(reestablish_2.len(), 1);
1351+
nodes[0].node.handle_channel_reestablish(nodes[1].node.get_our_node_id(), &reestablish_2[0]);
1352+
handle_chan_reestablish_msgs!(nodes[0], nodes[1]);
1353+
nodes[1].node.handle_channel_reestablish(nodes[0].node.get_our_node_id(), &reestablish_1[0]);
1354+
1355+
// Assert that same failure message is resent after reload.
1356+
let msgs = handle_chan_reestablish_msgs!(nodes[1], nodes[0]);
1357+
let htlc_fail_msg_after_reload = msgs.2.unwrap().update_fail_htlcs[0].clone();
1358+
assert_eq!(htlc_fail_msg, htlc_fail_msg_after_reload);
1359+
}

0 commit comments

Comments
 (0)