diff --git a/src/accountant.rs b/src/accountant.rs index 755a5510f1..5645a9271e 100644 --- a/src/accountant.rs +++ b/src/accountant.rs @@ -3,8 +3,8 @@ //! transfer funds to other users. use log::{Entry, Sha256Hash}; -use event::{get_pubkey, sign_transaction_data, verify_transfer, Event, PublicKey, Signature, - Transfer}; +use event::{get_pubkey, sign_transaction_data, verify_transaction, Event, PublicKey, Signature, + Transaction}; use genesis::Genesis; use historian::{reserve_signature, Historian}; use ring::signature::Ed25519KeyPair; @@ -76,8 +76,8 @@ impl Accountant { allow_deposits && from == to } - pub fn process_transfer(self: &mut Self, tr: Transfer) -> Result<()> { - if !verify_transfer(&tr) { + pub fn process_transaction(self: &mut Self, tr: Transaction) -> Result<()> { + if !verify_transaction(&tr) { return Err(AccountingError::InvalidTransfer); } @@ -85,7 +85,7 @@ impl Accountant { return Err(AccountingError::InsufficientFunds); } - self.process_verified_transfer(&tr, false)?; + self.process_verified_transaction(&tr, false)?; if let Err(SendError(_)) = self.historian.sender.send(Event::Transaction(tr)) { return Err(AccountingError::SendError); } @@ -93,9 +93,9 @@ impl Accountant { Ok(()) } - fn process_verified_transfer( + fn process_verified_transaction( self: &mut Self, - tr: &Transfer, + tr: &Transaction, allow_deposits: bool, ) -> Result<()> { if !reserve_signature(&mut self.historian.signatures, &tr.sig) { @@ -126,7 +126,7 @@ impl Accountant { ) -> Result<()> { match *event { Event::Tick => Ok(()), - Event::Transaction(ref tr) => self.process_verified_transfer(tr, allow_deposits), + Event::Transaction(ref tr) => self.process_verified_transaction(tr, allow_deposits), } } @@ -139,14 +139,14 @@ impl Accountant { let from = get_pubkey(keypair); let last_id = self.last_id; let sig = sign_transaction_data(&n, keypair, &to, &last_id); - let tr = Transfer { + let tr = Transaction { from, to, data: n, last_id, sig, }; - self.process_transfer(tr).map(|_| sig) + self.process_transaction(tr).map(|_| sig) } pub fn get_balance(self: &Self, pubkey: &PublicKey) -> Option { diff --git a/src/accountant_skel.rs b/src/accountant_skel.rs index ae82024644..262fe2cba8 100644 --- a/src/accountant_skel.rs +++ b/src/accountant_skel.rs @@ -1,6 +1,6 @@ use std::io; use accountant::Accountant; -use event::{Event, PublicKey, Transfer}; +use event::{PublicKey, Transaction}; use log::{Entry, Sha256Hash}; use std::net::UdpSocket; use bincode::{deserialize, serialize}; @@ -11,7 +11,7 @@ pub struct AccountantSkel { #[derive(Serialize, Deserialize, Debug)] pub enum Request { - Transaction(Transfer), + Transaction(Transaction), GetBalance { key: PublicKey }, GetEntries { last_id: Sha256Hash }, GetId { is_last: bool }, @@ -32,8 +32,8 @@ impl AccountantSkel { pub fn process_request(self: &mut Self, msg: Request) -> Option { match msg { Request::Transaction(tr) => { - if let Err(err) = self.acc.process_transfer(tr) { - eprintln!("Transfer error: {:?}", err); + if let Err(err) = self.acc.process_transaction(tr) { + eprintln!("Transaction error: {:?}", err); } None } diff --git a/src/accountant_stub.rs b/src/accountant_stub.rs index d39858e010..631ff5440f 100644 --- a/src/accountant_stub.rs +++ b/src/accountant_stub.rs @@ -5,7 +5,7 @@ use std::net::UdpSocket; use std::io; use bincode::{deserialize, serialize}; -use event::{get_pubkey, get_signature, sign_transaction_data, PublicKey, Signature, Transfer}; +use event::{get_pubkey, get_signature, sign_transaction_data, PublicKey, Signature, Transaction}; use log::{Entry, Sha256Hash}; use ring::signature::Ed25519KeyPair; use accountant_skel::{Request, Response}; @@ -33,7 +33,7 @@ impl AccountantStub { last_id: Sha256Hash, sig: Signature, ) -> io::Result { - let req = Request::Transaction(Transfer { + let req = Request::Transaction(Transaction { from, to, data: val, diff --git a/src/bin/client-demo.rs b/src/bin/client-demo.rs index 5e66d0c871..2f518e8cb4 100644 --- a/src/bin/client-demo.rs +++ b/src/bin/client-demo.rs @@ -3,7 +3,7 @@ extern crate silk; use silk::accountant_stub::AccountantStub; use silk::event::{generate_keypair, get_pubkey, sign_transaction_data, verify_event, Event, - Transfer}; + Transaction}; use silk::genesis::Genesis; use std::time::Instant; use std::net::UdpSocket; @@ -48,7 +48,7 @@ fn main() { println!("Verify signatures..."); let now = Instant::now(); for &(k, s) in &sigs { - let e = Event::Transaction(Transfer { + let e = Event::Transaction(Transaction { from: alice_pubkey, to: k, data: one, diff --git a/src/event.rs b/src/event.rs index 94f5fc47f9..98d426be04 100644 --- a/src/event.rs +++ b/src/event.rs @@ -1,17 +1,4 @@ -//! The `log` crate provides the foundational data structures for Proof-of-History, -//! an ordered log of events in time. - -/// Each log 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' -/// field points to an Event that took place shortly after 'id' was generated. -/// -/// If you divide 'num_hashes' by the amount of time it takes to generate a new hash, you -/// get a duration estimate since the last event. Since processing power increases -/// over time, one should expect the duration 'num_hashes' represents to decrease proportionally. -/// 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 logged. +//! The `event` crate provides the data structures for log events. use generic_array::GenericArray; use generic_array::typenum::{U32, U64}; @@ -26,7 +13,7 @@ pub type PublicKey = GenericArray; pub type Signature = GenericArray; #[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)] -pub struct Transfer { +pub struct Transaction { pub from: PublicKey, pub to: PublicKey, pub data: T, @@ -34,9 +21,9 @@ pub struct Transfer { pub sig: Signature, } -impl Transfer { +impl Transaction { pub fn new_claim(to: PublicKey, data: T, last_id: Sha256Hash, sig: Signature) -> Self { - Transfer { + Transaction { from: to, to, data, @@ -54,12 +41,12 @@ impl Transfer { #[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)] pub enum Event { Tick, - Transaction(Transfer), + Transaction(Transaction), } impl Event { pub fn new_claim(to: PublicKey, data: T, last_id: Sha256Hash, sig: Signature) -> Self { - Event::Transaction(Transfer::new_claim(to, data, last_id, sig)) + Event::Transaction(Transaction::new_claim(to, data, last_id, sig)) } } @@ -119,11 +106,11 @@ pub fn get_signature(event: &Event) -> Option { pub fn verify_event(event: &Event) -> bool { match *event { Event::Tick => true, - Event::Transaction(ref tr) => verify_transfer(tr), + Event::Transaction(ref tr) => verify_transaction(tr), } } -pub fn verify_transfer(tr: &Transfer) -> bool { +pub fn verify_transaction(tr: &Transaction) -> bool { let sign_data = serialize(&(&tr.from, &tr.to, &tr.data, &tr.last_id)).unwrap(); verify_signature(&tr.from, &sign_data, &tr.sig) } diff --git a/src/genesis.rs b/src/genesis.rs index 2d8be7c2b6..d379590b4f 100644 --- a/src/genesis.rs +++ b/src/genesis.rs @@ -1,6 +1,6 @@ //! A library for generating the chain's genesis block. -use event::{generate_keypair, get_pubkey, sign_transaction_data, Event, PublicKey, Transfer}; +use event::{generate_keypair, get_pubkey, sign_transaction_data, Event, PublicKey, Transaction}; use log::{create_entries, hash, Entry, Sha256Hash}; use ring::rand::SystemRandom; use ring::signature::Ed25519KeyPair; @@ -56,7 +56,7 @@ impl Genesis { let last_id = self.get_seed(); let from = self.get_pubkey(); let sig = sign_transaction_data(&data, &self.get_keypair(), to, &last_id); - Event::Transaction(Transfer { + Event::Transaction(Transaction { from, to: *to, data, @@ -93,7 +93,7 @@ mod tests { fn test_create_events() { let mut events = Genesis::new(100, vec![]).create_events().into_iter(); assert_eq!(events.next().unwrap(), Event::Tick); - if let Event::Transaction(Transfer { from, to, .. }) = events.next().unwrap() { + if let Event::Transaction(Transaction { from, to, .. }) = events.next().unwrap() { assert_eq!(from, to); } else { assert!(false); diff --git a/src/historian.rs b/src/historian.rs index bdb019ed9a..7b700e0613 100644 --- a/src/historian.rs +++ b/src/historian.rs @@ -114,7 +114,7 @@ mod tests { let data = b"hello, world"; let zero = Sha256Hash::default(); let sig = sign_claim_data(&data, &keypair, &zero); - let tr0 = Transfer::new_claim(to, &data, zero, sig); + let tr0 = Transaction::new_claim(to, &data, zero, sig); let mut sigs = HashSet::new(); assert!(reserve_signature(&mut sigs, &tr0.sig)); assert!(!reserve_signature(&mut sigs, &tr0.sig)); diff --git a/src/log.rs b/src/log.rs index 6dc20792ca..b5af9abf15 100644 --- a/src/log.rs +++ b/src/log.rs @@ -169,7 +169,7 @@ pub fn next_ticks(start_hash: &Sha256Hash, num_hashes: u64, len: usize) -> Vec