1 // This file is Copyright its original authors, visible in version control
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
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.
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;
19 use bitcoin::hash_types::Txid;
21 use bitcoin::secp256k1::{SecretKey,PublicKey};
23 use ln::PaymentPreimage;
24 use ln::chan_utils::{TxCreationKeys, HTLCOutputInCommitment};
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;
31 use util::logger::Logger;
32 use util::ser::{Readable, Writer, Writeable};
41 use super::chaininterface::LowerBoundedFeeEstimator;
43 const MAX_ALLOC_SIZE: usize = 64*1024;
46 pub(crate) fn weight_revoked_offered_htlc(opt_anchors: bool) -> u64 {
47 // number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script
48 const WEIGHT_REVOKED_OFFERED_HTLC: u64 = 1 + 1 + 73 + 1 + 33 + 1 + 133;
49 const WEIGHT_REVOKED_OFFERED_HTLC_ANCHORS: u64 = WEIGHT_REVOKED_OFFERED_HTLC + 3; // + OP_1 + OP_CSV + OP_DROP
50 if opt_anchors { WEIGHT_REVOKED_OFFERED_HTLC_ANCHORS } else { WEIGHT_REVOKED_OFFERED_HTLC }
53 pub(crate) fn weight_revoked_received_htlc(opt_anchors: bool) -> u64 {
54 // number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script
55 const WEIGHT_REVOKED_RECEIVED_HTLC: u64 = 1 + 1 + 73 + 1 + 33 + 1 + 139;
56 const WEIGHT_REVOKED_RECEIVED_HTLC_ANCHORS: u64 = WEIGHT_REVOKED_RECEIVED_HTLC + 3; // + OP_1 + OP_CSV + OP_DROP
57 if opt_anchors { WEIGHT_REVOKED_RECEIVED_HTLC_ANCHORS } else { WEIGHT_REVOKED_RECEIVED_HTLC }
60 pub(crate) fn weight_offered_htlc(opt_anchors: bool) -> u64 {
61 // number_of_witness_elements + sig_length + counterpartyhtlc_sig + preimage_length + preimage + witness_script_length + witness_script
62 const WEIGHT_OFFERED_HTLC: u64 = 1 + 1 + 73 + 1 + 32 + 1 + 133;
63 const WEIGHT_OFFERED_HTLC_ANCHORS: u64 = WEIGHT_OFFERED_HTLC + 3; // + OP_1 + OP_CSV + OP_DROP
64 if opt_anchors { WEIGHT_OFFERED_HTLC_ANCHORS } else { WEIGHT_OFFERED_HTLC }
67 pub(crate) fn weight_received_htlc(opt_anchors: bool) -> u64 {
68 // number_of_witness_elements + sig_length + counterpartyhtlc_sig + empty_vec_length + empty_vec + witness_script_length + witness_script
69 const WEIGHT_RECEIVED_HTLC: u64 = 1 + 1 + 73 + 1 + 1 + 1 + 139;
70 const WEIGHT_RECEIVED_HTLC_ANCHORS: u64 = WEIGHT_RECEIVED_HTLC + 3; // + OP_1 + OP_CSV + OP_DROP
71 if opt_anchors { WEIGHT_RECEIVED_HTLC_ANCHORS } else { WEIGHT_RECEIVED_HTLC }
74 // number_of_witness_elements + sig_length + revocation_sig + true_length + op_true + witness_script_length + witness_script
75 pub(crate) const WEIGHT_REVOKED_OUTPUT: u64 = 1 + 1 + 73 + 1 + 1 + 1 + 77;
77 /// Height delay at which transactions are fee-bumped/rebroadcasted with a low priority.
78 const LOW_FREQUENCY_BUMP_INTERVAL: u32 = 15;
79 /// Height delay at which transactions are fee-bumped/rebroadcasted with a middle priority.
80 const MIDDLE_FREQUENCY_BUMP_INTERVAL: u32 = 3;
81 /// Height delay at which transactions are fee-bumped/rebroadcasted with a high priority.
82 const HIGH_FREQUENCY_BUMP_INTERVAL: u32 = 1;
84 /// A struct to describe a revoked output and corresponding information to generate a solving
85 /// witness spending a commitment `to_local` output or a second-stage HTLC transaction output.
87 /// CSV and pubkeys are used as part of a witnessScript redeeming a balance output, amount is used
88 /// as part of the signature hash and revocation secret to generate a satisfying witness.
89 #[derive(Clone, PartialEq)]
90 pub(crate) struct RevokedOutput {
91 per_commitment_point: PublicKey,
92 counterparty_delayed_payment_base_key: PublicKey,
93 counterparty_htlc_base_key: PublicKey,
94 per_commitment_key: SecretKey,
97 on_counterparty_tx_csv: u16,
101 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 {
103 per_commitment_point,
104 counterparty_delayed_payment_base_key,
105 counterparty_htlc_base_key,
107 weight: WEIGHT_REVOKED_OUTPUT,
109 on_counterparty_tx_csv
114 impl_writeable_tlv_based!(RevokedOutput, {
115 (0, per_commitment_point, required),
116 (2, counterparty_delayed_payment_base_key, required),
117 (4, counterparty_htlc_base_key, required),
118 (6, per_commitment_key, required),
119 (8, weight, required),
120 (10, amount, required),
121 (12, on_counterparty_tx_csv, required),
124 /// A struct to describe a revoked offered output and corresponding information to generate a
127 /// HTLCOuputInCommitment (hash timelock, direction) and pubkeys are used to generate a suitable
130 /// CSV is used as part of a witnessScript redeeming a balance output, amount is used as part
131 /// of the signature hash and revocation secret to generate a satisfying witness.
132 #[derive(Clone, PartialEq)]
133 pub(crate) struct RevokedHTLCOutput {
134 per_commitment_point: PublicKey,
135 counterparty_delayed_payment_base_key: PublicKey,
136 counterparty_htlc_base_key: PublicKey,
137 per_commitment_key: SecretKey,
140 htlc: HTLCOutputInCommitment,
143 impl RevokedHTLCOutput {
144 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 {
145 let weight = if htlc.offered { weight_revoked_offered_htlc(opt_anchors) } else { weight_revoked_received_htlc(opt_anchors) };
147 per_commitment_point,
148 counterparty_delayed_payment_base_key,
149 counterparty_htlc_base_key,
158 impl_writeable_tlv_based!(RevokedHTLCOutput, {
159 (0, per_commitment_point, required),
160 (2, counterparty_delayed_payment_base_key, required),
161 (4, counterparty_htlc_base_key, required),
162 (6, per_commitment_key, required),
163 (8, weight, required),
164 (10, amount, required),
165 (12, htlc, required),
168 /// A struct to describe a HTLC output on a counterparty commitment transaction.
170 /// HTLCOutputInCommitment (hash, timelock, directon) and pubkeys are used to generate a suitable
173 /// The preimage is used as part of the witness.
174 #[derive(Clone, PartialEq)]
175 pub(crate) struct CounterpartyOfferedHTLCOutput {
176 per_commitment_point: PublicKey,
177 counterparty_delayed_payment_base_key: PublicKey,
178 counterparty_htlc_base_key: PublicKey,
179 preimage: PaymentPreimage,
180 htlc: HTLCOutputInCommitment
183 impl CounterpartyOfferedHTLCOutput {
184 pub(crate) fn build(per_commitment_point: PublicKey, counterparty_delayed_payment_base_key: PublicKey, counterparty_htlc_base_key: PublicKey, preimage: PaymentPreimage, htlc: HTLCOutputInCommitment) -> Self {
185 CounterpartyOfferedHTLCOutput {
186 per_commitment_point,
187 counterparty_delayed_payment_base_key,
188 counterparty_htlc_base_key,
195 impl_writeable_tlv_based!(CounterpartyOfferedHTLCOutput, {
196 (0, per_commitment_point, required),
197 (2, counterparty_delayed_payment_base_key, required),
198 (4, counterparty_htlc_base_key, required),
199 (6, preimage, required),
203 /// A struct to describe a HTLC output on a counterparty commitment transaction.
205 /// HTLCOutputInCommitment (hash, timelock, directon) and pubkeys are used to generate a suitable
207 #[derive(Clone, PartialEq)]
208 pub(crate) struct CounterpartyReceivedHTLCOutput {
209 per_commitment_point: PublicKey,
210 counterparty_delayed_payment_base_key: PublicKey,
211 counterparty_htlc_base_key: PublicKey,
212 htlc: HTLCOutputInCommitment
215 impl CounterpartyReceivedHTLCOutput {
216 pub(crate) fn build(per_commitment_point: PublicKey, counterparty_delayed_payment_base_key: PublicKey, counterparty_htlc_base_key: PublicKey, htlc: HTLCOutputInCommitment) -> Self {
217 CounterpartyReceivedHTLCOutput {
218 per_commitment_point,
219 counterparty_delayed_payment_base_key,
220 counterparty_htlc_base_key,
226 impl_writeable_tlv_based!(CounterpartyReceivedHTLCOutput, {
227 (0, per_commitment_point, required),
228 (2, counterparty_delayed_payment_base_key, required),
229 (4, counterparty_htlc_base_key, required),
233 /// A struct to describe a HTLC output on holder commitment transaction.
235 /// Either offered or received, the amount is always used as part of the bip143 sighash.
236 /// Preimage is only included as part of the witness in former case.
237 #[derive(Clone, PartialEq)]
238 pub(crate) struct HolderHTLCOutput {
239 preimage: Option<PaymentPreimage>,
241 /// Defaults to 0 for HTLC-Success transactions, which have no expiry
245 impl HolderHTLCOutput {
246 pub(crate) fn build_offered(amount: u64, cltv_expiry: u32) -> Self {
254 pub(crate) fn build_accepted(preimage: PaymentPreimage, amount: u64) -> Self {
256 preimage: Some(preimage),
263 impl_writeable_tlv_based!(HolderHTLCOutput, {
264 (0, amount, required),
265 (2, cltv_expiry, required),
266 (4, preimage, option)
269 /// A struct to describe the channel output on the funding transaction.
271 /// witnessScript is used as part of the witness redeeming the funding utxo.
272 #[derive(Clone, PartialEq)]
273 pub(crate) struct HolderFundingOutput {
274 funding_redeemscript: Script,
277 impl HolderFundingOutput {
278 pub(crate) fn build(funding_redeemscript: Script) -> Self {
279 HolderFundingOutput {
280 funding_redeemscript,
285 impl_writeable_tlv_based!(HolderFundingOutput, {
286 (0, funding_redeemscript, required),
289 /// A wrapper encapsulating all in-protocol differing outputs types.
291 /// The generic API offers access to an outputs common attributes or allow transformation such as
292 /// finalizing an input claiming the output.
293 #[derive(Clone, PartialEq)]
294 pub(crate) enum PackageSolvingData {
295 RevokedOutput(RevokedOutput),
296 RevokedHTLCOutput(RevokedHTLCOutput),
297 CounterpartyOfferedHTLCOutput(CounterpartyOfferedHTLCOutput),
298 CounterpartyReceivedHTLCOutput(CounterpartyReceivedHTLCOutput),
299 HolderHTLCOutput(HolderHTLCOutput),
300 HolderFundingOutput(HolderFundingOutput),
303 impl PackageSolvingData {
304 fn amount(&self) -> u64 {
305 let amt = match self {
306 PackageSolvingData::RevokedOutput(ref outp) => { outp.amount },
307 PackageSolvingData::RevokedHTLCOutput(ref outp) => { outp.amount },
308 PackageSolvingData::CounterpartyOfferedHTLCOutput(ref outp) => { outp.htlc.amount_msat / 1000 },
309 PackageSolvingData::CounterpartyReceivedHTLCOutput(ref outp) => { outp.htlc.amount_msat / 1000 },
310 // Note: Currently, amounts of holder outputs spending witnesses aren't used
311 // as we can't malleate spending package to increase their feerate. This
312 // should change with the remaining anchor output patchset.
313 PackageSolvingData::HolderHTLCOutput(..) => { unreachable!() },
314 PackageSolvingData::HolderFundingOutput(..) => { unreachable!() },
318 fn weight(&self, opt_anchors: bool) -> usize {
319 let weight = match self {
320 PackageSolvingData::RevokedOutput(ref outp) => { outp.weight as usize },
321 PackageSolvingData::RevokedHTLCOutput(ref outp) => { outp.weight as usize },
322 PackageSolvingData::CounterpartyOfferedHTLCOutput(..) => { weight_offered_htlc(opt_anchors) as usize },
323 PackageSolvingData::CounterpartyReceivedHTLCOutput(..) => { weight_received_htlc(opt_anchors) as usize },
324 // Note: Currently, weights of holder outputs spending witnesses aren't used
325 // as we can't malleate spending package to increase their feerate. This
326 // should change with the remaining anchor output patchset.
327 PackageSolvingData::HolderHTLCOutput(..) => { unreachable!() },
328 PackageSolvingData::HolderFundingOutput(..) => { unreachable!() },
332 fn is_compatible(&self, input: &PackageSolvingData) -> bool {
334 PackageSolvingData::RevokedOutput(..) => {
336 PackageSolvingData::RevokedHTLCOutput(..) => { true },
337 PackageSolvingData::RevokedOutput(..) => { true },
341 PackageSolvingData::RevokedHTLCOutput(..) => {
343 PackageSolvingData::RevokedOutput(..) => { true },
344 PackageSolvingData::RevokedHTLCOutput(..) => { true },
348 _ => { mem::discriminant(self) == mem::discriminant(&input) }
351 fn finalize_input<Signer: Sign>(&self, bumped_tx: &mut Transaction, i: usize, onchain_handler: &mut OnchainTxHandler<Signer>) -> bool {
353 PackageSolvingData::RevokedOutput(ref outp) => {
354 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) {
355 let witness_script = chan_utils::get_revokeable_redeemscript(&chan_keys.revocation_key, outp.on_counterparty_tx_csv, &chan_keys.broadcaster_delayed_payment_key);
356 //TODO: should we panic on signer failure ?
357 if let Ok(sig) = onchain_handler.signer.sign_justice_revoked_output(&bumped_tx, i, outp.amount, &outp.per_commitment_key, &onchain_handler.secp_ctx) {
358 let mut ser_sig = sig.serialize_der().to_vec();
359 ser_sig.push(EcdsaSighashType::All as u8);
360 bumped_tx.input[i].witness.push(ser_sig);
361 bumped_tx.input[i].witness.push(vec!(1));
362 bumped_tx.input[i].witness.push(witness_script.clone().into_bytes());
363 } else { return false; }
366 PackageSolvingData::RevokedHTLCOutput(ref outp) => {
367 if let Ok(chan_keys) = TxCreationKeys::derive_new(&onchain_handler.secp_ctx, &outp.per_commitment_point, &outp.counterparty_delayed_payment_base_key, &outp.counterparty_htlc_base_key, &onchain_handler.signer.pubkeys().revocation_basepoint, &onchain_handler.signer.pubkeys().htlc_basepoint) {
368 let witness_script = chan_utils::get_htlc_redeemscript_with_explicit_keys(&outp.htlc, onchain_handler.opt_anchors(), &chan_keys.broadcaster_htlc_key, &chan_keys.countersignatory_htlc_key, &chan_keys.revocation_key);
369 //TODO: should we panic on signer failure ?
370 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) {
371 let mut ser_sig = sig.serialize_der().to_vec();
372 ser_sig.push(EcdsaSighashType::All as u8);
373 bumped_tx.input[i].witness.push(ser_sig);
374 bumped_tx.input[i].witness.push(chan_keys.revocation_key.clone().serialize().to_vec());
375 bumped_tx.input[i].witness.push(witness_script.clone().into_bytes());
376 } else { return false; }
379 PackageSolvingData::CounterpartyOfferedHTLCOutput(ref outp) => {
380 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) {
381 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);
383 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) {
384 let mut ser_sig = sig.serialize_der().to_vec();
385 ser_sig.push(EcdsaSighashType::All as u8);
386 bumped_tx.input[i].witness.push(ser_sig);
387 bumped_tx.input[i].witness.push(outp.preimage.0.to_vec());
388 bumped_tx.input[i].witness.push(witness_script.clone().into_bytes());
392 PackageSolvingData::CounterpartyReceivedHTLCOutput(ref outp) => {
393 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) {
394 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);
396 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
397 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) {
398 let mut ser_sig = sig.serialize_der().to_vec();
399 ser_sig.push(EcdsaSighashType::All as u8);
400 bumped_tx.input[i].witness.push(ser_sig);
401 // Due to BIP146 (MINIMALIF) this must be a zero-length element to relay.
402 bumped_tx.input[i].witness.push(vec![]);
403 bumped_tx.input[i].witness.push(witness_script.clone().into_bytes());
407 _ => { panic!("API Error!"); }
411 fn get_finalized_tx<Signer: Sign>(&self, outpoint: &BitcoinOutPoint, onchain_handler: &mut OnchainTxHandler<Signer>) -> Option<Transaction> {
413 PackageSolvingData::HolderHTLCOutput(ref outp) => { return onchain_handler.get_fully_signed_htlc_tx(outpoint, &outp.preimage); }
414 PackageSolvingData::HolderFundingOutput(ref outp) => { return Some(onchain_handler.get_fully_signed_holder_tx(&outp.funding_redeemscript)); }
415 _ => { panic!("API Error!"); }
418 fn absolute_tx_timelock(&self, output_conf_height: u32) -> u32 {
419 // Get the absolute timelock at which this output can be spent given the height at which
420 // this output was confirmed. We use `output_conf_height + 1` as a safe default as we can
421 // be confirmed in the next block and transactions with time lock `current_height + 1`
423 let absolute_timelock = match self {
424 PackageSolvingData::RevokedOutput(_) => output_conf_height + 1,
425 PackageSolvingData::RevokedHTLCOutput(_) => output_conf_height + 1,
426 PackageSolvingData::CounterpartyOfferedHTLCOutput(_) => output_conf_height + 1,
427 PackageSolvingData::CounterpartyReceivedHTLCOutput(ref outp) => cmp::max(outp.htlc.cltv_expiry, output_conf_height + 1),
428 PackageSolvingData::HolderHTLCOutput(ref outp) => cmp::max(outp.cltv_expiry, output_conf_height + 1),
429 PackageSolvingData::HolderFundingOutput(_) => output_conf_height + 1,
435 impl_writeable_tlv_based_enum!(PackageSolvingData, ;
437 (1, RevokedHTLCOutput),
438 (2, CounterpartyOfferedHTLCOutput),
439 (3, CounterpartyReceivedHTLCOutput),
440 (4, HolderHTLCOutput),
441 (5, HolderFundingOutput),
444 /// A malleable package might be aggregated with other packages to save on fees.
445 /// A untractable package has been counter-signed and aggregable will break cached counterparty
447 #[derive(Clone, PartialEq)]
448 pub(crate) enum PackageMalleability {
453 /// A structure to describe a package content that is generated by ChannelMonitor and
454 /// used by OnchainTxHandler to generate and broadcast transactions settling onchain claims.
456 /// A package is defined as one or more transactions claiming onchain outputs in reaction
457 /// to confirmation of a channel transaction. Those packages might be aggregated to save on
458 /// fees, if satisfaction of outputs's witnessScript let's us do so.
460 /// As packages are time-sensitive, we fee-bump and rebroadcast them at scheduled intervals.
461 /// Failing to confirm a package translate as a loss of funds for the user.
462 #[derive(Clone, PartialEq)]
463 pub struct PackageTemplate {
464 // List of onchain outputs and solving data to generate satisfying witnesses.
465 inputs: Vec<(BitcoinOutPoint, PackageSolvingData)>,
466 // Packages are deemed as malleable if we have local knwoledge of at least one set of
467 // private keys yielding a satisfying witnesses. Malleability implies that we can aggregate
468 // packages among them to save on fees or rely on RBF to bump their feerates.
469 // Untractable packages have been counter-signed and thus imply that we can't aggregate
470 // them without breaking signatures. Fee-bumping strategy will also rely on CPFP.
471 malleability: PackageMalleability,
472 // Block height after which the earlier-output belonging to this package is mature for a
473 // competing claim by the counterparty. As our chain tip becomes nearer from the timelock,
474 // the fee-bumping frequency will increase. See `OnchainTxHandler::get_height_timer`.
475 soonest_conf_deadline: u32,
476 // Determines if this package can be aggregated.
477 // Timelocked outputs belonging to the same transaction might have differing
478 // satisfying heights. Picking up the later height among the output set would be a valid
479 // aggregable strategy but it comes with at least 2 trade-offs :
480 // * earlier-output fund are going to take longer to come back
481 // * CLTV delta backing up a corresponding HTLC on an upstream channel could be swallowed
482 // by the requirement of the later-output part of the set
483 // For now, we mark such timelocked outputs as non-aggregable, though we might introduce
484 // smarter aggregable strategy in the future.
486 // Cache of package feerate committed at previous (re)broadcast. If bumping resources
487 // (either claimed output value or external utxo), it will keep increasing until holder
488 // or counterparty successful claim.
489 feerate_previous: u64,
490 // Cache of next height at which fee-bumping and rebroadcast will be attempted. In
491 // the future, we might abstract it to an observed mempool fluctuation.
492 height_timer: Option<u32>,
493 // Confirmation height of the claimed outputs set transaction. In case of reorg reaching
494 // it, we wipe out and forget the package.
495 height_original: u32,
498 impl PackageTemplate {
499 pub(crate) fn is_malleable(&self) -> bool {
500 self.malleability == PackageMalleability::Malleable
502 pub(crate) fn timelock(&self) -> u32 {
503 self.soonest_conf_deadline
505 pub(crate) fn aggregable(&self) -> bool {
508 pub(crate) fn set_feerate(&mut self, new_feerate: u64) {
509 self.feerate_previous = new_feerate;
511 pub(crate) fn timer(&self) -> Option<u32> {
512 if let Some(ref timer) = self.height_timer {
517 pub(crate) fn set_timer(&mut self, new_timer: Option<u32>) {
518 self.height_timer = new_timer;
520 pub(crate) fn outpoints(&self) -> Vec<&BitcoinOutPoint> {
521 self.inputs.iter().map(|(o, _)| o).collect()
523 pub(crate) fn split_package(&mut self, split_outp: &BitcoinOutPoint) -> Option<PackageTemplate> {
524 match self.malleability {
525 PackageMalleability::Malleable => {
526 let mut split_package = None;
527 let timelock = self.soonest_conf_deadline;
528 let aggregable = self.aggregable;
529 let feerate_previous = self.feerate_previous;
530 let height_timer = self.height_timer;
531 let height_original = self.height_original;
532 self.inputs.retain(|outp| {
533 if *split_outp == outp.0 {
534 split_package = Some(PackageTemplate {
535 inputs: vec![(outp.0, outp.1.clone())],
536 malleability: PackageMalleability::Malleable,
537 soonest_conf_deadline: timelock,
547 return split_package;
550 // Note, we may try to split on remote transaction for
551 // which we don't have a competing one (HTLC-Success before
552 // timelock expiration). This explain we don't panic!
553 // We should refactor OnchainTxHandler::block_connected to
554 // only test equality on competing claims.
559 pub(crate) fn merge_package(&mut self, mut merge_from: PackageTemplate) {
560 assert_eq!(self.height_original, merge_from.height_original);
561 if self.malleability == PackageMalleability::Untractable || merge_from.malleability == PackageMalleability::Untractable {
562 panic!("Merging template on untractable packages");
564 if !self.aggregable || !merge_from.aggregable {
565 panic!("Merging non aggregatable packages");
567 if let Some((_, lead_input)) = self.inputs.first() {
568 for (_, v) in merge_from.inputs.iter() {
569 if !lead_input.is_compatible(v) { panic!("Merging outputs from differing types !"); }
571 } else { panic!("Merging template on an empty package"); }
572 for (k, v) in merge_from.inputs.drain(..) {
573 self.inputs.push((k, v));
575 //TODO: verify coverage and sanity?
576 if self.soonest_conf_deadline > merge_from.soonest_conf_deadline {
577 self.soonest_conf_deadline = merge_from.soonest_conf_deadline;
579 if self.feerate_previous > merge_from.feerate_previous {
580 self.feerate_previous = merge_from.feerate_previous;
582 self.height_timer = cmp::min(self.height_timer, merge_from.height_timer);
584 /// Gets the amount of all outptus being spent by this package, only valid for malleable
586 fn package_amount(&self) -> u64 {
588 for (_, outp) in self.inputs.iter() {
589 amounts += outp.amount();
593 pub(crate) fn package_timelock(&self) -> u32 {
594 self.inputs.iter().map(|(_, outp)| outp.absolute_tx_timelock(self.height_original))
595 .max().expect("There must always be at least one output to spend in a PackageTemplate")
597 pub(crate) fn package_weight(&self, destination_script: &Script, opt_anchors: bool) -> usize {
598 let mut inputs_weight = 0;
599 let mut witnesses_weight = 2; // count segwit flags
600 for (_, outp) in self.inputs.iter() {
601 // previous_out_point: 36 bytes ; var_int: 1 byte ; sequence: 4 bytes
602 inputs_weight += 41 * WITNESS_SCALE_FACTOR;
603 witnesses_weight += outp.weight(opt_anchors);
605 // version: 4 bytes ; count_tx_in: 1 byte ; count_tx_out: 1 byte ; lock_time: 4 bytes
606 let transaction_weight = 10 * WITNESS_SCALE_FACTOR;
607 // value: 8 bytes ; var_int: 1 byte ; pk_script: `destination_script.len()`
608 let output_weight = (8 + 1 + destination_script.len()) * WITNESS_SCALE_FACTOR;
609 inputs_weight + witnesses_weight + transaction_weight + output_weight
611 pub(crate) fn finalize_package<L: Deref, Signer: Sign>(&self, onchain_handler: &mut OnchainTxHandler<Signer>, value: u64, destination_script: Script, logger: &L) -> Option<Transaction>
612 where L::Target: Logger,
614 match self.malleability {
615 PackageMalleability::Malleable => {
616 let mut bumped_tx = Transaction {
621 script_pubkey: destination_script,
625 for (outpoint, _) in self.inputs.iter() {
626 bumped_tx.input.push(TxIn {
627 previous_output: *outpoint,
628 script_sig: Script::new(),
629 sequence: 0xfffffffd,
630 witness: Witness::new(),
633 for (i, (outpoint, out)) in self.inputs.iter().enumerate() {
634 log_debug!(logger, "Adding claiming input for outpoint {}:{}", outpoint.txid, outpoint.vout);
635 if !out.finalize_input(&mut bumped_tx, i, onchain_handler) { return None; }
637 log_debug!(logger, "Finalized transaction {} ready to broadcast", bumped_tx.txid());
638 return Some(bumped_tx);
640 PackageMalleability::Untractable => {
641 debug_assert_eq!(value, 0, "value is ignored for non-malleable packages, should be zero to ensure callsites are correct");
642 if let Some((outpoint, outp)) = self.inputs.first() {
643 if let Some(final_tx) = outp.get_finalized_tx(outpoint, onchain_handler) {
644 log_debug!(logger, "Adding claiming input for outpoint {}:{}", outpoint.txid, outpoint.vout);
645 log_debug!(logger, "Finalized transaction {} ready to broadcast", final_tx.txid());
646 return Some(final_tx);
649 } else { panic!("API Error: Package must not be inputs empty"); }
653 /// In LN, output claimed are time-sensitive, which means we have to spend them before reaching some timelock expiration. At in-channel
654 /// 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
655 /// height that once reached we should generate a new bumped "version" of the claim tx to be sure that we safely claim outputs before
656 /// that our counterparty can do so. If timelock expires soon, height timer is going to be scaled down in consequence to increase
657 /// frequency of the bump and so increase our bets of success.
658 pub(crate) fn get_height_timer(&self, current_height: u32) -> u32 {
659 if self.soonest_conf_deadline <= current_height + MIDDLE_FREQUENCY_BUMP_INTERVAL {
660 return current_height + HIGH_FREQUENCY_BUMP_INTERVAL
661 } else if self.soonest_conf_deadline - current_height <= LOW_FREQUENCY_BUMP_INTERVAL {
662 return current_height + MIDDLE_FREQUENCY_BUMP_INTERVAL
664 current_height + LOW_FREQUENCY_BUMP_INTERVAL
667 /// Returns value in satoshis to be included as package outgoing output amount and feerate
668 /// which was used to generate the value. Will not return less than `dust_limit_sats` for the
670 pub(crate) fn compute_package_output<F: Deref, L: Deref>(&self, predicted_weight: usize, dust_limit_sats: u64, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L) -> Option<(u64, u64)>
671 where F::Target: FeeEstimator,
674 debug_assert!(self.malleability == PackageMalleability::Malleable, "The package output is fixed for non-malleable packages");
675 let input_amounts = self.package_amount();
676 assert!(dust_limit_sats as i64 > 0, "Output script must be broadcastable/have a 'real' dust limit.");
677 // If old feerate is 0, first iteration of this claim, use normal fee calculation
678 if self.feerate_previous != 0 {
679 if let Some((new_fee, feerate)) = feerate_bump(predicted_weight, input_amounts, self.feerate_previous, fee_estimator, logger) {
680 return Some((cmp::max(input_amounts as i64 - new_fee as i64, dust_limit_sats as i64) as u64, feerate));
683 if let Some((new_fee, feerate)) = compute_fee_from_spent_amounts(input_amounts, predicted_weight, fee_estimator, logger) {
684 return Some((cmp::max(input_amounts as i64 - new_fee as i64, dust_limit_sats as i64) as u64, feerate));
689 pub (crate) fn build_package(txid: Txid, vout: u32, input_solving_data: PackageSolvingData, soonest_conf_deadline: u32, aggregable: bool, height_original: u32) -> Self {
690 let malleability = match input_solving_data {
691 PackageSolvingData::RevokedOutput(..) => { PackageMalleability::Malleable },
692 PackageSolvingData::RevokedHTLCOutput(..) => { PackageMalleability::Malleable },
693 PackageSolvingData::CounterpartyOfferedHTLCOutput(..) => { PackageMalleability::Malleable },
694 PackageSolvingData::CounterpartyReceivedHTLCOutput(..) => { PackageMalleability::Malleable },
695 PackageSolvingData::HolderHTLCOutput(..) => { PackageMalleability::Untractable },
696 PackageSolvingData::HolderFundingOutput(..) => { PackageMalleability::Untractable },
698 let mut inputs = Vec::with_capacity(1);
699 inputs.push((BitcoinOutPoint { txid, vout }, input_solving_data));
703 soonest_conf_deadline,
712 impl Writeable for PackageTemplate {
713 fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
714 writer.write_all(&byte_utils::be64_to_array(self.inputs.len() as u64))?;
715 for (ref outpoint, ref rev_outp) in self.inputs.iter() {
716 outpoint.write(writer)?;
717 rev_outp.write(writer)?;
719 write_tlv_fields!(writer, {
720 (0, self.soonest_conf_deadline, required),
721 (2, self.feerate_previous, required),
722 (4, self.height_original, required),
723 (6, self.height_timer, option)
729 impl Readable for PackageTemplate {
730 fn read<R: io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
731 let inputs_count = <u64 as Readable>::read(reader)?;
732 let mut inputs: Vec<(BitcoinOutPoint, PackageSolvingData)> = Vec::with_capacity(cmp::min(inputs_count as usize, MAX_ALLOC_SIZE / 128));
733 for _ in 0..inputs_count {
734 let outpoint = Readable::read(reader)?;
735 let rev_outp = Readable::read(reader)?;
736 inputs.push((outpoint, rev_outp));
738 let (malleability, aggregable) = if let Some((_, lead_input)) = inputs.first() {
740 PackageSolvingData::RevokedOutput(..) => { (PackageMalleability::Malleable, true) },
741 PackageSolvingData::RevokedHTLCOutput(..) => { (PackageMalleability::Malleable, true) },
742 PackageSolvingData::CounterpartyOfferedHTLCOutput(..) => { (PackageMalleability::Malleable, true) },
743 PackageSolvingData::CounterpartyReceivedHTLCOutput(..) => { (PackageMalleability::Malleable, false) },
744 PackageSolvingData::HolderHTLCOutput(..) => { (PackageMalleability::Untractable, false) },
745 PackageSolvingData::HolderFundingOutput(..) => { (PackageMalleability::Untractable, false) },
747 } else { return Err(DecodeError::InvalidValue); };
748 let mut soonest_conf_deadline = 0;
749 let mut feerate_previous = 0;
750 let mut height_timer = None;
751 let mut height_original = 0;
752 read_tlv_fields!(reader, {
753 (0, soonest_conf_deadline, required),
754 (2, feerate_previous, required),
755 (4, height_original, required),
756 (6, height_timer, option),
761 soonest_conf_deadline,
770 /// Attempt to propose a bumping fee for a transaction from its spent output's values and predicted
771 /// weight. We start with the highest priority feerate returned by the node's fee estimator then
772 /// fall-back to lower priorities until we have enough value available to suck from.
774 /// If the proposed fee is less than the available spent output's values, we return the proposed
775 /// fee and the corresponding updated feerate. If the proposed fee is equal or more than the
776 /// available spent output's values, we return nothing
777 fn compute_fee_from_spent_amounts<F: Deref, L: Deref>(input_amounts: u64, predicted_weight: usize, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L) -> Option<(u64, u64)>
778 where F::Target: FeeEstimator,
781 let mut updated_feerate = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::HighPriority) as u64;
782 let mut fee = updated_feerate * (predicted_weight as u64) / 1000;
783 if input_amounts <= fee {
784 updated_feerate = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Normal) as u64;
785 fee = updated_feerate * (predicted_weight as u64) / 1000;
786 if input_amounts <= fee {
787 updated_feerate = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Background) as u64;
788 fee = updated_feerate * (predicted_weight as u64) / 1000;
789 if input_amounts <= fee {
790 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)",
794 log_warn!(logger, "Used low priority fee for on-chain punishment tx as high priority fee was more than the entire claim balance ({} sat)",
796 Some((fee, updated_feerate))
799 log_warn!(logger, "Used medium priority fee for on-chain punishment tx as high priority fee was more than the entire claim balance ({} sat)",
801 Some((fee, updated_feerate))
804 Some((fee, updated_feerate))
808 /// Attempt to propose a bumping fee for a transaction from its spent output's values and predicted
809 /// weight. If feerates proposed by the fee-estimator have been increasing since last fee-bumping
810 /// attempt, use them. Otherwise, blindly bump the feerate by 25% of the previous feerate. We also
811 /// verify that those bumping heuristics respect BIP125 rules 3) and 4) and if required adjust
812 /// the new fee to meet the RBF policy requirement.
813 fn feerate_bump<F: Deref, L: Deref>(predicted_weight: usize, input_amounts: u64, previous_feerate: u64, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L) -> Option<(u64, u64)>
814 where F::Target: FeeEstimator,
817 // If old feerate inferior to actual one given back by Fee Estimator, use it to compute new fee...
818 let new_fee = if let Some((new_fee, _)) = compute_fee_from_spent_amounts(input_amounts, predicted_weight, fee_estimator, logger) {
819 let updated_feerate = new_fee / (predicted_weight as u64 * 1000);
820 if updated_feerate > previous_feerate {
823 // ...else just increase the previous feerate by 25% (because that's a nice number)
824 let new_fee = previous_feerate * (predicted_weight as u64) / 750;
825 if input_amounts <= new_fee {
826 log_warn!(logger, "Can't 25% bump new claiming tx, amount {} is too small", input_amounts);
832 log_warn!(logger, "Can't new-estimation bump new claiming tx, amount {} is too small", input_amounts);
836 let previous_fee = previous_feerate * (predicted_weight as u64) / 1000;
837 let min_relay_fee = MIN_RELAY_FEE_SAT_PER_1000_WEIGHT * (predicted_weight as u64) / 1000;
838 // BIP 125 Opt-in Full Replace-by-Fee Signaling
839 // * 3. The replacement transaction pays an absolute fee of at least the sum paid by the original transactions.
840 // * 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.
841 let new_fee = if new_fee < previous_fee + min_relay_fee {
842 new_fee + previous_fee + min_relay_fee - new_fee
846 Some((new_fee, new_fee * 1000 / (predicted_weight as u64)))
851 use chain::package::{CounterpartyOfferedHTLCOutput, CounterpartyReceivedHTLCOutput, HolderHTLCOutput, PackageTemplate, PackageSolvingData, RevokedOutput, WEIGHT_REVOKED_OUTPUT, weight_offered_htlc, weight_received_htlc};
853 use ln::chan_utils::HTLCOutputInCommitment;
854 use ln::{PaymentPreimage, PaymentHash};
856 use bitcoin::blockdata::constants::WITNESS_SCALE_FACTOR;
857 use bitcoin::blockdata::script::Script;
858 use bitcoin::blockdata::transaction::OutPoint as BitcoinOutPoint;
860 use bitcoin::hashes::hex::FromHex;
862 use bitcoin::secp256k1::{PublicKey,SecretKey};
863 use bitcoin::secp256k1::Secp256k1;
865 macro_rules! dumb_revk_output {
866 ($secp_ctx: expr) => {
868 let dumb_scalar = SecretKey::from_slice(&hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap();
869 let dumb_point = PublicKey::from_secret_key(&$secp_ctx, &dumb_scalar);
870 PackageSolvingData::RevokedOutput(RevokedOutput::build(dumb_point, dumb_point, dumb_point, dumb_scalar, 0, 0))
875 macro_rules! dumb_counterparty_output {
876 ($secp_ctx: expr, $amt: expr) => {
878 let dumb_scalar = SecretKey::from_slice(&hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap();
879 let dumb_point = PublicKey::from_secret_key(&$secp_ctx, &dumb_scalar);
880 let hash = PaymentHash([1; 32]);
881 let htlc = HTLCOutputInCommitment { offered: true, amount_msat: $amt, cltv_expiry: 0, payment_hash: hash, transaction_output_index: None };
882 PackageSolvingData::CounterpartyReceivedHTLCOutput(CounterpartyReceivedHTLCOutput::build(dumb_point, dumb_point, dumb_point, htlc))
887 macro_rules! dumb_counterparty_offered_output {
888 ($secp_ctx: expr, $amt: expr) => {
890 let dumb_scalar = SecretKey::from_slice(&hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap();
891 let dumb_point = PublicKey::from_secret_key(&$secp_ctx, &dumb_scalar);
892 let hash = PaymentHash([1; 32]);
893 let preimage = PaymentPreimage([2;32]);
894 let htlc = HTLCOutputInCommitment { offered: false, amount_msat: $amt, cltv_expiry: 1000, payment_hash: hash, transaction_output_index: None };
895 PackageSolvingData::CounterpartyOfferedHTLCOutput(CounterpartyOfferedHTLCOutput::build(dumb_point, dumb_point, dumb_point, preimage, htlc))
900 macro_rules! dumb_htlc_output {
903 let preimage = PaymentPreimage([2;32]);
904 PackageSolvingData::HolderHTLCOutput(HolderHTLCOutput::build_accepted(preimage, 0))
911 fn test_package_differing_heights() {
912 let txid = Txid::from_hex("c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e").unwrap();
913 let secp_ctx = Secp256k1::new();
914 let revk_outp = dumb_revk_output!(secp_ctx);
916 let mut package_one_hundred = PackageTemplate::build_package(txid, 0, revk_outp.clone(), 1000, true, 100);
917 let package_two_hundred = PackageTemplate::build_package(txid, 1, revk_outp.clone(), 1000, true, 200);
918 package_one_hundred.merge_package(package_two_hundred);
923 fn test_package_untractable_merge_to() {
924 let txid = Txid::from_hex("c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e").unwrap();
925 let secp_ctx = Secp256k1::new();
926 let revk_outp = dumb_revk_output!(secp_ctx);
927 let htlc_outp = dumb_htlc_output!();
929 let mut untractable_package = PackageTemplate::build_package(txid, 0, revk_outp.clone(), 1000, true, 100);
930 let malleable_package = PackageTemplate::build_package(txid, 1, htlc_outp.clone(), 1000, true, 100);
931 untractable_package.merge_package(malleable_package);
936 fn test_package_untractable_merge_from() {
937 let txid = Txid::from_hex("c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e").unwrap();
938 let secp_ctx = Secp256k1::new();
939 let htlc_outp = dumb_htlc_output!();
940 let revk_outp = dumb_revk_output!(secp_ctx);
942 let mut malleable_package = PackageTemplate::build_package(txid, 0, htlc_outp.clone(), 1000, true, 100);
943 let untractable_package = PackageTemplate::build_package(txid, 1, revk_outp.clone(), 1000, true, 100);
944 malleable_package.merge_package(untractable_package);
949 fn test_package_noaggregation_to() {
950 let txid = Txid::from_hex("c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e").unwrap();
951 let secp_ctx = Secp256k1::new();
952 let revk_outp = dumb_revk_output!(secp_ctx);
954 let mut noaggregation_package = PackageTemplate::build_package(txid, 0, revk_outp.clone(), 1000, false, 100);
955 let aggregation_package = PackageTemplate::build_package(txid, 1, revk_outp.clone(), 1000, true, 100);
956 noaggregation_package.merge_package(aggregation_package);
961 fn test_package_noaggregation_from() {
962 let txid = Txid::from_hex("c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e").unwrap();
963 let secp_ctx = Secp256k1::new();
964 let revk_outp = dumb_revk_output!(secp_ctx);
966 let mut aggregation_package = PackageTemplate::build_package(txid, 0, revk_outp.clone(), 1000, true, 100);
967 let noaggregation_package = PackageTemplate::build_package(txid, 1, revk_outp.clone(), 1000, false, 100);
968 aggregation_package.merge_package(noaggregation_package);
973 fn test_package_empty() {
974 let txid = Txid::from_hex("c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e").unwrap();
975 let secp_ctx = Secp256k1::new();
976 let revk_outp = dumb_revk_output!(secp_ctx);
978 let mut empty_package = PackageTemplate::build_package(txid, 0, revk_outp.clone(), 1000, true, 100);
979 empty_package.inputs = vec![];
980 let package = PackageTemplate::build_package(txid, 1, revk_outp.clone(), 1000, true, 100);
981 empty_package.merge_package(package);
986 fn test_package_differing_categories() {
987 let txid = Txid::from_hex("c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e").unwrap();
988 let secp_ctx = Secp256k1::new();
989 let revk_outp = dumb_revk_output!(secp_ctx);
990 let counterparty_outp = dumb_counterparty_output!(secp_ctx, 0);
992 let mut revoked_package = PackageTemplate::build_package(txid, 0, revk_outp, 1000, true, 100);
993 let counterparty_package = PackageTemplate::build_package(txid, 1, counterparty_outp, 1000, true, 100);
994 revoked_package.merge_package(counterparty_package);
998 fn test_package_split_malleable() {
999 let txid = Txid::from_hex("c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e").unwrap();
1000 let secp_ctx = Secp256k1::new();
1001 let revk_outp_one = dumb_revk_output!(secp_ctx);
1002 let revk_outp_two = dumb_revk_output!(secp_ctx);
1003 let revk_outp_three = dumb_revk_output!(secp_ctx);
1005 let mut package_one = PackageTemplate::build_package(txid, 0, revk_outp_one, 1000, true, 100);
1006 let package_two = PackageTemplate::build_package(txid, 1, revk_outp_two, 1000, true, 100);
1007 let package_three = PackageTemplate::build_package(txid, 2, revk_outp_three, 1000, true, 100);
1009 package_one.merge_package(package_two);
1010 package_one.merge_package(package_three);
1011 assert_eq!(package_one.outpoints().len(), 3);
1013 if let Some(split_package) = package_one.split_package(&BitcoinOutPoint { txid, vout: 1 }) {
1014 // Packages attributes should be identical
1015 assert!(split_package.is_malleable());
1016 assert_eq!(split_package.soonest_conf_deadline, package_one.soonest_conf_deadline);
1017 assert_eq!(split_package.aggregable, package_one.aggregable);
1018 assert_eq!(split_package.feerate_previous, package_one.feerate_previous);
1019 assert_eq!(split_package.height_timer, package_one.height_timer);
1020 assert_eq!(split_package.height_original, package_one.height_original);
1021 } else { panic!(); }
1022 assert_eq!(package_one.outpoints().len(), 2);
1026 fn test_package_split_untractable() {
1027 let txid = Txid::from_hex("c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e").unwrap();
1028 let htlc_outp_one = dumb_htlc_output!();
1030 let mut package_one = PackageTemplate::build_package(txid, 0, htlc_outp_one, 1000, true, 100);
1031 let ret_split = package_one.split_package(&BitcoinOutPoint { txid, vout: 0});
1032 assert!(ret_split.is_none());
1036 fn test_package_timer() {
1037 let txid = Txid::from_hex("c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e").unwrap();
1038 let secp_ctx = Secp256k1::new();
1039 let revk_outp = dumb_revk_output!(secp_ctx);
1041 let mut package = PackageTemplate::build_package(txid, 0, revk_outp, 1000, true, 100);
1042 let timer_none = package.timer();
1043 assert!(timer_none.is_none());
1044 package.set_timer(Some(100));
1045 if let Some(timer_some) = package.timer() {
1046 assert_eq!(timer_some, 100);
1051 fn test_package_amounts() {
1052 let txid = Txid::from_hex("c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e").unwrap();
1053 let secp_ctx = Secp256k1::new();
1054 let counterparty_outp = dumb_counterparty_output!(secp_ctx, 1_000_000);
1056 let package = PackageTemplate::build_package(txid, 0, counterparty_outp, 1000, true, 100);
1057 assert_eq!(package.package_amount(), 1000);
1061 fn test_package_weight() {
1062 let txid = Txid::from_hex("c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e").unwrap();
1063 let secp_ctx = Secp256k1::new();
1065 // (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)
1066 let weight_sans_output = (4 + 4 + 1 + 36 + 4 + 1 + 1 + 8 + 1) * WITNESS_SCALE_FACTOR + 2;
1069 let revk_outp = dumb_revk_output!(secp_ctx);
1070 let package = PackageTemplate::build_package(txid, 0, revk_outp, 0, true, 100);
1071 for &opt_anchors in [false, true].iter() {
1072 assert_eq!(package.package_weight(&Script::new(), opt_anchors), weight_sans_output + WEIGHT_REVOKED_OUTPUT as usize);
1077 let counterparty_outp = dumb_counterparty_output!(secp_ctx, 1_000_000);
1078 let package = PackageTemplate::build_package(txid, 0, counterparty_outp, 1000, true, 100);
1079 for &opt_anchors in [false, true].iter() {
1080 assert_eq!(package.package_weight(&Script::new(), opt_anchors), weight_sans_output + weight_received_htlc(opt_anchors) as usize);
1085 let counterparty_outp = dumb_counterparty_offered_output!(secp_ctx, 1_000_000);
1086 let package = PackageTemplate::build_package(txid, 0, counterparty_outp, 1000, true, 100);
1087 for &opt_anchors in [false, true].iter() {
1088 assert_eq!(package.package_weight(&Script::new(), opt_anchors), weight_sans_output + weight_offered_htlc(opt_anchors) as usize);