diff --git a/src/accountant.rs b/src/accountant.rs index bdad3cf6bf..9d6602e57d 100644 --- a/src/accountant.rs +++ b/src/accountant.rs @@ -2,20 +2,20 @@ //! event log to record transactions. Its users can deposit funds and //! transfer funds to other users. -use hash::Hash; +use chrono::prelude::*; use entry::Entry; use event::Event; -use plan::{Plan, Witness}; -use transaction::Transaction; -use signature::{KeyPair, PublicKey, Signature}; -use mint::Mint; +use hash::Hash; use historian::Historian; +use mint::Mint; +use plan::{Plan, Witness}; use recorder::Signal; -use std::sync::mpsc::SendError; -use std::collections::{HashMap, HashSet}; +use signature::{KeyPair, PublicKey, Signature}; use std::collections::hash_map::Entry::Occupied; +use std::collections::{HashMap, HashSet}; use std::result; -use chrono::prelude::*; +use std::sync::mpsc::SendError; +use transaction::Transaction; #[derive(Debug, PartialEq, Eq)] pub enum AccountingError { @@ -223,8 +223,8 @@ impl Accountant { #[cfg(test)] mod tests { use super::*; - use signature::KeyPairUtil; use recorder::ExitReason; + use signature::KeyPairUtil; #[test] fn test_accountant() { diff --git a/src/accountant_skel.rs b/src/accountant_skel.rs index 2f82f6441c..d4c175edbc 100644 --- a/src/accountant_skel.rs +++ b/src/accountant_skel.rs @@ -1,24 +1,26 @@ use accountant::Accountant; -use transaction::Transaction; -use signature::PublicKey; -use hash::Hash; -use entry::Entry; -use std::net::UdpSocket; use bincode::{deserialize, serialize}; +use entry::Entry; +use hash::Hash; use result::Result; -use streamer; -use std::sync::{Arc, Mutex}; -use std::time::Duration; +use serde_json; +use signature::PublicKey; +use std::default::Default; +use std::io::Write; +use std::net::UdpSocket; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::mpsc::channel; +use std::sync::{Arc, Mutex}; use std::thread::{spawn, JoinHandle}; -use std::default::Default; -use serde_json; +use std::time::Duration; +use streamer; +use transaction::Transaction; -pub struct AccountantSkel { +pub struct AccountantSkel { pub acc: Accountant, pub last_id: Hash, pub ledger: Vec, + writer: W, } #[cfg_attr(feature = "cargo-clippy", allow(large_enum_variant))] @@ -37,20 +39,21 @@ pub enum Response { Id { id: Hash, is_last: bool }, } -impl AccountantSkel { - pub fn new(acc: Accountant) -> Self { +impl AccountantSkel { + pub fn new(acc: Accountant, w: W) -> Self { let last_id = acc.first_id; AccountantSkel { acc, last_id, ledger: vec![], + writer: w, } } - pub fn sync(self: &mut Self) -> Hash { + pub fn sync(&mut self) -> Hash { while let Ok(entry) = self.acc.historian.receiver.try_recv() { self.last_id = entry.id; - println!("{}", serde_json::to_string(&entry).unwrap()); + write!(self.writer, "{}", serde_json::to_string(&entry).unwrap()).unwrap(); self.ledger.push(entry); } self.last_id @@ -131,7 +134,7 @@ impl AccountantSkel { /// UDP Server that forwards messages to Accountant methods. pub fn serve( - obj: Arc>, + obj: Arc>>, addr: &str, exit: Arc, ) -> Result>> { diff --git a/src/accountant_stub.rs b/src/accountant_stub.rs index 8bed5a20c5..4336cdd0f4 100644 --- a/src/accountant_stub.rs +++ b/src/accountant_stub.rs @@ -2,14 +2,14 @@ //! event log to record transactions. Its users can deposit funds and //! transfer funds to other users. -use std::net::UdpSocket; -use std::io; -use bincode::{deserialize, serialize}; -use transaction::Transaction; -use signature::{KeyPair, PublicKey, Signature}; -use hash::Hash; -use entry::Entry; use accountant_skel::{Request, Response}; +use bincode::{deserialize, serialize}; +use entry::Entry; +use hash::Hash; +use signature::{KeyPair, PublicKey, Signature}; +use std::io; +use std::net::UdpSocket; +use transaction::Transaction; pub struct AccountantStub { pub addr: String, @@ -122,12 +122,13 @@ mod tests { use super::*; use accountant::Accountant; use accountant_skel::AccountantSkel; - use std::thread::sleep; - use std::time::Duration; use mint::Mint; use signature::{KeyPair, KeyPairUtil}; - use std::sync::{Arc, Mutex}; + use std::io::sink; use std::sync::atomic::{AtomicBool, Ordering}; + use std::sync::{Arc, Mutex}; + use std::thread::sleep; + use std::time::Duration; #[test] fn test_accountant_stub() { @@ -137,9 +138,9 @@ mod tests { let acc = Accountant::new(&alice, Some(30)); let bob_pubkey = KeyPair::new().pubkey(); let exit = Arc::new(AtomicBool::new(false)); - let acc = Arc::new(Mutex::new(AccountantSkel::new(acc))); + let acc = Arc::new(Mutex::new(AccountantSkel::new(acc, sink()))); let threads = AccountantSkel::serve(acc, addr, exit.clone()).unwrap(); - sleep(Duration::from_millis(30)); + sleep(Duration::from_millis(300)); let socket = UdpSocket::bind(send_addr).unwrap(); let mut acc = AccountantStub::new(addr, socket); diff --git a/src/bin/client-demo.rs b/src/bin/client-demo.rs index eec97db87c..42916f84d6 100644 --- a/src/bin/client-demo.rs +++ b/src/bin/client-demo.rs @@ -2,12 +2,12 @@ extern crate serde_json; extern crate silk; use silk::accountant_stub::AccountantStub; +use silk::mint::Mint; use silk::signature::{KeyPair, KeyPairUtil}; use silk::transaction::Transaction; -use silk::mint::Mint; -use std::time::Instant; -use std::net::UdpSocket; use std::io::stdin; +use std::net::UdpSocket; +use std::time::Instant; fn main() { let addr = "127.0.0.1:8000"; diff --git a/src/bin/genesis-demo.rs b/src/bin/genesis-demo.rs index c5fddfead3..2c8f6e9ff1 100644 --- a/src/bin/genesis-demo.rs +++ b/src/bin/genesis-demo.rs @@ -1,12 +1,12 @@ extern crate serde_json; extern crate silk; -use silk::mint::Mint; -use silk::event::Event; -use silk::transaction::Transaction; use silk::entry::create_entry; -use silk::signature::{KeyPair, KeyPairUtil, PublicKey}; +use silk::event::Event; use silk::hash::Hash; +use silk::mint::Mint; +use silk::signature::{KeyPair, KeyPairUtil, PublicKey}; +use silk::transaction::Transaction; use std::io::stdin; fn transfer(from: &KeyPair, (to, tokens): (PublicKey, i64), last_id: Hash) -> Event { diff --git a/src/bin/historian-demo.rs b/src/bin/historian-demo.rs index 19c3db536b..1652c79431 100644 --- a/src/bin/historian-demo.rs +++ b/src/bin/historian-demo.rs @@ -1,16 +1,16 @@ extern crate silk; -use silk::historian::Historian; -use silk::hash::Hash; use silk::entry::Entry; +use silk::event::Event; +use silk::hash::Hash; +use silk::historian::Historian; use silk::ledger::verify_slice; use silk::recorder::Signal; use silk::signature::{KeyPair, KeyPairUtil}; use silk::transaction::Transaction; -use silk::event::Event; +use std::sync::mpsc::SendError; use std::thread::sleep; use std::time::Duration; -use std::sync::mpsc::SendError; fn create_ledger(hist: &Historian, seed: &Hash) -> Result<(), SendError> { sleep(Duration::from_millis(15)); diff --git a/src/bin/testnode.rs b/src/bin/testnode.rs index 43b397d19f..dce8257a29 100644 --- a/src/bin/testnode.rs +++ b/src/bin/testnode.rs @@ -1,11 +1,11 @@ extern crate serde_json; extern crate silk; -use silk::accountant_skel::AccountantSkel; use silk::accountant::Accountant; -use std::io::{self, BufRead}; -use std::sync::{Arc, Mutex}; +use silk::accountant_skel::AccountantSkel; +use std::io::{self, stdout, BufRead}; use std::sync::atomic::AtomicBool; +use std::sync::{Arc, Mutex}; fn main() { let addr = "127.0.0.1:8000"; @@ -16,7 +16,7 @@ fn main() { .map(|line| serde_json::from_str(&line.unwrap()).unwrap()); let acc = Accountant::new_from_entries(entries, Some(1000)); let exit = Arc::new(AtomicBool::new(false)); - let skel = Arc::new(Mutex::new(AccountantSkel::new(acc))); + let skel = Arc::new(Mutex::new(AccountantSkel::new(acc, stdout()))); eprintln!("Listening on {}", addr); let threads = AccountantSkel::serve(skel, addr, exit.clone()).unwrap(); for t in threads { diff --git a/src/entry.rs b/src/entry.rs index 56a612bb49..f69d8d125e 100644 --- a/src/entry.rs +++ b/src/entry.rs @@ -1,5 +1,5 @@ -use hash::{extend_and_hash, hash, Hash}; use event::Event; +use hash::{extend_and_hash, hash, Hash}; #[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)] pub struct Entry { @@ -87,11 +87,11 @@ pub fn next_tick(start_hash: &Hash, num_hashes: u64) -> Entry { #[cfg(test)] mod tests { use super::*; + use entry::create_entry; + use event::Event; use hash::hash; use signature::{KeyPair, KeyPairUtil}; use transaction::Transaction; - use event::Event; - use entry::create_entry; #[test] fn test_entry_verify() { diff --git a/src/event.rs b/src/event.rs index 8afa18b219..a3933bc36a 100644 --- a/src/event.rs +++ b/src/event.rs @@ -1,9 +1,9 @@ //! The `event` crate provides the data structures for log events. +use bincode::serialize; +use chrono::prelude::*; use signature::{KeyPair, KeyPairUtil, PublicKey, Signature, SignatureUtil}; use transaction::Transaction; -use chrono::prelude::*; -use bincode::serialize; #[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)] pub enum Event { diff --git a/src/historian.rs b/src/historian.rs index a35f41d835..04a41703ef 100644 --- a/src/historian.rs +++ b/src/historian.rs @@ -1,14 +1,14 @@ //! The `historian` crate provides a microservice for generating a Proof-of-History. //! It manages a thread containing a Proof-of-History Logger. -use std::thread::{spawn, JoinHandle}; -use std::collections::HashSet; -use std::sync::mpsc::{sync_channel, Receiver, SyncSender}; -use std::time::Instant; -use hash::Hash; use entry::Entry; +use hash::Hash; use recorder::{ExitReason, Recorder, Signal}; use signature::Signature; +use std::collections::HashSet; +use std::sync::mpsc::{sync_channel, Receiver, SyncSender}; +use std::thread::{spawn, JoinHandle}; +use std::time::Instant; pub struct Historian { pub sender: SyncSender, diff --git a/src/ledger.rs b/src/ledger.rs index d858d91799..5beef93e9c 100644 --- a/src/ledger.rs +++ b/src/ledger.rs @@ -1,6 +1,7 @@ //! The `ledger` crate provides the foundational data structures for Proof-of-History, //! an ordered log of events in time. +use entry::{next_tick, Entry}; /// Each entry contains three pieces of data. The `num_hashes` field is the number /// of hashes performed since the previous entry. The `id` field is the result /// of hashing `id` from the previous entry `num_hashes` times. The `event` @@ -12,9 +13,7 @@ /// Though processing power varies across nodes, the network gives priority to the /// fastest processor. Duration should therefore be estimated by assuming that the hash /// was generated by the fastest processor at the time the entry was recorded. - use hash::Hash; -use entry::{next_tick, Entry}; use rayon::prelude::*; /// Verifies the hashes and counts of a slice of events are all consistent. diff --git a/src/lib.rs b/src/lib.rs index b3af5bcd7f..3826a1bba6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,19 +1,19 @@ #![cfg_attr(feature = "unstable", feature(test))] -pub mod signature; -pub mod hash; -pub mod plan; -pub mod transaction; -pub mod event; -pub mod entry; -pub mod ledger; -pub mod mint; -pub mod recorder; -pub mod historian; -pub mod streamer; pub mod accountant; pub mod accountant_skel; pub mod accountant_stub; +pub mod entry; +pub mod event; +pub mod hash; +pub mod historian; +pub mod ledger; +pub mod mint; +pub mod plan; +pub mod recorder; pub mod result; +pub mod signature; +pub mod streamer; +pub mod transaction; extern crate bincode; extern crate chrono; extern crate generic_array; diff --git a/src/mint.rs b/src/mint.rs index 74339cd690..5799d97910 100644 --- a/src/mint.rs +++ b/src/mint.rs @@ -1,12 +1,12 @@ //! A library for generating the chain's genesis block. -use event::Event; -use transaction::Transaction; -use signature::{KeyPair, KeyPairUtil, PublicKey}; use entry::Entry; use entry::create_entry; +use event::Event; use hash::{hash, Hash}; use ring::rand::SystemRandom; +use signature::{KeyPair, KeyPairUtil, PublicKey}; +use transaction::Transaction; use untrusted::Input; #[derive(Serialize, Deserialize, Debug)] diff --git a/src/plan.rs b/src/plan.rs index f28ccec452..3ef51eed80 100644 --- a/src/plan.rs +++ b/src/plan.rs @@ -3,8 +3,8 @@ //! which it uses to reduce the payment plan. When the plan is reduced to a //! `Payment`, the payment is executed. -use signature::PublicKey; use chrono::prelude::*; +use signature::PublicKey; use std::mem; pub enum Witness { diff --git a/src/recorder.rs b/src/recorder.rs index a590cd8783..14b6a26348 100644 --- a/src/recorder.rs +++ b/src/recorder.rs @@ -5,12 +5,12 @@ //! Event, the latest hash, and the number of hashes since the last event. //! The resulting stream of entries represents ordered events in time. -use std::sync::mpsc::{Receiver, SyncSender, TryRecvError}; -use std::time::{Duration, Instant}; -use std::mem; -use hash::{hash, Hash}; use entry::{create_entry_mut, Entry}; use event::Event; +use hash::{hash, Hash}; +use std::mem; +use std::sync::mpsc::{Receiver, SyncSender, TryRecvError}; +use std::time::{Duration, Instant}; #[cfg_attr(feature = "cargo-clippy", allow(large_enum_variant))] pub enum Signal { diff --git a/src/result.rs b/src/result.rs index 96f2f7ff49..ea96453175 100644 --- a/src/result.rs +++ b/src/result.rs @@ -1,7 +1,7 @@ +use bincode; use serde_json; use std; use std::any::Any; -use bincode; #[derive(Debug)] pub enum Error { @@ -62,16 +62,16 @@ impl std::convert::From> for Error { #[cfg(test)] mod tests { - use result::Result; use result::Error; + use result::Result; + use serde_json; + use std::io; + use std::io::Write; use std::net::SocketAddr; use std::sync::mpsc::RecvError; use std::sync::mpsc::RecvTimeoutError; - use std::thread; - use std::io; - use std::io::Write; - use serde_json; use std::sync::mpsc::channel; + use std::thread; fn addr_parse_error() -> Result { let r = "12fdfasfsafsadfs".parse()?; diff --git a/src/streamer.rs b/src/streamer.rs index fe0b9ed153..3ad270ad8d 100644 --- a/src/streamer.rs +++ b/src/streamer.rs @@ -1,11 +1,11 @@ -use std::sync::{Arc, Mutex, RwLock}; +use result::{Error, Result}; +use std::fmt; +use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, UdpSocket}; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::mpsc; -use std::fmt; -use std::time::Duration; -use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, UdpSocket}; +use std::sync::{Arc, Mutex, RwLock}; use std::thread::{spawn, JoinHandle}; -use result::{Error, Result}; +use std::time::Duration; const BLOCK_SIZE: usize = 1024 * 8; pub const PACKET_SIZE: usize = 256; @@ -282,15 +282,15 @@ pub fn responder( mod bench { extern crate test; use self::test::Bencher; - use std::thread::sleep; - use std::sync::{Arc, Mutex}; + use result::Result; use std::net::{SocketAddr, UdpSocket}; + use std::sync::atomic::{AtomicBool, Ordering}; + use std::sync::mpsc::channel; + use std::sync::{Arc, Mutex}; + use std::thread::sleep; + use std::thread::{spawn, JoinHandle}; use std::time::Duration; use std::time::SystemTime; - use std::thread::{spawn, JoinHandle}; - use std::sync::mpsc::channel; - use std::sync::atomic::{AtomicBool, Ordering}; - use result::Result; use streamer::{allocate, receiver, recycle, Packet, PacketRecycler, Receiver, PACKET_SIZE}; fn producer( @@ -381,13 +381,13 @@ mod bench { #[cfg(test)] mod test { - use std::sync::{Arc, Mutex}; + use std::io; + use std::io::Write; use std::net::UdpSocket; - use std::time::Duration; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::mpsc::channel; - use std::io::Write; - use std::io; + use std::sync::{Arc, Mutex}; + use std::time::Duration; use streamer::{allocate, receiver, responder, Packet, Packets, Receiver, Response, Responses, PACKET_SIZE}; diff --git a/src/transaction.rs b/src/transaction.rs index 482bdddf57..7e7674c195 100644 --- a/src/transaction.rs +++ b/src/transaction.rs @@ -1,10 +1,10 @@ //! The `transaction` crate provides functionality for creating log transactions. -use signature::{KeyPair, KeyPairUtil, PublicKey, Signature, SignatureUtil}; use bincode::serialize; -use hash::Hash; use chrono::prelude::*; +use hash::Hash; use plan::{Condition, Payment, Plan}; +use signature::{KeyPair, KeyPairUtil, PublicKey, Signature, SignatureUtil}; #[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)] pub struct Transaction {