Skip to content

Commit 2a82a1f

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 3dcf40a commit 2a82a1f

File tree

5 files changed

+22
-51
lines changed

5 files changed

+22
-51
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3470,22 +3470,13 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
34703470
to_broadcaster_value: u64, to_countersignatory_value: u64, feerate_per_kw: u32,
34713471
nondust_htlcs: impl Iterator<Item = &'a mut HTLCOutputInCommitment>,
34723472
) -> CommitmentTransaction {
3473-
let broadcaster_keys = &self.onchain_tx_handler.channel_transaction_parameters
3474-
.counterparty_parameters.as_ref().unwrap().pubkeys;
3475-
let countersignatory_keys =
3476-
&self.onchain_tx_handler.channel_transaction_parameters.holder_pubkeys;
3477-
3478-
let broadcaster_funding_key = broadcaster_keys.funding_pubkey;
3479-
let countersignatory_funding_key = countersignatory_keys.funding_pubkey;
3480-
let keys = TxCreationKeys::from_channel_static_keys(&their_per_commitment_point,
3481-
&broadcaster_keys, &countersignatory_keys, &self.onchain_tx_handler.secp_ctx);
34823473
let channel_parameters =
34833474
&self.onchain_tx_handler.channel_transaction_parameters.as_counterparty_broadcastable();
3475+
let keys = TxCreationKeys::from_channel_static_keys(&their_per_commitment_point,
3476+
channel_parameters.broadcaster_pubkeys(), channel_parameters.countersignatory_pubkeys(), &self.onchain_tx_handler.secp_ctx);
34843477

34853478
CommitmentTransaction::new(commitment_number,
3486-
to_broadcaster_value, to_countersignatory_value, broadcaster_funding_key,
3487-
countersignatory_funding_key, keys, feerate_per_kw, nondust_htlcs,
3488-
channel_parameters)
3479+
to_broadcaster_value, to_countersignatory_value, keys, feerate_per_kw, nondust_htlcs, channel_parameters)
34893480
}
34903481

