Skip to content

Commit ff416c0

Browse files
committed
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.
1 parent 7fe000f commit ff416c0

File tree

4 files changed

+40
-10
lines changed

4 files changed

+40
-10
lines changed

lightning/src/chain/transaction.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,41 @@
77
// You may not use this file except in accordance with one or both of these
88
// licenses.
99

10-
//! Contains simple structs describing parts of transactions on the chain.
10+
//! Types describing on-chain transactions.
1111
1212
use bitcoin::hash_types::Txid;
1313
use bitcoin::blockdata::transaction::OutPoint as BitcoinOutPoint;
14+
use bitcoin::blockdata::transaction::Transaction;
15+
16+
/// Transaction data where each item consists of a transaction reference paired with the index of
17+
/// the transaction within a block.
18+
///
19+
/// Useful for passing enumerated transactions from a block, possibly filtered, in order to retain
20+
/// the transaction index.
21+
///
22+
/// ```
23+
/// extern crate bitcoin;
24+
/// extern crate lightning;
25+
///
26+
/// use bitcoin::blockdata::block::Block;
27+
/// use bitcoin::blockdata::constants::genesis_block;
28+
/// use bitcoin::network::constants::Network;
29+
/// use lightning::chain::transaction::TransactionData;
30+
///
31+
/// let block = genesis_block(Network::Bitcoin);
32+
/// let txdata: Vec<_> = block.txdata.iter().enumerate().collect();
33+
/// check_block(&block, &txdata);
34+
///
35+
/// fn check_block(block: &Block, txdata: &TransactionData) {
36+
/// assert_eq!(block.txdata.len(), 1);
37+
/// assert_eq!(txdata.len(), 1);
38+
///
39+
/// let (index, tx) = txdata[0];
40+
/// assert_eq!(index, 0);
41+
/// assert_eq!(tx, &block.txdata[0]);
42+
/// }
43+
/// ```
44+
pub type TransactionData<'a> = [(usize, &'a Transaction)];
1445

1546
/// A reference to a transaction output.
1647
///

