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