Skip to content

Commit 02ea7b5

Browse files
committed
Remove redundant parameters from the CommitmentTransaction constructor
The `DirectedChannelTransactionParameters` argument of the `CommitmentTransaction` constructor already contains the broadcaster, and the countersignatory public keys, which include the respective funding keys. It is therefore not necessary to ask for these funding keys in additional arguments.
1 parent 79bc926 commit 02ea7b5

File tree

5 files changed

+24
-59
lines changed

5 files changed

+24
-59
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3490,22 +3490,13 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
34903490
to_broadcaster_value: u64, to_countersignatory_value: u64, feerate_per_kw: u32,
34913491
mut nondust_htlcs: Vec<(HTLCOutputInCommitment, Option<Box<HTLCSource>>)>
34923492
) -> CommitmentTransaction {
3493-
let broadcaster_keys = &self.onchain_tx_handler.channel_transaction_parameters
3494-
.counterparty_parameters.as_ref().unwrap().pubkeys;
3495-
let countersignatory_keys =
3496-
&self.onchain_tx_handler.channel_transaction_parameters.holder_pubkeys;
3497-
3498-
let broadcaster_funding_key = broadcaster_keys.funding_pubkey;
3499-
let countersignatory_funding_key = countersignatory_keys.funding_pubkey;
3500-
let keys = TxCreationKeys::from_channel_static_keys(&their_per_commitment_point,
3501-
&broadcaster_keys, &countersignatory_keys, &self.onchain_tx_handler.secp_ctx);
35023493
let channel_parameters =
35033494
&self.onchain_tx_handler.channel_transaction_parameters.as_counterparty_broadcastable();
3495+
let keys = TxCreationKeys::from_channel_static_keys(&their_per_commitment_point,
3496+
channel_parameters.broadcaster_pubkeys(), channel_parameters.countersignatory_pubkeys(), &self.onchain_tx_handler.secp_ctx);
35043497

35053498
CommitmentTransaction::new_with_auxiliary_htlc_data(commitment_number,
3506-
to_broadcaster_value, to_countersignatory_value, broadcaster_funding_key,
3507-
countersignatory_funding_key, keys, feerate_per_kw, &mut nondust_htlcs,
3508-
channel_parameters)
3499+
to_broadcaster_value, to_countersignatory_value, keys, feerate_per_kw, &mut nondust_htlcs, channel_parameters)
35093500
}
35103501

