feat: block level indexing (#2275)

Co-authored-by: rakita <dragan0rakita@gmail.com>
Co-authored-by: Georgios Konstantopoulos <me@gakonst.com>
This commit is contained in:
Bjerg
2023-04-20 21:03:48 +02:00
committed by GitHub
parent 855a7d5541
commit 68d8506d8e
59 changed files with 1486 additions and 1726 deletions

View File

@ -25,7 +25,7 @@ use crate::{
tables::{
codecs::CompactU256,
models::{
accounts::{AccountBeforeTx, TransitionIdAddress},
accounts::{AccountBeforeTx, BlockNumberAddress},
blocks::{HeaderHash, StoredBlockOmmers},
storage_sharded_key::StorageShardedKey,
ShardedKey, StoredBlockBodyIndices, StoredBlockWithdrawals,
@ -35,7 +35,7 @@ use crate::{
use reth_primitives::{
trie::{BranchNodeCompact, StorageTrieEntry, StoredNibbles, StoredNibblesSubKey},
Account, Address, BlockHash, BlockNumber, Bytecode, Header, IntegerList, Receipt, StorageEntry,
TransactionSigned, TransitionId, TxHash, TxNumber, H256,
TransactionSigned, TxHash, TxNumber, H256,
};
/// Enum for the types of tables present in libmdbx.
@ -151,8 +151,7 @@ table!(
);
table!(
/// Stores block indices that contains indexes of transaction and transitions,
/// number of transactions and if block has a block change (block reward or withdrawals).
/// Stores block indices that contains indexes of transaction and the count of them.
///
/// More information about stored indices can be found in the [`StoredBlockBodyIndices`] struct.
( BlockBodyIndices ) BlockNumber | StoredBlockBodyIndices
@ -209,61 +208,61 @@ dupsort!(
);
table!(
/// Stores pointers to transition changeset with changes for each account key.
/// Stores pointers to block changeset with changes for each account key.
///
/// Last shard key of the storage will contains `u64::MAX` `TransitionId`,
/// Last shard key of the storage will contains `u64::MAX` `BlockNumber`,
/// this would allows us small optimization on db access when change is in plain state.
///
/// Imagine having shards as:
/// * `Address | 100`
/// * `Address | u64::MAX`
///
/// What we need to find is id that is one greater than N. Db `seek` function allows us to fetch
/// What we need to find is number that is one greater than N. Db `seek` function allows us to fetch
/// the shard that equal or more than asked. For example:
/// * For N=50 we would get first shard.
/// * for N=150 we would get second shard.
/// * If max transition id is 200 and we ask for N=250 we would fetch last shard and
/// * If max block number is 200 and we ask for N=250 we would fetch last shard and
/// know that needed entry is in `AccountPlainState`.
/// * If there were no shard we would get `None` entry or entry of different storage key.
///
/// Code example can be found in `reth_provider::HistoricalStateProviderRef`
( AccountHistory ) ShardedKey<Address> | TransitionList
( AccountHistory ) ShardedKey<Address> | BlockNumberList
);
table!(
/// Stores pointers to transition changeset with changes for each storage key.
/// Stores pointers to block number changeset with changes for each storage key.
///
/// Last shard key of the storage will contains `u64::MAX` `TransitionId`,
/// Last shard key of the storage will contains `u64::MAX` `BlockNumber`,
/// this would allows us small optimization on db access when change is in plain state.
///
/// Imagine having shards as:
/// * `Address | StorageKey | 100`
/// * `Address | StorageKey | u64::MAX`
///
/// What we need to find is id that is one greater than N. Db `seek` function allows us to fetch
/// What we need to find is number that is one greater than N. Db `seek` function allows us to fetch
/// the shard that equal or more than asked. For example:
/// * For N=50 we would get first shard.
/// * for N=150 we would get second shard.
/// * If max transition id is 200 and we ask for N=250 we would fetch last shard and
/// * If max block number is 200 and we ask for N=250 we would fetch last shard and
/// know that needed entry is in `StoragePlainState`.
/// * If there were no shard we would get `None` entry or entry of different storage key.
///
/// Code example can be found in `reth_provider::HistoricalStateProviderRef`
( StorageHistory ) StorageShardedKey | TransitionList
( StorageHistory ) StorageShardedKey | BlockNumberList
);
dupsort!(
/// Stores the state of an account before a certain transaction changed it.
/// Change on state can be: account is created, selfdestructed, touched while empty
/// or changed (balance,nonce).
( AccountChangeSet ) TransitionId | [Address] AccountBeforeTx
( AccountChangeSet ) BlockNumber | [Address] AccountBeforeTx
);
dupsort!(
/// Stores the state of a storage key before a certain transaction changed it.
/// If [`StorageEntry::value`] is zero, this means storage was not existing
/// and needs to be removed.
( StorageChangeSet ) TransitionIdAddress | [H256] StorageEntry
( StorageChangeSet ) BlockNumberAddress | [H256] StorageEntry
);
table!(
@ -309,14 +308,9 @@ table!(
( SyncStageProgress ) StageId | Vec<u8>
);
///
/// Alias Types
/// List with transaction numbers.
pub type TransitionList = IntegerList;
pub type BlockNumberList = IntegerList;
/// Encoded stage id.
pub type StageId = String;
//
// TODO: Temporary types, until they're properly defined alongside with the Encode and Decode Trait
//