From 296e64784ba4d3f18b1b6f18ed774a114dcc4d26 Mon Sep 17 00:00:00 2001 From: Greg Fitzgerald Date: Thu, 21 Jun 2018 16:02:52 -0700 Subject: [PATCH 1/3] Remove redundant clones --- src/ledger.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/ledger.rs b/src/ledger.rs index e76c469349716f..3e792d35e1b959 100644 --- a/src/ledger.rs +++ b/src/ledger.rs @@ -95,8 +95,7 @@ pub fn next_entries( ) -> Vec { 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); @@ -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); @@ -178,7 +177,7 @@ 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); From d64a1d87357a5ecc629f386609d3832aa835964a Mon Sep 17 00:00:00 2001 From: Greg Fitzgerald Date: Thu, 21 Jun 2018 16:09:17 -0700 Subject: [PATCH 2/3] Fix comment --- src/entry.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/entry.rs b/src/entry.rs index 03d163606b2da8..3555834e971f0f 100644 --- a/src/entry.rs +++ b/src/entry.rs @@ -85,7 +85,7 @@ fn add_transaction_data(hash_data: &mut Vec, 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 { From c9b4510775bd1f9c651d8faed7edc28728b0c598 Mon Sep 17 00:00:00 2001 From: Greg Fitzgerald Date: Thu, 21 Jun 2018 16:15:26 -0700 Subject: [PATCH 3/3] Add assertion for now next_entry must be called --- src/entry.rs | 10 ++++++++++ src/ledger.rs | 4 ++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/entry.rs b/src/entry.rs index 3555834e971f0f..19e4d13d8c2a72 100644 --- a/src/entry.rs +++ b/src/entry.rs @@ -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) -> Entry { + assert!(num_hashes > 0 || transactions.len() == 0); Entry { num_hashes, id: next_hash(start_hash, num_hashes, &transactions), @@ -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]); + } } diff --git a/src/ledger.rs b/src/ledger.rs index 3e792d35e1b959..6eaf3b1f26932c 100644 --- a/src/ledger.rs +++ b/src/ledger.rs @@ -179,13 +179,13 @@ mod tests { let tx0 = Transaction::new(&keypair, keypair.pubkey(), 1, next_id); 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); }