Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup naming #67

Merged
merged 5 commits into from
Mar 19, 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
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ Install the silk executables:
$ cargo install silk
```

The testnode server is initialized with a transaction log from stdin and
generates new log entries on stdout. To create the input log, we'll need
to create *the mint* and use it to generate a *genesis log*. It's done in
The testnode server is initialized with a ledger from stdin and
generates new ledger entries on stdout. To create the input ledger, we'll need
to create *the mint* and use it to generate a *genesis ledger*. It's done in
two steps because the mint.json file contains a private key that will be
used later in this demo.

Expand All @@ -55,23 +55,23 @@ Now you can start the server:
```

Then, in a separate shell, let's execute some transactions. Note we pass in
the JSON configuration file here, not the genesis log.
the JSON configuration file here, not the genesis ledger.

```bash
$ cat mint.json | silk-client-demo
```

Now kill the server with Ctrl-C, and take a look at the transaction log. You should
Now kill the server with Ctrl-C, and take a look at the ledger. You should
see something similar to:

```json
{"num_hashes":27,"id":[0, "..."],"event":"Tick"}
{"num_hashes":3,"id":[67, "..."],"event":{"Transaction":{"asset":42}}}
{"num_hashes":3,"id":[67, "..."],"event":{"Transaction":{"tokens":42}}}
{"num_hashes":27,"id":[0, "..."],"event":"Tick"}
```

Now restart the server from where we left off. Pass it both the genesis log, and
the transaction log.
Now restart the server from where we left off. Pass it both the genesis ledger, and
the transaction ledger.

```bash
$ cat genesis.log transactions0.log | silk-testnode > transactions1.log
Expand Down
16 changes: 8 additions & 8 deletions doc/historian.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
The Historian
===

Create a *Historian* and send it *events* to generate an *event log*, where each log *entry*
Create a *Historian* and send it *events* to generate an *event log*, where each *entry*
is tagged with the historian's latest *hash*. Then ensure the order of events was not tampered
with by verifying each entry's hash can be generated from the hash in the previous entry:

Expand All @@ -11,17 +11,17 @@ with by verifying each entry's hash can be generated from the hash in the previo
extern crate silk;

use silk::historian::Historian;
use silk::log::{verify_slice, Entry, Hash};
use silk::ledger::{verify_slice, Entry, Hash};
use silk::event::{generate_keypair, get_pubkey, sign_claim_data, Event};
use std::thread::sleep;
use std::time::Duration;
use std::sync::mpsc::SendError;

fn create_log(hist: &Historian<Hash>) -> Result<(), SendError<Event<Hash>>> {
fn create_ledger(hist: &Historian<Hash>) -> Result<(), SendError<Event<Hash>>> {
sleep(Duration::from_millis(15));
let asset = Hash::default();
let tokens = 42;
let keypair = generate_keypair();
let event0 = Event::new_claim(get_pubkey(&keypair), asset, sign_claim_data(&asset, &keypair));
let event0 = Event::new_claim(get_pubkey(&keypair), tokens, sign_claim_data(&tokens, &keypair));
hist.sender.send(event0)?;
sleep(Duration::from_millis(10));
Ok(())
Expand All @@ -30,7 +30,7 @@ fn create_log(hist: &Historian<Hash>) -> Result<(), SendError<Event<Hash>>> {
fn main() {
let seed = Hash::default();
let hist = Historian::new(&seed, Some(10));
create_log(&hist).expect("send error");
create_ledger(&hist).expect("send error");
drop(hist.sender);
let entries: Vec<Entry<Hash>> = hist.receiver.iter().collect();
for entry in &entries {
Expand All @@ -42,11 +42,11 @@ fn main() {
}
```

Running the program should produce a log similar to:
Running the program should produce a ledger similar to:

```rust
Entry { num_hashes: 0, id: [0, ...], event: Tick }
Entry { num_hashes: 3, id: [67, ...], event: Transaction { asset: [37, ...] } }
Entry { num_hashes: 3, id: [67, ...], event: Transaction { tokens: 42 } }
Entry { num_hashes: 3, id: [123, ...], event: Tick }
```

