X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fchain%2Ftransaction.rs;h=39a0a5449ac7cf1a4f5dc636363b0ad9c0245c6f;hb=44e87b86f27a1a6ff3860e8ab87a52e756aa7cc8;hp=ce43984ebd48b270f0f32da2266da2ac940e2a6b;hpb=88fef649b15fa030cb91de76d58346a0bc408834;p=rust-lightning diff --git a/lightning/src/chain/transaction.rs b/lightning/src/chain/transaction.rs index ce43984e..39a0a544 100644 --- a/lightning/src/chain/transaction.rs +++ b/lightning/src/chain/transaction.rs @@ -1,7 +1,49 @@ -//! 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. -use bitcoin_hashes::sha256d::Hash as Sha256dHash; +//! Types describing on-chain transactions. + +use crate::ln::ChannelId; +use bitcoin::hash_types::Txid; +use bitcoin::hashes::Hash; 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,27 +52,21 @@ 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]; - res[..].copy_from_slice(&self.txid[..]); - res[30] ^= ((self.index >> 8) & 0xff) as u8; - res[31] ^= ((self.index >> 0) & 0xff) as u8; - res + pub fn to_channel_id(&self) -> ChannelId { + ChannelId::v1_from_funding_txid(&self.txid.as_inner(), self.index) } /// Converts this OutPoint into the OutPoint field as used by rust-bitcoin + /// + /// This is not exported to bindings users 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,9 +75,17 @@ impl OutPoint { } } +impl core::fmt::Display for OutPoint { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + write!(f, "{}:{}", self.txid, self.index) + } +} + +impl_writeable!(OutPoint, { txid, index }); + #[cfg(test)] mod tests { - use chain::transaction::OutPoint; + use crate::chain::transaction::OutPoint; use bitcoin::blockdata::transaction::Transaction; use bitcoin::consensus::encode; @@ -54,10 +98,10 @@ mod tests { assert_eq!(&OutPoint { txid: tx.txid(), index: 0 - }.to_channel_id(), &hex::decode("3e88dd7165faf7be58b3c5bb2c9c452aebef682807ea57080f62e6f6e113c25e").unwrap()[..]); + }.to_channel_id().0[..], &hex::decode("3e88dd7165faf7be58b3c5bb2c9c452aebef682807ea57080f62e6f6e113c25e").unwrap()[..]); assert_eq!(&OutPoint { txid: tx.txid(), index: 1 - }.to_channel_id(), &hex::decode("3e88dd7165faf7be58b3c5bb2c9c452aebef682807ea57080f62e6f6e113c25f").unwrap()[..]); + }.to_channel_id().0[..], &hex::decode("3e88dd7165faf7be58b3c5bb2c9c452aebef682807ea57080f62e6f6e113c25f").unwrap()[..]); } }