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