diff --git a/beacon_node/eth2_libp2p/src/rpc/codec/ssz_snappy.rs b/beacon_node/eth2_libp2p/src/rpc/codec/ssz_snappy.rs index 8fe12adca..c2440a316 100644 --- a/beacon_node/eth2_libp2p/src/rpc/codec/ssz_snappy.rs +++ b/beacon_node/eth2_libp2p/src/rpc/codec/ssz_snappy.rs @@ -94,19 +94,20 @@ impl Decoder for SSZSnappyInboundCodec { type Error = RPCError; fn decode(&mut self, src: &mut BytesMut) -> Result, Self::Error> { - if self.len.is_none() { + let length = if let Some(length) = self.len { + length + } else { // Decode the length of the uncompressed bytes from an unsigned varint // Note: length-prefix of > 10 bytes(uint64) would be a decoding error match self.inner.decode(src).map_err(RPCError::from)? { Some(length) => { self.len = Some(length); + length } None => return Ok(None), // need more bytes to decode length } }; - let length = self.len.expect("length should be Some"); - // Should not attempt to decode rpc chunks with `length > max_packet_size` or not within bounds of // packet size for ssz container corresponding to `self.protocol`. let ssz_limits = self.protocol.rpc_request_limits(); @@ -245,19 +246,20 @@ impl Decoder for SSZSnappyOutboundCodec { type Error = RPCError; fn decode(&mut self, src: &mut BytesMut) -> Result, Self::Error> { - if self.len.is_none() { + let length = if let Some(length) = self.len { + length + } else { // Decode the length of the uncompressed bytes from an unsigned varint // Note: length-prefix of > 10 bytes(uint64) would be a decoding error match self.inner.decode(src).map_err(RPCError::from)? { Some(length) => { self.len = Some(length as usize); + length } None => return Ok(None), // need more bytes to decode length } }; - let length = self.len.expect("length should be Some"); - // Should not attempt to decode rpc chunks with `length > max_packet_size` or not within bounds of // packet size for ssz container corresponding to `self.protocol`. let ssz_limits = self.protocol.rpc_response_limits::(); @@ -323,18 +325,19 @@ impl OutboundCodec> for SSZSnappyOutboundCodec &mut self, src: &mut BytesMut, ) -> Result, RPCError> { - if self.len.is_none() { + let length = if let Some(length) = self.len { + length + } else { // Decode the length of the uncompressed bytes from an unsigned varint match self.inner.decode(src).map_err(RPCError::from)? { Some(length) => { self.len = Some(length as usize); + length } None => return Ok(None), // need more bytes to decode length } }; - let length = self.len.expect("length should be Some"); - // Should not attempt to decode rpc chunks with `length > max_packet_size` or not within bounds of // packet size for ssz container corresponding to `ErrorType`. if length > self.max_packet_size || length > *ERROR_TYPE_MAX || length < *ERROR_TYPE_MIN {