Skip to content

Commit 5f3c6bf

Browse files
committed
Consolidate sign_closing_transaction callsites to get_closing_signed_msg
1 parent 33e6995 commit 5f3c6bf

File tree

2 files changed

+70
-69
lines changed

2 files changed

+70
-69
lines changed

lightning/src/ln/channel.rs

Lines changed: 68 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use bitcoin::secp256k1;
2828
use crate::ln::types::{ChannelId, PaymentPreimage, PaymentHash};
2929
use crate::ln::features::{ChannelTypeFeatures, InitFeatures};
3030
use crate::ln::msgs;
31-
use crate::ln::msgs::DecodeError;
31+
use crate::ln::msgs::{ClosingSigned, ClosingSignedFeeRange, DecodeError};
3232
use crate::ln::script::{self, ShutdownScript};
3333
use crate::ln::channel_state::{ChannelShutdownState, CounterpartyForwardingInfo, InboundHTLCDetails, InboundHTLCStateDetails, OutboundHTLCDetails, OutboundHTLCStateDetails};
3434
use crate::ln::channelmanager::{self, PendingHTLCStatus, HTLCSource, SentHTLCId, HTLCFailureMsg, PendingHTLCInfo, RAACommitmentOrder, BREAKDOWN_TIMEOUT, MIN_CLTV_EXPIRY_DELTA, MAX_LOCAL_BREAKDOWN_TIMEOUT};
@@ -5933,7 +5933,7 @@ impl<SP: Deref> Channel<SP> where
59335933

59345934
if !self.context.is_outbound() {
59355935
if let Some(msg) = &self.context.pending_counterparty_closing_signed.take() {
5936-
return self.closing_signed(fee_estimator, &msg);
5936+
return self.closing_signed(fee_estimator, &msg, logger);
59375937
}
59385938
return Ok((None, None, None));
59395939
}
@@ -5951,27 +5951,11 @@ impl<SP: Deref> Channel<SP> where
59515951
log_trace!(logger, "Proposing initial closing_signed for our counterparty with a fee range of {}-{} sat (with initial proposal {} sats)",
59525952
our_min_fee, our_max_fee, total_fee_satoshis);
59535953

5954-
match &self.context.holder_signer {
5955-
ChannelSignerType::Ecdsa(ecdsa) => {
5956-
let sig = ecdsa
5957-
.sign_closing_transaction(&closing_tx, &self.context.secp_ctx)
5958-
.map_err(|()| ChannelError::close("Failed to get signature for closing transaction.".to_owned()))?;
5959-
5960-
self.context.last_sent_closing_fee = Some((total_fee_satoshis, sig.clone()));
5961-
Ok((Some(msgs::ClosingSigned {
5962-
channel_id: self.context.channel_id,
5963-
fee_satoshis: total_fee_satoshis,
5964-
signature: sig,
5965-
fee_range: Some(msgs::ClosingSignedFeeRange {
5966-
min_fee_satoshis: our_min_fee,
5967-
max_fee_satoshis: our_max_fee,
5968-
}),
5969-
}), None, None))
5970-
},
5971-
// TODO (taproot|arik)
5972-
#[cfg(taproot)]
5973-
_ => todo!()
5954+
let closing_signed = self.get_closing_signed_msg(&closing_tx, false, total_fee_satoshis, our_min_fee, our_max_fee, logger);
5955+
if closing_signed.is_none() {
5956+
return Err(ChannelError::close("Failed to get signature for closing transaction.".to_owned()));
59745957
}
5958+
Ok((closing_signed, None, None))
59755959
}
59765960

59775961
// Marks a channel as waiting for a response from the counterparty. If it's not received
@@ -6118,10 +6102,38 @@ impl<SP: Deref> Channel<SP> where
61186102
tx
61196103
}
61206104