34913482
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
@@ -1136,7 +1136,7 @@ impl HolderCommitmentTransaction {
11361136
for _ in 0..nondust_htlcs.len() {
11371137
counterparty_htlc_sigs.push(dummy_sig);
11381138
}
1139-
let inner = CommitmentTransaction::new(0, 0, 0, dummy_key.clone(), dummy_key.clone(), keys, 0, nondust_htlcs.iter_mut(), &channel_parameters.as_counterparty_broadcastable());
1139+
let inner = CommitmentTransaction::new(0, 0, 0, keys, 0, nondust_htlcs.iter_mut(), &channel_parameters.as_counterparty_broadcastable());
11401140
nondust_htlcs.sort_by_key(|htlc| htlc.transaction_output_index);
11411141
HolderCommitmentTransaction {
11421142
inner,
@@ -1443,12 +1443,12 @@ impl CommitmentTransaction {
14431443
/// Only include HTLCs that are above the dust limit for the channel.
14441444
///
14451445
/// This is not exported to bindings users due to the generic though we likely should expose a version without
1446-
pub fn new<'a>(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, nondust_htlcs: impl Iterator<Item = &'a mut HTLCOutputInCommitment>, channel_parameters: &DirectedChannelTransactionParameters) -> CommitmentTransaction {
1446+
pub fn new<'a>(commitment_number: u64, to_broadcaster_value_sat: u64, to_countersignatory_value_sat: u64, keys: TxCreationKeys, feerate_per_kw: u32, nondust_htlcs: impl Iterator<Item = &'a mut HTLCOutputInCommitment>, channel_parameters: &DirectedChannelTransactionParameters) -> CommitmentTransaction {
14471447
let to_broadcaster_value_sat = Amount::from_sat(to_broadcaster_value_sat);
14481448
let to_countersignatory_value_sat = Amount::from_sat(to_countersignatory_value_sat);
14491449

14501450
// Sort outputs and populate output indices while keeping track of the auxiliary data
1451-
let (outputs, nondust_htlcs) = Self::internal_build_outputs(&keys, to_broadcaster_value_sat, to_countersignatory_value_sat, nondust_htlcs, channel_parameters, &broadcaster_funding_key, &countersignatory_funding_key).unwrap();
1451+
let (outputs, nondust_htlcs) = Self::internal_build_outputs(&keys, to_broadcaster_value_sat, to_countersignatory_value_sat, nondust_htlcs, channel_parameters).unwrap();
14521452

14531453
let (obscured_commitment_transaction_number, txins) = Self::internal_build_inputs(commitment_number, channel_parameters);
14541454
let transaction = Self::make_transaction(obscured_commitment_transaction_number, txins, outputs);
@@ -1477,11 +1477,11 @@ impl CommitmentTransaction {
14771477
self
14781478
}
14791479

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

14831483
let mut nondust_htlcs = self.htlcs.clone();
1484-
let (outputs, _) = Self::internal_build_outputs(keys, self.to_broadcaster_value_sat, self.to_countersignatory_value_sat, nondust_htlcs.iter_mut(), channel_parameters, broadcaster_funding_key, countersignatory_funding_key)?;
1484+
let (outputs, _) = Self::internal_build_outputs(keys, self.to_broadcaster_value_sat, self.to_countersignatory_value_sat, nondust_htlcs.iter_mut(), channel_parameters)?;
14851485

14861486
let transaction = Self::make_transaction(obscured_commitment_transaction_number, txins, outputs);
14871487
let txid = transaction.compute_txid();
@@ -1505,8 +1505,10 @@ impl CommitmentTransaction {
15051505
// - initial sorting of outputs / HTLCs in the constructor, in which case T is auxiliary data the
15061506
// caller needs to have sorted together with the HTLCs so it can keep track of the output index
15071507
// - building of a bitcoin transaction during a verify() call, in which case T is just ()
1508-
fn internal_build_outputs<'a>(keys: &TxCreationKeys, to_broadcaster_value_sat: Amount, to_countersignatory_value_sat: Amount, nondust_htlcs: impl Iterator<Item = &'a mut HTLCOutputInCommitment>, channel_parameters: &DirectedChannelTransactionParameters, broadcaster_funding_key: &PublicKey, countersignatory_funding_key: &PublicKey) -> Result<(Vec<TxOut>, Vec<HTLCOutputInCommitment>), ()> {
1508+
fn internal_build_outputs<'a>(keys: &TxCreationKeys, to_broadcaster_value_sat: Amount, to_countersignatory_value_sat: Amount, nondust_htlcs: impl Iterator<Item = &'a mut HTLCOutputInCommitment>, channel_parameters: &DirectedChannelTransactionParameters) -> Result<(Vec<TxOut>, Vec<HTLCOutputInCommitment>), ()> {
15091509
let countersignatory_pubkeys = channel_parameters.countersignatory_pubkeys();
1510+
let broadcaster_funding_key = &channel_parameters.broadcaster_pubkeys().funding_pubkey;
1511+
let countersignatory_funding_key = &countersignatory_pubkeys.funding_pubkey;
15101512
let contest_delay = channel_parameters.contest_delay();
15111513

15121514
let mut txouts: Vec<(TxOut, Option<&mut HTLCOutputInCommitment>)> = Vec::new();
@@ -1680,14 +1682,14 @@ impl CommitmentTransaction {
16801682
///
16811683
/// An external validating signer must call this method before signing
16821684
/// or using the built transaction.
1683-
pub fn verify<T: secp256k1::Signing + secp256k1::Verification>(&self, channel_parameters: &DirectedChannelTransactionParameters, broadcaster_keys: &ChannelPublicKeys, countersignatory_keys: &ChannelPublicKeys, secp_ctx: &Secp256k1<T>) -> Result<TrustedCommitmentTransaction, ()> {
1685+
pub fn verify<T: secp256k1::Signing + secp256k1::Verification>(&self, channel_parameters: &DirectedChannelTransactionParameters, secp_ctx: &Secp256k1<T>) -> Result<TrustedCommitmentTransaction, ()> {
16841686
// This is the only field of the key cache that we trust
16851687
let per_commitment_point = self.keys.per_commitment_point;
1686-
let keys = TxCreationKeys::from_channel_static_keys(&per_commitment_point, broadcaster_keys, countersignatory_keys, secp_ctx);
1688+
let keys = TxCreationKeys::from_channel_static_keys(&per_commitment_point, channel_parameters.broadcaster_pubkeys(), channel_parameters.countersignatory_pubkeys(), secp_ctx);
16871689
if keys != self.keys {
16881690
return Err(());
16891691
}
1690-
let tx = self.internal_rebuild_transaction(&keys, channel_parameters, &broadcaster_keys.funding_pubkey, &countersignatory_keys.funding_pubkey)?;
1692+
let tx = self.internal_rebuild_transaction(&keys, channel_parameters)?;
16911693
if self.built.transaction != tx.transaction || self.built.txid != tx.txid {
16921694
return Err(());
16931695
}
@@ -1910,8 +1912,6 @@ mod tests {
19101912

19111913
struct TestCommitmentTxBuilder {
19121914
commitment_number: u64,
1913-
holder_funding_pubkey: PublicKey,
1914-
counterparty_funding_pubkey: PublicKey,
19151915
keys: TxCreationKeys,
19161916
feerate_per_kw: u32,
19171917
nondust_htlcs: Vec<HTLCOutputInCommitment>,
@@ -1946,8 +1946,6 @@ mod tests {
19461946

19471947
Self {
19481948
commitment_number: 0,
1949-
holder_funding_pubkey: holder_pubkeys.funding_pubkey,
1950-
counterparty_funding_pubkey: counterparty_pubkeys.funding_pubkey,
19511949
keys,
19521950
feerate_per_kw: 1,
19531951
nondust_htlcs,
@@ -1959,8 +1957,6 @@ mod tests {
19591957
fn build(&mut self, to_broadcaster_sats: u64, to_countersignatory_sats: u64) -> CommitmentTransaction {
19601958
CommitmentTransaction::new(
19611959
self.commitment_number, to_broadcaster_sats, to_countersignatory_sats,
1962-
self.holder_funding_pubkey.clone(),
1963-
self.counterparty_funding_pubkey.clone(),
19641960
self.keys.clone(), self.feerate_per_kw,
19651961
self.nondust_htlcs.iter_mut(), &self.channel_parameters.as_holder_broadcastable()
19661962
)

lightning/src/ln/channel.rs

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

35963596
let mut value_to_a = if local { value_to_self } else { value_to_remote };
35973597
let mut value_to_b = if local { value_to_remote } else { value_to_self };
3598-
let (funding_pubkey_a, funding_pubkey_b) = if local {
3599-
(self.get_holder_pubkeys().funding_pubkey, self.get_counterparty_pubkeys().funding_pubkey)
3600-
} else {
3601-
(self.get_counterparty_pubkeys().funding_pubkey, self.get_holder_pubkeys().funding_pubkey)
3602-
};
36033598

36043599
if value_to_a >= (broadcaster_dust_limit_satoshis as i64) {
36053600
log_trace!(logger, " ...including {} output with value {}", if local { "to_local" } else { "to_remote" }, value_to_a);
@@ -3621,8 +3616,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
36213616
let tx = CommitmentTransaction::new(commitment_number,
36223617
value_to_a as u64,
36233618
value_to_b as u64,
3624-
funding_pubkey_a,
3625-
funding_pubkey_b,
36263619
keys.clone(),
36273620
feerate_per_kw,
36283621
included_non_dust_htlcs.iter_mut().map(|(htlc, _)| htlc),

lightning/src/ln/functional_tests.rs

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

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

754753
// Assemble the set of keys we can use for signatures for our commitment_signed message.
@@ -765,7 +764,6 @@ fn test_update_fee_that_funder_cannot_afford() {
765764
INITIAL_COMMITMENT_NUMBER - 1,
766765
push_sats,
767766
channel_value - push_sats - commit_tx_fee_msat(non_buffer_feerate + 4, 0, &channel_type_features) / 1000,
768-
local_funding, remote_funding,
769767
commit_tx_keys.clone(),
770768
non_buffer_feerate + 4,
771769
htlcs.iter_mut(),
@@ -1456,7 +1454,7 @@ fn test_fee_spike_violation_fails_htlc() {
14561454

14571455
// Get the TestChannelSigner for each channel, which will be used to (1) get the keys
14581456
// needed to sign the new commitment tx and (2) sign the new commitment tx.
1459-
let (local_revocation_basepoint, local_htlc_basepoint, local_secret, next_local_point, local_funding) = {
1457+
let (local_revocation_basepoint, local_htlc_basepoint, local_secret, next_local_point) = {
14601458
let per_peer_state = nodes[0].node.per_peer_state.read().unwrap();
14611459
let chan_lock = per_peer_state.get(&nodes[1].node.get_our_node_id()).unwrap().lock().unwrap();
14621460
let local_chan = chan_lock.channel_by_id.get(&chan.2).and_then(Channel::as_funded).unwrap();
@@ -1467,18 +1465,16 @@ fn test_fee_spike_violation_fails_htlc() {
14671465
let pubkeys = chan_signer.as_ref().pubkeys();
14681466
(pubkeys.revocation_basepoint, pubkeys.htlc_basepoint,
14691467
chan_signer.as_ref().release_commitment_secret(INITIAL_COMMITMENT_NUMBER).unwrap(),
1470-
chan_signer.as_ref().get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 2, &secp_ctx).unwrap(),
1471-
chan_signer.as_ref().pubkeys().funding_pubkey)
1468+
chan_signer.as_ref().get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 2, &secp_ctx).unwrap())
14721469
};
1473-
let (remote_delayed_payment_basepoint, remote_htlc_basepoint, remote_point, remote_funding) = {
1470+
let (remote_delayed_payment_basepoint, remote_htlc_basepoint, remote_point) = {
14741471
let per_peer_state = nodes[1].node.per_peer_state.read().unwrap();
14751472
let chan_lock = per_peer_state.get(&nodes[0].node.get_our_node_id()).unwrap().lock().unwrap();
14761473
let remote_chan = chan_lock.channel_by_id.get(&chan.2).and_then(Channel::as_funded).unwrap();
14771474
let chan_signer = remote_chan.get_signer();
14781475
let pubkeys = chan_signer.as_ref().pubkeys();
14791476
(pubkeys.delayed_payment_basepoint, pubkeys.htlc_basepoint,
1480-
chan_signer.as_ref().get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 1, &secp_ctx).unwrap(),
1481-
chan_signer.as_ref().pubkeys().funding_pubkey)
1477+
chan_signer.as_ref().get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 1, &secp_ctx).unwrap())
14821478
};
14831479

14841480
// Assemble the set of keys we can use for signatures for our commitment_signed message.
@@ -1508,7 +1504,6 @@ fn test_fee_spike_violation_fails_htlc() {
15081504
commitment_number,
15091505
95000,
15101506
local_chan_balance,
1511-
local_funding, remote_funding,
15121507
commit_tx_keys.clone(),
15131508
feerate_per_kw,
15141509
vec![accepted_htlc_info].iter_mut(),

lightning/src/util/test_channel_signer.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -548,8 +548,6 @@ impl TestChannelSigner {
548548
commitment_tx
549549
.verify(
550550
&self.inner.get_channel_parameters().unwrap().as_counterparty_broadcastable(),
551-
self.inner.counterparty_pubkeys().unwrap(),
552-
self.inner.pubkeys(),
553551
secp_ctx,
554552
)
555553
.expect("derived different per-tx keys or built transaction")
@@ -561,8 +559,6 @@ impl TestChannelSigner {
561559
commitment_tx
562560
.verify(
563561
&self.inner.get_channel_parameters().unwrap().as_holder_broadcastable(),
564-
self.inner.pubkeys(),
565-
self.inner.counterparty_pubkeys().unwrap(),
566562
secp_ctx,
567563
)
568564
.expect("derived different per-tx keys or built transaction")

0 commit comments

Comments
 (0)