From f7149863ae057bbe5d634c9b7c06694a2b0f22ba Mon Sep 17 00:00:00 2001 From: Boog900 <54e72d8a-345f-4599-bd90-c6b9bc7d0ec5@aleeas.com> Date: Sat, 13 Jan 2024 14:39:18 +0000 Subject: [PATCH] levin: don't error when there isn't enough capacity --- net/levin/src/codec.rs | 10 ++++----- net/monero-wire/src/p2p/protocol.rs | 35 ++++++++++++++++++----------- 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/net/levin/src/codec.rs b/net/levin/src/codec.rs index 37f6579..11a7008 100644 --- a/net/levin/src/codec.rs +++ b/net/levin/src/codec.rs @@ -123,12 +123,12 @@ impl Decoder for LevinBucketCodec { impl Encoder> for LevinBucketCodec { type Error = BucketError; fn encode(&mut self, item: Bucket, dst: &mut BytesMut) -> Result<(), Self::Error> { - if dst.capacity() < BucketHead::::SIZE + item.body.len() { - return Err(BucketError::IO(std::io::Error::new( - ErrorKind::OutOfMemory, - "Not enough capacity to write the bucket", - ))); + if let Some(additional) = + (BucketHead::::SIZE + item.body.len()).checked_sub(dst.capacity()) + { + dst.reserve(additional) } + item.header.write_bytes(dst); dst.put_slice(&item.body); Ok(()) diff --git a/net/monero-wire/src/p2p/protocol.rs b/net/monero-wire/src/p2p/protocol.rs index 42b4f2a..1e547ad 100644 --- a/net/monero-wire/src/p2p/protocol.rs +++ b/net/monero-wire/src/p2p/protocol.rs @@ -80,6 +80,7 @@ pub struct ChainRequest { } /// A Chain Response +// TODO: Fix the fields on this: m_block_ids, m_block_weights #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct ChainResponse { /// Start Height @@ -87,36 +88,41 @@ pub struct ChainResponse { /// Total Height pub total_height: u64, /// Cumulative Difficulty Low - pub cumulative_difficulty_low: u64, + pub cumulative_difficulty: u64, /// Cumulative Difficulty High - pub cumulative_difficulty_high: u64, + #[serde(default = "default_zero")] + pub cumulative_difficulty_top64: u64, /// Block IDs - pub m_block_ids: Vec<[u8; 32]>, + #[serde(default = "ByteBuf::new")] + pub m_block_ids: ByteBuf, /// Block Weights - pub m_block_weights: Vec, + #[serde(default = "ByteBuf::new")] + pub m_block_weights: ByteBuf, /// The first Block in the response - pub first_block: Vec, + #[serde(default = "ByteBuf::new")] + pub first_block: ByteBuf, } impl ChainResponse { + /* pub fn new( start_height: u64, total_height: u64, cumulative_difficulty_128: u128, - m_block_ids: Vec<[u8; 32]>, + m_block_ids: ByteBuf, m_block_weights: Vec, - first_block: Vec, + first_block: ByteBuf, ) -> Self { let cumulative_difficulty_low = cumulative_difficulty_128 as u64; let cumulative_difficulty_high = (cumulative_difficulty_128 >> 64) as u64; Self { - start_height, - total_height, - cumulative_difficulty_low, - cumulative_difficulty_high, + // start_height, + // total_height, + // cumulative_difficulty_low, + // cumulative_difficulty_high, m_block_ids, - m_block_weights, - first_block, + // m_block_weights, + // first_block, } } pub fn cumulative_difficulty(&self) -> u128 { @@ -124,6 +130,9 @@ impl ChainResponse { ret <<= 64; ret | self.cumulative_difficulty_low as u128 } + + + */ } /// A Block that doesn't have transactions unless requested