Skip to content

Commit 36c2d54

Browse files
committed
Introduce a ChannelType enum in lieu of opt_anchors
1 parent 4dce209 commit 36c2d54

File tree

6 files changed

+113
-49
lines changed

6 files changed

+113
-49
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1566,7 +1566,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
15661566
debug_assert!(htlc_input_idx_opt.is_some());
15671567
BitcoinOutPoint::new(*txid, htlc_input_idx_opt.unwrap_or(0))
15681568
} else {
1569-
debug_assert!(!self.onchain_tx_handler.opt_anchors());
1569+
debug_assert!(!self.onchain_tx_handler.channel_type().supports_anchors());
15701570
BitcoinOutPoint::new(*txid, 0)
15711571
}
15721572
} else {
@@ -2425,10 +2425,10 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
24252425
// If the channel supports anchor outputs, we'll need to emit an external
24262426
// event to be consumed such that a child transaction is broadcast with a
24272427
// high enough feerate for the parent commitment transaction to confirm.
2428-
if self.onchain_tx_handler.opt_anchors() {
2428+
if self.onchain_tx_handler.channel_type().supports_anchors() {
24292429
let funding_output = HolderFundingOutput::build(
24302430
self.funding_redeemscript.clone(), self.channel_value_satoshis,
2431-
self.onchain_tx_handler.opt_anchors(),
2431+
self.onchain_tx_handler.channel_type().supports_anchors(),
24322432
);
24332433
let best_block_height = self.best_block.height();
24342434
let commitment_package = PackageTemplate::build_package(
@@ -2617,7 +2617,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
26172617
// First, process non-htlc outputs (to_holder & to_counterparty)
26182618
for (idx, outp) in tx.output.iter().enumerate() {
26192619
if outp.script_pubkey == revokeable_p2wsh {
2620-
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());
2620+
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());
26212621
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);
26222622
claimable_outpoints.push(justice_package);
26232623
to_counterparty_output_info =
@@ -2635,7 +2635,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
26352635
return (claimable_outpoints, (commitment_txid, watch_outputs),
26362636
to_counterparty_output_info);
26372637
}
2638-
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());
2638+
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);
26392639
let justice_package = PackageTemplate::build_package(commitment_txid, transaction_output_index, PackageSolvingData::RevokedHTLCOutput(revk_htlc_outp), htlc.cltv_expiry, height);
26402640
claimable_outpoints.push(justice_package);
26412641
}
@@ -2753,13 +2753,13 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
27532753
CounterpartyOfferedHTLCOutput::build(*per_commitment_point,
27542754
self.counterparty_commitment_params.counterparty_delayed_payment_base_key,
27552755
self.counterparty_commitment_params.counterparty_htlc_base_key,
2756-
preimage.unwrap(), htlc.clone(), self.onchain_tx_handler.opt_anchors()))
2756+
preimage.unwrap(), htlc.clone(), self.onchain_tx_handler.channel_type().supports_anchors()))
27572757
} else {
27582758
PackageSolvingData::CounterpartyReceivedHTLCOutput(
27592759
CounterpartyReceivedHTLCOutput::build(*per_commitment_point,
27602760
self.counterparty_commitment_params.counterparty_delayed_payment_base_key,
27612761
self.counterparty_commitment_params.counterparty_htlc_base_key,
2762-
htlc.clone(), self.onchain_tx_handler.opt_anchors()))
2762+
htlc.clone(), self.onchain_tx_handler.channel_type().supports_anchors()))
27632763
};
27642764
let counterparty_package = PackageTemplate::build_package(commitment_txid, transaction_output_index, counterparty_htlc_outp, htlc.cltv_expiry, 0);
27652765
claimable_outpoints.push(counterparty_package);
@@ -2830,7 +2830,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
28302830
if let Some(transaction_output_index) = htlc.transaction_output_index {
28312831
let htlc_output = if htlc.offered {
28322832
let htlc_output = HolderHTLCOutput::build_offered(
2833-
htlc.amount_msat, htlc.cltv_expiry, self.onchain_tx_handler.opt_anchors()
2833+
htlc.amount_msat, htlc.cltv_expiry, self.onchain_tx_handler.channel_type().supports_anchors()
28342834
);
28352835
htlc_output
28362836
} else {
@@ -2841,7 +2841,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
28412841
continue;
28422842
};
28432843
let htlc_output = HolderHTLCOutput::build_accepted(
2844-
payment_preimage, htlc.amount_msat, self.onchain_tx_handler.opt_anchors()
2844+
payment_preimage, htlc.amount_msat, self.onchain_tx_handler.channel_type().supports_anchors()
28452845
);
28462846
htlc_output
28472847
};
@@ -2925,7 +2925,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
29252925
let mut holder_transactions = vec![commitment_tx];
29262926
// When anchor outputs are present, the HTLC transactions are only valid once the commitment
29272927
// transaction confirms.
2928-
if self.onchain_tx_handler.opt_anchors() {
2928+
if self.onchain_tx_handler.channel_type().supports_anchors() {
29292929
return holder_transactions;
29302930
}
29312931
for htlc in self.current_holder_commitment_tx.htlc_outputs.iter() {
@@ -3187,7 +3187,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
31873187

31883188
let should_broadcast = self.should_broadcast_holder_commitment_txn(logger);
31893189
if should_broadcast {
3190-
let funding_outp = HolderFundingOutput::build(self.funding_redeemscript.clone(), self.channel_value_satoshis, self.onchain_tx_handler.opt_anchors());
3190+
let funding_outp = HolderFundingOutput::build(self.funding_redeemscript.clone(), self.channel_value_satoshis, self.onchain_tx_handler.channel_type().supports_anchors());
31913191
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());
31923192
claimable_outpoints.push(commitment_package);
31933193
self.pending_monitor_events.push(MonitorEvent::CommitmentTxConfirmed(self.funding_info.0));
@@ -3196,7 +3196,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
31963196
// We can't broadcast our HTLC transactions while the commitment transaction is
31973197
// unconfirmed. We'll delay doing so until we detect the confirmed commitment in
31983198
// `transactions_confirmed`.
3199-
if !self.onchain_tx_handler.opt_anchors() {
3199+
if !self.onchain_tx_handler.channel_type().supports_anchors() {
32003200
// Because we're broadcasting a commitment transaction, we should construct the package
32013201
// assuming it gets confirmed in the next block. Sadly, we have code which considers
32023202
// "not yet confirmed" things as discardable, so we cannot do that here.

lightning/src/chain/onchaintx.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ use core::mem::replace;
4949
#[cfg(anchors)]
5050
use core::mem::swap;
5151
use bitcoin::hashes::Hash;
52+
use crate::ln::channel::ChannelType;
5253

5354
const MAX_ALLOC_SIZE: usize = 64*1024;
5455

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

1212-
pub(crate) fn opt_anchors(&self) -> bool {
1213-
self.channel_transaction_parameters.opt_anchors.is_some()
1213+
pub(crate) fn channel_type(&self) -> ChannelType {
1214+
self.channel_transaction_parameters.channel_type.clone()
12141215
}
12151216

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

lightning/src/chain/package.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,25 @@ use core::convert::TryInto;
4040
use core::mem;
4141
use core::ops::Deref;
4242
use bitcoin::{PackedLockTime, Sequence, Witness};
43+
use crate::ln::channel::ChannelType;
4344

4445
use super::chaininterface::LowerBoundedFeeEstimator;
4546

4647
const MAX_ALLOC_SIZE: usize = 64*1024;
4748

4849

49-
pub(crate) fn weight_revoked_offered_htlc(opt_anchors: bool) -> u64 {
50+
pub(crate) fn weight_revoked_offered_htlc(channel_type: &ChannelType) -> u64 {
5051
// number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script
5152
const WEIGHT_REVOKED_OFFERED_HTLC: u64 = 1 + 1 + 73 + 1 + 33 + 1 + 133;
5253
const WEIGHT_REVOKED_OFFERED_HTLC_ANCHORS: u64 = WEIGHT_REVOKED_OFFERED_HTLC + 3; // + OP_1 + OP_CSV + OP_DROP
53-
if opt_anchors { WEIGHT_REVOKED_OFFERED_HTLC_ANCHORS } else { WEIGHT_REVOKED_OFFERED_HTLC }
54+
if channel_type.supports_anchors() { WEIGHT_REVOKED_OFFERED_HTLC_ANCHORS } else { WEIGHT_REVOKED_OFFERED_HTLC }
5455
}
5556

56-
pub(crate) fn weight_revoked_received_htlc(opt_anchors: bool) -> u64 {
57+
pub(crate) fn weight_revoked_received_htlc(channel_type: &ChannelType) -> u64 {
5758
// number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script
5859
const WEIGHT_REVOKED_RECEIVED_HTLC: u64 = 1 + 1 + 73 + 1 + 33 + 1 + 139;
5960
const WEIGHT_REVOKED_RECEIVED_HTLC_ANCHORS: u64 = WEIGHT_REVOKED_RECEIVED_HTLC + 3; // + OP_1 + OP_CSV + OP_DROP
60-
if opt_anchors { WEIGHT_REVOKED_RECEIVED_HTLC_ANCHORS } else { WEIGHT_REVOKED_RECEIVED_HTLC }
61+
if channel_type.supports_anchors() { WEIGHT_REVOKED_RECEIVED_HTLC_ANCHORS } else { WEIGHT_REVOKED_RECEIVED_HTLC }
6162
}
6263

6364
pub(crate) fn weight_offered_htlc(opt_anchors: bool) -> u64 {
@@ -147,8 +148,8 @@ pub(crate) struct RevokedHTLCOutput {
147148
}
148149

149150
impl RevokedHTLCOutput {
150-
pub(crate) fn build(per_commitment_point: PublicKey, counterparty_delayed_payment_base_key: PublicKey, counterparty_htlc_base_key: PublicKey, per_commitment_key: SecretKey, amount: u64, htlc: HTLCOutputInCommitment, opt_anchors: bool) -> Self {
151-
let weight = if htlc.offered { weight_revoked_offered_htlc(opt_anchors) } else { weight_revoked_received_htlc(opt_anchors) };
151+
pub(crate) fn build(per_commitment_point: PublicKey, counterparty_delayed_payment_base_key: PublicKey, counterparty_htlc_base_key: PublicKey, per_commitment_key: SecretKey, amount: u64, htlc: HTLCOutputInCommitment, channel_type: &ChannelType) -> Self {
152+
let weight = if htlc.offered { weight_revoked_offered_htlc(channel_type) } else { weight_revoked_received_htlc(channel_type) };
152153
RevokedHTLCOutput {
153154
per_commitment_point,
154155
counterparty_delayed_payment_base_key,
@@ -411,7 +412,7 @@ impl PackageSolvingData {
411412
},
412413
PackageSolvingData::RevokedHTLCOutput(ref outp) => {
413414
let chan_keys = TxCreationKeys::derive_new(&onchain_handler.secp_ctx, &outp.per_commitment_point, &outp.counterparty_delayed_payment_base_key, &outp.counterparty_htlc_base_key, &onchain_handler.signer.pubkeys().revocation_basepoint, &onchain_handler.signer.pubkeys().htlc_basepoint);
414-
let witness_script = chan_utils::get_htlc_redeemscript_with_explicit_keys(&outp.htlc, onchain_handler.opt_anchors(), &chan_keys.broadcaster_htlc_key, &chan_keys.countersignatory_htlc_key, &chan_keys.revocation_key);
415+
let witness_script = chan_utils::get_htlc_redeemscript_with_explicit_keys(&outp.htlc, onchain_handler.channel_type().supports_anchors(), &chan_keys.broadcaster_htlc_key, &chan_keys.countersignatory_htlc_key, &chan_keys.revocation_key);
415416
//TODO: should we panic on signer failure ?
416417
if let Ok(sig) = onchain_handler.signer.sign_justice_revoked_htlc(&bumped_tx, i, outp.amount, &outp.per_commitment_key, &outp.htlc, &onchain_handler.secp_ctx) {
417418
let mut ser_sig = sig.serialize_der().to_vec();
@@ -423,7 +424,7 @@ impl PackageSolvingData {
423424
},
424425
PackageSolvingData::CounterpartyOfferedHTLCOutput(ref outp) => {
425426
let chan_keys = TxCreationKeys::derive_new(&onchain_handler.secp_ctx, &outp.per_commitment_point, &outp.counterparty_delayed_payment_base_key, &outp.counterparty_htlc_base_key, &onchain_handler.signer.pubkeys().revocation_basepoint, &onchain_handler.signer.pubkeys().htlc_basepoint);
426-
let witness_script = chan_utils::get_htlc_redeemscript_with_explicit_keys(&outp.htlc, onchain_handler.opt_anchors(), &chan_keys.broadcaster_htlc_key, &chan_keys.countersignatory_htlc_key, &chan_keys.revocation_key);
427+
let witness_script = chan_utils::get_htlc_redeemscript_with_explicit_keys(&outp.htlc, onchain_handler.channel_type().supports_anchors(), &chan_keys.broadcaster_htlc_key, &chan_keys.countersignatory_htlc_key, &chan_keys.revocation_key);
427428

428429
if let Ok(sig) = onchain_handler.signer.sign_counterparty_htlc_transaction(&bumped_tx, i, &outp.htlc.amount_msat / 1000, &outp.per_commitment_point, &outp.htlc, &onchain_handler.secp_ctx) {
429430
let mut ser_sig = sig.serialize_der().to_vec();
@@ -435,7 +436,7 @@ impl PackageSolvingData {
435436
},
436437
PackageSolvingData::CounterpartyReceivedHTLCOutput(ref outp) => {
437438
let chan_keys = TxCreationKeys::derive_new(&onchain_handler.secp_ctx, &outp.per_commitment_point, &outp.counterparty_delayed_payment_base_key, &outp.counterparty_htlc_base_key, &onchain_handler.signer.pubkeys().revocation_basepoint, &onchain_handler.signer.pubkeys().htlc_basepoint);
438-
let witness_script = chan_utils::get_htlc_redeemscript_with_explicit_keys(&outp.htlc, onchain_handler.opt_anchors(), &chan_keys.broadcaster_htlc_key, &chan_keys.countersignatory_htlc_key, &chan_keys.revocation_key);
439+
let witness_script = chan_utils::get_htlc_redeemscript_with_explicit_keys(&outp.htlc, onchain_handler.channel_type().supports_anchors(), &chan_keys.broadcaster_htlc_key, &chan_keys.countersignatory_htlc_key, &chan_keys.revocation_key);
439440

440441
if let Ok(sig) = onchain_handler.signer.sign_counterparty_htlc_transaction(&bumped_tx, i, &outp.htlc.amount_msat / 1000, &outp.per_commitment_point, &outp.htlc, &onchain_handler.secp_ctx) {
441442
let mut ser_sig = sig.serialize_der().to_vec();

lightning/src/ln/chan_utils.rs

Lines changed: 58 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use bitcoin::hash_types::{Txid, PubkeyHash};
2424
use crate::sign::EntropySource;
2525
use crate::ln::{PaymentHash, PaymentPreimage};
2626
use crate::ln::msgs::DecodeError;
27-
use crate::util::ser::{Readable, Writeable, Writer};
27+
use crate::util::ser::{Readable, RequiredWrapper, Writeable, Writer};
2828
use crate::util::transaction_utils;
2929

3030
use bitcoin::secp256k1::{SecretKey, PublicKey, Scalar};
@@ -37,8 +37,9 @@ use crate::prelude::*;
3737
use core::cmp;
3838
use crate::ln::chan_utils;
3939
use crate::util::transaction_utils::sort_outputs;
40-
use crate::ln::channel::{INITIAL_COMMITMENT_NUMBER, ANCHOR_OUTPUT_VALUE_SATOSHI};
40+
use crate::ln::channel::{INITIAL_COMMITMENT_NUMBER, ANCHOR_OUTPUT_VALUE_SATOSHI, ChannelType};
4141
use core::ops::Deref;
42+
use std::io::{Error, Read};
4243
use crate::chain;
4344
use crate::util::crypto::{sign, sign_with_aux_rand};
4445

@@ -826,9 +827,8 @@ pub struct ChannelTransactionParameters {
826827
pub counterparty_parameters: Option<CounterpartyChannelTransactionParameters>,
827828
/// The late-bound funding outpoint
828829
pub funding_outpoint: Option<chain::transaction::OutPoint>,
829-
/// Are anchors (zero fee HTLC transaction variant) used for this channel. Boolean is
830-
/// serialization backwards-compatible.
831-
pub opt_anchors: Option<()>,
830+
/// Describes the channel type
831+
pub channel_type: ChannelType,
832832
/// Are non-zero-fee anchors are enabled (used in conjuction with opt_anchors)
833833
/// It is intended merely for backwards compatibility with signers that need it.
834834
/// There is no support for this feature in LDK channel negotiation.
@@ -880,15 +880,55 @@ impl_writeable_tlv_based!(CounterpartyChannelTransactionParameters, {
880880
(2, selected_contest_delay, required),
881881
});
882882

883-
impl_writeable_tlv_based!(ChannelTransactionParameters, {
884-
(0, holder_pubkeys, required),
885-
(2, holder_selected_contest_delay, required),
886-
(4, is_outbound_from_holder, required),
887-
(6, counterparty_parameters, option),
888-
(8, funding_outpoint, option),
889-
(10, opt_anchors, option),
890-
(12, opt_non_zero_fee_anchors, option),
891-
});
883+
impl Writeable for ChannelTransactionParameters {
884+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), Error> {
885+
let mut channel_type = Some(self.channel_type.clone());
886+
match self.channel_type {
887+
ChannelType::Legacy => { channel_type = None }
888+
_ => {}
889+
};
890+
encode_tlv_stream!(w, {
891+
(0, self.holder_pubkeys, required),
892+
(2, self.holder_selected_contest_delay, required),
893+
(4, self.is_outbound_from_holder, required),
894+
(6, self.counterparty_parameters, option),
895+
(8, self.funding_outpoint, option),
896+
(10, channel_type, option),
897+
(12, self.opt_non_zero_fee_anchors, option),
898+
});
899+
Ok(())
900+
}
901+
}
902+
903+
impl Readable for ChannelTransactionParameters {
904+
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
905+
let mut holder_pubkeys = RequiredWrapper(None);
906+
let mut holder_selected_contest_delay = RequiredWrapper(None);
907+
let mut is_outbound_from_holder = RequiredWrapper(None);
908+
let mut counterparty_parameters = None;
909+
let mut funding_outpoint = None;
910+
let mut channel_type = Some(ChannelType::Legacy);
911+
let mut opt_non_zero_fee_anchors = None;
912+
decode_tlv_stream!(r, {
913+
(0, holder_pubkeys, required),
914+
(2, holder_selected_contest_delay, required),
915+
(4, is_outbound_from_holder, required),
916+
(6, counterparty_parameters, option),
917+
(8, funding_outpoint, option),
918+
(10, channel_type, option),
919+
(12, opt_non_zero_fee_anchors, option),
920+
});
921+
Ok(Self {
922+
holder_pubkeys: holder_pubkeys.0.unwrap(),
923+
holder_selected_contest_delay: holder_selected_contest_delay.0.unwrap(),
924+
is_outbound_from_holder: is_outbound_from_holder.0.unwrap(),
925+
counterparty_parameters,
926+
funding_outpoint,
927+
channel_type: channel_type.unwrap(),
928+
opt_non_zero_fee_anchors,
929+
})
930+
}
931+
}
892932

893933
/// Static channel fields used to build transactions given per-commitment fields, organized by
894934
/// broadcaster/countersignatory.
@@ -941,9 +981,9 @@ impl<'a> DirectedChannelTransactionParameters<'a> {
941981
self.inner.funding_outpoint.unwrap().into_bitcoin_outpoint()
942982
}
943983

944-
/// Whether to use anchors for this channel
945-
pub fn opt_anchors(&self) -> bool {
946-
self.inner.opt_anchors.is_some()
984+
/// Returns channel type enum
985+
pub fn channel_type(&self) -> ChannelType {
986+
self.inner.channel_type.clone()
947987
}
948988
}
949989

@@ -1010,7 +1050,7 @@ impl HolderCommitmentTransaction {
10101050
is_outbound_from_holder: false,
10111051
counterparty_parameters: Some(CounterpartyChannelTransactionParameters { pubkeys: channel_pubkeys.clone(), selected_contest_delay: 0 }),
10121052
funding_outpoint: Some(chain::transaction::OutPoint { txid: Txid::all_zeros(), index: 0 }),
1013-
opt_anchors: None,
1053+
channel_type: ChannelType::Legacy,
10141054
opt_non_zero_fee_anchors: None,
10151055
};
10161056
let mut counterparty_htlc_sigs = Vec::new();

0 commit comments

Comments
 (0)