Define type alias for enumerated transaction data
authorJeffrey Czyz <jkczyz@gmail.com>
Wed, 9 Sep 2020 19:16:09 +0000 (12:16 -0700)
committerJeffrey Czyz <jkczyz@gmail.com>
Thu, 1 Oct 2020 05:41:52 +0000 (22:41 -0700)
Transaction data from a block may be filtered before it is passed to
block_connected functions, which may need the index of each transaction
within the block. Rather than define each function in terms of a slice
of tuples, define a type alias for the slice where it can be documented.

lightning/src/chain/transaction.rs
lightning/src/ln/channel.rs
lightning/src/ln/channelmanager.rs
lightning/src/ln/channelmonitor.rs

index 946562bc1781f95f77f62ce4aa252509d77eec10..502eb895b2683e4ad3b7fa5bc34f367111781ede 100644 (file)
@@ -7,10 +7,41 @@
 // You may not use this file except in accordance with one or both of these
 // licenses.
 
-//! Contains simple structs describing parts of transactions on the chain.
+//! Types describing on-chain transactions.
 
 use bitcoin::hash_types::Txid;
 use bitcoin::blockdata::transaction::OutPoint as BitcoinOutPoint;
+use bitcoin::blockdata::transaction::Transaction;
+
+/// Transaction data where each item consists of a transaction reference paired with the index of
+/// the transaction within a block.
+///
+/// Useful for passing enumerated transactions from a block, possibly filtered, in order to retain
+/// the transaction index.
+///
+/// ```
+/// extern crate bitcoin;
+/// extern crate lightning;
+///
+/// use bitcoin::blockdata::block::Block;
+/// use bitcoin::blockdata::constants::genesis_block;
+/// use bitcoin::network::constants::Network;
+/// use lightning::chain::transaction::TransactionData;
+///
+/// let block = genesis_block(Network::Bitcoin);
+/// let txdata: Vec<_> = block.txdata.iter().enumerate().collect();
+/// check_block(&block, &txdata);
+///
+/// fn check_block(block: &Block, txdata: &TransactionData) {
+///    assert_eq!(block.txdata.len(), 1);
+///    assert_eq!(txdata.len(), 1);
+///
+///    let (index, tx) = txdata[0];
+///    assert_eq!(index, 0);
+///    assert_eq!(tx, &block.txdata[0]);
+/// }
+/// ```
+pub type TransactionData<'a> = [(usize, &'a Transaction)];
 
 /// A reference to a transaction output.
 ///
