Skip to content

Commit e58184c

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 d671596 commit e58184c

File tree

5 files changed

+32
-65
lines changed

5 files changed

+32
-65
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3506,20 +3506,12 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
35063506
to_broadcaster_value: u64, to_countersignatory_value: u64, feerate_per_kw: u32,
35073507
mut nondust_htlcs: Vec<(HTLCOutputInCommitment, Option<Box<HTLCSource>>)>
35083508
) -> CommitmentTransaction {
3509-
let broadcaster_keys =
3510-
&self.funding.channel_parameters.counterparty_parameters.as_ref().unwrap().pubkeys;
3511-
let countersignatory_keys = &self.funding.channel_parameters.holder_pubkeys;
3512-
3513-
let broadcaster_funding_key = broadcaster_keys.funding_pubkey;
3514-
let countersignatory_funding_key = countersignatory_keys.funding_pubkey;
3515-
let keys = TxCreationKeys::from_channel_static_keys(&their_per_commitment_point,
3516-
&broadcaster_keys, &countersignatory_keys, &self.onchain_tx_handler.secp_ctx);
35173509
let channel_parameters = &self.funding.channel_parameters.as_counterparty_broadcastable();
3510+
let keys = TxCreationKeys::from_channel_static_keys(their_per_commitment_point,
3511+
channel_parameters.broadcaster_pubkeys(), channel_parameters.countersignatory_pubkeys(), &self.onchain_tx_handler.secp_ctx);
35183512

35193513
CommitmentTransaction::new_with_auxiliary_htlc_data(commitment_number,
3520-
to_broadcaster_value, to_countersignatory_value, broadcaster_funding_key,
3521-
countersignatory_funding_key, keys, feerate_per_kw, &mut nondust_htlcs,
3522-
channel_parameters)
3514+
to_broadcaster_value, to_countersignatory_value, keys, feerate_per_kw, &mut nondust_htlcs, channel_parameters)
35233515
}
35243516

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

