|
14 | 14 | use crate::chain;
|
15 | 15 | use crate::chain::{ChannelMonitorUpdateStatus, Confirm, Listen, Watch};
|
16 | 16 | use crate::chain::chaininterface::LowerBoundedFeeEstimator;
|
17 |
| -use crate::chain::channelmonitor; |
| 17 | +use crate::chain::channelmonitor::{self, TIMEOUT_FAIL_BACK_BUFFER}; |
18 | 18 | use crate::chain::channelmonitor::{CLTV_CLAIM_BUFFER, LATENCY_GRACE_PERIOD_BLOCKS, ANTI_REORG_DELAY};
|
19 | 19 | use crate::chain::transaction::OutPoint;
|
20 | 20 | use crate::sign::{ChannelSigner, EcdsaChannelSigner, EntropySource};
|
@@ -2211,6 +2211,118 @@ fn channel_reserve_in_flight_removes() {
|
2211 | 2211 | claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_3);
|
2212 | 2212 | }
|
2213 | 2213 |
|
| 2214 | +enum PostFailBackAction { |
| 2215 | + TimeoutOnChain, |
| 2216 | + ClaimOnChain, |
| 2217 | + FailOffChain, |
| 2218 | + ClaimOffChain, |
| 2219 | +} |
| 2220 | + |
| 2221 | +#[test] |
| 2222 | +fn test_fail_back_before_backwards_timeout() { |
| 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); |
| 2227 | +} |
| 2228 | + |
| 2229 | +fn do_test_fail_back_before_backwards_timeout(post_fail_back_action: PostFailBackAction) { |
| 2230 | + // Test that we fail an HTLC upstream if we are still waiting for confirmation downstream |
| 2231 | + // just before the upstream timeout expires |
| 2232 | + let chanmon_cfgs = create_chanmon_cfgs(3); |
| 2233 | + let node_cfgs = create_node_cfgs(3, &chanmon_cfgs); |
| 2234 | + let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]); |
| 2235 | + let nodes = create_network(3, &node_cfgs, &node_chanmgrs); |
| 2236 | + |
| 2237 | + create_announced_chan_between_nodes(&nodes, 0, 1); |
| 2238 | + let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2); |
| 2239 | + |
| 2240 | + connect_blocks(&nodes[0], 2*CHAN_CONFIRM_DEPTH + 1 - nodes[0].best_block_info().1); |
| 2241 | + connect_blocks(&nodes[1], 2*CHAN_CONFIRM_DEPTH + 1 - nodes[1].best_block_info().1); |
| 2242 | + connect_blocks(&nodes[2], 2*CHAN_CONFIRM_DEPTH + 1 - nodes[2].best_block_info().1); |
| 2243 | + |
| 2244 | + let (payment_preimage, payment_hash, _) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 3_000_000); |
| 2245 | + |
| 2246 | + // Force close downstream with timeout |
| 2247 | + nodes[1].node.force_close_broadcasting_latest_txn(&chan_2.2, &nodes[2].node.get_our_node_id()).unwrap(); |
| 2248 | + check_added_monitors!(nodes[1], 1); |
| 2249 | + check_closed_broadcast!(nodes[1], true); |
| 2250 | + |
| 2251 | + connect_blocks(&nodes[1], TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS + 1); |
| 2252 | + let node_1_txn = test_txn_broadcast(&nodes[1], &chan_2, None, HTLCType::TIMEOUT); |
| 2253 | + check_closed_event(&nodes[1], 1, ClosureReason::HolderForceClosed, false); |
| 2254 | + |
| 2255 | + // Nothing is confirmed for a while |
| 2256 | + connect_blocks(&nodes[1], MIN_CLTV_EXPIRY_DELTA as u32 - LATENCY_GRACE_PERIOD_BLOCKS - TIMEOUT_FAIL_BACK_BUFFER); |
| 2257 | + |
| 2258 | + // Check that nodes[1] fails the HTLC upstream |
| 2259 | + expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], vec![HTLCDestination::NextHopChannel { node_id: Some(nodes[2].node.get_our_node_id()), channel_id: chan_2.2 }]); |
| 2260 | + check_added_monitors!(nodes[1], 1); |
| 2261 | + let events = nodes[1].node.get_and_clear_pending_msg_events(); |
| 2262 | + assert_eq!(events.len(), 1); |
| 2263 | + let (update_fail, commitment_signed) = match events[0] { |
| 2264 | + MessageSendEvent::UpdateHTLCs { node_id: _, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, ref update_fee, ref commitment_signed } } => { |
| 2265 | + assert!(update_add_htlcs.is_empty()); |
| 2266 | + assert!(update_fulfill_htlcs.is_empty()); |
| 2267 | + assert_eq!(update_fail_htlcs.len(), 1); |
| 2268 | + assert!(update_fail_malformed_htlcs.is_empty()); |
| 2269 | + assert!(update_fee.is_none()); |
| 2270 | + (update_fail_htlcs[0].clone(), commitment_signed.clone()) |
| 2271 | + }, |
| 2272 | + _ => panic!("Unexpected event"), |
| 2273 | + }; |
| 2274 | + |
| 2275 | + nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_fail); |
| 2276 | + commitment_signed_dance!(nodes[0], nodes[1], commitment_signed, false); |
| 2277 | + expect_payment_failed_conditions(&nodes[0], payment_hash, false, PaymentFailedConditions::new().blamed_chan_closed(true)); |
| 2278 | + |
| 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 | + }; |
| 2324 | +} |
| 2325 | + |
2214 | 2326 | #[test]
|
2215 | 2327 | fn channel_monitor_network_test() {
|
2216 | 2328 | // Simple test which builds a network of ChannelManagers, connects them to each other, and
|
@@ -2307,7 +2419,7 @@ fn channel_monitor_network_test() {
|
2307 | 2419 | let node2_commitment_txid;
|
2308 | 2420 | {
|
2309 | 2421 | let node_txn = test_txn_broadcast(&nodes[2], &chan_3, None, HTLCType::NONE);
|
2310 |
| - connect_blocks(&nodes[2], TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS + MIN_CLTV_EXPIRY_DELTA as u32 + 1); |
| 2422 | + connect_blocks(&nodes[2], TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS); |
2311 | 2423 | test_txn_broadcast(&nodes[2], &chan_3, None, HTLCType::TIMEOUT);
|
2312 | 2424 | node2_commitment_txid = node_txn[0].txid();
|
2313 | 2425 |
|
@@ -2992,7 +3104,7 @@ fn do_test_htlc_on_chain_timeout(connect_style: ConnectStyle) {
|
2992 | 3104 | // Verify that B's ChannelManager is able to detect that HTLC is timeout by its own tx and react backward in consequence
|
2993 | 3105 | mine_transaction(&nodes[1], &commitment_tx[0]);
|
2994 | 3106 | check_closed_event(&nodes[1], 1, ClosureReason::CommitmentTxConfirmed, false);
|
2995 |
| - connect_blocks(&nodes[1], 200 - nodes[2].best_block_info().1); |
| 3107 | + connect_blocks(&nodes[1], 100 - nodes[2].best_block_info().1); |
2996 | 3108 | let timeout_tx = {
|
2997 | 3109 | let mut txn = nodes[1].tx_broadcaster.txn_broadcast();
|
2998 | 3110 | if nodes[1].connect_style.borrow().skips_blocks() {
|
|
0 commit comments