Skip to content

Commit 26b4f39

Browse files
committed
Close channels when find_funding_output fails to find an output
In `funding_transaction_generated_intern`, if `find_funding_output` fails (e.g. due to the require output not being present in the provided funding transaction) we'd previously not generated a `ChannelClosed` event which leaves users possibly in a confused state. Here we fix this, also fixing the relevant tests to check for the new event. Fixes #2843.
1 parent 2b14cc4 commit 26b4f39

File tree

2 files changed

+52
-29
lines changed

2 files changed

+52
-29
lines changed

lightning/src/ln/channelmanager.rs

+31-23
Original file line numberDiff line numberDiff line change
@@ -4480,7 +4480,7 @@ where
44804480

44814481
/// Handles the generation of a funding transaction, optionally (for tests) with a function
44824482
/// which checks the correctness of the funding transaction given the associated channel.
4483-
fn funding_transaction_generated_intern<FundingOutput: FnMut(&OutboundV1Channel<SP>, &Transaction) -> Result<OutPoint, APIError>>(
4483+
fn funding_transaction_generated_intern<FundingOutput: FnMut(&OutboundV1Channel<SP>, &Transaction) -> Result<OutPoint, &'static str>>(
44844484
&self, temporary_channel_id: &ChannelId, counterparty_node_id: &PublicKey, funding_transaction: Transaction, is_batch_funding: bool,
44854485
mut find_funding_output: FundingOutput,
44864486
) -> Result<(), APIError> {
@@ -4493,26 +4493,38 @@ where
44934493
let funding_txo;
44944494
let (mut chan, msg_opt) = match peer_state.channel_by_id.remove(temporary_channel_id) {
44954495
Some(ChannelPhase::UnfundedOutboundV1(mut chan)) => {
4496-
funding_txo = find_funding_output(&chan, &funding_transaction)?;
4496+
macro_rules! close_chan { ($err: expr, $api_err: expr, $chan: expr) => { {
4497+
let counterparty;
4498+
let err = if let ChannelError::Close(msg) = $err {
4499+
let channel_id = $chan.context.channel_id();
4500+
counterparty = chan.context.get_counterparty_node_id();
4501+
let reason = ClosureReason::ProcessingError { err: msg.clone() };
4502+
let shutdown_res = $chan.context.force_shutdown(false, reason);
4503+
MsgHandleErrInternal::from_finish_shutdown(msg, channel_id, shutdown_res, None)
4504+
} else { unreachable!(); };
4505+
4506+
mem::drop(peer_state_lock);
4507+
mem::drop(per_peer_state);
4508+
let _: Result<(), _> = handle_error!(self, Err(err), counterparty);
4509+
Err($api_err)
4510+
} } }
4511+
match find_funding_output(&chan, &funding_transaction) {
4512+
Ok(found_funding_txo) => funding_txo = found_funding_txo,
4513+
Err(err) => {
4514+
let chan_err = ChannelError::Close(err.to_owned());
4515+
let api_err = APIError::APIMisuseError { err: err.to_owned() };
4516+
return close_chan!(chan_err, api_err, chan);
4517+
},
4518+
}
44974519

44984520
let logger = WithChannelContext::from(&self.logger, &chan.context);
4499-
let funding_res = chan.get_funding_created(funding_transaction, funding_txo, is_batch_funding, &&logger)
4500-
.map_err(|(mut chan, e)| if let ChannelError::Close(msg) = e {
4501-
let channel_id = chan.context.channel_id();
4502-
let reason = ClosureReason::ProcessingError { err: msg.clone() };
4503-
let shutdown_res = chan.context.force_shutdown(false, reason);
4504-
(chan, MsgHandleErrInternal::from_finish_shutdown(msg, channel_id, shutdown_res, None))
4505-
} else { unreachable!(); });
4521+
let funding_res = chan.get_funding_created(funding_transaction, funding_txo, is_batch_funding, &&logger);
45064522
match funding_res {
45074523
Ok(funding_msg) => (chan, funding_msg),
4508-
Err((chan, err)) => {
4509-
mem::drop(peer_state_lock);
4510-
mem::drop(per_peer_state);
4511-
let _: Result<(), _> = handle_error!(self, Err(err), chan.context.get_counterparty_node_id());
4512-
return Err(APIError::ChannelUnavailable {
4513-
err: "Signer refused to sign the initial commitment transaction".to_owned()
4514-
});
4515-
},
4524+
Err((mut chan, chan_err)) => {
4525+
let api_err = APIError::ChannelUnavailable { err: "Signer refused to sign the initial commitment transaction".to_owned() };
4526+
return close_chan!(chan_err, api_err, chan);
4527+
}
45164528
}
45174529
},
45184530
Some(phase) => {
@@ -4677,17 +4689,13 @@ where
46774689
for (idx, outp) in tx.output.iter().enumerate() {
46784690
if outp.script_pubkey == expected_spk && outp.value == chan.context.get_value_satoshis() {
46794691
if output_index.is_some() {
4680-
return Err(APIError::APIMisuseError {
4681-
err: "Multiple outputs matched the expected script and value".to_owned()
4682-
});
4692+
return Err("Multiple outputs matched the expected script and value");
46834693
}
46844694
output_index = Some(idx as u16);
46854695
}
46864696
}
46874697
if output_index.is_none() {
4688-
return Err(APIError::APIMisuseError {
4689-
err: "No output matched the script_pubkey and value in the FundingGenerationReady event".to_owned()
4690-
});
4698+
return Err("No output matched the script_pubkey and value in the FundingGenerationReady event");
46914699
}
46924700
let outpoint = OutPoint { txid: tx.txid(), index: output_index.unwrap() };
46934701
if let Some(funding_batch_state) = funding_batch_state.as_mut() {

lightning/src/ln/shutdown_tests.rs

+21-6
Original file line numberDiff line numberDiff line change
@@ -1401,8 +1401,8 @@ fn batch_funding_failure() {
14011401
let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
14021402
let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
14031403

1404-
exchange_open_accept_chan(&nodes[0], &nodes[1], 1_000_000, 0);
1405-
exchange_open_accept_chan(&nodes[0], &nodes[2], 1_000_000, 0);
1404+
let temp_chan_id_a = exchange_open_accept_chan(&nodes[0], &nodes[1], 1_000_000, 0);
1405+
let temp_chan_id_b = exchange_open_accept_chan(&nodes[0], &nodes[2], 1_000_000, 0);
14061406

14071407
let events = nodes[0].node.get_and_clear_pending_events();
14081408
assert_eq!(events.len(), 2);
@@ -1419,14 +1419,29 @@ fn batch_funding_failure() {
14191419
} else { panic!(); }
14201420
}
14211421

1422-
// We should probably end up with an error for both channels, but currently we don't generate
1423-
// an error for the failing channel itself.
14241422
let err = "Error in transaction funding: Misuse error: No output matched the script_pubkey and value in the FundingGenerationReady event".to_string();
1425-
let close = [ExpectedCloseEvent::from_id_reason(ChannelId::v1_from_funding_txid(tx.txid().as_ref(), 0), true, ClosureReason::ProcessingError { err })];
1423+
let temp_err = "No output matched the script_pubkey and value in the FundingGenerationReady event".to_string();
1424+
let post_funding_chan_id_a = ChannelId::v1_from_funding_txid(tx.txid().as_ref(), 0);
1425+
let close = [
1426+
ExpectedCloseEvent::from_id_reason(post_funding_chan_id_a, true, ClosureReason::ProcessingError { err: err.clone() }),
1427+
ExpectedCloseEvent::from_id_reason(temp_chan_id_b, false, ClosureReason::ProcessingError { err: temp_err }),
1428+
];
14261429

14271430
nodes[0].node.batch_funding_transaction_generated(&chans, tx).unwrap_err();
14281431

1429-
get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, nodes[1].node.get_our_node_id());
1432+
let msgs = nodes[0].node.get_and_clear_pending_msg_events();
1433+
assert_eq!(msgs.len(), 3);
1434+
// We currently spuriously send `FundingCreated` for the first channel and then immediately
1435+
// fail both channels, which isn't ideal but should be fine.
1436+
assert!(matches!(msgs[0],
1437+
MessageSendEvent::SendFundingCreated { msg: msgs::FundingCreated { temporary_channel_id: temp_chan_id_a, .. }, .. }
1438+
));
1439+
assert!(matches!(msgs[1],
1440+
MessageSendEvent::HandleError { action: msgs::ErrorAction::SendErrorMessage {
1441+
msg: msgs::ErrorMessage { channel_id: post_funding_chan_id_a, .. }, ..
1442+
}, .. }
1443+
));
1444+
14301445
check_closed_events(&nodes[0], &close);
14311446
assert_eq!(nodes[0].node.list_channels().len(), 0);
14321447
}

0 commit comments

Comments
 (0)