Skip to content

Fix panic when calling batch_funding_transaction_generated early #4015

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 7 additions & 13 deletions fuzz/src/full_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -857,21 +857,15 @@ pub fn do_test(mut data: &[u8], logger: &Arc<dyn Logger>) {
}
if tx.version.0 <= 0xff && !channels.is_empty() {
let chans = channels.iter().map(|(a, b)| (a, b)).collect::<Vec<_>>();
if let Err(e) =
Copy link
Contributor

@tnull tnull Aug 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, should we just allow this specific error case, i.e., still fail on other errors? Otherwise this might also relax our checks for other errors which we still might want to catch?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could, but I'm not really sure its worth bothering here - matching on error messages sucks, and "spurious failure to fund channel" seems like an issue that is better caught with normal functional tests (I imagine it'll be kinda hard to have a spurious failure here, anyway).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright.

channelmanager.batch_funding_transaction_generated(&chans, tx.clone())
{
// It's possible the channel has been closed in the mean time, but any other
// failure may be a bug.
if let APIError::ChannelUnavailable { .. } = e {
} else {
panic!();
let res =
channelmanager.batch_funding_transaction_generated(&chans, tx.clone());
if res.is_ok() {
let funding_txid = tx.compute_txid();
for idx in 0..tx.output.len() {
let outpoint = OutPoint { txid: funding_txid, index: idx as u16 };
pending_funding_signatures.insert(outpoint, tx.clone());
}
}
let funding_txid = tx.compute_txid();
for idx in 0..tx.output.len() {
let outpoint = OutPoint { txid: funding_txid, index: idx as u16 };
pending_funding_signatures.insert(outpoint, tx.clone());
}
}
},
11 => {
Expand Down
16 changes: 16 additions & 0 deletions lightning/src/ln/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1565,6 +1565,22 @@ where
)
}

/// Returns true if this channel is waiting on a (batch) funding transaction to be provided.
///
/// If this method returns true, [`Self::into_unfunded_outbound_v1`] will also succeed.
pub fn ready_to_fund(&self) -> bool {
if !self.funding().is_outbound() {
return false;
}
match self.context().channel_state {
ChannelState::NegotiatingFunding(flags) => {
debug_assert!(matches!(self.phase, ChannelPhase::UnfundedOutboundV1(_)));
flags.is_our_init_sent() && flags.is_their_init_sent()
},
_ => false,
}
}

pub fn into_unfunded_outbound_v1(self) -> Result<OutboundV1Channel<SP>, Self> {
if let ChannelPhase::UnfundedOutboundV1(channel) = self.phase {
Ok(channel)
Expand Down
Loading