Skip to content

Commit

Permalink
debug: reorder to (c, b, n)
Browse files Browse the repository at this point in the history
  • Loading branch information
kien-rise committed Aug 17, 2024
1 parent cc2d24a commit e721b87
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
22 changes: 12 additions & 10 deletions src/storage/on_disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Address, Option<(B256, B64, B256)>>,
cache_encoded_accounts: DashMap<Address, Option<PackedAccount>>,
cache_storage: DashMap<(Address, U256), U256>,
cache_bytecodes: DashMap<B256, Option<EvmCode>>,
cache_block_hashes: DashMap<u64, B256>,
Expand Down Expand Up @@ -49,7 +51,7 @@ impl OnDiskStorage {
fn load_encoded_account(
&self,
address: Address,
) -> Result<OccupiedEntry<Address, Option<(B256, B64, B256)>>, reth_libmdbx::Error> {
) -> Result<OccupiedEntry<Address, Option<(B256, U256, u64)>>, reth_libmdbx::Error> {
match self.cache_encoded_accounts.entry(address) {
Entry::Occupied(occupied) => Ok(occupied),
Entry::Vacant(vacant) => {
Expand All @@ -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<PackedAccount> = 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))
}
Expand All @@ -80,8 +82,8 @@ impl Storage for OnDiskStorage {
fn basic(&self, address: &Address) -> Result<Option<AccountBasic>, 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,
}))
}

Expand All @@ -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<Option<EvmCode>, Self::Error> {
Expand Down
21 changes: 15 additions & 6 deletions tests/common/mdbx.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -40,6 +40,7 @@ fn write_table_to<K: AsRef<[u8]>, 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>(
Expand All @@ -56,15 +57,15 @@ pub(crate) fn create_db_dir<'a>(
)?;

// balance, nonce, code_hash (default: KECCAK_EMPTY)
let mut encoded_accounts = HashMap::<Address, (B256, B64, B256)>::new();
let mut encoded_accounts = HashMap::<Address, PackedAccount>::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() {
Expand All @@ -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(),
)
}),
)?;

Expand Down

0 comments on commit e721b87

Please sign in to comment.