From 6c1f1c2a7ab29de26055b9f1cef934af69fde627 Mon Sep 17 00:00:00 2001 From: Greg Fitzgerald Date: Wed, 16 May 2018 17:49:58 -0600 Subject: [PATCH] Promote create_entry() to Entry::new() --- src/bin/genesis-demo.rs | 4 ++-- src/entry.rs | 44 ++++++++++++++++++++--------------------- src/ledger.rs | 3 +-- src/mint.rs | 5 ++--- src/recorder.rs | 4 ++-- src/tvu.rs | 6 +++--- 6 files changed, 32 insertions(+), 34 deletions(-) diff --git a/src/bin/genesis-demo.rs b/src/bin/genesis-demo.rs index 0e408c11ec..3287324793 100644 --- a/src/bin/genesis-demo.rs +++ b/src/bin/genesis-demo.rs @@ -8,7 +8,7 @@ extern crate untrusted; use isatty::stdin_isatty; use rayon::prelude::*; use solana::bank::MAX_ENTRY_IDS; -use solana::entry::{create_entry, next_entry}; +use solana::entry::{next_entry, Entry}; use solana::event::Event; use solana::mint::MintDemo; use solana::signature::{GenKeys, KeyPair, KeyPairUtil}; @@ -61,7 +61,7 @@ fn main() { } eprintln!("Logging the creation of {} accounts...", num_accounts); - let entry = create_entry(&last_id, 0, events); + let entry = Entry::new(&last_id, 0, events); println!("{}", serde_json::to_string(&entry).unwrap()); eprintln!("Creating {} empty entries...", MAX_ENTRY_IDS); diff --git a/src/entry.rs b/src/entry.rs index badc4e5feb..295fbc4428 100644 --- a/src/entry.rs +++ b/src/entry.rs @@ -25,6 +25,25 @@ pub struct Entry { } impl Entry { + /// Creates the next Entry `num_hashes` after `start_hash`. + pub fn new(start_hash: &Hash, cur_hashes: u64, events: Vec) -> Self { + let num_hashes = cur_hashes + if events.is_empty() { 0 } else { 1 }; + let id = next_hash(start_hash, 0, &events); + Entry { + num_hashes, + id, + events, + } + } + + /// Creates the next Tick Entry `num_hashes` after `start_hash`. + pub fn new_mut(start_hash: &mut Hash, cur_hashes: &mut u64, events: Vec) -> Self { + let entry = Self::new(start_hash, *cur_hashes, events); + *start_hash = entry.id; + *cur_hashes = 0; + entry + } + /// Creates a Entry from the number of hashes `num_hashes` since the previous event /// and that resulting `id`. pub fn new_tick(num_hashes: u64, id: &Hash) -> Self { @@ -84,25 +103,6 @@ pub fn next_hash(start_hash: &Hash, num_hashes: u64, events: &[Event]) -> Hash { } } -/// Creates the next Entry `num_hashes` after `start_hash`. -pub fn create_entry(start_hash: &Hash, cur_hashes: u64, events: Vec) -> Entry { - let num_hashes = cur_hashes + if events.is_empty() { 0 } else { 1 }; - let id = next_hash(start_hash, 0, &events); - Entry { - num_hashes, - id, - events, - } -} - -/// Creates the next Tick Entry `num_hashes` after `start_hash`. -pub fn create_entry_mut(start_hash: &mut Hash, cur_hashes: &mut u64, events: Vec) -> Entry { - let entry = create_entry(start_hash, *cur_hashes, events); - *start_hash = entry.id; - *cur_hashes = 0; - 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) -> Entry { Entry { @@ -116,7 +116,7 @@ pub fn next_entry(start_hash: &Hash, num_hashes: u64, events: Vec) -> Ent mod tests { use super::*; use chrono::prelude::*; - use entry::create_entry; + use entry::Entry; use event::Event; use hash::hash; use signature::{KeyPair, KeyPairUtil}; @@ -139,7 +139,7 @@ mod tests { let keypair = KeyPair::new(); let tr0 = Event::new_transaction(&keypair, keypair.pubkey(), 0, zero); let tr1 = Event::new_transaction(&keypair, keypair.pubkey(), 1, zero); - let mut e0 = create_entry(&zero, 0, vec![tr0.clone(), tr1.clone()]); + let mut e0 = Entry::new(&zero, 0, vec![tr0.clone(), tr1.clone()]); assert!(e0.verify(&zero)); // Next, swap two events and ensure verification fails. @@ -156,7 +156,7 @@ mod tests { let keypair = KeyPair::new(); let tr0 = Event::new_timestamp(&keypair, Utc::now()); let tr1 = Event::new_signature(&keypair, Default::default()); - let mut e0 = create_entry(&zero, 0, vec![tr0.clone(), tr1.clone()]); + let mut e0 = Entry::new(&zero, 0, vec![tr0.clone(), tr1.clone()]); assert!(e0.verify(&zero)); // Next, swap two witness events and ensure verification fails. diff --git a/src/ledger.rs b/src/ledger.rs index 1b1895d4c1..f63846e008 100644 --- a/src/ledger.rs +++ b/src/ledger.rs @@ -123,7 +123,6 @@ pub fn reconstruct_entries_from_blobs(blobs: &VecDeque) -> Vec Vec { - let e0 = create_entry(&self.seed(), 0, vec![]); - let e1 = create_entry(&e0.id, 0, self.create_events()); + let e0 = Entry::new(&self.seed(), 0, vec![]); + let e1 = Entry::new(&e0.id, 0, self.create_events()); vec![e0, e1] } } diff --git a/src/recorder.rs b/src/recorder.rs index 1fc668acee..09e0d471e7 100644 --- a/src/recorder.rs +++ b/src/recorder.rs @@ -5,7 +5,7 @@ //! Event, the latest hash, and the number of hashes since the last event. //! The resulting stream of entries represents ordered events in time. -use entry::{create_entry_mut, Entry}; +use entry::Entry; use event::Event; use hash::{hash, Hash}; use std::sync::mpsc::{Receiver, Sender, TryRecvError}; @@ -48,7 +48,7 @@ impl Recorder { } pub fn record_entry(&mut self, events: Vec) -> Result<(), ExitReason> { - let entry = create_entry_mut(&mut self.last_hash, &mut self.num_hashes, events); + let entry = Entry::new_mut(&mut self.last_hash, &mut self.num_hashes, events); self.sender .send(entry) .or(Err(ExitReason::SendDisconnected))?; diff --git a/src/tvu.rs b/src/tvu.rs index dd018c9180..f0ec2ccac6 100644 --- a/src/tvu.rs +++ b/src/tvu.rs @@ -218,7 +218,7 @@ mod tests { use bincode::serialize; use chrono::prelude::*; use crdt::Crdt; - use entry; + use entry::Entry; use event::Event; use hash::{hash, Hash}; use logger; @@ -319,7 +319,7 @@ mod tests { let bank = &tvu.bank; let tr0 = Event::new_timestamp(&bob_keypair, Utc::now()); - let entry0 = entry::create_entry(&cur_hash, i, vec![tr0]); + let entry0 = Entry::new(&cur_hash, i, vec![tr0]); bank.register_entry_id(&cur_hash); cur_hash = hash(&cur_hash); @@ -331,7 +331,7 @@ mod tests { ); bank.register_entry_id(&cur_hash); cur_hash = hash(&cur_hash); - let entry1 = entry::create_entry(&cur_hash, i + num_blobs, vec![tr1]); + let entry1 = Entry::new(&cur_hash, i + num_blobs, vec![tr1]); bank.register_entry_id(&cur_hash); cur_hash = hash(&cur_hash);