commit
84df487f7d
|
@ -11,7 +11,7 @@ with by verifying each entry's hash can be generated from the hash in the previo
|
||||||
extern crate solana;
|
extern crate solana;
|
||||||
|
|
||||||
use solana::historian::Historian;
|
use solana::historian::Historian;
|
||||||
use solana::ledger::{verify_slice, Entry, Hash};
|
use solana::ledger::{Block, Entry, Hash};
|
||||||
use solana::event::{generate_keypair, get_pubkey, sign_claim_data, Event};
|
use solana::event::{generate_keypair, get_pubkey, sign_claim_data, Event};
|
||||||
use std::thread::sleep;
|
use std::thread::sleep;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
@ -38,7 +38,7 @@ fn main() {
|
||||||
}
|
}
|
||||||
// Proof-of-History: Verify the historian learned about the events
|
// Proof-of-History: Verify the historian learned about the events
|
||||||
// in the same order they appear in the vector.
|
// in the same order they appear in the vector.
|
||||||
assert!(verify_slice(&entries, &seed));
|
assert!(entries[..].verify(&seed));
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -56,7 +56,7 @@ Proof-of-History
|
||||||
Take note of the last line:
|
Take note of the last line:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
assert!(verify_slice(&entries, &seed));
|
assert!(entries[..].verify(&seed));
|
||||||
```
|
```
|
||||||
|
|
||||||
[It's a proof!](https://en.wikipedia.org/wiki/Curry–Howard_correspondence) For each entry returned by the
|
[It's a proof!](https://en.wikipedia.org/wiki/Curry–Howard_correspondence) For each entry returned by the
|
||||||
|
|
|
@ -14,5 +14,5 @@ msc {
|
||||||
recorder=>historian [ label = "e2 = Entry{id: h6, n: 3, event: Tick}" ] ;
|
recorder=>historian [ label = "e2 = Entry{id: h6, n: 3, event: Tick}" ] ;
|
||||||
client=>historian [ label = "collect()" ] ;
|
client=>historian [ label = "collect()" ] ;
|
||||||
historian=>client [ label = "entries = [e0, e1, e2]" ] ;
|
historian=>client [ label = "entries = [e0, e1, e2]" ] ;
|
||||||
client=>client [ label = "verify_slice(entries, h0)" ] ;
|
client=>client [ label = "entries.verify(h0)" ] ;
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,18 +4,14 @@
|
||||||
//! already been signed and verified.
|
//! already been signed and verified.
|
||||||
|
|
||||||
use chrono::prelude::*;
|
use chrono::prelude::*;
|
||||||
use entry::Entry;
|
|
||||||
use event::Event;
|
use event::Event;
|
||||||
use hash::Hash;
|
use hash::Hash;
|
||||||
use historian::Historian;
|
|
||||||
use mint::Mint;
|
use mint::Mint;
|
||||||
use plan::{Plan, Witness};
|
use plan::{Payment, Plan, Witness};
|
||||||
use recorder::Signal;
|
|
||||||
use signature::{KeyPair, PublicKey, Signature};
|
use signature::{KeyPair, PublicKey, Signature};
|
||||||
use std::collections::hash_map::Entry::Occupied;
|
use std::collections::hash_map::Entry::Occupied;
|
||||||
use std::collections::{HashMap, HashSet};
|
use std::collections::{HashMap, HashSet};
|
||||||
use std::result;
|
use std::result;
|
||||||
use std::sync::mpsc::SendError;
|
|
||||||
use transaction::Transaction;
|
use transaction::Transaction;
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
|
@ -23,124 +19,82 @@ pub enum AccountingError {
|
||||||
InsufficientFunds,
|
InsufficientFunds,
|
||||||
InvalidTransfer,
|
InvalidTransfer,
|
||||||
InvalidTransferSignature,
|
InvalidTransferSignature,
|
||||||
SendError,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type Result<T> = result::Result<T, AccountingError>;
|
pub type Result<T> = result::Result<T, AccountingError>;
|
||||||
|
|
||||||
/// Commit funds to the 'to' party.
|
/// Commit funds to the 'to' party.
|
||||||
fn complete_transaction(balances: &mut HashMap<PublicKey, i64>, plan: &Plan) {
|
fn apply_payment(balances: &mut HashMap<PublicKey, i64>, payment: &Payment) {
|
||||||
if let Plan::Pay(ref payment) = *plan {
|
*balances.entry(payment.to).or_insert(0) += payment.tokens;
|
||||||
*balances.entry(payment.to).or_insert(0) += payment.tokens;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Accountant {
|
pub struct Accountant {
|
||||||
pub historian: Historian,
|
balances: HashMap<PublicKey, i64>,
|
||||||
pub balances: HashMap<PublicKey, i64>,
|
|
||||||
pub first_id: Hash,
|
|
||||||
pending: HashMap<Signature, Plan>,
|
pending: HashMap<Signature, Plan>,
|
||||||
|
signatures: HashSet<Signature>,
|
||||||
time_sources: HashSet<PublicKey>,
|
time_sources: HashSet<PublicKey>,
|
||||||
last_time: DateTime<Utc>,
|
last_time: DateTime<Utc>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Accountant {
|
impl Accountant {
|
||||||
/// Create an Accountant using an existing ledger.
|
/// Create an Accountant using a deposit.
|
||||||
pub fn new_from_entries<I>(entries: I, ms_per_tick: Option<u64>) -> Self
|
pub fn new_from_deposit(deposit: &Payment) -> Self {
|
||||||
where
|
let mut balances = HashMap::new();
|
||||||
I: IntoIterator<Item = Entry>,
|
apply_payment(&mut balances, &deposit);
|
||||||
{
|
Accountant {
|
||||||
let mut entries = entries.into_iter();
|
balances,
|
||||||
|
|
||||||
// The first item in the ledger is required to be an entry with zero num_hashes,
|
|
||||||
// which implies its id can be used as the ledger's seed.
|
|
||||||
let entry0 = entries.next().unwrap();
|
|
||||||
let start_hash = entry0.id;
|
|
||||||
|
|
||||||
let hist = Historian::new(&start_hash, ms_per_tick);
|
|
||||||
let mut acc = Accountant {
|
|
||||||
historian: hist,
|
|
||||||
balances: HashMap::new(),
|
|
||||||
first_id: start_hash,
|
|
||||||
pending: HashMap::new(),
|
pending: HashMap::new(),
|
||||||
|
signatures: HashSet::new(),
|
||||||
time_sources: HashSet::new(),
|
time_sources: HashSet::new(),
|
||||||
last_time: Utc.timestamp(0, 0),
|
last_time: Utc.timestamp(0, 0),
|
||||||
};
|
|
||||||
|
|
||||||
// The second item in the ledger is a special transaction where the to and from
|
|
||||||
// fields are the same. That entry should be treated as a deposit, not a
|
|
||||||
// transfer to oneself.
|
|
||||||
let entry1 = entries.next().unwrap();
|
|
||||||
acc.process_verified_event(&entry1.events[0], true).unwrap();
|
|
||||||
|
|
||||||
for entry in entries {
|
|
||||||
for event in entry.events {
|
|
||||||
acc.process_verified_event(&event, false).unwrap();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
acc
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create an Accountant with only a Mint. Typically used by unit tests.
|
/// Create an Accountant with only a Mint. Typically used by unit tests.
|
||||||
pub fn new(mint: &Mint, ms_per_tick: Option<u64>) -> Self {
|
pub fn new(mint: &Mint) -> Self {
|
||||||
Self::new_from_entries(mint.create_entries(), ms_per_tick)
|
let deposit = Payment {
|
||||||
}
|
to: mint.pubkey(),
|
||||||
|
tokens: mint.tokens,
|
||||||
fn is_deposit(allow_deposits: bool, from: &PublicKey, plan: &Plan) -> bool {
|
};
|
||||||
if let Plan::Pay(ref payment) = *plan {
|
Self::new_from_deposit(&deposit)
|
||||||
allow_deposits && *from == payment.to
|
|
||||||
} else {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Process and log the given Transaction.
|
|
||||||
pub fn log_verified_transaction(&mut self, tr: Transaction) -> Result<()> {
|
|
||||||
if self.get_balance(&tr.from).unwrap_or(0) < tr.tokens {
|
|
||||||
return Err(AccountingError::InsufficientFunds);
|
|
||||||
}
|
|
||||||
|
|
||||||
self.process_verified_transaction(&tr, false)?;
|
|
||||||
if let Err(SendError(_)) = self.historian
|
|
||||||
.sender
|
|
||||||
.send(Signal::Event(Event::Transaction(tr)))
|
|
||||||
{
|
|
||||||
return Err(AccountingError::SendError);
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Verify and process the given Transaction.
|
/// Verify and process the given Transaction.
|
||||||
pub fn log_transaction(&mut self, tr: Transaction) -> Result<()> {
|
pub fn process_transaction(&mut self, tr: Transaction) -> Result<()> {
|
||||||
if !tr.verify() {
|
if !tr.verify() {
|
||||||
return Err(AccountingError::InvalidTransfer);
|
return Err(AccountingError::InvalidTransfer);
|
||||||
}
|
}
|
||||||
|
|
||||||
self.log_verified_transaction(tr)
|
self.process_verified_transaction(&tr)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn reserve_signature(&mut self, sig: &Signature) -> bool {
|
||||||
|
if self.signatures.contains(sig) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
self.signatures.insert(*sig);
|
||||||
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Process a Transaction that has already been verified.
|
/// Process a Transaction that has already been verified.
|
||||||
fn process_verified_transaction(
|
pub fn process_verified_transaction(&mut self, tr: &Transaction) -> Result<()> {
|
||||||
self: &mut Self,
|
if self.get_balance(&tr.from).unwrap_or(0) < tr.tokens {
|
||||||
tr: &Transaction,
|
return Err(AccountingError::InsufficientFunds);
|
||||||
allow_deposits: bool,
|
}
|
||||||
) -> Result<()> {
|
|
||||||
if !self.historian.reserve_signature(&tr.sig) {
|
if !self.reserve_signature(&tr.sig) {
|
||||||
return Err(AccountingError::InvalidTransferSignature);
|
return Err(AccountingError::InvalidTransferSignature);
|
||||||
}
|
}
|
||||||
|
|
||||||
if !Self::is_deposit(allow_deposits, &tr.from, &tr.plan) {
|
if let Some(x) = self.balances.get_mut(&tr.from) {
|
||||||
if let Some(x) = self.balances.get_mut(&tr.from) {
|
*x -= tr.tokens;
|
||||||
*x -= tr.tokens;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut plan = tr.plan.clone();
|
let mut plan = tr.plan.clone();
|
||||||
plan.apply_witness(&Witness::Timestamp(self.last_time));
|
plan.apply_witness(&Witness::Timestamp(self.last_time));
|
||||||
|
|
||||||
if plan.is_complete() {
|
if let Some(ref payment) = plan.final_payment() {
|
||||||
complete_transaction(&mut self.balances, &plan);
|
apply_payment(&mut self.balances, payment);
|
||||||
} else {
|
} else {
|
||||||
self.pending.insert(tr.sig, plan);
|
self.pending.insert(tr.sig, plan);
|
||||||
}
|
}
|
||||||
|
@ -152,8 +106,8 @@ impl Accountant {
|
||||||
fn process_verified_sig(&mut self, from: PublicKey, tx_sig: Signature) -> Result<()> {
|
fn process_verified_sig(&mut self, from: PublicKey, tx_sig: Signature) -> Result<()> {
|
||||||
if let Occupied(mut e) = self.pending.entry(tx_sig) {
|
if let Occupied(mut e) = self.pending.entry(tx_sig) {
|
||||||
e.get_mut().apply_witness(&Witness::Signature(from));
|
e.get_mut().apply_witness(&Witness::Signature(from));
|
||||||
if e.get().is_complete() {
|
if let Some(ref payment) = e.get().final_payment() {
|
||||||
complete_transaction(&mut self.balances, e.get());
|
apply_payment(&mut self.balances, payment);
|
||||||
e.remove_entry();
|
e.remove_entry();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -181,8 +135,8 @@ impl Accountant {
|
||||||
let mut completed = vec![];
|
let mut completed = vec![];
|
||||||
for (key, plan) in &mut self.pending {
|
for (key, plan) in &mut self.pending {
|
||||||
plan.apply_witness(&Witness::Timestamp(self.last_time));
|
plan.apply_witness(&Witness::Timestamp(self.last_time));
|
||||||
if plan.is_complete() {
|
if let Some(ref payment) = plan.final_payment() {
|
||||||
complete_transaction(&mut self.balances, plan);
|
apply_payment(&mut self.balances, payment);
|
||||||
completed.push(key.clone());
|
completed.push(key.clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -195,9 +149,9 @@ impl Accountant {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Process an Transaction or Witness that has already been verified.
|
/// Process an Transaction or Witness that has already been verified.
|
||||||
fn process_verified_event(self: &mut Self, event: &Event, allow_deposits: bool) -> Result<()> {
|
pub fn process_verified_event(&mut self, event: &Event) -> Result<()> {
|
||||||
match *event {
|
match *event {
|
||||||
Event::Transaction(ref tr) => self.process_verified_transaction(tr, allow_deposits),
|
Event::Transaction(ref tr) => self.process_verified_transaction(tr),
|
||||||
Event::Signature { from, tx_sig, .. } => self.process_verified_sig(from, tx_sig),
|
Event::Signature { from, tx_sig, .. } => self.process_verified_sig(from, tx_sig),
|
||||||
Event::Timestamp { from, dt, .. } => self.process_verified_timestamp(from, dt),
|
Event::Timestamp { from, dt, .. } => self.process_verified_timestamp(from, dt),
|
||||||
}
|
}
|
||||||
|
@ -206,7 +160,7 @@ impl Accountant {
|
||||||
/// Create, sign, and process a Transaction from `keypair` to `to` of
|
/// Create, sign, and process a Transaction from `keypair` to `to` of
|
||||||
/// `n` tokens where `last_id` is the last Entry ID observed by the client.
|
/// `n` tokens where `last_id` is the last Entry ID observed by the client.
|
||||||
pub fn transfer(
|
pub fn transfer(
|
||||||
self: &mut Self,
|
&mut self,
|
||||||
n: i64,
|
n: i64,
|
||||||
keypair: &KeyPair,
|
keypair: &KeyPair,
|
||||||
to: PublicKey,
|
to: PublicKey,
|
||||||
|
@ -214,14 +168,14 @@ impl Accountant {
|
||||||
) -> Result<Signature> {
|
) -> Result<Signature> {
|
||||||
let tr = Transaction::new(keypair, to, n, last_id);
|
let tr = Transaction::new(keypair, to, n, last_id);
|
||||||
let sig = tr.sig;
|
let sig = tr.sig;
|
||||||
self.log_transaction(tr).map(|_| sig)
|
self.process_transaction(tr).map(|_| sig)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create, sign, and process a postdated Transaction from `keypair`
|
/// Create, sign, and process a postdated Transaction from `keypair`
|
||||||
/// to `to` of `n` tokens on `dt` where `last_id` is the last Entry ID
|
/// to `to` of `n` tokens on `dt` where `last_id` is the last Entry ID
|
||||||
/// observed by the client.
|
/// observed by the client.
|
||||||
pub fn transfer_on_date(
|
pub fn transfer_on_date(
|
||||||
self: &mut Self,
|
&mut self,
|
||||||
n: i64,
|
n: i64,
|
||||||
keypair: &KeyPair,
|
keypair: &KeyPair,
|
||||||
to: PublicKey,
|
to: PublicKey,
|
||||||
|
@ -230,10 +184,10 @@ impl Accountant {
|
||||||
) -> Result<Signature> {
|
) -> Result<Signature> {
|
||||||
let tr = Transaction::new_on_date(keypair, to, dt, n, last_id);
|
let tr = Transaction::new_on_date(keypair, to, dt, n, last_id);
|
||||||
let sig = tr.sig;
|
let sig = tr.sig;
|
||||||
self.log_transaction(tr).map(|_| sig)
|
self.process_transaction(tr).map(|_| sig)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_balance(self: &Self, pubkey: &PublicKey) -> Option<i64> {
|
pub fn get_balance(&self, pubkey: &PublicKey) -> Option<i64> {
|
||||||
self.balances.get(pubkey).cloned()
|
self.balances.get(pubkey).cloned()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -241,63 +195,50 @@ impl Accountant {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use recorder::ExitReason;
|
|
||||||
use signature::KeyPairUtil;
|
use signature::KeyPairUtil;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_accountant() {
|
fn test_accountant() {
|
||||||
let alice = Mint::new(10_000);
|
let alice = Mint::new(10_000);
|
||||||
let bob_pubkey = KeyPair::new().pubkey();
|
let bob_pubkey = KeyPair::new().pubkey();
|
||||||
let mut acc = Accountant::new(&alice, Some(2));
|
let mut acc = Accountant::new(&alice);
|
||||||
acc.transfer(1_000, &alice.keypair(), bob_pubkey, alice.seed())
|
acc.transfer(1_000, &alice.keypair(), bob_pubkey, alice.last_id())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(acc.get_balance(&bob_pubkey).unwrap(), 1_000);
|
assert_eq!(acc.get_balance(&bob_pubkey).unwrap(), 1_000);
|
||||||
|
|
||||||
acc.transfer(500, &alice.keypair(), bob_pubkey, alice.seed())
|
acc.transfer(500, &alice.keypair(), bob_pubkey, alice.last_id())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(acc.get_balance(&bob_pubkey).unwrap(), 1_500);
|
assert_eq!(acc.get_balance(&bob_pubkey).unwrap(), 1_500);
|
||||||
|
|
||||||
drop(acc.historian.sender);
|
|
||||||
assert_eq!(
|
|
||||||
acc.historian.thread_hdl.join().unwrap(),
|
|
||||||
ExitReason::RecvDisconnected
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_invalid_transfer() {
|
fn test_invalid_transfer() {
|
||||||
let alice = Mint::new(11_000);
|
let alice = Mint::new(11_000);
|
||||||
let mut acc = Accountant::new(&alice, Some(2));
|
let mut acc = Accountant::new(&alice);
|
||||||
let bob_pubkey = KeyPair::new().pubkey();
|
let bob_pubkey = KeyPair::new().pubkey();
|
||||||
acc.transfer(1_000, &alice.keypair(), bob_pubkey, alice.seed())
|
acc.transfer(1_000, &alice.keypair(), bob_pubkey, alice.last_id())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
acc.transfer(10_001, &alice.keypair(), bob_pubkey, alice.seed()),
|
acc.transfer(10_001, &alice.keypair(), bob_pubkey, alice.last_id()),
|
||||||
Err(AccountingError::InsufficientFunds)
|
Err(AccountingError::InsufficientFunds)
|
||||||
);
|
);
|
||||||
|
|
||||||
let alice_pubkey = alice.keypair().pubkey();
|
let alice_pubkey = alice.keypair().pubkey();
|
||||||
assert_eq!(acc.get_balance(&alice_pubkey).unwrap(), 10_000);
|
assert_eq!(acc.get_balance(&alice_pubkey).unwrap(), 10_000);
|
||||||
assert_eq!(acc.get_balance(&bob_pubkey).unwrap(), 1_000);
|
assert_eq!(acc.get_balance(&bob_pubkey).unwrap(), 1_000);
|
||||||
|
|
||||||
drop(acc.historian.sender);
|
|
||||||
assert_eq!(
|
|
||||||
acc.historian.thread_hdl.join().unwrap(),
|
|
||||||
ExitReason::RecvDisconnected
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_overspend_attack() {
|
fn test_overspend_attack() {
|
||||||
let alice = Mint::new(1);
|
let alice = Mint::new(1);
|
||||||
let mut acc = Accountant::new(&alice, None);
|
let mut acc = Accountant::new(&alice);
|
||||||
let bob_pubkey = KeyPair::new().pubkey();
|
let bob_pubkey = KeyPair::new().pubkey();
|
||||||
let mut tr = Transaction::new(&alice.keypair(), bob_pubkey, 1, alice.seed());
|
let mut tr = Transaction::new(&alice.keypair(), bob_pubkey, 1, alice.last_id());
|
||||||
if let Plan::Pay(ref mut payment) = tr.plan {
|
if let Plan::Pay(ref mut payment) = tr.plan {
|
||||||
payment.tokens = 2; // <-- attack!
|
payment.tokens = 2; // <-- attack!
|
||||||
}
|
}
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
acc.log_transaction(tr.clone()),
|
acc.process_transaction(tr.clone()),
|
||||||
Err(AccountingError::InvalidTransfer)
|
Err(AccountingError::InvalidTransfer)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -306,7 +247,7 @@ mod tests {
|
||||||
payment.tokens = 0; // <-- whoops!
|
payment.tokens = 0; // <-- whoops!
|
||||||
}
|
}
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
acc.log_transaction(tr.clone()),
|
acc.process_transaction(tr.clone()),
|
||||||
Err(AccountingError::InvalidTransfer)
|
Err(AccountingError::InvalidTransfer)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -314,28 +255,22 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_transfer_to_newb() {
|
fn test_transfer_to_newb() {
|
||||||
let alice = Mint::new(10_000);
|
let alice = Mint::new(10_000);
|
||||||
let mut acc = Accountant::new(&alice, Some(2));
|
let mut acc = Accountant::new(&alice);
|
||||||
let alice_keypair = alice.keypair();
|
let alice_keypair = alice.keypair();
|
||||||
let bob_pubkey = KeyPair::new().pubkey();
|
let bob_pubkey = KeyPair::new().pubkey();
|
||||||
acc.transfer(500, &alice_keypair, bob_pubkey, alice.seed())
|
acc.transfer(500, &alice_keypair, bob_pubkey, alice.last_id())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(acc.get_balance(&bob_pubkey).unwrap(), 500);
|
assert_eq!(acc.get_balance(&bob_pubkey).unwrap(), 500);
|
||||||
|
|
||||||
drop(acc.historian.sender);
|
|
||||||
assert_eq!(
|
|
||||||
acc.historian.thread_hdl.join().unwrap(),
|
|
||||||
ExitReason::RecvDisconnected
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_transfer_on_date() {
|
fn test_transfer_on_date() {
|
||||||
let alice = Mint::new(1);
|
let alice = Mint::new(1);
|
||||||
let mut acc = Accountant::new(&alice, Some(2));
|
let mut acc = Accountant::new(&alice);
|
||||||
let alice_keypair = alice.keypair();
|
let alice_keypair = alice.keypair();
|
||||||
let bob_pubkey = KeyPair::new().pubkey();
|
let bob_pubkey = KeyPair::new().pubkey();
|
||||||
let dt = Utc::now();
|
let dt = Utc::now();
|
||||||
acc.transfer_on_date(1, &alice_keypair, bob_pubkey, dt, alice.seed())
|
acc.transfer_on_date(1, &alice_keypair, bob_pubkey, dt, alice.last_id())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
// Alice's balance will be zero because all funds are locked up.
|
// Alice's balance will be zero because all funds are locked up.
|
||||||
|
@ -357,14 +292,14 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_transfer_after_date() {
|
fn test_transfer_after_date() {
|
||||||
let alice = Mint::new(1);
|
let alice = Mint::new(1);
|
||||||
let mut acc = Accountant::new(&alice, Some(2));
|
let mut acc = Accountant::new(&alice);
|
||||||
let alice_keypair = alice.keypair();
|
let alice_keypair = alice.keypair();
|
||||||
let bob_pubkey = KeyPair::new().pubkey();
|
let bob_pubkey = KeyPair::new().pubkey();
|
||||||
let dt = Utc::now();
|
let dt = Utc::now();
|
||||||
acc.process_verified_timestamp(alice.pubkey(), dt).unwrap();
|
acc.process_verified_timestamp(alice.pubkey(), dt).unwrap();
|
||||||
|
|
||||||
// It's now past now, so this transfer should be processed immediately.
|
// It's now past now, so this transfer should be processed immediately.
|
||||||
acc.transfer_on_date(1, &alice_keypair, bob_pubkey, dt, alice.seed())
|
acc.transfer_on_date(1, &alice_keypair, bob_pubkey, dt, alice.last_id())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
assert_eq!(acc.get_balance(&alice.pubkey()), Some(0));
|
assert_eq!(acc.get_balance(&alice.pubkey()), Some(0));
|
||||||
|
@ -374,11 +309,11 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_cancel_transfer() {
|
fn test_cancel_transfer() {
|
||||||
let alice = Mint::new(1);
|
let alice = Mint::new(1);
|
||||||
let mut acc = Accountant::new(&alice, Some(2));
|
let mut acc = Accountant::new(&alice);
|
||||||
let alice_keypair = alice.keypair();
|
let alice_keypair = alice.keypair();
|
||||||
let bob_pubkey = KeyPair::new().pubkey();
|
let bob_pubkey = KeyPair::new().pubkey();
|
||||||
let dt = Utc::now();
|
let dt = Utc::now();
|
||||||
let sig = acc.transfer_on_date(1, &alice_keypair, bob_pubkey, dt, alice.seed())
|
let sig = acc.transfer_on_date(1, &alice_keypair, bob_pubkey, dt, alice.last_id())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
// Alice's balance will be zero because all funds are locked up.
|
// Alice's balance will be zero because all funds are locked up.
|
||||||
|
@ -396,4 +331,13 @@ mod tests {
|
||||||
acc.process_verified_sig(alice.pubkey(), sig).unwrap(); // <-- Attack! Attempt to cancel completed transaction.
|
acc.process_verified_sig(alice.pubkey(), sig).unwrap(); // <-- Attack! Attempt to cancel completed transaction.
|
||||||
assert_ne!(acc.get_balance(&alice.pubkey()), Some(2));
|
assert_ne!(acc.get_balance(&alice.pubkey()), Some(2));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_duplicate_event_signature() {
|
||||||
|
let alice = Mint::new(1);
|
||||||
|
let mut acc = Accountant::new(&alice);
|
||||||
|
let sig = Signature::default();
|
||||||
|
assert!(acc.reserve_signature(&sig));
|
||||||
|
assert!(!acc.reserve_signature(&sig));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,8 +3,11 @@
|
||||||
//! in flux. Clients should use AccountantStub to interact with it.
|
//! in flux. Clients should use AccountantStub to interact with it.
|
||||||
|
|
||||||
use accountant::Accountant;
|
use accountant::Accountant;
|
||||||
|
use historian::Historian;
|
||||||
|
use recorder::Signal;
|
||||||
use bincode::{deserialize, serialize};
|
use bincode::{deserialize, serialize};
|
||||||
use entry::Entry;
|
use entry::Entry;
|
||||||
|
use event::Event;
|
||||||
use hash::Hash;
|
use hash::Hash;
|
||||||
use result::Result;
|
use result::Result;
|
||||||
use serde_json;
|
use serde_json;
|
||||||
|
@ -13,7 +16,7 @@ use std::default::Default;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use std::net::{SocketAddr, UdpSocket};
|
use std::net::{SocketAddr, UdpSocket};
|
||||||
use std::sync::atomic::{AtomicBool, Ordering};
|
use std::sync::atomic::{AtomicBool, Ordering};
|
||||||
use std::sync::mpsc::channel;
|
use std::sync::mpsc::{channel, SendError};
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
use std::thread::{spawn, JoinHandle};
|
use std::thread::{spawn, JoinHandle};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
@ -22,9 +25,10 @@ use transaction::Transaction;
|
||||||
use rayon::prelude::*;
|
use rayon::prelude::*;
|
||||||
|
|
||||||
pub struct AccountantSkel<W: Write + Send + 'static> {
|
pub struct AccountantSkel<W: Write + Send + 'static> {
|
||||||
pub acc: Accountant,
|
acc: Accountant,
|
||||||
pub last_id: Hash,
|
last_id: Hash,
|
||||||
writer: W,
|
writer: W,
|
||||||
|
historian: Historian,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg_attr(feature = "cargo-clippy", allow(large_enum_variant))]
|
#[cfg_attr(feature = "cargo-clippy", allow(large_enum_variant))]
|
||||||
|
@ -32,7 +36,7 @@ pub struct AccountantSkel<W: Write + Send + 'static> {
|
||||||
pub enum Request {
|
pub enum Request {
|
||||||
Transaction(Transaction),
|
Transaction(Transaction),
|
||||||
GetBalance { key: PublicKey },
|
GetBalance { key: PublicKey },
|
||||||
GetId { is_last: bool },
|
GetLastId,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Request {
|
impl Request {
|
||||||
|
@ -54,23 +58,23 @@ fn filter_valid_requests(reqs: Vec<(Request, SocketAddr)>) -> Vec<(Request, Sock
|
||||||
pub enum Response {
|
pub enum Response {
|
||||||
Balance { key: PublicKey, val: Option<i64> },
|
Balance { key: PublicKey, val: Option<i64> },
|
||||||
Entries { entries: Vec<Entry> },
|
Entries { entries: Vec<Entry> },
|
||||||
Id { id: Hash, is_last: bool },
|
LastId { id: Hash },
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<W: Write + Send + 'static> AccountantSkel<W> {
|
impl<W: Write + Send + 'static> AccountantSkel<W> {
|
||||||
/// Create a new AccountantSkel that wraps the given Accountant.
|
/// Create a new AccountantSkel that wraps the given Accountant.
|
||||||
pub fn new(acc: Accountant, w: W) -> Self {
|
pub fn new(acc: Accountant, last_id: Hash, writer: W, historian: Historian) -> Self {
|
||||||
let last_id = acc.first_id;
|
|
||||||
AccountantSkel {
|
AccountantSkel {
|
||||||
acc,
|
acc,
|
||||||
last_id,
|
last_id,
|
||||||
writer: w,
|
writer,
|
||||||
|
historian,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Process any Entry items that have been published by the Historian.
|
/// Process any Entry items that have been published by the Historian.
|
||||||
pub fn sync(&mut self) -> Hash {
|
pub fn sync(&mut self) -> Hash {
|
||||||
while let Ok(entry) = self.acc.historian.receiver.try_recv() {
|
while let Ok(entry) = self.historian.receiver.try_recv() {
|
||||||
self.last_id = entry.id;
|
self.last_id = entry.id;
|
||||||
writeln!(self.writer, "{}", serde_json::to_string(&entry).unwrap()).unwrap();
|
writeln!(self.writer, "{}", serde_json::to_string(&entry).unwrap()).unwrap();
|
||||||
}
|
}
|
||||||
|
@ -81,8 +85,13 @@ impl<W: Write + Send + 'static> AccountantSkel<W> {
|
||||||
pub fn log_verified_request(&mut self, msg: Request) -> Option<Response> {
|
pub fn log_verified_request(&mut self, msg: Request) -> Option<Response> {
|
||||||
match msg {
|
match msg {
|
||||||
Request::Transaction(tr) => {
|
Request::Transaction(tr) => {
|
||||||
if let Err(err) = self.acc.log_verified_transaction(tr) {
|
if let Err(err) = self.acc.process_verified_transaction(&tr) {
|
||||||
eprintln!("Transaction error: {:?}", err);
|
eprintln!("Transaction error: {:?}", err);
|
||||||
|
} else if let Err(SendError(_)) = self.historian
|
||||||
|
.sender
|
||||||
|
.send(Signal::Event(Event::Transaction(tr)))
|
||||||
|
{
|
||||||
|
eprintln!("Channel send error");
|
||||||
}
|
}
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
@ -90,14 +99,7 @@ impl<W: Write + Send + 'static> AccountantSkel<W> {
|
||||||
let val = self.acc.get_balance(&key);
|
let val = self.acc.get_balance(&key);
|
||||||
Some(Response::Balance { key, val })
|
Some(Response::Balance { key, val })
|
||||||
}
|
}
|
||||||
Request::GetId { is_last } => Some(Response::Id {
|
Request::GetLastId => Some(Response::LastId { id: self.sync() }),
|
||||||
id: if is_last {
|
|
||||||
self.sync()
|
|
||||||
} else {
|
|
||||||
self.acc.first_id
|
|
||||||
},
|
|
||||||
is_last,
|
|
||||||
}),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -65,26 +65,21 @@ impl AccountantStub {
|
||||||
Ok(None)
|
Ok(None)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Request the first or last Entry ID from the server.
|
|
||||||
fn get_id(&self, is_last: bool) -> io::Result<Hash> {
|
|
||||||
let req = Request::GetId { is_last };
|
|
||||||
let data = serialize(&req).expect("serialize GetId");
|
|
||||||
self.socket.send_to(&data, &self.addr)?;
|
|
||||||
let mut buf = vec![0u8; 1024];
|
|
||||||
self.socket.recv_from(&mut buf)?;
|
|
||||||
let resp = deserialize(&buf).expect("deserialize Id");
|
|
||||||
if let Response::Id { id, .. } = resp {
|
|
||||||
return Ok(id);
|
|
||||||
}
|
|
||||||
Ok(Default::default())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Request the last Entry ID from the server. This method blocks
|
/// Request the last Entry ID from the server. This method blocks
|
||||||
/// until the server sends a response. At the time of this writing,
|
/// until the server sends a response. At the time of this writing,
|
||||||
/// it also has the side-effect of causing the server to log any
|
/// it also has the side-effect of causing the server to log any
|
||||||
/// entries that have been published by the Historian.
|
/// entries that have been published by the Historian.
|
||||||
pub fn get_last_id(&self) -> io::Result<Hash> {
|
pub fn get_last_id(&self) -> io::Result<Hash> {
|
||||||
self.get_id(true)
|
let req = Request::GetLastId;
|
||||||
|
let data = serialize(&req).expect("serialize GetId");
|
||||||
|
self.socket.send_to(&data, &self.addr)?;
|
||||||
|
let mut buf = vec![0u8; 1024];
|
||||||
|
self.socket.recv_from(&mut buf)?;
|
||||||
|
let resp = deserialize(&buf).expect("deserialize Id");
|
||||||
|
if let Response::LastId { id } = resp {
|
||||||
|
return Ok(id);
|
||||||
|
}
|
||||||
|
Ok(Default::default())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -92,6 +87,7 @@ impl AccountantStub {
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use accountant::Accountant;
|
use accountant::Accountant;
|
||||||
|
use historian::Historian;
|
||||||
use accountant_skel::AccountantSkel;
|
use accountant_skel::AccountantSkel;
|
||||||
use mint::Mint;
|
use mint::Mint;
|
||||||
use signature::{KeyPair, KeyPairUtil};
|
use signature::{KeyPair, KeyPairUtil};
|
||||||
|
@ -107,10 +103,16 @@ mod tests {
|
||||||
let addr = "127.0.0.1:9000";
|
let addr = "127.0.0.1:9000";
|
||||||
let send_addr = "127.0.0.1:9001";
|
let send_addr = "127.0.0.1:9001";
|
||||||
let alice = Mint::new(10_000);
|
let alice = Mint::new(10_000);
|
||||||
let acc = Accountant::new(&alice, Some(30));
|
let acc = Accountant::new(&alice);
|
||||||
let bob_pubkey = KeyPair::new().pubkey();
|
let bob_pubkey = KeyPair::new().pubkey();
|
||||||
let exit = Arc::new(AtomicBool::new(false));
|
let exit = Arc::new(AtomicBool::new(false));
|
||||||
let acc = Arc::new(Mutex::new(AccountantSkel::new(acc, sink())));
|
let historian = Historian::new(&alice.last_id(), Some(30));
|
||||||
|
let acc = Arc::new(Mutex::new(AccountantSkel::new(
|
||||||
|
acc,
|
||||||
|
alice.last_id(),
|
||||||
|
sink(),
|
||||||
|
historian,
|
||||||
|
)));
|
||||||
let _threads = AccountantSkel::serve(acc, addr, exit.clone()).unwrap();
|
let _threads = AccountantSkel::serve(acc, addr, exit.clone()).unwrap();
|
||||||
sleep(Duration::from_millis(300));
|
sleep(Duration::from_millis(300));
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@ use solana::entry::Entry;
|
||||||
use solana::event::Event;
|
use solana::event::Event;
|
||||||
use solana::hash::Hash;
|
use solana::hash::Hash;
|
||||||
use solana::historian::Historian;
|
use solana::historian::Historian;
|
||||||
use solana::ledger::verify_slice;
|
use solana::ledger::Block;
|
||||||
use solana::recorder::Signal;
|
use solana::recorder::Signal;
|
||||||
use solana::signature::{KeyPair, KeyPairUtil};
|
use solana::signature::{KeyPair, KeyPairUtil};
|
||||||
use solana::transaction::Transaction;
|
use solana::transaction::Transaction;
|
||||||
|
@ -33,5 +33,5 @@ fn main() {
|
||||||
}
|
}
|
||||||
// Proof-of-History: Verify the historian learned about the events
|
// Proof-of-History: Verify the historian learned about the events
|
||||||
// in the same order they appear in the vector.
|
// in the same order they appear in the vector.
|
||||||
assert!(verify_slice(&entries, &seed));
|
assert!(entries[..].verify(&seed));
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,9 @@ extern crate serde_json;
|
||||||
extern crate solana;
|
extern crate solana;
|
||||||
|
|
||||||
use solana::accountant::Accountant;
|
use solana::accountant::Accountant;
|
||||||
|
use solana::event::Event;
|
||||||
|
use solana::entry::Entry;
|
||||||
|
use solana::historian::Historian;
|
||||||
use solana::accountant_skel::AccountantSkel;
|
use solana::accountant_skel::AccountantSkel;
|
||||||
use std::io::{self, stdout, BufRead};
|
use std::io::{self, stdout, BufRead};
|
||||||
use std::sync::atomic::AtomicBool;
|
use std::sync::atomic::AtomicBool;
|
||||||
|
@ -10,13 +13,43 @@ use std::sync::{Arc, Mutex};
|
||||||
fn main() {
|
fn main() {
|
||||||
let addr = "127.0.0.1:8000";
|
let addr = "127.0.0.1:8000";
|
||||||
let stdin = io::stdin();
|
let stdin = io::stdin();
|
||||||
let entries = stdin
|
let mut entries = stdin
|
||||||
.lock()
|
.lock()
|
||||||
.lines()
|
.lines()
|
||||||
.map(|line| serde_json::from_str(&line.unwrap()).unwrap());
|
.map(|line| serde_json::from_str(&line.unwrap()).unwrap());
|
||||||
let acc = Accountant::new_from_entries(entries, Some(1000));
|
|
||||||
|
// The first item in the ledger is required to be an entry with zero num_hashes,
|
||||||
|
// which implies its id can be used as the ledger's seed.
|
||||||
|
entries.next().unwrap();
|
||||||
|
|
||||||
|
// The second item in the ledger is a special transaction where the to and from
|
||||||
|
// fields are the same. That entry should be treated as a deposit, not a
|
||||||
|
// transfer to oneself.
|
||||||
|
let entry1: Entry = entries.next().unwrap();
|
||||||
|
let deposit = if let Event::Transaction(ref tr) = entry1.events[0] {
|
||||||
|
tr.plan.final_payment()
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut acc = Accountant::new_from_deposit(&deposit.unwrap());
|
||||||
|
|
||||||
|
let mut last_id = entry1.id;
|
||||||
|
for entry in entries {
|
||||||
|
last_id = entry.id;
|
||||||
|
for event in entry.events {
|
||||||
|
acc.process_verified_event(&event).unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let historian = Historian::new(&last_id, Some(1000));
|
||||||
let exit = Arc::new(AtomicBool::new(false));
|
let exit = Arc::new(AtomicBool::new(false));
|
||||||
let skel = Arc::new(Mutex::new(AccountantSkel::new(acc, stdout())));
|
let skel = Arc::new(Mutex::new(AccountantSkel::new(
|
||||||
|
acc,
|
||||||
|
last_id,
|
||||||
|
stdout(),
|
||||||
|
historian,
|
||||||
|
)));
|
||||||
eprintln!("Listening on {}", addr);
|
eprintln!("Listening on {}", addr);
|
||||||
let threads = AccountantSkel::serve(skel, addr, exit.clone()).unwrap();
|
let threads = AccountantSkel::serve(skel, addr, exit.clone()).unwrap();
|
||||||
for t in threads {
|
for t in threads {
|
||||||
|
|
|
@ -4,8 +4,6 @@
|
||||||
use entry::Entry;
|
use entry::Entry;
|
||||||
use hash::Hash;
|
use hash::Hash;
|
||||||
use recorder::{ExitReason, Recorder, Signal};
|
use recorder::{ExitReason, Recorder, Signal};
|
||||||
use signature::Signature;
|
|
||||||
use std::collections::HashSet;
|
|
||||||
use std::sync::mpsc::{sync_channel, Receiver, SyncSender};
|
use std::sync::mpsc::{sync_channel, Receiver, SyncSender};
|
||||||
use std::thread::{spawn, JoinHandle};
|
use std::thread::{spawn, JoinHandle};
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
|
@ -14,7 +12,6 @@ pub struct Historian {
|
||||||
pub sender: SyncSender<Signal>,
|
pub sender: SyncSender<Signal>,
|
||||||
pub receiver: Receiver<Entry>,
|
pub receiver: Receiver<Entry>,
|
||||||
pub thread_hdl: JoinHandle<ExitReason>,
|
pub thread_hdl: JoinHandle<ExitReason>,
|
||||||
pub signatures: HashSet<Signature>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Historian {
|
impl Historian {
|
||||||
|
@ -23,23 +20,13 @@ impl Historian {
|
||||||
let (entry_sender, receiver) = sync_channel(1000);
|
let (entry_sender, receiver) = sync_channel(1000);
|
||||||
let thread_hdl =
|
let thread_hdl =
|
||||||
Historian::create_recorder(*start_hash, ms_per_tick, event_receiver, entry_sender);
|
Historian::create_recorder(*start_hash, ms_per_tick, event_receiver, entry_sender);
|
||||||
let signatures = HashSet::new();
|
|
||||||
Historian {
|
Historian {
|
||||||
sender,
|
sender,
|
||||||
receiver,
|
receiver,
|
||||||
thread_hdl,
|
thread_hdl,
|
||||||
signatures,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reserve_signature(&mut self, sig: &Signature) -> bool {
|
|
||||||
if self.signatures.contains(sig) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
self.signatures.insert(*sig);
|
|
||||||
true
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A background thread that will continue tagging received Event messages and
|
/// A background thread that will continue tagging received Event messages and
|
||||||
/// sending back Entry messages until either the receiver or sender channel is closed.
|
/// sending back Entry messages until either the receiver or sender channel is closed.
|
||||||
fn create_recorder(
|
fn create_recorder(
|
||||||
|
@ -66,7 +53,7 @@ impl Historian {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use ledger::*;
|
use ledger::Block;
|
||||||
use std::thread::sleep;
|
use std::thread::sleep;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
|
@ -95,7 +82,7 @@ mod tests {
|
||||||
ExitReason::RecvDisconnected
|
ExitReason::RecvDisconnected
|
||||||
);
|
);
|
||||||
|
|
||||||
assert!(verify_slice(&[entry0, entry1, entry2], &zero));
|
assert!([entry0, entry1, entry2].verify(&zero));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -110,15 +97,6 @@ mod tests {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_duplicate_event_signature() {
|
|
||||||
let zero = Hash::default();
|
|
||||||
let mut hist = Historian::new(&zero, None);
|
|
||||||
let sig = Signature::default();
|
|
||||||
assert!(hist.reserve_signature(&sig));
|
|
||||||
assert!(!hist.reserve_signature(&sig));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_ticking_historian() {
|
fn test_ticking_historian() {
|
||||||
let zero = Hash::default();
|
let zero = Hash::default();
|
||||||
|
|
|
@ -5,11 +5,17 @@ use entry::{next_tick, Entry};
|
||||||
use hash::Hash;
|
use hash::Hash;
|
||||||
use rayon::prelude::*;
|
use rayon::prelude::*;
|
||||||
|
|
||||||
/// Verifies the hashes and counts of a slice of events are all consistent.
|
pub trait Block {
|
||||||
pub fn verify_slice(entries: &[Entry], start_hash: &Hash) -> bool {
|
/// Verifies the hashes and counts of a slice of events are all consistent.
|
||||||
let genesis = [Entry::new_tick(Default::default(), start_hash)];
|
fn verify(&self, start_hash: &Hash) -> bool;
|
||||||
let entry_pairs = genesis.par_iter().chain(entries).zip(entries);
|
}
|
||||||
entry_pairs.all(|(x0, x1)| x1.verify(&x0.id))
|
|
||||||
|
impl Block for [Entry] {
|
||||||
|
fn verify(&self, start_hash: &Hash) -> bool {
|
||||||
|
let genesis = [Entry::new_tick(0, start_hash)];
|
||||||
|
let entry_pairs = genesis.par_iter().chain(self).zip(self);
|
||||||
|
entry_pairs.all(|(x0, x1)| x1.verify(&x0.id))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a vector of Ticks of length `len` from `start_hash` hash and `num_hashes`.
|
/// Create a vector of Ticks of length `len` from `start_hash` hash and `num_hashes`.
|
||||||
|
@ -33,14 +39,14 @@ mod tests {
|
||||||
fn test_verify_slice() {
|
fn test_verify_slice() {
|
||||||
let zero = Hash::default();
|
let zero = Hash::default();
|
||||||
let one = hash(&zero);
|
let one = hash(&zero);
|
||||||
assert!(verify_slice(&vec![], &zero)); // base case
|
assert!(vec![][..].verify(&zero)); // base case
|
||||||
assert!(verify_slice(&vec![Entry::new_tick(0, &zero)], &zero)); // singleton case 1
|
assert!(vec![Entry::new_tick(0, &zero)][..].verify(&zero)); // singleton case 1
|
||||||
assert!(!verify_slice(&vec![Entry::new_tick(0, &zero)], &one)); // singleton case 2, bad
|
assert!(!vec![Entry::new_tick(0, &zero)][..].verify(&one)); // singleton case 2, bad
|
||||||
assert!(verify_slice(&next_ticks(&zero, 0, 2), &zero)); // inductive step
|
assert!(next_ticks(&zero, 0, 2)[..].verify(&zero)); // inductive step
|
||||||
|
|
||||||
let mut bad_ticks = next_ticks(&zero, 0, 2);
|
let mut bad_ticks = next_ticks(&zero, 0, 2);
|
||||||
bad_ticks[1].id = one;
|
bad_ticks[1].id = one;
|
||||||
assert!(!verify_slice(&bad_ticks, &zero)); // inductive step, bad
|
assert!(!bad_ticks.verify(&zero)); // inductive step, bad
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -52,10 +58,10 @@ mod bench {
|
||||||
|
|
||||||
#[bench]
|
#[bench]
|
||||||
fn event_bench(bencher: &mut Bencher) {
|
fn event_bench(bencher: &mut Bencher) {
|
||||||
let start_hash = Default::default();
|
let start_hash = Hash::default();
|
||||||
let events = next_ticks(&start_hash, 10_000, 8);
|
let entries = next_ticks(&start_hash, 10_000, 8);
|
||||||
bencher.iter(|| {
|
bencher.iter(|| {
|
||||||
assert!(verify_slice(&events, &start_hash));
|
assert!(entries.verify(&start_hash));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,6 +33,10 @@ impl Mint {
|
||||||
hash(&self.pkcs8)
|
hash(&self.pkcs8)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn last_id(&self) -> Hash {
|
||||||
|
self.create_entries()[1].id
|
||||||
|
}
|
||||||
|
|
||||||
pub fn keypair(&self) -> KeyPair {
|
pub fn keypair(&self) -> KeyPair {
|
||||||
KeyPair::from_pkcs8(Input::from(&self.pkcs8)).unwrap()
|
KeyPair::from_pkcs8(Input::from(&self.pkcs8)).unwrap()
|
||||||
}
|
}
|
||||||
|
@ -57,7 +61,7 @@ impl Mint {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use ledger::verify_slice;
|
use ledger::Block;
|
||||||
use plan::Plan;
|
use plan::Plan;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -74,6 +78,6 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_verify_entries() {
|
fn test_verify_entries() {
|
||||||
let entries = Mint::new(100).create_entries();
|
let entries = Mint::new(100).create_entries();
|
||||||
assert!(verify_slice(&entries, &entries[0].id));
|
assert!(entries[..].verify(&entries[0].id));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -72,11 +72,11 @@ impl Plan {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return true if the spending plan requires no additional Witnesses.
|
/// Return Payment if the spending plan requires no additional Witnesses.
|
||||||
pub fn is_complete(&self) -> bool {
|
pub fn final_payment(&self) -> Option<Payment> {
|
||||||
match *self {
|
match *self {
|
||||||
Plan::Pay(_) => true,
|
Plan::Pay(ref payment) => Some(payment.clone()),
|
||||||
_ => false,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -34,11 +34,11 @@ pub struct Recorder {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Recorder {
|
impl Recorder {
|
||||||
pub fn new(receiver: Receiver<Signal>, sender: SyncSender<Entry>, start_hash: Hash) -> Self {
|
pub fn new(receiver: Receiver<Signal>, sender: SyncSender<Entry>, last_hash: Hash) -> Self {
|
||||||
Recorder {
|
Recorder {
|
||||||
receiver,
|
receiver,
|
||||||
sender,
|
sender,
|
||||||
last_hash: start_hash,
|
last_hash,
|
||||||
events: vec![],
|
events: vec![],
|
||||||
num_hashes: 0,
|
num_hashes: 0,
|
||||||
num_ticks: 0,
|
num_ticks: 0,
|
||||||
|
|
Loading…
Reference in New Issue