Skip to content

Commit 519ec90

Browse files
committed
Store anchors option for supported output types
As we integrate the support of anchor outputs, we'll want to know if each input we're working with came from an anchor outputs channel. Instead of threading through a `opt_anchors` boolean across several methods on `PackageSolvingData` and `PackageTemplate`, we decide to store a reference in each `PackageSolvingData` variant instead that features a change in behavior between channels with and without anchor outputs.
1 parent e61f3a2 commit 519ec90

File tree

3 files changed

+41
-29
lines changed

3 files changed

+41
-29
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2521,13 +2521,13 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
25212521
CounterpartyOfferedHTLCOutput::build(*per_commitment_point,
25222522
self.counterparty_commitment_params.counterparty_delayed_payment_base_key,
25232523
self.counterparty_commitment_params.counterparty_htlc_base_key,
2524-
preimage.unwrap(), htlc.clone()))
2524+
preimage.unwrap(), htlc.clone(), self.onchain_tx_handler.opt_anchors()))
25252525
} else {
25262526
PackageSolvingData::CounterpartyReceivedHTLCOutput(
25272527
CounterpartyReceivedHTLCOutput::build(*per_commitment_point,
25282528
self.counterparty_commitment_params.counterparty_delayed_payment_base_key,
25292529
self.counterparty_commitment_params.counterparty_htlc_base_key,
2530-
htlc.clone()))
2530+
htlc.clone(), self.onchain_tx_handler.opt_anchors()))
25312531
};
25322532
let aggregation = if !htlc.offered { false } else { true };
25332533
let counterparty_package = PackageTemplate::build_package(commitment_txid, transaction_output_index, counterparty_htlc_outp, htlc.cltv_expiry,aggregation, 0);

