Skip to content

Introduce a ChannelType enum in lieu of opt_anchors #2321

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 23 additions & 22 deletions lightning/src/chain/channelmonitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1566,7 +1566,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
debug_assert!(htlc_input_idx_opt.is_some());
BitcoinOutPoint::new(*txid, htlc_input_idx_opt.unwrap_or(0))
} else {
debug_assert!(!self.onchain_tx_handler.opt_anchors());
debug_assert!(!self.onchain_tx_handler.channel_type().supports_anchors());
BitcoinOutPoint::new(*txid, 0)
}
} else {
Expand Down Expand Up @@ -2425,10 +2425,10 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
// If the channel supports anchor outputs, we'll need to emit an external
// event to be consumed such that a child transaction is broadcast with a
// high enough feerate for the parent commitment transaction to confirm.
if self.onchain_tx_handler.opt_anchors() {
if self.onchain_tx_handler.channel_type().supports_anchors() {
let funding_output = HolderFundingOutput::build(
self.funding_redeemscript.clone(), self.channel_value_satoshis,
self.onchain_tx_handler.opt_anchors(),
self.onchain_tx_handler.channel_type(),
);
let best_block_height = self.best_block.height();
let commitment_package = PackageTemplate::build_package(
Expand Down Expand Up @@ -2617,7 +2617,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
// First, process non-htlc outputs (to_holder & to_counterparty)
for (idx, outp) in tx.output.iter().enumerate() {
if outp.script_pubkey == revokeable_p2wsh {
let revk_outp = RevokedOutput::build(per_commitment_point, self.counterparty_commitment_params.counterparty_delayed_payment_base_key, self.counterparty_commitment_params.counterparty_htlc_base_key, per_commitment_key, outp.value, self.counterparty_commitment_params.on_counterparty_tx_csv, self.onchain_tx_handler.opt_anchors());
let revk_outp = RevokedOutput::build(per_commitment_point, self.counterparty_commitment_params.counterparty_delayed_payment_base_key, self.counterparty_commitment_params.counterparty_htlc_base_key, per_commitment_key, outp.value, self.counterparty_commitment_params.on_counterparty_tx_csv, self.onchain_tx_handler.channel_type().supports_anchors());
let justice_package = PackageTemplate::build_package(commitment_txid, idx as u32, PackageSolvingData::RevokedOutput(revk_outp), height + self.counterparty_commitment_params.on_counterparty_tx_csv as u32, height);
claimable_outpoints.push(justice_package);
to_counterparty_output_info =
Expand All @@ -2635,7 +2635,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
return (claimable_outpoints, (commitment_txid, watch_outputs),
to_counterparty_output_info);
}
let revk_htlc_outp = RevokedHTLCOutput::build(per_commitment_point, self.counterparty_commitment_params.counterparty_delayed_payment_base_key, self.counterparty_commitment_params.counterparty_htlc_base_key, per_commitment_key, htlc.amount_msat / 1000, htlc.clone(), self.onchain_tx_handler.channel_transaction_parameters.opt_anchors.is_some());
let revk_htlc_outp = RevokedHTLCOutput::build(per_commitment_point, self.counterparty_commitment_params.counterparty_delayed_payment_base_key, self.counterparty_commitment_params.counterparty_htlc_base_key, per_commitment_key, htlc.amount_msat / 1000, htlc.clone(), &self.onchain_tx_handler.channel_transaction_parameters.channel_type);
let justice_package = PackageTemplate::build_package(commitment_txid, transaction_output_index, PackageSolvingData::RevokedHTLCOutput(revk_htlc_outp), htlc.cltv_expiry, height);
claimable_outpoints.push(justice_package);
}
Expand Down Expand Up @@ -2753,13 +2753,13 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
CounterpartyOfferedHTLCOutput::build(*per_commitment_point,
self.counterparty_commitment_params.counterparty_delayed_payment_base_key,
self.counterparty_commitment_params.counterparty_htlc_base_key,
preimage.unwrap(), htlc.clone(), self.onchain_tx_handler.opt_anchors()))
preimage.unwrap(), htlc.clone(), self.onchain_tx_handler.channel_type()))
} else {
PackageSolvingData::CounterpartyReceivedHTLCOutput(
CounterpartyReceivedHTLCOutput::build(*per_commitment_point,
self.counterparty_commitment_params.counterparty_delayed_payment_base_key,
self.counterparty_commitment_params.counterparty_htlc_base_key,
htlc.clone(), self.onchain_tx_handler.opt_anchors()))
htlc.clone(), self.onchain_tx_handler.channel_type()))
};
let counterparty_package = PackageTemplate::build_package(commitment_txid, transaction_output_index, counterparty_htlc_outp, htlc.cltv_expiry, 0);
claimable_outpoints.push(counterparty_package);
Expand Down Expand Up @@ -2830,7 +2830,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
if let Some(transaction_output_index) = htlc.transaction_output_index {
let htlc_output = if htlc.offered {
let htlc_output = HolderHTLCOutput::build_offered(
htlc.amount_msat, htlc.cltv_expiry, self.onchain_tx_handler.opt_anchors()
htlc.amount_msat, htlc.cltv_expiry, self.onchain_tx_handler.channel_type()
);
htlc_output
} else {
Expand All @@ -2841,7 +2841,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
continue;
};
let htlc_output = HolderHTLCOutput::build_accepted(
payment_preimage, htlc.amount_msat, self.onchain_tx_handler.opt_anchors()
payment_preimage, htlc.amount_msat, self.onchain_tx_handler.channel_type()
);
htlc_output
};
Expand Down Expand Up @@ -2925,7 +2925,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
let mut holder_transactions = vec![commitment_tx];
// When anchor outputs are present, the HTLC transactions are only valid once the commitment
// transaction confirms.
if self.onchain_tx_handler.opt_anchors() {
if self.onchain_tx_handler.channel_type().supports_anchors() {
return holder_transactions;
}
for htlc in self.current_holder_commitment_tx.htlc_outputs.iter() {
Expand Down Expand Up @@ -2963,7 +2963,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
let mut holder_transactions = vec![commitment_tx];
// When anchor outputs are present, the HTLC transactions are only final once the commitment
// transaction confirms due to the CSV 1 encumberance.
if self.onchain_tx_handler.opt_anchors() {
if self.onchain_tx_handler.channel_type().supports_anchors() {
return holder_transactions;
}
for htlc in self.current_holder_commitment_tx.htlc_outputs.iter() {
Expand Down Expand Up @@ -3187,7 +3187,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {

let should_broadcast = self.should_broadcast_holder_commitment_txn(logger);
if should_broadcast {
let funding_outp = HolderFundingOutput::build(self.funding_redeemscript.clone(), self.channel_value_satoshis, self.onchain_tx_handler.opt_anchors());
let funding_outp = HolderFundingOutput::build(self.funding_redeemscript.clone(), self.channel_value_satoshis, self.onchain_tx_handler.channel_type());
let commitment_package = PackageTemplate::build_package(self.funding_info.0.txid.clone(), self.funding_info.0.index as u32, PackageSolvingData::HolderFundingOutput(funding_outp), self.best_block.height(), self.best_block.height());
claimable_outpoints.push(commitment_package);
self.pending_monitor_events.push(MonitorEvent::CommitmentTxConfirmed(self.funding_info.0));
Expand All @@ -3196,7 +3196,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
// We can't broadcast our HTLC transactions while the commitment transaction is
// unconfirmed. We'll delay doing so until we detect the confirmed commitment in
// `transactions_confirmed`.
if !self.onchain_tx_handler.opt_anchors() {
if !self.onchain_tx_handler.channel_type().supports_anchors() {
// Because we're broadcasting a commitment transaction, we should construct the package
// assuming it gets confirmed in the next block. Sadly, we have code which considers
// "not yet confirmed" things as discardable, so we cannot do that here.
Expand Down Expand Up @@ -4124,6 +4124,7 @@ mod tests {
use crate::sync::{Arc, Mutex};
use crate::io;
use bitcoin::{PackedLockTime, Sequence, Witness};
use crate::ln::channel::ChannelType;
use crate::prelude::*;

fn do_test_funding_spend_refuses_updates(use_local_txn: bool) {
Expand Down Expand Up @@ -4297,7 +4298,7 @@ mod tests {
selected_contest_delay: 67,
}),
funding_outpoint: Some(funding_outpoint),
opt_anchors: None,
channel_type: ChannelType::Legacy,
opt_non_zero_fee_anchors: None,
};
// Prune with one old state and a holder commitment tx holding a few overlaps with the
Expand Down Expand Up @@ -4414,7 +4415,7 @@ mod tests {
let txid = Txid::from_hex("56944c5d3f98413ef45cf54545538103cc9f298e0575820ad3591376e2e0f65d").unwrap();

// Justice tx with 1 to_holder, 2 revoked offered HTLCs, 1 revoked received HTLCs
for &opt_anchors in [false, true].iter() {
for channel_type in [ChannelType::Legacy, ChannelType::Anchors].iter() {
let mut claim_tx = Transaction { version: 0, lock_time: PackedLockTime::ZERO, input: Vec::new(), output: Vec::new() };
let mut sum_actual_sigs = 0;
for i in 0..4 {
Expand All @@ -4433,20 +4434,20 @@ mod tests {
value: 0,
});
let base_weight = claim_tx.weight();
let inputs_weight = vec![WEIGHT_REVOKED_OUTPUT, weight_revoked_offered_htlc(opt_anchors), weight_revoked_offered_htlc(opt_anchors), weight_revoked_received_htlc(opt_anchors)];
let inputs_weight = vec![WEIGHT_REVOKED_OUTPUT, weight_revoked_offered_htlc(&channel_type), weight_revoked_offered_htlc(&channel_type), weight_revoked_received_htlc(&channel_type)];
let mut inputs_total_weight = 2; // count segwit flags
{
let mut sighash_parts = sighash::SighashCache::new(&mut claim_tx);
for (idx, inp) in inputs_weight.iter().enumerate() {
sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs, opt_anchors);
sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs, &channel_type);
inputs_total_weight += inp;
}
}
assert_eq!(base_weight + inputs_total_weight as usize, claim_tx.weight() + /* max_length_sig */ (73 * inputs_weight.len() - sum_actual_sigs));
}

// Claim tx with 1 offered HTLCs, 3 received HTLCs
for &opt_anchors in [false, true].iter() {
for channel_type in [ChannelType::Legacy, ChannelType::Anchors].iter() {
let mut claim_tx = Transaction { version: 0, lock_time: PackedLockTime::ZERO, input: Vec::new(), output: Vec::new() };
let mut sum_actual_sigs = 0;
for i in 0..4 {
Expand All @@ -4465,20 +4466,20 @@ mod tests {
value: 0,
});
let base_weight = claim_tx.weight();
let inputs_weight = vec![weight_offered_htlc(opt_anchors), weight_received_htlc(opt_anchors), weight_received_htlc(opt_anchors), weight_received_htlc(opt_anchors)];
let inputs_weight = vec![weight_offered_htlc(&channel_type), weight_received_htlc(&channel_type), weight_received_htlc(&channel_type), weight_received_htlc(&channel_type)];
let mut inputs_total_weight = 2; // count segwit flags
{
let mut sighash_parts = sighash::SighashCache::new(&mut claim_tx);
for (idx, inp) in inputs_weight.iter().enumerate() {
sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs, opt_anchors);
sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs, &channel_type);
inputs_total_weight += inp;
}
}
assert_eq!(base_weight + inputs_total_weight as usize, claim_tx.weight() + /* max_length_sig */ (73 * inputs_weight.len() - sum_actual_sigs));
}

// Justice tx with 1 revoked HTLC-Success tx output
for &opt_anchors in [false, true].iter() {
for channel_type in [ChannelType::Legacy, ChannelType::Anchors].iter() {
let mut claim_tx = Transaction { version: 0, lock_time: PackedLockTime::ZERO, input: Vec::new(), output: Vec::new() };
let mut sum_actual_sigs = 0;
claim_tx.input.push(TxIn {
Expand All @@ -4500,7 +4501,7 @@ mod tests {
{
let mut sighash_parts = sighash::SighashCache::new(&mut claim_tx);
for (idx, inp) in inputs_weight.iter().enumerate() {
sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs, opt_anchors);
sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs, &channel_type);
inputs_total_weight += inp;
}
}
Expand Down
5 changes: 3 additions & 2 deletions lightning/src/chain/onchaintx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ use core::mem::replace;
#[cfg(anchors)]
use core::mem::swap;
use bitcoin::hashes::Hash;
use crate::ln::channel::ChannelType;

const MAX_ALLOC_SIZE: usize = 64*1024;

Expand Down Expand Up @@ -1209,8 +1210,8 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
.or_else(|| self.prev_holder_commitment.as_ref().map(|c| find_htlc(c)).flatten())
}

pub(crate) fn opt_anchors(&self) -> bool {
self.channel_transaction_parameters.opt_anchors.is_some()
pub(crate) fn channel_type(&self) -> ChannelType {
self.channel_transaction_parameters.channel_type.clone()
}

#[cfg(any(test,feature = "unsafe_revoked_tx_signing"))]
Expand Down
Loading