Skip to content

Commit 39d86ae

Browse files
committed
fixup! more cleanup
1 parent fb34098 commit 39d86ae

File tree

7 files changed

+70
-60
lines changed

7 files changed

+70
-60
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -969,12 +969,12 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
969969

970970
let secp_ctx = Secp256k1::new();
971971

972-
let txid = initial_holder_commitment_tx.trust_txid();
972+
let txid = initial_holder_commitment_tx.untrusted_txid();
973973

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.trust_key_derivation();
977+
let tx_keys = commitment_tx.untrusted_key_derivation();
978978
let holder_commitment_tx = HolderSignedTx {
979979
txid,
980980
revocation_key: tx_keys.revocation_key,
@@ -1143,12 +1143,12 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
11431143
/// up-to-date as our holder commitment transaction is updated.
11441144
/// Panics if set_on_holder_tx_csv has never been called.
11451145
fn provide_latest_holder_commitment_tx(&mut self, holder_commitment_tx: HolderCommitmentTransaction, htlc_outputs: Vec<(HTLCOutputInCommitment, Option<Signature>, Option<HTLCSource>)>) -> Result<(), MonitorUpdateError> {
1146-
let txid = holder_commitment_tx.trust_txid();
1146+
let txid = holder_commitment_tx.untrusted_txid();
11471147

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.trust_key_derivation();
1151+
let tx_keys = &commitment_tx.untrusted_key_derivation();
11521152
self.current_holder_commitment_number = commitment_tx.commitment_number();
11531153
HolderSignedTx {
11541154
txid,

lightning/src/chain/keysinterface.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -463,15 +463,15 @@ impl ChannelKeys for InMemoryChannelKeys {
463463
fn key_derivation_params(&self) -> (u64, u64) { self.key_derivation_params }
464464

465465
fn sign_counterparty_commitment<T: secp256k1::Signing + secp256k1::Verification>(&self, commitment_tx: &CommitmentTransaction, secp_ctx: &Secp256k1<T>) -> Result<(Signature, Vec<Signature>), ()> {
466-
let keys = commitment_tx.trust_key_derivation();
466+
let keys = commitment_tx.untrusted_key_derivation();
467467

468468
let funding_pubkey = PublicKey::from_secret_key(secp_ctx, &self.funding_key);
469469
let channel_funding_redeemscript = make_funding_redeemscript(&funding_pubkey, &self.counterparty_pubkeys().funding_pubkey);
470470

471-
let built_tx = commitment_tx.trust_built_transaction();
471+
let built_tx = commitment_tx.untrusted_built_transaction();
472472
let commitment_sig = built_tx.sign(&self.funding_key, &channel_funding_redeemscript, self.channel_value_satoshis, secp_ctx);
473473

474-
let commitment_txid = commitment_tx.trust_txid();
474+
let commitment_txid = commitment_tx.untrusted_txid();
475475

476476
let mut htlc_sigs = Vec::with_capacity(commitment_tx.htlcs().len());
477477
for htlc in commitment_tx.htlcs() {
@@ -492,7 +492,7 @@ impl ChannelKeys for InMemoryChannelKeys {
492492
let funding_pubkey = PublicKey::from_secret_key(secp_ctx, &self.funding_key);
493493
let funding_redeemscript = make_funding_redeemscript(&funding_pubkey, &self.counterparty_pubkeys().funding_pubkey);
494494

495-
let built_tx = commitment_tx.inner.trust_built_transaction();
495+
let built_tx = commitment_tx.inner.untrusted_built_transaction();
496496
let sig = built_tx.sign(&self.funding_key, &funding_redeemscript, self.channel_value_satoshis, secp_ctx);
497497
let htlc_sigs_o = self.sign_holder_commitment_htlc_transactions(&commitment_tx, secp_ctx)?;
498498
let htlc_sigs = htlc_sigs_o.iter().map(|o| o.unwrap()).collect();
@@ -505,13 +505,13 @@ impl ChannelKeys for InMemoryChannelKeys {
505505
let funding_pubkey = PublicKey::from_secret_key(secp_ctx, &self.funding_key);
506506
let channel_funding_redeemscript = make_funding_redeemscript(&funding_pubkey, &self.counterparty_pubkeys().funding_pubkey);
507507

508-
let built_tx = holder_commitment_tx.inner.trust_built_transaction();
508+
let built_tx = holder_commitment_tx.inner.untrusted_built_transaction();
509509
Ok(built_tx.sign(&self.funding_key, &channel_funding_redeemscript, self.channel_value_satoshis, secp_ctx))
510510
}
511511

512512
fn sign_holder_commitment_htlc_transactions<T: secp256k1::Signing + secp256k1::Verification>(&self, commitment_tx: &HolderCommitmentTransaction, secp_ctx: &Secp256k1<T>) -> Result<Vec<Option<Signature>>, ()> {
513513
let channel_parameters = self.make_channel_parameters();
514-
let channel_parameters = channel_parameters.as_holder_directed();
514+
let channel_parameters = channel_parameters.as_holder_broadcastable();
515515
commitment_tx.inner.get_htlc_sigs(&self.htlc_base_key, &channel_parameters, secp_ctx)
516516
}
517517

lightning/src/ln/chan_utils.rs

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ use chain;
4141

4242
const HTLC_OUTPUT_IN_COMMITMENT_SIZE: usize = 1 + 8 + 4 + 32 + 5;
4343

44+
pub(crate) const MAX_HTLCS: u16 = 483;
45+
46+
// This checks that the buffer size is greater than the maximum possible size for serialized HTLCS
47+
const _EXCESS_BUFFER_SIZE: usize = MAX_BUF_SIZE - MAX_HTLCS as usize * HTLC_OUTPUT_IN_COMMITMENT_SIZE;
48+
4449
pub(super) const HTLC_SUCCESS_TX_WEIGHT: u64 = 703;
4550
pub(super) const HTLC_TIMEOUT_TX_WEIGHT: u64 = 663;
4651

@@ -298,7 +303,7 @@ pub fn derive_public_revocation_key<T: secp256k1::Verification>(secp_ctx: &Secp2
298303
///
299304
/// These keys are assumed to be good, either because the code derived them from
300305
/// channel basepoints via the new function, or they were obtained via
301-
/// PreCalculatedTxCreationKeys.trust_key_derivation because we trusted the source of the
306+
/// CommitmentTransaction.untrusted_key_derivation because we trusted the source of the
302307
/// pre-calculated keys.
303308
#[derive(PartialEq, Clone)]
304309
pub struct TxCreationKeys {
@@ -545,9 +550,10 @@ pub fn build_htlc_transaction(prev_hash: &Txid, feerate_per_kw: u32, contest_del
545550
}
546551

547552
/// Per-channel data used to build transactions in conjunction with the per-commitment data (CommitmentTransaction).
553+
/// The fields are organized by holder/counterparty.
548554
///
549-
/// Normally, this is converted to the owner-independent DirectedChannelTransactionParameters
550-
/// before use, via the as_holder_directed and as_counterparty_directed functions.
555+
/// Normally, this is converted to the broadcaster/countersignatory-organized DirectedChannelTransactionParameters
556+
/// before use, via the as_holder_broadcastable and as_counterparty_broadcastable functions.
551557
#[derive(Clone)]
552558
pub struct ChannelTransactionParameters {
553559
/// Holder public keys
@@ -583,8 +589,8 @@ impl ChannelTransactionParameters {
583589
/// given that the holder is the broadcaster.
584590
///
585591
/// self.is_populated() must be true before calling this function.
586-
pub fn as_holder_directed(&self) -> DirectedChannelTransactionParameters {
587-
assert!(self.is_populated(), "self.late_parameters must be set before using as_holder_directed");
592+
pub fn as_holder_broadcastable(&self) -> DirectedChannelTransactionParameters {
593+
assert!(self.is_populated(), "self.late_parameters must be set before using as_holder_broadcastable");
588594
DirectedChannelTransactionParameters {
589595
inner: self,
590596
holder_is_broadcaster: true
@@ -595,8 +601,8 @@ impl ChannelTransactionParameters {
595601
/// given that the counterparty is the broadcaster.
596602
///
597603
/// self.is_populated() must be true before calling this function.
598-
pub fn as_counterparty_directed(&self) -> DirectedChannelTransactionParameters {
599-
assert!(self.is_populated(), "self.late_parameters must be set before using as_counterparty_directed");
604+
pub fn as_counterparty_broadcastable(&self) -> DirectedChannelTransactionParameters {
605+
assert!(self.is_populated(), "self.late_parameters must be set before using as_counterparty_broadcastable");
600606
DirectedChannelTransactionParameters {
601607
inner: self,
602608
holder_is_broadcaster: false
@@ -617,11 +623,11 @@ impl_writeable!(ChannelTransactionParameters, 0, {
617623
funding_outpoint
618624
});
619625

620-
/// Static channel fields used to build transactions given per-commitment fields, independent of
621-
/// transaction ownership.
626+
/// Static channel fields used to build transactions given per-commitment fields, organized by
627+
/// broadcaster/countersignatory.
622628
///
623629
/// This is derived from the owner-specific ChannelTransactionParameters via the
624-
/// as_holder_directed and as_counterparty_directed functions.
630+
/// as_holder_broadcastable and as_counterparty_broadcastable functions.
625631
pub struct DirectedChannelTransactionParameters<'a> {
626632
/// The holder's channel static parameters
627633
inner: &'a ChannelTransactionParameters,
@@ -725,7 +731,7 @@ impl HolderCommitmentTransaction {
725731
funding_outpoint: Some(chain::transaction::OutPoint { txid: Default::default(), index: 0 })
726732
};
727733
let aux: Vec<()> = Vec::new();
728-
let inner = CommitmentTransaction::new_with_auxiliary_htlc_data(0, 0, 0, keys, 0, Vec::new(), aux, &channel_parameters.as_counterparty_directed()).0;
734+
let inner = CommitmentTransaction::new_with_auxiliary_htlc_data(0, 0, 0, keys, 0, Vec::new(), aux, &channel_parameters.as_counterparty_broadcastable()).0;
729735
HolderCommitmentTransaction {
730736
inner,
731737
counterparty_sig: dummy_sig,
@@ -734,9 +740,9 @@ impl HolderCommitmentTransaction {
734740
}
735741
}
736742

737-
// Trust the pre-built commitment transaction and return its ID.
738-
pub(crate) fn trust_txid(&self) -> Txid {
739-
self.inner.trust_txid()
743+
// Get pre-built commitment transaction and return its ID.
744+
pub(crate) fn untrusted_txid(&self) -> Txid {
745+
self.inner.untrusted_txid()
740746
}
741747

742748
/// Gets a signed HTLC transaction given a preimage (for !htlc.offered) and the holder HTLC transaction signature.
@@ -757,8 +763,8 @@ impl HolderCommitmentTransaction {
757763

758764
/// The pre-calculated transaction creation public keys.
759765
/// An external validating signer should not trust these keys.
760-
pub fn trust_key_derivation(&self) -> &TxCreationKeys {
761-
self.inner.trust_key_derivation()
766+
pub fn untrusted_key_derivation(&self) -> &TxCreationKeys {
767+
self.inner.untrusted_key_derivation()
762768
}
763769
}
764770

@@ -798,7 +804,7 @@ pub struct BuiltCommitmentTransaction {
798804
impl_writeable!(BuiltCommitmentTransaction, 0, { transaction, txid });
799805

800806
impl BuiltCommitmentTransaction {
801-
/// Get the SIGHASH_ALL sighash value and the transaction.
807+
/// Get the SIGHASH_ALL sighash value of the transaction.
802808
///
803809
/// This can be used to verify a signature.
804810
pub fn get_sighash_all(&self, funding_redeemscript: &Script, channel_value_satoshis: u64) -> Message {
@@ -808,7 +814,7 @@ impl BuiltCommitmentTransaction {
808814

809815
/// Sign a transaction, either because we are counter-signing the counterparty's transaction or
810816
/// because we are about to broadcast a holder transaction.
811-
pub fn sign<T: secp256k1::Signing + secp256k1::Verification>(&self, funding_key: &SecretKey, funding_redeemscript: &Script, channel_value_satoshis: u64, secp_ctx: &Secp256k1<T>) -> Signature {
817+
pub fn sign<T: secp256k1::Signing>(&self, funding_key: &SecretKey, funding_redeemscript: &Script, channel_value_satoshis: u64, secp_ctx: &Secp256k1<T>) -> Signature {
812818
let sighash = self.get_sighash_all(funding_redeemscript, channel_value_satoshis);
813819
secp_ctx.sign(&sighash, funding_key)
814820
}
@@ -829,7 +835,7 @@ pub struct CommitmentTransaction {
829835
htlcs: Vec<HTLCOutputInCommitment>,
830836
// A cache of the parties' pubkeys required to construct the transaction
831837
keys: TxCreationKeys,
832-
// For access to the built transaction, see doc for trust_built_transaction
838+
// For access to the pre-built transaction, see doc for untrusted_built_transaction
833839
built: BuiltCommitmentTransaction,
834840
}
835841

@@ -888,6 +894,10 @@ impl CommitmentTransaction {
888894
/// Also keeps track of auxiliary HTLC data and returns it along with the mutated and sorted HTLCs.
889895
/// This allows the caller to match the HTLC output index with the auxiliary data.
890896
/// This auxiliary data is not stored in this object.
897+
///
898+
/// Only include HTLCs that are above the dust limit for the channel.
899+
///
900+
/// Panics if the length of htlcs and aux are different.
891901
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: Vec<HTLCOutputInCommitment>, aux: Vec<T>, channel_parameters: &DirectedChannelTransactionParameters) -> (CommitmentTransaction, Vec<(HTLCOutputInCommitment, T)>) {
892902
// Sort outputs and populate output indices while keeping track of the auxiliary data
893903
let mut txouts = Self::internal_build_outputs(&keys, to_broadcaster_value_sat, to_countersignatory_value_sat, &htlcs, aux, channel_parameters).unwrap();
@@ -921,16 +931,16 @@ impl CommitmentTransaction {
921931
(info, result_htlcs_with_aux)
922932
}
923933

924-
// Trust the pre-built commitment transaction and return its ID.
925-
pub(crate) fn trust_txid(&self) -> Txid {
934+
// Get the pre-built commitment transaction's ID.
935+
pub(crate) fn untrusted_txid(&self) -> Txid {
926936
self.built.txid
927937
}
928938

929-
/// Trust the pre-built commitment transaction.
939+
/// Get the pre-built commitment transaction.
930940
///
931941
/// This should only be used if you fully trust the builder of this object. It should not
932942
/// be used by an external signer - instead use the build function.
933-
pub fn trust_built_transaction(&self) -> &BuiltCommitmentTransaction {
943+
pub fn untrusted_built_transaction(&self) -> &BuiltCommitmentTransaction {
934944
&self.built
935945
}
936946

@@ -1068,8 +1078,8 @@ impl CommitmentTransaction {
10681078
/// The returned Vec has one entry for each HTLC, and in the same order. For HTLCs which were
10691079
/// considered dust and not included, a None entry exists, for all others a signature is
10701080
/// included.
1071-
pub fn get_htlc_sigs<T: secp256k1::Signing + secp256k1::Verification>(&self, htlc_base_key: &SecretKey, channel_parameters: &DirectedChannelTransactionParameters, secp_ctx: &Secp256k1<T>) -> Result<Vec<Option<Signature>>, ()> {
1072-
let txid = self.trust_txid();
1081+
pub fn get_htlc_sigs<T: secp256k1::Signing>(&self, htlc_base_key: &SecretKey, channel_parameters: &DirectedChannelTransactionParameters, secp_ctx: &Secp256k1<T>) -> Result<Vec<Option<Signature>>, ()> {
1082+
let txid = self.untrusted_txid();
10731083
let mut ret = Vec::with_capacity(self.htlcs.len());
10741084
let holder_htlc_key = derive_private_key(secp_ctx, &self.keys.per_commitment_point, htlc_base_key).map_err(|_| ())?;
10751085

@@ -1090,7 +1100,7 @@ impl CommitmentTransaction {
10901100

10911101
/// Gets a signed HTLC transaction given a preimage (for !htlc.offered) and the holder HTLC transaction signature.
10921102
pub(crate) fn get_signed_htlc_tx(&self, channel_parameters: &DirectedChannelTransactionParameters, htlc_index: usize, counterparty_signature: &Signature, signature: &Signature, preimage: &Option<PaymentPreimage>) -> Transaction {
1093-
let txid = self.trust_txid();
1103+
let txid = self.untrusted_txid();
10941104
let this_htlc = &self.htlcs[htlc_index];
10951105
assert!(this_htlc.transaction_output_index.is_some());
10961106
// if we don't have preimage for an HTLC-Success, we can't generate an HTLC transaction.
@@ -1123,7 +1133,7 @@ impl CommitmentTransaction {
11231133

11241134
/// The pre-calculated transaction creation public keys.
11251135
/// An external validating signer should not trust these keys.
1126-
pub fn trust_key_derivation(&self) -> &TxCreationKeys {
1136+
pub fn untrusted_key_derivation(&self) -> &TxCreationKeys {
11271137
&self.keys
11281138
}
11291139

0 commit comments

Comments
 (0)