Replace `opt_anchors` with `ChannelTypeFeatures`
[rust-lightning] / lightning / src / chain / package.rs
1 // This file is Copyright its original authors, visible in version control
2 // history.
3 //
4 // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5 // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7 // You may not use this file except in accordance with one or both of these
8 // licenses.
9
10 //! Various utilities to assemble claimable outpoints in package of one or more transactions. Those
11 //! packages are attached metadata, guiding their aggregable or fee-bumping re-schedule. This file
12 //! also includes witness weight computation and fee computation methods.
13
14 use bitcoin::blockdata::constants::WITNESS_SCALE_FACTOR;
15 use bitcoin::blockdata::transaction::{TxOut,TxIn, Transaction, EcdsaSighashType};
16 use bitcoin::blockdata::transaction::OutPoint as BitcoinOutPoint;
17 use bitcoin::blockdata::script::Script;
18
19 use bitcoin::hash_types::Txid;
20
21 use bitcoin::secp256k1::{SecretKey,PublicKey};
22
23 use crate::ln::PaymentPreimage;
24 use crate::ln::chan_utils::{TxCreationKeys, HTLCOutputInCommitment};
25 use crate::ln::chan_utils;
26 use crate::ln::msgs::DecodeError;
27 use crate::chain::chaininterface::{FeeEstimator, ConfirmationTarget, MIN_RELAY_FEE_SAT_PER_1000_WEIGHT};
28 use crate::sign::WriteableEcdsaChannelSigner;
29 #[cfg(anchors)]
30 use crate::chain::onchaintx::ExternalHTLCClaim;
31 use crate::chain::onchaintx::OnchainTxHandler;
32 use crate::util::logger::Logger;
33 use crate::util::ser::{Readable, Writer, Writeable, RequiredWrapper};
34
35 use crate::io;
36 use crate::prelude::*;
37 use core::cmp;
38 #[cfg(anchors)]
39 use core::convert::TryInto;
40 use core::mem;
41 use core::ops::Deref;
42 use bitcoin::{PackedLockTime, Sequence, Witness};
43 use crate::ln::features::ChannelTypeFeatures;
44
45 use super::chaininterface::LowerBoundedFeeEstimator;
46
47 const MAX_ALLOC_SIZE: usize = 64*1024;
48
49
50 pub(crate) fn weight_revoked_offered_htlc(channel_type_features: &ChannelTypeFeatures) -> u64 {
51         // number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script
52         const WEIGHT_REVOKED_OFFERED_HTLC: u64 = 1 + 1 + 73 + 1 + 33 + 1 + 133;
53         const WEIGHT_REVOKED_OFFERED_HTLC_ANCHORS: u64 = WEIGHT_REVOKED_OFFERED_HTLC + 3; // + OP_1 + OP_CSV + OP_DROP
54         if channel_type_features.supports_anchors_zero_fee_htlc_tx() { WEIGHT_REVOKED_OFFERED_HTLC_ANCHORS } else { WEIGHT_REVOKED_OFFERED_HTLC }
55 }
56
57 pub(crate) fn weight_revoked_received_htlc(channel_type_features: &ChannelTypeFeatures) -> u64 {
58         // number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script
59         const WEIGHT_REVOKED_RECEIVED_HTLC: u64 = 1 + 1 + 73 + 1 + 33 + 1 +  139;
60         const WEIGHT_REVOKED_RECEIVED_HTLC_ANCHORS: u64 = WEIGHT_REVOKED_RECEIVED_HTLC + 3; // + OP_1 + OP_CSV + OP_DROP
61         if channel_type_features.supports_anchors_zero_fee_htlc_tx() { WEIGHT_REVOKED_RECEIVED_HTLC_ANCHORS } else { WEIGHT_REVOKED_RECEIVED_HTLC }
62 }
63
64 pub(crate) fn weight_offered_htlc(channel_type_features: &ChannelTypeFeatures) -> u64 {
65         // number_of_witness_elements + sig_length + counterpartyhtlc_sig  + preimage_length + preimage + witness_script_length + witness_script
66         const WEIGHT_OFFERED_HTLC: u64 = 1 + 1 + 73 + 1 + 32 + 1 + 133;
67         const WEIGHT_OFFERED_HTLC_ANCHORS: u64 = WEIGHT_OFFERED_HTLC + 3; // + OP_1 + OP_CSV + OP_DROP
68         if channel_type_features.supports_anchors_zero_fee_htlc_tx() { WEIGHT_OFFERED_HTLC_ANCHORS } else { WEIGHT_OFFERED_HTLC }
69 }
70
71 pub(crate) fn weight_received_htlc(channel_type_features: &ChannelTypeFeatures) -> u64 {
72         // number_of_witness_elements + sig_length + counterpartyhtlc_sig + empty_vec_length + empty_vec + witness_script_length + witness_script
73         const WEIGHT_RECEIVED_HTLC: u64 = 1 + 1 + 73 + 1 + 1 + 1 + 139;
74         const WEIGHT_RECEIVED_HTLC_ANCHORS: u64 = WEIGHT_RECEIVED_HTLC + 3; // + OP_1 + OP_CSV + OP_DROP
75         if channel_type_features.supports_anchors_zero_fee_htlc_tx() { WEIGHT_RECEIVED_HTLC_ANCHORS } else { WEIGHT_RECEIVED_HTLC }
76 }
77
78 // number_of_witness_elements + sig_length + revocation_sig + true_length + op_true + witness_script_length + witness_script
79 pub(crate) const WEIGHT_REVOKED_OUTPUT: u64 = 1 + 1 + 73 + 1 + 1 + 1 + 77;
80
81 /// Height delay at which transactions are fee-bumped/rebroadcasted with a low priority.
82 const LOW_FREQUENCY_BUMP_INTERVAL: u32 = 15;
83 /// Height delay at which transactions are fee-bumped/rebroadcasted with a middle priority.
84 const MIDDLE_FREQUENCY_BUMP_INTERVAL: u32 = 3;
85 /// Height delay at which transactions are fee-bumped/rebroadcasted with a high priority.
86 const HIGH_FREQUENCY_BUMP_INTERVAL: u32 = 1;
87
88 /// A struct to describe a revoked output and corresponding information to generate a solving
89 /// witness spending a commitment `to_local` output or a second-stage HTLC transaction output.
90 ///
91 /// CSV and pubkeys are used as part of a witnessScript redeeming a balance output, amount is used
92 /// as part of the signature hash and revocation secret to generate a satisfying witness.
93 #[derive(Clone, PartialEq, Eq)]
94 pub(crate) struct RevokedOutput {
95         per_commitment_point: PublicKey,
96         counterparty_delayed_payment_base_key: PublicKey,
97         counterparty_htlc_base_key: PublicKey,
98         per_commitment_key: SecretKey,
99         weight: u64,
100         amount: u64,
101         on_counterparty_tx_csv: u16,
102         is_counterparty_balance_on_anchors: Option<()>,
103 }
104
105 impl RevokedOutput {
106         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, on_counterparty_tx_csv: u16, is_counterparty_balance_on_anchors: bool) -> Self {
107                 RevokedOutput {
108                         per_commitment_point,
109                         counterparty_delayed_payment_base_key,
110                         counterparty_htlc_base_key,
111                         per_commitment_key,
112                         weight: WEIGHT_REVOKED_OUTPUT,
113                         amount,
114                         on_counterparty_tx_csv,
115                         is_counterparty_balance_on_anchors: if is_counterparty_balance_on_anchors { Some(()) } else { None }
116                 }
117         }
118 }
119
120 impl_writeable_tlv_based!(RevokedOutput, {
121         (0, per_commitment_point, required),
122         (2, counterparty_delayed_payment_base_key, required),
123         (4, counterparty_htlc_base_key, required),
124         (6, per_commitment_key, required),
125         (8, weight, required),
126         (10, amount, required),
127         (12, on_counterparty_tx_csv, required),
128         (14, is_counterparty_balance_on_anchors, option)
129 });
130
131 /// A struct to describe a revoked offered output and corresponding information to generate a
132 /// solving witness.
133 ///
134 /// HTLCOuputInCommitment (hash timelock, direction) and pubkeys are used to generate a suitable
135 /// witnessScript.
136 ///
137 /// CSV is used as part of a witnessScript redeeming a balance output, amount is used as part
138 /// of the signature hash and revocation secret to generate a satisfying witness.
139 #[derive(Clone, PartialEq, Eq)]
140 pub(crate) struct RevokedHTLCOutput {
141         per_commitment_point: PublicKey,
142         counterparty_delayed_payment_base_key: PublicKey,
143         counterparty_htlc_base_key: PublicKey,
144         per_commitment_key: SecretKey,
145         weight: u64,
146         amount: u64,
147         htlc: HTLCOutputInCommitment,
148 }
149
150 impl RevokedHTLCOutput {
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_features: &ChannelTypeFeatures) -> Self {
152                 let weight = if htlc.offered { weight_revoked_offered_htlc(channel_type_features) } else { weight_revoked_received_htlc(channel_type_features) };
153                 RevokedHTLCOutput {
154                         per_commitment_point,
155                         counterparty_delayed_payment_base_key,
156                         counterparty_htlc_base_key,
157                         per_commitment_key,
158                         weight,
159                         amount,
160                         htlc
161                 }
162         }
163 }
164
165 impl_writeable_tlv_based!(RevokedHTLCOutput, {
166         (0, per_commitment_point, required),
167         (2, counterparty_delayed_payment_base_key, required),
168         (4, counterparty_htlc_base_key, required),
169         (6, per_commitment_key, required),
170         (8, weight, required),
171         (10, amount, required),
172         (12, htlc, required),
173 });
174
175 /// A struct to describe a HTLC output on a counterparty commitment transaction.
176 ///
177 /// HTLCOutputInCommitment (hash, timelock, directon) and pubkeys are used to generate a suitable
178 /// witnessScript.
179 ///
180 /// The preimage is used as part of the witness.
181 ///
182 /// Note that on upgrades, some features of existing outputs may be missed.
183 #[derive(Clone, PartialEq, Eq)]
184 pub(crate) struct CounterpartyOfferedHTLCOutput {
185         per_commitment_point: PublicKey,
186         counterparty_delayed_payment_base_key: PublicKey,
187         counterparty_htlc_base_key: PublicKey,
188         preimage: PaymentPreimage,
189         htlc: HTLCOutputInCommitment,
190         channel_type_features: ChannelTypeFeatures,
191 }
192
193 impl CounterpartyOfferedHTLCOutput {
194         pub(crate) fn build(per_commitment_point: PublicKey, counterparty_delayed_payment_base_key: PublicKey, counterparty_htlc_base_key: PublicKey, preimage: PaymentPreimage, htlc: HTLCOutputInCommitment, channel_type_features: ChannelTypeFeatures) -> Self {
195                 CounterpartyOfferedHTLCOutput {
196                         per_commitment_point,
197                         counterparty_delayed_payment_base_key,
198                         counterparty_htlc_base_key,
199                         preimage,
200                         htlc,
201                         channel_type_features,
202                 }
203         }
204 }
205
206 impl Writeable for CounterpartyOfferedHTLCOutput {
207         fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
208                 let legacy_deserialization_prevention_marker = chan_utils::legacy_deserialization_prevention_marker_for_channel_type_features(&self.channel_type_features);
209                 write_tlv_fields!(writer, {
210                         (0, self.per_commitment_point, required),
211                         (2, self.counterparty_delayed_payment_base_key, required),
212                         (4, self.counterparty_htlc_base_key, required),
213                         (6, self.preimage, required),
214                         (8, self.htlc, required),
215                         (10, legacy_deserialization_prevention_marker, option),
216                         (11, self.channel_type_features, required),
217                 });
218                 Ok(())
219         }
220 }
221
222 impl Readable for CounterpartyOfferedHTLCOutput {
223         fn read<R: io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
224                 let mut per_commitment_point = RequiredWrapper(None);
225                 let mut counterparty_delayed_payment_base_key = RequiredWrapper(None);
226                 let mut counterparty_htlc_base_key = RequiredWrapper(None);
227                 let mut preimage = RequiredWrapper(None);
228                 let mut htlc = RequiredWrapper(None);
229                 let mut legacy_deserialization_prevention_marker: Option<()> = None;
230                 let mut channel_type_features = None;
231
232                 read_tlv_fields!(reader, {
233                         (0, per_commitment_point, required),
234                         (2, counterparty_delayed_payment_base_key, required),
235                         (4, counterparty_htlc_base_key, required),
236                         (6, preimage, required),
237                         (8, htlc, required),
238                         (10, legacy_deserialization_prevention_marker, option),
239                         (11, channel_type_features, option),
240                 });
241
242                 Ok(Self {
243                         per_commitment_point: per_commitment_point.0.unwrap(),
244                         counterparty_delayed_payment_base_key: counterparty_delayed_payment_base_key.0.unwrap(),
245                         counterparty_htlc_base_key: counterparty_htlc_base_key.0.unwrap(),
246                         preimage: preimage.0.unwrap(),
247                         htlc: htlc.0.unwrap(),
248                         channel_type_features: channel_type_features.unwrap_or(ChannelTypeFeatures::only_static_remote_key())
249                 })
250         }
251 }
252
253 /// A struct to describe a HTLC output on a counterparty commitment transaction.
254 ///
255 /// HTLCOutputInCommitment (hash, timelock, directon) and pubkeys are used to generate a suitable
256 /// witnessScript.
257 ///
258 /// Note that on upgrades, some features of existing outputs may be missed.
259 #[derive(Clone, PartialEq, Eq)]
260 pub(crate) struct CounterpartyReceivedHTLCOutput {
261         per_commitment_point: PublicKey,
262         counterparty_delayed_payment_base_key: PublicKey,
263         counterparty_htlc_base_key: PublicKey,
264         htlc: HTLCOutputInCommitment,
265         channel_type_features: ChannelTypeFeatures,
266 }
267
268 impl CounterpartyReceivedHTLCOutput {
269         pub(crate) fn build(per_commitment_point: PublicKey, counterparty_delayed_payment_base_key: PublicKey, counterparty_htlc_base_key: PublicKey, htlc: HTLCOutputInCommitment, channel_type_features: ChannelTypeFeatures) -> Self {
270                 CounterpartyReceivedHTLCOutput {
271                         per_commitment_point,
272                         counterparty_delayed_payment_base_key,
273                         counterparty_htlc_base_key,
274                         htlc,
275                         channel_type_features
276                 }
277         }
278 }
279
280 impl Writeable for CounterpartyReceivedHTLCOutput {
281         fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
282                 let legacy_deserialization_prevention_marker = chan_utils::legacy_deserialization_prevention_marker_for_channel_type_features(&self.channel_type_features);
283                 write_tlv_fields!(writer, {
284                         (0, self.per_commitment_point, required),
285                         (2, self.counterparty_delayed_payment_base_key, required),
286                         (4, self.counterparty_htlc_base_key, required),
287                         (6, self.htlc, required),
288                         (8, legacy_deserialization_prevention_marker, option),
289                         (9, self.channel_type_features, required),
290                 });
291                 Ok(())
292         }
293 }
294
295 impl Readable for CounterpartyReceivedHTLCOutput {
296         fn read<R: io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
297                 let mut per_commitment_point = RequiredWrapper(None);
298                 let mut counterparty_delayed_payment_base_key = RequiredWrapper(None);
299                 let mut counterparty_htlc_base_key = RequiredWrapper(None);
300                 let mut htlc = RequiredWrapper(None);
301                 let mut legacy_deserialization_prevention_marker: Option<()> = None;
302                 let mut channel_type_features = None;
303
304                 read_tlv_fields!(reader, {
305                         (0, per_commitment_point, required),
306                         (2, counterparty_delayed_payment_base_key, required),
307                         (4, counterparty_htlc_base_key, required),
308                         (6, htlc, required),
309                         (8, legacy_deserialization_prevention_marker, option),
310                         (9, channel_type_features, option),
311                 });
312
313                 Ok(Self {
314                         per_commitment_point: per_commitment_point.0.unwrap(),
315                         counterparty_delayed_payment_base_key: counterparty_delayed_payment_base_key.0.unwrap(),
316                         counterparty_htlc_base_key: counterparty_htlc_base_key.0.unwrap(),
317                         htlc: htlc.0.unwrap(),
318                         channel_type_features: channel_type_features.unwrap_or(ChannelTypeFeatures::only_static_remote_key())
319                 })
320         }
321 }
322
323 /// A struct to describe a HTLC output on holder commitment transaction.
324 ///
325 /// Either offered or received, the amount is always used as part of the bip143 sighash.
326 /// Preimage is only included as part of the witness in former case.
327 ///
328 /// Note that on upgrades, some features of existing outputs may be missed.
329 #[derive(Clone, PartialEq, Eq)]
330 pub(crate) struct HolderHTLCOutput {
331         preimage: Option<PaymentPreimage>,
332         amount_msat: u64,
333         /// Defaults to 0 for HTLC-Success transactions, which have no expiry
334         cltv_expiry: u32,
335         channel_type_features: ChannelTypeFeatures,
336 }
337
338 impl HolderHTLCOutput {
339         pub(crate) fn build_offered(amount_msat: u64, cltv_expiry: u32, channel_type_features: ChannelTypeFeatures) -> Self {
340                 HolderHTLCOutput {
341                         preimage: None,
342                         amount_msat,
343                         cltv_expiry,
344                         channel_type_features,
345                 }
346         }
347
348         pub(crate) fn build_accepted(preimage: PaymentPreimage, amount_msat: u64, channel_type_features: ChannelTypeFeatures) -> Self {
349                 HolderHTLCOutput {
350                         preimage: Some(preimage),
351                         amount_msat,
352                         cltv_expiry: 0,
353                         channel_type_features,
354                 }
355         }
356 }
357
358 impl Writeable for HolderHTLCOutput {
359         fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
360                 let legacy_deserialization_prevention_marker = chan_utils::legacy_deserialization_prevention_marker_for_channel_type_features(&self.channel_type_features);
361                 write_tlv_fields!(writer, {
362                         (0, self.amount_msat, required),
363                         (2, self.cltv_expiry, required),
364                         (4, self.preimage, option),
365                         (6, legacy_deserialization_prevention_marker, option),
366                         (7, self.channel_type_features, required),
367                 });
368                 Ok(())
369         }
370 }
371
372 impl Readable for HolderHTLCOutput {
373         fn read<R: io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
374                 let mut amount_msat = RequiredWrapper(None);
375                 let mut cltv_expiry = RequiredWrapper(None);
376                 let mut preimage = None;
377                 let mut legacy_deserialization_prevention_marker: Option<()> = None;
378                 let mut channel_type_features = None;
379
380                 read_tlv_fields!(reader, {
381                         (0, amount_msat, required),
382                         (2, cltv_expiry, required),
383                         (4, preimage, option),
384                         (6, legacy_deserialization_prevention_marker, option),
385                         (7, channel_type_features, option),
386                 });
387
388                 Ok(Self {
389                         amount_msat: amount_msat.0.unwrap(),
390                         cltv_expiry: cltv_expiry.0.unwrap(),
391                         preimage,
392                         channel_type_features: channel_type_features.unwrap_or(ChannelTypeFeatures::only_static_remote_key())
393                 })
394         }
395 }
396
397 /// A struct to describe the channel output on the funding transaction.
398 ///
399 /// witnessScript is used as part of the witness redeeming the funding utxo.
400 ///
401 /// Note that on upgrades, some features of existing outputs may be missed.
402 #[derive(Clone, PartialEq, Eq)]
403 pub(crate) struct HolderFundingOutput {
404         funding_redeemscript: Script,
405         funding_amount: Option<u64>,
406         channel_type_features: ChannelTypeFeatures,
407 }
408
409
410 impl HolderFundingOutput {
411         pub(crate) fn build(funding_redeemscript: Script, funding_amount: u64, channel_type_features: ChannelTypeFeatures) -> Self {
412                 HolderFundingOutput {
413                         funding_redeemscript,
414                         funding_amount: Some(funding_amount),
415                         channel_type_features,
416                 }
417         }
418 }
419
420 impl Writeable for HolderFundingOutput {
421         fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
422                 let legacy_deserialization_prevention_marker = chan_utils::legacy_deserialization_prevention_marker_for_channel_type_features(&self.channel_type_features);
423                 write_tlv_fields!(writer, {
424                         (0, self.funding_redeemscript, required),
425                         (1, self.channel_type_features, required),
426                         (2, legacy_deserialization_prevention_marker, option),
427                         (3, self.funding_amount, option),
428                 });
429                 Ok(())
430         }
431 }
432
433 impl Readable for HolderFundingOutput {
434         fn read<R: io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
435                 let mut funding_redeemscript = RequiredWrapper(None);
436                 let mut legacy_deserialization_prevention_marker: Option<()> = None;
437                 let mut channel_type_features = None;
438                 let mut funding_amount = None;
439
440                 read_tlv_fields!(reader, {
441                         (0, funding_redeemscript, required),
442                         (1, channel_type_features, option),
443                         (2, legacy_deserialization_prevention_marker, option),
444                         (3, funding_amount, option)
445                 });
446
447                 Ok(Self {
448                         funding_redeemscript: funding_redeemscript.0.unwrap(),
449                         channel_type_features: channel_type_features.unwrap_or(ChannelTypeFeatures::only_static_remote_key()),
450                         funding_amount
451                 })
452         }
453 }
454
455 /// A wrapper encapsulating all in-protocol differing outputs types.
456 ///
457 /// The generic API offers access to an outputs common attributes or allow transformation such as
458 /// finalizing an input claiming the output.
459 #[derive(Clone, PartialEq, Eq)]
460 pub(crate) enum PackageSolvingData {
461         RevokedOutput(RevokedOutput),
462         RevokedHTLCOutput(RevokedHTLCOutput),
463         CounterpartyOfferedHTLCOutput(CounterpartyOfferedHTLCOutput),
464         CounterpartyReceivedHTLCOutput(CounterpartyReceivedHTLCOutput),
465         HolderHTLCOutput(HolderHTLCOutput),
466         HolderFundingOutput(HolderFundingOutput),
467 }
468
469 impl PackageSolvingData {
470         fn amount(&self) -> u64 {
471                 let amt = match self {
472                         PackageSolvingData::RevokedOutput(ref outp) => outp.amount,
473                         PackageSolvingData::RevokedHTLCOutput(ref outp) => outp.amount,
474                         PackageSolvingData::CounterpartyOfferedHTLCOutput(ref outp) => outp.htlc.amount_msat / 1000,
475                         PackageSolvingData::CounterpartyReceivedHTLCOutput(ref outp) => outp.htlc.amount_msat / 1000,
476                         PackageSolvingData::HolderHTLCOutput(ref outp) => {
477                                 debug_assert!(outp.channel_type_features.supports_anchors_zero_fee_htlc_tx());
478                                 outp.amount_msat / 1000
479                         },
480                         PackageSolvingData::HolderFundingOutput(ref outp) => {
481                                 debug_assert!(outp.channel_type_features.supports_anchors_zero_fee_htlc_tx());
482                                 outp.funding_amount.unwrap()
483                         }
484                 };
485                 amt
486         }
487         fn weight(&self) -> usize {
488                 match self {
489                         PackageSolvingData::RevokedOutput(ref outp) => outp.weight as usize,
490                         PackageSolvingData::RevokedHTLCOutput(ref outp) => outp.weight as usize,
491                         PackageSolvingData::CounterpartyOfferedHTLCOutput(ref outp) => weight_offered_htlc(&outp.channel_type_features) as usize,
492                         PackageSolvingData::CounterpartyReceivedHTLCOutput(ref outp) => weight_received_htlc(&outp.channel_type_features) as usize,
493                         PackageSolvingData::HolderHTLCOutput(ref outp) => {
494                                 debug_assert!(outp.channel_type_features.supports_anchors_zero_fee_htlc_tx());
495                                 if outp.preimage.is_none() {
496                                         weight_offered_htlc(&outp.channel_type_features) as usize
497                                 } else {
498                                         weight_received_htlc(&outp.channel_type_features) as usize
499                                 }
500                         },
501                         // Since HolderFundingOutput maps to an untractable package that is already signed, its
502                         // weight can be determined from the transaction itself.
503                         PackageSolvingData::HolderFundingOutput(..) => unreachable!(),
504                 }
505         }
506         fn is_compatible(&self, input: &PackageSolvingData) -> bool {
507                 match self {
508                         PackageSolvingData::RevokedOutput(..) => {
509                                 match input {
510                                         PackageSolvingData::RevokedHTLCOutput(..) => { true },
511                                         PackageSolvingData::RevokedOutput(..) => { true },
512                                         _ => { false }
513                                 }
514                         },
515                         PackageSolvingData::RevokedHTLCOutput(..) => {
516                                 match input {
517                                         PackageSolvingData::RevokedOutput(..) => { true },
518                                         PackageSolvingData::RevokedHTLCOutput(..) => { true },
519                                         _ => { false }
520                                 }
521                         },
522                         _ => { mem::discriminant(self) == mem::discriminant(&input) }
523                 }
524         }
525         fn finalize_input<Signer: WriteableEcdsaChannelSigner>(&self, bumped_tx: &mut Transaction, i: usize, onchain_handler: &mut OnchainTxHandler<Signer>) -> bool {
526                 match self {
527                         PackageSolvingData::RevokedOutput(ref outp) => {
528                                 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);
529                                 let witness_script = chan_utils::get_revokeable_redeemscript(&chan_keys.revocation_key, outp.on_counterparty_tx_csv, &chan_keys.broadcaster_delayed_payment_key);
530                                 //TODO: should we panic on signer failure ?
531                                 if let Ok(sig) = onchain_handler.signer.sign_justice_revoked_output(&bumped_tx, i, outp.amount, &outp.per_commitment_key, &onchain_handler.secp_ctx) {
532                                         let mut ser_sig = sig.serialize_der().to_vec();
533                                         ser_sig.push(EcdsaSighashType::All as u8);
534                                         bumped_tx.input[i].witness.push(ser_sig);
535                                         bumped_tx.input[i].witness.push(vec!(1));
536                                         bumped_tx.input[i].witness.push(witness_script.clone().into_bytes());
537                                 } else { return false; }
538                         },
539                         PackageSolvingData::RevokedHTLCOutput(ref outp) => {
540                                 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);
541                                 let witness_script = chan_utils::get_htlc_redeemscript_with_explicit_keys(&outp.htlc, &onchain_handler.channel_type_features(), &chan_keys.broadcaster_htlc_key, &chan_keys.countersignatory_htlc_key, &chan_keys.revocation_key);
542                                 //TODO: should we panic on signer failure ?
543                                 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) {
544                                         let mut ser_sig = sig.serialize_der().to_vec();
545                                         ser_sig.push(EcdsaSighashType::All as u8);
546                                         bumped_tx.input[i].witness.push(ser_sig);
547                                         bumped_tx.input[i].witness.push(chan_keys.revocation_key.clone().serialize().to_vec());
548                                         bumped_tx.input[i].witness.push(witness_script.clone().into_bytes());
549                                 } else { return false; }
550                         },
551                         PackageSolvingData::CounterpartyOfferedHTLCOutput(ref outp) => {
552                                 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);
553                                 let witness_script = chan_utils::get_htlc_redeemscript_with_explicit_keys(&outp.htlc, &onchain_handler.channel_type_features(), &chan_keys.broadcaster_htlc_key, &chan_keys.countersignatory_htlc_key, &chan_keys.revocation_key);
554
555                                 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) {
556                                         let mut ser_sig = sig.serialize_der().to_vec();
557                                         ser_sig.push(EcdsaSighashType::All as u8);
558                                         bumped_tx.input[i].witness.push(ser_sig);
559                                         bumped_tx.input[i].witness.push(outp.preimage.0.to_vec());
560                                         bumped_tx.input[i].witness.push(witness_script.clone().into_bytes());
561                                 }
562                         },
563                         PackageSolvingData::CounterpartyReceivedHTLCOutput(ref outp) => {
564                                 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);
565                                 let witness_script = chan_utils::get_htlc_redeemscript_with_explicit_keys(&outp.htlc, &onchain_handler.channel_type_features(), &chan_keys.broadcaster_htlc_key, &chan_keys.countersignatory_htlc_key, &chan_keys.revocation_key);
566
567                                 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) {
568                                         let mut ser_sig = sig.serialize_der().to_vec();
569                                         ser_sig.push(EcdsaSighashType::All as u8);
570                                         bumped_tx.input[i].witness.push(ser_sig);
571                                         // Due to BIP146 (MINIMALIF) this must be a zero-length element to relay.
572                                         bumped_tx.input[i].witness.push(vec![]);
573                                         bumped_tx.input[i].witness.push(witness_script.clone().into_bytes());
574                                 }
575                         },
576                         _ => { panic!("API Error!"); }
577                 }
578                 true
579         }
580         fn get_finalized_tx<Signer: WriteableEcdsaChannelSigner>(&self, outpoint: &BitcoinOutPoint, onchain_handler: &mut OnchainTxHandler<Signer>) -> Option<Transaction> {
581                 match self {
582                         PackageSolvingData::HolderHTLCOutput(ref outp) => {
583                                 debug_assert!(!outp.channel_type_features.supports_anchors_zero_fee_htlc_tx());
584                                 return onchain_handler.get_fully_signed_htlc_tx(outpoint, &outp.preimage);
585                         }
586                         PackageSolvingData::HolderFundingOutput(ref outp) => {
587                                 return Some(onchain_handler.get_fully_signed_holder_tx(&outp.funding_redeemscript));
588                         }
589                         _ => { panic!("API Error!"); }
590                 }
591         }
592         fn absolute_tx_timelock(&self, current_height: u32) -> u32 {
593                 // We use `current_height` as our default locktime to discourage fee sniping and because
594                 // transactions with it always propagate.
595                 let absolute_timelock = match self {
596                         PackageSolvingData::RevokedOutput(_) => current_height,
597                         PackageSolvingData::RevokedHTLCOutput(_) => current_height,
598                         PackageSolvingData::CounterpartyOfferedHTLCOutput(_) => current_height,
599                         PackageSolvingData::CounterpartyReceivedHTLCOutput(ref outp) => cmp::max(outp.htlc.cltv_expiry, current_height),
600                         // HTLC timeout/success transactions rely on a fixed timelock due to the counterparty's
601                         // signature.
602                         PackageSolvingData::HolderHTLCOutput(ref outp) => {
603                                 if outp.preimage.is_some() {
604                                         debug_assert_eq!(outp.cltv_expiry, 0);
605                                 }
606                                 outp.cltv_expiry
607                         },
608                         PackageSolvingData::HolderFundingOutput(_) => current_height,
609                 };
610                 absolute_timelock
611         }
612
613         fn map_output_type_flags(&self) -> (PackageMalleability, bool) {
614                 // Post-anchor, aggregation of outputs of different types is unsafe. See https://github.com/lightning/bolts/pull/803.
615                 let (malleability, aggregable) = match self {
616                         PackageSolvingData::RevokedOutput(RevokedOutput { is_counterparty_balance_on_anchors: Some(()), .. }) => { (PackageMalleability::Malleable, false) },
617                         PackageSolvingData::RevokedOutput(RevokedOutput { is_counterparty_balance_on_anchors: None, .. }) => { (PackageMalleability::Malleable, true) },
618                         PackageSolvingData::RevokedHTLCOutput(..) => { (PackageMalleability::Malleable, true) },
619                         PackageSolvingData::CounterpartyOfferedHTLCOutput(..) => { (PackageMalleability::Malleable, true) },
620                         PackageSolvingData::CounterpartyReceivedHTLCOutput(..) => { (PackageMalleability::Malleable, false) },
621                         PackageSolvingData::HolderHTLCOutput(ref outp) => if outp.channel_type_features.supports_anchors_zero_fee_htlc_tx() {
622                                 (PackageMalleability::Malleable, outp.preimage.is_some())
623                         } else {
624                                 (PackageMalleability::Untractable, false)
625                         },
626                         PackageSolvingData::HolderFundingOutput(..) => { (PackageMalleability::Untractable, false) },
627                 };
628                 (malleability, aggregable)
629         }
630 }
631
632 impl_writeable_tlv_based_enum!(PackageSolvingData, ;
633         (0, RevokedOutput),
634         (1, RevokedHTLCOutput),
635         (2, CounterpartyOfferedHTLCOutput),
636         (3, CounterpartyReceivedHTLCOutput),
637         (4, HolderHTLCOutput),
638         (5, HolderFundingOutput),
639 );
640
641 /// A malleable package might be aggregated with other packages to save on fees.
642 /// A untractable package has been counter-signed and aggregable will break cached counterparty signatures.
643 #[derive(Clone, PartialEq, Eq)]
644 pub(crate) enum PackageMalleability {
645         Malleable,
646         Untractable,
647 }
648
649 /// A structure to describe a package content that is generated by ChannelMonitor and
650 /// used by OnchainTxHandler to generate and broadcast transactions settling onchain claims.
651 ///
652 /// A package is defined as one or more transactions claiming onchain outputs in reaction
653 /// to confirmation of a channel transaction. Those packages might be aggregated to save on
654 /// fees, if satisfaction of outputs's witnessScript let's us do so.
655 ///
656 /// As packages are time-sensitive, we fee-bump and rebroadcast them at scheduled intervals.
657 /// Failing to confirm a package translate as a loss of funds for the user.
658 #[derive(Clone, PartialEq, Eq)]
659 pub struct PackageTemplate {
660         // List of onchain outputs and solving data to generate satisfying witnesses.
661         inputs: Vec<(BitcoinOutPoint, PackageSolvingData)>,
662         // Packages are deemed as malleable if we have local knwoledge of at least one set of
663         // private keys yielding a satisfying witnesses. Malleability implies that we can aggregate
664         // packages among them to save on fees or rely on RBF to bump their feerates.
665         // Untractable packages have been counter-signed and thus imply that we can't aggregate
666         // them without breaking signatures. Fee-bumping strategy will also rely on CPFP.
667         malleability: PackageMalleability,
668         // Block height after which the earlier-output belonging to this package is mature for a
669         // competing claim by the counterparty. As our chain tip becomes nearer from the timelock,
670         // the fee-bumping frequency will increase. See `OnchainTxHandler::get_height_timer`.
671         soonest_conf_deadline: u32,
672         // Determines if this package can be aggregated.
673         // Timelocked outputs belonging to the same transaction might have differing
674         // satisfying heights. Picking up the later height among the output set would be a valid
675         // aggregable strategy but it comes with at least 2 trade-offs :
676         // * earlier-output fund are going to take longer to come back
677         // * CLTV delta backing up a corresponding HTLC on an upstream channel could be swallowed
678         // by the requirement of the later-output part of the set
679         // For now, we mark such timelocked outputs as non-aggregable, though we might introduce
680         // smarter aggregable strategy in the future.
681         aggregable: bool,
682         // Cache of package feerate committed at previous (re)broadcast. If bumping resources
683         // (either claimed output value or external utxo), it will keep increasing until holder
684         // or counterparty successful claim.
685         feerate_previous: u64,
686         // Cache of next height at which fee-bumping and rebroadcast will be attempted. In
687         // the future, we might abstract it to an observed mempool fluctuation.
688         height_timer: u32,
689         // Confirmation height of the claimed outputs set transaction. In case of reorg reaching
690         // it, we wipe out and forget the package.
691         height_original: u32,
692 }
693
694 impl PackageTemplate {
695         pub(crate) fn is_malleable(&self) -> bool {
696                 self.malleability == PackageMalleability::Malleable
697         }
698         pub(crate) fn timelock(&self) -> u32 {
699                 self.soonest_conf_deadline
700         }
701         pub(crate) fn aggregable(&self) -> bool {
702                 self.aggregable
703         }
704         pub(crate) fn previous_feerate(&self) -> u64 {
705                 self.feerate_previous
706         }
707         pub(crate) fn set_feerate(&mut self, new_feerate: u64) {
708                 self.feerate_previous = new_feerate;
709         }
710         pub(crate) fn timer(&self) -> u32 {
711                 self.height_timer
712         }
713         pub(crate) fn set_timer(&mut self, new_timer: u32) {
714                 self.height_timer = new_timer;
715         }
716         pub(crate) fn outpoints(&self) -> Vec<&BitcoinOutPoint> {
717                 self.inputs.iter().map(|(o, _)| o).collect()
718         }
719         pub(crate) fn inputs(&self) -> impl ExactSizeIterator<Item = &PackageSolvingData> {
720                 self.inputs.iter().map(|(_, i)| i)
721         }
722         pub(crate) fn split_package(&mut self, split_outp: &BitcoinOutPoint) -> Option<PackageTemplate> {
723                 match self.malleability {
724                         PackageMalleability::Malleable => {
725                                 let mut split_package = None;
726                                 let timelock = self.soonest_conf_deadline;
727                                 let aggregable = self.aggregable;
728                                 let feerate_previous = self.feerate_previous;
729                                 let height_timer = self.height_timer;
730                                 let height_original = self.height_original;
731                                 self.inputs.retain(|outp| {
732                                         if *split_outp == outp.0 {
733                                                 split_package = Some(PackageTemplate {
734                                                         inputs: vec![(outp.0, outp.1.clone())],
735                                                         malleability: PackageMalleability::Malleable,
736                                                         soonest_conf_deadline: timelock,
737                                                         aggregable,
738                                                         feerate_previous,
739                                                         height_timer,
740                                                         height_original,
741                                                 });
742                                                 return false;
743                                         }
744                                         return true;
745                                 });
746                                 return split_package;
747                         },
748                         _ => {
749                                 // Note, we may try to split on remote transaction for
750                                 // which we don't have a competing one (HTLC-Success before
751                                 // timelock expiration). This explain we don't panic!
752                                 // We should refactor OnchainTxHandler::block_connected to
753                                 // only test equality on competing claims.
754                                 return None;
755                         }
756                 }
757         }
758         pub(crate) fn merge_package(&mut self, mut merge_from: PackageTemplate) {
759                 assert_eq!(self.height_original, merge_from.height_original);
760                 if self.malleability == PackageMalleability::Untractable || merge_from.malleability == PackageMalleability::Untractable {
761                         panic!("Merging template on untractable packages");
762                 }
763                 if !self.aggregable || !merge_from.aggregable {
764                         panic!("Merging non aggregatable packages");
765                 }
766                 if let Some((_, lead_input)) = self.inputs.first() {
767                         for (_, v) in merge_from.inputs.iter() {
768                                 if !lead_input.is_compatible(v) { panic!("Merging outputs from differing types !"); }
769                         }
770                 } else { panic!("Merging template on an empty package"); }
771                 for (k, v) in merge_from.inputs.drain(..) {
772                         self.inputs.push((k, v));
773                 }
774                 //TODO: verify coverage and sanity?
775                 if self.soonest_conf_deadline > merge_from.soonest_conf_deadline {
776                         self.soonest_conf_deadline = merge_from.soonest_conf_deadline;
777                 }
778                 if self.feerate_previous > merge_from.feerate_previous {
779                         self.feerate_previous = merge_from.feerate_previous;
780                 }
781                 self.height_timer = cmp::min(self.height_timer, merge_from.height_timer);
782         }
783         /// Gets the amount of all outptus being spent by this package, only valid for malleable
784         /// packages.
785         pub(crate) fn package_amount(&self) -> u64 {
786                 let mut amounts = 0;
787                 for (_, outp) in self.inputs.iter() {
788                         amounts += outp.amount();
789                 }
790                 amounts
791         }
792         pub(crate) fn package_locktime(&self, current_height: u32) -> u32 {
793                 let locktime = self.inputs.iter().map(|(_, outp)| outp.absolute_tx_timelock(current_height))
794                         .max().expect("There must always be at least one output to spend in a PackageTemplate");
795
796                 // If we ever try to aggregate a `HolderHTLCOutput`s with another output type, we'll likely
797                 // end up with an incorrect transaction locktime since the counterparty has included it in
798                 // its HTLC signature. This should never happen unless we decide to aggregate outputs across
799                 // different channel commitments.
800                 #[cfg(debug_assertions)] {
801                         if self.inputs.iter().any(|(_, outp)|
802                                 if let PackageSolvingData::HolderHTLCOutput(outp) = outp {
803                                         outp.preimage.is_some()
804                                 } else {
805                                         false
806                                 }
807                         ) {
808                                 debug_assert_eq!(locktime, 0);
809                         };
810                         for timeout_htlc_expiry in self.inputs.iter().filter_map(|(_, outp)|
811                                 if let PackageSolvingData::HolderHTLCOutput(outp) = outp {
812                                         if outp.preimage.is_none() {
813                                                 Some(outp.cltv_expiry)
814                                         } else { None }
815                                 } else { None }
816                         ) {
817                                 debug_assert_eq!(locktime, timeout_htlc_expiry);
818                         }
819                 }
820
821                 locktime
822         }
823         pub(crate) fn package_weight(&self, destination_script: &Script) -> usize {
824                 let mut inputs_weight = 0;
825                 let mut witnesses_weight = 2; // count segwit flags
826                 for (_, outp) in self.inputs.iter() {
827                         // previous_out_point: 36 bytes ; var_int: 1 byte ; sequence: 4 bytes
828                         inputs_weight += 41 * WITNESS_SCALE_FACTOR;
829                         witnesses_weight += outp.weight();
830                 }
831                 // version: 4 bytes ; count_tx_in: 1 byte ; count_tx_out: 1 byte ; lock_time: 4 bytes
832                 let transaction_weight = 10 * WITNESS_SCALE_FACTOR;
833                 // value: 8 bytes ; var_int: 1 byte ; pk_script: `destination_script.len()`
834                 let output_weight = (8 + 1 + destination_script.len()) * WITNESS_SCALE_FACTOR;
835                 inputs_weight + witnesses_weight + transaction_weight + output_weight
836         }
837         #[cfg(anchors)]
838         pub(crate) fn construct_malleable_package_with_external_funding<Signer: WriteableEcdsaChannelSigner>(
839                 &self, onchain_handler: &mut OnchainTxHandler<Signer>,
840         ) -> Option<Vec<ExternalHTLCClaim>> {
841                 debug_assert!(self.requires_external_funding());
842                 let mut htlcs: Option<Vec<ExternalHTLCClaim>> = None;
843                 for (previous_output, input) in &self.inputs {
844                         match input {
845                                 PackageSolvingData::HolderHTLCOutput(ref outp) => {
846                                         debug_assert!(outp.channel_type_features.supports_anchors_zero_fee_htlc_tx());
847                                         onchain_handler.generate_external_htlc_claim(&previous_output, &outp.preimage).map(|htlc| {
848                                                 htlcs.get_or_insert_with(|| Vec::with_capacity(self.inputs.len())).push(htlc);
849                                         });
850                                 }
851                                 _ => debug_assert!(false, "Expected HolderHTLCOutputs to not be aggregated with other input types"),
852                         }
853                 }
854                 htlcs
855         }
856         pub(crate) fn finalize_malleable_package<L: Deref, Signer: WriteableEcdsaChannelSigner>(
857                 &self, current_height: u32, onchain_handler: &mut OnchainTxHandler<Signer>, value: u64,
858                 destination_script: Script, logger: &L
859         ) -> Option<Transaction> where L::Target: Logger {
860                 debug_assert!(self.is_malleable());
861                 let mut bumped_tx = Transaction {
862                         version: 2,
863                         lock_time: PackedLockTime(self.package_locktime(current_height)),
864                         input: vec![],
865                         output: vec![TxOut {
866                                 script_pubkey: destination_script,
867                                 value,
868                         }],
869                 };
870                 for (outpoint, _) in self.inputs.iter() {
871                         bumped_tx.input.push(TxIn {
872                                 previous_output: *outpoint,
873                                 script_sig: Script::new(),
874                                 sequence: Sequence::ENABLE_RBF_NO_LOCKTIME,
875                                 witness: Witness::new(),
876                         });
877                 }
878                 for (i, (outpoint, out)) in self.inputs.iter().enumerate() {
879                         log_debug!(logger, "Adding claiming input for outpoint {}:{}", outpoint.txid, outpoint.vout);
880                         if !out.finalize_input(&mut bumped_tx, i, onchain_handler) { return None; }
881                 }
882                 log_debug!(logger, "Finalized transaction {} ready to broadcast", bumped_tx.txid());
883                 Some(bumped_tx)
884         }
885         pub(crate) fn finalize_untractable_package<L: Deref, Signer: WriteableEcdsaChannelSigner>(
886                 &self, onchain_handler: &mut OnchainTxHandler<Signer>, logger: &L,
887         ) -> Option<Transaction> where L::Target: Logger {
888                 debug_assert!(!self.is_malleable());
889                 if let Some((outpoint, outp)) = self.inputs.first() {
890                         if let Some(final_tx) = outp.get_finalized_tx(outpoint, onchain_handler) {
891                                 log_debug!(logger, "Adding claiming input for outpoint {}:{}", outpoint.txid, outpoint.vout);
892                                 log_debug!(logger, "Finalized transaction {} ready to broadcast", final_tx.txid());
893                                 return Some(final_tx);
894                         }
895                         return None;
896                 } else { panic!("API Error: Package must not be inputs empty"); }
897         }
898         /// In LN, output claimed are time-sensitive, which means we have to spend them before reaching some timelock expiration. At in-channel
899         /// output detection, we generate a first version of a claim tx and associate to it a height timer. A height timer is an absolute block
900         /// height that once reached we should generate a new bumped "version" of the claim tx to be sure that we safely claim outputs before
901         /// that our counterparty can do so. If timelock expires soon, height timer is going to be scaled down in consequence to increase
902         /// frequency of the bump and so increase our bets of success.
903         pub(crate) fn get_height_timer(&self, current_height: u32) -> u32 {
904                 if self.soonest_conf_deadline <= current_height + MIDDLE_FREQUENCY_BUMP_INTERVAL {
905                         return current_height + HIGH_FREQUENCY_BUMP_INTERVAL
906                 } else if self.soonest_conf_deadline - current_height <= LOW_FREQUENCY_BUMP_INTERVAL {
907                         return current_height + MIDDLE_FREQUENCY_BUMP_INTERVAL
908                 }
909                 current_height + LOW_FREQUENCY_BUMP_INTERVAL
910         }
911
912         /// Returns value in satoshis to be included as package outgoing output amount and feerate
913         /// which was used to generate the value. Will not return less than `dust_limit_sats` for the
914         /// value.
915         pub(crate) fn compute_package_output<F: Deref, L: Deref>(
916                 &self, predicted_weight: usize, dust_limit_sats: u64, force_feerate_bump: bool,
917                 fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L,
918         ) -> Option<(u64, u64)>
919         where
920                 F::Target: FeeEstimator,
921                 L::Target: Logger,
922         {
923                 debug_assert!(self.malleability == PackageMalleability::Malleable, "The package output is fixed for non-malleable packages");
924                 let input_amounts = self.package_amount();
925                 assert!(dust_limit_sats as i64 > 0, "Output script must be broadcastable/have a 'real' dust limit.");
926                 // If old feerate is 0, first iteration of this claim, use normal fee calculation
927                 if self.feerate_previous != 0 {
928                         if let Some((new_fee, feerate)) = feerate_bump(
929                                 predicted_weight, input_amounts, self.feerate_previous, force_feerate_bump,
930                                 fee_estimator, logger,
931                         ) {
932                                 return Some((cmp::max(input_amounts as i64 - new_fee as i64, dust_limit_sats as i64) as u64, feerate));
933                         }
934                 } else {
935                         if let Some((new_fee, feerate)) = compute_fee_from_spent_amounts(input_amounts, predicted_weight, fee_estimator, logger) {
936                                 return Some((cmp::max(input_amounts as i64 - new_fee as i64, dust_limit_sats as i64) as u64, feerate));
937                         }
938                 }
939                 None
940         }
941
942         #[cfg(anchors)]
943         /// Computes a feerate based on the given confirmation target. If a previous feerate was used,
944         /// the new feerate is below it, and `force_feerate_bump` is set, we'll use a 25% increase of
945         /// the previous feerate instead of the new feerate.
946         pub(crate) fn compute_package_feerate<F: Deref>(
947                 &self, fee_estimator: &LowerBoundedFeeEstimator<F>, conf_target: ConfirmationTarget,
948                 force_feerate_bump: bool,
949         ) -> u32 where F::Target: FeeEstimator {
950                 let feerate_estimate = fee_estimator.bounded_sat_per_1000_weight(conf_target);
951                 if self.feerate_previous != 0 {
952                         // If old feerate inferior to actual one given back by Fee Estimator, use it to compute new fee...
953                         if feerate_estimate as u64 > self.feerate_previous {
954                                 feerate_estimate
955                         } else if !force_feerate_bump {
956                                 self.feerate_previous.try_into().unwrap_or(u32::max_value())
957                         } else {
958                                 // ...else just increase the previous feerate by 25% (because that's a nice number)
959                                 (self.feerate_previous + (self.feerate_previous / 4)).try_into().unwrap_or(u32::max_value())
960                         }
961                 } else {
962                         feerate_estimate
963                 }
964         }
965
966         /// Determines whether a package contains an input which must have additional external inputs
967         /// attached to help the spending transaction reach confirmation.
968         pub(crate) fn requires_external_funding(&self) -> bool {
969                 self.inputs.iter().find(|input| match input.1 {
970                         PackageSolvingData::HolderFundingOutput(ref outp) => outp.channel_type_features.supports_anchors_zero_fee_htlc_tx(),
971                         PackageSolvingData::HolderHTLCOutput(ref outp) => outp.channel_type_features.supports_anchors_zero_fee_htlc_tx(),
972                         _ => false,
973                 }).is_some()
974         }
975
976         pub (crate) fn build_package(txid: Txid, vout: u32, input_solving_data: PackageSolvingData, soonest_conf_deadline: u32, height_original: u32) -> Self {
977                 let (malleability, aggregable) = PackageSolvingData::map_output_type_flags(&input_solving_data);
978                 let mut inputs = Vec::with_capacity(1);
979                 inputs.push((BitcoinOutPoint { txid, vout }, input_solving_data));
980                 PackageTemplate {
981                         inputs,
982                         malleability,
983                         soonest_conf_deadline,
984                         aggregable,
985                         feerate_previous: 0,
986                         height_timer: height_original,
987                         height_original,
988                 }
989         }
990 }
991
992 impl Writeable for PackageTemplate {
993         fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
994                 writer.write_all(&(self.inputs.len() as u64).to_be_bytes())?;
995                 for (ref outpoint, ref rev_outp) in self.inputs.iter() {
996                         outpoint.write(writer)?;
997                         rev_outp.write(writer)?;
998                 }
999                 write_tlv_fields!(writer, {
1000                         (0, self.soonest_conf_deadline, required),
1001                         (2, self.feerate_previous, required),
1002                         (4, self.height_original, required),
1003                         (6, self.height_timer, required)
1004                 });
1005                 Ok(())
1006         }
1007 }
1008
1009 impl Readable for PackageTemplate {
1010         fn read<R: io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
1011                 let inputs_count = <u64 as Readable>::read(reader)?;
1012                 let mut inputs: Vec<(BitcoinOutPoint, PackageSolvingData)> = Vec::with_capacity(cmp::min(inputs_count as usize, MAX_ALLOC_SIZE / 128));
1013                 for _ in 0..inputs_count {
1014                         let outpoint = Readable::read(reader)?;
1015                         let rev_outp = Readable::read(reader)?;
1016                         inputs.push((outpoint, rev_outp));
1017                 }
1018                 let (malleability, aggregable) = if let Some((_, lead_input)) = inputs.first() {
1019                         PackageSolvingData::map_output_type_flags(&lead_input)
1020                 } else { return Err(DecodeError::InvalidValue); };
1021                 let mut soonest_conf_deadline = 0;
1022                 let mut feerate_previous = 0;
1023                 let mut height_timer = None;
1024                 let mut height_original = 0;
1025                 read_tlv_fields!(reader, {
1026                         (0, soonest_conf_deadline, required),
1027                         (2, feerate_previous, required),
1028                         (4, height_original, required),
1029                         (6, height_timer, option),
1030                 });
1031                 if height_timer.is_none() {
1032                         height_timer = Some(height_original);
1033                 }
1034                 Ok(PackageTemplate {
1035                         inputs,
1036                         malleability,
1037                         soonest_conf_deadline,
1038                         aggregable,
1039                         feerate_previous,
1040                         height_timer: height_timer.unwrap(),
1041                         height_original,
1042                 })
1043         }
1044 }
1045
1046 /// Attempt to propose a bumping fee for a transaction from its spent output's values and predicted
1047 /// weight. We start with the highest priority feerate returned by the node's fee estimator then
1048 /// fall-back to lower priorities until we have enough value available to suck from.
1049 ///
1050 /// If the proposed fee is less than the available spent output's values, we return the proposed
1051 /// fee and the corresponding updated feerate. If the proposed fee is equal or more than the
1052 /// available spent output's values, we return nothing
1053 fn compute_fee_from_spent_amounts<F: Deref, L: Deref>(input_amounts: u64, predicted_weight: usize, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L) -> Option<(u64, u64)>
1054         where F::Target: FeeEstimator,
1055               L::Target: Logger,
1056 {
1057         let mut updated_feerate = fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::HighPriority) as u64;
1058         let mut fee = updated_feerate * (predicted_weight as u64) / 1000;
1059         if input_amounts <= fee {
1060                 updated_feerate = fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::Normal) as u64;
1061                 fee = updated_feerate * (predicted_weight as u64) / 1000;
1062                 if input_amounts <= fee {
1063                         updated_feerate = fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::Background) as u64;
1064                         fee = updated_feerate * (predicted_weight as u64) / 1000;
1065                         if input_amounts <= fee {
1066                                 log_error!(logger, "Failed to generate an on-chain punishment tx as even low priority fee ({} sat) was more than the entire claim balance ({} sat)",
1067                                         fee, input_amounts);
1068                                 None
1069                         } else {
1070                                 log_warn!(logger, "Used low priority fee for on-chain punishment tx as high priority fee was more than the entire claim balance ({} sat)",
1071                                         input_amounts);
1072                                 Some((fee, updated_feerate))
1073                         }
1074                 } else {
1075                         log_warn!(logger, "Used medium priority fee for on-chain punishment tx as high priority fee was more than the entire claim balance ({} sat)",
1076                                 input_amounts);
1077                         Some((fee, updated_feerate))
1078                 }
1079         } else {
1080                 Some((fee, updated_feerate))
1081         }
1082 }
1083
1084 /// Attempt to propose a bumping fee for a transaction from its spent output's values and predicted
1085 /// weight. If feerates proposed by the fee-estimator have been increasing since last fee-bumping
1086 /// attempt, use them. If `force_feerate_bump` is set, we bump the feerate by 25% of the previous
1087 /// feerate, or just use the previous feerate otherwise. If a feerate bump did happen, we also
1088 /// verify that those bumping heuristics respect BIP125 rules 3) and 4) and if required adjust the
1089 /// new fee to meet the RBF policy requirement.
1090 fn feerate_bump<F: Deref, L: Deref>(
1091         predicted_weight: usize, input_amounts: u64, previous_feerate: u64, force_feerate_bump: bool,
1092         fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L,
1093 ) -> Option<(u64, u64)>
1094 where
1095         F::Target: FeeEstimator,
1096         L::Target: Logger,
1097 {
1098         // If old feerate inferior to actual one given back by Fee Estimator, use it to compute new fee...
1099         let (new_fee, new_feerate) = if let Some((new_fee, new_feerate)) = compute_fee_from_spent_amounts(input_amounts, predicted_weight, fee_estimator, logger) {
1100                 if new_feerate > previous_feerate {
1101                         (new_fee, new_feerate)
1102                 } else if !force_feerate_bump {
1103                         let previous_fee = previous_feerate * (predicted_weight as u64) / 1000;
1104                         (previous_fee, previous_feerate)
1105                 } else {
1106                         // ...else just increase the previous feerate by 25% (because that's a nice number)
1107                         let bumped_feerate = previous_feerate + (previous_feerate / 4);
1108                         let bumped_fee = bumped_feerate * (predicted_weight as u64) / 1000;
1109                         if input_amounts <= bumped_fee {
1110                                 log_warn!(logger, "Can't 25% bump new claiming tx, amount {} is too small", input_amounts);
1111                                 return None;
1112                         }
1113                         (bumped_fee, bumped_feerate)
1114                 }
1115         } else {
1116                 log_warn!(logger, "Can't new-estimation bump new claiming tx, amount {} is too small", input_amounts);
1117                 return None;
1118         };
1119
1120         // Our feerates should never decrease. If it hasn't changed though, we just need to
1121         // rebroadcast/re-sign the previous claim.
1122         debug_assert!(new_feerate >= previous_feerate);
1123         if new_feerate == previous_feerate {
1124                 return Some((new_fee, new_feerate));
1125         }
1126
1127         let previous_fee = previous_feerate * (predicted_weight as u64) / 1000;
1128         let min_relay_fee = MIN_RELAY_FEE_SAT_PER_1000_WEIGHT * (predicted_weight as u64) / 1000;
1129         // BIP 125 Opt-in Full Replace-by-Fee Signaling
1130         //      * 3. The replacement transaction pays an absolute fee of at least the sum paid by the original transactions.
1131         //      * 4. The replacement transaction must also pay for its own bandwidth at or above the rate set by the node's minimum relay fee setting.
1132         let new_fee = if new_fee < previous_fee + min_relay_fee {
1133                 new_fee + previous_fee + min_relay_fee - new_fee
1134         } else {
1135                 new_fee
1136         };
1137         Some((new_fee, new_fee * 1000 / (predicted_weight as u64)))
1138 }
1139
1140 #[cfg(test)]
1141 mod tests {
1142         use crate::chain::package::{CounterpartyOfferedHTLCOutput, CounterpartyReceivedHTLCOutput, HolderHTLCOutput, PackageTemplate, PackageSolvingData, RevokedOutput, WEIGHT_REVOKED_OUTPUT, weight_offered_htlc, weight_received_htlc};
1143         use crate::chain::Txid;
1144         use crate::ln::chan_utils::HTLCOutputInCommitment;
1145         use crate::ln::{PaymentPreimage, PaymentHash};
1146
1147         use bitcoin::blockdata::constants::WITNESS_SCALE_FACTOR;
1148         use bitcoin::blockdata::script::Script;
1149         use bitcoin::blockdata::transaction::OutPoint as BitcoinOutPoint;
1150
1151         use bitcoin::hashes::hex::FromHex;
1152
1153         use bitcoin::secp256k1::{PublicKey,SecretKey};
1154         use bitcoin::secp256k1::Secp256k1;
1155         use crate::ln::features::ChannelTypeFeatures;
1156
1157         macro_rules! dumb_revk_output {
1158                 ($secp_ctx: expr, $is_counterparty_balance_on_anchors: expr) => {
1159                         {
1160                                 let dumb_scalar = SecretKey::from_slice(&hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap();
1161                                 let dumb_point = PublicKey::from_secret_key(&$secp_ctx, &dumb_scalar);
1162                                 PackageSolvingData::RevokedOutput(RevokedOutput::build(dumb_point, dumb_point, dumb_point, dumb_scalar, 0, 0, $is_counterparty_balance_on_anchors))
1163                         }
1164                 }
1165         }
1166
1167         macro_rules! dumb_counterparty_output {
1168                 ($secp_ctx: expr, $amt: expr, $opt_anchors: expr) => {
1169                         {
1170                                 let dumb_scalar = SecretKey::from_slice(&hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap();
1171                                 let dumb_point = PublicKey::from_secret_key(&$secp_ctx, &dumb_scalar);
1172                                 let hash = PaymentHash([1; 32]);
1173                                 let htlc = HTLCOutputInCommitment { offered: true, amount_msat: $amt, cltv_expiry: 0, payment_hash: hash, transaction_output_index: None };
1174                                 PackageSolvingData::CounterpartyReceivedHTLCOutput(CounterpartyReceivedHTLCOutput::build(dumb_point, dumb_point, dumb_point, htlc, $opt_anchors))
1175                         }
1176                 }
1177         }
1178
1179         macro_rules! dumb_counterparty_offered_output {
1180                 ($secp_ctx: expr, $amt: expr, $opt_anchors: expr) => {
1181                         {
1182                                 let dumb_scalar = SecretKey::from_slice(&hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap();
1183                                 let dumb_point = PublicKey::from_secret_key(&$secp_ctx, &dumb_scalar);
1184                                 let hash = PaymentHash([1; 32]);
1185                                 let preimage = PaymentPreimage([2;32]);
1186                                 let htlc = HTLCOutputInCommitment { offered: false, amount_msat: $amt, cltv_expiry: 1000, payment_hash: hash, transaction_output_index: None };
1187                                 PackageSolvingData::CounterpartyOfferedHTLCOutput(CounterpartyOfferedHTLCOutput::build(dumb_point, dumb_point, dumb_point, preimage, htlc, $opt_anchors))
1188                         }
1189                 }
1190         }
1191
1192         macro_rules! dumb_htlc_output {
1193                 () => {
1194                         {
1195                                 let preimage = PaymentPreimage([2;32]);
1196                                 PackageSolvingData::HolderHTLCOutput(HolderHTLCOutput::build_accepted(preimage, 0, ChannelTypeFeatures::only_static_remote_key()))
1197                         }
1198                 }
1199         }
1200
1201         #[test]
1202         #[should_panic]
1203         fn test_package_differing_heights() {
1204                 let txid = Txid::from_hex("c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e").unwrap();
1205                 let secp_ctx = Secp256k1::new();
1206                 let revk_outp = dumb_revk_output!(secp_ctx, false);
1207
1208                 let mut package_one_hundred = PackageTemplate::build_package(txid, 0, revk_outp.clone(), 1000, 100);
1209                 let package_two_hundred = PackageTemplate::build_package(txid, 1, revk_outp.clone(), 1000, 200);
1210                 package_one_hundred.merge_package(package_two_hundred);
1211         }
1212
1213         #[test]
1214         #[should_panic]
1215         fn test_package_untractable_merge_to() {
1216                 let txid = Txid::from_hex("c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e").unwrap();
1217                 let secp_ctx = Secp256k1::new();
1218                 let revk_outp = dumb_revk_output!(secp_ctx, false);
1219                 let htlc_outp = dumb_htlc_output!();
1220
1221                 let mut untractable_package = PackageTemplate::build_package(txid, 0, revk_outp.clone(), 1000, 100);
1222                 let malleable_package = PackageTemplate::build_package(txid, 1, htlc_outp.clone(), 1000, 100);
1223                 untractable_package.merge_package(malleable_package);
1224         }
1225
1226         #[test]
1227         #[should_panic]
1228         fn test_package_untractable_merge_from() {
1229                 let txid = Txid::from_hex("c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e").unwrap();
1230                 let secp_ctx = Secp256k1::new();
1231                 let htlc_outp = dumb_htlc_output!();
1232                 let revk_outp = dumb_revk_output!(secp_ctx, false);
1233
1234                 let mut malleable_package = PackageTemplate::build_package(txid, 0, htlc_outp.clone(), 1000, 100);
1235                 let untractable_package = PackageTemplate::build_package(txid, 1, revk_outp.clone(), 1000, 100);
1236                 malleable_package.merge_package(untractable_package);
1237         }
1238
1239         #[test]
1240         #[should_panic]
1241         fn test_package_noaggregation_to() {
1242                 let txid = Txid::from_hex("c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e").unwrap();
1243                 let secp_ctx = Secp256k1::new();
1244                 let revk_outp = dumb_revk_output!(secp_ctx, false);
1245                 let revk_outp_counterparty_balance = dumb_revk_output!(secp_ctx, true);
1246
1247                 let mut noaggregation_package = PackageTemplate::build_package(txid, 0, revk_outp_counterparty_balance.clone(), 1000, 100);
1248                 let aggregation_package = PackageTemplate::build_package(txid, 1, revk_outp.clone(), 1000, 100);
1249                 noaggregation_package.merge_package(aggregation_package);
1250         }
1251
1252         #[test]
1253         #[should_panic]
1254         fn test_package_noaggregation_from() {
1255                 let txid = Txid::from_hex("c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e").unwrap();
1256                 let secp_ctx = Secp256k1::new();
1257                 let revk_outp = dumb_revk_output!(secp_ctx, false);
1258                 let revk_outp_counterparty_balance = dumb_revk_output!(secp_ctx, true);
1259
1260                 let mut aggregation_package = PackageTemplate::build_package(txid, 0, revk_outp.clone(), 1000, 100);
1261                 let noaggregation_package = PackageTemplate::build_package(txid, 1, revk_outp_counterparty_balance.clone(), 1000, 100);
1262                 aggregation_package.merge_package(noaggregation_package);
1263         }
1264
1265         #[test]
1266         #[should_panic]
1267         fn test_package_empty() {
1268                 let txid = Txid::from_hex("c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e").unwrap();
1269                 let secp_ctx = Secp256k1::new();
1270                 let revk_outp = dumb_revk_output!(secp_ctx, false);
1271
1272                 let mut empty_package = PackageTemplate::build_package(txid, 0, revk_outp.clone(), 1000, 100);
1273                 empty_package.inputs = vec![];
1274                 let package = PackageTemplate::build_package(txid, 1, revk_outp.clone(), 1000, 100);
1275                 empty_package.merge_package(package);
1276         }
1277
1278         #[test]
1279         #[should_panic]
1280         fn test_package_differing_categories() {
1281                 let txid = Txid::from_hex("c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e").unwrap();
1282                 let secp_ctx = Secp256k1::new();
1283                 let revk_outp = dumb_revk_output!(secp_ctx, false);
1284                 let counterparty_outp = dumb_counterparty_output!(secp_ctx, 0, ChannelTypeFeatures::only_static_remote_key());
1285
1286                 let mut revoked_package = PackageTemplate::build_package(txid, 0, revk_outp, 1000, 100);
1287                 let counterparty_package = PackageTemplate::build_package(txid, 1, counterparty_outp, 1000, 100);
1288                 revoked_package.merge_package(counterparty_package);
1289         }
1290
1291         #[test]
1292         fn test_package_split_malleable() {
1293                 let txid = Txid::from_hex("c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e").unwrap();
1294                 let secp_ctx = Secp256k1::new();
1295                 let revk_outp_one = dumb_revk_output!(secp_ctx, false);
1296                 let revk_outp_two = dumb_revk_output!(secp_ctx, false);
1297                 let revk_outp_three = dumb_revk_output!(secp_ctx, false);
1298
1299                 let mut package_one = PackageTemplate::build_package(txid, 0, revk_outp_one, 1000, 100);
1300                 let package_two = PackageTemplate::build_package(txid, 1, revk_outp_two, 1000, 100);
1301                 let package_three = PackageTemplate::build_package(txid, 2, revk_outp_three, 1000, 100);
1302
1303                 package_one.merge_package(package_two);
1304                 package_one.merge_package(package_three);
1305                 assert_eq!(package_one.outpoints().len(), 3);
1306
1307                 if let Some(split_package) = package_one.split_package(&BitcoinOutPoint { txid, vout: 1 }) {
1308                         // Packages attributes should be identical
1309                         assert!(split_package.is_malleable());
1310                         assert_eq!(split_package.soonest_conf_deadline, package_one.soonest_conf_deadline);
1311                         assert_eq!(split_package.aggregable, package_one.aggregable);
1312                         assert_eq!(split_package.feerate_previous, package_one.feerate_previous);
1313                         assert_eq!(split_package.height_timer, package_one.height_timer);
1314                         assert_eq!(split_package.height_original, package_one.height_original);
1315                 } else { panic!(); }
1316                 assert_eq!(package_one.outpoints().len(), 2);
1317         }
1318
1319         #[test]
1320         fn test_package_split_untractable() {
1321                 let txid = Txid::from_hex("c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e").unwrap();
1322                 let htlc_outp_one = dumb_htlc_output!();
1323
1324                 let mut package_one = PackageTemplate::build_package(txid, 0, htlc_outp_one, 1000, 100);
1325                 let ret_split = package_one.split_package(&BitcoinOutPoint { txid, vout: 0});
1326                 assert!(ret_split.is_none());
1327         }
1328
1329         #[test]
1330         fn test_package_timer() {
1331                 let txid = Txid::from_hex("c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e").unwrap();
1332                 let secp_ctx = Secp256k1::new();
1333                 let revk_outp = dumb_revk_output!(secp_ctx, false);
1334
1335                 let mut package = PackageTemplate::build_package(txid, 0, revk_outp, 1000, 100);
1336                 assert_eq!(package.timer(), 100);
1337                 package.set_timer(101);
1338                 assert_eq!(package.timer(), 101);
1339         }
1340
1341         #[test]
1342         fn test_package_amounts() {
1343                 let txid = Txid::from_hex("c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e").unwrap();
1344                 let secp_ctx = Secp256k1::new();
1345                 let counterparty_outp = dumb_counterparty_output!(secp_ctx, 1_000_000, ChannelTypeFeatures::only_static_remote_key());
1346
1347                 let package = PackageTemplate::build_package(txid, 0, counterparty_outp, 1000, 100);
1348                 assert_eq!(package.package_amount(), 1000);
1349         }
1350
1351         #[test]
1352         fn test_package_weight() {
1353                 let txid = Txid::from_hex("c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e").unwrap();
1354                 let secp_ctx = Secp256k1::new();
1355
1356                 // (nVersion (4) + nLocktime (4) + count_tx_in (1) + prevout (36) + sequence (4) + script_length (1) + count_tx_out (1) + value (8) + var_int (1)) * WITNESS_SCALE_FACTOR + witness marker (2)
1357                 let weight_sans_output = (4 + 4 + 1 + 36 + 4 + 1 + 1 + 8 + 1) * WITNESS_SCALE_FACTOR + 2;
1358
1359                 {
1360                         let revk_outp = dumb_revk_output!(secp_ctx, false);
1361                         let package = PackageTemplate::build_package(txid, 0, revk_outp, 0, 100);
1362                         assert_eq!(package.package_weight(&Script::new()),  weight_sans_output + WEIGHT_REVOKED_OUTPUT as usize);
1363                 }
1364
1365                 {
1366                         for channel_type_features in [ChannelTypeFeatures::only_static_remote_key(), ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies()].iter() {
1367                                 let counterparty_outp = dumb_counterparty_output!(secp_ctx, 1_000_000, channel_type_features.clone());
1368                                 let package = PackageTemplate::build_package(txid, 0, counterparty_outp, 1000, 100);
1369                                 assert_eq!(package.package_weight(&Script::new()), weight_sans_output + weight_received_htlc(channel_type_features) as usize);
1370                         }
1371                 }
1372
1373                 {
1374                         for channel_type_features in [ChannelTypeFeatures::only_static_remote_key(), ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies()].iter() {
1375                                 let counterparty_outp = dumb_counterparty_offered_output!(secp_ctx, 1_000_000, channel_type_features.clone());
1376                                 let package = PackageTemplate::build_package(txid, 0, counterparty_outp, 1000, 100);
1377                                 assert_eq!(package.package_weight(&Script::new()), weight_sans_output + weight_offered_htlc(channel_type_features) as usize);
1378                         }
1379                 }
1380         }
1381 }