implement Blob::get_size(), the counterpart of Blob::set_size()
This commit is contained in:
		| @@ -575,7 +575,7 @@ pub fn recover( | ||||
|             data_size, | ||||
|             locks[n].data()[0] | ||||
|         ); | ||||
|         if data_size > BLOB_SIZE as u64 { | ||||
|         if data_size > BLOB_DATA_SIZE as u64 { | ||||
|             corrupt = true; | ||||
|         } | ||||
|     } | ||||
|   | ||||
| @@ -4,7 +4,7 @@ | ||||
| use bincode::{self, deserialize, serialize_into}; | ||||
| use entry::Entry; | ||||
| use hash::Hash; | ||||
| use packet::{self, SharedBlob, BLOB_SIZE}; | ||||
| use packet::{self, SharedBlob, BLOB_DATA_SIZE}; | ||||
| use rayon::prelude::*; | ||||
| use std::collections::VecDeque; | ||||
| use std::io::Cursor; | ||||
| @@ -44,7 +44,7 @@ impl Block for [Entry] { | ||||
|                 serialize_into(&mut out, &entry).expect("failed to serialize output"); | ||||
|                 out.position() as usize | ||||
|             }; | ||||
|             assert!(pos < BLOB_SIZE); | ||||
|             assert!(pos < BLOB_DATA_SIZE); | ||||
|             blob.write().unwrap().set_size(pos); | ||||
|             q.push_back(blob); | ||||
|         } | ||||
| @@ -57,7 +57,8 @@ pub fn reconstruct_entries_from_blobs(blobs: VecDeque<SharedBlob>) -> bincode::R | ||||
|     for blob in blobs { | ||||
|         let entry = { | ||||
|             let msg = blob.read().unwrap(); | ||||
|             deserialize(&msg.data()[..msg.get_data_size().unwrap() as usize]) | ||||
|             let msg_size = msg.get_size().unwrap(); | ||||
|             deserialize(&msg.data()[..msg_size]) | ||||
|         }; | ||||
|  | ||||
|         match entry { | ||||
| @@ -149,6 +150,7 @@ pub fn next_entries( | ||||
| mod tests { | ||||
|     use super::*; | ||||
|     use bincode::serialized_size; | ||||
|     use chrono::prelude::*; | ||||
|     use entry::{next_entry, Entry}; | ||||
|     use hash::hash; | ||||
|     use packet::{BlobRecycler, BLOB_DATA_SIZE, PACKET_DATA_SIZE}; | ||||
| @@ -175,10 +177,30 @@ mod tests { | ||||
|         let zero = Hash::default(); | ||||
|         let one = hash(&zero); | ||||
|         let keypair = KeyPair::new(); | ||||
|         let tx0 = Transaction::new(&keypair, keypair.pubkey(), 1, one); | ||||
|         let transactions = vec![tx0; 10_000]; | ||||
|         let tx0 = Transaction::new_vote( | ||||
|             &keypair, | ||||
|             Vote { | ||||
|                 version: 0, | ||||
|                 contact_info_version: 1, | ||||
|             }, | ||||
|             one, | ||||
|             1, | ||||
|         ); | ||||
|         let tx1 = Transaction::new_timestamp(&keypair, Utc::now(), one); | ||||
|         // | ||||
|         // TODO: this magic number and the mix of transaction types | ||||
|         //       is designed to fill up a Blob more or less exactly, | ||||
|         //       to get near enough the the threshold that | ||||
|         //       deserialization falls over if it uses the wrong size() | ||||
|         //       parameter to index into blob.data() | ||||
|         // | ||||
|         // magic numbers -----------------+ | ||||
|         //                                | | ||||
|         //                                V | ||||
|         let mut transactions = vec![tx0; 362]; | ||||
|         transactions.extend(vec![tx1; 100]); | ||||
|         let entries = next_entries(&zero, 0, transactions); | ||||
|  | ||||
|         eprintln!("entries.len() {}", entries.len()); | ||||
|         let blob_recycler = BlobRecycler::default(); | ||||
|         let mut blob_q = VecDeque::new(); | ||||
|         entries.to_blobs(&blob_recycler, &mut blob_q); | ||||
|   | ||||
| @@ -373,11 +373,18 @@ impl Blob { | ||||
|     pub fn data_mut(&mut self) -> &mut [u8] { | ||||
|         &mut self.data[BLOB_HEADER_SIZE..] | ||||
|     } | ||||
|     pub fn get_size(&self) -> Result<usize> { | ||||
|         let size = self.get_data_size()? as usize; | ||||
|         assert_eq!(self.meta.size, size); | ||||
|         // TODO: return an error instead of panicking | ||||
|         Ok(size) | ||||
|     } | ||||
|     pub fn set_size(&mut self, size: usize) { | ||||
|         let new_size = size + BLOB_HEADER_SIZE; | ||||
|         self.meta.size = new_size; | ||||
|         self.set_data_size(new_size as u64).unwrap(); | ||||
|     } | ||||
|  | ||||
|     pub fn recv_from(re: &BlobRecycler, socket: &UdpSocket) -> Result<SharedBlobs> { | ||||
|         let mut v = VecDeque::new(); | ||||
|         //DOCUMENTED SIDE-EFFECT | ||||
|   | ||||
		Reference in New Issue
	
	Block a user