index 5ce39592fabdc59fcdfad91e43e153b7bbb2b806..ac331f31e132ee98e0a3f67b081fbf70351c5c1a 100644 (file)
@@ -30,7 +30,7 @@ use ln::channelmanager::{PendingHTLCStatus, HTLCSource, HTLCFailReason, HTLCFail
 use ln::chan_utils::{CounterpartyCommitmentSecrets, HolderCommitmentTransaction, TxCreationKeys, HTLCOutputInCommitment, HTLC_SUCCESS_TX_WEIGHT, HTLC_TIMEOUT_TX_WEIGHT, make_funding_redeemscript, ChannelPublicKeys, PreCalculatedTxCreationKeys};
 use ln::chan_utils;
 use chain::chaininterface::{FeeEstimator,ConfirmationTarget};
-use chain::transaction::OutPoint;
+use chain::transaction::{OutPoint, TransactionData};
 use chain::keysinterface::{ChannelKeys, KeysInterface};
 use util::transaction_utils;
 use util::ser::{Readable, Writeable, Writer};
@@ -3315,7 +3315,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
        ///
        /// May return some HTLCs (and their payment_hash) which have timed out and should be failed
        /// back.
-       pub fn block_connected(&mut self, header: &BlockHeader, txdata: &[(usize, &Transaction)], height: u32) -> Result<(Option<msgs::FundingLocked>, Vec<(HTLCSource, PaymentHash)>), msgs::ErrorMessage> {
+       pub fn block_connected(&mut self, header: &BlockHeader, txdata: &TransactionData, height: u32) -> Result<(Option<msgs::FundingLocked>, Vec<(HTLCSource, PaymentHash)>), msgs::ErrorMessage> {
                let mut timed_out_htlcs = Vec::new();
                self.holding_cell_htlc_updates.retain(|htlc_update| {
                        match htlc_update {
index 98e0ec91d50c098b56e693f964642f67b9eeaeea..488dc75fadad27b0c264ad5e2e9a11a52a59c276 100644 (file)
@@ -19,7 +19,6 @@
 
 use bitcoin::blockdata::block::BlockHeader;
 use bitcoin::blockdata::constants::genesis_block;
-use bitcoin::blockdata::transaction::Transaction;
 use bitcoin::network::constants::Network;
 
 use bitcoin::hashes::{Hash, HashEngine};
@@ -37,7 +36,7 @@ use bitcoin::secp256k1;
 use chain;
 use chain::Watch;
 use chain::chaininterface::{BroadcasterInterface, FeeEstimator};
-use chain::transaction::OutPoint;
+use chain::transaction::{OutPoint, TransactionData};
 use ln::channel::{Channel, ChannelError};
 use ln::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateErr, HTLC_FAIL_BACK_BUFFER, CLTV_CLAIM_BUFFER, LATENCY_GRACE_PERIOD_BLOCKS, ANTI_REORG_DELAY, MonitorEvent};
 use ln::features::{InitFeatures, NodeFeatures};
@@ -3060,7 +3059,7 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
         L::Target: Logger,
 {
        /// Updates channel state based on transactions seen in a connected block.
-       pub fn block_connected(&self, header: &BlockHeader, txdata: &[(usize, &Transaction)], height: u32) {
+       pub fn block_connected(&self, header: &BlockHeader, txdata: &TransactionData, height: u32) {
                let header_hash = header.block_hash();
                log_trace!(self.logger, "Block {} at height {} connected", header_hash, height);
                let _ = self.total_consistency_lock.read().unwrap();
index 803a20ce3ab8042cc7d330060b1699c692ebf737..b64841c44b5bc627cf14c70b3ac841d55e523cf8 100644 (file)
@@ -45,7 +45,7 @@ use ln::onchaintx::{OnchainTxHandler, InputDescriptors};
 use chain;
 use chain::Filter;
 use chain::chaininterface::{BroadcasterInterface, FeeEstimator};
-use chain::transaction::OutPoint;
+use chain::transaction::{OutPoint, TransactionData};
 use chain::keysinterface::{SpendableOutputDescriptor, ChannelKeys};
 use util::logger::Logger;
 use util::ser::{Readable, MaybeReadable, Writer, Writeable, U48};
@@ -230,7 +230,7 @@ impl<ChanSigner: ChannelKeys, C: Deref, T: Deref, F: Deref, L: Deref> ChainMonit
        /// [`ChannelMonitor::block_connected`]: struct.ChannelMonitor.html#method.block_connected
        /// [`chain::Watch::release_pending_monitor_events`]: ../../chain/trait.Watch.html#tymethod.release_pending_monitor_events
        /// [`chain::Filter`]: ../../chain/trait.Filter.html
-       pub fn block_connected(&self, header: &BlockHeader, txdata: &[(usize, &Transaction)], height: u32) -> bool {
+       pub fn block_connected(&self, header: &BlockHeader, txdata: &TransactionData, height: u32) -> bool {
                let mut has_new_outputs_to_watch = false;
                {
                        let mut monitors = self.monitors.lock().unwrap();
@@ -1865,7 +1865,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
        /// [`get_outputs_to_watch`].
        ///
        /// [`get_outputs_to_watch`]: #method.get_outputs_to_watch
-       pub fn block_connected<B: Deref, F: Deref, L: Deref>(&mut self, header: &BlockHeader, txdata: &[(usize, &Transaction)], height: u32, broadcaster: B, fee_estimator: F, logger: L)-> Vec<(Txid, Vec<TxOut>)>
+       pub fn block_connected<B: Deref, F: Deref, L: Deref>(&mut self, header: &BlockHeader, txdata: &TransactionData, height: u32, broadcaster: B, fee_estimator: F, logger: L)-> Vec<(Txid, Vec<TxOut>)>
                where B::Target: BroadcasterInterface,
                      F::Target: FeeEstimator,
                                        L::Target: Logger,
@@ -1995,7 +1995,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
 
        /// Filters a block's `txdata` for transactions spending watched outputs or for any child
        /// transactions thereof.
-       fn filter_block<'a>(&self, txdata: &[(usize, &'a Transaction)]) -> Vec<&'a Transaction> {
+       fn filter_block<'a>(&self, txdata: &TransactionData<'a>) -> Vec<&'a Transaction> {
                let mut matched_txn = HashSet::new();
                txdata.iter().filter(|&&(_, tx)| {
                        let mut matches = self.spends_watched_output(tx);