Make `get_outputs_to_watch` return a `Vec` instead of a `HashMap`
[rust-lightning] / lightning / src / chain / channelmonitor.rs
index 8a8a5b96d66fbbf9c99105a1dc7c1fadd574ad38..c2c791c4b172b8335c43117ff191e93320c3cddc 100644 (file)
@@ -45,6 +45,7 @@ use chain;
 use chain::chaininterface::{BroadcasterInterface, FeeEstimator};
 use chain::transaction::{OutPoint, TransactionData};
 use chain::keysinterface::{SpendableOutputDescriptor, StaticPaymentOutputDescriptor, DelayedPaymentOutputDescriptor, Sign, KeysInterface};
+use chain::Filter;
 use util::logger::Logger;
 use util::ser::{Readable, ReadableArgs, MaybeReadable, Writer, Writeable, U48};
 use util::byte_utils;
@@ -1165,10 +1166,23 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
 
        /// Gets a list of txids, with their output scripts (in the order they appear in the
        /// transaction), which we must learn about spends of via block_connected().
-       ///
-       /// (C-not exported) because we have no HashMap bindings
-       pub fn get_outputs_to_watch(&self) -> HashMap<Txid, Vec<(u32, Script)>> {
-               self.inner.lock().unwrap().get_outputs_to_watch().clone()
+       pub fn get_outputs_to_watch(&self) -> Vec<(Txid, Vec<(u32, Script)>)> {
+               self.inner.lock().unwrap().get_outputs_to_watch()
+                       .iter().map(|(txid, outputs)| (*txid, outputs.clone())).collect()
+       }
+
+       /// Loads the funding txo and outputs to watch into the given `chain::Filter` by repeatedly
+       /// calling `chain::Filter::register_output` and `chain::Filter::register_tx` until all outputs
+       /// have been registered.
+       pub fn load_outputs_to_watch<F: Deref>(&self, filter: &F) where F::Target: chain::Filter {
+               let lock = self.inner.lock().unwrap();
+               filter.register_tx(&lock.get_funding_txo().0.txid, &lock.get_funding_txo().1);
+               for (txid, outputs) in lock.get_outputs_to_watch().iter() {
+                       for (index, script_pubkey) in outputs.iter() {
+                               assert!(*index <= u16::max_value() as u32);
+                               filter.register_output(&OutPoint { txid: *txid, index: *index as u16 }, script_pubkey);
+                       }
+               }
        }
 
        /// Get the list of HTLCs who's status has been updated on chain. This should be called by
@@ -2090,8 +2104,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
                      F::Target: FeeEstimator,
                      L::Target: Logger,
        {
-               let block_hash = header.block_hash();
-               log_trace!(logger, "Block {} at height {} disconnected", block_hash, height);
+               log_trace!(logger, "Block {} at height {} disconnected", header.block_hash(), height);
 
                if let Some(_) = self.onchain_events_waiting_threshold_conf.remove(&(height + ANTI_REORG_DELAY - 1)) {
                        //We may discard:
@@ -2101,7 +2114,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
 
                self.onchain_tx_handler.block_disconnected(height, broadcaster, fee_estimator, logger);
 
-               self.last_block_hash = block_hash;
+               self.last_block_hash = header.prev_blockhash;
        }
 
        /// Filters a block's `txdata` for transactions spending watched outputs or for any child