diff --git a/src/accountant.rs b/src/accountant.rs index 3af394965..61838382c 100644 --- a/src/accountant.rs +++ b/src/accountant.rs @@ -2,7 +2,8 @@ //! event log to record transactions. Its users can deposit funds and //! transfer funds to other users. -use log::{Entry, Sha256Hash}; +use hash::Sha256Hash; +use log::Entry; use event::Event; use transaction::Transaction; use signature::{PublicKey, Signature}; diff --git a/src/accountant_skel.rs b/src/accountant_skel.rs index 4c6c12a98..9296b6a17 100644 --- a/src/accountant_skel.rs +++ b/src/accountant_skel.rs @@ -2,7 +2,8 @@ use std::io; use accountant::Accountant; use transaction::Transaction; use signature::PublicKey; -use log::{Entry, Sha256Hash}; +use hash::Sha256Hash; +use log::Entry; use std::net::UdpSocket; use bincode::{deserialize, serialize}; diff --git a/src/accountant_stub.rs b/src/accountant_stub.rs index ea713a9b5..1bded971e 100644 --- a/src/accountant_stub.rs +++ b/src/accountant_stub.rs @@ -7,7 +7,8 @@ use std::io; use bincode::{deserialize, serialize}; use transaction::Transaction; use signature::{PublicKey, Signature}; -use log::{Entry, Sha256Hash}; +use hash::Sha256Hash; +use log::Entry; use ring::signature::Ed25519KeyPair; use accountant_skel::{Request, Response}; diff --git a/src/bin/demo.rs b/src/bin/demo.rs index bdbad0ba2..76c27dc30 100644 --- a/src/bin/demo.rs +++ b/src/bin/demo.rs @@ -1,7 +1,8 @@ extern crate silk; use silk::historian::Historian; -use silk::log::{verify_slice, Entry, Sha256Hash}; +use silk::hash::Sha256Hash; +use silk::log::{verify_slice, Entry}; use silk::signature::{generate_keypair, get_pubkey}; use silk::transaction::Transaction; use silk::event::Event; diff --git a/src/genesis.rs b/src/genesis.rs index 01bcd1416..6ef4151ad 100644 --- a/src/genesis.rs +++ b/src/genesis.rs @@ -3,7 +3,8 @@ use event::Event; use transaction::Transaction; use signature::{generate_keypair, get_pubkey, PublicKey}; -use log::{create_entries, hash, Entry, Sha256Hash}; +use log::{create_entries, Entry}; +use hash::{hash, Sha256Hash}; use ring::rand::SystemRandom; use ring::signature::Ed25519KeyPair; use untrusted::Input; diff --git a/src/hash.rs b/src/hash.rs new file mode 100644 index 000000000..5efb933f5 --- /dev/null +++ b/src/hash.rs @@ -0,0 +1,19 @@ +use generic_array::GenericArray; +use generic_array::typenum::U32; +use sha2::{Digest, Sha256}; + +pub type Sha256Hash = GenericArray; + +/// Return a Sha256 hash for the given data. +pub fn hash(val: &[u8]) -> Sha256Hash { + let mut hasher = Sha256::default(); + hasher.input(val); + hasher.result() +} + +/// Return the hash of the given hash extended with the given value. +pub fn extend_and_hash(id: &Sha256Hash, val: &[u8]) -> Sha256Hash { + let mut hash_data = id.to_vec(); + hash_data.extend_from_slice(val); + hash(&hash_data) +} diff --git a/src/historian.rs b/src/historian.rs index 03605a247..f0683c8f7 100644 --- a/src/historian.rs +++ b/src/historian.rs @@ -5,7 +5,8 @@ use std::thread::{spawn, JoinHandle}; use std::collections::HashSet; use std::sync::mpsc::{sync_channel, Receiver, SyncSender}; use std::time::Instant; -use log::{hash, Entry, Sha256Hash}; +use hash::{hash, Sha256Hash}; +use log::Entry; use logger::{ExitReason, Logger}; use signature::Signature; use event::Event; diff --git a/src/lib.rs b/src/lib.rs index 91cbf8b70..4551c6833 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,5 @@ #![cfg_attr(feature = "unstable", feature(test))] +pub mod hash; pub mod log; pub mod logger; pub mod event; diff --git a/src/log.rs b/src/log.rs index 1da66d778..6a13f3abe 100644 --- a/src/log.rs +++ b/src/log.rs @@ -13,15 +13,11 @@ /// fastest processor. Duration should therefore be estimated by assuming that the hash /// was generated by the fastest processor at the time the entry was logged. -use generic_array::GenericArray; -use generic_array::typenum::U32; +use hash::{extend_and_hash, hash, Sha256Hash}; use serde::Serialize; use event::Event; -use sha2::{Digest, Sha256}; use rayon::prelude::*; -pub type Sha256Hash = GenericArray; - #[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)] pub struct Entry { pub num_hashes: u64, @@ -50,20 +46,6 @@ impl Entry { } } -/// Return a Sha256 hash for the given data. -pub fn hash(val: &[u8]) -> Sha256Hash { - let mut hasher = Sha256::default(); - hasher.input(val); - hasher.result() -} - -/// Return the hash of the given hash extended with the given value. -pub fn extend_and_hash(id: &Sha256Hash, val: &[u8]) -> Sha256Hash { - let mut hash_data = id.to_vec(); - hash_data.extend_from_slice(val); - hash(&hash_data) -} - /// Creates the hash 'num_hashes' after start_hash. If the event contains /// signature, the final hash will be a hash of both the previous ID and /// the signature. diff --git a/src/logger.rs b/src/logger.rs index 8fcf101f6..8f47bc97c 100644 --- a/src/logger.rs +++ b/src/logger.rs @@ -7,7 +7,8 @@ use std::sync::mpsc::{Receiver, SyncSender, TryRecvError}; use std::time::{Duration, Instant}; -use log::{create_entry_mut, Entry, Sha256Hash}; +use hash::Sha256Hash; +use log::{create_entry_mut, Entry}; use event::Event; use serde::Serialize; use std::fmt::Debug; diff --git a/src/transaction.rs b/src/transaction.rs index f0be7c1d8..119335866 100644 --- a/src/transaction.rs +++ b/src/transaction.rs @@ -4,7 +4,7 @@ use signature::{get_pubkey, verify_signature, PublicKey, Signature}; use ring::signature::Ed25519KeyPair; use serde::Serialize; use bincode::serialize; -use log::Sha256Hash; +use hash::Sha256Hash; #[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)] pub struct Transaction { @@ -51,8 +51,8 @@ impl Transaction { mod tests { use super::*; use bincode::{deserialize, serialize}; - use log::*; use signature::*; + use hash::hash; #[test] fn test_claim() {