Skip to content

Commit

Permalink
temp please review
Browse files Browse the repository at this point in the history
  • Loading branch information
tankyleo committed Dec 4, 2024
1 parent 467a0b6 commit 17f3366
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 16 deletions.
3 changes: 2 additions & 1 deletion lightning/src/chain/channelmonitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3388,9 +3388,10 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
let channel_parameters =
&self.onchain_tx_handler.channel_transaction_parameters.as_counterparty_broadcastable();
let counterparty_payment_script = self.onchain_tx_handler.signer.get_counterparty_payment_script(&channel_parameters.channel_type_features(), &channel_parameters.countersignatory_pubkeys().payment_point);
let revokeable_spk = self.onchain_tx_handler.signer.get_revokeable_spk(&keys.revocation_key, channel_parameters.contest_delay(), &keys.broadcaster_delayed_payment_key);

CommitmentTransaction::new_with_auxiliary_htlc_data(commitment_number,
to_broadcaster_value, to_countersignatory_value, counterparty_payment_script, broadcaster_funding_key,
to_broadcaster_value, revokeable_spk, to_countersignatory_value, counterparty_payment_script, broadcaster_funding_key,
countersignatory_funding_key, keys, feerate_per_kw, &mut nondust_htlcs,
channel_parameters)
}
Expand Down
32 changes: 17 additions & 15 deletions lightning/src/ln/chan_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1175,7 +1175,8 @@ impl HolderCommitmentTransaction {
}
let channel_parameters = channel_parameters.as_counterparty_broadcastable();
let counterparty_payment_script = get_counterparty_payment_script(&channel_parameters.channel_type_features(), &channel_parameters.countersignatory_pubkeys().payment_point);
let inner = CommitmentTransaction::new_with_auxiliary_htlc_data(0, 0, 0, counterparty_payment_script, dummy_key.clone(), dummy_key.clone(), keys, 0, htlcs, &channel_parameters);
let revokeable_spk = get_revokeable_spk(&keys.revocation_key, channel_parameters.contest_delay(), &keys.broadcaster_delayed_payment_key).to_p2wsh();
let inner = CommitmentTransaction::new_with_auxiliary_htlc_data(0, 0, revokeable_spk, 0, counterparty_payment_script, dummy_key.clone(), dummy_key.clone(), keys, 0, htlcs, &channel_parameters);
htlcs.sort_by_key(|htlc| htlc.0.transaction_output_index);
HolderCommitmentTransaction {
inner,
Expand Down Expand Up @@ -1403,6 +1404,7 @@ pub struct CommitmentTransaction {
// For access to the pre-built transaction, see doc for trust()
built: BuiltCommitmentTransaction,
counterparty_payment_script: ScriptBuf,
revokeable_spk: ScriptBuf,
}

impl Eq for CommitmentTransaction {}
Expand Down Expand Up @@ -1438,6 +1440,7 @@ impl Writeable for CommitmentTransaction {
(14, legacy_deserialization_prevention_marker, option),
(15, self.channel_type_features, required),
(16, Some(self.counterparty_payment_script.clone()), option),
(19, Some(self.revokeable_spk.clone()), option),
});
Ok(())
}
Expand All @@ -1457,6 +1460,7 @@ impl Readable for CommitmentTransaction {
(14, _legacy_deserialization_prevention_marker, (option, explicit_type: ())),
(15, channel_type_features, option),
(16, counterparty_payment_script, option),
(19, revokeable_spk, option),
});

