Rename Transfer to Transaction

struct names should be nouns
This commit is contained in:
Greg Fitzgerald
2018-03-06 11:54:45 -07:00
parent a2811842c8
commit 66bf889c39
8 changed files with 34 additions and 47 deletions

View File

@ -3,8 +3,8 @@
//! transfer funds to other users. //! transfer funds to other users.
use log::{Entry, Sha256Hash}; use log::{Entry, Sha256Hash};
use event::{get_pubkey, sign_transaction_data, verify_transfer, Event, PublicKey, Signature, use event::{get_pubkey, sign_transaction_data, verify_transaction, Event, PublicKey, Signature,
Transfer}; Transaction};
use genesis::Genesis; use genesis::Genesis;
use historian::{reserve_signature, Historian}; use historian::{reserve_signature, Historian};
use ring::signature::Ed25519KeyPair; use ring::signature::Ed25519KeyPair;
@ -76,8 +76,8 @@ impl Accountant {
allow_deposits && from == to allow_deposits && from == to
} }
pub fn process_transfer(self: &mut Self, tr: Transfer<i64>) -> Result<()> { pub fn process_transaction(self: &mut Self, tr: Transaction<i64>) -> Result<()> {
if !verify_transfer(&tr) { if !verify_transaction(&tr) {
return Err(AccountingError::InvalidTransfer); return Err(AccountingError::InvalidTransfer);
} }
@ -85,7 +85,7 @@ impl Accountant {
return Err(AccountingError::InsufficientFunds); 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)) { if let Err(SendError(_)) = self.historian.sender.send(Event::Transaction(tr)) {
return Err(AccountingError::SendError); return Err(AccountingError::SendError);
} }
@ -93,9 +93,9 @@ impl Accountant {
Ok(()) Ok(())
} }
fn process_verified_transfer( fn process_verified_transaction(
self: &mut Self, self: &mut Self,
tr: &Transfer<i64>, tr: &Transaction<i64>,
allow_deposits: bool, allow_deposits: bool,
) -> Result<()> { ) -> Result<()> {
if !reserve_signature(&mut self.historian.signatures, &tr.sig) { if !reserve_signature(&mut self.historian.signatures, &tr.sig) {
@ -126,7 +126,7 @@ impl Accountant {
) -> Result<()> { ) -> Result<()> {
match *event { match *event {
Event::Tick => Ok(()), 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 from = get_pubkey(keypair);
let last_id = self.last_id; let last_id = self.last_id;
let sig = sign_transaction_data(&n, keypair, &to, &last_id); let sig = sign_transaction_data(&n, keypair, &to, &last_id);
let tr = Transfer { let tr = Transaction {
from, from,
to, to,
data: n, data: n,
last_id, last_id,
sig, sig,
}; };
self.process_transfer(tr).map(|_| sig) self.process_transaction(tr).map(|_| sig)
} }
pub fn get_balance(self: &Self, pubkey: &PublicKey) -> Option<i64> { pub fn get_balance(self: &Self, pubkey: &PublicKey) -> Option<i64> {

View File

@ -1,6 +1,6 @@
use std::io; use std::io;
use accountant::Accountant; use accountant::Accountant;
use event::{Event, PublicKey, Transfer}; use event::{PublicKey, Transaction};
use log::{Entry, Sha256Hash}; use log::{Entry, Sha256Hash};
use std::net::UdpSocket; use std::net::UdpSocket;
use bincode::{deserialize, serialize}; use bincode::{deserialize, serialize};
@ -11,7 +11,7 @@ pub struct AccountantSkel {
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub enum Request { pub enum Request {
Transaction(Transfer<i64>), Transaction(Transaction<i64>),
GetBalance { key: PublicKey }, GetBalance { key: PublicKey },
GetEntries { last_id: Sha256Hash }, GetEntries { last_id: Sha256Hash },
GetId { is_last: bool }, GetId { is_last: bool },
@ -32,8 +32,8 @@ impl AccountantSkel {
pub fn process_request(self: &mut Self, msg: Request) -> Option<Response> { pub fn process_request(self: &mut Self, msg: Request) -> Option<Response> {
match msg { match msg {
Request::Transaction(tr) => { Request::Transaction(tr) => {
if let Err(err) = self.acc.process_transfer(tr) { if let Err(err) = self.acc.process_transaction(tr) {
eprintln!("Transfer error: {:?}", err); eprintln!("Transaction error: {:?}", err);
} }
None None
} }

View File

@ -5,7 +5,7 @@
use std::net::UdpSocket; use std::net::UdpSocket;
use std::io; use std::io;
use bincode::{deserialize, serialize}; 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 log::{Entry, Sha256Hash};
use ring::signature::Ed25519KeyPair; use ring::signature::Ed25519KeyPair;
use accountant_skel::{Request, Response}; use accountant_skel::{Request, Response};
@ -33,7 +33,7 @@ impl AccountantStub {
last_id: Sha256Hash, last_id: Sha256Hash,
sig: Signature, sig: Signature,
) -> io::Result<usize> { ) -> io::Result<usize> {
let req = Request::Transaction(Transfer { let req = Request::Transaction(Transaction {
from, from,
to, to,
data: val, data: val,

View File

@ -3,7 +3,7 @@ extern crate silk;
use silk::accountant_stub::AccountantStub; use silk::accountant_stub::AccountantStub;
use silk::event::{generate_keypair, get_pubkey, sign_transaction_data, verify_event, Event, use silk::event::{generate_keypair, get_pubkey, sign_transaction_data, verify_event, Event,
Transfer}; Transaction};
use silk::genesis::Genesis; use silk::genesis::Genesis;
use std::time::Instant; use std::time::Instant;
use std::net::UdpSocket; use std::net::UdpSocket;
@ -48,7 +48,7 @@ fn main() {
println!("Verify signatures..."); println!("Verify signatures...");
let now = Instant::now(); let now = Instant::now();
for &(k, s) in &sigs { for &(k, s) in &sigs {
let e = Event::Transaction(Transfer { let e = Event::Transaction(Transaction {
from: alice_pubkey, from: alice_pubkey,
to: k, to: k,
data: one, data: one,

View File

@ -1,17 +1,4 @@
//! The `log` crate provides the foundational data structures for Proof-of-History, //! The `event` crate provides the data structures for log events.
//! 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.
use generic_array::GenericArray; use generic_array::GenericArray;
use generic_array::typenum::{U32, U64}; use generic_array::typenum::{U32, U64};
@ -26,7 +13,7 @@ pub type PublicKey = GenericArray<u8, U32>;
pub type Signature = GenericArray<u8, U64>; pub type Signature = GenericArray<u8, U64>;
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)] #[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct Transfer<T> { pub struct Transaction<T> {
pub from: PublicKey, pub from: PublicKey,
pub to: PublicKey, pub to: PublicKey,
pub data: T, pub data: T,
@ -34,9 +21,9 @@ pub struct Transfer<T> {
pub sig: Signature, 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 { pub fn new_claim(to: PublicKey, data: T, last_id: Sha256Hash, sig: Signature) -> Self {
Transfer { Transaction {
from: to, from: to,
to, to,
data, data,
@ -54,12 +41,12 @@ impl<T> Transfer<T> {
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)] #[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub enum Event<T> { pub enum Event<T> {
Tick, Tick,
Transaction(Transfer<T>), Transaction(Transaction<T>),
} }
impl<T> Event<T> { impl<T> Event<T> {
pub fn new_claim(to: PublicKey, data: T, last_id: Sha256Hash, sig: Signature) -> Self { 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 { pub fn verify_event<T: Serialize>(event: &Event<T>) -> bool {
match *event { match *event {
Event::Tick => true, 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(); let sign_data = serialize(&(&tr.from, &tr.to, &tr.data, &tr.last_id)).unwrap();
verify_signature(&tr.from, &sign_data, &tr.sig) verify_signature(&tr.from, &sign_data, &tr.sig)
} }

View File

@ -1,6 +1,6 @@
//! A library for generating the chain's genesis block. //! 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 log::{create_entries, hash, Entry, Sha256Hash};
use ring::rand::SystemRandom; use ring::rand::SystemRandom;
use ring::signature::Ed25519KeyPair; use ring::signature::Ed25519KeyPair;
@ -56,7 +56,7 @@ impl Genesis {
let last_id = self.get_seed(); let last_id = self.get_seed();
let from = self.get_pubkey(); let from = self.get_pubkey();
let sig = sign_transaction_data(&data, &self.get_keypair(), to, &last_id); let sig = sign_transaction_data(&data, &self.get_keypair(), to, &last_id);
Event::Transaction(Transfer { Event::Transaction(Transaction {
from, from,
to: *to, to: *to,
data, data,
@ -93,7 +93,7 @@ mod tests {
fn test_create_events() { fn test_create_events() {
let mut events = Genesis::new(100, vec![]).create_events().into_iter(); let mut events = Genesis::new(100, vec![]).create_events().into_iter();
assert_eq!(events.next().unwrap(), Event::Tick); 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); assert_eq!(from, to);
} else { } else {
assert!(false); assert!(false);

View File

@ -114,7 +114,7 @@ mod tests {
let data = b"hello, world"; let data = b"hello, world";
let zero = Sha256Hash::default(); let zero = Sha256Hash::default();
let sig = sign_claim_data(&data, &keypair, &zero); 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(); let mut sigs = HashSet::new();
assert!(reserve_signature(&mut sigs, &tr0.sig)); assert!(reserve_signature(&mut sigs, &tr0.sig));
assert!(!reserve_signature(&mut sigs, &tr0.sig)); assert!(!reserve_signature(&mut sigs, &tr0.sig));

View File

@ -169,7 +169,7 @@ pub fn next_ticks(start_hash: &Sha256Hash, num_hashes: u64, len: usize) -> Vec<E
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; 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] #[test]
fn test_event_verify() { fn test_event_verify() {
@ -277,7 +277,7 @@ mod tests {
let keypair1 = generate_keypair(); let keypair1 = generate_keypair();
let pubkey1 = get_pubkey(&keypair1); let pubkey1 = get_pubkey(&keypair1);
let data = hash(b"hello, world"); let data = hash(b"hello, world");
let event0 = Event::Transaction(Transfer { let event0 = Event::Transaction(Transaction {
from: get_pubkey(&keypair0), from: get_pubkey(&keypair0),
to: pubkey1, to: pubkey1,
data, data,
@ -295,7 +295,7 @@ mod tests {
let pubkey1 = get_pubkey(&keypair1); let pubkey1 = get_pubkey(&keypair1);
let data = hash(b"hello, world"); let data = hash(b"hello, world");
let zero = Sha256Hash::default(); let zero = Sha256Hash::default();
let event0 = Event::Transaction(Transfer { let event0 = Event::Transaction(Transaction {
from: get_pubkey(&keypair0), from: get_pubkey(&keypair0),
to: pubkey1, to: pubkey1,
data: hash(b"goodbye cruel world"), // <-- attack! data: hash(b"goodbye cruel world"), // <-- attack!
@ -314,7 +314,7 @@ mod tests {
let pubkey1 = get_pubkey(&keypair1); let pubkey1 = get_pubkey(&keypair1);
let data = hash(b"hello, world"); let data = hash(b"hello, world");
let zero = Sha256Hash::default(); let zero = Sha256Hash::default();
let event0 = Event::Transaction(Transfer { let event0 = Event::Transaction(Transaction {
from: get_pubkey(&keypair0), from: get_pubkey(&keypair0),
to: get_pubkey(&thief_keypair), // <-- attack! to: get_pubkey(&thief_keypair), // <-- attack!
data: hash(b"goodbye cruel world"), data: hash(b"goodbye cruel world"),