Skip to content

Commit

Permalink
Merge pull request #201 from CriesofCarrots/master
Browse files Browse the repository at this point in the history
Generalize next tick functions to carry events
  • Loading branch information
garious authored May 11, 2018
2 parents 736d3ea + 7144090 commit 8a9f6b9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/bin/genesis-demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ extern crate untrusted;
use isatty::stdin_isatty;
use rayon::prelude::*;
use solana::accountant::MAX_ENTRY_IDS;
use solana::entry::{create_entry, next_tick};
use solana::entry::{create_entry, next_entry};
use solana::event::Event;
use solana::mint::MintDemo;
use solana::signature::{KeyPair, KeyPairUtil};
Expand Down Expand Up @@ -62,7 +62,7 @@ fn main() {
// Offer client lots of entry IDs to use for each transaction's last_id.
let mut last_id = last_id;
for _ in 0..MAX_ENTRY_IDS {
let entry = next_tick(&last_id, 1);
let entry = next_entry(&last_id, 1, vec![]);
last_id = entry.id;
let serialized = serde_json::to_string(&entry).unwrap_or_else(|e| {
eprintln!("failed to serialize: {}", e);
Expand Down
16 changes: 8 additions & 8 deletions src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,12 @@ pub fn create_entry_mut(start_hash: &mut Hash, cur_hashes: &mut u64, events: Vec
entry
}

/// Creates the next Tick Entry `num_hashes` after `start_hash`.
pub fn next_tick(start_hash: &Hash, num_hashes: u64) -> Entry {
/// Creates the next Tick or Event Entry `num_hashes` after `start_hash`.
pub fn next_entry(start_hash: &Hash, num_hashes: u64, events: Vec<Event>) -> Entry {
Entry {
num_hashes,
id: next_hash(start_hash, num_hashes, &[]),
events: vec![],
id: next_hash(start_hash, num_hashes, &events),
events: events,
}
}

Expand All @@ -128,8 +128,8 @@ mod tests {
let one = hash(&zero);
assert!(Entry::new_tick(0, &zero).verify(&zero)); // base case
assert!(!Entry::new_tick(0, &zero).verify(&one)); // base case, bad
assert!(next_tick(&zero, 1).verify(&zero)); // inductive step
assert!(!next_tick(&zero, 1).verify(&one)); // inductive step, bad
assert!(next_entry(&zero, 1, vec![]).verify(&zero)); // inductive step
assert!(!next_entry(&zero, 1, vec![]).verify(&one)); // inductive step, bad
}

#[test]
Expand Down Expand Up @@ -167,9 +167,9 @@ mod tests {
}

#[test]
fn test_next_tick() {
fn test_next_entry() {
let zero = Hash::default();
let tick = next_tick(&zero, 1);
let tick = next_entry(&zero, 1, vec![]);
assert_eq!(tick.num_hashes, 1);
assert_ne!(tick.id, zero);
}
Expand Down
44 changes: 33 additions & 11 deletions src/ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//! Proof of History ledger.
use bincode::{deserialize, serialize_into};
use entry::{next_tick, Entry};
use entry::{next_entry, Entry};
use event::Event;
use hash::Hash;
use packet;
Expand All @@ -26,16 +26,17 @@ impl Block for [Entry] {
}
}

/// Create a vector of Ticks of length `len` from `start_hash` hash and `num_hashes`.
pub fn next_ticks(start_hash: &Hash, num_hashes: u64, len: usize) -> Vec<Entry> {
/// Create a vector of Entries of length `event_set.len()` from `start_hash` hash, `num_hashes`, and `event_set`.
pub fn next_entries(start_hash: &Hash, num_hashes: u64, event_set: Vec<Vec<Event>>) -> Vec<Entry> {
let mut id = *start_hash;
let mut ticks = vec![];
for _ in 0..len {
let entry = next_tick(&id, num_hashes);
let mut entries = vec![];
for event_list in &event_set {
let events = event_list.clone();
let entry = next_entry(&id, num_hashes, events);
id = entry.id;
ticks.push(entry);
entries.push(entry);
}
ticks
entries
}

pub fn process_entry_list_into_blobs(
Expand Down Expand Up @@ -135,9 +136,9 @@ mod tests {
assert!(vec![][..].verify(&zero)); // base case
assert!(vec![Entry::new_tick(0, &zero)][..].verify(&zero)); // singleton case 1
assert!(!vec![Entry::new_tick(0, &zero)][..].verify(&one)); // singleton case 2, bad
assert!(next_ticks(&zero, 0, 2)[..].verify(&zero)); // inductive step
assert!(next_entries(&zero, 0, vec![vec![]; 2])[..].verify(&zero)); // inductive step

let mut bad_ticks = next_ticks(&zero, 0, 2);
let mut bad_ticks = next_entries(&zero, 0, vec![vec![]; 2]);
bad_ticks[1].id = one;
assert!(!bad_ticks.verify(&zero)); // inductive step, bad
}
Expand All @@ -159,6 +160,27 @@ mod tests {

assert_eq!(entry_list, entries);
}

#[test]
fn test_next_entries() {
let mut id = Hash::default();
let next_id = hash(&id);
let keypair = KeyPair::new();
let tr0 = Event::Transaction(Transaction::new(&keypair, keypair.pubkey(), 1, next_id));
let events = vec![tr0.clone(); 5];
let event_set = vec![events.clone(); 5];
let entries0 = next_entries(&id, 0, event_set);

assert_eq!(entries0.len(), 5);

let mut entries1 = vec![];
for _ in 0..5 {
let entry = next_entry(&id, 0, events.clone());
id = entry.id;
entries1.push(entry);
}
assert_eq!(entries0, entries1);
}
}

#[cfg(all(feature = "unstable", test))]
Expand All @@ -170,7 +192,7 @@ mod bench {
#[bench]
fn event_bench(bencher: &mut Bencher) {
let start_hash = Hash::default();
let entries = next_ticks(&start_hash, 10_000, 8);
let entries = next_entries(&start_hash, 10_000, vec![vec![]; 8]);
bencher.iter(|| {
assert!(entries.verify(&start_hash));
});
Expand Down

0 comments on commit 8a9f6b9

Please sign in to comment.