let mut additional_features = ChannelTypeFeatures::empty();
Expand All @@ -1474,6 +1478,7 @@ impl Readable for CommitmentTransaction {
htlcs,
channel_type_features: channel_type_features.unwrap_or(ChannelTypeFeatures::only_static_remote_key()),
counterparty_payment_script: counterparty_payment_script.unwrap_or(ScriptBuf::new()),
revokeable_spk: revokeable_spk.unwrap_or(ScriptBuf::new()),
})
}
}
Expand All @@ -1489,12 +1494,12 @@ impl CommitmentTransaction {
/// Only include HTLCs that are above the dust limit for the channel.
///
/// This is not exported to bindings users due to the generic though we likely should expose a version without
pub fn new_with_auxiliary_htlc_data<T>(commitment_number: u64, to_broadcaster_value_sat: u64, to_countersignatory_value_sat: u64, counterparty_payment_script: ScriptBuf, 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 {
pub fn new_with_auxiliary_htlc_data<T>(commitment_number: u64, to_broadcaster_value_sat: u64, revokeable_spk: ScriptBuf, to_countersignatory_value_sat: u64, counterparty_payment_script: ScriptBuf, 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 {
let to_broadcaster_value_sat = Amount::from_sat(to_broadcaster_value_sat);
let to_countersignatory_value_sat = Amount::from_sat(to_countersignatory_value_sat);

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

let (obscured_commitment_transaction_number, txins) = Self::internal_build_inputs(commitment_number, channel_parameters);
let transaction = Self::make_transaction(obscured_commitment_transaction_number, txins, outputs);
Expand All @@ -1513,6 +1518,7 @@ impl CommitmentTransaction {
txid
},
counterparty_payment_script,
revokeable_spk,
}
}

Expand All @@ -1528,7 +1534,7 @@ impl CommitmentTransaction {
let (obscured_commitment_transaction_number, txins) = Self::internal_build_inputs(self.commitment_number, channel_parameters);

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

let transaction = Self::make_transaction(obscured_commitment_transaction_number, txins, outputs);
let txid = transaction.compute_txid();
Expand All @@ -1552,9 +1558,7 @@ impl CommitmentTransaction {
// - initial sorting of outputs / HTLCs in the constructor, in which case T is auxiliary data the
// caller needs to have sorted together with the HTLCs so it can keep track of the output index
// - building of a bitcoin transaction during a verify() call, in which case T is just ()
fn internal_build_outputs<T>(keys: &TxCreationKeys, to_broadcaster_value_sat: Amount, to_countersignatory_value_sat: Amount, counterparty_payment_script: ScriptBuf, htlcs_with_aux: &mut Vec<(HTLCOutputInCommitment, T)>, channel_parameters: &DirectedChannelTransactionParameters, broadcaster_funding_key: &PublicKey, countersignatory_funding_key: &PublicKey) -> Result<(Vec<TxOut>, Vec<HTLCOutputInCommitment>), ()> {
let contest_delay = channel_parameters.contest_delay();

fn internal_build_outputs<T>(keys: &TxCreationKeys, to_broadcaster_value_sat: Amount, revokeable_spk: ScriptBuf, to_countersignatory_value_sat: Amount, counterparty_payment_script: ScriptBuf, htlcs_with_aux: &mut Vec<(HTLCOutputInCommitment, T)>, channel_parameters: &DirectedChannelTransactionParameters, broadcaster_funding_key: &PublicKey, countersignatory_funding_key: &PublicKey) -> Result<(Vec<TxOut>, Vec<HTLCOutputInCommitment>), ()> {
let mut txouts: Vec<(TxOut, Option<&mut HTLCOutputInCommitment>)> = Vec::new();

if to_countersignatory_value_sat > Amount::ZERO {
Expand All @@ -1568,14 +1572,9 @@ impl CommitmentTransaction {
}

if to_broadcaster_value_sat > Amount::ZERO {
let script_pubkey = signer.get_revokeable_spk(
&keys.revocation_key,
contest_delay,
&keys.broadcaster_delayed_payment_key,
);
txouts.push((
TxOut {
script_pubkey,
script_pubkey: revokeable_spk,
value: to_broadcaster_value_sat,
},
None,
Expand Down Expand Up @@ -1934,7 +1933,7 @@ pub fn get_commitment_transaction_number_obscure_factor(
mod tests {
use super::{CounterpartyCommitmentSecrets, ChannelPublicKeys};
use crate::chain;
use crate::ln::chan_utils::{get_counterparty_payment_script, get_htlc_redeemscript, get_to_countersignatory_with_anchors_redeemscript, CommitmentTransaction, TxCreationKeys, ChannelTransactionParameters, CounterpartyChannelTransactionParameters, HTLCOutputInCommitment};
use crate::ln::chan_utils::{get_revokeable_spk, get_counterparty_payment_script, get_htlc_redeemscript, get_to_countersignatory_with_anchors_redeemscript, CommitmentTransaction, TxCreationKeys, ChannelTransactionParameters, CounterpartyChannelTransactionParameters, HTLCOutputInCommitment};
use bitcoin::secp256k1::{PublicKey, SecretKey, Secp256k1};
use crate::util::test_utils;
use crate::sign::{ChannelSigner, SignerProvider};
Expand Down Expand Up @@ -1999,8 +1998,11 @@ mod tests {
fn build(&mut self, to_broadcaster_sats: u64, to_countersignatory_sats: u64) -> CommitmentTransaction {
let channel_parameters = self.channel_parameters.as_holder_broadcastable();
let counterparty_payment_script = get_counterparty_payment_script(&channel_parameters.channel_type_features(), &channel_parameters.countersignatory_pubkeys().payment_point);
let revokeable_spk = get_revokeable_spk(&self.keys.revocation_key, channel_parameters.contest_delay(), &self.keys.broadcaster_delayed_payment_key).to_p2wsh();
CommitmentTransaction::new_with_auxiliary_htlc_data(
self.commitment_number, to_broadcaster_sats, to_countersignatory_sats,
self.commitment_number, to_broadcaster_sats,
revokeable_spk,
to_countersignatory_sats,
counterparty_payment_script,
self.holder_funding_pubkey.clone(),
self.counterparty_funding_pubkey.clone(),
Expand Down
2 changes: 2 additions & 0 deletions lightning/src/ln/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3166,8 +3166,10 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
if local { self.channel_transaction_parameters.as_holder_broadcastable() }
else { self.channel_transaction_parameters.as_counterparty_broadcastable() };
let counterparty_payment_script = self.holder_signer.as_ref().get_counterparty_payment_script(channel_parameters.channel_type_features(), &channel_parameters.countersignatory_pubkeys().payment_point);
let revokeable_spk = self.holder_signer.as_ref().get_revokeable_spk(&keys.revocation_key, channel_parameters.contest_delay(), &keys.broadcaster_delayed_payment_key);
let tx = CommitmentTransaction::new_with_auxiliary_htlc_data(commitment_number,
value_to_a as u64,
revokeable_spk,
value_to_b as u64,
counterparty_payment_script,
funding_pubkey_a,
Expand Down
4 changes: 4 additions & 0 deletions lightning/src/ln/functional_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -768,10 +768,12 @@ fn test_update_fee_that_funder_cannot_afford() {
let local_chan_signer = local_chan.get_signer();
let channel_parameters = local_chan.context.channel_transaction_parameters.as_counterparty_broadcastable();
let counterparty_payment_script = local_chan_signer.as_ref().get_counterparty_payment_script(&channel_parameters.channel_type_features(), &channel_parameters.countersignatory_pubkeys().payment_point);
let revokeable_spk = local_chan_signer.as_ref().get_revokeable_spk(&commit_tx_keys.revocation_key, channel_parameters.contest_delay(), &commit_tx_keys.broadcaster_delayed_payment_key).to_p2wsh();
let mut htlcs: Vec<(HTLCOutputInCommitment, ())> = vec![];
let commitment_tx = CommitmentTransaction::new_with_auxiliary_htlc_data(
INITIAL_COMMITMENT_NUMBER - 1,
push_sats,
revokeable_spk,
channel_value - push_sats - commit_tx_fee_msat(non_buffer_feerate + 4, 0, &channel_type_features) / 1000,
counterparty_payment_script,
local_funding, remote_funding,
Expand Down Expand Up @@ -1522,9 +1524,11 @@ fn test_fee_spike_violation_fails_htlc() {
let local_chan_signer = local_chan.get_signer();
let channel_parameters = local_chan.context.channel_transaction_parameters.as_counterparty_broadcastable();
let counterparty_payment_script = local_chan_signer.as_ref().get_counterparty_payment_script(&channel_parameters.channel_type_features(), &channel_parameters.countersignatory_pubkeys().payment_point);
let revokeable_spk = local_chan_signer.as_ref().get_revokeable_spk(&commit_tx_keys.revocation_key, channel_parameters.contest_delay(), &commit_tx_keys.broadcaster_delayed_payment_key).to_p2wsh();
let commitment_tx = CommitmentTransaction::new_with_auxiliary_htlc_data(
commitment_number,
95000,
revokeable_spk,
local_chan_balance,
counterparty_payment_script,
local_funding, remote_funding,
Expand Down

0 comments on commit 17f3366

Please sign in to comment.