Refactor and add test for new Entry::serialized_size()

This commit is contained in:
Greg Fitzgerald 2018-10-12 23:27:52 -06:00
parent 32aab82e32
commit 515c200d86
2 changed files with 20 additions and 7 deletions

View File

@ -97,11 +97,10 @@ impl Entry {
blob blob
} }
pub fn will_fit(transactions: &[Transaction]) -> bool { /// Estimate serialized_size of Entry without creating an Entry.
let txs_size = serialized_size(transactions).unwrap() as usize; pub fn serialized_size(transactions: &[Transaction]) -> u64 {
let txs_size = serialized_size(transactions).unwrap();
// Estimate serialized_size of Entry without creating an Entry. size_of::<u64>() as u64 + size_of::<Hash>() as u64 + txs_size
size_of::<u64>() + size_of::<Hash>() + txs_size <= BLOB_DATA_SIZE
} }
pub fn num_will_fit(transactions: &[Transaction]) -> usize { pub fn num_will_fit(transactions: &[Transaction]) -> usize {
@ -121,7 +120,7 @@ impl Entry {
next, next,
transactions.len() transactions.len()
); );
if Self::will_fit(&transactions[..num]) { if Self::serialized_size(&transactions[..num]) <= BLOB_DATA_SIZE as u64 {
next = (upper + num) / 2; next = (upper + num) / 2;
lower = num; lower = num;
debug!("num {} fits, maybe too well? trying {}", num, next); 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); let tx = Transaction::system_new(&keypair, keypair.pubkey(), 0, zero);
next_entry(&zero, 0, vec![tx]); 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()
);
}
} }

View File

@ -549,7 +549,9 @@ pub fn next_entries_mut(
next, next,
transactions.len() 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; next = (upper + chunk_end) / 2;
lower = chunk_end; lower = chunk_end;
debug!( debug!(