Skip to content

Commit 15c9f5b

Browse files
authored
Merge pull request #2841 from TheBlueMatt/2024-01-batch-deadlock
Fix deadlock when handling bad calls to `batch_funding.._generated`
2 parents 444740c + 983ca37 commit 15c9f5b

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

lightning/src/ln/channelmanager.rs

+1
Original file line numberDiff line numberDiff line change
@@ -3984,6 +3984,7 @@ where
39843984
});
39853985
}
39863986
}
3987+
mem::drop(funding_batch_states);
39873988
for shutdown_result in shutdown_results.drain(..) {
39883989
self.finish_close_channel(shutdown_result);
39893990
}

lightning/src/ln/shutdown_tests.rs

+44-3
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@
77
// You may not use this file except in accordance with one or both of these
88
// licenses.
99

10-
//! Tests of our shutdown and closing_signed negotiation logic.
10+
//! Tests of our shutdown and closing_signed negotiation logic as well as some assorted force-close
11+
//! handling tests.
1112
1213
use crate::sign::{EntropySource, SignerProvider};
1314
use crate::chain::ChannelMonitorUpdateStatus;
1415
use crate::chain::transaction::OutPoint;
15-
use crate::events::{MessageSendEvent, HTLCDestination, MessageSendEventsProvider, ClosureReason};
16+
use crate::events::{Event, MessageSendEvent, HTLCDestination, MessageSendEventsProvider, ClosureReason};
1617
use crate::ln::channelmanager::{self, PaymentSendFailure, PaymentId, RecipientOnionFields, Retry, ChannelShutdownState, ChannelDetails};
1718
use crate::routing::router::{PaymentParameters, get_route, RouteParameters};
18-
use crate::ln::msgs;
19+
use crate::ln::{ChannelId, msgs};
1920
use crate::ln::msgs::{ChannelMessageHandler, ErrorAction};
2021
use crate::ln::onion_utils::INVALID_ONION_BLINDING;
2122
use crate::ln::script::ShutdownScript;
@@ -25,6 +26,8 @@ use crate::util::errors::APIError;
2526
use crate::util::config::UserConfig;
2627
use crate::util::string::UntrustedString;
2728

29+
use bitcoin::{Transaction, TxOut};
30+
use bitcoin::blockdata::locktime::absolute::LockTime;
2831
use bitcoin::blockdata::script::Builder;
2932
use bitcoin::blockdata::opcodes;
3033
use bitcoin::network::constants::Network;
@@ -1375,3 +1378,41 @@ fn outbound_update_no_early_closing_signed() {
13751378
do_outbound_update_no_early_closing_signed(true);
13761379
do_outbound_update_no_early_closing_signed(false);
13771380
}
1381+
1382+
#[test]
1383+
fn batch_funding_failure() {
1384+
// Provides test coverage of batch funding failure, which previously deadlocked
1385+
let chanmon_cfgs = create_chanmon_cfgs(4);
1386+
let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
1387+
let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
1388+
let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
1389+
1390+
exchange_open_accept_chan(&nodes[0], &nodes[1], 1_000_000, 0);
1391+
exchange_open_accept_chan(&nodes[0], &nodes[2], 1_000_000, 0);
1392+
1393+
let events = nodes[0].node.get_and_clear_pending_events();
1394+
assert_eq!(events.len(), 2);
1395+
// Build a transaction which only has the output for one of the two channels we're trying to
1396+
// confirm. Previously this led to a deadlock in channel closure handling.
1397+
let mut tx = Transaction { version: 2, lock_time: LockTime::ZERO, input: Vec::new(), output: Vec::new() };
1398+
let mut chans = Vec::new();
1399+
for (idx, ev) in events.iter().enumerate() {
1400+
if let Event::FundingGenerationReady { temporary_channel_id, counterparty_node_id, output_script, .. } = ev {
1401+
if idx == 0 {
1402+
tx.output.push(TxOut { value: 1_000_000, script_pubkey: output_script.clone() });
1403+
}
1404+
chans.push((temporary_channel_id, counterparty_node_id));
1405+
} else { panic!(); }
1406+
}
1407+
1408+
// We should probably end up with an error for both channels, but currently we don't generate
1409+
// an error for the failing channel itself.
1410+
let err = "Error in transaction funding: Misuse error: No output matched the script_pubkey and value in the FundingGenerationReady event".to_string();
1411+
let close = [ExpectedCloseEvent::from_id_reason(ChannelId::v1_from_funding_txid(tx.txid().as_ref(), 0), true, ClosureReason::ProcessingError { err })];
1412+
1413+
nodes[0].node.batch_funding_transaction_generated(&chans, tx).unwrap_err();
1414+
1415+
get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, nodes[1].node.get_our_node_id());
1416+
check_closed_events(&nodes[0], &close);
1417+
assert_eq!(nodes[0].node.list_channels().len(), 0);
1418+
}

0 commit comments

Comments
 (0)