X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fchain%2Ftransaction.rs;h=4db5fbed6a8b9ed50f476f93c5a9581243857e33;hb=2eb7db9ace08b57bfedbdac2b26961897b032e0b;hp=d490797bc8e74cc541d1c4db4269d876991c9324;hpb=8ade071d568f8668e3f820e43c894b9e574fd569;p=rust-lightning diff --git a/lightning/src/chain/transaction.rs b/lightning/src/chain/transaction.rs index d490797bc..4db5fbed6 100644 --- a/lightning/src/chain/transaction.rs +++ b/lightning/src/chain/transaction.rs @@ -1,7 +1,47 @@ -//! Contains simple structs describing parts of transactions on the chain. +// This file is Copyright its original authors, visible in version control +// history. +// +// This file is licensed under the Apache License, Version 2.0 or the MIT license +// , at your option. +// You may not use this file except in accordance with one or both of these +// licenses. + +//! 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. /// @@ -26,12 +66,21 @@ impl OutPoint { } /// Converts this OutPoint into the OutPoint field as used by rust-bitcoin + /// (C-not exported) as the same type is used universally in the C bindings for all outpoints pub fn into_bitcoin_outpoint(self) -> BitcoinOutPoint { BitcoinOutPoint { txid: self.txid, vout: self.index as u32, } } + + /// Creates a dummy BitcoinOutPoint, useful for deserializing into. + pub(crate) fn null() -> Self { + Self { + txid: Default::default(), + index: 0 + } + } } impl_writeable!(OutPoint, 0, { txid, index });