From 115f912b2768c99ed11c66de194d989540cd520f Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Fri, 21 Jun 2024 23:26:35 +0200 Subject: [PATCH] chore: remove some more usages of BytesMut --- crates/net/downloaders/src/file_client.rs | 18 +++++++----------- crates/net/ecies/src/codec.rs | 4 ++-- crates/net/eth-wire/src/multiplex.rs | 13 +++++++------ crates/primitives/src/transaction/eip1559.rs | 3 +-- crates/primitives/src/transaction/eip2930.rs | 3 +-- crates/primitives/src/transaction/legacy.rs | 3 +-- crates/primitives/src/transaction/mod.rs | 3 ++- crates/rpc/rpc-engine-api/tests/it/payload.rs | 12 +++++------- crates/storage/codecs/src/alloy/access_list.rs | 14 ++++++-------- crates/storage/codecs/src/alloy/log.rs | 14 ++++++-------- 10 files changed, 38 insertions(+), 49 deletions(-) diff --git a/crates/net/downloaders/src/file_client.rs b/crates/net/downloaders/src/file_client.rs index 0ef12f1ba552..3464baf5d6ff 100644 --- a/crates/net/downloaders/src/file_client.rs +++ b/crates/net/downloaders/src/file_client.rs @@ -10,8 +10,8 @@ use reth_network_p2p::{ }; use reth_network_peers::PeerId; use reth_primitives::{ - BlockBody, BlockHash, BlockHashOrNumber, BlockNumber, BytesMut, Header, HeadersDirection, - SealedHeader, B256, + BlockBody, BlockHash, BlockHashOrNumber, BlockNumber, Header, HeadersDirection, SealedHeader, + B256, }; use std::{collections::HashMap, io, path::Path}; use thiserror::Error; @@ -419,26 +419,22 @@ impl ChunkedFileReader { let new_read_bytes_target_len = chunk_target_len - old_bytes_len; // read new bytes from file - let mut reader = BytesMut::zeroed(new_read_bytes_target_len as usize); + let prev_read_bytes_len = self.chunk.len(); + self.chunk.extend(std::iter::repeat(0).take(new_read_bytes_target_len as usize)); + let reader = &mut self.chunk[prev_read_bytes_len..]; // actual bytes that have been read - let new_read_bytes_len = self.file.read_exact(&mut reader).await? as u64; + let new_read_bytes_len = self.file.read_exact(reader).await? as u64; + let next_chunk_byte_len = self.chunk.len(); // update remaining file length self.file_byte_len -= new_read_bytes_len; - let prev_read_bytes_len = self.chunk.len(); - - // read new bytes from file into chunk - self.chunk.extend_from_slice(&reader[..]); - let next_chunk_byte_len = self.chunk.len(); - debug!(target: "downloaders::file", max_chunk_byte_len=self.chunk_byte_len, prev_read_bytes_len, new_read_bytes_target_len, new_read_bytes_len, - reader_capacity=reader.capacity(), next_chunk_byte_len, remaining_file_byte_len=self.file_byte_len, "new bytes were read from file" diff --git a/crates/net/ecies/src/codec.rs b/crates/net/ecies/src/codec.rs index 7ad30d38d0d9..54250e10210e 100644 --- a/crates/net/ecies/src/codec.rs +++ b/crates/net/ecies/src/codec.rs @@ -43,7 +43,7 @@ impl Decoder for ECIESCodec { type Item = IngressECIESValue; type Error = ECIESError; - #[instrument(level = "trace", skip_all, fields(peer=&*format!("{:?}", self.ecies.remote_id.map(|s| s.to_string())), state=&*format!("{:?}", self.state)))] + #[instrument(level = "trace", skip_all, fields(peer=?self.ecies.remote_id, state=?self.state))] fn decode(&mut self, buf: &mut BytesMut) -> Result, Self::Error> { loop { match self.state { @@ -115,7 +115,7 @@ impl Decoder for ECIESCodec { impl Encoder for ECIESCodec { type Error = io::Error; - #[instrument(level = "trace", skip(self, buf), fields(peer=&*format!("{:?}", self.ecies.remote_id.map(|s| s.to_string())), state=&*format!("{:?}", self.state)))] + #[instrument(level = "trace", skip(self, buf), fields(peer=?self.ecies.remote_id, state=?self.state))] fn encode(&mut self, item: EgressECIESValue, buf: &mut BytesMut) -> Result<(), Self::Error> { match item { EgressECIESValue::Auth => { diff --git a/crates/net/eth-wire/src/multiplex.rs b/crates/net/eth-wire/src/multiplex.rs index 262c2c193921..08e281f1a26c 100644 --- a/crates/net/eth-wire/src/multiplex.rs +++ b/crates/net/eth-wire/src/multiplex.rs @@ -312,13 +312,14 @@ impl ProtocolProxy { return Err(io::ErrorKind::InvalidInput.into()) } - let mut masked_bytes = BytesMut::zeroed(msg.len()); - masked_bytes[0] = msg[0] - .checked_add(self.shared_cap.relative_message_id_offset()) - .ok_or(io::ErrorKind::InvalidInput)?; + let offset = self.shared_cap.relative_message_id_offset(); + if offset == 0 { + return Ok(msg); + } - masked_bytes[1..].copy_from_slice(&msg[1..]); - Ok(masked_bytes.freeze()) + let mut masked = Vec::from(msg); + masked[0] = masked[0].checked_add(offset).ok_or(io::ErrorKind::InvalidInput)?; + Ok(masked.into()) } /// Unmasks the message ID of a message received from the wire. diff --git a/crates/primitives/src/transaction/eip1559.rs b/crates/primitives/src/transaction/eip1559.rs index 878efaa4f954..cce6f0ca22fc 100644 --- a/crates/primitives/src/transaction/eip1559.rs +++ b/crates/primitives/src/transaction/eip1559.rs @@ -1,7 +1,6 @@ use super::access_list::AccessList; use crate::{keccak256, Bytes, ChainId, Signature, TxKind, TxType, B256, U256}; use alloy_rlp::{length_of_length, Decodable, Encodable, Header}; -use bytes::BytesMut; use core::mem; use reth_codecs::{main_codec, Compact}; @@ -216,7 +215,7 @@ impl TxEip1559 { /// Outputs the signature hash of the transaction by first encoding without a signature, then /// hashing. pub(crate) fn signature_hash(&self) -> B256 { - let mut buf = BytesMut::with_capacity(self.payload_len_for_signature()); + let mut buf = Vec::with_capacity(self.payload_len_for_signature()); self.encode_for_signing(&mut buf); keccak256(&buf) } diff --git a/crates/primitives/src/transaction/eip2930.rs b/crates/primitives/src/transaction/eip2930.rs index 45bc5e67a28e..ebaa12785c1d 100644 --- a/crates/primitives/src/transaction/eip2930.rs +++ b/crates/primitives/src/transaction/eip2930.rs @@ -1,7 +1,6 @@ use super::access_list::AccessList; use crate::{keccak256, Bytes, ChainId, Signature, TxKind, TxType, B256, U256}; use alloy_rlp::{length_of_length, Decodable, Encodable, Header}; -use bytes::BytesMut; use core::mem; use reth_codecs::{main_codec, Compact}; @@ -179,7 +178,7 @@ impl TxEip2930 { /// Outputs the signature hash of the transaction by first encoding without a signature, then /// hashing. pub(crate) fn signature_hash(&self) -> B256 { - let mut buf = BytesMut::with_capacity(self.payload_len_for_signature()); + let mut buf = Vec::with_capacity(self.payload_len_for_signature()); self.encode_for_signing(&mut buf); keccak256(&buf) } diff --git a/crates/primitives/src/transaction/legacy.rs b/crates/primitives/src/transaction/legacy.rs index ebbe29a78c1e..09b661cf7995 100644 --- a/crates/primitives/src/transaction/legacy.rs +++ b/crates/primitives/src/transaction/legacy.rs @@ -1,6 +1,5 @@ use crate::{keccak256, Bytes, ChainId, Signature, TxKind, TxType, B256, U256}; use alloy_rlp::{length_of_length, Encodable, Header}; -use bytes::BytesMut; use core::mem; use reth_codecs::{main_codec, Compact}; @@ -163,7 +162,7 @@ impl TxLegacy { /// /// See [`Self::encode_for_signing`] for more information on the encoding format. pub(crate) fn signature_hash(&self) -> B256 { - let mut buf = BytesMut::with_capacity(self.payload_len_for_signature()); + let mut buf = Vec::with_capacity(self.payload_len_for_signature()); self.encode_for_signing(&mut buf); keccak256(&buf) } diff --git a/crates/primitives/src/transaction/mod.rs b/crates/primitives/src/transaction/mod.rs index 7a6d2a85b514..c23d454f868d 100644 --- a/crates/primitives/src/transaction/mod.rs +++ b/crates/primitives/src/transaction/mod.rs @@ -860,10 +860,11 @@ impl Compact for TransactionSignedNoHash { let tx_bits = if zstd_bit { TRANSACTION_COMPRESSOR.with(|compressor| { let mut compressor = compressor.borrow_mut(); - let mut tmp = bytes::BytesMut::with_capacity(200); + let mut tmp = Vec::with_capacity(256); let tx_bits = self.transaction.to_compact(&mut tmp); buf.put_slice(&compressor.compress(&tmp).expect("Failed to compress")); + tx_bits as u8 }) } else { diff --git a/crates/rpc/rpc-engine-api/tests/it/payload.rs b/crates/rpc/rpc-engine-api/tests/it/payload.rs index 94f011c02db3..0f2853f1f022 100644 --- a/crates/rpc/rpc-engine-api/tests/it/payload.rs +++ b/crates/rpc/rpc-engine-api/tests/it/payload.rs @@ -3,8 +3,7 @@ use alloy_rlp::{Decodable, Error as RlpError}; use assert_matches::assert_matches; use reth_primitives::{ - bytes::{Bytes, BytesMut}, - proofs, Block, SealedBlock, TransactionSigned, Withdrawals, B256, U256, + proofs, Block, Bytes, SealedBlock, TransactionSigned, Withdrawals, B256, U256, }; use reth_rpc_types::engine::{ ExecutionPayload, ExecutionPayloadBodyV1, ExecutionPayloadV1, PayloadError, @@ -59,20 +58,19 @@ fn payload_validation() { // Valid extra data let block_with_valid_extra_data = transform_block(block.clone(), |mut b| { - b.header.extra_data = BytesMut::zeroed(32).freeze().into(); + b.header.extra_data = Bytes::from_static(&[0; 32]); b }); assert_matches!(try_into_sealed_block(block_with_valid_extra_data, None), Ok(_)); // Invalid extra data - let block_with_invalid_extra_data: Bytes = BytesMut::zeroed(33).freeze(); + let block_with_invalid_extra_data = Bytes::from_static(&[0; 33]); let invalid_extra_data_block = transform_block(block.clone(), |mut b| { - b.header.extra_data = block_with_invalid_extra_data.clone().into(); + b.header.extra_data = block_with_invalid_extra_data.clone(); b }); assert_matches!( - try_into_sealed_block(invalid_extra_data_block,None), Err(PayloadError::ExtraData(data)) if data == block_with_invalid_extra_data ); @@ -92,7 +90,7 @@ fn payload_validation() { let mut payload_with_invalid_txs: ExecutionPayloadV1 = block_to_payload_v1(block.clone()); payload_with_invalid_txs.transactions.iter_mut().for_each(|tx| { - *tx = Bytes::new().into(); + *tx = Bytes::new(); }); let payload_with_invalid_txs = try_payload_v1_to_block(payload_with_invalid_txs); assert_matches!(payload_with_invalid_txs, Err(PayloadError::Decode(RlpError::InputTooShort))); diff --git a/crates/storage/codecs/src/alloy/access_list.rs b/crates/storage/codecs/src/alloy/access_list.rs index d3f906318848..f5564e81601a 100644 --- a/crates/storage/codecs/src/alloy/access_list.rs +++ b/crates/storage/codecs/src/alloy/access_list.rs @@ -8,12 +8,11 @@ impl Compact for AccessListItem { where B: bytes::BufMut + AsMut<[u8]>, { - let mut buffer = bytes::BytesMut::new(); + let mut buffer = Vec::new(); self.address.to_compact(&mut buffer); self.storage_keys.specialized_to_compact(&mut buffer); - let total_length = buffer.len(); - buf.put(buffer); - total_length + buf.put(&buffer[..]); + buffer.len() } fn from_compact(mut buf: &[u8], _: usize) -> (Self, &[u8]) { @@ -31,11 +30,10 @@ impl Compact for AccessList { where B: bytes::BufMut + AsMut<[u8]>, { - let mut buffer = bytes::BytesMut::new(); + let mut buffer = Vec::new(); self.0.to_compact(&mut buffer); - let total_length = buffer.len(); - buf.put(buffer); - total_length + buf.put(&buffer[..]); + buffer.len() } fn from_compact(mut buf: &[u8], _: usize) -> (Self, &[u8]) { diff --git a/crates/storage/codecs/src/alloy/log.rs b/crates/storage/codecs/src/alloy/log.rs index 8d5c30e0a0b3..eadcb894f3f1 100644 --- a/crates/storage/codecs/src/alloy/log.rs +++ b/crates/storage/codecs/src/alloy/log.rs @@ -10,13 +10,12 @@ impl Compact for LogData { where B: BufMut + AsMut<[u8]>, { - let mut buffer = bytes::BytesMut::new(); + let mut buffer = Vec::new(); let (topics, data) = self.split(); topics.specialized_to_compact(&mut buffer); data.to_compact(&mut buffer); - let total_length = buffer.len(); - buf.put(buffer); - total_length + buf.put(&buffer[..]); + buffer.len() } fn from_compact(mut buf: &[u8], _: usize) -> (Self, &[u8]) { @@ -33,12 +32,11 @@ impl Compact for Log { where B: BufMut + AsMut<[u8]>, { - let mut buffer = bytes::BytesMut::new(); + let mut buffer = Vec::new(); self.address.to_compact(&mut buffer); self.data.to_compact(&mut buffer); - let total_length = buffer.len(); - buf.put(buffer); - total_length + buf.put(&buffer[..]); + buffer.len() } fn from_compact(mut buf: &[u8], _: usize) -> (Self, &[u8]) {