forked from paradigmxyz/reth
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(db): add traits
Encode/Decode
for keys and `Compress/Uncompress…
…` for values (paradigmxyz#151) * add UncompressedUint * add more UncompressedUint keys * add docs * split key/value to encode/decode and compress/uncompress traits * reveert into * clippy * rm whitespaces * remove TODO * Remove scale encode/decode traits * decompress * clippy Co-authored-by: rakita <[email protected]>
- Loading branch information
Showing
18 changed files
with
195 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,59 @@ | ||
use crate::db::{models::accounts::AccountBeforeTx, Decode, Encode, Error}; | ||
use crate::db::{models::accounts::AccountBeforeTx, Compress, Decompress, Error}; | ||
use parity_scale_codec::decode_from_bytes; | ||
use reth_primitives::*; | ||
|
||
mod sealed { | ||
pub trait Sealed {} | ||
} | ||
/// Marker trait type to restrict the TableEncode and TableDecode with scale to chosen types. | ||
pub trait ScaleOnly: sealed::Sealed {} | ||
|
||
impl<T> Encode for T | ||
/// Marker trait type to restrict the [`Compress`] and [`Decompress`] with scale to chosen types. | ||
pub trait ScaleValue: sealed::Sealed {} | ||
|
||
impl<T> Compress for T | ||
where | ||
T: ScaleOnly + parity_scale_codec::Encode + Sync + Send + std::fmt::Debug, | ||
T: ScaleValue + parity_scale_codec::Encode + Sync + Send + std::fmt::Debug, | ||
{ | ||
type Encoded = Vec<u8>; | ||
type Compressed = Vec<u8>; | ||
|
||
fn encode(self) -> Self::Encoded { | ||
fn compress(self) -> Self::Compressed { | ||
parity_scale_codec::Encode::encode(&self) | ||
} | ||
} | ||
|
||
impl<T> Decode for T | ||
impl<T> Decompress for T | ||
where | ||
T: ScaleOnly + parity_scale_codec::Decode + Sync + Send + std::fmt::Debug, | ||
T: ScaleValue + parity_scale_codec::Decode + Sync + Send + std::fmt::Debug, | ||
{ | ||
fn decode<B: Into<bytes::Bytes>>(value: B) -> Result<T, Error> { | ||
fn decompress<B: Into<bytes::Bytes>>(value: B) -> Result<T, Error> { | ||
decode_from_bytes(value.into()).map_err(|e| Error::Decode(e.into())) | ||
} | ||
} | ||
|
||
/// Implements SCALE both for value and key types. | ||
macro_rules! impl_scale { | ||
($($name:tt),+) => { | ||
$( | ||
impl ScaleOnly for $name {} | ||
impl ScaleValue for $name {} | ||
impl sealed::Sealed for $name {} | ||
)+ | ||
}; | ||
} | ||
|
||
impl ScaleOnly for Vec<u8> {} | ||
impl sealed::Sealed for Vec<u8> {} | ||
/// Implements SCALE only for value types. | ||
macro_rules! impl_scale_value { | ||
($($name:tt),+) => { | ||
$( | ||
impl ScaleValue for $name {} | ||
impl sealed::Sealed for $name {} | ||
)+ | ||
}; | ||
} | ||
|
||
impl_scale!(u8, u32, u16, u64, U256, H256, H160); | ||
impl ScaleValue for Vec<u8> {} | ||
impl sealed::Sealed for Vec<u8> {} | ||
|
||
impl_scale!(U256, H256, H160); | ||
impl_scale!(Header, Account, Log, Receipt, TxType, StorageEntry); | ||
|
||
impl_scale!(AccountBeforeTx); | ||
|
||
impl_scale_value!(u8, u32, u16, u64); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,22 @@ | ||
//! Implements [`Encode`] and [`Decode`] for [`IntegerList`] | ||
//! Implements [`Compress`] and [`Decompress`] for [`IntegerList`] | ||
use crate::db::{ | ||
error::Error, | ||
table::{Decode, Encode}, | ||
table::{Compress, Decompress}, | ||
}; | ||
use bytes::Bytes; | ||
use reth_primitives::IntegerList; | ||
|
||
impl Encode for IntegerList { | ||
type Encoded = Vec<u8>; | ||
impl Compress for IntegerList { | ||
type Compressed = Vec<u8>; | ||
|
||
fn encode(self) -> Self::Encoded { | ||
fn compress(self) -> Self::Compressed { | ||
self.to_bytes() | ||
} | ||
} | ||
|
||
impl Decode for IntegerList { | ||
fn decode<B: Into<Bytes>>(value: B) -> Result<Self, Error> { | ||
impl Decompress for IntegerList { | ||
fn decompress<B: Into<Bytes>>(value: B) -> Result<Self, Error> { | ||
IntegerList::from_bytes(&value.into()).map_err(|e| Error::Decode(eyre::eyre!("{e}"))) | ||
} | ||
} |
Oops, something went wrong.