lightning/src/ln/channel.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use ln::channelmanager::{PendingHTLCStatus, HTLCSource, HTLCFailReason, HTLCFail
3030
use ln::chan_utils::{CounterpartyCommitmentSecrets, HolderCommitmentTransaction, TxCreationKeys, HTLCOutputInCommitment, HTLC_SUCCESS_TX_WEIGHT, HTLC_TIMEOUT_TX_WEIGHT, make_funding_redeemscript, ChannelPublicKeys, PreCalculatedTxCreationKeys};
3131
use ln::chan_utils;
3232
use chain::chaininterface::{FeeEstimator,ConfirmationTarget};
33-
use chain::transaction::OutPoint;
33+
use chain::transaction::{OutPoint, TransactionData};
3434
use chain::keysinterface::{ChannelKeys, KeysInterface};
3535
use util::transaction_utils;
3636
use util::ser::{Readable, Writeable, Writer};
@@ -3315,7 +3315,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
33153315
///
33163316
/// May return some HTLCs (and their payment_hash) which have timed out and should be failed
33173317
/// back.
3318-
pub fn block_connected(&mut self, header: &BlockHeader, txdata: &[(usize, &Transaction)], height: u32) -> Result<(Option<msgs::FundingLocked>, Vec<(HTLCSource, PaymentHash)>), msgs::ErrorMessage> {
3318+
pub fn block_connected(&mut self, header: &BlockHeader, txdata: &TransactionData, height: u32) -> Result<(Option<msgs::FundingLocked>, Vec<(HTLCSource, PaymentHash)>), msgs::ErrorMessage> {
33193319
let mut timed_out_htlcs = Vec::new();
33203320
self.holding_cell_htlc_updates.retain(|htlc_update| {
33213321
match htlc_update {

lightning/src/ln/channelmanager.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
2020
use bitcoin::blockdata::block::BlockHeader;
2121
use bitcoin::blockdata::constants::genesis_block;
22-
use bitcoin::blockdata::transaction::Transaction;
2322
use bitcoin::network::constants::Network;
2423

2524
use bitcoin::hashes::{Hash, HashEngine};
@@ -37,7 +36,7 @@ use bitcoin::secp256k1;
3736
use chain;
3837
use chain::Watch;
3938
use chain::chaininterface::{BroadcasterInterface, FeeEstimator};
40-
use chain::transaction::OutPoint;
39+
use chain::transaction::{OutPoint, TransactionData};
4140
use ln::channel::{Channel, ChannelError};
4241
use ln::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateErr, HTLC_FAIL_BACK_BUFFER, CLTV_CLAIM_BUFFER, LATENCY_GRACE_PERIOD_BLOCKS, ANTI_REORG_DELAY, MonitorEvent};
4342
use ln::features::{InitFeatures, NodeFeatures};
@@ -3060,7 +3059,7 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
30603059
L::Target: Logger,
30613060
{
30623061
/// Updates channel state based on transactions seen in a connected block.
3063-
pub fn block_connected(&self, header: &BlockHeader, txdata: &[(usize, &Transaction)], height: u32) {
3062+
pub fn block_connected(&self, header: &BlockHeader, txdata: &TransactionData, height: u32) {
30643063
let header_hash = header.block_hash();
30653064
log_trace!(self.logger, "Block {} at height {} connected", header_hash, height);
30663065
let _ = self.total_consistency_lock.read().unwrap();

lightning/src/ln/channelmonitor.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use ln::onchaintx::{OnchainTxHandler, InputDescriptors};
4545
use chain;
4646
use chain::Filter;
4747
use chain::chaininterface::{BroadcasterInterface, FeeEstimator};
48-
use chain::transaction::OutPoint;
48+
use chain::transaction::{OutPoint, TransactionData};
4949
use chain::keysinterface::{SpendableOutputDescriptor, ChannelKeys};
5050
use util::logger::Logger;
5151
use util::ser::{Readable, MaybeReadable, Writer, Writeable, U48};
@@ -230,7 +230,7 @@ impl<ChanSigner: ChannelKeys, C: Deref, T: Deref, F: Deref, L: Deref> ChainMonit
230230
/// [`ChannelMonitor::block_connected`]: struct.ChannelMonitor.html#method.block_connected
231231
/// [`chain::Watch::release_pending_monitor_events`]: ../../chain/trait.Watch.html#tymethod.release_pending_monitor_events
232232
/// [`chain::Filter`]: ../../chain/trait.Filter.html
233-
pub fn block_connected(&self, header: &BlockHeader, txdata: &[(usize, &Transaction)], height: u32) -> bool {
233+
pub fn block_connected(&self, header: &BlockHeader, txdata: &TransactionData, height: u32) -> bool {
234234
let mut has_new_outputs_to_watch = false;
235235
{
236236
let mut monitors = self.monitors.lock().unwrap();
@@ -1865,7 +1865,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
18651865
/// [`get_outputs_to_watch`].
18661866
///
18671867
/// [`get_outputs_to_watch`]: #method.get_outputs_to_watch
1868-
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>)>
1868+
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>)>
18691869
where B::Target: BroadcasterInterface,
18701870
F::Target: FeeEstimator,
18711871
L::Target: Logger,
@@ -1995,7 +1995,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
19951995

19961996
/// Filters a block's `txdata` for transactions spending watched outputs or for any child
19971997
/// transactions thereof.
1998-
fn filter_block<'a>(&self, txdata: &[(usize, &'a Transaction)]) -> Vec<&'a Transaction> {
1998+
fn filter_block<'a>(&self, txdata: &TransactionData<'a>) -> Vec<&'a Transaction> {
19991999
let mut matched_txn = HashSet::new();
20002000
txdata.iter().filter(|&&(_, tx)| {
20012001
let mut matches = self.spends_watched_output(tx);

0 commit comments

Comments
 (0)