Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.

Reorg #47

Merged
merged 10 commits into from
Mar 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 40 additions & 29 deletions src/accountant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
//! transfer funds to other users.

use log::{Entry, Sha256Hash};
use event::{get_pubkey, sign_transaction_data, verify_event, Event, PublicKey, Signature};
use event::Event;
use transaction::{sign_transaction_data, Transaction};
use signature::{get_pubkey, PublicKey, Signature};
use genesis::Genesis;
use historian::{reserve_signature, Historian};
use ring::signature::Ed25519KeyPair;
Expand All @@ -14,7 +16,8 @@ use std::result;
#[derive(Debug, PartialEq, Eq)]
pub enum AccountingError {
InsufficientFunds,
InvalidEvent,
InvalidTransfer,
InvalidTransferSignature,
SendError,
}

Expand Down Expand Up @@ -74,52 +77,60 @@ impl Accountant {
allow_deposits && from == to
}

pub fn process_event(self: &mut Self, event: Event<i64>) -> Result<()> {
if !verify_event(&event) {
return Err(AccountingError::InvalidEvent);
pub fn process_transaction(self: &mut Self, tr: Transaction<i64>) -> Result<()> {
if !tr.verify() {
return Err(AccountingError::InvalidTransfer);
}

if let Event::Transaction { from, data, .. } = event {
if self.get_balance(&from).unwrap_or(0) < data {
return Err(AccountingError::InsufficientFunds);
}
if self.get_balance(&tr.from).unwrap_or(0) < tr.data {
return Err(AccountingError::InsufficientFunds);
}

self.process_verified_event(&event, false)?;
if let Err(SendError(_)) = self.historian.sender.send(event) {
self.process_verified_transaction(&tr, false)?;
if let Err(SendError(_)) = self.historian.sender.send(Event::Transaction(tr)) {
return Err(AccountingError::SendError);
}

Ok(())
}

fn process_verified_event(
fn process_verified_transaction(
self: &mut Self,
event: &Event<i64>,
tr: &Transaction<i64>,
allow_deposits: bool,
) -> Result<()> {
if !reserve_signature(&mut self.historian.signatures, event) {
return Err(AccountingError::InvalidEvent);
if !reserve_signature(&mut self.historian.signatures, &tr.sig) {
return Err(AccountingError::InvalidTransferSignature);
}

if let Event::Transaction { from, to, data, .. } = *event {
if !Self::is_deposit(allow_deposits, &from, &to) {
if let Some(x) = self.balances.get_mut(&from) {
*x -= data;
}
if !Self::is_deposit(allow_deposits, &tr.from, &tr.to) {
if let Some(x) = self.balances.get_mut(&tr.from) {
*x -= tr.data;
}
}

if self.balances.contains_key(&to) {
if let Some(x) = self.balances.get_mut(&to) {
*x += data;
}
} else {
self.balances.insert(to, data);
if self.balances.contains_key(&tr.to) {
if let Some(x) = self.balances.get_mut(&tr.to) {
*x += tr.data;
}
} else {
self.balances.insert(tr.to, tr.data);
}

Ok(())
}

fn process_verified_event(
self: &mut Self,
event: &Event<i64>,
allow_deposits: bool,
) -> Result<()> {
match *event {
Event::Tick => Ok(()),
Event::Transaction(ref tr) => self.process_verified_transaction(tr, allow_deposits),
}
}

pub fn transfer(
self: &mut Self,
n: i64,
Expand All @@ -129,14 +140,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 event = Event::Transaction {
let tr = Transaction {
from,
to,
data: n,
last_id,
sig,
};
self.process_event(event).map(|_| sig)
self.process_transaction(tr).map(|_| sig)
}

pub fn get_balance(self: &Self, pubkey: &PublicKey) -> Option<i64> {
Expand All @@ -147,7 +158,7 @@ impl Accountant {
#[cfg(test)]
mod tests {
use super::*;
use event::{generate_keypair, get_pubkey};
use signature::{generate_keypair, get_pubkey};
use logger::ExitReason;
use genesis::Creator;

Expand Down
42 changes: 9 additions & 33 deletions src/accountant_skel.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::io;
use accountant::Accountant;
use event::{Event, PublicKey, Signature};
use transaction::Transaction;
use signature::PublicKey;
use log::{Entry, Sha256Hash};
use std::net::UdpSocket;
use bincode::{deserialize, serialize};
Expand All @@ -11,22 +12,10 @@ pub struct AccountantSkel {

#[derive(Serialize, Deserialize, Debug)]
pub enum Request {
Transfer {
from: PublicKey,
to: PublicKey,
val: i64,
last_id: Sha256Hash,
sig: Signature,
},
GetBalance {
key: PublicKey,
},
GetEntries {
last_id: Sha256Hash,
},
GetId {
is_last: bool,
},
Transaction(Transaction<i64>),
GetBalance { key: PublicKey },
GetEntries { last_id: Sha256Hash },
GetId { is_last: bool },
}

#[derive(Serialize, Deserialize, Debug)]
Expand All @@ -43,22 +32,9 @@ impl AccountantSkel {

pub fn process_request(self: &mut Self, msg: Request) -> Option<Response> {
match msg {
Request::Transfer {
from,
to,
val,
last_id,
sig,
} => {
let event = Event::Transaction {
from,
to,
data: val,
last_id,
sig,
};
if let Err(err) = self.acc.process_event(event) {
eprintln!("Transfer error: {:?}", err);
Request::Transaction(tr) => {
if let Err(err) = self.acc.process_transaction(tr) {
eprintln!("Transaction error: {:?}", err);
}
None
}
Expand Down
11 changes: 6 additions & 5 deletions src/accountant_stub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
use std::net::UdpSocket;
use std::io;
use bincode::{deserialize, serialize};
use event::{get_pubkey, get_signature, sign_transaction_data, PublicKey, Signature};
use transaction::{sign_transaction_data, Transaction};
use signature::{get_pubkey, PublicKey, Signature};
use log::{Entry, Sha256Hash};
use ring::signature::Ed25519KeyPair;
use accountant_skel::{Request, Response};
Expand Down Expand Up @@ -33,13 +34,13 @@ impl AccountantStub {
last_id: Sha256Hash,
sig: Signature,
) -> io::Result<usize> {
let req = Request::Transfer {
let req = Request::Transaction(Transaction {
from,
to,
val,
data: val,
last_id,
sig,
};
});
let data = serialize(&req).unwrap();
self.socket.send_to(&data, &self.addr)
}
Expand Down Expand Up @@ -108,7 +109,7 @@ impl AccountantStub {
if let Response::Entries { entries } = resp {
for Entry { id, event, .. } in entries {
self.last_id = Some(id);
if let Some(sig) = get_signature(&event) {
if let Some(sig) = event.get_signature() {
if sig == *wait_sig {
return Ok(());
}
Expand Down
10 changes: 6 additions & 4 deletions src/bin/client-demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ extern crate serde_json;
extern crate silk;

use silk::accountant_stub::AccountantStub;
use silk::event::{generate_keypair, get_pubkey, sign_transaction_data, verify_event, Event};
use silk::event::Event;
use silk::signature::{generate_keypair, get_pubkey};
use silk::transaction::{sign_transaction_data, Transaction};
use silk::genesis::Genesis;
use std::time::Instant;
use std::net::UdpSocket;
Expand Down Expand Up @@ -47,14 +49,14 @@ fn main() {
println!("Verify signatures...");
let now = Instant::now();
for &(k, s) in &sigs {
let e = Event::Transaction {
let e = Event::Transaction(Transaction {
from: alice_pubkey,
to: k,
data: one,
last_id,
sig: s,
};
assert!(verify_event(&e));
});
assert!(e.verify());
}
let duration = now.elapsed();
let ns = duration.as_secs() * 1_000_000_000 + duration.subsec_nanos() as u64;
Expand Down
4 changes: 3 additions & 1 deletion src/bin/demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ extern crate silk;

use silk::historian::Historian;
use silk::log::{verify_slice, Entry, Sha256Hash};
use silk::event::{generate_keypair, get_pubkey, sign_claim_data, Event};
use silk::signature::{generate_keypair, get_pubkey};
use silk::transaction::sign_claim_data;
use silk::event::Event;
use std::thread::sleep;
use std::time::Duration;
use std::sync::mpsc::SendError;
Expand Down
2 changes: 1 addition & 1 deletion src/bin/genesis-file-demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ extern crate serde_json;
extern crate silk;

use silk::genesis::{Creator, Genesis};
use silk::event::{generate_keypair, get_pubkey};
use silk::signature::{generate_keypair, get_pubkey};

fn main() {
let alice = Creator {
Expand Down
Loading