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