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