Skip to content

Commit 16114a2

Browse files
committed
Pre-build CommitmentTransaction
reduces CPU and memory allocation effort involved in bitcoin tx construction and txid derivation
1 parent 52f6d6b commit 16114a2

File tree

7 files changed

+258
-174
lines changed

7 files changed

+258
-174
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ pub(crate) const ANTI_REORG_DELAY: u32 = 6;
251251
/// end up force-closing the channel on us to claim it.
252252
pub(crate) const HTLC_FAIL_BACK_BUFFER: u32 = CLTV_CLAIM_BUFFER + LATENCY_GRACE_PERIOD_BLOCKS;
253253

254+
// TODO(devrandom) replace this with HolderCommitmentTransaction
254255
#[derive(Clone, PartialEq)]
255256
struct HolderSignedTx {
256257
/// txid of the transaction in tx, just used to make comparison faster
@@ -968,8 +969,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
968969

969970
let secp_ctx = Secp256k1::new();
970971

971-
let directed_channel_parameters = channel_parameters.to_directed(true);
972-
let txid = initial_holder_commitment_tx.calculate_txid(&directed_channel_parameters, &secp_ctx);
972+
let txid = initial_holder_commitment_tx.trust_txid();
973973

974974
// block for Rust 1.34 compat
975975
let (holder_commitment_tx, current_holder_commitment_number) = {
@@ -1146,7 +1146,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
11461146
/// up-to-date as our holder commitment transaction is updated.
11471147
/// Panics if set_on_holder_tx_csv has never been called.
11481148
fn provide_latest_holder_commitment_tx(&mut self, holder_commitment_tx: HolderCommitmentTransaction, htlc_outputs: Vec<(HTLCOutputInCommitment, Option<Signature>, Option<HTLCSource>)>) -> Result<(), MonitorUpdateError> {
1149-
let txid = holder_commitment_tx.calculate_txid(&self.onchain_tx_handler.channel_transaction_parameters.to_directed(true), &self.secp_ctx);
1149+
let txid = holder_commitment_tx.trust_txid();
11501150

11511151
// block for Rust 1.34 compat
11521152
let mut new_holder_commitment_tx = {

lightning/src/chain/keysinterface.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -486,11 +486,10 @@ impl ChannelKeys for InMemoryChannelKeys {
486486
let funding_pubkey = PublicKey::from_secret_key(secp_ctx, &self.funding_key);
487487
let channel_funding_redeemscript = make_funding_redeemscript(&funding_pubkey, &self.counterparty_pubkeys().funding_pubkey);
488488

489-
let channel_parameters = self.make_channel_parameters();
490-
let directed_channel_parameters = channel_parameters.to_directed(false);
491-
let commitment_sig = commitment_tx.get_signature(&directed_channel_parameters, &self.funding_key, &channel_funding_redeemscript, self.channel_value_satoshis, secp_ctx);
489+
let built_tx = &commitment_tx.built;
490+
let commitment_sig = built_tx.get_signature(&self.funding_key, &channel_funding_redeemscript, self.channel_value_satoshis, secp_ctx);
492491

493-
let commitment_txid = commitment_tx.calculate_txid(&directed_channel_parameters, secp_ctx);
492+
let commitment_txid = commitment_tx.trust_txid();
494493

495494
let mut htlc_sigs = Vec::with_capacity(commitment_tx.htlcs.len());
496495
for htlc in &commitment_tx.htlcs {
@@ -511,9 +510,8 @@ impl ChannelKeys for InMemoryChannelKeys {
511510
let funding_pubkey = PublicKey::from_secret_key(secp_ctx, &self.funding_key);
512511
let funding_redeemscript = make_funding_redeemscript(&funding_pubkey, &self.counterparty_pubkeys().funding_pubkey);
513512

514-
let channel_parameters = self.make_channel_parameters();
515-
let channel_paramters = channel_parameters.to_directed(true);
516-
let sig = commitment_tx.inner.get_signature(&channel_paramters, &self.funding_key, &funding_redeemscript, self.channel_value_satoshis, secp_ctx);
513+
let built_tx = &commitment_tx.inner.built;
514+
let sig = built_tx.get_signature(&self.funding_key, &funding_redeemscript, self.channel_value_satoshis, secp_ctx);
517515
let htlc_sigs_o = self.sign_holder_commitment_htlc_transactions(&commitment_tx, secp_ctx)?;
518516
let htlc_sigs = htlc_sigs_o.iter().map(|o| o.unwrap()).collect();
519517

@@ -524,10 +522,9 @@ impl ChannelKeys for InMemoryChannelKeys {
524522
fn unsafe_sign_holder_commitment<T: secp256k1::Signing + secp256k1::Verification>(&self, holder_commitment_tx: &HolderCommitmentTransaction, secp_ctx: &Secp256k1<T>) -> Result<Signature, ()> {
525523
let funding_pubkey = PublicKey::from_secret_key(secp_ctx, &self.funding_key);
526524
let channel_funding_redeemscript = make_funding_redeemscript(&funding_pubkey, &self.counterparty_pubkeys().funding_pubkey);
527-
let channel_parameters = self.make_channel_parameters();
528-
let channel_parameters = channel_parameters.to_directed(true);
529525

530-
Ok(holder_commitment_tx.inner.get_signature(&channel_parameters, &self.funding_key, &channel_funding_redeemscript, self.channel_value_satoshis, secp_ctx))
526+
let built_tx = &holder_commitment_tx.inner.built;
527+
Ok(built_tx.get_signature(&self.funding_key, &channel_funding_redeemscript, self.channel_value_satoshis, secp_ctx))
531528
}
532529

533530
fn sign_holder_commitment_htlc_transactions<T: secp256k1::Signing + secp256k1::Verification>(&self, commitment_tx: &HolderCommitmentTransaction, secp_ctx: &Secp256k1<T>) -> Result<Vec<Option<Signature>>, ()> {

0 commit comments

Comments
 (0)