levin: don't error when there isn't enough capacity

This commit is contained in:
Boog900 2024-01-13 14:39:18 +00:00
parent a0b9280801
commit f7149863ae
No known key found for this signature in database
GPG key ID: 5401367FB7302004
2 changed files with 27 additions and 18 deletions

View file

@ -123,12 +123,12 @@ impl<C: LevinCommand> Decoder for LevinBucketCodec<C> {
impl<C: LevinCommand> Encoder<Bucket<C>> for LevinBucketCodec<C> { impl<C: LevinCommand> Encoder<Bucket<C>> for LevinBucketCodec<C> {
type Error = BucketError; type Error = BucketError;
fn encode(&mut self, item: Bucket<C>, dst: &mut BytesMut) -> Result<(), Self::Error> { fn encode(&mut self, item: Bucket<C>, dst: &mut BytesMut) -> Result<(), Self::Error> {
if dst.capacity() < BucketHead::<C>::SIZE + item.body.len() { if let Some(additional) =
return Err(BucketError::IO(std::io::Error::new( (BucketHead::<C>::SIZE + item.body.len()).checked_sub(dst.capacity())
ErrorKind::OutOfMemory, {
"Not enough capacity to write the bucket", dst.reserve(additional)
)));
} }
item.header.write_bytes(dst); item.header.write_bytes(dst);
dst.put_slice(&item.body); dst.put_slice(&item.body);
Ok(()) Ok(())

View file

@ -80,6 +80,7 @@ pub struct ChainRequest {
} }
/// A Chain Response /// A Chain Response
// TODO: Fix the fields on this: m_block_ids, m_block_weights
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ChainResponse { pub struct ChainResponse {
/// Start Height /// Start Height
@ -87,36 +88,41 @@ pub struct ChainResponse {
/// Total Height /// Total Height
pub total_height: u64, pub total_height: u64,
/// Cumulative Difficulty Low /// Cumulative Difficulty Low
pub cumulative_difficulty_low: u64, pub cumulative_difficulty: u64,
/// Cumulative Difficulty High /// Cumulative Difficulty High
pub cumulative_difficulty_high: u64, #[serde(default = "default_zero")]
pub cumulative_difficulty_top64: u64,
/// Block IDs /// Block IDs
pub m_block_ids: Vec<[u8; 32]>, #[serde(default = "ByteBuf::new")]
pub m_block_ids: ByteBuf,
/// Block Weights /// Block Weights
pub m_block_weights: Vec<u64>, #[serde(default = "ByteBuf::new")]
pub m_block_weights: ByteBuf,
/// The first Block in the response /// The first Block in the response
pub first_block: Vec<u8>, #[serde(default = "ByteBuf::new")]
pub first_block: ByteBuf,
} }
impl ChainResponse { impl ChainResponse {
/*
pub fn new( pub fn new(
start_height: u64, start_height: u64,
total_height: u64, total_height: u64,
cumulative_difficulty_128: u128, cumulative_difficulty_128: u128,
m_block_ids: Vec<[u8; 32]>, m_block_ids: ByteBuf,
m_block_weights: Vec<u64>, m_block_weights: Vec<u64>,
first_block: Vec<u8>, first_block: ByteBuf,
) -> Self { ) -> Self {
let cumulative_difficulty_low = cumulative_difficulty_128 as u64; let cumulative_difficulty_low = cumulative_difficulty_128 as u64;
let cumulative_difficulty_high = (cumulative_difficulty_128 >> 64) as u64; let cumulative_difficulty_high = (cumulative_difficulty_128 >> 64) as u64;
Self { Self {
start_height, // start_height,
total_height, // total_height,
cumulative_difficulty_low, // cumulative_difficulty_low,
cumulative_difficulty_high, // cumulative_difficulty_high,
m_block_ids, m_block_ids,
m_block_weights, // m_block_weights,
first_block, // first_block,
} }
} }
pub fn cumulative_difficulty(&self) -> u128 { pub fn cumulative_difficulty(&self) -> u128 {
@ -124,6 +130,9 @@ impl ChainResponse {
ret <<= 64; ret <<= 64;
ret | self.cumulative_difficulty_low as u128 ret | self.cumulative_difficulty_low as u128
} }
*/
} }
/// A Block that doesn't have transactions unless requested /// A Block that doesn't have transactions unless requested