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