From 8b1e5afddd931fb57b48f92493b3a1ae7ab523d9 Mon Sep 17 00:00:00 2001 From: Jeffrey Czyz Date: Wed, 9 Sep 2020 12:16:09 -0700 Subject: [PATCH] Define type alias for enumerated transaction data 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 | 33 +++++++++++++++++++++++++++++- lightning/src/ln/channel.rs | 4 ++-- lightning/src/ln/channelmanager.rs | 5 ++--- lightning/src/ln/channelmonitor.rs | 8 ++++---- 4 files changed, 40 insertions(+), 10 deletions(-) diff --git a/lightning/src/chain/transaction.rs b/lightning/src/chain/transaction.rs index 946562bc1..502eb895b 100644 --- a/lightning/src/chain/transaction.rs +++ b/lightning/src/chain/transaction.rs @@ -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. /// diff --git a/lightning/src/ln/channel.rs b/lightning/src/ln/channel.rs index 5ce39592f..ac331f31e 100644 --- a/lightning/src/ln/channel.rs +++ b/lightning/src/ln/channel.rs @@ -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 Channel { /// /// 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, Vec<(HTLCSource, PaymentHash)>), msgs::ErrorMessage> { + pub fn block_connected(&mut self, header: &BlockHeader, txdata: &TransactionData, height: u32) -> Result<(Option, Vec<(HTLCSource, PaymentHash)>), msgs::ErrorMessage> { let mut timed_out_htlcs = Vec::new(); self.holding_cell_htlc_updates.retain(|htlc_update| { match htlc_update { diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index 98e0ec91d..488dc75fa 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -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 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(); diff --git a/lightning/src/ln/channelmonitor.rs b/lightning/src/ln/channelmonitor.rs index 803a20ce3..b64841c44 100644 --- a/lightning/src/ln/channelmonitor.rs +++ b/lightning/src/ln/channelmonitor.rs @@ -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 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 ChannelMonitor { /// [`get_outputs_to_watch`]. /// /// [`get_outputs_to_watch`]: #method.get_outputs_to_watch - pub fn block_connected(&mut self, header: &BlockHeader, txdata: &[(usize, &Transaction)], height: u32, broadcaster: B, fee_estimator: F, logger: L)-> Vec<(Txid, Vec)> + pub fn block_connected(&mut self, header: &BlockHeader, txdata: &TransactionData, height: u32, broadcaster: B, fee_estimator: F, logger: L)-> Vec<(Txid, Vec)> where B::Target: BroadcasterInterface, F::Target: FeeEstimator, L::Target: Logger, @@ -1995,7 +1995,7 @@ impl ChannelMonitor { /// 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); -- 2.39.5