Files
nanoreth/crates/db/src/kv/tables.rs
joshieDo 063b444792 feat(db): codec encoding/decoding (#51)
* wip

* add table macro

* add simple put get test with Address

* add Env.view and Env.update

* docs

* slightly change the test

* add initial table initialization and placeholders

* lint & some

* replace String with str

* add error.rs

* add docs to encode

* add docs

* clamp

* add source on libmdbx_max_page_size

* add BlockNumer_BlockHash

* add scale

* set header filed to bytes Bytes

* remove unwrap

* restrict scale to chosen types

* into bytes

* add postcard

* changed to BlockNumHash

* add proc_macro_attribute codecs

* fix feature flagging

* set a version for postcard

* cleanup

* seal ScaleOnly

* remove unnecessary dependencies

* properly encode/decode blocknumhash

* change Account codec to scale

* add missing feature to scale

* add codec to a couple more types

* silence clippy

* add docs about table encoding

* move and add reth-codecs

* clippy

* make proc-macro visible

* add README.md
2022-10-17 01:04:57 -07:00

112 lines
3.8 KiB
Rust

//! Declaration of all MDBX tables.
use crate::{
kv::blocks::{BlockNumHash, HeaderHash, NumTransactions, NumTxesInBlock},
utils::TableType,
};
use reth_primitives::{Account, Address, BlockNumber, Header, Receipt};
/// Default tables that should be present inside database.
pub const TABLES: [(TableType, &str); 18] = [
(TableType::Table, CanonicalHeaders::const_name()),
(TableType::Table, HeaderTD::const_name()),
(TableType::Table, HeaderNumbers::const_name()),
(TableType::Table, Headers::const_name()),
(TableType::Table, BlockBodies::const_name()),
(TableType::Table, CumulativeTxCount::const_name()),
(TableType::Table, NonCanonicalTransactions::const_name()),
(TableType::Table, Transactions::const_name()),
(TableType::Table, Receipts::const_name()),
(TableType::Table, Logs::const_name()),
(TableType::Table, PlainState::const_name()),
(TableType::Table, AccountHistory::const_name()),
(TableType::Table, StorageHistory::const_name()),
(TableType::DupSort, AccountChangeSet::const_name()),
(TableType::DupSort, StorageChangeSet::const_name()),
(TableType::Table, TxSenders::const_name()),
(TableType::Table, Config::const_name()),
(TableType::Table, SyncStage::const_name()),
];
#[macro_export]
/// Macro to declare all necessary tables.
macro_rules! table {
($name:ident => $key:ty => $value:ty => $seek:ty) => {
/// $name MDBX table.
#[derive(Clone, Copy, Debug, Default)]
pub struct $name;
impl $crate::kv::table::Table for $name {
const NAME: &'static str = $name::const_name();
type Key = $key;
type Value = $value;
type SeekKey = $seek;
}
impl $name {
/// Return $name as it is present inside the database.
pub const fn const_name() -> &'static str {
stringify!($name)
}
}
impl std::fmt::Display for $name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", stringify!($name))
}
}
};
($name:ident => $key:ty => $value:ty) => {
table!($name => $key => $value => $key);
};
}
//
// TABLE DEFINITIONS
//
table!(CanonicalHeaders => BlockNumber => HeaderHash);
table!(HeaderTD => BlockNumHash => RlpTotalDifficulty);
table!(HeaderNumbers => BlockNumHash => BlockNumber);
table!(Headers => BlockNumHash => Header);
table!(BlockBodies => BlockNumHash => NumTxesInBlock);
table!(CumulativeTxCount => BlockNumHash => NumTransactions); // TODO U256?
table!(NonCanonicalTransactions => BlockNumHashTxNumber => RlpTxBody);
table!(Transactions => TxNumber => RlpTxBody); // Canonical only
table!(Receipts => TxNumber => Receipt); // Canonical only
table!(Logs => TxNumber => Receipt); // Canonical only
table!(PlainState => PlainStateKey => Account);
table!(AccountHistory => Address => TxNumberList);
table!(StorageHistory => Address_StorageKey => TxNumberList);
table!(AccountChangeSet => TxNumber => AccountBeforeTx);
table!(StorageChangeSet => TxNumber => StorageKeyBeforeTx);
table!(TxSenders => TxNumber => Address); // Is it necessary?
table!(Config => ConfigKey => ConfigValue);
table!(SyncStage => StageId => BlockNumber);
//
// TODO: Temporary types, until they're properly defined alongside with the Encode and Decode Trait
//
type ConfigKey = Vec<u8>;
type ConfigValue = Vec<u8>;
#[allow(non_camel_case_types)]
type BlockNumHashTxNumber = Vec<u8>;
type RlpTotalDifficulty = Vec<u8>;
type RlpTxBody = Vec<u8>;
type TxNumber = u64; // TODO check size
type PlainStateKey = Address; // TODO new type will have to account for address_incarna_skey as well
type TxNumberList = Vec<u8>;
#[allow(non_camel_case_types)]
type Address_StorageKey = Vec<u8>;
type AccountBeforeTx = Vec<u8>;
type StorageKeyBeforeTx = Vec<u8>;
type StageId = Vec<u8>;