Skip to content

Commit

Permalink
Add InMemorySigner::overwrite_channel_parameters
Browse files Browse the repository at this point in the history
Builds of `CommitmentTransaction` now query a `ChannelSigner` for the
script pubkey to use in the `to_remote` output of the commitment
transaction. So we need to overwrite the `ChannelTransactionParameters`
of a `ChannelSigner` anytime we want to build a new commitment
transaction with a different set of features.

This is feature is only used in tests, so we cfg-gate it behind the
test flag.
  • Loading branch information
tankyleo committed Dec 11, 2024
1 parent 480236c commit 9633310
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 19 deletions.
30 changes: 18 additions & 12 deletions lightning/src/ln/chan_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1896,7 +1896,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_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 All @@ -1906,6 +1906,7 @@ mod tests {
use crate::types::payment::PaymentHash;
use bitcoin::PublicKey as BitcoinPublicKey;
use crate::types::features::ChannelTypeFeatures;
use crate::util::test_channel_signer::TestChannelSigner;

#[allow(unused_imports)]
use crate::prelude::*;
Expand All @@ -1919,6 +1920,7 @@ mod tests {
htlcs_with_aux: Vec<(HTLCOutputInCommitment, ())>,
channel_parameters: ChannelTransactionParameters,
counterparty_pubkeys: ChannelPublicKeys,
signer: TestChannelSigner,
}

impl TestCommitmentTxBuilder {
Expand All @@ -1927,15 +1929,13 @@ mod tests {
let seed = [42; 32];
let network = Network::Testnet;
let keys_provider = test_utils::TestKeysInterface::new(&seed, network);
let signer = keys_provider.derive_channel_signer(3000, keys_provider.generate_channel_keys_id(false, 1_000_000, 0));
let mut signer = keys_provider.derive_channel_signer(3000, keys_provider.generate_channel_keys_id(false, 1_000_000, 0));
let counterparty_signer = keys_provider.derive_channel_signer(3000, keys_provider.generate_channel_keys_id(true, 1_000_000, 1));
let delayed_payment_base = &signer.pubkeys().delayed_payment_basepoint;
let per_commitment_secret = SecretKey::from_slice(&<Vec<u8>>::from_hex("1f1e1d1c1b1a191817161514131211100f0e0d0c0b0a09080706050403020100").unwrap()[..]).unwrap();
let per_commitment_point = PublicKey::from_secret_key(&secp_ctx, &per_commitment_secret);
let htlc_basepoint = &signer.pubkeys().htlc_basepoint;
let holder_pubkeys = signer.pubkeys();
let holder_pubkeys = signer.pubkeys().clone();
let counterparty_pubkeys = counterparty_signer.pubkeys().clone();
let keys = TxCreationKeys::derive_new(&secp_ctx, &per_commitment_point, delayed_payment_base, htlc_basepoint, &counterparty_pubkeys.revocation_basepoint, &counterparty_pubkeys.htlc_basepoint);
let keys = TxCreationKeys::from_channel_static_keys(&per_commitment_point, &holder_pubkeys, &counterparty_pubkeys, &secp_ctx);
let channel_parameters = ChannelTransactionParameters {
holder_pubkeys: holder_pubkeys.clone(),
holder_selected_contest_delay: 0,
Expand All @@ -1944,6 +1944,7 @@ mod tests {
funding_outpoint: Some(chain::transaction::OutPoint { txid: Txid::all_zeros(), index: 0 }),
channel_type_features: ChannelTypeFeatures::only_static_remote_key(),
};
signer.provide_channel_parameters(&channel_parameters);
let htlcs_with_aux = Vec::new();

Self {
Expand All @@ -1955,12 +1956,12 @@ mod tests {
htlcs_with_aux,
channel_parameters,
counterparty_pubkeys,
signer,
}
}

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 counterparty_payment_script = self.signer.get_counterparty_payment_script(false);
let counterparty_txout = TxOut {
script_pubkey: counterparty_payment_script,
value: Amount::from_sat(to_countersignatory_sats),
Expand All @@ -1972,9 +1973,14 @@ mod tests {
self.holder_funding_pubkey.clone(),
self.counterparty_funding_pubkey.clone(),
self.keys.clone(), self.feerate_per_kw,
&mut self.htlcs_with_aux, &channel_parameters,
&mut self.htlcs_with_aux, &self.channel_parameters.as_holder_broadcastable(),
)
}

fn overwrite_channel_features(&mut self, channel_type_features: ChannelTypeFeatures) {
self.channel_parameters.channel_type_features = channel_type_features;
self.signer.overwrite_channel_parameters(&self.channel_parameters);
}
}

#[test]
Expand All @@ -1987,7 +1993,7 @@ mod tests {
assert_eq!(tx.built.transaction.output[1].script_pubkey, bitcoin::address::Address::p2wpkh(&CompressedPublicKey(builder.counterparty_pubkeys.payment_point), Network::Testnet).script_pubkey());

// Generate broadcaster and counterparty outputs as well as two anchors
builder.channel_parameters.channel_type_features = ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies();
builder.overwrite_channel_features(ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies());
let tx = builder.build(1000, 2000);
assert_eq!(tx.built.transaction.output.len(), 4);
assert_eq!(tx.built.transaction.output[3].script_pubkey, get_to_countersignatory_with_anchors_redeemscript(&builder.counterparty_pubkeys.payment_point).to_p2wsh());
Expand Down Expand Up @@ -2017,7 +2023,7 @@ mod tests {
};

// Generate broadcaster output and received and offered HTLC outputs, w/o anchors
builder.channel_parameters.channel_type_features = ChannelTypeFeatures::only_static_remote_key();
builder.overwrite_channel_features(ChannelTypeFeatures::only_static_remote_key());
builder.htlcs_with_aux = vec![(received_htlc.clone(), ()), (offered_htlc.clone(), ())];
let tx = builder.build(3000, 0);
let keys = &builder.keys.clone();
Expand All @@ -2030,7 +2036,7 @@ mod tests {
"0020215d61bba56b19e9eadb6107f5a85d7f99c40f65992443f69229c290165bc00d");

// Generate broadcaster output and received and offered HTLC outputs, with anchors
builder.channel_parameters.channel_type_features = ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies();
builder.overwrite_channel_features(ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies());
builder.htlcs_with_aux = vec![(received_htlc.clone(), ()), (offered_htlc.clone(), ())];
let tx = builder.build(3000, 0);
assert_eq!(tx.built.transaction.output.len(), 5);
Expand Down
11 changes: 4 additions & 7 deletions lightning/src/ln/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10843,7 +10843,6 @@ mod tests {
use crate::ln::channel::{HTLCOutputInCommitment ,TxCreationKeys};
use crate::ln::channel_keys::{DelayedPaymentBasepoint, HtlcBasepoint};
use crate::ln::chan_utils::{ChannelPublicKeys, HolderCommitmentTransaction, CounterpartyChannelTransactionParameters};
use crate::sign::type_resolver::ChannelSignerType;
use crate::util::logger::Logger;
use crate::sync::Arc;
use core::str::FromStr;
Expand Down Expand Up @@ -10917,19 +10916,17 @@ mod tests {
macro_rules! test_commitment {
( $counterparty_sig_hex: expr, $sig_hex: expr, $tx_hex: expr, $($remain:tt)* ) => {
chan.context.channel_transaction_parameters.channel_type_features = ChannelTypeFeatures::only_static_remote_key();
let mut holder_signer = keys_provider.derive_channel_signer(chan.context.channel_value_satoshis, chan.context.channel_keys_id);
holder_signer.provide_channel_parameters(&chan.context.channel_transaction_parameters);
chan.context.holder_signer = ChannelSignerType::Ecdsa(holder_signer);
let mut_signer = chan.context.holder_signer.as_mut_ecdsa().unwrap();
mut_signer.overwrite_channel_parameters(&chan.context.channel_transaction_parameters);
test_commitment_common!($counterparty_sig_hex, $sig_hex, $tx_hex, &ChannelTypeFeatures::only_static_remote_key(), $($remain)*);
};
}

macro_rules! test_commitment_with_anchors {
( $counterparty_sig_hex: expr, $sig_hex: expr, $tx_hex: expr, $($remain:tt)* ) => {
chan.context.channel_transaction_parameters.channel_type_features = ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies();
let mut holder_signer = keys_provider.derive_channel_signer(chan.context.channel_value_satoshis, chan.context.channel_keys_id);
holder_signer.provide_channel_parameters(&chan.context.channel_transaction_parameters);
chan.context.holder_signer = ChannelSignerType::Ecdsa(holder_signer);
let mut_signer = chan.context.holder_signer.as_mut_ecdsa().unwrap();
mut_signer.overwrite_channel_parameters(&chan.context.channel_transaction_parameters);
test_commitment_common!($counterparty_sig_hex, $sig_hex, $tx_hex, &ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies(), $($remain)*);
};
}
Expand Down
8 changes: 8 additions & 0 deletions lightning/src/sign/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1334,6 +1334,14 @@ impl InMemorySigner {
witness_script.as_bytes(),
]))
}

#[cfg(test)]
pub(crate) fn overwrite_channel_parameters(
&mut self, channel_parameters: &ChannelTransactionParameters,
) {
assert!(channel_parameters.is_populated(), "Channel parameters must be fully populated");
self.channel_parameters = Some(channel_parameters.clone());
}
}

impl EntropySource for InMemorySigner {
Expand Down
5 changes: 5 additions & 0 deletions lightning/src/util/test_channel_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ impl TestChannelSigner {
fn is_signer_available(&self, signer_op: SignerOp) -> bool {
!self.get_enforcement_state().disabled_signer_ops.contains(&signer_op)
}

#[cfg(test)]
pub(crate) fn overwrite_channel_parameters(&mut self, channel_parameters: &ChannelTransactionParameters) {
self.inner.overwrite_channel_parameters(channel_parameters)
}
}

impl ChannelSigner for TestChannelSigner {
Expand Down

0 comments on commit 9633310

Please sign in to comment.