Skip to content

Commit

Permalink
Replaced all hash160, sha256 and sha256d with the new hash types thro…
Browse files Browse the repository at this point in the history
…ughout the code

Embedding Txid's in the doc exaples
  • Loading branch information
dr-orlovsky committed Jan 1, 2020
1 parent 5ef39e3 commit 5f4f629
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 54 deletions.
14 changes: 7 additions & 7 deletions src/blockdata/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use util;
use util::Error::{BlockBadTarget, BlockBadProofOfWork};
use util::hash::{BitcoinHash, MerkleRoot, bitcoin_merkle_root};
use util::uint::Uint256;
use hash_types::BlockHash;
use hash_types::{Txid, BlockHash};
use consensus::encode::Encodable;
use network::constants::Network;
use blockdata::transaction::Transaction;
Expand Down Expand Up @@ -105,7 +105,7 @@ impl Block {

/// Merkle root of transactions hashed for witness
pub fn witness_root(&self) -> sha256d::Hash {
let mut txhashes = vec!(sha256d::Hash::default());
let mut txhashes = vec!(Txid::default());
txhashes.extend(self.txdata.iter().skip(1).map(|t|t.bitcoin_hash()));
bitcoin_merkle_root(txhashes)
}
Expand Down Expand Up @@ -189,15 +189,15 @@ impl BlockHeader {
}
}

