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