lightning/src/ln/chan_utils.rs

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,7 +1208,7 @@ impl HolderCommitmentTransaction {
12081208
for _ in 0..htlcs.len() {
12091209
counterparty_htlc_sigs.push(dummy_sig);
12101210
}
1211-
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());
1211+
let inner = CommitmentTransaction::new_with_auxiliary_htlc_data(0, 0, 0, keys, 0, htlcs, &channel_parameters.as_counterparty_broadcastable());
12121212
htlcs.sort_by_key(|htlc| htlc.0.transaction_output_index);
12131213
HolderCommitmentTransaction {
12141214
inner,
@@ -1518,12 +1518,12 @@ impl CommitmentTransaction {
15181518
/// Only include HTLCs that are above the dust limit for the channel.
15191519
///
15201520
/// This is not exported to bindings users due to the generic though we likely should expose a version without
1521-
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 {
1521+
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 {
15221522
let to_broadcaster_value_sat = Amount::from_sat(to_broadcaster_value_sat);
15231523
let to_countersignatory_value_sat = Amount::from_sat(to_countersignatory_value_sat);
15241524

15251525
// Sort outputs and populate output indices while keeping track of the auxiliary data
1526-
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();
1526+
let (outputs, htlcs) = Self::internal_build_outputs(&keys, to_broadcaster_value_sat, to_countersignatory_value_sat, htlcs_with_aux, channel_parameters).unwrap();
15271527

15281528
let (obscured_commitment_transaction_number, txins) = Self::internal_build_inputs(commitment_number, channel_parameters);
15291529
let transaction = Self::make_transaction(obscured_commitment_transaction_number, txins, outputs);
@@ -1552,11 +1552,11 @@ impl CommitmentTransaction {
15521552
self
15531553
}
15541554

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

15581558
let mut htlcs_with_aux = self.htlcs.iter().map(|h| (h.clone(), ())).collect();
1559-
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)?;
1559+
let (outputs, _) = Self::internal_build_outputs(keys, self.to_broadcaster_value_sat, self.to_countersignatory_value_sat, &mut htlcs_with_aux, channel_parameters)?;
15601560

15611561
let transaction = Self::make_transaction(obscured_commitment_transaction_number, txins, outputs);
15621562
let txid = transaction.compute_txid();
@@ -1580,17 +1580,20 @@ impl CommitmentTransaction {
15801580
// - initial sorting of outputs / HTLCs in the constructor, in which case T is auxiliary data the
15811581
// caller needs to have sorted together with the HTLCs so it can keep track of the output index
15821582
// - building of a bitcoin transaction during a verify() call, in which case T is just ()
1583-
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>), ()> {
1584-
let countersignatory_pubkeys = channel_parameters.countersignatory_pubkeys();
1583+
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>), ()> {
1584+
let countersignatory_payment_point = &channel_parameters.countersignatory_pubkeys().payment_point;
1585+
let countersignatory_funding_key = &channel_parameters.countersignatory_pubkeys().funding_pubkey;
1586+
let broadcaster_funding_key = &channel_parameters.broadcaster_pubkeys().funding_pubkey;
1587+
let channel_type = channel_parameters.channel_type_features();
15851588
let contest_delay = channel_parameters.contest_delay();
15861589

15871590
let mut txouts: Vec<(TxOut, Option<&mut HTLCOutputInCommitment>)> = Vec::new();
15881591

15891592
if to_countersignatory_value_sat > Amount::ZERO {
1590-
let script = if channel_parameters.channel_type_features().supports_anchors_zero_fee_htlc_tx() {
1591-
get_to_countersigner_keyed_anchor_redeemscript(&countersignatory_pubkeys.payment_point).to_p2wsh()
1593+
let script = if channel_type.supports_anchors_zero_fee_htlc_tx() {
1594+
get_to_countersigner_keyed_anchor_redeemscript(countersignatory_payment_point).to_p2wsh()
15921595
} else {
1593-
ScriptBuf::new_p2wpkh(&Hash160::hash(&countersignatory_pubkeys.payment_point.serialize()).into())
1596+
ScriptBuf::new_p2wpkh(&Hash160::hash(&countersignatory_payment_point.serialize()).into())
15941597
};
15951598
txouts.push((
15961599
TxOut {
@@ -1616,7 +1619,7 @@ impl CommitmentTransaction {
16161619
));
16171620
}
16181621

1619-
if channel_parameters.channel_type_features().supports_anchors_zero_fee_htlc_tx() {
1622+
if channel_type.supports_anchors_zero_fee_htlc_tx() {
16201623
if to_broadcaster_value_sat > Amount::ZERO || !htlcs_with_aux.is_empty() {
16211624
let anchor_script = get_keyed_anchor_redeemscript(broadcaster_funding_key);
16221625
txouts.push((
@@ -1642,7 +1645,7 @@ impl CommitmentTransaction {
16421645

16431646
let mut htlcs = Vec::with_capacity(htlcs_with_aux.len());
16441647
for (htlc, _) in htlcs_with_aux {
1645-
let script = get_htlc_redeemscript(&htlc, &channel_parameters.channel_type_features(), &keys);
1648+
let script = get_htlc_redeemscript(htlc, channel_type, keys);
16461649
let txout = TxOut {
16471650
script_pubkey: script.to_p2wsh(),
16481651
value: htlc.to_bitcoin_amount(),
@@ -1753,14 +1756,14 @@ impl CommitmentTransaction {
17531756
///
17541757
/// An external validating signer must call this method before signing
17551758
/// or using the built transaction.
1756-
pub fn verify<T: secp256k1::Signing + secp256k1::Verification>(&self, channel_parameters: &DirectedChannelTransactionParameters, broadcaster_keys: &ChannelPublicKeys, countersignatory_keys: &ChannelPublicKeys, secp_ctx: &Secp256k1<T>) -> Result<TrustedCommitmentTransaction, ()> {
1759+
pub fn verify<T: secp256k1::Signing + secp256k1::Verification>(&self, channel_parameters: &DirectedChannelTransactionParameters, secp_ctx: &Secp256k1<T>) -> Result<TrustedCommitmentTransaction, ()> {
17571760
// This is the only field of the key cache that we trust
1758-
let per_commitment_point = self.keys.per_commitment_point;
1759-
let keys = TxCreationKeys::from_channel_static_keys(&per_commitment_point, broadcaster_keys, countersignatory_keys, secp_ctx);
1761+
let per_commitment_point = &self.keys.per_commitment_point;
1762+
let keys = TxCreationKeys::from_channel_static_keys(per_commitment_point, channel_parameters.broadcaster_pubkeys(), channel_parameters.countersignatory_pubkeys(), secp_ctx);
17601763
if keys != self.keys {
17611764
return Err(());
17621765
}
1763-
let tx = self.internal_rebuild_transaction(&keys, channel_parameters, &broadcaster_keys.funding_pubkey, &countersignatory_keys.funding_pubkey)?;
1766+
let tx = self.internal_rebuild_transaction(&keys, channel_parameters)?;
17641767
if self.built.transaction != tx.transaction || self.built.txid != tx.txid {
17651768
return Err(());
17661769
}
@@ -1983,8 +1986,6 @@ mod tests {
19831986

19841987
struct TestCommitmentTxBuilder {
19851988
commitment_number: u64,
1986-
holder_funding_pubkey: PublicKey,
1987-
counterparty_funding_pubkey: PublicKey,
19881989
keys: TxCreationKeys,
19891990
feerate_per_kw: u32,
19901991
htlcs_with_aux: Vec<(HTLCOutputInCommitment, ())>,
@@ -2023,8 +2024,6 @@ mod tests {
20232024

20242025
Self {
20252026
commitment_number: 0,
2026-
holder_funding_pubkey: holder_pubkeys.funding_pubkey,
2027-
counterparty_funding_pubkey: counterparty_pubkeys.funding_pubkey,
20282027
keys,
20292028
feerate_per_kw: 1,
20302029
htlcs_with_aux,
@@ -2036,8 +2035,6 @@ mod tests {
20362035
fn build(&mut self, to_broadcaster_sats: u64, to_countersignatory_sats: u64) -> CommitmentTransaction {
20372036
CommitmentTransaction::new_with_auxiliary_htlc_data(
20382037
self.commitment_number, to_broadcaster_sats, to_countersignatory_sats,
2039-
self.holder_funding_pubkey.clone(),
2040-
self.counterparty_funding_pubkey.clone(),
20412038
self.keys.clone(), self.feerate_per_kw,
20422039
&mut self.htlcs_with_aux, &self.channel_parameters.as_holder_broadcastable()
20432040
)

lightning/src/ln/channel.rs

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

37963796
let mut value_to_a = if local { value_to_self } else { value_to_remote };
37973797
let mut value_to_b = if local { value_to_remote } else { value_to_self };
3798-
let (funding_pubkey_a, funding_pubkey_b) = if local {
3799-
(funding.get_holder_pubkeys().funding_pubkey, funding.get_counterparty_pubkeys().funding_pubkey)
3800-
} else {
3801-
(funding.get_counterparty_pubkeys().funding_pubkey, funding.get_holder_pubkeys().funding_pubkey)
3802-
};
38033798

38043799
if value_to_a >= (broadcaster_dust_limit_satoshis as i64) {
38053800
log_trace!(logger, " ...including {} output with value {}", if local { "to_local" } else { "to_remote" }, value_to_a);
@@ -3821,8 +3816,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
38213816
let tx = CommitmentTransaction::new_with_auxiliary_htlc_data(commitment_number,
38223817
value_to_a as u64,
38233818
value_to_b as u64,
3824-
funding_pubkey_a,
3825-
funding_pubkey_b,
38263819
keys.clone(),
38273820
feerate_per_kw,
38283821
&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
@@ -734,24 +734,23 @@ pub fn test_update_fee_that_funder_cannot_afford() {
734734

735735
// Get the TestChannelSigner for each channel, which will be used to (1) get the keys
736736
// needed to sign the new commitment tx and (2) sign the new commitment tx.
737-
let (local_revocation_basepoint, local_htlc_basepoint, local_funding) = {
737+
let (local_revocation_basepoint, local_htlc_basepoint) = {
738738
let per_peer_state = nodes[0].node.per_peer_state.read().unwrap();
739739
let chan_lock = per_peer_state.get(&nodes[1].node.get_our_node_id()).unwrap().lock().unwrap();
740740
let local_chan = chan_lock.channel_by_id.get(&chan.2).and_then(Channel::as_funded).unwrap();
741741
let chan_signer = local_chan.get_signer();
742742
let pubkeys = chan_signer.as_ref().pubkeys(None, &secp_ctx);
743-
(pubkeys.revocation_basepoint, pubkeys.htlc_basepoint,
744-
pubkeys.funding_pubkey)
743+
(pubkeys.revocation_basepoint, pubkeys.htlc_basepoint)
745744
};
746-
let (remote_delayed_payment_basepoint, remote_htlc_basepoint, remote_point, remote_funding) = {
745+
let (remote_delayed_payment_basepoint, remote_htlc_basepoint, remote_point) = {
747746
let per_peer_state = nodes[1].node.per_peer_state.read().unwrap();
748747
let chan_lock = per_peer_state.get(&nodes[0].node.get_our_node_id()).unwrap().lock().unwrap();
749748
let remote_chan = chan_lock.channel_by_id.get(&chan.2).and_then(Channel::as_funded).unwrap();
750749
let chan_signer = remote_chan.get_signer();
751750
let pubkeys = chan_signer.as_ref().pubkeys(None, &secp_ctx);
752751
(pubkeys.delayed_payment_basepoint, pubkeys.htlc_basepoint,
753752
chan_signer.as_ref().get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 1, &secp_ctx).unwrap(),
754-
pubkeys.funding_pubkey)
753+
)
755754
};
756755

757756
// Assemble the set of keys we can use for signatures for our commitment_signed message.
@@ -768,7 +767,6 @@ pub fn test_update_fee_that_funder_cannot_afford() {
768767
INITIAL_COMMITMENT_NUMBER - 1,
769768
push_sats,
770769
channel_value - push_sats - commit_tx_fee_msat(non_buffer_feerate + 4, 0, &channel_type_features) / 1000,
771-
local_funding, remote_funding,
772770
commit_tx_keys.clone(),
773771
non_buffer_feerate + 4,
774772
&mut htlcs,
@@ -1462,7 +1460,7 @@ pub fn test_fee_spike_violation_fails_htlc() {
14621460

14631461
// Get the TestChannelSigner for each channel, which will be used to (1) get the keys
14641462
// needed to sign the new commitment tx and (2) sign the new commitment tx.
1465-
let (local_revocation_basepoint, local_htlc_basepoint, local_secret, next_local_point, local_funding) = {
1463+
let (local_revocation_basepoint, local_htlc_basepoint, local_secret, next_local_point) = {
14661464
let per_peer_state = nodes[0].node.per_peer_state.read().unwrap();
14671465
let chan_lock = per_peer_state.get(&nodes[1].node.get_our_node_id()).unwrap().lock().unwrap();
14681466
let local_chan = chan_lock.channel_by_id.get(&chan.2).and_then(Channel::as_funded).unwrap();
@@ -1473,18 +1471,16 @@ pub fn test_fee_spike_violation_fails_htlc() {
14731471
let pubkeys = chan_signer.as_ref().pubkeys(None, &secp_ctx);
14741472
(pubkeys.revocation_basepoint, pubkeys.htlc_basepoint,
14751473
chan_signer.as_ref().release_commitment_secret(INITIAL_COMMITMENT_NUMBER).unwrap(),
1476-
chan_signer.as_ref().get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 2, &secp_ctx).unwrap(),
1477-
chan_signer.as_ref().pubkeys(None, &secp_ctx).funding_pubkey)
1474+
chan_signer.as_ref().get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 2, &secp_ctx).unwrap())
14781475
};
1479-
let (remote_delayed_payment_basepoint, remote_htlc_basepoint, remote_point, remote_funding) = {
1476+
let (remote_delayed_payment_basepoint, remote_htlc_basepoint, remote_point) = {
14801477
let per_peer_state = nodes[1].node.per_peer_state.read().unwrap();
14811478
let chan_lock = per_peer_state.get(&nodes[0].node.get_our_node_id()).unwrap().lock().unwrap();
14821479
let remote_chan = chan_lock.channel_by_id.get(&chan.2).and_then(Channel::as_funded).unwrap();
14831480
let chan_signer = remote_chan.get_signer();
14841481
let pubkeys = chan_signer.as_ref().pubkeys(None, &secp_ctx);
14851482
(pubkeys.delayed_payment_basepoint, pubkeys.htlc_basepoint,
1486-
chan_signer.as_ref().get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 1, &secp_ctx).unwrap(),
1487-
chan_signer.as_ref().pubkeys(None, &secp_ctx).funding_pubkey)
1483+
chan_signer.as_ref().get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 1, &secp_ctx).unwrap())
14881484
};
14891485

14901486
// Assemble the set of keys we can use for signatures for our commitment_signed message.
@@ -1514,7 +1510,6 @@ pub fn test_fee_spike_violation_fails_htlc() {
15141510
commitment_number,
15151511
95000,
15161512
local_chan_balance,
1517-
local_funding, remote_funding,
15181513
commit_tx_keys.clone(),
15191514
feerate_per_kw,
15201515
&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
@@ -549,12 +549,7 @@ impl TestChannelSigner {
549549
commitment_tx: &'a CommitmentTransaction, secp_ctx: &Secp256k1<T>,
550550
) -> TrustedCommitmentTransaction<'a> {
551551
commitment_tx
552-
.verify(
553-
&channel_parameters.as_counterparty_broadcastable(),
554-
channel_parameters.counterparty_pubkeys().unwrap(),
555-
&channel_parameters.holder_pubkeys,
556-
secp_ctx,
557-
)
552+
.verify(&channel_parameters.as_counterparty_broadcastable(), secp_ctx)
558553
.expect("derived different per-tx keys or built transaction")
559554
}
560555

@@ -563,12 +558,7 @@ impl TestChannelSigner {
563558
commitment_tx: &'a CommitmentTransaction, secp_ctx: &Secp256k1<T>,
564559
) -> TrustedCommitmentTransaction<'a> {
565560
commitment_tx
566-
.verify(
567-
&channel_parameters.as_holder_broadcastable(),
568-
&channel_parameters.holder_pubkeys,
569-
channel_parameters.counterparty_pubkeys().unwrap(),
570-
secp_ctx,
571-
)
561+
.verify(&channel_parameters.as_holder_broadcastable(), secp_ctx)
572562
.expect("derived different per-tx keys or built transaction")
573563
}
574564
}

0 commit comments

Comments
 (0)