From 8e0e4eb55a62e4dd7b2b904d8029db828960f663 Mon Sep 17 00:00:00 2001 From: Carl Dong Date: Sun, 2 Sep 2018 22:41:08 -0700 Subject: [PATCH] Move serialize::BitcoinHash to util::hash::BitcoinHash - Use Sha256dEncoder for calculating merkle root - Remove BitcoinHash implementation for Vec --- src/blockdata/block.rs | 3 +-- src/blockdata/constants.rs | 3 ++- src/blockdata/transaction.rs | 7 +++---- src/lib.rs | 2 +- src/network/serialize.rs | 14 -------------- src/util/hash.rs | 12 ++++++++---- 6 files changed, 15 insertions(+), 26 deletions(-) diff --git a/src/blockdata/block.rs b/src/blockdata/block.rs index 3761e2b4..3ef1040d 100644 --- a/src/blockdata/block.rs +++ b/src/blockdata/block.rs @@ -22,10 +22,9 @@ use util; use util::Error::{SpvBadTarget, SpvBadProofOfWork}; -use util::hash::Sha256dHash; +use util::hash::{BitcoinHash, Sha256dHash}; use util::uint::Uint256; use network::encodable::VarInt; -use network::serialize::BitcoinHash; use network::constants::Network; use blockdata::transaction::Transaction; use blockdata::constants::max_target; diff --git a/src/blockdata/constants.rs b/src/blockdata/constants.rs index 408af931..43776421 100644 --- a/src/blockdata/constants.rs +++ b/src/blockdata/constants.rs @@ -143,9 +143,10 @@ mod test { use hex::decode as hex_decode; use network::constants::Network; - use network::serialize::{BitcoinHash, serialize}; + use network::serialize::serialize; use blockdata::constants::{genesis_block, bitcoin_genesis_tx}; use blockdata::constants::{MAX_SEQUENCE, COIN_VALUE}; + use util::hash::BitcoinHash; #[test] fn bitcoin_genesis_first_transaction() { diff --git a/src/blockdata/transaction.rs b/src/blockdata/transaction.rs index 19e88934..93aa4f72 100644 --- a/src/blockdata/transaction.rs +++ b/src/blockdata/transaction.rs @@ -28,10 +28,10 @@ use std::default::Default; use std::fmt; #[cfg(feature="bitcoinconsensus")] use std::collections::HashMap; -use util::hash::Sha256dHash; +use util::hash::{BitcoinHash, Sha256dHash}; #[cfg(feature="bitcoinconsensus")] use blockdata::script; use blockdata::script::Script; -use network::serialize::{self, serialize, BitcoinHash, SimpleEncoder, SimpleDecoder}; +use network::serialize::{self, serialize, SimpleEncoder, SimpleDecoder}; use network::encodable::{ConsensusEncodable, ConsensusDecodable, VarInt}; /// A reference to a transaction output @@ -499,11 +499,10 @@ mod tests { use super::{Transaction, TxIn}; use blockdata::script::Script; - use network::serialize::BitcoinHash; #[cfg(all(feature = "serde", feature = "strason"))] use network::serialize::serialize; use network::serialize::deserialize; - use util::hash::Sha256dHash; + use util::hash::{BitcoinHash, Sha256dHash}; use util::misc::hex_bytes; #[test] diff --git a/src/lib.rs b/src/lib.rs index d2f7da80..c6d9c20b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -72,9 +72,9 @@ pub use blockdata::transaction::TxOut; pub use blockdata::transaction::OutPoint; pub use blockdata::transaction::SigHashType; pub use network::encodable::VarInt; -pub use network::serialize::BitcoinHash; pub use util::Error; pub use util::address::Address; +pub use util::hash::BitcoinHash; pub use util::privkey::Privkey; pub use util::decimal::Decimal; pub use util::decimal::UDecimal; diff --git a/src/network/serialize.rs b/src/network/serialize.rs index 81df314d..34e18f9a 100644 --- a/src/network/serialize.rs +++ b/src/network/serialize.rs @@ -30,7 +30,6 @@ use bitcoin_bech32; use network::encodable::{ConsensusDecodable, ConsensusEncodable}; use util::base58; -use util::hash::Sha256dHash; /// Serialization error #[derive(Debug)] @@ -158,19 +157,6 @@ impl From for Error { } } -/// Objects which are referred to by hash -pub trait BitcoinHash { - /// Produces a Sha256dHash which can be used to refer to the object - fn bitcoin_hash(&self) -> Sha256dHash; -} - -impl BitcoinHash for Vec { - #[inline] - fn bitcoin_hash(&self) -> Sha256dHash { - Sha256dHash::from_data(&self[..]) - } -} - /// Encode an object into a vector pub fn serialize(data: &T) -> Result, Error> where T: ConsensusEncodable>>>, diff --git a/src/util/hash.rs b/src/util/hash.rs index d740fe0d..a70b06d2 100644 --- a/src/util/hash.rs +++ b/src/util/hash.rs @@ -20,7 +20,6 @@ use std::cmp::min; use std::default::Default; use std::error; use std::fmt; -use std::io::Cursor; use std::mem; #[cfg(feature = "serde")] use serde; @@ -29,7 +28,7 @@ use crypto::digest::Digest; use crypto::ripemd160::Ripemd160; use network::encodable::{ConsensusDecodable, ConsensusEncodable}; -use network::serialize::{self, SimpleEncoder, RawEncoder, BitcoinHash}; +use network::serialize::{self, SimpleEncoder}; use util::uint::Uint256; #[cfg(feature="fuzztarget")] use util::sha2::Sha256; @@ -462,10 +461,10 @@ pub fn bitcoin_merkle_root(data: Vec) -> Sha256dHash { for idx in 0..((data.len() + 1) / 2) { let idx1 = 2 * idx; let idx2 = min(idx1 + 1, data.len() - 1); - let mut encoder = RawEncoder::new(Cursor::new(vec![])); + let mut encoder = Sha256dEncoder::new(); data[idx1].consensus_encode(&mut encoder).unwrap(); data[idx2].consensus_encode(&mut encoder).unwrap(); - next.push(encoder.into_inner().into_inner().bitcoin_hash()); + next.push(encoder.into_hash()); } bitcoin_merkle_root(next) } @@ -482,6 +481,11 @@ impl MerkleRoot for Vec { } } +/// Objects which are referred to by hash +pub trait BitcoinHash { + /// Produces a Sha256dHash which can be used to refer to the object + fn bitcoin_hash(&self) -> Sha256dHash; +} #[cfg(test)] mod tests {