Skip to content

Commit 3973865

Browse files
authored
Merge pull request #2361 from arik-so/2023-06-anchor-channel-type-features
Replace `opt_anchors` with `ChannelTypeFeatures`
2 parents 773e8ad + 82b5359 commit 3973865

12 files changed

+685
-382
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1600,7 +1600,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
16001600
debug_assert!(htlc_input_idx_opt.is_some());
16011601
BitcoinOutPoint::new(*txid, htlc_input_idx_opt.unwrap_or(0))
16021602
} else {
1603-
debug_assert!(!self.onchain_tx_handler.opt_anchors());
1603+
debug_assert!(!self.onchain_tx_handler.channel_type_features().supports_anchors_zero_fee_htlc_tx());
16041604
BitcoinOutPoint::new(*txid, 0)
16051605
}
16061606
} else {
@@ -2459,10 +2459,10 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
24592459
// If the channel supports anchor outputs, we'll need to emit an external
24602460
// event to be consumed such that a child transaction is broadcast with a
24612461
// high enough feerate for the parent commitment transaction to confirm.
2462-
if self.onchain_tx_handler.opt_anchors() {
2462+
if self.onchain_tx_handler.channel_type_features().supports_anchors_zero_fee_htlc_tx() {
24632463
let funding_output = HolderFundingOutput::build(
24642464
self.funding_redeemscript.clone(), self.channel_value_satoshis,
2465-
self.onchain_tx_handler.opt_anchors(),
2465+
self.onchain_tx_handler.channel_type_features().clone(),
24662466
);
24672467
let best_block_height = self.best_block.height();
24682468
let commitment_package = PackageTemplate::build_package(
@@ -2653,7 +2653,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
26532653
// First, process non-htlc outputs (to_holder & to_counterparty)
26542654
for (idx, outp) in tx.output.iter().enumerate() {
26552655
if outp.script_pubkey == revokeable_p2wsh {
2656-
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());
2656+
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_features().supports_anchors_zero_fee_htlc_tx());
26572657
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);
26582658
claimable_outpoints.push(justice_package);
26592659
to_counterparty_output_info =
@@ -2671,7 +2671,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
26712671
return (claimable_outpoints, (commitment_txid, watch_outputs),
26722672
to_counterparty_output_info);
26732673
}
2674-
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());
2674+
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_features);
26752675
let justice_package = PackageTemplate::build_package(commitment_txid, transaction_output_index, PackageSolvingData::RevokedHTLCOutput(revk_htlc_outp), htlc.cltv_expiry, height);
26762676
claimable_outpoints.push(justice_package);
26772677
}
@@ -2789,13 +2789,13 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
27892789
CounterpartyOfferedHTLCOutput::build(*per_commitment_point,
27902790
self.counterparty_commitment_params.counterparty_delayed_payment_base_key,
27912791
self.counterparty_commitment_params.counterparty_htlc_base_key,
2792-
preimage.unwrap(), htlc.clone(), self.onchain_tx_handler.opt_anchors()))
2792+
preimage.unwrap(), htlc.clone(), self.onchain_tx_handler.channel_type_features().clone()))
27932793
} else {
27942794
PackageSolvingData::CounterpartyReceivedHTLCOutput(
27952795
CounterpartyReceivedHTLCOutput::build(*per_commitment_point,
27962796
self.counterparty_commitment_params.counterparty_delayed_payment_base_key,
27972797
self.counterparty_commitment_params.counterparty_htlc_base_key,
2798-
htlc.clone(), self.onchain_tx_handler.opt_anchors()))
2798+
htlc.clone(), self.onchain_tx_handler.channel_type_features().clone()))
27992799
};
28002800
let counterparty_package = PackageTemplate::build_package(commitment_txid, transaction_output_index, counterparty_htlc_outp, htlc.cltv_expiry, 0);
28012801
claimable_outpoints.push(counterparty_package);
@@ -2866,7 +2866,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
28662866
if let Some(transaction_output_index) = htlc.transaction_output_index {
28672867
let htlc_output = if htlc.offered {
28682868
let htlc_output = HolderHTLCOutput::build_offered(
2869-
htlc.amount_msat, htlc.cltv_expiry, self.onchain_tx_handler.opt_anchors()
2869+
htlc.amount_msat, htlc.cltv_expiry, self.onchain_tx_handler.channel_type_features().clone()
28702870
);
28712871
htlc_output
28722872
} else {
@@ -2877,7 +2877,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
28772877
continue;
28782878
};
28792879
let htlc_output = HolderHTLCOutput::build_accepted(
2880-
payment_preimage, htlc.amount_msat, self.onchain_tx_handler.opt_anchors()
2880+
payment_preimage, htlc.amount_msat, self.onchain_tx_handler.channel_type_features().clone()
28812881
);
28822882
htlc_output
28832883
};
@@ -2961,7 +2961,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
29612961
let mut holder_transactions = vec![commitment_tx];
29622962
// When anchor outputs are present, the HTLC transactions are only valid once the commitment
29632963
// transaction confirms.
2964-
if self.onchain_tx_handler.opt_anchors() {
2964+
if self.onchain_tx_handler.channel_type_features().supports_anchors_zero_fee_htlc_tx() {
29652965
return holder_transactions;
29662966
}
29672967
for htlc in self.current_holder_commitment_tx.htlc_outputs.iter() {
@@ -2999,7 +2999,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
29992999
let mut holder_transactions = vec![commitment_tx];
30003000
// When anchor outputs are present, the HTLC transactions are only final once the commitment
30013001
// transaction confirms due to the CSV 1 encumberance.
3002-
if self.onchain_tx_handler.opt_anchors() {
3002+
if self.onchain_tx_handler.channel_type_features().supports_anchors_zero_fee_htlc_tx() {
30033003
return holder_transactions;
30043004
}
30053005
for htlc in self.current_holder_commitment_tx.htlc_outputs.iter() {
@@ -3223,7 +3223,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
32233223

32243224
let should_broadcast = self.should_broadcast_holder_commitment_txn(logger);
32253225
if should_broadcast {
3226-
let funding_outp = HolderFundingOutput::build(self.funding_redeemscript.clone(), self.channel_value_satoshis, self.onchain_tx_handler.opt_anchors());
3226+
let funding_outp = HolderFundingOutput::build(self.funding_redeemscript.clone(), self.channel_value_satoshis, self.onchain_tx_handler.channel_type_features().clone());
32273227
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());
32283228
claimable_outpoints.push(commitment_package);
32293229
self.pending_monitor_events.push(MonitorEvent::CommitmentTxConfirmed(self.funding_info.0));
@@ -3232,7 +3232,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
32323232
// We can't broadcast our HTLC transactions while the commitment transaction is
32333233
// unconfirmed. We'll delay doing so until we detect the confirmed commitment in
32343234
// `transactions_confirmed`.
3235-
if !self.onchain_tx_handler.opt_anchors() {
3235+
if !self.onchain_tx_handler.channel_type_features().supports_anchors_zero_fee_htlc_tx() {
32363236
// Because we're broadcasting a commitment transaction, we should construct the package
32373237
// assuming it gets confirmed in the next block. Sadly, we have code which considers
32383238
// "not yet confirmed" things as discardable, so we cannot do that here.
@@ -4160,6 +4160,7 @@ mod tests {
41604160
use crate::sync::{Arc, Mutex};
41614161
use crate::io;
41624162
use bitcoin::{PackedLockTime, Sequence, Witness};
4163+
use crate::ln::features::ChannelTypeFeatures;
41634164
use crate::prelude::*;
41644165

41654166
fn do_test_funding_spend_refuses_updates(use_local_txn: bool) {
@@ -4333,8 +4334,7 @@ mod tests {
43334334
selected_contest_delay: 67,
43344335
}),
43354336
funding_outpoint: Some(funding_outpoint),
4336-
opt_anchors: None,
4337-
opt_non_zero_fee_anchors: None,
4337+
channel_type_features: ChannelTypeFeatures::only_static_remote_key()
43384338
};
43394339
// Prune with one old state and a holder commitment tx holding a few overlaps with the
43404340
// old state.
@@ -4450,7 +4450,7 @@ mod tests {
44504450
let txid = Txid::from_hex("56944c5d3f98413ef45cf54545538103cc9f298e0575820ad3591376e2e0f65d").unwrap();
44514451

44524452
// Justice tx with 1 to_holder, 2 revoked offered HTLCs, 1 revoked received HTLCs
4453-
for &opt_anchors in [false, true].iter() {
4453+
for channel_type_features in [ChannelTypeFeatures::only_static_remote_key(), ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies()].iter() {
44544454
let mut claim_tx = Transaction { version: 0, lock_time: PackedLockTime::ZERO, input: Vec::new(), output: Vec::new() };
44554455
let mut sum_actual_sigs = 0;
44564456
for i in 0..4 {
@@ -4469,20 +4469,20 @@ mod tests {
44694469
value: 0,
44704470
});
44714471
let base_weight = claim_tx.weight();
4472-
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)];
4472+
let inputs_weight = vec![WEIGHT_REVOKED_OUTPUT, weight_revoked_offered_htlc(channel_type_features), weight_revoked_offered_htlc(channel_type_features), weight_revoked_received_htlc(channel_type_features)];
44734473
let mut inputs_total_weight = 2; // count segwit flags
44744474
{
44754475
let mut sighash_parts = sighash::SighashCache::new(&mut claim_tx);
44764476
for (idx, inp) in inputs_weight.iter().enumerate() {
4477-
sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs, opt_anchors);
4477+
sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs, channel_type_features);
44784478
inputs_total_weight += inp;
44794479
}
44804480
}
44814481
assert_eq!(base_weight + inputs_total_weight as usize, claim_tx.weight() + /* max_length_sig */ (73 * inputs_weight.len() - sum_actual_sigs));
44824482
}
44834483

44844484
// Claim tx with 1 offered HTLCs, 3 received HTLCs
4485-
for &opt_anchors in [false, true].iter() {
4485+
for channel_type_features in [ChannelTypeFeatures::only_static_remote_key(), ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies()].iter() {
44864486
let mut claim_tx = Transaction { version: 0, lock_time: PackedLockTime::ZERO, input: Vec::new(), output: Vec::new() };
44874487
let mut sum_actual_sigs = 0;
44884488
for i in 0..4 {
@@ -4501,20 +4501,20 @@ mod tests {
45014501
value: 0,
45024502
});
45034503
let base_weight = claim_tx.weight();
4504-
let inputs_weight = vec![weight_offered_htlc(opt_anchors), weight_received_htlc(opt_anchors), weight_received_htlc(opt_anchors), weight_received_htlc(opt_anchors)];
4504+
let inputs_weight = vec![weight_offered_htlc(channel_type_features), weight_received_htlc(channel_type_features), weight_received_htlc(channel_type_features), weight_received_htlc(channel_type_features)];
45054505
let mut inputs_total_weight = 2; // count segwit flags
45064506
{
45074507
let mut sighash_parts = sighash::SighashCache::new(&mut claim_tx);
45084508
for (idx, inp) in inputs_weight.iter().enumerate() {
4509-
sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs, opt_anchors);
4509+
sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs, channel_type_features);
45104510
inputs_total_weight += inp;
45114511
}
45124512
}
45134513
assert_eq!(base_weight + inputs_total_weight as usize, claim_tx.weight() + /* max_length_sig */ (73 * inputs_weight.len() - sum_actual_sigs));
45144514
}
45154515

45164516
// Justice tx with 1 revoked HTLC-Success tx output
4517-
for &opt_anchors in [false, true].iter() {
4517+
for channel_type_features in [ChannelTypeFeatures::only_static_remote_key(), ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies()].iter() {
45184518
let mut claim_tx = Transaction { version: 0, lock_time: PackedLockTime::ZERO, input: Vec::new(), output: Vec::new() };
45194519
let mut sum_actual_sigs = 0;
45204520
claim_tx.input.push(TxIn {
@@ -4536,7 +4536,7 @@ mod tests {
45364536
{
45374537
let mut sighash_parts = sighash::SighashCache::new(&mut claim_tx);
45384538
for (idx, inp) in inputs_weight.iter().enumerate() {
4539-
sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs, opt_anchors);
4539+
sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs, channel_type_features);
45404540
inputs_total_weight += inp;
45414541
}
45424542
}

lightning/src/chain/onchaintx.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ use core::ops::Deref;
5252
use core::mem::replace;
5353
#[cfg(anchors)]
5454
use core::mem::swap;
55+
use crate::ln::features::ChannelTypeFeatures;
5556

5657
const MAX_ALLOC_SIZE: usize = 64*1024;
5758

@@ -1215,8 +1216,8 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
12151216
.or_else(|| self.prev_holder_commitment.as_ref().map(|c| find_htlc(c)).flatten())
12161217
}
12171218

1218-
pub(crate) fn opt_anchors(&self) -> bool {
1219-
self.channel_transaction_parameters.opt_anchors.is_some()
1219+
pub(crate) fn channel_type_features(&self) -> &ChannelTypeFeatures {
1220+
&self.channel_transaction_parameters.channel_type_features
12201221
}
12211222

12221223
#[cfg(any(test,feature = "unsafe_revoked_tx_signing"))]

0 commit comments

Comments
 (0)