impl BitcoinHash for BlockHeader {
fn bitcoin_hash(&self) -> sha256d::Hash {
impl BitcoinHash<BlockHash> for BlockHeader {
fn bitcoin_hash(&self) -> BlockHash {
use consensus::encode::serialize;
sha256d::Hash::hash(&serialize(self))
BlockHash::hash(&serialize(self))
}
}

impl BitcoinHash for Block {
fn bitcoin_hash(&self) -> sha256d::Hash {
impl BitcoinHash<BlockHash> for Block {
fn bitcoin_hash(&self) -> BlockHash {
self.header.bitcoin_hash()
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/blockdata/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ use std::{error, fmt, io};

#[cfg(feature = "serde")] use serde;

use hash_types::WScriptHash;
use blockdata::opcodes;
use consensus::{encode, Decodable, Encodable};
use hashes::{hash160, sha256, Hash};
use hashes::{hash160, Hash};
#[cfg(feature="bitcoinconsensus")] use bitcoinconsensus;
#[cfg(feature="bitcoinconsensus")] use std::convert;
#[cfg(feature="bitcoinconsensus")] use OutPoint;
Expand Down Expand Up @@ -242,7 +243,7 @@ impl Script {
/// script")
pub fn to_v0_p2wsh(&self) -> Script {
Builder::new().push_int(0)
.push_slice(&sha256::Hash::hash(&self.0)[..])
.push_slice(&WScriptHash::hash(&self.0)[..])
.into_script()
}

Expand Down
8 changes: 4 additions & 4 deletions src/blockdata/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ impl Transaction {
input: self.input.iter().map(|txin| TxIn { script_sig: Script::new(), witness: vec![], .. *txin }).collect(),
output: self.output.clone(),
};
cloned_tx.bitcoin_hash()
cloned_tx.bitcoin_hash().into()
}

/// Computes the txid. For non-segwit transactions this will be identical
Expand Down Expand Up @@ -438,11 +438,11 @@ impl Transaction {
}
}

impl BitcoinHash for Transaction {
fn bitcoin_hash(&self) -> sha256d::Hash {
impl BitcoinHash<Txid> for Transaction {
fn bitcoin_hash(&self) -> Txid {
let mut enc = sha256d::Hash::engine();
self.consensus_encode(&mut enc).unwrap();
sha256d::Hash::from_engine(enc)
Txid::from_engine(enc)
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/util/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ use std::fmt::{self, Display, Formatter};
use std::str::FromStr;

use bech32;
use hashes::{hash160, sha256, Hash};
use hashes::{hash160, Hash};

use hash_types::{PubkeyHash, ScriptHash, WScriptHash};
use blockdata::opcodes;
Expand Down Expand Up @@ -309,7 +309,7 @@ impl Address {
pub fn p2shwsh(script: &script::Script, network: Network) -> Address {
let ws = script::Builder::new()
.push_int(0)
.push_slice(&sha256::Hash::hash(&script[..])[..])
.push_slice(&WScriptHash::hash(&script[..])[..])
.into_script();

Address {
Expand Down
12 changes: 7 additions & 5 deletions src/util/bip158.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ use std::error;
use std::fmt::{Display, Formatter};
use std::io::Cursor;

use hash_types::BlockHash;
use hashes::{Hash, sha256d, siphash24};

use blockdata::block::Block;
Expand Down Expand Up @@ -133,13 +134,13 @@ impl BlockFilter {
}

/// match any query pattern
pub fn match_any(&self, block_hash: &sha256d::Hash, query: &mut Iterator<Item=&[u8]>) -> Result<bool, Error> {
pub fn match_any(&self, block_hash: &BlockHash, query: &mut Iterator<Item=&[u8]>) -> Result<bool, Error> {
let filter_reader = BlockFilterReader::new(block_hash);
filter_reader.match_any(&mut Cursor::new(self.content.as_slice()), query)
}

/// match all query pattern
pub fn match_all(&self, block_hash: &sha256d::Hash, query: &mut Iterator<Item=&[u8]>) -> Result<bool, Error> {
pub fn match_all(&self, block_hash: &BlockHash, query: &mut Iterator<Item=&[u8]>) -> Result<bool, Error> {
let filter_reader = BlockFilterReader::new(block_hash);
filter_reader.match_all(&mut Cursor::new(self.content.as_slice()), query)
}
Expand Down Expand Up @@ -206,7 +207,7 @@ pub struct BlockFilterReader {

impl BlockFilterReader {
/// Create a block filter reader
pub fn new(block_hash: &sha256d::Hash) -> BlockFilterReader {
pub fn new(block_hash: &BlockHash) -> BlockFilterReader {
let block_hash_as_int = block_hash.into_inner();
let k0 = endian::slice_to_u64_le(&block_hash_as_int[0..8]);
let k1 = endian::slice_to_u64_le(&block_hash_as_int[8..16]);
Expand Down Expand Up @@ -523,6 +524,7 @@ mod test {
use std::collections::{HashSet, HashMap};
use std::io::Cursor;

use hash_types::BlockHash;
use hashes::hex::FromHex;

use super::*;
Expand Down Expand Up @@ -555,7 +557,7 @@ mod test {

let testdata = serde_json::from_str::<Value>(data).unwrap().as_array().unwrap().clone();
for t in testdata.iter().skip(1) {
let block_hash = sha256d::Hash::from_hex(&t.get(1).unwrap().as_str().unwrap()).unwrap();
let block_hash = BlockHash::from_hex(&t.get(1).unwrap().as_str().unwrap()).unwrap();
let block: Block = deserialize(hex::decode(&t.get(2).unwrap().as_str().unwrap().as_bytes()).unwrap().as_slice()).unwrap();
assert_eq!(block.bitcoin_hash(), block_hash);
let scripts = t.get(3).unwrap().as_array().unwrap();
Expand Down Expand Up @@ -583,7 +585,7 @@ mod test {
assert_eq!(test_filter.content, filter.content);

let block_hash = &block.header.bitcoin_hash();
assert!(filter.match_all(&block_hash, &mut txmap.iter()
assert!(filter.match_all(block_hash, &mut txmap.iter()
.filter_map(|(_, s)| if !s.is_empty() { Some(s.as_bytes()) } else { None })).unwrap());

for (_, script) in &txmap {
Expand Down
11 changes: 6 additions & 5 deletions src/util/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use std::cmp::min;
use std::default::Default;

use hash_types::Txid;
use hashes::{sha256d, Hash};

use consensus::encode::Encodable;
Expand All @@ -30,13 +31,13 @@ pub trait MerkleRoot {
}

/// Calculates the merkle root of a list of txids hashes directly
pub fn bitcoin_merkle_root(data: Vec<sha256d::Hash>) -> sha256d::Hash {
pub fn bitcoin_merkle_root(data: Vec<Txid>) -> sha256d::Hash {
// Base case
if data.len() < 1 {
return Default::default();
}
if data.len() < 2 {
return data[0];
return data[0].into();
}
// Recursion
let mut next = vec![];
Expand All @@ -46,13 +47,13 @@ pub fn bitcoin_merkle_root(data: Vec<sha256d::Hash>) -> sha256d::Hash {
let mut encoder = sha256d::Hash::engine();
data[idx1].consensus_encode(&mut encoder).unwrap();
data[idx2].consensus_encode(&mut encoder).unwrap();
next.push(sha256d::Hash::from_engine(encoder));
next.push(Txid::from_engine(encoder));
}
bitcoin_merkle_root(next)
}

/// Objects which are referred to by hash
pub trait BitcoinHash {
pub trait BitcoinHash<T: Hash> {
/// Produces a Sha256dHash which can be used to refer to the object
fn bitcoin_hash(&self) -> sha256d::Hash;
fn bitcoin_hash(&self) -> T;
}
Loading

0 comments on commit 5f4f629

Please sign in to comment.