Always pass height to OnchainTxHandler::update_claims_view
[rust-lightning] / lightning / src / chain / onchaintx.rs
index 131caff7a0d107f94afebf36ce672b24808832d3..fc4805b3391eba3b62a683e947918ca8bd6eab10 100644 (file)
@@ -28,11 +28,11 @@ use chain::chaininterface::{FeeEstimator, BroadcasterInterface};
 use chain::channelmonitor::{ANTI_REORG_DELAY, CLTV_SHARED_CLAIM_BUFFER};
 use chain::keysinterface::{Sign, KeysInterface};
 use chain::package::PackageTemplate;
-use chain::package;
 use util::logger::Logger;
 use util::ser::{Readable, ReadableArgs, Writer, Writeable, VecWriter};
 use util::byte_utils;
 
+use prelude::*;
 use std::collections::HashMap;
 use core::cmp;
 use core::ops::Deref;
@@ -167,7 +167,6 @@ pub struct OnchainTxHandler<ChannelSigner: Sign> {
 
        onchain_events_awaiting_threshold_conf: Vec<OnchainEventEntry>,
 
-       latest_height: u32,
 
        pub(super) secp_ctx: Secp256k1<secp256k1::All>,
 }
@@ -222,7 +221,6 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
                                }
                        }
                }
-               self.latest_height.write(writer)?;
 
                write_tlv_fields!(writer, {}, {});
                Ok(())
@@ -289,7 +287,6 @@ impl<'a, K: KeysInterface> ReadableArgs<&'a K> for OnchainTxHandler<K::Signer> {
                        };
                        onchain_events_awaiting_threshold_conf.push(OnchainEventEntry { txid, height, event });
                }
-               let latest_height = Readable::read(reader)?;
 
                read_tlv_fields!(reader, {}, {});
 
@@ -307,7 +304,6 @@ impl<'a, K: KeysInterface> ReadableArgs<&'a K> for OnchainTxHandler<K::Signer> {
                        claimable_outpoints,
                        pending_claim_requests,
                        onchain_events_awaiting_threshold_conf,
-                       latest_height,
                        secp_ctx,
                })
        }
@@ -326,26 +322,11 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
                        pending_claim_requests: HashMap::new(),
                        claimable_outpoints: HashMap::new(),
                        onchain_events_awaiting_threshold_conf: Vec::new(),
-                       latest_height: 0,
 
                        secp_ctx,
                }
        }
 
-       /// In LN, output claimed are time-sensitive, which means we have to spend them before reaching some timelock expiration. At in-channel
-       /// 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
-       /// height than once reached we should generate a new bumped "version" of the claim tx to be sure than we safely claim outputs before
-       /// than our counterparty can do it too. If timelock expires soon, height timer is going to be scale down in consequence to increase
-       /// frequency of the bump and so increase our bets of success.
-       fn get_height_timer(current_height: u32, timelock_expiration: u32) -> u32 {
-               if timelock_expiration <= current_height + 3 {
-                       return current_height + 1
-               } else if timelock_expiration - current_height <= 15 {
-                       return current_height + 3
-               }
-               current_height + 15
-       }
-
        /// Lightning security model (i.e being able to redeem/timeout HTLC or penalize coutnerparty onchain) lays on the assumption of claim transactions getting confirmed before timelock expiration
        /// (CSV or CLTV following cases). In case of high-fee spikes, claim tx may stuck in the mempool, so you need to bump its feerate quickly using Replace-By-Fee or Child-Pay-For-Parent.
        /// Panics if there are signing errors, because signing operations in reaction to on-chain events
@@ -358,11 +339,10 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
 
                // Compute new height timer to decide when we need to regenerate a new bumped version of the claim tx (if we
                // didn't receive confirmation of it before, or not enough reorg-safe depth on top of it).
-               let new_timer = Some(Self::get_height_timer(height, cached_request.timelock()));
-               let amt = cached_request.package_amount();
+               let new_timer = Some(cached_request.get_height_timer(height));
                if cached_request.is_malleable() {
                        let predicted_weight = cached_request.package_weight(&self.destination_script);
-                       if let Some((output_value, new_feerate)) = package::compute_output_value(predicted_weight, amt, cached_request.feerate(), fee_estimator, logger) {
+                       if let Some((output_value, new_feerate)) = cached_request.compute_package_output(predicted_weight, fee_estimator, logger) {
                                assert!(new_feerate != 0);
 
                                let transaction = cached_request.finalize_package(self, output_value, self.destination_script.clone(), logger).unwrap();
@@ -374,8 +354,7 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
                        // Note: Currently, amounts of holder outputs spending witnesses aren't used
                        // as we can't malleate spending package to increase their feerate. This
                        // should change with the remaining anchor output patchset.
-                       debug_assert!(amt == 0);
-                       if let Some(transaction) = cached_request.finalize_package(self, amt, self.destination_script.clone(), logger) {
+                       if let Some(transaction) = cached_request.finalize_package(self, 0, self.destination_script.clone(), logger) {
                                return Some((None, 0, transaction));
                        }
                }
@@ -386,15 +365,11 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
        /// for this channel, provide new relevant on-chain transactions and/or new claim requests.
        /// Formerly this was named `block_connected`, but it is now also used for claiming an HTLC output
        /// if we receive a preimage after force-close.
-       pub(crate) fn update_claims_view<B: Deref, F: Deref, L: Deref>(&mut self, txn_matched: &[&Transaction], requests: Vec<PackageTemplate>, latest_height: Option<u32>, broadcaster: &B, fee_estimator: &F, logger: &L)
+       pub(crate) fn update_claims_view<B: Deref, F: Deref, L: Deref>(&mut self, txn_matched: &[&Transaction], requests: Vec<PackageTemplate>, height: u32, broadcaster: &B, fee_estimator: &F, logger: &L)
                where B::Target: BroadcasterInterface,
                      F::Target: FeeEstimator,
                                        L::Target: Logger,
        {
-               let height = match latest_height {
-                       Some(h) => h,
-                       None => self.latest_height,
-               };
                log_trace!(logger, "Updating claims view at height {} with {} matched transactions and {} claim requests", height, txn_matched.len(), requests.len());
                let mut preprocessed_requests = Vec::with_capacity(requests.len());
                let mut aggregated_request = None;