Skip to content

Commit

Permalink
Move setting signer flags to get_funding_created_msg
Browse files Browse the repository at this point in the history
This also slightly refactors get_funding_signed_msg to match the logic
more similarly, as well as removes a log to fix a nit from lightningdevkit#3152.
  • Loading branch information
alecchendev committed Sep 16, 2024
1 parent 423a1b6 commit 83881ca
Showing 1 changed file with 39 additions and 44 deletions.
83 changes: 39 additions & 44 deletions lightning/src/ln/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3540,38 +3540,38 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
log_trace!(logger, "Initial counterparty tx for channel {} is: txid {} tx {}",
&self.channel_id(), counterparty_initial_bitcoin_tx.txid, encode::serialize_hex(&counterparty_initial_bitcoin_tx.transaction));

match &self.holder_signer {
let signature = match &self.holder_signer {
// TODO (arik): move match into calling method for Taproot
ChannelSignerType::Ecdsa(ecdsa) => {
let funding_signed = ecdsa.sign_counterparty_commitment(&counterparty_initial_commitment_tx, Vec::new(), Vec::new(), &self.secp_ctx)
.map(|(signature, _)| msgs::FundingSigned {
channel_id: self.channel_id(),
signature,
#[cfg(taproot)]
partial_signature_with_nonce: None,
})
.ok();

if funding_signed.is_none() {
#[cfg(not(async_signing))] {
panic!("Failed to get signature for funding_signed");
}
#[cfg(async_signing)] {
log_trace!(logger, "Counterparty commitment signature not available for funding_signed message; setting signer_pending_funding");
self.signer_pending_funding = true;
}
} else if self.signer_pending_funding {
log_trace!(logger, "Counterparty commitment signature available for funding_signed message; clearing signer_pending_funding");
self.signer_pending_funding = false;
}

// We sign "counterparty" commitment transaction, allowing them to broadcast the tx if they wish.
(counterparty_initial_commitment_tx, funding_signed)
},
ChannelSignerType::Ecdsa(ecdsa) => ecdsa.sign_counterparty_commitment(
&counterparty_initial_commitment_tx, Vec::new(), Vec::new(), &self.secp_ctx
).ok(),
// TODO (taproot|arik)
#[cfg(taproot)]
_ => todo!()
};

if signature.is_some() && self.signer_pending_funding {
log_trace!(logger, "Counterparty commitment signature available for funding_signed message; clearing signer_pending_funding");
self.signer_pending_funding = false;
} else if signature.is_none() {
#[cfg(not(async_signing))] {
panic!("Failed to get signature for funding_signed");
}
#[cfg(async_signing)] {
log_trace!(logger, "Counterparty commitment signature not available for funding_signed message; setting signer_pending_funding");
self.signer_pending_funding = true;
}
}

let funding_signed = signature.map(|(signature, _)| msgs::FundingSigned {
channel_id: self.channel_id(),
signature,
#[cfg(taproot)]
partial_signature_with_nonce: None,
});

// We sign "counterparty" commitment transaction, allowing them to broadcast the tx if they wish.
(counterparty_initial_commitment_tx, funding_signed)
}

/// If we receive an error message when attempting to open a channel, it may only be a rejection
Expand Down Expand Up @@ -7681,19 +7681,27 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
// TODO (taproot|arik): move match into calling method for Taproot
ChannelSignerType::Ecdsa(ecdsa) => {
ecdsa.sign_counterparty_commitment(&counterparty_initial_commitment_tx, Vec::new(), Vec::new(), &self.context.secp_ctx)
.map(|(sig, _)| sig).ok()?
.map(|(sig, _)| sig).ok()
},
// TODO (taproot|arik)
#[cfg(taproot)]
_ => todo!()
};

if self.context.signer_pending_funding {
if signature.is_some() && self.context.signer_pending_funding {
log_trace!(logger, "Counterparty commitment signature ready for funding_created message: clearing signer_pending_funding");
self.context.signer_pending_funding = false;
}
} else if signature.is_none() {
#[cfg(not(async_signing))] {
panic!("Failed to get signature for new funding creation");
}
#[cfg(async_signing)] {
log_trace!(logger, "funding_created awaiting signer; setting signer_pending_funding");
self.context.signer_pending_funding = true;
}
};

Some(msgs::FundingCreated {
signature.map(|signature| msgs::FundingCreated {
temporary_channel_id: self.context.temporary_channel_id.unwrap(),
funding_txid: self.context.channel_transaction_parameters.funding_outpoint.as_ref().unwrap().txid,
funding_output_index: self.context.channel_transaction_parameters.funding_outpoint.as_ref().unwrap().index,
Expand Down Expand Up @@ -7749,18 +7757,6 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
self.context.is_batch_funding = Some(()).filter(|_| is_batch_funding);

let funding_created = self.get_funding_created_msg(logger);
if funding_created.is_none() {
#[cfg(not(async_signing))] {
panic!("Failed to get signature for new funding creation");
}
#[cfg(async_signing)] {
if !self.context.signer_pending_funding {
log_trace!(logger, "funding_created awaiting signer; setting signer_pending_funding");
self.context.signer_pending_funding = true;
}
}
}

Ok(funding_created)
}

Expand Down Expand Up @@ -7988,7 +7984,6 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
};
let funding_created = if self.context.signer_pending_funding && self.context.is_outbound() {
log_trace!(logger, "Attempting to generate pending funding created...");
self.context.signer_pending_funding = false;
self.get_funding_created_msg(logger)
} else { None };
(open_channel, funding_created)
Expand Down

0 comments on commit 83881ca

Please sign in to comment.