mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
perf: replace BytesMut with alloy_rlp::encode (#7087)
This commit is contained in:
committed by
GitHub
parent
455b2afa5e
commit
d2c8b77e64
@ -433,11 +433,9 @@ mod tests {
|
||||
});
|
||||
|
||||
// TODO resolve dependency issue
|
||||
// let mut encoded = BytesMut::new();
|
||||
// transaction.encode(&mut encoded);
|
||||
// let expected =
|
||||
// hex!("ec098504a817c800825208943535353535353535353535353535353535353535880de0b6b3a764000080018080");
|
||||
// assert_eq!(expected, encoded.as_ref());
|
||||
// assert_eq!(expected, &alloy_rlp::encode(transaction));
|
||||
|
||||
let hash = transaction.signature_hash();
|
||||
let expected =
|
||||
|
||||
@ -813,9 +813,7 @@ mod tests {
|
||||
assert_eq!(pubkey.to_vec(), expected_pubkey);
|
||||
assert!(enr.0.verify());
|
||||
|
||||
let mut encoded = Vec::new();
|
||||
enr.encode(&mut encoded);
|
||||
assert_eq!(&encoded[..], &valid_record[..]);
|
||||
assert_eq!(&alloy_rlp::encode(&enr)[..], &valid_record[..]);
|
||||
|
||||
// ensure the length is equal
|
||||
assert_eq!(enr.length(), valid_record.len());
|
||||
@ -867,9 +865,7 @@ mod tests {
|
||||
EnrWrapper::new(builder.build(&key).unwrap())
|
||||
};
|
||||
|
||||
let mut encoded = Vec::new();
|
||||
enr.encode(&mut encoded);
|
||||
let mut encoded_bytes = &encoded[..];
|
||||
let mut encoded_bytes = &alloy_rlp::encode(&enr)[..];
|
||||
let decoded_enr = EnrWrapper::<SecretKey>::decode(&mut encoded_bytes).unwrap();
|
||||
|
||||
// Byte array must be consumed after enr has finished decoding
|
||||
|
||||
@ -215,10 +215,9 @@ where
|
||||
&mut self,
|
||||
item: EthBroadcastMessage,
|
||||
) -> Result<(), EthStreamError> {
|
||||
let mut bytes = Vec::new();
|
||||
ProtocolBroadcastMessage::from(item).encode(&mut bytes);
|
||||
|
||||
self.inner.start_send_unpin(bytes.into())?;
|
||||
self.inner.start_send_unpin(Bytes::from(alloy_rlp::encode(
|
||||
ProtocolBroadcastMessage::from(item),
|
||||
)))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@ -296,10 +295,9 @@ where
|
||||
return Err(EthStreamError::EthHandshakeError(EthHandshakeError::StatusNotInHandshake))
|
||||
}
|
||||
|
||||
let mut bytes = Vec::new();
|
||||
ProtocolMessage::from(item).encode(&mut bytes);
|
||||
|
||||
self.project().inner.start_send(bytes.into())?;
|
||||
self.project()
|
||||
.inner
|
||||
.start_send(Bytes::from(alloy_rlp::encode(ProtocolMessage::from(item))))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -97,9 +97,7 @@ where
|
||||
trace!(?hello, "sending p2p hello to peer");
|
||||
|
||||
// send our hello message with the Sink
|
||||
let mut raw_hello_bytes = Vec::with_capacity(142);
|
||||
P2PMessage::Hello(hello.message()).encode(&mut raw_hello_bytes);
|
||||
self.inner.send(raw_hello_bytes.into()).await?;
|
||||
self.inner.send(alloy_rlp::encode(P2PMessage::Hello(hello.message())).into()).await?;
|
||||
|
||||
let first_message_bytes = tokio::time::timeout(HANDSHAKE_TIMEOUT, self.inner.next())
|
||||
.await
|
||||
@ -305,18 +303,12 @@ impl<S> P2PStream<S> {
|
||||
|
||||
/// Queues in a _snappy_ encoded [`P2PMessage::Pong`] message.
|
||||
fn send_pong(&mut self) {
|
||||
let pong = P2PMessage::Pong;
|
||||
let mut pong_bytes = BytesMut::with_capacity(pong.length());
|
||||
pong.encode(&mut pong_bytes);
|
||||
self.outgoing_messages.push_back(pong_bytes.freeze());
|
||||
self.outgoing_messages.push_back(Bytes::from(alloy_rlp::encode(P2PMessage::Pong)));
|
||||
}
|
||||
|
||||
/// Queues in a _snappy_ encoded [`P2PMessage::Ping`] message.
|
||||
fn send_ping(&mut self) {
|
||||
let ping = P2PMessage::Ping;
|
||||
let mut ping_bytes = BytesMut::with_capacity(ping.length());
|
||||
ping.encode(&mut ping_bytes);
|
||||
self.outgoing_messages.push_back(ping_bytes.freeze());
|
||||
self.outgoing_messages.push_back(Bytes::from(alloy_rlp::encode(P2PMessage::Ping)));
|
||||
}
|
||||
}
|
||||
|
||||
@ -897,11 +889,10 @@ mod tests {
|
||||
|
||||
// Unrolled `disconnect` method, without compression
|
||||
p2p_stream.outgoing_messages.clear();
|
||||
let disconnect = P2PMessage::Disconnect(DisconnectReason::SubprotocolSpecific);
|
||||
let mut buf = BytesMut::with_capacity(disconnect.length());
|
||||
disconnect.encode(&mut buf);
|
||||
|
||||
p2p_stream.outgoing_messages.push_back(buf.freeze());
|
||||
p2p_stream.outgoing_messages.push_back(Bytes::from(alloy_rlp::encode(
|
||||
P2PMessage::Disconnect(DisconnectReason::SubprotocolSpecific),
|
||||
)));
|
||||
p2p_stream.disconnecting = true;
|
||||
p2p_stream.close().await.unwrap();
|
||||
});
|
||||
@ -1031,10 +1022,7 @@ mod tests {
|
||||
let snappy_ping = b"\x02\x01\0\xc0";
|
||||
let ping = P2PMessage::decode(&mut &snappy_ping[..]).unwrap();
|
||||
assert!(matches!(ping, P2PMessage::Ping));
|
||||
|
||||
let mut buf = BytesMut::with_capacity(ping.length());
|
||||
ping.encode(&mut buf);
|
||||
assert_eq!(buf.as_ref(), &snappy_ping[..]);
|
||||
assert_eq!(alloy_rlp::encode(ping), &snappy_ping[..]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -1042,9 +1030,6 @@ mod tests {
|
||||
let snappy_pong = b"\x03\x01\0\xc0";
|
||||
let pong = P2PMessage::decode(&mut &snappy_pong[..]).unwrap();
|
||||
assert!(matches!(pong, P2PMessage::Pong));
|
||||
|
||||
let mut buf = BytesMut::with_capacity(pong.length());
|
||||
pong.encode(&mut buf);
|
||||
assert_eq!(buf.as_ref(), &snappy_pong[..]);
|
||||
assert_eq!(alloy_rlp::encode(pong), &snappy_pong[..]);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
//! Config traits for various node components.
|
||||
|
||||
use alloy_rlp::Encodable;
|
||||
use reth_network::protocol::IntoRlpxSubProtocol;
|
||||
use reth_primitives::Bytes;
|
||||
use reth_rpc::{
|
||||
@ -89,9 +88,7 @@ pub trait PayloadBuilderConfig {
|
||||
|
||||
/// Returns the rlp-encoded extradata bytes.
|
||||
fn extradata_rlp_bytes(&self) -> Bytes {
|
||||
let mut extradata = Vec::with_capacity(self.extradata().as_bytes().len() + 1);
|
||||
self.extradata().as_bytes().encode(&mut extradata);
|
||||
extradata.into()
|
||||
alloy_rlp::encode(self.extradata().as_bytes()).into()
|
||||
}
|
||||
|
||||
/// The interval at which the job should build a new payload after the last.
|
||||
|
||||
@ -9,7 +9,6 @@
|
||||
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
|
||||
|
||||
use crate::metrics::PayloadBuilderMetrics;
|
||||
use alloy_rlp::Encodable;
|
||||
use futures_core::ready;
|
||||
use futures_util::FutureExt;
|
||||
use reth_interfaces::RethResult;
|
||||
@ -300,10 +299,8 @@ impl BasicPayloadJobGeneratorConfig {
|
||||
|
||||
impl Default for BasicPayloadJobGeneratorConfig {
|
||||
fn default() -> Self {
|
||||
let mut extradata = Vec::with_capacity(RETH_CLIENT_VERSION.as_bytes().len() + 1);
|
||||
RETH_CLIENT_VERSION.as_bytes().encode(&mut extradata);
|
||||
Self {
|
||||
extradata: extradata.into(),
|
||||
extradata: alloy_rlp::encode(RETH_CLIENT_VERSION.as_bytes()).into(),
|
||||
max_gas_limit: ETHEREUM_BLOCK_GAS_LIMIT,
|
||||
interval: Duration::from_secs(1),
|
||||
// 12s slot time
|
||||
|
||||
@ -1544,7 +1544,6 @@ impl DepositContract {
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::{b256, hex, trie::TrieAccount, ChainConfig, GenesisAccount};
|
||||
use alloy_rlp::Encodable;
|
||||
use std::{collections::HashMap, str::FromStr};
|
||||
|
||||
fn test_fork_ids(spec: &ChainSpec, cases: &[(Head, ForkId)]) {
|
||||
@ -2615,9 +2614,7 @@ Post-merge hard forks (timestamp based):
|
||||
|
||||
for (key, expected_rlp) in key_rlp {
|
||||
let account = chainspec.genesis.alloc.get(&key).expect("account should exist");
|
||||
let mut account_rlp = Vec::new();
|
||||
TrieAccount::from(account.clone()).encode(&mut account_rlp);
|
||||
assert_eq!(account_rlp, expected_rlp)
|
||||
assert_eq!(&alloy_rlp::encode(TrieAccount::from(account.clone())), expected_rlp)
|
||||
}
|
||||
|
||||
assert_eq!(chainspec.genesis_hash, None);
|
||||
|
||||
@ -206,9 +206,7 @@ impl Header {
|
||||
/// Heavy function that will calculate hash of data and will *not* save the change to metadata.
|
||||
/// Use [`Header::seal`], [`SealedHeader`] and unlock if you need hash to be persistent.
|
||||
pub fn hash_slow(&self) -> B256 {
|
||||
let mut out = Vec::new();
|
||||
self.encode(&mut out);
|
||||
keccak256(&out)
|
||||
keccak256(alloy_rlp::encode(self))
|
||||
}
|
||||
|
||||
/// Checks if the header is empty - has no transactions and no ommers
|
||||
|
||||
@ -1640,11 +1640,7 @@ mod tests {
|
||||
|
||||
let decoded = TransactionSigned::decode(&mut &tx_bytes[..]).unwrap();
|
||||
assert_eq!(tx_bytes.len(), decoded.length());
|
||||
|
||||
let mut encoded = BytesMut::new();
|
||||
decoded.encode(&mut encoded);
|
||||
|
||||
assert_eq!(tx_bytes, encoded[..]);
|
||||
assert_eq!(tx_bytes, &alloy_rlp::encode(decoded)[..]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -1827,10 +1823,7 @@ mod tests {
|
||||
|
||||
let decoded = TransactionSigned::decode(&mut &bytes[..]).unwrap();
|
||||
assert_eq!(expected, decoded);
|
||||
|
||||
let mut encoded = BytesMut::new();
|
||||
expected.encode(&mut encoded);
|
||||
assert_eq!(bytes, encoded);
|
||||
assert_eq!(bytes, &alloy_rlp::encode(expected));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -1961,10 +1954,8 @@ mod tests {
|
||||
signature,
|
||||
);
|
||||
|
||||
let mut encoded = BytesMut::new();
|
||||
signed_tx.encode(&mut encoded);
|
||||
|
||||
assert_eq!(hex!("c98080808080801b8080"), &encoded[..]);
|
||||
let encoded = &alloy_rlp::encode(signed_tx);
|
||||
assert_eq!(hex!("c98080808080801b8080"), encoded[..]);
|
||||
assert_eq!(MIN_LENGTH_LEGACY_TX_ENCODED, encoded.len());
|
||||
|
||||
TransactionSigned::decode(&mut &encoded[..]).unwrap();
|
||||
@ -1980,10 +1971,8 @@ mod tests {
|
||||
signature,
|
||||
);
|
||||
|
||||
let mut encoded = BytesMut::new();
|
||||
signed_tx.encode(&mut encoded);
|
||||
|
||||
assert_eq!(hex!("8d01cb80808080808080c0808080"), &encoded[..]);
|
||||
let encoded = &alloy_rlp::encode(signed_tx);
|
||||
assert_eq!(hex!("8d01cb80808080808080c0808080"), encoded[..]);
|
||||
assert_eq!(MIN_LENGTH_EIP2930_TX_ENCODED, encoded.len());
|
||||
|
||||
TransactionSigned::decode(&mut &encoded[..]).unwrap();
|
||||
@ -1999,10 +1988,8 @@ mod tests {
|
||||
signature,
|
||||
);
|
||||
|
||||
let mut encoded = BytesMut::new();
|
||||
signed_tx.encode(&mut encoded);
|
||||
|
||||
assert_eq!(hex!("8e02cc8080808080808080c0808080"), &encoded[..]);
|
||||
let encoded = &alloy_rlp::encode(signed_tx);
|
||||
assert_eq!(hex!("8e02cc8080808080808080c0808080"), encoded[..]);
|
||||
assert_eq!(MIN_LENGTH_EIP1559_TX_ENCODED, encoded.len());
|
||||
|
||||
TransactionSigned::decode(&mut &encoded[..]).unwrap();
|
||||
@ -2018,10 +2005,8 @@ mod tests {
|
||||
signature,
|
||||
);
|
||||
|
||||
let mut encoded = BytesMut::new();
|
||||
signed_tx.encode(&mut encoded);
|
||||
|
||||
assert_eq!(hex!("9003ce8080808080808080c080c0808080"), &encoded[..]);
|
||||
let encoded = alloy_rlp::encode(signed_tx);
|
||||
assert_eq!(hex!("9003ce8080808080808080c080c0808080"), encoded[..]);
|
||||
assert_eq!(MIN_LENGTH_EIP4844_TX_ENCODED, encoded.len());
|
||||
|
||||
TransactionSigned::decode(&mut &encoded[..]).unwrap();
|
||||
@ -2041,8 +2026,7 @@ mod tests {
|
||||
signature,
|
||||
);
|
||||
|
||||
let mut encoded = BytesMut::new();
|
||||
signed_tx.encode(&mut encoded);
|
||||
let encoded = &alloy_rlp::encode(signed_tx);
|
||||
|
||||
assert_eq!(b"\xb8?~\xf8<\xa0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x94\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x80\x80\x80\x80\x80\x80", &encoded[..]);
|
||||
assert_eq!(MIN_LENGTH_DEPOSIT_TX_ENCODED, encoded.len());
|
||||
|
||||
Reference in New Issue
Block a user