Skip to content

Commit bb5621b

Browse files
alecchendevvalentinewallace
authored andcommitted
Move setting signer flags to get_funding_created_msg
For all of our async signing logic in channel establishment v1, we set signer flags in the method where we create the raw lightning message object. To keep things consistent, this commit moves setting the signer flags to where we create funding_created, since this was being set elsewhere before. While we're doing this cleanup, this also slightly refactors our funding_signed method to move some code out of an indent, as well as removes a log to fix a nit from lightningdevkit#3152.
1 parent d66866e commit bb5621b

File tree

1 file changed

+37
-44
lines changed

1 file changed

+37
-44
lines changed

lightning/src/ln/channel.rs

+37-44
Original file line numberDiff line numberDiff line change
@@ -3939,38 +3939,36 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
39393939
log_trace!(logger, "Initial counterparty tx for channel {} is: txid {} tx {}",
39403940
&self.channel_id(), counterparty_initial_bitcoin_tx.txid, encode::serialize_hex(&counterparty_initial_bitcoin_tx.transaction));
39413941

3942-
match &self.holder_signer {
3942+
// We sign "counterparty" commitment transaction, allowing them to broadcast the tx if they wish.
3943+
let signature = match &self.holder_signer {
39433944
// TODO (arik): move match into calling method for Taproot
3944-
ChannelSignerType::Ecdsa(ecdsa) => {
3945-
let funding_signed = ecdsa.sign_counterparty_commitment(&counterparty_initial_commitment_tx, Vec::new(), Vec::new(), &self.secp_ctx)
3946-
.map(|(signature, _)| msgs::FundingSigned {
3947-
channel_id: self.channel_id(),
3948-
signature,
3949-
#[cfg(taproot)]
3950-
partial_signature_with_nonce: None,
3951-
})
3952-
.ok();
3953-
3954-
if funding_signed.is_none() {
3955-
#[cfg(not(async_signing))] {
3956-
panic!("Failed to get signature for funding_signed");
3957-
}
3958-
#[cfg(async_signing)] {
3959-
log_trace!(logger, "Counterparty commitment signature not available for funding_signed message; setting signer_pending_funding");
3960-
self.signer_pending_funding = true;
3961-
}
3962-
} else if self.signer_pending_funding {
3963-
log_trace!(logger, "Counterparty commitment signature available for funding_signed message; clearing signer_pending_funding");
3964-
self.signer_pending_funding = false;
3965-
}
3966-
3967-
// We sign "counterparty" commitment transaction, allowing them to broadcast the tx if they wish.
3968-
funding_signed
3969-
},
3945+
ChannelSignerType::Ecdsa(ecdsa) => ecdsa.sign_counterparty_commitment(
3946+
&counterparty_initial_commitment_tx, Vec::new(), Vec::new(), &self.secp_ctx
3947+
).ok(),
39703948
// TODO (taproot|arik)
39713949
#[cfg(taproot)]
39723950
_ => todo!()
3951+
};
3952+
3953+
if signature.is_some() && self.signer_pending_funding {
3954+
log_trace!(logger, "Counterparty commitment signature available for funding_signed message; clearing signer_pending_funding");
3955+
self.signer_pending_funding = false;
3956+
} else if signature.is_none() {
3957+
#[cfg(not(async_signing))] {
3958+
panic!("Failed to get signature for funding_signed");
3959+
}
3960+
#[cfg(async_signing)] {
3961+
log_trace!(logger, "Counterparty commitment signature not available for funding_signed message; setting signer_pending_funding");
3962+
self.signer_pending_funding = true;
3963+
}
39733964
}
3965+
3966+
signature.map(|(signature, _)| msgs::FundingSigned {
3967+
channel_id: self.channel_id(),
3968+
signature,
3969+
#[cfg(taproot)]
3970+
partial_signature_with_nonce: None,
3971+
})
39743972
}
39753973

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

8358-
if self.context.signer_pending_funding {
8356+
if signature.is_some() && self.context.signer_pending_funding {
83598357
log_trace!(logger, "Counterparty commitment signature ready for funding_created message: clearing signer_pending_funding");
83608358
self.context.signer_pending_funding = false;
8361-
}
8359+
} else if signature.is_none() {
8360+
#[cfg(not(async_signing))] {
8361+
panic!("Failed to get signature for new funding creation");
8362+
}
8363+
#[cfg(async_signing)] {
8364+
log_trace!(logger, "funding_created awaiting signer; setting signer_pending_funding");
8365+
self.context.signer_pending_funding = true;
8366+
}
8367+
};
83628368

8363-
Some(msgs::FundingCreated {
8369+
signature.map(|signature| msgs::FundingCreated {
83648370
temporary_channel_id: self.context.temporary_channel_id.unwrap(),
83658371
funding_txid: self.context.channel_transaction_parameters.funding_outpoint.as_ref().unwrap().txid,
83668372
funding_output_index: self.context.channel_transaction_parameters.funding_outpoint.as_ref().unwrap().index,
@@ -8413,18 +8419,6 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
84138419
self.context.is_batch_funding = Some(()).filter(|_| is_batch_funding);
84148420

84158421
let funding_created = self.get_funding_created_msg(logger);
8416-
if funding_created.is_none() {
8417-
#[cfg(not(async_signing))] {
8418-
panic!("Failed to get signature for new funding creation");
8419-
}
8420-
#[cfg(async_signing)] {
8421-
if !self.context.signer_pending_funding {
8422-
log_trace!(logger, "funding_created awaiting signer; setting signer_pending_funding");
8423-
self.context.signer_pending_funding = true;
8424-
}
8425-
}
8426-
}
8427-
84288422
Ok(funding_created)
84298423
}
84308424

@@ -8582,7 +8576,6 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
85828576
} else { None };
85838577
let funding_created = if self.context.signer_pending_funding && self.context.is_outbound() {
85848578
log_trace!(logger, "Attempting to generate pending funding created...");
8585-
self.context.signer_pending_funding = false;
85868579
self.get_funding_created_msg(logger)
85878580
} else { None };
85888581
(open_channel, funding_created)

0 commit comments

Comments
 (0)