Skip to content

Commit fb34098

Browse files
committed
fixup! private fields in CommitmentTransaction
1 parent 4a38ff2 commit fb34098

File tree

6 files changed

+32
-31
lines changed

6 files changed

+32
-31
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -974,18 +974,18 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
974974
// block for Rust 1.34 compat
975975
let (holder_commitment_tx, current_holder_commitment_number) = {
976976
let commitment_tx = &initial_holder_commitment_tx.inner;
977-
let tx_keys = &commitment_tx.keys;
977+
let tx_keys = commitment_tx.trust_key_derivation();
978978
let holder_commitment_tx = HolderSignedTx {
979979
txid,
980980
revocation_key: tx_keys.revocation_key,
981981
a_htlc_key: tx_keys.broadcaster_htlc_key,
982982
b_htlc_key: tx_keys.countersignatory_htlc_key,
983983
delayed_payment_key: tx_keys.broadcaster_delayed_payment_key,
984984
per_commitment_point: tx_keys.per_commitment_point,
985-
feerate_per_kw: commitment_tx.feerate_per_kw,
985+
feerate_per_kw: commitment_tx.feerate_per_kw(),
986986
htlc_outputs: Vec::new(), // There are never any HTLCs in the initial commitment transactions
987987
};
988-
(holder_commitment_tx, commitment_tx.commitment_number)
988+
(holder_commitment_tx, commitment_tx.commitment_number())
989989
};
990990
onchain_tx_handler.provide_latest_holder_tx(initial_holder_commitment_tx);
991991

@@ -1148,16 +1148,16 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
11481148
// block for Rust 1.34 compat
11491149
let mut new_holder_commitment_tx = {
11501150
let commitment_tx = &holder_commitment_tx.inner;
1151-
let tx_keys = &commitment_tx.keys;
1152-
self.current_holder_commitment_number = commitment_tx.commitment_number;
1151+
let tx_keys = &commitment_tx.trust_key_derivation();
1152+
self.current_holder_commitment_number = commitment_tx.commitment_number();
11531153
HolderSignedTx {
11541154
txid,
11551155
revocation_key: tx_keys.revocation_key,
11561156
a_htlc_key: tx_keys.broadcaster_htlc_key,
11571157
b_htlc_key: tx_keys.countersignatory_htlc_key,
11581158
delayed_payment_key: tx_keys.broadcaster_delayed_payment_key,
11591159
per_commitment_point: tx_keys.per_commitment_point,
1160-
feerate_per_kw: commitment_tx.feerate_per_kw,
1160+
feerate_per_kw: commitment_tx.feerate_per_kw(),
11611161
htlc_outputs,
11621162
}
11631163
};

