diff --git a/crates/stages/stages/src/stages/hashing_storage.rs b/crates/stages/stages/src/stages/hashing_storage.rs
index ba1e03c1a296..1862a3248ded 100644
--- a/crates/stages/stages/src/stages/hashing_storage.rs
+++ b/crates/stages/stages/src/stages/hashing_storage.rs
@@ -134,7 +134,7 @@ where
B256::from_slice(&addr_key[..32]),
StorageEntry {
key: B256::from_slice(&addr_key[32..]),
- value: CompactU256::decompress(value)?.into(),
+ value: CompactU256::decompress_owned(value)?.into(),
},
)?;
}
diff --git a/crates/stages/stages/src/stages/index_account_history.rs b/crates/stages/stages/src/stages/index_account_history.rs
index e0fcde2b194f..75648d69e773 100644
--- a/crates/stages/stages/src/stages/index_account_history.rs
+++ b/crates/stages/stages/src/stages/index_account_history.rs
@@ -118,7 +118,7 @@ where
collector,
first_sync,
ShardedKey::new,
- ShardedKey::
::decode,
+ ShardedKey::::decode_owned,
|key| key.key,
)?;
diff --git a/crates/stages/stages/src/stages/index_storage_history.rs b/crates/stages/stages/src/stages/index_storage_history.rs
index 4af2cb3efea2..c28867d9fc5c 100644
--- a/crates/stages/stages/src/stages/index_storage_history.rs
+++ b/crates/stages/stages/src/stages/index_storage_history.rs
@@ -124,7 +124,7 @@ where
|AddressStorageKey((address, storage_key)), highest_block_number| {
StorageShardedKey::new(address, storage_key, highest_block_number)
},
- StorageShardedKey::decode,
+ StorageShardedKey::decode_owned,
|key| AddressStorageKey((key.address, key.sharded_key.key)),
)?;
diff --git a/crates/storage/db-api/src/models/accounts.rs b/crates/storage/db-api/src/models/accounts.rs
index 338a3a06f600..94922632129b 100644
--- a/crates/storage/db-api/src/models/accounts.rs
+++ b/crates/storage/db-api/src/models/accounts.rs
@@ -64,11 +64,9 @@ impl Encode for BlockNumberAddress {
}
impl Decode for BlockNumberAddress {
- fn decode>(value: B) -> Result {
- let value = value.as_ref();
+ fn decode(value: &[u8]) -> Result {
let num = u64::from_be_bytes(value[..8].try_into().map_err(|_| DatabaseError::Decode)?);
let hash = Address::from_slice(&value[8..]);
-
Ok(Self((num, hash)))
}
}
@@ -97,11 +95,9 @@ impl Encode for AddressStorageKey {
}
impl Decode for AddressStorageKey {
- fn decode>(value: B) -> Result {
- let value = value.as_ref();
+ fn decode(value: &[u8]) -> Result {
let address = Address::from_slice(&value[..20]);
let storage_key = StorageKey::from_slice(&value[20..]);
-
Ok(Self((address, storage_key)))
}
}
@@ -127,7 +123,7 @@ mod tests {
let encoded = Encode::encode(key);
assert_eq!(encoded, bytes);
- let decoded: BlockNumberAddress = Decode::decode(encoded).unwrap();
+ let decoded: BlockNumberAddress = Decode::decode(&encoded).unwrap();
assert_eq!(decoded, key);
}
@@ -152,7 +148,7 @@ mod tests {
let encoded = Encode::encode(key);
assert_eq!(encoded, bytes);
- let decoded: AddressStorageKey = Decode::decode(encoded).unwrap();
+ let decoded: AddressStorageKey = Decode::decode(&encoded).unwrap();
assert_eq!(decoded, key);
}
diff --git a/crates/storage/db-api/src/models/blocks.rs b/crates/storage/db-api/src/models/blocks.rs
index b48baf6d6b26..7268d82dd3cc 100644
--- a/crates/storage/db-api/src/models/blocks.rs
+++ b/crates/storage/db-api/src/models/blocks.rs
@@ -29,9 +29,6 @@ mod tests {
let mut ommer = StoredBlockOmmers::default();
ommer.ommers.push(Header::default());
ommer.ommers.push(Header::default());
- assert_eq!(
- ommer.clone(),
- StoredBlockOmmers::decompress::>(ommer.compress()).unwrap()
- );
+ assert_eq!(ommer.clone(), StoredBlockOmmers::decompress(&ommer.compress()).unwrap());
}
}
diff --git a/crates/storage/db-api/src/models/integer_list.rs b/crates/storage/db-api/src/models/integer_list.rs
index f47605bf88b5..17c2009bbe3b 100644
--- a/crates/storage/db-api/src/models/integer_list.rs
+++ b/crates/storage/db-api/src/models/integer_list.rs
@@ -18,7 +18,7 @@ impl Compress for IntegerList {
}
impl Decompress for IntegerList {
- fn decompress>(value: B) -> Result {
- Self::from_bytes(value.as_ref()).map_err(|_| DatabaseError::Decode)
+ fn decompress(value: &[u8]) -> Result {
+ Self::from_bytes(value).map_err(|_| DatabaseError::Decode)
}
}
diff --git a/crates/storage/db-api/src/models/mod.rs b/crates/storage/db-api/src/models/mod.rs
index 6e832a0314f4..9e7e8957b5a9 100644
--- a/crates/storage/db-api/src/models/mod.rs
+++ b/crates/storage/db-api/src/models/mod.rs
@@ -42,10 +42,10 @@ macro_rules! impl_uints {
}
impl Decode for $name {
- fn decode>(value: B) -> Result {
+ fn decode(value: &[u8]) -> Result {
Ok(
$name::from_be_bytes(
- value.as_ref().try_into().map_err(|_| $crate::DatabaseError::Decode)?
+ value.try_into().map_err(|_| $crate::DatabaseError::Decode)?
)
)
}
@@ -65,8 +65,12 @@ impl Encode for Vec {
}
impl Decode for Vec {
- fn decode>(value: B) -> Result {
- Ok(value.as_ref().to_vec())
+ fn decode(value: &[u8]) -> Result {
+ Ok(value.to_vec())
+ }
+
+ fn decode_owned(value: Vec) -> Result {
+ Ok(value)
}
}
@@ -79,8 +83,8 @@ impl Encode for Address {
}
impl Decode for Address {
- fn decode>(value: B) -> Result {
- Ok(Self::from_slice(value.as_ref()))
+ fn decode(value: &[u8]) -> Result {
+ Ok(Self::from_slice(value))
}
}
@@ -93,8 +97,8 @@ impl Encode for B256 {
}
impl Decode for B256 {
- fn decode>(value: B) -> Result {
- Ok(Self::new(value.as_ref().try_into().map_err(|_| DatabaseError::Decode)?))
+ fn decode(value: &[u8]) -> Result {
+ Ok(Self::new(value.try_into().map_err(|_| DatabaseError::Decode)?))
}
}
@@ -107,8 +111,12 @@ impl Encode for String {
}
impl Decode for String {
- fn decode>(value: B) -> Result {
- Self::from_utf8(value.as_ref().to_vec()).map_err(|_| DatabaseError::Decode)
+ fn decode(value: &[u8]) -> Result {
+ Self::decode_owned(value.to_vec())
+ }
+
+ fn decode_owned(value: Vec) -> Result {
+ Self::from_utf8(value).map_err(|_| DatabaseError::Decode)
}
}
@@ -124,9 +132,8 @@ impl Encode for StoredNibbles {
}
impl Decode for StoredNibbles {
- fn decode>(value: B) -> Result {
- let buf = value.as_ref();
- Ok(Self::from_compact(buf, buf.len()).0)
+ fn decode(value: &[u8]) -> Result {
+ Ok(Self::from_compact(value, value.len()).0)
}
}
@@ -142,9 +149,8 @@ impl Encode for StoredNibblesSubKey {
}
impl Decode for StoredNibblesSubKey {
- fn decode>(value: B) -> Result {
- let buf = value.as_ref();
- Ok(Self::from_compact(buf, buf.len()).0)
+ fn decode(value: &[u8]) -> Result {
+ Ok(Self::from_compact(value, value.len()).0)
}
}
@@ -159,9 +165,8 @@ impl Encode for PruneSegment {
}
impl Decode for PruneSegment {
- fn decode>(value: B) -> Result {
- let buf = value.as_ref();
- Ok(Self::from_compact(buf, buf.len()).0)
+ fn decode(value: &[u8]) -> Result {
+ Ok(Self::from_compact(value, value.len()).0)
}
}
@@ -177,9 +182,8 @@ impl Encode for ClientVersion {
}
impl Decode for ClientVersion {
- fn decode>(value: B) -> Result {
- let buf = value.as_ref();
- Ok(Self::from_compact(buf, buf.len()).0)
+ fn decode(value: &[u8]) -> Result {
+ Ok(Self::from_compact(value, value.len()).0)
}
}
@@ -196,9 +200,8 @@ macro_rules! impl_compression_for_compact {
}
impl Decompress for $name {
- fn decompress>(value: B) -> Result<$name, $crate::DatabaseError> {
- let value = value.as_ref();
- let (obj, _) = Compact::from_compact(&value, value.len());
+ fn decompress(value: &[u8]) -> Result<$name, $crate::DatabaseError> {
+ let (obj, _) = Compact::from_compact(value, value.len());
Ok(obj)
}
}
@@ -236,23 +239,20 @@ impl_compression_for_compact!(
macro_rules! impl_compression_fixed_compact {
($($name:tt),+) => {
$(
- impl Compress for $name
- {
+ impl Compress for $name {
type Compressed = Vec;
- fn compress_to_buf>(self, buf: &mut B) {
- let _ = Compact::to_compact(&self, buf);
- }
-
fn uncompressable_ref(&self) -> Option<&[u8]> {
Some(self.as_ref())
}
+
+ fn compress_to_buf>(self, buf: &mut B) {
+ let _ = Compact::to_compact(&self, buf);
+ }
}
- impl Decompress for $name
- {
- fn decompress>(value: B) -> Result<$name, $crate::DatabaseError> {
- let value = value.as_ref();
+ impl Decompress for $name {
+ fn decompress(value: &[u8]) -> Result<$name, $crate::DatabaseError> {
let (obj, _) = Compact::from_compact(&value, value.len());
Ok(obj)
}
diff --git a/crates/storage/db-api/src/models/sharded_key.rs b/crates/storage/db-api/src/models/sharded_key.rs
index dd8702a4812b..d1de1bd400c4 100644
--- a/crates/storage/db-api/src/models/sharded_key.rs
+++ b/crates/storage/db-api/src/models/sharded_key.rs
@@ -16,7 +16,7 @@ pub const NUM_OF_INDICES_IN_SHARD: usize = 2_000;
/// `Address | 200` -> data is from block 0 to 200.
///
/// `Address | 300` -> data is from block 201 to 300.
-#[derive(Debug, Default, Clone, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize)]
+#[derive(Debug, Default, Clone, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize, Hash)]
pub struct ShardedKey {
/// The key for this type.
pub key: T,
@@ -43,11 +43,7 @@ impl ShardedKey {
}
}
-impl Encode for ShardedKey
-where
- T: Encode,
- Vec: From<::Encoded>,
-{
+impl Encode for ShardedKey {
type Encoded = Vec;
fn encode(self) -> Self::Encoded {
@@ -57,30 +53,11 @@ where
}
}
-impl Decode for ShardedKey
-where
- T: Decode,
-{
- fn decode>(value: B) -> Result {
- let value = value.as_ref();
-
- let tx_num_index = value.len() - 8;
-
- let highest_tx_number = u64::from_be_bytes(
- value[tx_num_index..].try_into().map_err(|_| DatabaseError::Decode)?,
- );
- let key = T::decode(&value[..tx_num_index])?;
-
+impl Decode for ShardedKey {
+ fn decode(value: &[u8]) -> Result {
+ let (key, highest_tx_number) = value.split_last_chunk().unwrap();
+ let key = T::decode(key)?;
+ let highest_tx_number = u64::from_be_bytes(*highest_tx_number);
Ok(Self::new(key, highest_tx_number))
}
}
-
-impl Hash for ShardedKey
-where
- T: Hash,
-{
- fn hash(&self, state: &mut H) {
- self.key.hash(state);
- self.highest_block_number.hash(state);
- }
-}
diff --git a/crates/storage/db-api/src/models/storage_sharded_key.rs b/crates/storage/db-api/src/models/storage_sharded_key.rs
index b6538256e629..5fd79ba655c1 100644
--- a/crates/storage/db-api/src/models/storage_sharded_key.rs
+++ b/crates/storage/db-api/src/models/storage_sharded_key.rs
@@ -61,8 +61,7 @@ impl Encode for StorageShardedKey {
}
impl Decode for StorageShardedKey {
- fn decode>(value: B) -> Result {
- let value = value.as_ref();
+ fn decode(value: &[u8]) -> Result {
let tx_num_index = value.len() - 8;
let highest_tx_number = u64::from_be_bytes(
diff --git a/crates/storage/db-api/src/scale.rs b/crates/storage/db-api/src/scale.rs
index 99382a4a9179..c432f32e6df2 100644
--- a/crates/storage/db-api/src/scale.rs
+++ b/crates/storage/db-api/src/scale.rs
@@ -30,8 +30,8 @@ impl Decompress for T
where
T: ScaleValue + parity_scale_codec::Decode + Sync + Send + std::fmt::Debug,
{
- fn decompress>(value: B) -> Result {
- parity_scale_codec::Decode::decode(&mut value.as_ref()).map_err(|_| DatabaseError::Decode)
+ fn decompress(mut value: &[u8]) -> Result {
+ parity_scale_codec::Decode::decode(&mut value).map_err(|_| DatabaseError::Decode)
}
}
diff --git a/crates/storage/db-api/src/table.rs b/crates/storage/db-api/src/table.rs
index 6d3f52198d28..963457af05c3 100644
--- a/crates/storage/db-api/src/table.rs
+++ b/crates/storage/db-api/src/table.rs
@@ -38,11 +38,11 @@ pub trait Compress: Send + Sync + Sized + Debug {
/// Trait that will transform the data to be read from the DB.
pub trait Decompress: Send + Sync + Sized + Debug {
/// Decompresses data coming from the database.
- fn decompress>(value: B) -> Result;
+ fn decompress(value: &[u8]) -> Result;
/// Decompresses owned data coming from the database.
fn decompress_owned(value: Vec) -> Result {
- Self::decompress(value)
+ Self::decompress(&value)
}
}
@@ -58,7 +58,12 @@ pub trait Encode: Send + Sync + Sized + Debug {
/// Trait that will transform the data to be read from the DB.
pub trait Decode: Send + Sync + Sized + Debug {
/// Decodes data coming from the database.
- fn decode>(value: B) -> Result;
+ fn decode(value: &[u8]) -> Result;
+
+ /// Decodes owned data coming from the database.
+ fn decode_owned(value: Vec) -> Result {
+ Self::decode(&value)
+ }
}
/// Generic trait that enforces the database key to implement [`Encode`] and [`Decode`].
diff --git a/crates/storage/db-api/src/utils.rs b/crates/storage/db-api/src/utils.rs
index b9ee6277e959..65ed5b6c01d4 100644
--- a/crates/storage/db-api/src/utils.rs
+++ b/crates/storage/db-api/src/utils.rs
@@ -10,8 +10,7 @@ macro_rules! impl_fixed_arbitrary {
fn arbitrary(u: &mut Unstructured<'a>) -> Result {
let mut buffer = vec![0; $size];
u.fill_buffer(buffer.as_mut_slice())?;
-
- Decode::decode(buffer).map_err(|_| arbitrary::Error::IncorrectFormat)
+ Decode::decode_owned(buffer).map_err(|_| arbitrary::Error::IncorrectFormat)
}
}
@@ -26,7 +25,7 @@ macro_rules! impl_fixed_arbitrary {
fn arbitrary_with(args: Self::Parameters) -> Self::Strategy {
use proptest::strategy::Strategy;
proptest::collection::vec(proptest::arbitrary::any_with::(args), $size)
- .prop_map(move |vec| Decode::decode(vec).unwrap())
+ .prop_map(move |vec| Decode::decode_owned(vec).unwrap())
}
}
)+
diff --git a/crates/storage/db/benches/criterion.rs b/crates/storage/db/benches/criterion.rs
index 6d273a8ce93c..7ac9566d80c5 100644
--- a/crates/storage/db/benches/criterion.rs
+++ b/crates/storage/db/benches/criterion.rs
@@ -87,7 +87,7 @@ where
|input| {
{
for (_, k, _, _) in input {
- let _ = ::Key::decode(k);
+ let _ = ::Key::decode(&k);
}
};
black_box(());
@@ -115,7 +115,7 @@ where
|input| {
{
for (_, _, _, v) in input {
- let _ = ::Value::decompress(v);
+ let _ = ::Value::decompress(&v);
}
};
black_box(());
diff --git a/crates/storage/db/benches/iai.rs b/crates/storage/db/benches/iai.rs
index ebcf6c8a42c0..167cd0860e26 100644
--- a/crates/storage/db/benches/iai.rs
+++ b/crates/storage/db/benches/iai.rs
@@ -25,7 +25,7 @@ macro_rules! impl_iai_callgrind_inner {
#[library_benchmark]
pub fn $decompress() {
for (_, _, _, comp) in black_box(load_vectors::()) {
- let _ = black_box(::Value::decompress(comp));
+ let _ = black_box(::Value::decompress(&comp));
}
}
@@ -39,7 +39,7 @@ macro_rules! impl_iai_callgrind_inner {
#[library_benchmark]
pub fn $decode() {
for (_, enc, _, _) in black_box(load_vectors::()) {
- let _ = black_box(::Key::decode(enc));
+ let _ = black_box(::Key::decode(&enc));
}
}
diff --git a/crates/storage/db/benches/utils.rs b/crates/storage/db/benches/utils.rs
index 72d121aa75cc..9700ef94b241 100644
--- a/crates/storage/db/benches/utils.rs
+++ b/crates/storage/db/benches/utils.rs
@@ -1,7 +1,5 @@
-#![cfg(feature = "test-utils")]
#![allow(missing_docs)]
-
-use std::{path::Path, sync::Arc};
+#![cfg(feature = "test-utils")]
use alloy_primitives::Bytes;
use reth_db::{test_utils::create_test_rw_db_with_path, DatabaseEnv};
@@ -11,6 +9,7 @@ use reth_db_api::{
Database,
};
use reth_fs_util as fs;
+use std::{path::Path, sync::Arc};
/// Path where the DB is initialized for benchmarks.
#[allow(dead_code)]
diff --git a/crates/storage/db/src/implementation/mdbx/cursor.rs b/crates/storage/db/src/implementation/mdbx/cursor.rs
index c908bad45942..756a622bcb03 100644
--- a/crates/storage/db/src/implementation/mdbx/cursor.rs
+++ b/crates/storage/db/src/implementation/mdbx/cursor.rs
@@ -81,7 +81,7 @@ macro_rules! compress_to_buf_or_ref {
if let Some(value) = $value.uncompressable_ref() {
Some(value)
} else {
- $self.buf.truncate(0);
+ $self.buf.clear();
$value.compress_to_buf(&mut $self.buf);
None
}
diff --git a/crates/storage/db/src/tables/codecs/fuzz/mod.rs b/crates/storage/db/src/tables/codecs/fuzz/mod.rs
index 846ed17e1f1a..e64a3841df49 100644
--- a/crates/storage/db/src/tables/codecs/fuzz/mod.rs
+++ b/crates/storage/db/src/tables/codecs/fuzz/mod.rs
@@ -30,13 +30,12 @@ macro_rules! impl_fuzzer_with_input {
/// Encodes and decodes table types returning its encoded size and the decoded object.
/// This method is used for benchmarking, so its parameter should be the actual type that is being tested.
- pub fn encode_and_decode(obj: $name) -> (usize, $name)
- {
+ pub fn encode_and_decode(obj: $name) -> (usize, $name) {
let data = table::$encode::$encode_method(obj);
let size = data.len();
// Some `data` might be a fixed array.
- (size, table::$decode::$decode_method(data.to_vec()).expect("failed to decode"))
+ (size, table::$decode::$decode_method(&data).expect("failed to decode"))
}
#[cfg(test)]
diff --git a/crates/storage/db/src/tables/mod.rs b/crates/storage/db/src/tables/mod.rs
index 835d1486dafe..384139618163 100644
--- a/crates/storage/db/src/tables/mod.rs
+++ b/crates/storage/db/src/tables/mod.rs
@@ -429,8 +429,8 @@ impl Encode for ChainStateKey {
}
impl Decode for ChainStateKey {
- fn decode>(value: B) -> Result {
- if value.as_ref() == [0] {
+ fn decode(value: &[u8]) -> Result {
+ if value == [0] {
Ok(Self::LastFinalizedBlock)
} else {
Err(reth_db_api::DatabaseError::Decode)
diff --git a/crates/storage/db/src/tables/raw.rs b/crates/storage/db/src/tables/raw.rs
index 1e8fa56b3603..6b6de41613eb 100644
--- a/crates/storage/db/src/tables/raw.rs
+++ b/crates/storage/db/src/tables/raw.rs
@@ -96,8 +96,12 @@ impl Encode for RawKey {
// Decode
impl Decode for RawKey {
- fn decode>(key: B) -> Result {
- Ok(Self { key: key.as_ref().to_vec(), _phantom: std::marker::PhantomData })
+ fn decode(value: &[u8]) -> Result {
+ Ok(Self { key: value.to_vec(), _phantom: std::marker::PhantomData })
+ }
+
+ fn decode_owned(value: Vec) -> Result {
+ Ok(Self { key: value, _phantom: std::marker::PhantomData })
}
}
@@ -168,8 +172,8 @@ impl Compress for RawValue {
}
impl Decompress for RawValue {
- fn decompress>(value: B) -> Result {
- Ok(Self { value: value.as_ref().to_vec(), _phantom: std::marker::PhantomData })
+ fn decompress(value: &[u8]) -> Result {
+ Ok(Self { value: value.to_vec(), _phantom: std::marker::PhantomData })
}
fn decompress_owned(value: Vec) -> Result {
diff --git a/crates/storage/db/src/tables/utils.rs b/crates/storage/db/src/tables/utils.rs
index 616d1038264e..0948ee108f68 100644
--- a/crates/storage/db/src/tables/utils.rs
+++ b/crates/storage/db/src/tables/utils.rs
@@ -6,7 +6,7 @@ use std::borrow::Cow;
/// Helper function to decode a `(key, value)` pair.
pub(crate) fn decoder<'a, T>(
- kv: (Cow<'a, [u8]>, Cow<'a, [u8]>),
+ (k, v): (Cow<'a, [u8]>, Cow<'a, [u8]>),
) -> Result, DatabaseError>
where
T: Table,
@@ -14,11 +14,11 @@ where
T::Value: Decompress,
{
Ok((
- match kv.0 {
+ match k {
Cow::Borrowed(k) => Decode::decode(k)?,
- Cow::Owned(k) => Decode::decode(k)?,
+ Cow::Owned(k) => Decode::decode_owned(k)?,
},
- match kv.1 {
+ match v {
Cow::Borrowed(v) => Decompress::decompress(v)?,
Cow::Owned(v) => Decompress::decompress_owned(v)?,
},