|
9 | 9 |
|
10 | 10 | //! Tests that test the channel open process. |
11 | 11 |
|
12 | | -use crate::chain::chaininterface::LowerBoundedFeeEstimator; |
| 12 | +use crate::chain::chaininterface::{LowerBoundedFeeEstimator, TransactionType}; |
13 | 13 | use crate::chain::channelmonitor::{self, ChannelMonitorUpdateStep}; |
14 | 14 | use crate::chain::transaction::OutPoint; |
15 | 15 | use crate::chain::{self, ChannelMonitorUpdateStatus}; |
@@ -2231,6 +2231,173 @@ pub fn test_batch_funding_close_after_funding_signed() { |
2231 | 2231 | assert!(nodes[0].node.list_channels().is_empty()); |
2232 | 2232 | } |
2233 | 2233 |
|
| 2234 | +#[xtest(feature = "_externalize_tests")] |
| 2235 | +pub fn test_funding_tx_rebroadcast() { |
| 2236 | + // Tests that `ChannelManager::timer_tick_occurred` periodically rebroadcasts an unconfirmed |
| 2237 | + // funding transaction, and that rebroadcasting stops once the transaction has been confirmed. |
| 2238 | + let chanmon_cfgs = create_chanmon_cfgs(2); |
| 2239 | + let node_cfgs = create_node_cfgs(2, &chanmon_cfgs); |
| 2240 | + let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]); |
| 2241 | + let nodes = create_network(2, &node_cfgs, &node_chanmgrs); |
| 2242 | + |
| 2243 | + let node_b_id = nodes[1].node.get_our_node_id(); |
| 2244 | + |
| 2245 | + // Open a channel without confirming. `sign_funding_transaction` verifies the initial |
| 2246 | + // broadcast happened and clears the broadcaster, so the queue is empty from here on. |
| 2247 | + let tx = create_chan_between_nodes_with_value_init(&nodes[0], &nodes[1], 100_000, 10_000); |
| 2248 | + let funding_txo = OutPoint { txid: tx.compute_txid(), index: 0 }; |
| 2249 | + let channel_id = ChannelId::v1_from_funding_outpoint(funding_txo); |
| 2250 | + assert!(nodes[0].tx_broadcaster.txn_broadcast().is_empty()); |
| 2251 | + |
| 2252 | + // The next timer tick should rebroadcast the funding transaction because it hasn't |
| 2253 | + // confirmed yet. |
| 2254 | + nodes[0].node.timer_tick_occurred(); |
| 2255 | + let rebroadcast = nodes[0].tx_broadcaster.txn_broadcast_with_types(); |
| 2256 | + assert_eq!(rebroadcast.len(), 1); |
| 2257 | + assert_eq!(rebroadcast[0].0, tx); |
| 2258 | + match &rebroadcast[0].1 { |
| 2259 | + TransactionType::Funding { channels } => { |
| 2260 | + assert_eq!(channels.as_slice(), &[(node_b_id, channel_id)]); |
| 2261 | + }, |
| 2262 | + other => panic!("Expected Funding, got {:?}", other), |
| 2263 | + } |
| 2264 | + |
| 2265 | + // The V1 acceptor does not have the funding transaction and must not rebroadcast. |
| 2266 | + nodes[1].node.timer_tick_occurred(); |
| 2267 | + assert!(nodes[1].tx_broadcaster.txn_broadcast().is_empty()); |
| 2268 | + |
| 2269 | + // A subsequent tick before confirmation still rebroadcasts. |
| 2270 | + nodes[0].node.timer_tick_occurred(); |
| 2271 | + assert_eq!(nodes[0].tx_broadcaster.txn_broadcast().len(), 1); |
| 2272 | + |
| 2273 | + // Once the funding tx confirms, timer_tick_occurred should no longer rebroadcast. |
| 2274 | + mine_transaction(&nodes[0], &tx); |
| 2275 | + nodes[0].tx_broadcaster.clear(); |
| 2276 | + nodes[0].node.timer_tick_occurred(); |
| 2277 | + assert!( |
| 2278 | + nodes[0].tx_broadcaster.txn_broadcast().is_empty(), |
| 2279 | + "Should not rebroadcast after confirmation", |
| 2280 | + ); |
| 2281 | +} |
| 2282 | + |
| 2283 | +#[xtest(feature = "_externalize_tests")] |
| 2284 | +pub fn test_funding_tx_rebroadcast_skipped_for_manual_broadcast() { |
| 2285 | + // Tests that `ChannelManager::timer_tick_occurred` does not rebroadcast the funding |
| 2286 | + // transaction when the user is responsible for broadcasting it |
| 2287 | + // (i.e., `ChannelContext::is_manual_broadcast`). |
| 2288 | + let mut cfg = UserConfig::default(); |
| 2289 | + cfg.channel_handshake_config.minimum_depth = 1; |
| 2290 | + let chanmon_cfgs = create_chanmon_cfgs(2); |
| 2291 | + let node_cfgs = create_node_cfgs(2, &chanmon_cfgs); |
| 2292 | + let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(cfg.clone()), Some(cfg)]); |
| 2293 | + let nodes = create_network(2, &node_cfgs, &node_chanmgrs); |
| 2294 | + |
| 2295 | + let node_a_id = nodes[0].node.get_our_node_id(); |
| 2296 | + let node_b_id = nodes[1].node.get_our_node_id(); |
| 2297 | + |
| 2298 | + assert!(nodes[0].node.create_channel(node_b_id, 100_000, 0, 42, None, None).is_ok()); |
| 2299 | + let open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, node_b_id); |
| 2300 | + handle_and_accept_open_channel(&nodes[1], node_a_id, &open_channel); |
| 2301 | + |
| 2302 | + let accept_channel = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, node_a_id); |
| 2303 | + nodes[0].node.handle_accept_channel(node_b_id, &accept_channel); |
| 2304 | + let (temp_channel_id, _tx, funding_outpoint) = |
| 2305 | + create_funding_transaction(&nodes[0], &node_b_id, 100_000, 42); |
| 2306 | + nodes[0] |
| 2307 | + .node |
| 2308 | + .unsafe_manual_funding_transaction_generated(temp_channel_id, node_b_id, funding_outpoint) |
| 2309 | + .unwrap(); |
| 2310 | + check_added_monitors(&nodes[0], 0); |
| 2311 | + |
| 2312 | + let funding_created = get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, node_b_id); |
| 2313 | + nodes[1].node.handle_funding_created(node_a_id, &funding_created); |
| 2314 | + check_added_monitors(&nodes[1], 1); |
| 2315 | + expect_channel_pending_event(&nodes[1], &node_a_id); |
| 2316 | + |
| 2317 | + let funding_signed = get_event_msg!(nodes[1], MessageSendEvent::SendFundingSigned, node_a_id); |
| 2318 | + nodes[0].node.handle_funding_signed(node_b_id, &funding_signed); |
| 2319 | + check_added_monitors(&nodes[0], 1); |
| 2320 | + let _ = nodes[0].node.get_and_clear_pending_events(); |
| 2321 | + |
| 2322 | + // Only a FundingTxBroadcastSafe event was emitted; the broadcaster was never invoked. |
| 2323 | + assert!(nodes[0].tx_broadcaster.txn_broadcast().is_empty()); |
| 2324 | + |
| 2325 | + // A timer tick must not kick off a broadcast either — the user owns the funding tx. |
| 2326 | + nodes[0].node.timer_tick_occurred(); |
| 2327 | + assert!(nodes[0].tx_broadcaster.txn_broadcast().is_empty()); |
| 2328 | +} |
| 2329 | + |
| 2330 | +#[xtest(feature = "_externalize_tests")] |
| 2331 | +pub fn test_batch_funding_rebroadcast_dedupe() { |
| 2332 | + // Tests that when a batch funding transaction opens multiple channels, a timer-driven |
| 2333 | + // rebroadcast emits the transaction exactly once, with all funded channels merged into the |
| 2334 | + // `TransactionType::Funding { channels }` list. |
| 2335 | + let chanmon_cfgs = create_chanmon_cfgs(3); |
| 2336 | + let node_cfgs = create_node_cfgs(3, &chanmon_cfgs); |
| 2337 | + let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]); |
| 2338 | + let nodes = create_network(3, &node_cfgs, &node_chanmgrs); |
| 2339 | + |
| 2340 | + let node_a_id = nodes[0].node.get_our_node_id(); |
| 2341 | + let node_b_id = nodes[1].node.get_our_node_id(); |
| 2342 | + let node_c_id = nodes[2].node.get_our_node_id(); |
| 2343 | + |
| 2344 | + let (tx, funding_created_msgs) = create_batch_channel_funding( |
| 2345 | + &nodes[0], |
| 2346 | + &[(&nodes[1], 100_000, 0, 42, None), (&nodes[2], 200_000, 0, 43, None)], |
| 2347 | + ); |
| 2348 | + |
| 2349 | + nodes[1].node.handle_funding_created(node_a_id, &funding_created_msgs[0]); |
| 2350 | + check_added_monitors(&nodes[1], 1); |
| 2351 | + expect_channel_pending_event(&nodes[1], &node_a_id); |
| 2352 | + let funding_signed_msg = |
| 2353 | + get_event_msg!(nodes[1], MessageSendEvent::SendFundingSigned, node_a_id); |
| 2354 | + nodes[0].node.handle_funding_signed(node_b_id, &funding_signed_msg); |
| 2355 | + check_added_monitors(&nodes[0], 1); |
| 2356 | + |
| 2357 | + nodes[2].node.handle_funding_created(node_a_id, &funding_created_msgs[1]); |
| 2358 | + check_added_monitors(&nodes[2], 1); |
| 2359 | + expect_channel_pending_event(&nodes[2], &node_a_id); |
| 2360 | + let funding_signed_msg = |
| 2361 | + get_event_msg!(nodes[2], MessageSendEvent::SendFundingSigned, node_a_id); |
| 2362 | + nodes[0].node.handle_funding_signed(node_c_id, &funding_signed_msg); |
| 2363 | + check_added_monitors(&nodes[0], 1); |
| 2364 | + |
| 2365 | + // The batch funding transaction has been broadcast once now that both channels' monitors |
| 2366 | + // completed. |
| 2367 | + let _ = nodes[0].node.get_and_clear_pending_events(); |
| 2368 | + let initial = nodes[0].tx_broadcaster.txn_broadcast(); |
| 2369 | + assert_eq!(initial.len(), 1); |
| 2370 | + assert_eq!(initial[0], tx); |
| 2371 | + |
| 2372 | + // The channel IDs are derived from the outpoints in the funding tx. There's one outpoint |
| 2373 | + // per channel; their indices come from the funding script order the batch path uses. |
| 2374 | + let channel_id_b = |
| 2375 | + ChannelId::v1_from_funding_outpoint(OutPoint { txid: tx.compute_txid(), index: 0 }); |
| 2376 | + let channel_id_c = |
| 2377 | + ChannelId::v1_from_funding_outpoint(OutPoint { txid: tx.compute_txid(), index: 1 }); |
| 2378 | + |
| 2379 | + // A timer tick rebroadcasts the batch funding transaction exactly once, and the merged |
| 2380 | + // `channels` list contains both funded channels. |
| 2381 | + nodes[0].node.timer_tick_occurred(); |
| 2382 | + let rebroadcast = nodes[0].tx_broadcaster.txn_broadcast_with_types(); |
| 2383 | + assert_eq!(rebroadcast.len(), 1); |
| 2384 | + assert_eq!(rebroadcast[0].0, tx); |
| 2385 | + match &rebroadcast[0].1 { |
| 2386 | + TransactionType::Funding { channels } => { |
| 2387 | + assert_eq!(channels.len(), 2); |
| 2388 | + assert!(channels.contains(&(node_b_id, channel_id_b))); |
| 2389 | + assert!(channels.contains(&(node_c_id, channel_id_c))); |
| 2390 | + }, |
| 2391 | + other => panic!("Expected Funding, got {:?}", other), |
| 2392 | + } |
| 2393 | + |
| 2394 | + // Confirm the funding transaction and verify no more rebroadcasts occur. |
| 2395 | + mine_transaction(&nodes[0], &tx); |
| 2396 | + nodes[0].tx_broadcaster.clear(); |
| 2397 | + nodes[0].node.timer_tick_occurred(); |
| 2398 | + assert!(nodes[0].tx_broadcaster.txn_broadcast().is_empty()); |
| 2399 | +} |
| 2400 | + |
2234 | 2401 | #[xtest(feature = "_externalize_tests")] |
2235 | 2402 | pub fn test_funding_and_commitment_tx_confirm_same_block() { |
2236 | 2403 | // Tests that a node will forget the channel (when it only requires 1 confirmation) if the |
|
0 commit comments