feat(db): derive Compact codec (#177)

This commit is contained in:
joshieDo
2022-11-23 12:46:45 +08:00
committed by GitHub
parent 0f45f16455
commit 027fc2bbf2
30 changed files with 1413 additions and 72 deletions

View File

@@ -0,0 +1,36 @@
use crate::db::{
models::{accounts::AccountBeforeTx, StoredBlockBody},
Compress, Decompress, Error,
};
use reth_codecs::Compact;
use reth_primitives::*;
/// Implements compression for Compact type.
macro_rules! impl_compression_for_compact {
($($name:tt),+) => {
$(
impl Compress for $name
{
type Compressed = Vec<u8>;
fn compress(self) -> Self::Compressed {
let mut buf = vec![];
let _ = Compact::to_compact(self, &mut buf);
buf
}
}
impl Decompress for $name
{
fn decompress<B: Into<bytes::Bytes>>(value: B) -> Result<$name, Error> {
let value = value.into();
let (obj, _) = Compact::from_compact(&value, value.len());
Ok(obj)
}
}
)+
};
}
impl_compression_for_compact!(Header, Account, Log, Receipt, TxType, StorageEntry, StoredBlockBody);
impl_compression_for_compact!(AccountBeforeTx);

View File

@@ -66,6 +66,7 @@ macro_rules! impl_fuzzer_key {
/// Fuzzer generates a random instance of the object and proceeds to compress and decompress it. It
/// then makes sure that it matches the original object.
#[allow(unused)]
macro_rules! impl_fuzzer_value {
($($name:tt),+) => {
$(
@@ -85,7 +86,5 @@ macro_rules! impl_fuzzer_value_with_input {
};
}
impl_fuzzer_value!(Header, Account);
impl_fuzzer_key!(BlockNumHash, TxNumberAddress);
impl_fuzzer_value_with_input!((IntegerList, IntegerListInput));

View File

@@ -1,5 +1,6 @@
//! Integrates different codecs into table::Encode and table::Decode
mod compact;
pub mod fuzz;
mod postcard;
#[cfg(not(feature = "bench-postcard"))]

View File

@@ -1,7 +1,4 @@
use crate::db::{
models::{accounts::AccountBeforeTx, StoredBlockBody},
Compress, Decompress, Error,
};
use crate::db::{Compress, Decompress, Error};
use parity_scale_codec::decode_from_bytes;
use reth_primitives::*;
@@ -32,18 +29,8 @@ where
}
}
/// Implements SCALE both for value and key types.
macro_rules! impl_scale {
($($name:tt),+) => {
$(
impl ScaleValue for $name {}
impl sealed::Sealed for $name {}
)+
};
}
/// Implements SCALE only for value types.
macro_rules! impl_scale_value {
/// Implements compression for SCALE type.
macro_rules! impl_compression_for_scale {
($($name:tt),+) => {
$(
impl ScaleValue for $name {}
@@ -55,17 +42,6 @@ macro_rules! impl_scale_value {
impl ScaleValue for Vec<u8> {}
impl sealed::Sealed for Vec<u8> {}
impl_scale!(U256, H256, H160);
impl_scale!(
Header,
Account,
Log,
Receipt,
TxType,
StorageEntry,
TransactionSigned,
StoredBlockBody
);
impl_scale!(AccountBeforeTx);
impl_scale_value!(u8, u32, u16, u64);
impl_compression_for_scale!(U256, H256, H160);
impl_compression_for_scale!(TransactionSigned);
impl_compression_for_scale!(u8, u32, u16, u64);

View File

@@ -8,13 +8,13 @@ use crate::{
impl_fixed_arbitrary,
};
use bytes::Bytes;
use reth_codecs::main_codec;
use reth_codecs::{use_compact, Compact};
use reth_primitives::{Account, Address, TxNumber};
use serde::{Deserialize, Serialize};
/// Account as it is saved inside [`AccountChangeSet`]. [`Address`] is the subkey.
#[main_codec]
#[derive(Debug, Default, Clone)]
#[use_compact]
#[derive(Debug, Default, Clone, PartialEq)]
pub struct AccountBeforeTx {
/// Address for the account. Acts as `DupSort::SubKey`.
address: Address,

View File

@@ -7,8 +7,9 @@ use crate::{
},
impl_fixed_arbitrary,
};
use bytes::Bytes;
use reth_codecs::main_codec;
use bytes::{Buf, Bytes};
use modular_bitfield::prelude::*;
use reth_codecs::{use_compact, Compact};
use reth_primitives::{BlockHash, BlockNumber, Header, TxNumber, H256};
use serde::{Deserialize, Serialize};
@@ -22,8 +23,8 @@ pub type NumTransactions = u64;
///
/// The [TxNumber]s for all the transactions in the block are `base_tx_id..(base_tx_id +
/// tx_amount)`.
#[derive(Debug)]
#[main_codec]
#[derive(Debug, Default, PartialEq, Clone)]
#[use_compact]
pub struct StoredBlockBody {
/// The ID of the first transaction in the block.
pub base_tx_id: TxNumber,