Skip to content

Commit 42db0df

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 42db0df

File tree

1 file changed

+73
-1
lines changed

1 file changed

+73
-1
lines changed

lightning/src/ln/reload_tests.rs

+73-1
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

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

0 commit comments

Comments
 (0)