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 generics from Decode and Decompress #11295

Merged
merged 2 commits into from
Sep 27, 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
2 changes: 1 addition & 1 deletion crates/stages/stages/src/stages/hashing_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
},
)?;
}
Expand Down
2 changes: 1 addition & 1 deletion crates/stages/stages/src/stages/index_account_history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ where
collector,
first_sync,
ShardedKey::new,
ShardedKey::<Address>::decode,
ShardedKey::<Address>::decode_owned,
|key| key.key,
)?;

Expand Down
2 changes: 1 addition & 1 deletion crates/stages/stages/src/stages/index_storage_history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
)?;

Expand Down
12 changes: 4 additions & 8 deletions crates/storage/db-api/src/models/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,9 @@ impl Encode for BlockNumberAddress {
}

impl Decode for BlockNumberAddress {
fn decode<B: AsRef<[u8]>>(value: B) -> Result<Self, DatabaseError> {
let value = value.as_ref();
fn decode(value: &[u8]) -> Result<Self, DatabaseError> {
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)))
}
}
Expand Down Expand Up @@ -97,11 +95,9 @@ impl Encode for AddressStorageKey {
}

impl Decode for AddressStorageKey {
fn decode<B: AsRef<[u8]>>(value: B) -> Result<Self, DatabaseError> {
let value = value.as_ref();
fn decode(value: &[u8]) -> Result<Self, DatabaseError> {
let address = Address::from_slice(&value[..20]);
let storage_key = StorageKey::from_slice(&value[20..]);

Ok(Self((address, storage_key)))
}
}
Expand All @@ -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);
}

Expand All @@ -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);
}

Expand Down
5 changes: 1 addition & 4 deletions crates/storage/db-api/src/models/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>(ommer.compress()).unwrap()
);
assert_eq!(ommer.clone(), StoredBlockOmmers::decompress(&ommer.compress()).unwrap());
}
}
4 changes: 2 additions & 2 deletions crates/storage/db-api/src/models/integer_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl Compress for IntegerList {
}

