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 entry #406

Merged
merged 3 commits into from
Jun 22, 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
12 changes: 11 additions & 1 deletion src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ fn add_transaction_data(hash_data: &mut Vec<u8>, tx: &Transaction) {
/// Creates the hash `num_hashes` after `start_hash`. If the transaction contains
/// a signature, the final hash will be a hash of both the previous ID and
/// the signature. If num_hashes is zero and there's no transaction data,
// start_hash is returned.
/// start_hash is returned.
fn next_hash(start_hash: &Hash, num_hashes: u64, transactions: &[Transaction]) -> Hash {
let mut id = *start_hash;
for _ in 1..num_hashes {
Expand All @@ -109,6 +109,7 @@ fn next_hash(start_hash: &Hash, num_hashes: u64, transactions: &[Transaction]) -

/// Creates the next Tick or Transaction Entry `num_hashes` after `start_hash`.
pub fn next_entry(start_hash: &Hash, num_hashes: u64, transactions: Vec<Transaction>) -> Entry {
assert!(num_hashes > 0 || transactions.len() == 0);
Entry {
num_hashes,
id: next_hash(start_hash, num_hashes, &transactions),
Expand Down Expand Up @@ -180,4 +181,13 @@ mod tests {
assert_eq!(tick.num_hashes, 0);
assert_eq!(tick.id, zero);
}

#[test]
#[should_panic]
fn test_next_entry_panic() {
let zero = Hash::default();
let keypair = KeyPair::new();
let tx = Transaction::new(&keypair, keypair.pubkey(), 0, zero);
next_entry(&zero, 0, vec![tx]);
}
}
13 changes: 6 additions & 7 deletions src/ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ pub fn next_entries(
) -> Vec<Entry> {
let mut id = *start_hash;
let mut entries = vec![];
for transactions in &transaction_batches {
let transactions = transactions.clone();
for transactions in transaction_batches {
let entry = next_entry(&id, num_hashes, transactions);
id = entry.id;
entries.push(entry);
Expand Down Expand Up @@ -153,10 +152,10 @@ mod tests {
let one = hash(&zero);
let keypair = KeyPair::new();
let tx0 = Transaction::new(&keypair, keypair.pubkey(), 1, one);
let transactions = vec![tx0.clone(); 10000];
let transactions = vec![tx0; 10000];
let e0 = Entry::new(&zero, 0, transactions);

let entries = vec![e0.clone(); 1];
let entries = vec![e0];
let blob_recycler = BlobRecycler::default();
let mut blob_q = VecDeque::new();
entries.to_blobs(&blob_recycler, &mut blob_q);
Expand All @@ -178,15 +177,15 @@ mod tests {
let next_id = hash(&id);
let keypair = KeyPair::new();
let tx0 = Transaction::new(&keypair, keypair.pubkey(), 1, next_id);
let transactions = vec![tx0.clone(); 5];
let transactions = vec![tx0; 5];
let transaction_batches = vec![transactions.clone(); 5];
let entries0 = next_entries(&id, 0, transaction_batches);
let entries0 = next_entries(&id, 1, transaction_batches);

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

let mut entries1 = vec![];
for _ in 0..5 {
let entry = next_entry(&id, 0, transactions.clone());
let entry = next_entry(&id, 1, transactions.clone());
id = entry.id;
entries1.push(entry);
}
Expand Down