Skip to content

Commit a99fb8a

Browse files
committed
Remove unnecessary funding tx clone & remote tx_sigs received check
We actually don't need to check if the counterparty had already sent their `tx_signatures` in `InteractiveTxSigningSession::received_tx_signatures`. Further, we can get rid of the clone of `funding_tx_opt` in `Channel::tx_signatures` when setting the `Context::funding_transaction` as we don't actually need to propagate it through to `ChannelManager::internal_tx_complete` as we can use `ChannelContext::unbroadcasted_funding()` which clones the `ChannelContext::funding_transaction` anyway.
1 parent 9fdb6ff commit a99fb8a

File tree

3 files changed

+27
-22
lines changed

3 files changed

+27
-22
lines changed

lightning/src/ln/channel.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5703,7 +5703,7 @@ impl<SP: Deref> Channel<SP> where
57035703
}
57045704
}
57055705

5706-
pub fn tx_signatures<L: Deref>(&mut self, msg: &msgs::TxSignatures, logger: &L) -> Result<(Option<msgs::TxSignatures>, Option<Transaction>), ChannelError>
5706+
pub fn tx_signatures<L: Deref>(&mut self, msg: &msgs::TxSignatures, logger: &L) -> Result<Option<msgs::TxSignatures>, ChannelError>
57075707
where L::Target: Logger
57085708
{
57095709
if !matches!(self.context.channel_state, ChannelState::FundingNegotiated) {
@@ -5740,25 +5740,23 @@ impl<SP: Deref> Channel<SP> where
57405740
// for spending. Doesn't seem to be anything in rust-bitcoin.
57415741
}
57425742

5743-
let (tx_signatures_opt, funding_tx_opt) = signing_session.received_tx_signatures(msg.clone())
5743+
let (holder_tx_signatures_opt, funding_tx_opt) = signing_session.received_tx_signatures(msg.clone())
57445744
.map_err(|_| ChannelError::Warn("Witness count did not match contributed input count".to_string()))?;
5745+
if holder_tx_signatures_opt.is_some() && self.is_awaiting_initial_mon_persist() {
5746+
log_debug!(logger, "Not sending tx_signatures: a monitor update is in progress. Setting monitor_pending_tx_signatures.");
5747+
self.context.monitor_pending_tx_signatures = holder_tx_signatures_opt;
5748+
return Ok(None);
5749+
}
57455750
if funding_tx_opt.is_some() {
5751+
// We have a persisted channel monitor and and a finalized funding transaction, so we can move
5752+
// the channel state forward, set the funding transaction and reset the signing session fields.
5753+
self.context.funding_transaction = funding_tx_opt;
5754+
self.context.next_funding_txid = None;
5755+
self.interactive_tx_signing_session = None;
57465756
self.context.channel_state = ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::new());
57475757
}
5748-
self.context.funding_transaction = funding_tx_opt.clone();
5749-
5750-
self.context.next_funding_txid = None;
5751-
5752-
// Clear out the signing session
5753-
self.interactive_tx_signing_session = None;
5754-
5755-
if tx_signatures_opt.is_some() && self.context.channel_state.is_monitor_update_in_progress() {
5756-
log_debug!(logger, "Not sending tx_signatures: a monitor update is in progress. Setting monitor_pending_tx_signatures.");
5757-
self.context.monitor_pending_tx_signatures = tx_signatures_opt;
5758-
return Ok((None, None));
5759-
}
57605758

5761-
Ok((tx_signatures_opt, funding_tx_opt))
5759+
Ok(holder_tx_signatures_opt)
57625760
} else {
57635761
Err(ChannelError::Close((
57645762
"Unexpected tx_signatures. No funding transaction awaiting signatures".to_string(),

lightning/src/ln/channelmanager.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8415,14 +8415,14 @@ where
84158415
match channel_phase {
84168416
ChannelPhase::Funded(chan) => {
84178417
let logger = WithChannelContext::from(&self.logger, &chan.context, None);
8418-
let (tx_signatures_opt, funding_tx_opt) = try_chan_phase_entry!(self, peer_state, chan.tx_signatures(msg, &&logger), chan_phase_entry);
8418+
let tx_signatures_opt = try_chan_phase_entry!(self, peer_state, chan.tx_signatures(msg, &&logger), chan_phase_entry);
84198419
if let Some(tx_signatures) = tx_signatures_opt {
84208420
peer_state.pending_msg_events.push(events::MessageSendEvent::SendTxSignatures {
84218421
node_id: *counterparty_node_id,
84228422
msg: tx_signatures,
84238423
});
84248424
}
8425-
if let Some(ref funding_tx) = funding_tx_opt {
8425+
if let Some(ref funding_tx) = chan.context.unbroadcasted_funding() {
84268426
self.tx_broadcaster.broadcast_transactions(&[funding_tx]);
84278427
{
84288428
let mut pending_events = self.pending_events.lock().unwrap();

lightning/src/ln/interactivetxs.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -316,14 +316,18 @@ impl InteractiveTxSigningSession {
316316

317317
/// Handles a `tx_signatures` message received from the counterparty.
318318
///
319+
/// If the holder is required to send their `tx_signatures` message and these signatures have
320+
/// already been provided to the signing session, then this return value will be `Some`, otherwise
321+
/// None.
322+
///
323+
/// If the holder has already provided their `tx_signatures` to the signing session, a funding
324+
/// transaction will be finalized and returned as Some, otherwise None.
325+
///
319326
/// Returns an error if the witness count does not equal the counterparty's input count in the
320327
/// unsigned transaction.
321328
pub fn received_tx_signatures(
322329
&mut self, tx_signatures: TxSignatures,
323330
) -> Result<(Option<TxSignatures>, Option<Transaction>), ()> {
324-
if self.counterparty_sent_tx_signatures {
325-
return Ok((None, None));
326-
};
327331
if self.remote_inputs_count() != tx_signatures.witnesses.len() {
328332
return Err(());
329333
}
@@ -336,13 +340,16 @@ impl InteractiveTxSigningSession {
336340
None
337341
};
338342

339-
let funding_tx = if self.holder_tx_signatures.is_some() {
343+
// Check if the holder has provided its signatures and if so,
344+
// return the finalized funding transaction.
345+
let funding_tx_opt = if self.holder_tx_signatures.is_some() {
340346
Some(self.finalize_funding_tx())
341347
} else {
348+
// This means we're still waiting for the holder to provide their signatures.
342349
None
343350
};
344351

345-
Ok((holder_tx_signatures, funding_tx))
352+
Ok((holder_tx_signatures, funding_tx_opt))
346353
}
347354

348355
/// Provides the holder witnesses for the unsigned transaction.

0 commit comments

Comments
 (0)