diff --git a/radicle-node/src/wire.rs b/radicle-node/src/wire.rs index 9676fbca..84711d79 100644 --- a/radicle-node/src/wire.rs +++ b/radicle-node/src/wire.rs @@ -26,10 +26,13 @@ use crate::storage::refs::Refs; use crate::storage::refs::SignedRefs; use crate::storage::WriteStorage; -/// The default type we use to represent sizes. -/// Four bytes is more than enough for anything sent over the wire. -/// Note that in certain cases, we may use only one or two byte types. -pub type Size = u32; +/// The default type we use to represent sizes on the wire. +/// +/// Since wire messages are limited to 64KB by the transport layer, +/// two bytes is enough to represent any message. +/// +/// Note that in certain cases, we may use a smaller type. +pub type Size = u16; #[derive(thiserror::Error, Debug)] pub enum Error { @@ -123,21 +126,6 @@ impl Encode for u64 { } } -impl Encode for usize { - /// We encode this type to a [`u32`], since there's no need to send larger messages - /// over the network. - fn encode(&self, writer: &mut W) -> Result { - assert!( - *self <= u32::MAX as usize, - "Cannot encode sizes larger than {}", - u32::MAX - ); - writer.write_u32::(*self as u32)?; - - Ok(mem::size_of::()) - } -} - impl Encode for PublicKey { fn encode(&self, writer: &mut W) -> Result { self.deref().encode(writer) @@ -215,7 +203,11 @@ impl Encode for Id { impl Encode for Refs { fn encode(&self, writer: &mut W) -> Result { - let mut n = self.len().encode(writer)?; + let len: Size = self + .len() + .try_into() + .map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?; + let mut n = len.encode(writer)?; for (name, oid) in self.iter() { n += name.as_str().encode(writer)?; @@ -251,7 +243,7 @@ impl Decode for PublicKey { impl Decode for Refs { fn decode(reader: &mut R) -> Result { - let len = usize::decode(reader)?; + let len = Size::decode(reader)?; let mut refs = BTreeMap::new(); for _ in 0..len { @@ -267,7 +259,7 @@ impl Decode for Refs { impl Decode for git::Oid { fn decode(reader: &mut R) -> Result { - let len = usize::decode(reader)?; + let len = Size::decode(reader)? as usize; #[allow(non_upper_case_globals)] const expected: usize = mem::size_of::(); @@ -318,16 +310,6 @@ impl Decode for u64 { } } -impl Decode for usize { - fn decode(reader: &mut R) -> Result { - let size: usize = u32::decode(reader)? - .try_into() - .map_err(|_| io::Error::from(io::ErrorKind::InvalidInput))?; - - Ok(size) - } -} - impl Decode for [u8; N] { fn decode(reader: &mut R) -> Result { let mut ary = [0; N]; @@ -587,16 +569,6 @@ mod tests { assert_eq!(deserialize::(&serialize(&input)).unwrap(), input); } - #[quickcheck] - fn prop_usize(input: usize) -> quickcheck::TestResult { - if input > u32::MAX as usize { - return quickcheck::TestResult::discard(); - } - assert_eq!(deserialize::(&serialize(&input)).unwrap(), input); - - quickcheck::TestResult::passed() - } - #[quickcheck] fn prop_string(input: String) -> quickcheck::TestResult { if input.len() > u8::MAX as usize { diff --git a/radicle-node/src/wire/message.rs b/radicle-node/src/wire/message.rs index fed0bbcd..9c5685ba 100644 --- a/radicle-node/src/wire/message.rs +++ b/radicle-node/src/wire/message.rs @@ -1,4 +1,4 @@ -use std::{io, net}; +use std::{io, mem, net}; use byteorder::{NetworkEndian, ReadBytesExt}; @@ -7,10 +7,9 @@ use crate::prelude::*; use crate::service::message::*; use crate::wire; -use std::mem::size_of; - /// The maximum supported message size in bytes. -pub const MAX_PAYLOAD_SIZE_BYTES: u16 = u16::MAX - (size_of::() as u16); +pub const MAX_PAYLOAD_SIZE_BYTES: wire::Size = + wire::Size::MAX - (mem::size_of::() as wire::Size); /// Message type. #[repr(u16)]