Rename Hash::zero() to Hash::default() (#858)

It is more idiomatic, also `zero` is misleading in context of `+` operator.
This commit is contained in:
Alexey Miroshkin 2018-03-24 00:35:10 +01:00 committed by Ignotus Peverell
parent 388beafb65
commit bb134758e7
6 changed files with 17 additions and 16 deletions

View file

@ -67,11 +67,6 @@ impl Hash {
self.0.to_vec() self.0.to_vec()
} }
/// The "zero" hash. No known preimage.
pub fn zero() -> Hash {
ZERO_HASH
}
/// Convert a hash to hex string format. /// Convert a hash to hex string format.
pub fn to_hex(&self) -> String { pub fn to_hex(&self) -> String {
util::to_hex(self.to_vec()) util::to_hex(self.to_vec())
@ -154,6 +149,12 @@ impl Add for Hash {
} }
} }
impl Default for Hash {
fn default() -> Hash {
ZERO_HASH
}
}
/// Serializer that outputs a hash of the serialized object /// Serializer that outputs a hash of the serialized object
pub struct HashWriter { pub struct HashWriter {
state: Blake2b, state: Blake2b,

View file

@ -150,7 +150,7 @@ mod test {
).unwrap(); ).unwrap();
assert_eq!(foo.hash(), expected_hash); assert_eq!(foo.hash(), expected_hash);
let other_hash = Hash::zero(); let other_hash = Hash::default();
assert_eq!( assert_eq!(
foo.short_id(&other_hash, foo.0), foo.short_id(&other_hash, foo.0),
ShortId::from_hex("4cc808b62476").unwrap() ShortId::from_hex("4cc808b62476").unwrap()
@ -162,7 +162,7 @@ mod test {
).unwrap(); ).unwrap();
assert_eq!(foo.hash(), expected_hash); assert_eq!(foo.hash(), expected_hash);
let other_hash = Hash::zero(); let other_hash = Hash::default();
assert_eq!( assert_eq!(
foo.short_id(&other_hash, foo.0), foo.short_id(&other_hash, foo.0),
ShortId::from_hex("02955a094534").unwrap() ShortId::from_hex("02955a094534").unwrap()

View file

@ -178,8 +178,8 @@ impl MerkleProof {
/// Basically some reasonable defaults. Will not verify successfully. /// Basically some reasonable defaults. Will not verify successfully.
pub fn empty() -> MerkleProof { pub fn empty() -> MerkleProof {
MerkleProof { MerkleProof {
root: Hash::zero(), root: Hash::default(),
node: Hash::zero(), node: Hash::default(),
peaks: vec![], peaks: vec![],
path: vec![], path: vec![],
} }
@ -336,7 +336,7 @@ where
let path = family_branch let path = family_branch
.iter() .iter()
.map(|x| (self.get_from_file(x.1).unwrap_or(Hash::zero()), x.1)) .map(|x| (self.get_from_file(x.1).unwrap_or(Hash::default()), x.1))
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let peaks = peaks(self.last_pos) let peaks = peaks(self.last_pos)

View file

@ -583,10 +583,10 @@ impl Input {
} }
/// Convenience functon to return the (optional) block_hash for this input. /// Convenience functon to return the (optional) block_hash for this input.
/// Will return the "zero" hash if we do not have one. /// Will return the default hash if we do not have one.
pub fn block_hash(&self) -> Hash { pub fn block_hash(&self) -> Hash {
let block_hash = self.block_hash.clone(); let block_hash = self.block_hash.clone();
block_hash.unwrap_or(Hash::zero()) block_hash.unwrap_or(Hash::default())
} }
/// Convenience function to return the (optional) merkle_proof for this input. /// Convenience function to return the (optional) merkle_proof for this input.

View file

@ -107,7 +107,7 @@ impl DummyChainImpl {
impl BlockChain for DummyChainImpl { impl BlockChain for DummyChainImpl {
fn is_unspent(&self, output_ref: &OutputIdentifier) -> Result<hash::Hash, PoolError> { fn is_unspent(&self, output_ref: &OutputIdentifier) -> Result<hash::Hash, PoolError> {
match self.output.read().unwrap().get_output(&output_ref.commit) { match self.output.read().unwrap().get_output(&output_ref.commit) {
Some(_) => Ok(hash::Hash::zero()), Some(_) => Ok(hash::Hash::default()),
None => Err(PoolError::GenericPoolError), None => Err(PoolError::GenericPoolError),
} }
} }

View file

@ -1654,9 +1654,9 @@ mod tests {
let mut tx_elements = Vec::new(); let mut tx_elements = Vec::new();
let merkle_proof = MerkleProof { let merkle_proof = MerkleProof {
node: Hash::zero(), node: Hash::default(),
root: Hash::zero(), root: Hash::default(),
peaks: vec![Hash::zero()], peaks: vec![Hash::default()],
..MerkleProof::default() ..MerkleProof::default()
}; };