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