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