From bd126ca235108c206643bbb758cc63bfc38a7aab Mon Sep 17 00:00:00 2001 From: Kien Nguyen Date: Sun, 18 Aug 2024 00:49:58 +0700 Subject: [PATCH] debug: reorder to (c, b, n) --- src/storage/on_disk.rs | 22 ++++++++++++---------- tests/common/mdbx.rs | 21 +++++++++++++++------ 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/src/storage/on_disk.rs b/src/storage/on_disk.rs index fe8df308..1e47ae46 100644 --- a/src/storage/on_disk.rs +++ b/src/storage/on_disk.rs @@ -7,11 +7,13 @@ use reth_libmdbx::{Database, Environment, Transaction, RO}; use super::{AccountBasic, EvmCode, Storage}; +type PackedAccount = (B256, U256, u64); + /// A storage that reads data from an on-disk MDBX database. #[derive(Debug)] pub struct OnDiskStorage { env: Environment, - cache_encoded_accounts: DashMap>, + cache_encoded_accounts: DashMap>, cache_storage: DashMap<(Address, U256), U256>, cache_bytecodes: DashMap>, cache_block_hashes: DashMap, @@ -49,7 +51,7 @@ impl OnDiskStorage { fn load_encoded_account( &self, address: Address, - ) -> Result>, reth_libmdbx::Error> { + ) -> Result>, reth_libmdbx::Error> { match self.cache_encoded_accounts.entry(address) { Entry::Occupied(occupied) => Ok(occupied), Entry::Vacant(vacant) => { @@ -62,11 +64,11 @@ impl OnDiskStorage { .get(self.table_encoded_accounts.dbi(), address.as_ref())?; drop(tx_ref); - let decoded = bytes.map(|bytes| { - let b = B256::from_slice(&bytes[0..32]); - let n = B64::from_slice(&bytes[32..(32 + 8)]); - let c = B256::from_slice(&bytes[(32 + 8)..(32 + 8 + 32)]); - (b, n, c) + let decoded: Option = bytes.map(|bytes| { + let c = B256::from_slice(&bytes[0..32]); + let b = B256::from_slice(&bytes[32..64]).into(); + let n = B64::from_slice(&bytes[64..]).into(); + (c, b, n) }); Ok(vacant.insert_entry(decoded)) } @@ -80,8 +82,8 @@ impl Storage for OnDiskStorage { fn basic(&self, address: &Address) -> Result, Self::Error> { let entry = self.load_encoded_account(*address)?; Ok(entry.get().as_ref().map(|account| AccountBasic { - balance: account.0.into(), - nonce: account.1.into(), + balance: account.1, + nonce: account.2, })) } @@ -90,7 +92,7 @@ impl Storage for OnDiskStorage { Ok(entry .get() .as_ref() - .and_then(|(_, _, c)| (*c != KECCAK_EMPTY).then_some(*c))) + .and_then(|(c, _, _)| (*c != KECCAK_EMPTY).then_some(*c))) } fn code_by_hash(&self, code_hash: &B256) -> Result, Self::Error> { diff --git a/tests/common/mdbx.rs b/tests/common/mdbx.rs index 34813e45..5af48cbb 100644 --- a/tests/common/mdbx.rs +++ b/tests/common/mdbx.rs @@ -1,6 +1,6 @@ use std::{collections::HashMap, path::Path}; -use alloy_primitives::{b256, Address, B256, B64}; +use alloy_primitives::{b256, Address, B256, B64, U256}; use pevm::{EvmAccount, EvmCode}; use reth_libmdbx::{ DatabaseFlags, Environment, EnvironmentFlags, Geometry, Mode, SyncMode, WriteFlags, @@ -40,6 +40,7 @@ fn write_table_to, V: AsRef<[u8]>>( /// The Keccak-256 hash of the empty string `""`. const KECCAK_EMPTY: B256 = b256!("c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"); +type PackedAccount = (B256, U256, u64); /// Create a temp dir containing MDBX pub(crate) fn create_db_dir<'a>( @@ -56,15 +57,15 @@ pub(crate) fn create_db_dir<'a>( )?; // balance, nonce, code_hash (default: KECCAK_EMPTY) - let mut encoded_accounts = HashMap::::new(); + let mut encoded_accounts = HashMap::::new(); let mut storage = HashMap::<(Address, B256), B256>::new(); for (&address, account) in pre_state { encoded_accounts.insert( address, ( - B256::from(account.balance), - B64::from(account.nonce), account.code_hash.unwrap_or(KECCAK_EMPTY), + account.balance, + account.nonce, ), ); for (&index, &storage_value) in account.storage.iter() { @@ -75,8 +76,16 @@ pub(crate) fn create_db_dir<'a>( write_table_to( &env, "encoded_accounts", - encoded_accounts.into_iter().map(|(address, (b, n, c))| { - (address, [b.as_slice(), n.as_slice(), c.as_slice()].concat()) + encoded_accounts.into_iter().map(|(address, (c, b, n))| { + ( + address, + [ + c.as_slice(), + B256::from(b).as_slice(), + B64::from(n).as_slice(), + ] + .concat(), + ) }), )?;