lightning/src/chain/keysinterface.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -473,9 +473,9 @@ impl ChannelKeys for InMemoryChannelKeys {
473473

474474
let commitment_txid = commitment_tx.trust_txid();
475475

476-
let mut htlc_sigs = Vec::with_capacity(commitment_tx.htlcs.len());
477-
for htlc in &commitment_tx.htlcs {
478-
let htlc_tx = chan_utils::build_htlc_transaction(&commitment_txid, commitment_tx.feerate_per_kw, self.holder_selected_contest_delay(), htlc, &keys.broadcaster_delayed_payment_key, &keys.revocation_key);
476+
let mut htlc_sigs = Vec::with_capacity(commitment_tx.htlcs().len());
477+
for htlc in commitment_tx.htlcs() {
478+
let htlc_tx = chan_utils::build_htlc_transaction(&commitment_txid, commitment_tx.feerate_per_kw(), self.holder_selected_contest_delay(), htlc, &keys.broadcaster_delayed_payment_key, &keys.revocation_key);
479479
let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, &keys);
480480
let htlc_sighash = hash_to_message!(&bip143::SigHashCache::new(&htlc_tx).signature_hash(0, &htlc_redeemscript, htlc.amount_msat / 1000, SigHashType::All)[..]);
481481
let holder_htlc_key = match chan_utils::derive_private_key(&secp_ctx, &keys.per_commitment_point, &self.htlc_base_key) {

lightning/src/ln/chan_utils.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -822,15 +822,15 @@ impl BuiltCommitmentTransaction {
822822
/// secret key.
823823
#[derive(Clone)]
824824
pub struct CommitmentTransaction {
825-
pub(crate) commitment_number: u64,
826-
pub(crate) to_broadcaster_value_sat: u64,
827-
pub(crate) to_countersignatory_value_sat: u64,
828-
pub(crate) feerate_per_kw: u32,
829-
pub(crate) htlcs: Vec<HTLCOutputInCommitment>,
825+
commitment_number: u64,
826+
to_broadcaster_value_sat: u64,
827+
to_countersignatory_value_sat: u64,
828+
feerate_per_kw: u32,
829+
htlcs: Vec<HTLCOutputInCommitment>,
830830
// A cache of the parties' pubkeys required to construct the transaction
831-
pub(crate) keys: TxCreationKeys,
831+
keys: TxCreationKeys,
832832
// For access to the built transaction, see doc for trust_built_transaction
833-
pub(crate) built: BuiltCommitmentTransaction,
833+
built: BuiltCommitmentTransaction,
834834
}
835835

836836
impl PartialEq for CommitmentTransaction {

lightning/src/ln/channel.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,7 +1468,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
14681468
let keys = self.build_holder_transaction_keys(self.cur_holder_commitment_transaction_number)?;
14691469
let initial_commitment_tx = self.build_commitment_transaction(self.cur_holder_commitment_transaction_number, &keys, true, false, self.feerate_per_kw, logger).0;
14701470
{
1471-
let initial_commitment_bitcoin_tx = &initial_commitment_tx.built;
1471+
let initial_commitment_bitcoin_tx = initial_commitment_tx.trust_built_transaction();
14721472
let sighash = initial_commitment_bitcoin_tx.get_sighash_all(&funding_script, self.channel_value_satoshis);
14731473
// They sign the holder commitment transaction...
14741474
log_trace!(logger, "Checking funding_created tx signature {} by key {} against tx {} (sighash {}) with redeemscript {}", log_bytes!(sig.serialize_compact()[..]), log_bytes!(self.counterparty_funding_pubkey().serialize()), encode::serialize_hex(&initial_commitment_bitcoin_tx.transaction), log_bytes!(sighash[..]), encode::serialize_hex(&funding_script));
@@ -1478,7 +1478,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
14781478
let counterparty_keys = self.build_remote_transaction_keys()?;
14791479
let counterparty_initial_commitment_tx = self.build_commitment_transaction(self.cur_counterparty_commitment_transaction_number, &counterparty_keys, false, false, self.feerate_per_kw, logger).0;
14801480

1481-
let counterparty_initial_bitcoin_tx = &counterparty_initial_commitment_tx.built;
1481+
let counterparty_initial_bitcoin_tx = &counterparty_initial_commitment_tx.trust_built_transaction();
14821482
log_trace!(logger, "Initial counterparty ID {} tx {}", counterparty_initial_bitcoin_tx.txid, encode::serialize_hex(&counterparty_initial_bitcoin_tx.transaction));
14831483

14841484
let counterparty_signature = self.holder_keys.sign_counterparty_commitment(&counterparty_initial_commitment_tx, &self.secp_ctx)
@@ -1580,14 +1580,14 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
15801580
let counterparty_keys = self.build_remote_transaction_keys()?;
15811581
let counterparty_initial_commitment_tx = self.build_commitment_transaction(self.cur_counterparty_commitment_transaction_number, &counterparty_keys, false, false, self.feerate_per_kw, logger).0;
15821582
// TODO the txid is not integration tested
1583-
let counterparty_initial_bitcoin_tx = counterparty_initial_commitment_tx.built;
1583+
let counterparty_initial_bitcoin_tx = counterparty_initial_commitment_tx.trust_built_transaction();
15841584

15851585
log_trace!(logger, "Initial counterparty ID {} tx {}", counterparty_initial_bitcoin_tx.txid, encode::serialize_hex(&counterparty_initial_bitcoin_tx.transaction));
15861586

15871587
let holder_keys = self.build_holder_transaction_keys(self.cur_holder_commitment_transaction_number)?;
15881588
let initial_commitment_tx = self.build_commitment_transaction(self.cur_holder_commitment_transaction_number, &holder_keys, true, false, self.feerate_per_kw, logger).0;
15891589
{
1590-
let initial_commitment_bitcoin_tx = &initial_commitment_tx.built;
1590+
let initial_commitment_bitcoin_tx = &initial_commitment_tx.trust_built_transaction();
15911591
let sighash = initial_commitment_bitcoin_tx.get_sighash_all(&funding_script, self.channel_value_satoshis);
15921592
// They sign our commitment transaction, allowing us to broadcast the tx if we wish.
15931593
if let Err(_) = self.secp_ctx.verify(&sighash, &msg.signature, &self.get_counterparty_pubkeys().funding_pubkey) {
@@ -1982,7 +1982,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
19821982
let (num_htlcs, mut htlcs_cloned, commitment_tx, commitment_txid) = {
19831983
let commitment_tx = self.build_commitment_transaction(self.cur_holder_commitment_transaction_number, &keys, true, false, feerate_per_kw, logger);
19841984
let commitment_txid = {
1985-
let initial_commitment_bitcoin_tx = &commitment_tx.0.built;
1985+
let initial_commitment_bitcoin_tx = &commitment_tx.0.trust_built_transaction();
19861986
let sighash = initial_commitment_bitcoin_tx.get_sighash_all(&funding_script, self.channel_value_satoshis);
19871987

19881988
let commitment_txid = initial_commitment_bitcoin_tx.txid;
@@ -4756,7 +4756,7 @@ mod tests {
47564756
let htlc_sigs = chan_keys.sign_holder_commitment_htlc_transactions(&holder_commitment_tx, &secp_ctx).unwrap();
47574757

47584758
// ((htlc, counterparty_sig), (index, holder_sig))
4759-
let mut htlc_sig_iter = holder_commitment_tx.inner.htlcs.iter().zip(holder_commitment_tx.counterparty_htlc_sigs).zip(htlc_sigs.iter().enumerate());
4759+
let mut htlc_sig_iter = holder_commitment_tx.inner.htlcs().iter().zip(holder_commitment_tx.counterparty_htlc_sigs).zip(htlc_sigs.iter().enumerate());
47604760

47614761
$({
47624762
let remote_signature = Signature::from_der(&hex::decode($counterparty_htlc_sig_hex).unwrap()[..]).unwrap();

lightning/src/ln/onchaintx.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
651651
let signed_tx = self.get_fully_signed_holder_tx(funding_redeemscript).unwrap();
652652
// Timer set to $NEVER given we can't bump tx without anchor outputs
653653
log_trace!(logger, "Going to broadcast Holder Transaction {} claiming funding output {} from {}...", signed_tx.txid(), outp.vout, outp.txid);
654-
return Some((None, self.holder_commitment.as_ref().unwrap().inner.feerate_per_kw, signed_tx));
654+
return Some((None, self.holder_commitment.as_ref().unwrap().inner.feerate_per_kw(), signed_tx));
655655
}
656656
_ => unreachable!()
657657
}
@@ -915,7 +915,7 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
915915

916916
fn extract_holder_sigs(holder_commitment: &HolderCommitmentTransaction, sigs: Vec<Signature>) -> Vec<Option<(usize, Signature)>> {
917917
let mut ret = Vec::new();
918-
for (htlc_idx, (holder_sig, htlc)) in sigs.iter().zip(holder_commitment.inner.htlcs.iter()).enumerate() {
918+
for (htlc_idx, (holder_sig, htlc)) in sigs.iter().zip(holder_commitment.inner.htlcs().iter()).enumerate() {
919919
let tx_idx = htlc.transaction_output_index.unwrap();
920920
if ret.len() <= tx_idx as usize { ret.resize(tx_idx as usize + 1, None); }
921921
ret[tx_idx as usize] = Some((htlc_idx, holder_sig.clone()));

lightning/src/util/enforcing_trait_impls.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,13 @@ impl ChannelKeys for EnforcingChannelKeys {
7979
self.check_keys(secp_ctx, commitment_tx.trust_key_derivation());
8080

8181
{
82-
let mut last_commitment_number = self.last_commitment_number.lock().unwrap();
83-
let commitment_number = last_commitment_number.unwrap_or(commitment_tx.commitment_number);
82+
let mut last_commitment_number_guard = self.last_commitment_number.lock().unwrap();
83+
let actual_commitment_number = commitment_tx.commitment_number();
84+
let last_commitment_number = last_commitment_number_guard.unwrap_or(actual_commitment_number);
8485
// These commitment numbers are backwards counting. We expect either the same as the previously encountered,
8586
// or the next one.
86-
assert!(commitment_number == commitment_tx.commitment_number || commitment_number - 1 == commitment_tx.commitment_number, "{} doesn't come after {}", commitment_tx.commitment_number, commitment_number);
87-
*last_commitment_number = Some(cmp::min(commitment_number, commitment_tx.commitment_number))
87+
assert!(last_commitment_number == actual_commitment_number || last_commitment_number - 1 == actual_commitment_number, "{} doesn't come after {}", actual_commitment_number, last_commitment_number);
88+
*last_commitment_number_guard = Some(cmp::min(last_commitment_number, actual_commitment_number))
8889
}
8990

9091
Ok(self.inner.sign_counterparty_commitment(commitment_tx, secp_ctx).unwrap())
@@ -106,10 +107,10 @@ impl ChannelKeys for EnforcingChannelKeys {
106107
let commitment_txid = commitment_tx.trust_txid();
107108
let holder_csv = self.inner.counterparty_selected_contest_delay();
108109

109-
for (this_htlc, sig) in commitment_tx.htlcs.iter().zip(&holder_commitment_tx.counterparty_htlc_sigs) {
110+
for (this_htlc, sig) in commitment_tx.htlcs().iter().zip(&holder_commitment_tx.counterparty_htlc_sigs) {
110111
assert!(this_htlc.transaction_output_index.is_some());
111-
let keys = &commitment_tx.keys;
112-
let htlc_tx = chan_utils::build_htlc_transaction(&commitment_txid, commitment_tx.feerate_per_kw, holder_csv, &this_htlc, &keys.broadcaster_delayed_payment_key, &keys.revocation_key);
112+
let keys = &commitment_tx.trust_key_derivation();
113+
let htlc_tx = chan_utils::build_htlc_transaction(&commitment_txid, commitment_tx.feerate_per_kw(), holder_csv, &this_htlc, &keys.broadcaster_delayed_payment_key, &keys.revocation_key);
113114

114115
let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&this_htlc, &keys);
115116

0 commit comments

Comments
 (0)