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