From 515c200d86581f70c5b9efc510e3b4408d896065 Mon Sep 17 00:00:00 2001 From: Greg Fitzgerald Date: Fri, 12 Oct 2018 23:27:52 -0600 Subject: [PATCH] Refactor and add test for new Entry::serialized_size() --- src/entry.rs | 23 +++++++++++++++++------ src/ledger.rs | 4 +++- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/entry.rs b/src/entry.rs index 96db77170..31cc4a528 100644 --- a/src/entry.rs +++ b/src/entry.rs @@ -97,11 +97,10 @@ impl Entry { blob } - pub fn will_fit(transactions: &[Transaction]) -> bool { - let txs_size = serialized_size(transactions).unwrap() as usize; - - // Estimate serialized_size of Entry without creating an Entry. - size_of::() + size_of::() + txs_size <= BLOB_DATA_SIZE + /// Estimate serialized_size of Entry without creating an Entry. + pub fn serialized_size(transactions: &[Transaction]) -> u64 { + let txs_size = serialized_size(transactions).unwrap(); + size_of::() as u64 + size_of::() as u64 + txs_size } pub fn num_will_fit(transactions: &[Transaction]) -> usize { @@ -121,7 +120,7 @@ impl Entry { next, transactions.len() ); - if Self::will_fit(&transactions[..num]) { + if Self::serialized_size(&transactions[..num]) <= BLOB_DATA_SIZE as u64 { next = (upper + num) / 2; lower = num; debug!("num {} fits, maybe too well? trying {}", num, next); @@ -304,4 +303,16 @@ mod tests { let tx = Transaction::system_new(&keypair, keypair.pubkey(), 0, zero); next_entry(&zero, 0, vec![tx]); } + + #[test] + fn test_serialized_size() { + let zero = Hash::default(); + let keypair = Keypair::new(); + let tx = Transaction::system_new(&keypair, keypair.pubkey(), 0, zero); + let entry = next_entry(&zero, 1, vec![tx.clone()]); + assert_eq!( + Entry::serialized_size(&[tx]), + serialized_size(&entry).unwrap() + ); + } } diff --git a/src/ledger.rs b/src/ledger.rs index c821f6d80..7177da055 100644 --- a/src/ledger.rs +++ b/src/ledger.rs @@ -549,7 +549,9 @@ pub fn next_entries_mut( next, transactions.len() ); - if Entry::will_fit(&transactions[chunk_start..chunk_end]) { + if Entry::serialized_size(&transactions[chunk_start..chunk_end]) + <= BLOB_DATA_SIZE as u64 + { next = (upper + chunk_end) / 2; lower = chunk_end; debug!(