Skip to content

Commit 8aa518f

Browse files
committed
Add an infallible no-sign version of send_commitment_no_status_check
In the coming commits we'll move to async `ChannelMonitorUpdate` application, which means we'll want to generate a `ChannelMonitorUpdate` (including a new counterparty commitment transaction) before we actually send it to our counterparty. To do that today we'd have to actually sign the commitment transaction by calling the signer, then drop it, apply the `ChannelMonitorUpdate`, then re-sign the commitment transaction to send it to our peer. In this commit we instead split `send_commitment_no_status_check` and `send_commitment_no_state_update` into `build_` and `send_` variants, allowing us to generate new counterparty commitment transactions without actually signing, then build them for sending, with signatures, later.
1 parent 5be29c6 commit 8aa518f

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed

lightning/src/ln/channel.rs

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5791,8 +5791,16 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
57915791
Ok(Some(res))
57925792
}
57935793

5794-
/// Only fails in case of bad keys
5794+
/// Only fails in case of signer rejection.
57955795
fn send_commitment_no_status_check<L: Deref>(&mut self, logger: &L) -> Result<(msgs::CommitmentSigned, ChannelMonitorUpdate), ChannelError> where L::Target: Logger {
5796+
let monitor_update = self.build_commitment_no_status_check(logger);
5797+
match self.send_commitment_no_state_update(logger) {
5798+
Ok((commitment_signed, _)) => Ok((commitment_signed, monitor_update)),
5799+
Err(e) => Err(e),
5800+
}
5801+
}
5802+
5803+
fn build_commitment_no_status_check<L: Deref>(&mut self, logger: &L) -> ChannelMonitorUpdate where L::Target: Logger {
57965804
log_trace!(logger, "Updating HTLC state for a newly-sent commitment_signed...");
57975805
// We can upgrade the status of some HTLCs that are waiting on a commitment, even if we
57985806
// fail to generate this, we still are at least at a position where upgrading their status
@@ -5825,15 +5833,9 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
58255833
}
58265834
self.resend_order = RAACommitmentOrder::RevokeAndACKFirst;
58275835

5828-
let (res, counterparty_commitment_txid, htlcs) = match self.send_commitment_no_state_update(logger) {
5829-
Ok((res, (counterparty_commitment_tx, mut htlcs))) => {
5830-
// Update state now that we've passed all the can-fail calls...
5831-
let htlcs_no_ref: Vec<(HTLCOutputInCommitment, Option<Box<HTLCSource>>)> =
5832-
htlcs.drain(..).map(|(htlc, htlc_source)| (htlc, htlc_source.map(|source_ref| Box::new(source_ref.clone())))).collect();
5833-
(res, counterparty_commitment_tx, htlcs_no_ref)
5834-
},
5835-
Err(e) => return Err(e),
5836-
};
5836+
let (counterparty_commitment_txid, mut htlcs_ref) = self.build_commitment_no_state_update(logger);
5837+
let htlcs: Vec<(HTLCOutputInCommitment, Option<Box<HTLCSource>>)> =
5838+
htlcs_ref.drain(..).map(|(htlc, htlc_source)| (htlc, htlc_source.map(|source_ref| Box::new(source_ref.clone())))).collect();
58375839

58385840
if self.announcement_sigs_state == AnnouncementSigsState::MessageSent {
58395841
self.announcement_sigs_state = AnnouncementSigsState::Committed;
@@ -5850,16 +5852,13 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
58505852
}]
58515853
};
58525854
self.channel_state |= ChannelState::AwaitingRemoteRevoke as u32;
5853-
Ok((res, monitor_update))
5855+
monitor_update
58545856
}
58555857

5856-
/// Only fails in case of bad keys. Used for channel_reestablish commitment_signed generation
5857-
/// when we shouldn't change HTLC/channel state.
5858-
fn send_commitment_no_state_update<L: Deref>(&self, logger: &L) -> Result<(msgs::CommitmentSigned, (Txid, Vec<(HTLCOutputInCommitment, Option<&HTLCSource>)>)), ChannelError> where L::Target: Logger {
5858+
fn build_commitment_no_state_update<L: Deref>(&self, logger: &L) -> (Txid, Vec<(HTLCOutputInCommitment, Option<&HTLCSource>)>) where L::Target: Logger {
58595859
let counterparty_keys = self.build_remote_transaction_keys();
58605860
let commitment_stats = self.build_commitment_transaction(self.cur_counterparty_commitment_transaction_number, &counterparty_keys, false, true, logger);
58615861
let counterparty_commitment_txid = commitment_stats.tx.trust().txid();
5862-
let (signature, htlc_signatures);
58635862

58645863
#[cfg(any(test, fuzzing))]
58655864
{
@@ -5879,6 +5878,21 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
58795878
}
58805879
}
58815880

5881+
(counterparty_commitment_txid, commitment_stats.htlcs_included)
5882+
}
5883+
5884+
/// Only fails in case of signer rejection. Used for channel_reestablish commitment_signed
5885+
/// generation when we shouldn't change HTLC/channel state.
5886+
fn send_commitment_no_state_update<L: Deref>(&self, logger: &L) -> Result<(msgs::CommitmentSigned, (Txid, Vec<(HTLCOutputInCommitment, Option<&HTLCSource>)>)), ChannelError> where L::Target: Logger {
5887+
// Get the fee tests from `build_commitment_no_state_update`
5888+
#[cfg(any(test, fuzzing))]
5889+
self.build_commitment_no_state_update(logger);
5890+
5891+
let counterparty_keys = self.build_remote_transaction_keys();
5892+
let commitment_stats = self.build_commitment_transaction(self.cur_counterparty_commitment_transaction_number, &counterparty_keys, false, true, logger);
5893+
let counterparty_commitment_txid = commitment_stats.tx.trust().txid();
5894+
let (signature, htlc_signatures);
5895+
58825896
{
58835897
let mut htlcs = Vec::with_capacity(commitment_stats.htlcs_included.len());
58845898
for &(ref htlc, _) in commitment_stats.htlcs_included.iter() {

0 commit comments

Comments
 (0)