Skip to content
This repository has been archived by the owner on Sep 7, 2024. It is now read-only.

Commit

Permalink
Improved consensus.encode.deserealize_partial function signature
Browse files Browse the repository at this point in the history
Now function communicateds back to the caller the amount of consumed data by adding Cursor part to the Result value tuple – instead of using &mut argument.
This have led to simplified and more straightforward logic of the function and its callers.
  • Loading branch information
dr-orlovsky committed Feb 2, 2019
1 parent f5587cd commit 4426a14
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 22 deletions.
15 changes: 6 additions & 9 deletions src/consensus/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,29 +192,26 @@ pub fn serialize_hex<T: ?Sized>(data: &T) -> String
pub fn deserialize<'a, T>(data: &'a [u8]) -> Result<T, Error>
where T: Decodable<Cursor<&'a [u8]>>
{
let mut consumed: u64 = 0;
let rv = deserialize_partial(data, &mut consumed)?;
let (rv, consumed) = deserialize_partial(data)?;

// Fail if data is not consumed entirely.
if consumed == data.len() as u64 {
// Fail if data are not consumed entirely.
if consumed == data.len() {
Ok(rv)
} else {
eprintln!("total {}, consumed {}", data.len(), consumed);
Err(Error::ParseFailed("data not consumed entirely when explicitly deserializing"))
}
}

/// Deserialize an object from a vector, but will not report an error if said deserialization
/// doesn't consume the entire vector.
pub fn deserialize_partial<'a, T>(data: &'a [u8], consumed: &mut u64) -> Result<T, Error>
pub fn deserialize_partial<'a, T>(data: &'a [u8]) -> Result<(T, usize), Error>
where T: Decodable<Cursor<&'a [u8]>>
{
let mut decoder = Cursor::new(data);
let rv = Decodable::consensus_decode(&mut decoder)?;
let consumed = decoder.position() as usize;

*consumed = decoder.position();

Ok(rv)
Ok((rv, consumed))
}


Expand Down
23 changes: 10 additions & 13 deletions src/network/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,22 +156,21 @@ impl RawNetworkMessage {
max_iterations -= 1;

if remaining_part.len() > 0 {
let mut consumed: u64 = 0;
let result = encode::deserialize_partial::<RawNetworkMessage>(&remaining_part, &mut consumed);
let index = consumed as usize;
remaining_part.drain(..index);

match result {
match encode::deserialize_partial::<RawNetworkMessage>(&remaining_part) {
// In this case we just have an incomplete data, so we need to read more
Err(encode::Error::Io(ref err)) if err.kind() == io::ErrorKind::UnexpectedEof => (),
_ => return result,
Err(err) => return Err(err),
Ok((message, index)) => {
remaining_part.drain(..index);
return Ok(message)
},
}
}

let mut new_data = vec![0u8; 1024];
let count = stream.read(&mut new_data)?;
if count > 0 {
remaining_part.append(&mut new_data[0..count].to_vec());
remaining_part.extend(new_data[0..count].iter());
}
}
Err(encode::Error::ParseFailed("Zero-length input"))
Expand Down Expand Up @@ -345,7 +344,6 @@ mod test {

#[test]
fn deserialize_partial_message_test() {
let mut consumed: u64 = 0;
let data = [ 0xf9, 0xbe, 0xb4, 0xd9, 0x76, 0x65, 0x72, 0x73,
0x69, 0x6f, 0x6e, 0x00, 0x00, 0x00, 0x00, 0x00,
0x66, 0x00, 0x00, 0x00, 0xbe, 0x61, 0xb8, 0x27,
Expand All @@ -362,12 +360,11 @@ mod test {
0x10, 0x2f, 0x53, 0x61, 0x74, 0x6f, 0x73, 0x68,
0x69, 0x3a, 0x30, 0x2e, 0x31, 0x37, 0x2e, 0x31,
0x2f, 0x93, 0x8c, 0x08, 0x00, 0x01, 0, 0 ];
let msg = deserialize_partial::<RawNetworkMessage>(&data, &mut consumed);

let msg = deserialize_partial::<RawNetworkMessage>(&data);
assert!(msg.is_ok());
assert_eq!(consumed as usize, data.to_vec().len() - 2);

let msg = msg.unwrap();
let (msg, consumed) = msg.unwrap();
assert_eq!(consumed, data.to_vec().len() - 2);
assert_eq!(msg.magic, 0xd9b4bef9);
if let NetworkMessage::Version(version_msg) = msg.payload {
assert_eq!(version_msg.version, 70015);
Expand Down

0 comments on commit 4426a14

Please sign in to comment.