Skip to content

Commit ebd86b0

Browse files
committed
Refactor out first-closing-sent creation
1 parent 6a608a2 commit ebd86b0

File tree

1 file changed

+23
-25
lines changed

1 file changed

+23
-25
lines changed

src/ln/channel.rs

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2328,6 +2328,26 @@ impl Channel {
23282328
}
23292329
}
23302330

2331+
fn propose_first_closing_signed(&mut self, fee_estimator: &FeeEstimator) -> msgs::ClosingSigned {
2332+
let mut proposed_feerate = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Background);
2333+
if self.feerate_per_kw > proposed_feerate {
2334+
proposed_feerate = self.feerate_per_kw;
2335+
}
2336+
let tx_weight = Self::get_closing_transaction_weight(&self.get_closing_scriptpubkey(), self.their_shutdown_scriptpubkey.as_ref().unwrap());
2337+
let proposed_total_fee_satoshis = proposed_feerate * tx_weight / 1000;
2338+
2339+
let (closing_tx, total_fee_satoshis) = self.build_closing_transaction(proposed_total_fee_satoshis, false);
2340+
let funding_redeemscript = self.get_funding_redeemscript();
2341+
let sighash = Message::from_slice(&bip143::SighashComponents::new(&closing_tx).sighash_all(&closing_tx.input[0], &funding_redeemscript, self.channel_value_satoshis)[..]).unwrap();
2342+
2343+
self.last_sent_closing_fee = Some((proposed_feerate, total_fee_satoshis));
2344+
msgs::ClosingSigned {
2345+
channel_id: self.channel_id,
2346+
fee_satoshis: total_fee_satoshis,
2347+
signature: self.secp_ctx.sign(&sighash, &self.local_keys.funding_key),
2348+
}
2349+
}
2350+
23312351
pub fn shutdown(&mut self, fee_estimator: &FeeEstimator, msg: &msgs::Shutdown) -> Result<(Option<msgs::Shutdown>, Option<msgs::ClosingSigned>, Vec<(HTLCSource, [u8; 32])>), ChannelError> {
23322352
if self.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
23332353
return Err(ChannelError::Close("Peer sent shutdown when we needed a channel_reestablish"));
@@ -2367,23 +2387,6 @@ impl Channel {
23672387
self.their_shutdown_scriptpubkey = Some(msg.scriptpubkey.clone());
23682388
}
23692389

2370-
let our_closing_script = self.get_closing_scriptpubkey();
2371-
2372-
let (proposed_feerate, proposed_fee, our_sig) = if self.channel_outbound && self.pending_inbound_htlcs.is_empty() && self.pending_outbound_htlcs.is_empty() {
2373-
let mut proposed_feerate = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Background);
2374-
if self.feerate_per_kw > proposed_feerate {
2375-
proposed_feerate = self.feerate_per_kw;
2376-
}
2377-
let tx_weight = Self::get_closing_transaction_weight(&our_closing_script, &msg.scriptpubkey);
2378-
let proposed_total_fee_satoshis = proposed_feerate * tx_weight / 1000;
2379-
2380-
let (closing_tx, total_fee_satoshis) = self.build_closing_transaction(proposed_total_fee_satoshis, false);
2381-
let funding_redeemscript = self.get_funding_redeemscript();
2382-
let sighash = Message::from_slice(&bip143::SighashComponents::new(&closing_tx).sighash_all(&closing_tx.input[0], &funding_redeemscript, self.channel_value_satoshis)[..]).unwrap();
2383-
2384-
(Some(proposed_feerate), Some(total_fee_satoshis), Some(self.secp_ctx.sign(&sighash, &self.local_keys.funding_key)))
2385-
} else { (None, None, None) };
2386-
23872390
// From here on out, we may not fail!
23882391

23892392
self.channel_state |= ChannelState::RemoteShutdownSent as u32;
@@ -2413,21 +2416,16 @@ impl Channel {
24132416
} else {
24142417
Some(msgs::Shutdown {
24152418
channel_id: self.channel_id,
2416-
scriptpubkey: our_closing_script,
2419+
scriptpubkey: self.get_closing_scriptpubkey(),
24172420
})
24182421
};
24192422

24202423
self.channel_state |= ChannelState::LocalShutdownSent as u32;
24212424
self.channel_update_count += 1;
2422-
if self.pending_inbound_htlcs.is_empty() && self.pending_outbound_htlcs.is_empty() && self.channel_outbound {
2425+
if self.channel_outbound && self.pending_inbound_htlcs.is_empty() && self.pending_outbound_htlcs.is_empty() {
24232426
// There are no more HTLCs and we're the funder, this means we start the closing_signed
24242427
// dance with an initial fee proposal!
2425-
self.last_sent_closing_fee = Some((proposed_feerate.unwrap(), proposed_fee.unwrap()));
2426-
Ok((our_shutdown, Some(msgs::ClosingSigned {
2427-
channel_id: self.channel_id,
2428-
fee_satoshis: proposed_fee.unwrap(),
2429-
signature: our_sig.unwrap(),
2430-
}), dropped_outbound_htlcs))
2428+
Ok((our_shutdown, Some(self.propose_first_closing_signed(fee_estimator)), dropped_outbound_htlcs))
24312429
} else {
24322430
Ok((our_shutdown, None, dropped_outbound_htlcs))
24332431
}

0 commit comments

Comments
 (0)