impl Decompress for IntegerList {
fn decompress<B: AsRef<[u8]>>(value: B) -> Result<Self, DatabaseError> {
Self::from_bytes(value.as_ref()).map_err(|_| DatabaseError::Decode)
fn decompress(value: &[u8]) -> Result<Self, DatabaseError> {
Self::from_bytes(value).map_err(|_| DatabaseError::Decode)
}
}
70 changes: 35 additions & 35 deletions crates/storage/db-api/src/models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ macro_rules! impl_uints {
}

impl Decode for $name {
fn decode<B: AsRef<[u8]>>(value: B) -> Result<Self, $crate::DatabaseError> {
fn decode(value: &[u8]) -> Result<Self, $crate::DatabaseError> {
Ok(
$name::from_be_bytes(
value.as_ref().try_into().map_err(|_| $crate::DatabaseError::Decode)?
value.try_into().map_err(|_| $crate::DatabaseError::Decode)?
)
)
}
Expand All @@ -65,8 +65,12 @@ impl Encode for Vec<u8> {
}

impl Decode for Vec<u8> {
fn decode<B: AsRef<[u8]>>(value: B) -> Result<Self, DatabaseError> {
Ok(value.as_ref().to_vec())
fn decode(value: &[u8]) -> Result<Self, DatabaseError> {
Ok(value.to_vec())
}

fn decode_owned(value: Vec<u8>) -> Result<Self, DatabaseError> {
Ok(value)
}
}

Expand All @@ -79,8 +83,8 @@ impl Encode for Address {
}

impl Decode for Address {
fn decode<B: AsRef<[u8]>>(value: B) -> Result<Self, DatabaseError> {
Ok(Self::from_slice(value.as_ref()))
fn decode(value: &[u8]) -> Result<Self, DatabaseError> {
Ok(Self::from_slice(value))
}
}

Expand All @@ -93,8 +97,8 @@ impl Encode for B256 {
}

impl Decode for B256 {
fn decode<B: AsRef<[u8]>>(value: B) -> Result<Self, DatabaseError> {
Ok(Self::new(value.as_ref().try_into().map_err(|_| DatabaseError::Decode)?))
fn decode(value: &[u8]) -> Result<Self, DatabaseError> {
Ok(Self::new(value.try_into().map_err(|_| DatabaseError::Decode)?))
}
}

Expand All @@ -107,8 +111,12 @@ impl Encode for String {
}

impl Decode for String {
fn decode<B: AsRef<[u8]>>(value: B) -> Result<Self, DatabaseError> {
Self::from_utf8(value.as_ref().to_vec()).map_err(|_| DatabaseError::Decode)
fn decode(value: &[u8]) -> Result<Self, DatabaseError> {
Self::decode_owned(value.to_vec())
}

fn decode_owned(value: Vec<u8>) -> Result<Self, DatabaseError> {
Self::from_utf8(value).map_err(|_| DatabaseError::Decode)
}
}

Expand All @@ -124,9 +132,8 @@ impl Encode for StoredNibbles {
}

impl Decode for StoredNibbles {
fn decode<B: AsRef<[u8]>>(value: B) -> Result<Self, DatabaseError> {
let buf = value.as_ref();
Ok(Self::from_compact(buf, buf.len()).0)
fn decode(value: &[u8]) -> Result<Self, DatabaseError> {
Ok(Self::from_compact(value, value.len()).0)
}
}

Expand All @@ -142,9 +149,8 @@ impl Encode for StoredNibblesSubKey {
}

impl Decode for StoredNibblesSubKey {
fn decode<B: AsRef<[u8]>>(value: B) -> Result<Self, DatabaseError> {
let buf = value.as_ref();
Ok(Self::from_compact(buf, buf.len()).0)
fn decode(value: &[u8]) -> Result<Self, DatabaseError> {
Ok(Self::from_compact(value, value.len()).0)
}
}

Expand All @@ -159,9 +165,8 @@ impl Encode for PruneSegment {
}

impl Decode for PruneSegment {
fn decode<B: AsRef<[u8]>>(value: B) -> Result<Self, DatabaseError> {
let buf = value.as_ref();
Ok(Self::from_compact(buf, buf.len()).0)
fn decode(value: &[u8]) -> Result<Self, DatabaseError> {
Ok(Self::from_compact(value, value.len()).0)
}
}

Expand All @@ -177,9 +182,8 @@ impl Encode for ClientVersion {
}

impl Decode for ClientVersion {
fn decode<B: AsRef<[u8]>>(value: B) -> Result<Self, DatabaseError> {
let buf = value.as_ref();
Ok(Self::from_compact(buf, buf.len()).0)
fn decode(value: &[u8]) -> Result<Self, DatabaseError> {
Ok(Self::from_compact(value, value.len()).0)
}
}

Expand All @@ -196,9 +200,8 @@ macro_rules! impl_compression_for_compact {
}

impl Decompress for $name {
fn decompress<B: AsRef<[u8]>>(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)
}
}
Expand Down Expand Up @@ -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<u8>;

fn compress_to_buf<B: bytes::BufMut + AsMut<[u8]>>(self, buf: &mut B) {
let _ = Compact::to_compact(&self, buf);
}

fn uncompressable_ref(&self) -> Option<&[u8]> {
Some(self.as_ref())
}

fn compress_to_buf<B: bytes::BufMut + AsMut<[u8]>>(self, buf: &mut B) {
let _ = Compact::to_compact(&self, buf);
}
}

impl Decompress for $name
{
fn decompress<B: AsRef<[u8]>>(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)
}
Expand Down
37 changes: 7 additions & 30 deletions crates/storage/db-api/src/models/sharded_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> {
/// The key for this type.
pub key: T,
Expand All @@ -43,11 +43,7 @@ impl<T> ShardedKey<T> {
}
}

impl<T> Encode for ShardedKey<T>
where
T: Encode,
Vec<u8>: From<<T as Encode>::Encoded>,
{
impl<T: Encode> Encode for ShardedKey<T> {
type Encoded = Vec<u8>;

fn encode(self) -> Self::Encoded {
Expand All @@ -57,30 +53,11 @@ where
}
}

impl<T> Decode for ShardedKey<T>
where
T: Decode,
{
fn decode<B: AsRef<[u8]>>(value: B) -> Result<Self, DatabaseError> {
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<T: Decode> Decode for ShardedKey<T> {
fn decode(value: &[u8]) -> Result<Self, DatabaseError> {
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);
DaniPopes marked this conversation as resolved.
Show resolved Hide resolved
Ok(Self::new(key, highest_tx_number))
}
}

impl<T> Hash for ShardedKey<T>
where
T: Hash,
{
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.key.hash(state);
self.highest_block_number.hash(state);
}
}
3 changes: 1 addition & 2 deletions crates/storage/db-api/src/models/storage_sharded_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ impl Encode for StorageShardedKey {
}

impl Decode for StorageShardedKey {
fn decode<B: AsRef<[u8]>>(value: B) -> Result<Self, DatabaseError> {
let value = value.as_ref();
fn decode(value: &[u8]) -> Result<Self, DatabaseError> {
let tx_num_index = value.len() - 8;

let highest_tx_number = u64::from_be_bytes(
Expand Down
4 changes: 2 additions & 2 deletions crates/storage/db-api/src/scale.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ impl<T> Decompress for T
where
T: ScaleValue + parity_scale_codec::Decode + Sync + Send + std::fmt::Debug,
{
fn decompress<B: AsRef<[u8]>>(value: B) -> Result<T, DatabaseError> {
parity_scale_codec::Decode::decode(&mut value.as_ref()).map_err(|_| DatabaseError::Decode)
fn decompress(mut value: &[u8]) -> Result<T, DatabaseError> {
parity_scale_codec::Decode::decode(&mut value).map_err(|_| DatabaseError::Decode)
}
}

Expand Down
11 changes: 8 additions & 3 deletions crates/storage/db-api/src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<B: AsRef<[u8]>>(value: B) -> Result<Self, DatabaseError>;
fn decompress(value: &[u8]) -> Result<Self, DatabaseError>;

/// Decompresses owned data coming from the database.
fn decompress_owned(value: Vec<u8>) -> Result<Self, DatabaseError> {
Self::decompress(value)
Self::decompress(&value)
}
}

Expand All @@ -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<B: AsRef<[u8]>>(value: B) -> Result<Self, DatabaseError>;
fn decode(value: &[u8]) -> Result<Self, DatabaseError>;

/// Decodes owned data coming from the database.
fn decode_owned(value: Vec<u8>) -> Result<Self, DatabaseError> {
Self::decode(&value)
}
}

/// Generic trait that enforces the database key to implement [`Encode`] and [`Decode`].
Expand Down
5 changes: 2 additions & 3 deletions crates/storage/db-api/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ macro_rules! impl_fixed_arbitrary {
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self, arbitrary::Error> {
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)
}
}

Expand All @@ -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::<u8>(args), $size)
.prop_map(move |vec| Decode::decode(vec).unwrap())
.prop_map(move |vec| Decode::decode_owned(vec).unwrap())
}
}
)+
Expand Down
4 changes: 2 additions & 2 deletions crates/storage/db/benches/criterion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ where
|input| {
{
for (_, k, _, _) in input {
let _ = <T as Table>::Key::decode(k);
let _ = <T as Table>::Key::decode(&k);
}
};
black_box(());
Expand Down Expand Up @@ -115,7 +115,7 @@ where
|input| {
{
for (_, _, _, v) in input {
let _ = <T as Table>::Value::decompress(v);
let _ = <T as Table>::Value::decompress(&v);
}
};
black_box(());
Expand Down
Loading
Loading