`rustfmt`: Reformat `lightning-transaction-sync/src/common.rs`
[rust-lightning] / lightning-transaction-sync / src / electrum.rs
index 07e11338905370d9385dc7d5259053b624055ea4..4c809d40716f6c3e87cba1b0f80bbec9f047a4bd 100644 (file)
@@ -58,6 +58,8 @@ where
        }
 
        /// Returns a new [`ElectrumSyncClient`] object using the given Electrum client.
+       ///
+       /// This is not exported to bindings users as the underlying client from BDK is not exported.
        pub fn from_client(client: ElectrumClient, logger: L) -> Result<Self, TxSyncError> {
                let sync_state = Mutex::new(SyncState::new());
                let queue = Mutex::new(FilterQueue::new());
@@ -81,11 +83,14 @@ where
        /// [`ChainMonitor`]: lightning::chain::chainmonitor::ChainMonitor
        /// [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager
        /// [`Filter`]: lightning::chain::Filter
-       pub fn sync(&self, confirmables: Vec<&(dyn Confirm + Sync + Send)>) -> Result<(), TxSyncError> {
+       pub fn sync<C: Deref>(&self, confirmables: Vec<C>) -> Result<(), TxSyncError>
+               where C::Target: Confirm
+       {
                // This lock makes sure we're syncing once at a time.
                let mut sync_state = self.sync_state.lock().unwrap();
 
                log_trace!(self.logger, "Starting transaction sync.");
+               #[cfg(feature = "time")]
                let start_time = Instant::now();
                let mut num_confirmed = 0;
                let mut num_unconfirmed = 0;
@@ -156,6 +161,9 @@ where
                                        for c in &confirmables {
                                                c.best_block_updated(&tip_header, tip_height);
                                        }
+
+                                       // Prune any sufficiently confirmed output spends
+                                       sync_state.prune_output_spends(tip_height);
                                }
 
                                match self.get_confirmed_transactions(&sync_state) {
@@ -210,10 +218,15 @@ where
                                sync_state.pending_sync = false;
                        }
                }
+               #[cfg(feature = "time")]
                log_debug!(self.logger,
                        "Finished transaction sync at tip {} in {}ms: {} confirmed, {} unconfirmed.",
                        tip_header.block_hash(), start_time.elapsed().as_millis(), num_confirmed,
                        num_unconfirmed);
+               #[cfg(not(feature = "time"))]
+               log_debug!(self.logger,
+                       "Finished transaction sync at tip {}: {} confirmed, {} unconfirmed.",
+                       tip_header.block_hash(), num_confirmed, num_unconfirmed);
                Ok(())
        }
 
@@ -248,7 +261,7 @@ where
 
                // First, check the confirmation status of registered transactions as well as the
                // status of dependent transactions of registered outputs.
-               let mut confirmed_txs = Vec::new();
+               let mut confirmed_txs: Vec<ConfirmedTx> = Vec::new();
                let mut watched_script_pubkeys = Vec::with_capacity(
                        sync_state.watched_transactions.len() + sync_state.watched_outputs.len());
                let mut watched_txs = Vec::with_capacity(sync_state.watched_transactions.len());
@@ -296,6 +309,9 @@ where
 
                                for (i, script_history) in tx_results.iter().enumerate() {
                                        let (txid, tx) = &watched_txs[i];
+                                       if confirmed_txs.iter().any(|ctx| ctx.txid == **txid) {
+                                               continue;
+                                       }
                                        let mut filtered_history = script_history.iter().filter(|h| h.tx_hash == **txid);
                                        if let Some(history) = filtered_history.next()
                                        {
@@ -315,6 +331,10 @@ where
                                                }
 
                                                let txid = possible_output_spend.tx_hash;
+                                               if confirmed_txs.iter().any(|ctx| ctx.txid == txid) {
+                                                       continue;
+                                               }
+
                                                match self.client.transaction_get(&txid) {
                                                        Ok(tx) => {
                                                                let mut is_spend = false;
@@ -360,9 +380,11 @@ where
                Ok(confirmed_txs)
        }
 
-       fn get_unconfirmed_transactions(
-               &self, confirmables: &Vec<&(dyn Confirm + Sync + Send)>,
-       ) -> Result<Vec<Txid>, InternalError> {
+       fn get_unconfirmed_transactions<C: Deref>(
+               &self, confirmables: &Vec<C>,
+       ) -> Result<Vec<Txid>, InternalError>
+               where C::Target: Confirm
+       {
                // Query the interface for relevant txids and check whether the relevant blocks are still
                // in the best chain, mark them unconfirmed otherwise
                let relevant_txids = confirmables
@@ -410,6 +432,7 @@ where
                                                }
                                                let confirmed_tx = ConfirmedTx {
                                                        tx: tx.clone(),
+                                                       txid,
                                                        block_header, block_height: prob_conf_height,
                                                        pos,
                                                };
@@ -433,6 +456,8 @@ where
        }
 
        /// Returns a reference to the underlying Electrum client.
+       ///
+       /// This is not exported to bindings users as the underlying client from BDK is not exported.
        pub fn client(&self) -> &ElectrumClient {
                &self.client
        }