diff --git a/zebra-chain/src/transaction.rs b/zebra-chain/src/transaction.rs index 128f73a2..eabf051c 100644 --- a/zebra-chain/src/transaction.rs +++ b/zebra-chain/src/transaction.rs @@ -1,8 +1,26 @@ //! Transaction types. -/// Stub-- delete later. +use std::io; + +use crate::serialization::{SerializationError, ZcashDeserialize, ZcashSerialize}; +use crate::sha256d_writer::Sha256dWriter; + +/// A hash of a `Transaction` +/// +/// TODO: I'm pretty sure this is also a SHA256d hash but I haven't +/// confirmed it yet. #[derive(Copy, Clone, Debug, Eq, PartialEq)] -pub struct TxHash(pub [u8; 32]); +pub struct TransactionHash(pub [u8; 32]); + +impl From for TransactionHash { + fn from(transaction: Transaction) -> Self { + let mut hash_writer = Sha256dWriter::default(); + transaction + .zcash_serialize(&mut hash_writer) + .expect("Block headers must serialize."); + Self(hash_writer.finish()) + } +} /// OutPoint /// @@ -10,7 +28,7 @@ pub struct TxHash(pub [u8; 32]); #[derive(Copy, Clone, Debug, Eq, PartialEq)] pub struct OutPoint { /// References the transaction that contains the UTXO being spent. - pub hash: [u8; 32], + pub hash: TransactionHash, /// Identifies which UTXO from that transaction is referenced; the /// first output is 0, etc. @@ -92,3 +110,15 @@ pub struct Transaction { /// transaction may not be added to a block until after `lock_time`. pub lock_time: u32, } + +impl ZcashSerialize for Transaction { + fn zcash_serialize(&self, writer: W) -> Result<(), SerializationError> { + unimplemented!(); + } +} + +impl ZcashDeserialize for Transaction { + fn zcash_deserialize(reader: R) -> Result { + unimplemented!(); + } +} diff --git a/zebra-network/src/protocol/inv.rs b/zebra-network/src/protocol/inv.rs index be8758bb..072e1909 100644 --- a/zebra-network/src/protocol/inv.rs +++ b/zebra-network/src/protocol/inv.rs @@ -12,7 +12,7 @@ use zebra_chain::block::BlockHeaderHash; use zebra_chain::serialization::{ ReadZcashExt, SerializationError, ZcashDeserialize, ZcashSerialize, }; -use zebra_chain::transaction::TxHash; +use zebra_chain::transaction::TransactionHash; /// An inventory hash which refers to some advertised or requested data. /// @@ -28,7 +28,7 @@ pub enum InventoryHash { /// so we don't include a typed hash. Error, /// A hash of a transaction. - Tx(TxHash), + Tx(TransactionHash), /// A hash of a block. Block(BlockHeaderHash), /// A hash of a filtered block. @@ -60,7 +60,7 @@ impl ZcashDeserialize for InventoryHash { let bytes = reader.read_32_bytes()?; match code { 0 => Ok(InventoryHash::Error), - 1 => Ok(InventoryHash::Tx(TxHash(bytes))), + 1 => Ok(InventoryHash::Tx(TransactionHash(bytes))), 2 => Ok(InventoryHash::Block(BlockHeaderHash(bytes))), 3 => Ok(InventoryHash::FilteredBlock(BlockHeaderHash(bytes))), _ => Err(SerializationError::ParseError("invalid inventory code")),