Expand Down
22 changes: 11 additions & 11 deletions doc/historian.msc
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
msc {
client,historian,logger;
client,historian,recorder;

logger=>historian [ label = "e0 = Entry{id: h0, n: 0, event: Tick}" ] ;
logger=>logger [ label = "h1 = hash(h0)" ] ;
logger=>logger [ label = "h2 = hash(h1)" ] ;
recorder=>historian [ label = "e0 = Entry{id: h0, n: 0, event: Tick}" ] ;
recorder=>recorder [ label = "h1 = hash(h0)" ] ;
recorder=>recorder [ label = "h2 = hash(h1)" ] ;
client=>historian [ label = "Transaction(d0)" ] ;
historian=>logger [ label = "Transaction(d0)" ] ;
logger=>logger [ label = "h3 = hash(h2 + d0)" ] ;
logger=>historian [ label = "e1 = Entry{id: hash(h3), n: 3, event: Transaction(d0)}" ] ;
logger=>logger [ label = "h4 = hash(h3)" ] ;
logger=>logger [ label = "h5 = hash(h4)" ] ;
logger=>logger [ label = "h6 = hash(h5)" ] ;
logger=>historian [ label = "e2 = Entry{id: h6, n: 3, event: Tick}" ] ;
historian=>recorder [ label = "Transaction(d0)" ] ;
recorder=>recorder [ label = "h3 = hash(h2 + d0)" ] ;
recorder=>historian [ label = "e1 = Entry{id: hash(h3), n: 3, event: Transaction(d0)}" ] ;
recorder=>recorder [ label = "h4 = hash(h3)" ] ;
recorder=>recorder [ label = "h5 = hash(h4)" ] ;
recorder=>recorder [ label = "h6 = hash(h5)" ] ;
recorder=>historian [ label = "e2 = Entry{id: h6, n: 3, event: Tick}" ] ;
client=>historian [ label = "collect()" ] ;
historian=>client [ label = "entries = [e0, e1, e2]" ] ;
client=>client [ label = "verify_slice(entries, h0)" ] ;
Expand Down
24 changes: 12 additions & 12 deletions src/accountant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use transaction::Transaction;
use signature::{KeyPair, PublicKey, Signature};
use mint::Mint;
use historian::{reserve_signature, Historian};
use logger::Signal;
use recorder::Signal;
use std::sync::mpsc::SendError;
use std::collections::{HashMap, HashSet};
use std::result;
Expand Down Expand Up @@ -43,8 +43,8 @@ impl Accountant {
{
let mut entries = entries.into_iter();

// The first item in the log is required to be an entry with zero num_hashes,
// which implies its id can be used as the log's seed.
// 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;

Expand All @@ -59,7 +59,7 @@ impl Accountant {
last_time: Utc.timestamp(0, 0),
};

// The second item in the log is a special transaction where the to and from
// 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();
Expand Down Expand Up @@ -97,7 +97,7 @@ impl Accountant {
return Err(AccountingError::InvalidTransfer);
}

if self.get_balance(&tr.from).unwrap_or(0) < tr.asset {
if self.get_balance(&tr.from).unwrap_or(0) < tr.tokens {
return Err(AccountingError::InsufficientFunds);
}

Expand All @@ -117,10 +117,10 @@ impl Accountant {
if let Plan::Action(Action::Pay(ref payment)) = *plan {
if self.balances.contains_key(&payment.to) {
if let Some(x) = self.balances.get_mut(&payment.to) {
*x += payment.asset;
*x += payment.tokens;
}
} else {
self.balances.insert(payment.to, payment.asset);
self.balances.insert(payment.to, payment.tokens);
}
}
}
Expand All @@ -136,7 +136,7 @@ impl Accountant {

if !Self::is_deposit(allow_deposits, &tr.from, &tr.plan) {
if let Some(x) = self.balances.get_mut(&tr.from) {
*x -= tr.asset;
*x -= tr.tokens;
}
}

Expand Down Expand Up @@ -240,7 +240,7 @@ impl Accountant {
mod tests {
use super::*;
use signature::KeyPairUtil;
use logger::ExitReason;
use recorder::ExitReason;

#[test]
fn test_accountant() {
Expand Down Expand Up @@ -289,16 +289,16 @@ mod tests {
let bob_pubkey = KeyPair::new().pubkey();
let mut tr = Transaction::new(&alice.keypair(), bob_pubkey, 1, alice.seed());
if let Plan::Action(Action::Pay(ref mut payment)) = tr.plan {
payment.asset = 2; // <-- attack!
payment.tokens = 2; // <-- attack!
}
assert_eq!(
acc.process_transaction(tr.clone()),
Err(AccountingError::InvalidTransfer)
);

// Also, ensure all branchs of the plan spend all assets
// Also, ensure all branchs of the plan spend all tokens
if let Plan::Action(Action::Pay(ref mut payment)) = tr.plan {
payment.asset = 0; // <-- whoops!
payment.tokens = 0; // <-- whoops!
}
assert_eq!(
acc.process_transaction(tr.clone()),
Expand Down
8 changes: 4 additions & 4 deletions src/bin/historian-demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ extern crate silk;
use silk::historian::Historian;
use silk::hash::Hash;
use silk::entry::Entry;
use silk::log::verify_slice;
use silk::logger::Signal;
use silk::ledger::verify_slice;
use silk::recorder::Signal;
use silk::signature::{KeyPair, KeyPairUtil};
use silk::transaction::Transaction;
use silk::event::Event;
use std::thread::sleep;
use std::time::Duration;
use std::sync::mpsc::SendError;

fn create_log(hist: &Historian, seed: &Hash) -> Result<(), SendError<Signal>> {
fn create_ledger(hist: &Historian, seed: &Hash) -> Result<(), SendError<Signal>> {
sleep(Duration::from_millis(15));
let keypair = KeyPair::new();
let tr = Transaction::new(&keypair, keypair.pubkey(), 42, *seed);
Expand All @@ -25,7 +25,7 @@ fn create_log(hist: &Historian, seed: &Hash) -> Result<(), SendError<Signal>> {
fn main() {
let seed = Hash::default();
let hist = Historian::new(&seed, Some(10));
create_log(&hist, &seed).expect("send error");
create_ledger(&hist, &seed).expect("send error");
drop(hist.sender);
let entries: Vec<Entry> = hist.receiver.iter().collect();
for entry in &entries {
Expand Down
18 changes: 9 additions & 9 deletions src/historian.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::sync::mpsc::{sync_channel, Receiver, SyncSender};
use std::time::Instant;
use hash::{hash, Hash};
use entry::Entry;
use logger::{ExitReason, Logger, Signal};
use recorder::{ExitReason, Recorder, Signal};
use signature::Signature;

pub struct Historian {
Expand All @@ -22,7 +22,7 @@ impl Historian {
let (sender, event_receiver) = sync_channel(1000);
let (entry_sender, receiver) = sync_channel(1000);
let thread_hdl =
Historian::create_logger(*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 {
sender,
Expand All @@ -34,22 +34,22 @@ impl Historian {

/// A background thread that will continue tagging received Event messages and
/// sending back Entry messages until either the receiver or sender channel is closed.
fn create_logger(
fn create_recorder(
start_hash: Hash,
ms_per_tick: Option<u64>,
receiver: Receiver<Signal>,
sender: SyncSender<Entry>,
) -> JoinHandle<ExitReason> {
spawn(move || {
let mut logger = Logger::new(receiver, sender, start_hash);
let mut recorder = Recorder::new(receiver, sender, start_hash);
let now = Instant::now();
loop {
if let Err(err) = logger.process_events(now, ms_per_tick) {
if let Err(err) = recorder.process_events(now, ms_per_tick) {
return err;
}
if ms_per_tick.is_some() {
logger.last_id = hash(&logger.last_id);
logger.num_hashes += 1;
recorder.last_id = hash(&recorder.last_id);
recorder.num_hashes += 1;
}
}
})
Expand All @@ -67,7 +67,7 @@ pub fn reserve_signature(sigs: &mut HashSet<Signature>, sig: &Signature) -> bool
#[cfg(test)]
mod tests {
use super::*;
use log::*;
use ledger::*;
use std::thread::sleep;
use std::time::Duration;

Expand Down Expand Up @@ -132,7 +132,7 @@ mod tests {
assert_eq!(entries.len(), 1);

// Ensure the ID is not the seed, which indicates another Tick
// was logged before the one we sent.
// was recorded before the one we sent.
assert_ne!(entries[0].id, zero);
}
}
8 changes: 4 additions & 4 deletions src/log.rs → src/ledger.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! The `log` crate provides the foundational data structures for Proof-of-History,
//! The `ledger` 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
/// Each 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.
Expand All @@ -11,7 +11,7 @@
/// 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.
/// was generated by the fastest processor at the time the entry was recorded.

use hash::Hash;
use entry::{next_tick, Entry};
Expand Down Expand Up @@ -60,7 +60,7 @@ mod tests {
mod bench {
extern crate test;
use self::test::Bencher;
use log::*;
use ledger::*;

#[bench]
fn event_bench(bencher: &mut Bencher) {
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ pub mod plan;
pub mod transaction;
pub mod event;
pub mod entry;
pub mod log;
pub mod ledger;
pub mod mint;
pub mod logger;
pub mod recorder;
pub mod historian;
pub mod streamer;
pub mod accountant;
Expand All @@ -18,7 +18,7 @@ extern crate bincode;
extern crate chrono;
extern crate generic_array;
#[macro_use]
extern crate log as logging;
extern crate log;
extern crate rayon;
extern crate ring;
extern crate serde;
Expand Down
2 changes: 1 addition & 1 deletion src/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl Mint {
#[cfg(test)]
mod tests {
use super::*;
use log::verify_slice;
use ledger::verify_slice;
use plan::{Action, Plan};

#[test]
Expand Down
Loading