Skip to content

Commit 011a41a

Browse files
committed
f - extend test to cover update_fulfill/fail_htlc
1 parent a6b28c8 commit 011a41a

File tree

1 file changed

+57
-25
lines changed

1 file changed

+57
-25
lines changed

lightning/src/ln/functional_tests.rs

+57-25
Original file line numberDiff line numberDiff line change
@@ -2211,13 +2211,22 @@ fn channel_reserve_in_flight_removes() {
22112211
claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_3);
22122212
}
22132213

2214+
enum PostFailBackAction {
2215+
TimeoutOnChain,
2216+
ClaimOnChain,
2217+
FailOffChain,
2218+
ClaimOffChain,
2219+
}
2220+
22142221
#[test]
22152222
fn test_fail_back_before_backwards_timeout() {
2216-
do_test_fail_back_before_backwards_timeout(false);
2217-
do_test_fail_back_before_backwards_timeout(true);
2223+
do_test_fail_back_before_backwards_timeout(PostFailBackAction::TimeoutOnChain);
2224+
do_test_fail_back_before_backwards_timeout(PostFailBackAction::ClaimOnChain);
2225+
do_test_fail_back_before_backwards_timeout(PostFailBackAction::FailOffChain);
2226+
do_test_fail_back_before_backwards_timeout(PostFailBackAction::ClaimOffChain);
22182227
}
22192228

2220-
fn do_test_fail_back_before_backwards_timeout(forward_claims: bool) {
2229+
fn do_test_fail_back_before_backwards_timeout(post_fail_back_action: PostFailBackAction) {
22212230
// Test that we fail an HTLC upstream if we are still waiting for confirmation downstream
22222231
// just before the upstream timeout expires
22232232
let chanmon_cfgs = create_chanmon_cfgs(3);
@@ -2267,28 +2276,51 @@ fn do_test_fail_back_before_backwards_timeout(forward_claims: bool) {
22672276
commitment_signed_dance!(nodes[0], nodes[1], commitment_signed, false);
22682277
expect_payment_failed_conditions(&nodes[0], payment_hash, false, PaymentFailedConditions::new().blamed_chan_closed(true));
22692278

2270-
// Make sure we don't generate duplicate fails from monitor events
2271-
if forward_claims {
2272-
// Claim and force close as nodes[2]
2273-
nodes[2].node.claim_funds(payment_preimage);
2274-
expect_payment_claimed!(nodes[2], payment_hash, 3_000_000);
2275-
check_added_monitors!(nodes[2], 1);
2276-
get_htlc_update_msgs(&nodes[2], &nodes[1].node.get_our_node_id());
2277-
2278-
connect_blocks(&nodes[2], TEST_FINAL_CLTV - CLTV_CLAIM_BUFFER + 2);
2279-
let node_2_txn = test_txn_broadcast(&nodes[2], &chan_2, None, HTLCType::SUCCESS);
2280-
check_closed_broadcast!(nodes[2], true);
2281-
check_closed_event(&nodes[2], 1, ClosureReason::CommitmentTxConfirmed, false);
2282-
check_added_monitors!(nodes[2], 1);
2283-
2284-
// Confirm nodes[2]'s claim with preimage
2285-
mine_transaction(&nodes[1], &node_2_txn[0]); // Commitment
2286-
mine_transaction(&nodes[1], &node_2_txn[1]); // HTLC success
2287-
} else {
2288-
// Confirm nodes[1]'s claim with timeout, make sure we don't fail upstream again
2289-
mine_transaction(&nodes[1], &node_1_txn[0]); // Commitment
2290-
mine_transaction(&nodes[1], &node_1_txn[1]); // HTLC timeout
2291-
}
2279+
// Make sure we handle possible duplicate fails or extra messages after failing back
2280+
match post_fail_back_action {
2281+
PostFailBackAction::TimeoutOnChain => {
2282+
// Confirm nodes[1]'s claim with timeout, make sure we don't fail upstream again
2283+
mine_transaction(&nodes[1], &node_1_txn[0]); // Commitment
2284+
mine_transaction(&nodes[1], &node_1_txn[1]); // HTLC timeout
2285+
},
2286+
PostFailBackAction::ClaimOnChain => {
2287+
nodes[2].node.claim_funds(payment_preimage);
2288+
expect_payment_claimed!(nodes[2], payment_hash, 3_000_000);
2289+
check_added_monitors!(nodes[2], 1);
2290+
get_htlc_update_msgs(&nodes[2], &nodes[1].node.get_our_node_id());
2291+
2292+
connect_blocks(&nodes[2], TEST_FINAL_CLTV - CLTV_CLAIM_BUFFER + 2);
2293+
let node_2_txn = test_txn_broadcast(&nodes[2], &chan_2, None, HTLCType::SUCCESS);
2294+
check_closed_broadcast!(nodes[2], true);
2295+
check_closed_event(&nodes[2], 1, ClosureReason::CommitmentTxConfirmed, false);
2296+
check_added_monitors!(nodes[2], 1);
2297+
2298+
mine_transaction(&nodes[1], &node_2_txn[0]); // Commitment
2299+
mine_transaction(&nodes[1], &node_2_txn[1]); // HTLC success
2300+
},
2301+
PostFailBackAction::FailOffChain => {
2302+
nodes[2].node.fail_htlc_backwards(&payment_hash);
2303+
expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[2], vec![HTLCDestination::FailedPayment { payment_hash: payment_hash.clone() }]);
2304+
check_added_monitors!(nodes[2], 1);
2305+
let commitment_update = get_htlc_update_msgs(&nodes[2], &nodes[1].node.get_our_node_id());
2306+
let update_fail = commitment_update.update_fail_htlcs[0].clone();
2307+
2308+
nodes[1].node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &update_fail);
2309+
let err_msg = get_err_msg(&nodes[1], &nodes[2].node.get_our_node_id());
2310+
assert_eq!(err_msg.channel_id, chan_2.2);
2311+
},
2312+
PostFailBackAction::ClaimOffChain => {
2313+
nodes[2].node.claim_funds(payment_preimage);
2314+
expect_payment_claimed!(nodes[2], payment_hash, 3_000_000);
2315+
check_added_monitors!(nodes[2], 1);
2316+
let commitment_update = get_htlc_update_msgs(&nodes[2], &nodes[1].node.get_our_node_id());
2317+
let update_fulfill = commitment_update.update_fulfill_htlcs[0].clone();
2318+
2319+
nodes[1].node.handle_update_fulfill_htlc(&nodes[2].node.get_our_node_id(), &update_fulfill);
2320+
let err_msg = get_err_msg(&nodes[1], &nodes[2].node.get_our_node_id());
2321+
assert_eq!(err_msg.channel_id, chan_2.2);
2322+
},
2323+
};
22922324
}
22932325

22942326
#[test]

0 commit comments

Comments
 (0)