Clean up ChannelMonitor object serialization to use TLVs/versions
[rust-lightning] / lightning / src / chain / transaction.rs
index ce43984ebd48b270f0f32da2266da2ac940e2a6b..502eb895b2683e4ad3b7fa5bc34f367111781ede 100644 (file)
@@ -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 <LICENSE-APACHE
+// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
+// You may not use this file except in accordance with one or both of these
+// licenses.
 
-use bitcoin_hashes::sha256d::Hash as Sha256dHash;
+//! 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.
 ///
@@ -10,17 +50,12 @@ use bitcoin::blockdata::transaction::OutPoint as BitcoinOutPoint;
 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
 pub struct OutPoint {
        /// The referenced transaction's txid.
-       pub txid: Sha256dHash,
+       pub txid: Txid,
        /// The index of the referenced output in its transaction's vout.
        pub index: u16,
 }
 
 impl OutPoint {
-       /// Creates a new `OutPoint` from the txid and the index.
-       pub fn new(txid: Sha256dHash, index: u16) -> OutPoint {
-               OutPoint { txid, index }
-       }
-
        /// Convert an `OutPoint` to a lightning channel id.
        pub fn to_channel_id(&self) -> [u8; 32] {
                let mut res = [0; 32];
@@ -31,6 +66,7 @@ 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,
@@ -39,6 +75,8 @@ impl OutPoint {
        }
 }
 
+impl_writeable!(OutPoint, 0, { txid, index });
+
 #[cfg(test)]
 mod tests {
        use chain::transaction::OutPoint;