From 9f00d48402e00192e1b27a7afcd1574177985a01 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Thu, 16 Mar 2023 15:22:08 +0100 Subject: [PATCH] chore: rearrange impl order (#1796) --- crates/net/eth-wire/src/types/broadcast.rs | 32 +++++++-------- crates/primitives/src/chain/mod.rs | 12 +++--- crates/primitives/src/genesis.rs | 12 +++--- crates/primitives/src/receipt.rs | 6 +-- crates/rlp/src/encode.rs | 40 +++++++++---------- .../rpc-types/src/eth/transaction/typed.rs | 12 +++--- 6 files changed, 57 insertions(+), 57 deletions(-) diff --git a/crates/net/eth-wire/src/types/broadcast.rs b/crates/net/eth-wire/src/types/broadcast.rs index b7de075db..c31ce7244 100644 --- a/crates/net/eth-wire/src/types/broadcast.rs +++ b/crates/net/eth-wire/src/types/broadcast.rs @@ -259,22 +259,6 @@ pub struct NewPooledTransactionHashes68 { } impl Encodable for NewPooledTransactionHashes68 { - fn length(&self) -> usize { - #[derive(RlpEncodable)] - struct EncodableNewPooledTransactionHashes68<'a> { - types: &'a [u8], - sizes: &'a Vec, - hashes: &'a Vec, - } - - let encodable = EncodableNewPooledTransactionHashes68 { - types: &self.types[..], - sizes: &self.sizes, - hashes: &self.hashes, - }; - - encodable.length() - } fn encode(&self, out: &mut dyn bytes::BufMut) { #[derive(RlpEncodable)] struct EncodableNewPooledTransactionHashes68<'a> { @@ -291,6 +275,22 @@ impl Encodable for NewPooledTransactionHashes68 { encodable.encode(out); } + fn length(&self) -> usize { + #[derive(RlpEncodable)] + struct EncodableNewPooledTransactionHashes68<'a> { + types: &'a [u8], + sizes: &'a Vec, + hashes: &'a Vec, + } + + let encodable = EncodableNewPooledTransactionHashes68 { + types: &self.types[..], + sizes: &self.sizes, + hashes: &self.hashes, + }; + + encodable.length() + } } impl Decodable for NewPooledTransactionHashes68 { diff --git a/crates/primitives/src/chain/mod.rs b/crates/primitives/src/chain/mod.rs index d03aff38a..c5ef094f4 100644 --- a/crates/primitives/src/chain/mod.rs +++ b/crates/primitives/src/chain/mod.rs @@ -154,18 +154,18 @@ impl FromStr for Chain { } impl Encodable for Chain { - fn length(&self) -> usize { - match self { - Self::Named(chain) => u64::from(*chain).length(), - Self::Id(id) => id.length(), - } - } fn encode(&self, out: &mut dyn reth_rlp::BufMut) { match self { Self::Named(chain) => u64::from(*chain).encode(out), Self::Id(id) => id.encode(out), } } + fn length(&self) -> usize { + match self { + Self::Named(chain) => u64::from(*chain).length(), + Self::Id(id) => id.length(), + } + } } impl Decodable for Chain { diff --git a/crates/primitives/src/genesis.rs b/crates/primitives/src/genesis.rs index fde9db63f..c9a3f9abc 100644 --- a/crates/primitives/src/genesis.rs +++ b/crates/primitives/src/genesis.rs @@ -146,12 +146,6 @@ impl GenesisAccount { } impl Encodable for GenesisAccount { - fn length(&self) -> usize { - let len = self.payload_len(); - // RLP header length + payload length - len + length_of_length(len) - } - fn encode(&self, out: &mut dyn bytes::BufMut) { let header = RlpHeader { list: true, payload_length: self.payload_len() }; header.encode(out); @@ -174,6 +168,12 @@ impl Encodable for GenesisAccount { .encode(out); self.code.as_ref().map_or(KECCAK_EMPTY, keccak256).encode(out); } + + fn length(&self) -> usize { + let len = self.payload_len(); + // RLP header length + payload length + len + length_of_length(len) + } } impl From for GenesisAccount { diff --git a/crates/primitives/src/receipt.rs b/crates/primitives/src/receipt.rs index 80761da98..ae4303856 100644 --- a/crates/primitives/src/receipt.rs +++ b/crates/primitives/src/receipt.rs @@ -106,6 +106,9 @@ impl Receipt { } impl Encodable for Receipt { + fn encode(&self, out: &mut dyn BufMut) { + self.encode_inner(out, true) + } fn length(&self) -> usize { let mut payload_len = self.receipt_length(); // account for eip-2718 type prefix and set the list @@ -117,9 +120,6 @@ impl Encodable for Receipt { payload_len } - fn encode(&self, out: &mut dyn BufMut) { - self.encode_inner(out, true) - } } impl Decodable for Receipt { diff --git a/crates/rlp/src/encode.rs b/crates/rlp/src/encode.rs index 98c00d61e..0350a76b0 100644 --- a/crates/rlp/src/encode.rs +++ b/crates/rlp/src/encode.rs @@ -79,6 +79,13 @@ pub trait Encodable { } impl<'a> Encodable for &'a [u8] { + fn encode(&self, out: &mut dyn BufMut) { + if self.len() != 1 || self[0] >= EMPTY_STRING_CODE { + Header { list: false, payload_length: self.len() }.encode(out); + } + out.put_slice(self); + } + fn length(&self) -> usize { let mut len = self.len(); if self.len() != 1 || self[0] >= EMPTY_STRING_CODE { @@ -86,23 +93,16 @@ impl<'a> Encodable for &'a [u8] { } len } - - fn encode(&self, out: &mut dyn BufMut) { - if self.len() != 1 || self[0] >= EMPTY_STRING_CODE { - Header { list: false, payload_length: self.len() }.encode(out); - } - out.put_slice(self); - } } impl Encodable for [u8; LEN] { - fn length(&self) -> usize { - (self as &[u8]).length() - } - fn encode(&self, out: &mut dyn BufMut) { (self as &[u8]).encode(out) } + + fn length(&self) -> usize { + (self as &[u8]).length() + } } unsafe impl MaxEncodedLenAssoc for [u8; LEN] { @@ -164,13 +164,13 @@ encodable_uint!(u128); max_encoded_len_uint!(u128); impl Encodable for bool { - fn length(&self) -> usize { - (*self as u8).length() - } - fn encode(&self, out: &mut dyn BufMut) { (*self as u8).encode(out) } + + fn length(&self) -> usize { + (*self as u8).length() + } } impl_max_encoded_len!(bool, { ::LEN }); @@ -340,13 +340,13 @@ mod alloc_support { where T: Encodable, { - fn length(&self) -> usize { - list_length(self) - } - fn encode(&self, out: &mut dyn BufMut) { encode_list(self, out) } + + fn length(&self) -> usize { + list_length(self) + } } impl Encodable for ::alloc::string::String { @@ -583,7 +583,7 @@ mod tests { "0100020003000400050006000700080009000A0B4B000C000D000E010100020003000400050006000700080009000A0B4B000C000D000E01", 16, ) - .unwrap(), + .unwrap(), &hex!("b8380100020003000400050006000700080009000A0B4B000C000D000E010100020003000400050006000700080009000A0B4B000C000D000E01")[..], )]) } diff --git a/crates/rpc/rpc-types/src/eth/transaction/typed.rs b/crates/rpc/rpc-types/src/eth/transaction/typed.rs index bcca7a0b7..f91903c69 100644 --- a/crates/rpc/rpc-types/src/eth/transaction/typed.rs +++ b/crates/rpc/rpc-types/src/eth/transaction/typed.rs @@ -83,18 +83,18 @@ impl TransactionKind { } impl Encodable for TransactionKind { - fn length(&self) -> usize { - match self { - TransactionKind::Call(to) => to.length(), - TransactionKind::Create => ([]).length(), - } - } fn encode(&self, out: &mut dyn BufMut) { match self { TransactionKind::Call(to) => to.encode(out), TransactionKind::Create => ([]).encode(out), } } + fn length(&self) -> usize { + match self { + TransactionKind::Call(to) => to.length(), + TransactionKind::Create => ([]).length(), + } + } } impl Decodable for TransactionKind {