Move hash into its own module
This commit is contained in:
@ -2,7 +2,8 @@
|
|||||||
//! event log to record transactions. Its users can deposit funds and
|
//! event log to record transactions. Its users can deposit funds and
|
||||||
//! transfer funds to other users.
|
//! transfer funds to other users.
|
||||||
|
|
||||||
use log::{Entry, Sha256Hash};
|
use hash::Sha256Hash;
|
||||||
|
use log::Entry;
|
||||||
use event::Event;
|
use event::Event;
|
||||||
use transaction::Transaction;
|
use transaction::Transaction;
|
||||||
use signature::{PublicKey, Signature};
|
use signature::{PublicKey, Signature};
|
||||||
|
@ -2,7 +2,8 @@ use std::io;
|
|||||||
use accountant::Accountant;
|
use accountant::Accountant;
|
||||||
use transaction::Transaction;
|
use transaction::Transaction;
|
||||||
use signature::PublicKey;
|
use signature::PublicKey;
|
||||||
use log::{Entry, Sha256Hash};
|
use hash::Sha256Hash;
|
||||||
|
use log::Entry;
|
||||||
use std::net::UdpSocket;
|
use std::net::UdpSocket;
|
||||||
use bincode::{deserialize, serialize};
|
use bincode::{deserialize, serialize};
|
||||||
|
|
||||||
|
@ -7,7 +7,8 @@ use std::io;
|
|||||||
use bincode::{deserialize, serialize};
|
use bincode::{deserialize, serialize};
|
||||||
use transaction::Transaction;
|
use transaction::Transaction;
|
||||||
use signature::{PublicKey, Signature};
|
use signature::{PublicKey, Signature};
|
||||||
use log::{Entry, Sha256Hash};
|
use hash::Sha256Hash;
|
||||||
|
use log::Entry;
|
||||||
use ring::signature::Ed25519KeyPair;
|
use ring::signature::Ed25519KeyPair;
|
||||||
use accountant_skel::{Request, Response};
|
use accountant_skel::{Request, Response};
|
||||||
|
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
extern crate silk;
|
extern crate silk;
|
||||||
|
|
||||||
use silk::historian::Historian;
|
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::signature::{generate_keypair, get_pubkey};
|
||||||
use silk::transaction::Transaction;
|
use silk::transaction::Transaction;
|
||||||
use silk::event::Event;
|
use silk::event::Event;
|
||||||
|
@ -3,7 +3,8 @@
|
|||||||
use event::Event;
|
use event::Event;
|
||||||
use transaction::Transaction;
|
use transaction::Transaction;
|
||||||
use signature::{generate_keypair, get_pubkey, PublicKey};
|
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::rand::SystemRandom;
|
||||||
use ring::signature::Ed25519KeyPair;
|
use ring::signature::Ed25519KeyPair;
|
||||||
use untrusted::Input;
|
use untrusted::Input;
|
||||||
|
19
src/hash.rs
Normal file
19
src/hash.rs
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
use generic_array::GenericArray;
|
||||||
|
use generic_array::typenum::U32;
|
||||||
|
use sha2::{Digest, Sha256};
|
||||||
|
|
||||||
|
pub type Sha256Hash = GenericArray<u8, U32>;
|
||||||
|
|
||||||
|
/// 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)
|
||||||
|
}
|
@ -5,7 +5,8 @@ use std::thread::{spawn, JoinHandle};
|
|||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
use std::sync::mpsc::{sync_channel, Receiver, SyncSender};
|
use std::sync::mpsc::{sync_channel, Receiver, SyncSender};
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
use log::{hash, Entry, Sha256Hash};
|
use hash::{hash, Sha256Hash};
|
||||||
|
use log::Entry;
|
||||||
use logger::{ExitReason, Logger};
|
use logger::{ExitReason, Logger};
|
||||||
use signature::Signature;
|
use signature::Signature;
|
||||||
use event::Event;
|
use event::Event;
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
#![cfg_attr(feature = "unstable", feature(test))]
|
#![cfg_attr(feature = "unstable", feature(test))]
|
||||||
|
pub mod hash;
|
||||||
pub mod log;
|
pub mod log;
|
||||||
pub mod logger;
|
pub mod logger;
|
||||||
pub mod event;
|
pub mod event;
|
||||||
|
20
src/log.rs
20
src/log.rs
@ -13,15 +13,11 @@
|
|||||||
/// fastest processor. Duration should therefore be estimated by assuming that the hash
|
/// 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.
|
/// was generated by the fastest processor at the time the entry was logged.
|
||||||
|
|
||||||
use generic_array::GenericArray;
|
use hash::{extend_and_hash, hash, Sha256Hash};
|
||||||
use generic_array::typenum::U32;
|
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use event::Event;
|
use event::Event;
|
||||||
use sha2::{Digest, Sha256};
|
|
||||||
use rayon::prelude::*;
|
use rayon::prelude::*;
|
||||||
|
|
||||||
pub type Sha256Hash = GenericArray<u8, U32>;
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
|
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
|
||||||
pub struct Entry<T> {
|
pub struct Entry<T> {
|
||||||
pub num_hashes: u64,
|
pub num_hashes: u64,
|
||||||
@ -50,20 +46,6 @@ impl<T: Serialize> Entry<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 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
|
/// 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
|
/// signature, the final hash will be a hash of both the previous ID and
|
||||||
/// the signature.
|
/// the signature.
|
||||||
|
@ -7,7 +7,8 @@
|
|||||||
|
|
||||||
use std::sync::mpsc::{Receiver, SyncSender, TryRecvError};
|
use std::sync::mpsc::{Receiver, SyncSender, TryRecvError};
|
||||||
use std::time::{Duration, Instant};
|
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 event::Event;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
|
@ -4,7 +4,7 @@ use signature::{get_pubkey, verify_signature, PublicKey, Signature};
|
|||||||
use ring::signature::Ed25519KeyPair;
|
use ring::signature::Ed25519KeyPair;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use bincode::serialize;
|
use bincode::serialize;
|
||||||
use log::Sha256Hash;
|
use hash::Sha256Hash;
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
|
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
|
||||||
pub struct Transaction<T> {
|
pub struct Transaction<T> {
|
||||||
@ -51,8 +51,8 @@ impl<T: Serialize> Transaction<T> {
|
|||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use bincode::{deserialize, serialize};
|
use bincode::{deserialize, serialize};
|
||||||
use log::*;
|
|
||||||
use signature::*;
|
use signature::*;
|
||||||
|
use hash::hash;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_claim() {
|
fn test_claim() {
|
||||||
|
Reference in New Issue
Block a user