Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: remove some more usages of BytesMut #9025

Merged
merged 1 commit into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions crates/net/downloaders/src/file_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions crates/net/ecies/src/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option<Self::Item>, Self::Error> {
loop {
match self.state {
Expand Down Expand Up @@ -115,7 +115,7 @@ impl Decoder for ECIESCodec {
impl Encoder<EgressECIESValue> 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 => {
Expand Down
13 changes: 7 additions & 6 deletions crates/net/eth-wire/src/multiplex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for the eth protocol this only happens once when the initial status message is sent.

so likely not worth checking

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.
Expand Down
3 changes: 1 addition & 2 deletions crates/primitives/src/transaction/eip1559.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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)
}
Expand Down
3 changes: 1 addition & 2 deletions crates/primitives/src/transaction/eip2930.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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)
}
Expand Down
3 changes: 1 addition & 2 deletions crates/primitives/src/transaction/legacy.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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)
}
Expand Down
3 changes: 2 additions & 1 deletion crates/primitives/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 5 additions & 7 deletions crates/rpc/rpc-engine-api/tests/it/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
);
Expand All @@ -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)));
Expand Down
14 changes: 6 additions & 8 deletions crates/storage/codecs/src/alloy/access_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]) {
Expand All @@ -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]) {
Expand Down
14 changes: 6 additions & 8 deletions crates/storage/codecs/src/alloy/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]) {
Expand All @@ -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]) {
Expand Down
Loading