lightning/src/chain/onchaintx.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
388388
// didn't receive confirmation of it before, or not enough reorg-safe depth on top of it).
389389
let new_timer = Some(cached_request.get_height_timer(cur_height));
390390
if cached_request.is_malleable() {
391-
let predicted_weight = cached_request.package_weight(&self.destination_script, self.channel_transaction_parameters.opt_anchors.is_some());
391+
let predicted_weight = cached_request.package_weight(&self.destination_script);
392392
if let Some((output_value, new_feerate)) =
393393
cached_request.compute_package_output(predicted_weight, self.destination_script.dust_value().to_sat(), fee_estimator, logger) {
394394
assert!(new_feerate != 0);

lightning/src/chain/package.rs

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -177,23 +177,30 @@ pub(crate) struct CounterpartyOfferedHTLCOutput {
177177
counterparty_delayed_payment_base_key: PublicKey,
178178
counterparty_htlc_base_key: PublicKey,
179179
preimage: PaymentPreimage,
180-
htlc: HTLCOutputInCommitment
180+
htlc: HTLCOutputInCommitment,
181+
opt_anchors: Option<()>,
181182
}
182183

183184
impl CounterpartyOfferedHTLCOutput {
184-
pub(crate) fn build(per_commitment_point: PublicKey, counterparty_delayed_payment_base_key: PublicKey, counterparty_htlc_base_key: PublicKey, preimage: PaymentPreimage, htlc: HTLCOutputInCommitment) -> Self {
185+
pub(crate) fn build(per_commitment_point: PublicKey, counterparty_delayed_payment_base_key: PublicKey, counterparty_htlc_base_key: PublicKey, preimage: PaymentPreimage, htlc: HTLCOutputInCommitment, opt_anchors: bool) -> Self {
185186
CounterpartyOfferedHTLCOutput {
186187
per_commitment_point,
187188
counterparty_delayed_payment_base_key,
188189
counterparty_htlc_base_key,
189190
preimage,
190-
htlc
191+
htlc,
192+
opt_anchors: if opt_anchors { Some(()) } else { None },
191193
}
192194
}
195+
196+
fn opt_anchors(&self) -> bool {
197+
self.opt_anchors.is_some()
198+
}
193199
}
194200

195201
impl_writeable_tlv_based!(CounterpartyOfferedHTLCOutput, {
196202
(0, per_commitment_point, required),
203+
(1, opt_anchors, option),
197204
(2, counterparty_delayed_payment_base_key, required),
198205
(4, counterparty_htlc_base_key, required),
199206
(6, preimage, required),
@@ -209,22 +216,29 @@ pub(crate) struct CounterpartyReceivedHTLCOutput {
209216
per_commitment_point: PublicKey,
210217
counterparty_delayed_payment_base_key: PublicKey,
211218
counterparty_htlc_base_key: PublicKey,
212-
htlc: HTLCOutputInCommitment
219+
htlc: HTLCOutputInCommitment,
220+
opt_anchors: Option<()>,
213221
}
214222

215223
impl CounterpartyReceivedHTLCOutput {
216-
pub(crate) fn build(per_commitment_point: PublicKey, counterparty_delayed_payment_base_key: PublicKey, counterparty_htlc_base_key: PublicKey, htlc: HTLCOutputInCommitment) -> Self {
224+
pub(crate) fn build(per_commitment_point: PublicKey, counterparty_delayed_payment_base_key: PublicKey, counterparty_htlc_base_key: PublicKey, htlc: HTLCOutputInCommitment, opt_anchors: bool) -> Self {
217225
CounterpartyReceivedHTLCOutput {
218226
per_commitment_point,
219227
counterparty_delayed_payment_base_key,
220228
counterparty_htlc_base_key,
221-
htlc
229+
htlc,
230+
opt_anchors: if opt_anchors { Some(()) } else { None },
222231
}
223232
}
233+
234+
fn opt_anchors(&self) -> bool {
235+
self.opt_anchors.is_some()
236+
}
224237
}
225238

226239
impl_writeable_tlv_based!(CounterpartyReceivedHTLCOutput, {
227240
(0, per_commitment_point, required),
241+
(1, opt_anchors, option),
228242
(2, counterparty_delayed_payment_base_key, required),
229243
(4, counterparty_htlc_base_key, required),
230244
(6, htlc, required),
@@ -315,12 +329,12 @@ impl PackageSolvingData {
315329
};
316330
amt
317331
}
318-
fn weight(&self, opt_anchors: bool) -> usize {
332+
fn weight(&self) -> usize {
319333
let weight = match self {
320334
PackageSolvingData::RevokedOutput(ref outp) => { outp.weight as usize },
321335
PackageSolvingData::RevokedHTLCOutput(ref outp) => { outp.weight as usize },
322-
PackageSolvingData::CounterpartyOfferedHTLCOutput(..) => { weight_offered_htlc(opt_anchors) as usize },
323-
PackageSolvingData::CounterpartyReceivedHTLCOutput(..) => { weight_received_htlc(opt_anchors) as usize },
336+
PackageSolvingData::CounterpartyOfferedHTLCOutput(ref outp) => { weight_offered_htlc(outp.opt_anchors()) as usize },
337+
PackageSolvingData::CounterpartyReceivedHTLCOutput(ref outp) => { weight_received_htlc(outp.opt_anchors()) as usize },
324338
// Note: Currently, weights of holder outputs spending witnesses aren't used
325339
// as we can't malleate spending package to increase their feerate. This
326340
// should change with the remaining anchor output patchset.
@@ -594,13 +608,13 @@ impl PackageTemplate {
594608
self.inputs.iter().map(|(_, outp)| outp.absolute_tx_timelock(self.height_original))
595609
.max().expect("There must always be at least one output to spend in a PackageTemplate")
596610
}
597-
pub(crate) fn package_weight(&self, destination_script: &Script, opt_anchors: bool) -> usize {
611+
pub(crate) fn package_weight(&self, destination_script: &Script) -> usize {
598612
let mut inputs_weight = 0;
599613
let mut witnesses_weight = 2; // count segwit flags
600614
for (_, outp) in self.inputs.iter() {
601615
// previous_out_point: 36 bytes ; var_int: 1 byte ; sequence: 4 bytes
602616
inputs_weight += 41 * WITNESS_SCALE_FACTOR;
603-
witnesses_weight += outp.weight(opt_anchors);
617+
witnesses_weight += outp.weight();
604618
}
605619
// version: 4 bytes ; count_tx_in: 1 byte ; count_tx_out: 1 byte ; lock_time: 4 bytes
606620
let transaction_weight = 10 * WITNESS_SCALE_FACTOR;
@@ -873,26 +887,26 @@ mod tests {
873887
}
874888

875889
macro_rules! dumb_counterparty_output {
876-
($secp_ctx: expr, $amt: expr) => {
890+
($secp_ctx: expr, $amt: expr, $opt_anchors: expr) => {
877891
{
878892
let dumb_scalar = SecretKey::from_slice(&hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap();
879893
let dumb_point = PublicKey::from_secret_key(&$secp_ctx, &dumb_scalar);
880894
let hash = PaymentHash([1; 32]);
881895
let htlc = HTLCOutputInCommitment { offered: true, amount_msat: $amt, cltv_expiry: 0, payment_hash: hash, transaction_output_index: None };
882-
PackageSolvingData::CounterpartyReceivedHTLCOutput(CounterpartyReceivedHTLCOutput::build(dumb_point, dumb_point, dumb_point, htlc))
896+
PackageSolvingData::CounterpartyReceivedHTLCOutput(CounterpartyReceivedHTLCOutput::build(dumb_point, dumb_point, dumb_point, htlc, $opt_anchors))
883897
}
884898
}
885899
}
886900

887901
macro_rules! dumb_counterparty_offered_output {
888-
($secp_ctx: expr, $amt: expr) => {
902+
($secp_ctx: expr, $amt: expr, $opt_anchors: expr) => {
889903
{
890904
let dumb_scalar = SecretKey::from_slice(&hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap();
891905
let dumb_point = PublicKey::from_secret_key(&$secp_ctx, &dumb_scalar);
892906
let hash = PaymentHash([1; 32]);
893907
let preimage = PaymentPreimage([2;32]);
894908
let htlc = HTLCOutputInCommitment { offered: false, amount_msat: $amt, cltv_expiry: 1000, payment_hash: hash, transaction_output_index: None };
895-
PackageSolvingData::CounterpartyOfferedHTLCOutput(CounterpartyOfferedHTLCOutput::build(dumb_point, dumb_point, dumb_point, preimage, htlc))
909+
PackageSolvingData::CounterpartyOfferedHTLCOutput(CounterpartyOfferedHTLCOutput::build(dumb_point, dumb_point, dumb_point, preimage, htlc, $opt_anchors))
896910
}
897911
}
898912
}
@@ -987,7 +1001,7 @@ mod tests {
9871001
let txid = Txid::from_hex("c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e").unwrap();
9881002
let secp_ctx = Secp256k1::new();
9891003
let revk_outp = dumb_revk_output!(secp_ctx);
990-
let counterparty_outp = dumb_counterparty_output!(secp_ctx, 0);
1004+
let counterparty_outp = dumb_counterparty_output!(secp_ctx, 0, false);
9911005

9921006
let mut revoked_package = PackageTemplate::build_package(txid, 0, revk_outp, 1000, true, 100);
9931007
let counterparty_package = PackageTemplate::build_package(txid, 1, counterparty_outp, 1000, true, 100);
@@ -1051,7 +1065,7 @@ mod tests {
10511065
fn test_package_amounts() {
10521066
let txid = Txid::from_hex("c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e").unwrap();
10531067
let secp_ctx = Secp256k1::new();
1054-
let counterparty_outp = dumb_counterparty_output!(secp_ctx, 1_000_000);
1068+
let counterparty_outp = dumb_counterparty_output!(secp_ctx, 1_000_000, false);
10551069

10561070
let package = PackageTemplate::build_package(txid, 0, counterparty_outp, 1000, true, 100);
10571071
assert_eq!(package.package_amount(), 1000);
@@ -1068,24 +1082,22 @@ mod tests {
10681082
{
10691083
let revk_outp = dumb_revk_output!(secp_ctx);
10701084
let package = PackageTemplate::build_package(txid, 0, revk_outp, 0, true, 100);
1071-
for &opt_anchors in [false, true].iter() {
1072-
assert_eq!(package.package_weight(&Script::new(), opt_anchors), weight_sans_output + WEIGHT_REVOKED_OUTPUT as usize);
1073-
}
1085+
assert_eq!(package.package_weight(&Script::new()), weight_sans_output + WEIGHT_REVOKED_OUTPUT as usize);
10741086
}
10751087

10761088
{
1077-
let counterparty_outp = dumb_counterparty_output!(secp_ctx, 1_000_000);
1078-
let package = PackageTemplate::build_package(txid, 0, counterparty_outp, 1000, true, 100);
10791089
for &opt_anchors in [false, true].iter() {
1080-
assert_eq!(package.package_weight(&Script::new(), opt_anchors), weight_sans_output + weight_received_htlc(opt_anchors) as usize);
1090+
let counterparty_outp = dumb_counterparty_output!(secp_ctx, 1_000_000, opt_anchors);
1091+
let package = PackageTemplate::build_package(txid, 0, counterparty_outp, 1000, true, 100);
1092+
assert_eq!(package.package_weight(&Script::new()), weight_sans_output + weight_received_htlc(opt_anchors) as usize);
10811093
}
10821094
}
10831095

10841096
{
1085-
let counterparty_outp = dumb_counterparty_offered_output!(secp_ctx, 1_000_000);
1086-
let package = PackageTemplate::build_package(txid, 0, counterparty_outp, 1000, true, 100);
10871097
for &opt_anchors in [false, true].iter() {
1088-
assert_eq!(package.package_weight(&Script::new(), opt_anchors), weight_sans_output + weight_offered_htlc(opt_anchors) as usize);
1098+
let counterparty_outp = dumb_counterparty_offered_output!(secp_ctx, 1_000_000, opt_anchors);
1099+
let package = PackageTemplate::build_package(txid, 0, counterparty_outp, 1000, true, 100);
1100+
assert_eq!(package.package_weight(&Script::new()), weight_sans_output + weight_offered_htlc(opt_anchors) as usize);
10891101
}
10901102
}
10911103
}

0 commit comments

Comments
 (0)