Add Hash wrapper and supporting traits
This commit is contained in:
parent
4d77fa900b
commit
448b8b1c17
|
@ -12,7 +12,7 @@ use std::collections::VecDeque;
|
|||
|
||||
fn bench_block_to_blobs_to_block(bencher: &mut Bencher) {
|
||||
let zero = Hash::default();
|
||||
let one = hash(&zero);
|
||||
let one = hash(&zero.as_ref());
|
||||
let keypair = KeyPair::new();
|
||||
let tx0 = Transaction::new(&keypair, keypair.pubkey(), 1, one);
|
||||
let transactions = vec![tx0; 10];
|
||||
|
|
|
@ -268,7 +268,7 @@ impl Bank {
|
|||
let _ = self.apply_signature(tx.from, *tx_sig);
|
||||
}
|
||||
Instruction::NewVote(_vote) => {
|
||||
info!("GOT VOTE! last_id={:?}", &tx.last_id[..8]);
|
||||
info!("GOT VOTE! last_id={:?}", &tx.last_id.as_ref()[..8]);
|
||||
// TODO: record the vote in the stake table...
|
||||
}
|
||||
}
|
||||
|
|
|
@ -149,7 +149,7 @@ fn add_transaction_data(hash_data: &mut Vec<u8>, tx: &Transaction) {
|
|||
fn next_hash(start_hash: &Hash, num_hashes: u64, transactions: &[Transaction]) -> Hash {
|
||||
let mut id = *start_hash;
|
||||
for _ in 1..num_hashes {
|
||||
id = hash(&id);
|
||||
id = hash(&id.as_ref());
|
||||
}
|
||||
|
||||
// Hash all the transaction data
|
||||
|
@ -161,7 +161,7 @@ fn next_hash(start_hash: &Hash, num_hashes: u64, transactions: &[Transaction]) -
|
|||
if !hash_data.is_empty() {
|
||||
extend_and_hash(&id, &hash_data)
|
||||
} else if num_hashes != 0 {
|
||||
hash(&id)
|
||||
hash(&id.as_ref())
|
||||
} else {
|
||||
id
|
||||
}
|
||||
|
@ -191,7 +191,7 @@ mod tests {
|
|||
#[test]
|
||||
fn test_entry_verify() {
|
||||
let zero = Hash::default();
|
||||
let one = hash(&zero);
|
||||
let one = hash(&zero.as_ref());
|
||||
assert!(Entry::new_tick(0, &zero).verify(&zero)); // base case
|
||||
assert!(!Entry::new_tick(0, &zero).verify(&one)); // base case, bad
|
||||
assert!(next_entry(&zero, 1, vec![]).verify(&zero)); // inductive step
|
||||
|
|
26
src/hash.rs
26
src/hash.rs
|
@ -1,11 +1,31 @@
|
|||
//! The `hash` module provides functions for creating SHA-256 hashes.
|
||||
|
||||
use bs58;
|
||||
use generic_array::typenum::U32;
|
||||
use generic_array::GenericArray;
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::fmt;
|
||||
|
||||
pub type Hash = GenericArray<u8, U32>;
|
||||
#[derive(Serialize, Deserialize, Clone, Copy, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
|
||||
pub struct Hash(GenericArray<u8, U32>);
|
||||
|
||||
impl AsRef<[u8]> for Hash {
|
||||
fn as_ref(&self) -> &[u8] {
|
||||
&self.0[..]
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for Hash {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{}", bs58::encode(self.0).into_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Hash {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{}", bs58::encode(self.0).into_string())
|
||||
}
|
||||
}
|
||||
/// Return a Sha256 hash for the given data.
|
||||
pub fn hash(val: &[u8]) -> Hash {
|
||||
let mut hasher = Sha256::default();
|
||||
|
@ -13,12 +33,12 @@ pub fn hash(val: &[u8]) -> Hash {
|
|||
|
||||
// At the time of this writing, the sha2 library is stuck on an old version
|
||||
// of generic_array (0.9.0). Decouple ourselves with a clone to our version.
|
||||
GenericArray::clone_from_slice(hasher.result().as_slice())
|
||||
Hash(GenericArray::clone_from_slice(hasher.result().as_slice()))
|
||||
}
|
||||
|
||||
/// Return the hash of the given hash extended with the given value.
|
||||
pub fn extend_and_hash(id: &Hash, val: &[u8]) -> Hash {
|
||||
let mut hash_data = id.to_vec();
|
||||
let mut hash_data = id.as_ref().to_vec();
|
||||
hash_data.extend_from_slice(val);
|
||||
hash(&hash_data)
|
||||
}
|
||||
|
|
|
@ -324,7 +324,7 @@ mod tests {
|
|||
#[test]
|
||||
fn test_verify_slice() {
|
||||
let zero = Hash::default();
|
||||
let one = hash(&zero);
|
||||
let one = hash(&zero.as_ref());
|
||||
assert!(vec![][..].verify(&zero)); // base case
|
||||
assert!(vec![Entry::new_tick(0, &zero)][..].verify(&zero)); // singleton case 1
|
||||
assert!(!vec![Entry::new_tick(0, &zero)][..].verify(&one)); // singleton case 2, bad
|
||||
|
@ -337,7 +337,7 @@ mod tests {
|
|||
|
||||
fn make_test_entries() -> Vec<Entry> {
|
||||
let zero = Hash::default();
|
||||
let one = hash(&zero);
|
||||
let one = hash(&zero.as_ref());
|
||||
let keypair = KeyPair::new();
|
||||
let tx0 = Transaction::new_vote(
|
||||
&keypair,
|
||||
|
@ -388,7 +388,7 @@ mod tests {
|
|||
use logger;
|
||||
logger::setup();
|
||||
let id = Hash::default();
|
||||
let next_id = hash(&id);
|
||||
let next_id = hash(&id.as_ref());
|
||||
let keypair = KeyPair::new();
|
||||
let tx_small = Transaction::new_vote(
|
||||
&keypair,
|
||||
|
|
|
@ -23,7 +23,7 @@ impl Recorder {
|
|||
}
|
||||
|
||||
pub fn hash(&mut self) {
|
||||
self.last_hash = hash(&self.last_hash);
|
||||
self.last_hash = hash(&self.last_hash.as_ref());
|
||||
self.num_hashes += 1;
|
||||
}
|
||||
|
||||
|
|
|
@ -251,7 +251,7 @@ pub mod tests {
|
|||
for i in 0..num_transfers {
|
||||
let entry0 = Entry::new(&cur_hash, i, vec![], false);
|
||||
bank.register_entry_id(&cur_hash);
|
||||
cur_hash = hash(&cur_hash);
|
||||
cur_hash = hash(&cur_hash.as_ref());
|
||||
|
||||
let tx0 = Transaction::new(
|
||||
&mint.keypair(),
|
||||
|
@ -260,10 +260,10 @@ pub mod tests {
|
|||
cur_hash,
|
||||
);
|
||||
bank.register_entry_id(&cur_hash);
|
||||
cur_hash = hash(&cur_hash);
|
||||
cur_hash = hash(&cur_hash.as_ref());
|
||||
let entry1 = Entry::new(&cur_hash, i + num_transfers, vec![tx0], false);
|
||||
bank.register_entry_id(&cur_hash);
|
||||
cur_hash = hash(&cur_hash);
|
||||
cur_hash = hash(&cur_hash.as_ref());
|
||||
|
||||
alice_ref_balance -= transfer_amount;
|
||||
|
||||
|
|
|
@ -34,7 +34,7 @@ pub fn create_vote_tx_and_blob(
|
|||
let (vote, addr) = {
|
||||
let mut wcrdt = crdt.write().unwrap();
|
||||
//TODO: doesn't seem like there is a synchronous call to get height and id
|
||||
info!("voting on {:?}", &last_id[..8]);
|
||||
info!("voting on {:?}", &last_id.as_ref()[..8]);
|
||||
wcrdt.new_vote(last_id)
|
||||
}?;
|
||||
let tx = Transaction::new_vote(&keypair, vote, last_id, 0);
|
||||
|
|
Loading…
Reference in New Issue