6121-
pub fn closing_signed<F: Deref>(
6122-
&mut self, fee_estimator: &LowerBoundedFeeEstimator<F>, msg: &msgs::ClosingSigned)
6105+
fn get_closing_signed_msg<L: Deref>(
6106+
&mut self, closing_tx: &ClosingTransaction, skip_remote_output: bool,
6107+
fee_satoshis: u64, min_fee_satoshis: u64, max_fee_satoshis: u64, logger: &L
6108+
) -> Option<msgs::ClosingSigned>
6109+
where L::Target: Logger
6110+
{
6111+
match &self.context.holder_signer {
6112+
ChannelSignerType::Ecdsa(ecdsa) => {
6113+
let fee_range = msgs::ClosingSignedFeeRange {
6114+
min_fee_satoshis,
6115+
max_fee_satoshis,
6116+
};
6117+
let sig = ecdsa.sign_closing_transaction(closing_tx, &self.context.secp_ctx).ok()?;
6118+
6119+
self.context.last_sent_closing_fee = Some((fee_satoshis, sig.clone()));
6120+
Some(msgs::ClosingSigned {
6121+
channel_id: self.context.channel_id,
6122+
fee_satoshis,
6123+
signature: sig,
6124+
fee_range: Some(fee_range),
6125+
})
6126+
},
6127+
// TODO (taproot|arik)
6128+
#[cfg(taproot)]
6129+
_ => todo!()
6130+
}
6131+
}
6132+
6133+
pub fn closing_signed<F: Deref, L: Deref>(
6134+
&mut self, fee_estimator: &LowerBoundedFeeEstimator<F>, msg: &msgs::ClosingSigned, logger: &L)
61236135
-> Result<(Option<msgs::ClosingSigned>, Option<Transaction>, Option<ShutdownResult>), ChannelError>
6124-
where F::Target: FeeEstimator
6136+
where F::Target: FeeEstimator, L::Target: Logger
61256137
{
61266138
if !self.context.channel_state.is_both_sides_shutdown() {
61276139
return Err(ChannelError::close("Remote end sent us a closing_signed before both sides provided a shutdown".to_owned()));
@@ -6146,7 +6158,8 @@ impl<SP: Deref> Channel<SP> where
61466158
}
61476159

61486160
let funding_redeemscript = self.context.get_funding_redeemscript();
6149-
let (mut closing_tx, used_total_fee) = self.build_closing_transaction(msg.fee_satoshis, false);
6161+
let mut skip_remote_output = false;
6162+
let (mut closing_tx, used_total_fee) = self.build_closing_transaction(msg.fee_satoshis, skip_remote_output);
61506163
if used_total_fee != msg.fee_satoshis {
61516164
return Err(ChannelError::close(format!("Remote sent us a closing_signed with a fee other than the value they can claim. Fee in message: {}. Actual closing tx fee: {}", msg.fee_satoshis, used_total_fee)));
61526165
}
@@ -6157,7 +6170,8 @@ impl<SP: Deref> Channel<SP> where
61576170
Err(_e) => {
61586171
// The remote end may have decided to revoke their output due to inconsistent dust
61596172
// limits, so check for that case by re-checking the signature here.
6160-
closing_tx = self.build_closing_transaction(msg.fee_satoshis, true).0;
6173+
skip_remote_output = true;
6174+
closing_tx = self.build_closing_transaction(msg.fee_satoshis, skip_remote_output).0;
61616175
let sighash = closing_tx.trust().get_sighash_all(&funding_redeemscript, self.context.channel_value_satoshis);
61626176
secp_check!(self.context.secp_ctx.verify_ecdsa(&sighash, &msg.signature, self.context.counterparty_funding_pubkey()), "Invalid closing tx signature from peer".to_owned());
61636177
},
@@ -6204,50 +6218,36 @@ impl<SP: Deref> Channel<SP> where
62046218
let (closing_tx, used_fee) = if $new_fee == msg.fee_satoshis {
62056219
(closing_tx, $new_fee)
62066220
} else {
6207-
self.build_closing_transaction($new_fee, false)
6221+
skip_remote_output = false;
6222+
self.build_closing_transaction($new_fee, skip_remote_output)
62086223
};
62096224

6210-
return match &self.context.holder_signer {
6211-
ChannelSignerType::Ecdsa(ecdsa) => {
6212-
let sig = ecdsa
6213-
.sign_closing_transaction(&closing_tx, &self.context.secp_ctx)
6214-
.map_err(|_| ChannelError::close("External signer refused to sign closing transaction".to_owned()))?;
6215-
let (signed_tx, shutdown_result) = if $new_fee == msg.fee_satoshis {
6216-
let shutdown_result = ShutdownResult {
6217-
closure_reason,
6218-
monitor_update: None,
6219-
dropped_outbound_htlcs: Vec::new(),
6220-
unbroadcasted_batch_funding_txid: self.context.unbroadcasted_batch_funding_txid(),
6221-
channel_id: self.context.channel_id,
6222-
user_channel_id: self.context.user_id,
6223-
channel_capacity_satoshis: self.context.channel_value_satoshis,
6224-
counterparty_node_id: self.context.counterparty_node_id,
6225-
unbroadcasted_funding_tx: self.context.unbroadcasted_funding(),
6226-
channel_funding_txo: self.context.get_funding_txo(),
6227-
};
6228-
self.context.channel_state = ChannelState::ShutdownComplete;
6229-
self.context.update_time_counter += 1;
6230-
let tx = self.build_signed_closing_transaction(&closing_tx, &msg.signature, &sig);
6231-
(Some(tx), Some(shutdown_result))
6232-
} else {
6233-
(None, None)
6234-
};
6235-
6236-
self.context.last_sent_closing_fee = Some((used_fee, sig.clone()));
6237-
Ok((Some(msgs::ClosingSigned {
6238-
channel_id: self.context.channel_id,
6239-
fee_satoshis: used_fee,
6240-
signature: sig,
6241-
fee_range: Some(msgs::ClosingSignedFeeRange {
6242-
min_fee_satoshis: our_min_fee,
6243-
max_fee_satoshis: our_max_fee,
6244-
}),
6245-
}), signed_tx, shutdown_result))
6246-
},
6247-
// TODO (taproot|arik)
6248-
#[cfg(taproot)]
6249-
_ => todo!()
6225+
let closing_signed = self.get_closing_signed_msg(&closing_tx, skip_remote_output, used_fee, our_min_fee, our_max_fee, logger);
6226+
if closing_signed.is_none() {
6227+
return Err(ChannelError::close("Failed to get signature for closing transaction.".to_owned()));
62506228
}
6229+
let (signed_tx, shutdown_result) = if $new_fee == msg.fee_satoshis {
6230+
let shutdown_result = ShutdownResult {
6231+
closure_reason,
6232+
monitor_update: None,
6233+
dropped_outbound_htlcs: Vec::new(),
6234+
unbroadcasted_batch_funding_txid: self.context.unbroadcasted_batch_funding_txid(),
6235+
channel_id: self.context.channel_id,
6236+
user_channel_id: self.context.user_id,
6237+
channel_capacity_satoshis: self.context.channel_value_satoshis,
6238+
counterparty_node_id: self.context.counterparty_node_id,
6239+
unbroadcasted_funding_tx: self.context.unbroadcasted_funding(),
6240+
channel_funding_txo: self.context.get_funding_txo(),
6241+
};
6242+
self.context.channel_state = ChannelState::ShutdownComplete;
6243+
self.context.update_time_counter += 1;
6244+
let tx = closing_signed.as_ref().map(|ClosingSigned { signature, .. }|
6245+
self.build_signed_closing_transaction(&closing_tx, &msg.signature, signature));
6246+
(tx, Some(shutdown_result))
6247+
} else {
6248+
(None, None)
6249+
};
6250+
return Ok((closing_signed, signed_tx, shutdown_result))
62516251
}
62526252
}
62536253

lightning/src/ln/channelmanager.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7793,7 +7793,8 @@ where
77937793
match peer_state.channel_by_id.entry(msg.channel_id.clone()) {
77947794
hash_map::Entry::Occupied(mut chan_phase_entry) => {
77957795
if let ChannelPhase::Funded(chan) = chan_phase_entry.get_mut() {
7796-
let (closing_signed, tx, shutdown_result) = try_chan_phase_entry!(self, chan.closing_signed(&self.fee_estimator, &msg), chan_phase_entry);
7796+
let logger = WithChannelContext::from(&self.logger, &chan.context, None);
7797+
let (closing_signed, tx, shutdown_result) = try_chan_phase_entry!(self, chan.closing_signed(&self.fee_estimator, &msg, &&logger), chan_phase_entry);
77977798
debug_assert_eq!(shutdown_result.is_some(), chan.is_shutdown());
77987799
if let Some(msg) = closing_signed {
77997800
peer_state.pending_msg_events.push(events::MessageSendEvent::SendClosingSigned {

0 commit comments

Comments
 (0)