From ca4a5ce30cd18b1849b39c145c9caa3120fda5ca Mon Sep 17 00:00:00 2001 From: Henry de Valence Date: Wed, 2 Sep 2020 14:59:16 -0700 Subject: [PATCH] chain: add Transaction::hash() method. This makes Transaction and Block have a consistent API. --- zebra-chain/src/block.rs | 2 +- zebra-chain/src/transaction.rs | 5 +++++ zebra-chain/src/transaction/hash.rs | 4 ++-- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/zebra-chain/src/block.rs b/zebra-chain/src/block.rs index 6ce2fb24..9215a484 100644 --- a/zebra-chain/src/block.rs +++ b/zebra-chain/src/block.rs @@ -50,7 +50,7 @@ impl Block { }) } - /// Get the hash for the current block + /// Compute the hash of this block. pub fn hash(&self) -> Hash { Hash::from(self) } diff --git a/zebra-chain/src/transaction.rs b/zebra-chain/src/transaction.rs index 5bd13f92..28d03616 100644 --- a/zebra-chain/src/transaction.rs +++ b/zebra-chain/src/transaction.rs @@ -97,6 +97,11 @@ pub enum Transaction { } impl Transaction { + /// Compute the hash of this transaction. + pub fn hash(&self) -> Hash { + Hash::from(self) + } + /// Access the transparent inputs of this transaction, regardless of version. pub fn inputs(&self) -> &[transparent::Input] { match self { diff --git a/zebra-chain/src/transaction/hash.rs b/zebra-chain/src/transaction/hash.rs index 322bf40b..af6dc8c2 100644 --- a/zebra-chain/src/transaction/hash.rs +++ b/zebra-chain/src/transaction/hash.rs @@ -17,8 +17,8 @@ use super::Transaction; #[cfg_attr(test, derive(Arbitrary))] pub struct Hash(pub [u8; 32]); -impl From for Hash { - fn from(transaction: Transaction) -> Self { +impl<'a> From<&'a Transaction> for Hash { + fn from(transaction: &'a Transaction) -> Self { let mut hash_writer = sha256d::Writer::default(); transaction .zcash_serialize(&mut hash_writer)