Rename Transfer to Transaction
struct names should be nouns
This commit is contained in:
		| @@ -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<i64>) -> Result<()> { | ||||
|         if !verify_transfer(&tr) { | ||||
|     pub fn process_transaction(self: &mut Self, tr: Transaction<i64>) -> 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<i64>, | ||||
|         tr: &Transaction<i64>, | ||||
|         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<i64> { | ||||
|   | ||||
| @@ -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<i64>), | ||||
|     Transaction(Transaction<i64>), | ||||
|     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<Response> { | ||||
|         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 | ||||
|             } | ||||
|   | ||||
| @@ -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<usize> { | ||||
|         let req = Request::Transaction(Transfer { | ||||
|         let req = Request::Transaction(Transaction { | ||||
|             from, | ||||
|             to, | ||||
|             data: val, | ||||
|   | ||||
| @@ -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, | ||||
|   | ||||
							
								
								
									
										29
									
								
								src/event.rs
									
									
									
									
									
								
							
							
						
						
									
										29
									
								
								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<u8, U32>; | ||||
| pub type Signature = GenericArray<u8, U64>; | ||||
|  | ||||
| #[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)] | ||||
| pub struct Transfer<T> { | ||||
| pub struct Transaction<T> { | ||||
|     pub from: PublicKey, | ||||
|     pub to: PublicKey, | ||||
|     pub data: T, | ||||
| @@ -34,9 +21,9 @@ pub struct Transfer<T> { | ||||
|     pub sig: Signature, | ||||
| } | ||||
|  | ||||
| impl<T> Transfer<T> { | ||||
| impl<T> Transaction<T> { | ||||
|     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<T> Transfer<T> { | ||||
| #[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)] | ||||
| pub enum Event<T> { | ||||
|     Tick, | ||||
|     Transaction(Transfer<T>), | ||||
|     Transaction(Transaction<T>), | ||||
| } | ||||
|  | ||||
| impl<T> Event<T> { | ||||
|     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<T>(event: &Event<T>) -> Option<Signature> { | ||||
| pub fn verify_event<T: Serialize>(event: &Event<T>) -> bool { | ||||
|     match *event { | ||||
|         Event::Tick => true, | ||||
|         Event::Transaction(ref tr) => verify_transfer(tr), | ||||
|         Event::Transaction(ref tr) => verify_transaction(tr), | ||||
|     } | ||||
| } | ||||
|  | ||||
| pub fn verify_transfer<T: Serialize>(tr: &Transfer<T>) -> bool { | ||||
| pub fn verify_transaction<T: Serialize>(tr: &Transaction<T>) -> bool { | ||||
|     let sign_data = serialize(&(&tr.from, &tr.to, &tr.data, &tr.last_id)).unwrap(); | ||||
|     verify_signature(&tr.from, &sign_data, &tr.sig) | ||||
| } | ||||
|   | ||||
| @@ -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); | ||||
|   | ||||
| @@ -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)); | ||||
|   | ||||
| @@ -169,7 +169,7 @@ pub fn next_ticks(start_hash: &Sha256Hash, num_hashes: u64, len: usize) -> Vec<E | ||||
| #[cfg(test)] | ||||
| mod tests { | ||||
|     use super::*; | ||||
|     use event::{generate_keypair, get_pubkey, sign_claim_data, sign_transaction_data, Transfer}; | ||||
|     use event::{generate_keypair, get_pubkey, sign_claim_data, sign_transaction_data, Transaction}; | ||||
|  | ||||
|     #[test] | ||||
|     fn test_event_verify() { | ||||
| @@ -277,7 +277,7 @@ mod tests { | ||||
|         let keypair1 = generate_keypair(); | ||||
|         let pubkey1 = get_pubkey(&keypair1); | ||||
|         let data = hash(b"hello, world"); | ||||
|         let event0 = Event::Transaction(Transfer { | ||||
|         let event0 = Event::Transaction(Transaction { | ||||
|             from: get_pubkey(&keypair0), | ||||
|             to: pubkey1, | ||||
|             data, | ||||
| @@ -295,7 +295,7 @@ mod tests { | ||||
|         let pubkey1 = get_pubkey(&keypair1); | ||||
|         let data = hash(b"hello, world"); | ||||
|         let zero = Sha256Hash::default(); | ||||
|         let event0 = Event::Transaction(Transfer { | ||||
|         let event0 = Event::Transaction(Transaction { | ||||
|             from: get_pubkey(&keypair0), | ||||
|             to: pubkey1, | ||||
|             data: hash(b"goodbye cruel world"), // <-- attack! | ||||
| @@ -314,7 +314,7 @@ mod tests { | ||||
|         let pubkey1 = get_pubkey(&keypair1); | ||||
|         let data = hash(b"hello, world"); | ||||
|         let zero = Sha256Hash::default(); | ||||
|         let event0 = Event::Transaction(Transfer { | ||||
|         let event0 = Event::Transaction(Transaction { | ||||
|             from: get_pubkey(&keypair0), | ||||
|             to: get_pubkey(&thief_keypair), // <-- attack! | ||||
|             data: hash(b"goodbye cruel world"), | ||||
|   | ||||
		Reference in New Issue
	
	Block a user