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