35113502
fn counterparty_commitment_txs_from_update(&self, update: &ChannelMonitorUpdate) -> Vec<CommitmentTransaction> {

lightning/src/ln/chan_utils.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,7 +1178,7 @@ impl HolderCommitmentTransaction {
11781178
for _ in 0..htlcs.len() {
11791179
counterparty_htlc_sigs.push(dummy_sig);
11801180
}
1181-
let inner = CommitmentTransaction::new_with_auxiliary_htlc_data(0, 0, 0, dummy_key.clone(), dummy_key.clone(), keys, 0, htlcs, &channel_parameters.as_counterparty_broadcastable());
1181+
let inner = CommitmentTransaction::new_with_auxiliary_htlc_data(0, 0, 0, keys, 0, htlcs, &channel_parameters.as_counterparty_broadcastable());
11821182
htlcs.sort_by_key(|htlc| htlc.0.transaction_output_index);
11831183
HolderCommitmentTransaction {
11841184
inner,
@@ -1488,12 +1488,12 @@ impl CommitmentTransaction {
14881488
/// Only include HTLCs that are above the dust limit for the channel.
14891489
///
14901490
/// This is not exported to bindings users due to the generic though we likely should expose a version without
1491-
pub fn new_with_auxiliary_htlc_data<T>(commitment_number: u64, to_broadcaster_value_sat: u64, to_countersignatory_value_sat: u64, broadcaster_funding_key: PublicKey, countersignatory_funding_key: PublicKey, keys: TxCreationKeys, feerate_per_kw: u32, htlcs_with_aux: &mut Vec<(HTLCOutputInCommitment, T)>, channel_parameters: &DirectedChannelTransactionParameters) -> CommitmentTransaction {
1491+
pub fn new_with_auxiliary_htlc_data<T>(commitment_number: u64, to_broadcaster_value_sat: u64, to_countersignatory_value_sat: u64, keys: TxCreationKeys, feerate_per_kw: u32, htlcs_with_aux: &mut Vec<(HTLCOutputInCommitment, T)>, channel_parameters: &DirectedChannelTransactionParameters) -> CommitmentTransaction {
14921492
let to_broadcaster_value_sat = Amount::from_sat(to_broadcaster_value_sat);
14931493
let to_countersignatory_value_sat = Amount::from_sat(to_countersignatory_value_sat);
14941494

14951495
// Sort outputs and populate output indices while keeping track of the auxiliary data
1496-
let (outputs, htlcs) = Self::internal_build_outputs(&keys, to_broadcaster_value_sat, to_countersignatory_value_sat, htlcs_with_aux, channel_parameters, &broadcaster_funding_key, &countersignatory_funding_key).unwrap();
1496+
let (outputs, htlcs) = Self::internal_build_outputs(&keys, to_broadcaster_value_sat, to_countersignatory_value_sat, htlcs_with_aux, channel_parameters).unwrap();
14971497

14981498
let (obscured_commitment_transaction_number, txins) = Self::internal_build_inputs(commitment_number, channel_parameters);
14991499
let transaction = Self::make_transaction(obscured_commitment_transaction_number, txins, outputs);
@@ -1522,11 +1522,11 @@ impl CommitmentTransaction {
15221522
self
15231523
}
15241524

1525-
fn internal_rebuild_transaction(&self, keys: &TxCreationKeys, channel_parameters: &DirectedChannelTransactionParameters, broadcaster_funding_key: &PublicKey, countersignatory_funding_key: &PublicKey) -> Result<BuiltCommitmentTransaction, ()> {
1525+
fn internal_rebuild_transaction(&self, keys: &TxCreationKeys, channel_parameters: &DirectedChannelTransactionParameters) -> Result<BuiltCommitmentTransaction, ()> {
15261526
let (obscured_commitment_transaction_number, txins) = Self::internal_build_inputs(self.commitment_number, channel_parameters);
15271527

15281528
let mut htlcs_with_aux = self.htlcs.iter().map(|h| (h.clone(), ())).collect();
1529-
let (outputs, _) = Self::internal_build_outputs(keys, self.to_broadcaster_value_sat, self.to_countersignatory_value_sat, &mut htlcs_with_aux, channel_parameters, broadcaster_funding_key, countersignatory_funding_key)?;
1529+
let (outputs, _) = Self::internal_build_outputs(keys, self.to_broadcaster_value_sat, self.to_countersignatory_value_sat, &mut htlcs_with_aux, channel_parameters)?;
15301530

15311531
let transaction = Self::make_transaction(obscured_commitment_transaction_number, txins, outputs);
15321532
let txid = transaction.compute_txid();
@@ -1550,8 +1550,10 @@ impl CommitmentTransaction {
15501550
// - initial sorting of outputs / HTLCs in the constructor, in which case T is auxiliary data the
15511551
// caller needs to have sorted together with the HTLCs so it can keep track of the output index
15521552
// - building of a bitcoin transaction during a verify() call, in which case T is just ()
1553-
fn internal_build_outputs<T>(keys: &TxCreationKeys, to_broadcaster_value_sat: Amount, to_countersignatory_value_sat: Amount, htlcs_with_aux: &mut Vec<(HTLCOutputInCommitment, T)>, channel_parameters: &DirectedChannelTransactionParameters, broadcaster_funding_key: &PublicKey, countersignatory_funding_key: &PublicKey) -> Result<(Vec<TxOut>, Vec<HTLCOutputInCommitment>), ()> {
1553+
fn internal_build_outputs<T>(keys: &TxCreationKeys, to_broadcaster_value_sat: Amount, to_countersignatory_value_sat: Amount, htlcs_with_aux: &mut Vec<(HTLCOutputInCommitment, T)>, channel_parameters: &DirectedChannelTransactionParameters) -> Result<(Vec<TxOut>, Vec<HTLCOutputInCommitment>), ()> {
15541554
let countersignatory_pubkeys = channel_parameters.countersignatory_pubkeys();
1555+
let broadcaster_funding_key = &channel_parameters.broadcaster_pubkeys().funding_pubkey;
1556+
let countersignatory_funding_key = &countersignatory_pubkeys.funding_pubkey;
15551557
let contest_delay = channel_parameters.contest_delay();
15561558

15571559
let mut txouts: Vec<(TxOut, Option<&mut HTLCOutputInCommitment>)> = Vec::new();
@@ -1723,14 +1725,14 @@ impl CommitmentTransaction {
17231725
///
17241726
/// An external validating signer must call this method before signing
17251727
/// or using the built transaction.
1726-
pub fn verify<T: secp256k1::Signing + secp256k1::Verification>(&self, channel_parameters: &DirectedChannelTransactionParameters, broadcaster_keys: &ChannelPublicKeys, countersignatory_keys: &ChannelPublicKeys, secp_ctx: &Secp256k1<T>) -> Result<TrustedCommitmentTransaction, ()> {
1728+
pub fn verify<T: secp256k1::Signing + secp256k1::Verification>(&self, channel_parameters: &DirectedChannelTransactionParameters, secp_ctx: &Secp256k1<T>) -> Result<TrustedCommitmentTransaction, ()> {
17271729
// This is the only field of the key cache that we trust
17281730
let per_commitment_point = self.keys.per_commitment_point;
1729-
let keys = TxCreationKeys::from_channel_static_keys(&per_commitment_point, broadcaster_keys, countersignatory_keys, secp_ctx);
1731+
let keys = TxCreationKeys::from_channel_static_keys(&per_commitment_point, channel_parameters.broadcaster_pubkeys(), channel_parameters.countersignatory_pubkeys(), secp_ctx);
17301732
if keys != self.keys {
17311733
return Err(());
17321734
}
1733-
let tx = self.internal_rebuild_transaction(&keys, channel_parameters, &broadcaster_keys.funding_pubkey, &countersignatory_keys.funding_pubkey)?;
1735+
let tx = self.internal_rebuild_transaction(&keys, channel_parameters)?;
17341736
if self.built.transaction != tx.transaction || self.built.txid != tx.txid {
17351737
return Err(());
17361738
}
@@ -1953,8 +1955,6 @@ mod tests {
19531955

19541956
struct TestCommitmentTxBuilder {
19551957
commitment_number: u64,
1956-
holder_funding_pubkey: PublicKey,
1957-
counterparty_funding_pubkey: PublicKey,
19581958
keys: TxCreationKeys,
19591959
feerate_per_kw: u32,
19601960
htlcs_with_aux: Vec<(HTLCOutputInCommitment, ())>,
@@ -1993,8 +1993,6 @@ mod tests {
19931993

19941994
Self {
19951995
commitment_number: 0,
1996-
holder_funding_pubkey: holder_pubkeys.funding_pubkey,
1997-
counterparty_funding_pubkey: counterparty_pubkeys.funding_pubkey,
19981996
keys,
19991997
feerate_per_kw: 1,
20001998
htlcs_with_aux,
@@ -2006,8 +2004,6 @@ mod tests {
20062004
fn build(&mut self, to_broadcaster_sats: u64, to_countersignatory_sats: u64) -> CommitmentTransaction {
20072005
CommitmentTransaction::new_with_auxiliary_htlc_data(
20082006
self.commitment_number, to_broadcaster_sats, to_countersignatory_sats,
2009-
self.holder_funding_pubkey.clone(),
2010-
self.counterparty_funding_pubkey.clone(),
20112007
self.keys.clone(), self.feerate_per_kw,
20122008
&mut self.htlcs_with_aux, &self.channel_parameters.as_holder_broadcastable()
20132009
)

lightning/src/ln/channel.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3773,11 +3773,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
37733773

37743774
let mut value_to_a = if local { value_to_self } else { value_to_remote };
37753775
let mut value_to_b = if local { value_to_remote } else { value_to_self };
3776-
let (funding_pubkey_a, funding_pubkey_b) = if local {
3777-
(funding.get_holder_pubkeys().funding_pubkey, funding.get_counterparty_pubkeys().funding_pubkey)
3778-
} else {
3779-
(funding.get_counterparty_pubkeys().funding_pubkey, funding.get_holder_pubkeys().funding_pubkey)
3780-
};
37813776

37823777
if value_to_a >= (broadcaster_dust_limit_satoshis as i64) {
37833778
log_trace!(logger, " ...including {} output with value {}", if local { "to_local" } else { "to_remote" }, value_to_a);
@@ -3799,8 +3794,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
37993794
let tx = CommitmentTransaction::new_with_auxiliary_htlc_data(commitment_number,
38003795
value_to_a as u64,
38013796
value_to_b as u64,
3802-
funding_pubkey_a,
3803-
funding_pubkey_b,
38043797
keys.clone(),
38053798
feerate_per_kw,
38063799
&mut included_non_dust_htlcs,

lightning/src/ln/functional_tests.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -732,24 +732,23 @@ pub fn test_update_fee_that_funder_cannot_afford() {
732732

733733
// Get the TestChannelSigner for each channel, which will be used to (1) get the keys
734734
// needed to sign the new commitment tx and (2) sign the new commitment tx.
735-
let (local_revocation_basepoint, local_htlc_basepoint, local_funding) = {
735+
let (local_revocation_basepoint, local_htlc_basepoint) = {
736736
let per_peer_state = nodes[0].node.per_peer_state.read().unwrap();
737737
let chan_lock = per_peer_state.get(&nodes[1].node.get_our_node_id()).unwrap().lock().unwrap();
738738
let local_chan = chan_lock.channel_by_id.get(&chan.2).and_then(Channel::as_funded).unwrap();
739739
let chan_signer = local_chan.get_signer();
740740
let pubkeys = chan_signer.as_ref().pubkeys(None, &secp_ctx);
741-
(pubkeys.revocation_basepoint, pubkeys.htlc_basepoint,
742-
pubkeys.funding_pubkey)
741+
(pubkeys.revocation_basepoint, pubkeys.htlc_basepoint)
743742
};
744-
let (remote_delayed_payment_basepoint, remote_htlc_basepoint, remote_point, remote_funding) = {
743+
let (remote_delayed_payment_basepoint, remote_htlc_basepoint, remote_point) = {
745744
let per_peer_state = nodes[1].node.per_peer_state.read().unwrap();
746745
let chan_lock = per_peer_state.get(&nodes[0].node.get_our_node_id()).unwrap().lock().unwrap();
747746
let remote_chan = chan_lock.channel_by_id.get(&chan.2).and_then(Channel::as_funded).unwrap();
748747
let chan_signer = remote_chan.get_signer();
749748
let pubkeys = chan_signer.as_ref().pubkeys(None, &secp_ctx);
750749
(pubkeys.delayed_payment_basepoint, pubkeys.htlc_basepoint,
751750
chan_signer.as_ref().get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 1, &secp_ctx).unwrap(),
752-
pubkeys.funding_pubkey)
751+
)
753752
};
754753

755754
// Assemble the set of keys we can use for signatures for our commitment_signed message.
@@ -766,7 +765,6 @@ pub fn test_update_fee_that_funder_cannot_afford() {
766765
INITIAL_COMMITMENT_NUMBER - 1,
767766
push_sats,
768767
channel_value - push_sats - commit_tx_fee_msat(non_buffer_feerate + 4, 0, &channel_type_features) / 1000,
769-
local_funding, remote_funding,
770768
commit_tx_keys.clone(),
771769
non_buffer_feerate + 4,
772770
&mut htlcs,
@@ -1460,7 +1458,7 @@ pub fn test_fee_spike_violation_fails_htlc() {
14601458

14611459
// Get the TestChannelSigner for each channel, which will be used to (1) get the keys
14621460
// needed to sign the new commitment tx and (2) sign the new commitment tx.
1463-
let (local_revocation_basepoint, local_htlc_basepoint, local_secret, next_local_point, local_funding) = {
1461+
let (local_revocation_basepoint, local_htlc_basepoint, local_secret, next_local_point) = {
14641462
let per_peer_state = nodes[0].node.per_peer_state.read().unwrap();
14651463
let chan_lock = per_peer_state.get(&nodes[1].node.get_our_node_id()).unwrap().lock().unwrap();
14661464
let local_chan = chan_lock.channel_by_id.get(&chan.2).and_then(Channel::as_funded).unwrap();
@@ -1471,18 +1469,16 @@ pub fn test_fee_spike_violation_fails_htlc() {
14711469
let pubkeys = chan_signer.as_ref().pubkeys(None, &secp_ctx);
14721470
(pubkeys.revocation_basepoint, pubkeys.htlc_basepoint,
14731471
chan_signer.as_ref().release_commitment_secret(INITIAL_COMMITMENT_NUMBER).unwrap(),
1474-
chan_signer.as_ref().get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 2, &secp_ctx).unwrap(),
1475-
chan_signer.as_ref().pubkeys(None, &secp_ctx).funding_pubkey)
1472+
chan_signer.as_ref().get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 2, &secp_ctx).unwrap())
14761473
};
1477-
let (remote_delayed_payment_basepoint, remote_htlc_basepoint, remote_point, remote_funding) = {
1474+
let (remote_delayed_payment_basepoint, remote_htlc_basepoint, remote_point) = {
14781475
let per_peer_state = nodes[1].node.per_peer_state.read().unwrap();
14791476
let chan_lock = per_peer_state.get(&nodes[0].node.get_our_node_id()).unwrap().lock().unwrap();
14801477
let remote_chan = chan_lock.channel_by_id.get(&chan.2).and_then(Channel::as_funded).unwrap();
14811478
let chan_signer = remote_chan.get_signer();
14821479
let pubkeys = chan_signer.as_ref().pubkeys(None, &secp_ctx);
14831480
(pubkeys.delayed_payment_basepoint, pubkeys.htlc_basepoint,
1484-
chan_signer.as_ref().get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 1, &secp_ctx).unwrap(),
1485-
chan_signer.as_ref().pubkeys(None, &secp_ctx).funding_pubkey)
1481+
chan_signer.as_ref().get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 1, &secp_ctx).unwrap())
14861482
};
14871483

14881484
// Assemble the set of keys we can use for signatures for our commitment_signed message.
@@ -1512,7 +1508,6 @@ pub fn test_fee_spike_violation_fails_htlc() {
15121508
commitment_number,
15131509
95000,
15141510
local_chan_balance,
1515-
local_funding, remote_funding,
15161511
commit_tx_keys.clone(),
15171512
feerate_per_kw,
15181513
&mut vec![(accepted_htlc_info, ())],

lightning/src/util/test_channel_signer.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -561,12 +561,7 @@ impl TestChannelSigner {
561561
commitment_tx: &'a CommitmentTransaction, secp_ctx: &Secp256k1<T>,
562562
) -> TrustedCommitmentTransaction<'a> {
563563
commitment_tx
564-
.verify(
565-
&channel_parameters.as_counterparty_broadcastable(),
566-
channel_parameters.counterparty_pubkeys().unwrap(),
567-
&channel_parameters.holder_pubkeys,
568-
secp_ctx,
569-
)
564+
.verify(&channel_parameters.as_counterparty_broadcastable(), secp_ctx)
570565
.expect("derived different per-tx keys or built transaction")
571566
}
572567

@@ -575,12 +570,7 @@ impl TestChannelSigner {
575570
commitment_tx: &'a CommitmentTransaction, secp_ctx: &Secp256k1<T>,
576571
) -> TrustedCommitmentTransaction<'a> {
577572
commitment_tx
578-
.verify(
579-
&channel_parameters.as_holder_broadcastable(),
580-
&channel_parameters.holder_pubkeys,
581-
channel_parameters.counterparty_pubkeys().unwrap(),
582-
secp_ctx,
583-
)
573+
.verify(&channel_parameters.as_holder_broadcastable(), secp_ctx)
584574
.expect("derived different per-tx keys or built transaction")
585575
}
586576
}

0 commit comments

Comments
 (0)