mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
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:
@ -3,13 +3,13 @@ use crate::Nibbles;
|
||||
use derive_more::Deref;
|
||||
use reth_db::{
|
||||
cursor::DbCursorRO,
|
||||
models::{AccountBeforeTx, TransitionIdAddress},
|
||||
models::{AccountBeforeTx, BlockNumberAddress},
|
||||
tables,
|
||||
transaction::DbTx,
|
||||
Error,
|
||||
};
|
||||
use reth_primitives::{keccak256, Address, StorageEntry, TransitionId, H256};
|
||||
use std::{collections::HashMap, ops::Range};
|
||||
use reth_primitives::{keccak256, BlockNumber, StorageEntry, H256};
|
||||
use std::{collections::HashMap, ops::RangeInclusive};
|
||||
|
||||
/// A wrapper around a database transaction that loads prefix sets within a given transition range.
|
||||
#[derive(Deref)]
|
||||
@ -29,7 +29,7 @@ where
|
||||
/// Load all account and storage changes for the given transition id range.
|
||||
pub fn load(
|
||||
self,
|
||||
tid_range: Range<TransitionId>,
|
||||
range: RangeInclusive<BlockNumber>,
|
||||
) -> Result<(PrefixSet, HashMap<H256, PrefixSet>), Error> {
|
||||
// Initialize prefix sets.
|
||||
let mut account_prefix_set = PrefixSet::default();
|
||||
@ -37,7 +37,7 @@ where
|
||||
|
||||
// Walk account changeset and insert account prefixes.
|
||||
let mut account_cursor = self.cursor_read::<tables::AccountChangeSet>()?;
|
||||
for account_entry in account_cursor.walk_range(tid_range.clone())? {
|
||||
for account_entry in account_cursor.walk_range(range.clone())? {
|
||||
let (_, AccountBeforeTx { address, .. }) = account_entry?;
|
||||
account_prefix_set.insert(Nibbles::unpack(keccak256(address)));
|
||||
}
|
||||
@ -45,10 +45,9 @@ where
|
||||
// Walk storage changeset and insert storage prefixes as well as account prefixes if missing
|
||||
// from the account prefix set.
|
||||
let mut storage_cursor = self.cursor_dup_read::<tables::StorageChangeSet>()?;
|
||||
let start = TransitionIdAddress((tid_range.start, Address::zero()));
|
||||
let end = TransitionIdAddress((tid_range.end, Address::zero()));
|
||||
for storage_entry in storage_cursor.walk_range(start..end)? {
|
||||
let (TransitionIdAddress((_, address)), StorageEntry { key, .. }) = storage_entry?;
|
||||
let storage_range = BlockNumberAddress::range(range);
|
||||
for storage_entry in storage_cursor.walk_range(storage_range)? {
|
||||
let (BlockNumberAddress((_, address)), StorageEntry { key, .. }) = storage_entry?;
|
||||
let hashed_address = keccak256(address);
|
||||
account_prefix_set.insert(Nibbles::unpack(hashed_address));
|
||||
storage_prefix_set
|
||||
|
||||
@ -16,10 +16,10 @@ use reth_primitives::{
|
||||
keccak256,
|
||||
proofs::EMPTY_ROOT,
|
||||
trie::{BranchNodeCompact, StorageTrieEntry, StoredNibblesSubKey},
|
||||
Address, StorageEntry, TransitionId, H256,
|
||||
Address, BlockNumber, StorageEntry, H256,
|
||||
};
|
||||
use reth_rlp::Encodable;
|
||||
use std::{collections::HashMap, ops::Range, sync::mpsc};
|
||||
use std::{collections::HashMap, ops::RangeInclusive, sync::mpsc};
|
||||
|
||||
/// The branch node update sender
|
||||
pub type BranchNodeUpdateSender = mpsc::Sender<BranchNodeUpdate>;
|
||||
@ -77,11 +77,11 @@ impl<'a, 'tx, TX: DbTx<'tx> + DbTxMut<'tx>> StateRoot<'a, TX> {
|
||||
/// The updated state root hash.
|
||||
pub fn incremental_root(
|
||||
tx: &'a TX,
|
||||
tid_range: Range<TransitionId>,
|
||||
range: RangeInclusive<BlockNumber>,
|
||||
branch_node_sender: Option<BranchNodeUpdateSender>,
|
||||
) -> Result<H256, StateRootError> {
|
||||
tracing::debug!(target: "loader", "incremental state root");
|
||||
let (account_prefixes, storage_prefixes) = PrefixSetLoader::new(tx).load(tid_range)?;
|
||||
let (account_prefixes, storage_prefixes) = PrefixSetLoader::new(tx).load(range)?;
|
||||
let this = Self::new(tx)
|
||||
.with_changed_account_prefixes(account_prefixes)
|
||||
.with_changed_storage_prefixes(storage_prefixes);
|
||||
@ -162,7 +162,7 @@ impl<'a, 'tx, TX: DbTx<'tx> + DbTxMut<'tx>> StateRoot<'a, TX> {
|
||||
|
||||
let account = EthAccount::from(account).with_storage_root(storage_root);
|
||||
let mut account_rlp = Vec::with_capacity(account.length());
|
||||
account.encode(&mut account_rlp);
|
||||
account.encode(&mut &mut account_rlp);
|
||||
|
||||
hash_builder.add_leaf(account_nibbles, &account_rlp);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user