Skip to content

Commit

Permalink
try to refine code
Browse files Browse the repository at this point in the history
  • Loading branch information
forcodedancing committed Sep 23, 2024
1 parent 77d9587 commit b55e45d
Show file tree
Hide file tree
Showing 15 changed files with 198 additions and 243 deletions.
16 changes: 8 additions & 8 deletions crates/chain-state/src/cache/cached_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use reth_storage_api::{
StateRootProvider, StorageRootProvider,
};
use reth_trie::{
cache::TrieCache, prefix_set::TriePrefixSetsMut, updates::TrieUpdates, AccountProof,
BranchNodeCompact, HashedPostState, HashedStorage, Nibbles,
prefix_set::TriePrefixSetsMut, updates::TrieUpdates, AccountProof, BranchNodeCompact,
HashedPostState, HashedStorage, Nibbles,
};
use std::collections::HashMap;

Expand Down Expand Up @@ -64,12 +64,12 @@ impl BlockHashReader for CachedStateProvider {
impl AccountReader for CachedStateProvider {
fn basic_account(&self, address: Address) -> ProviderResult<Option<Account>> {
// Check cache first
if let Some(v) = crate::cache::CACHED_PLAIN_STATES.get_account(&address) {
if let Some(v) = crate::cache::get_account(&address) {
return Ok(Some(v))
}
// Fallback to underlying provider
if let Some(value) = AccountReader::basic_account(&self.underlying, address)? {
crate::cache::CACHED_PLAIN_STATES.insert_account(address, value);
crate::cache::insert_account(address, value);
return Ok(Some(value))
}
Ok(None)
Expand Down Expand Up @@ -151,25 +151,25 @@ impl StateProvider for CachedStateProvider {
) -> ProviderResult<Option<StorageValue>> {
let key = (address, storage_key);
// Check cache first
if let Some(v) = crate::cache::CACHED_PLAIN_STATES.get_storage(&key) {
if let Some(v) = crate::cache::get_storage(&key) {
return Ok(Some(v))
}
// Fallback to underlying provider
if let Some(value) = StateProvider::storage(&self.underlying, address, storage_key)? {
crate::cache::CACHED_PLAIN_STATES.insert_storage(key, value);
crate::cache::insert_storage(key, value);
return Ok(Some(value))
}
Ok(None)
}

fn bytecode_by_hash(&self, code_hash: B256) -> ProviderResult<Option<Bytecode>> {
// Check cache first
if let Some(v) = crate::cache::CACHED_PLAIN_STATES.get_code(&code_hash) {
if let Some(v) = crate::cache::get_code(&code_hash) {
return Ok(Some(v))
}
// Fallback to underlying provider
if let Some(value) = StateProvider::bytecode_by_hash(&self.underlying, code_hash)? {
crate::cache::CACHED_PLAIN_STATES.insert_code(code_hash, value.clone());
crate::cache::insert_code(code_hash, value.clone());
return Ok(Some(value))
}
Ok(None)
Expand Down
10 changes: 5 additions & 5 deletions crates/chain-state/src/cache/mod.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
pub mod cached_provider;
mod plain_state;

pub use plain_state::CACHED_PLAIN_STATES;

use crate::ExecutedBlock;
use tracing::debug;

use crate::cache::plain_state::{clear_plain_state, write_plain_state};
use crate::cache::plain_state::{
clear_plain_state, get_account, get_code, get_storage, insert_account, insert_code,
insert_storage, write_plain_state,
};

/// Writes the execution outcomes, trie updates, and hashed states of the given blocks to the cache.
pub fn write_to_cache(blocks: Vec<ExecutedBlock>) {
for block in blocks {
debug!("Start to write block {} to cache", block.block.header.number);
let bundle_state = block.execution_outcome().clone().bundle;
let trie_updates = block.trie_updates().clone();
let hashed_state = block.hashed_state();
let trie_updates = block.trie_updates();
write_plain_state(bundle_state);
reth_trie_db::cache::write_trie_updates(&trie_updates);
debug!("Finish to write block {} to cache", block.block.header.number);
Expand Down
138 changes: 70 additions & 68 deletions crates/chain-state/src/cache/plain_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,83 +32,83 @@ lazy_static! {
/// The size of contract is large and the hot contracts should be limited.
static ref CONTRACT_CODES: Cache<B256, Bytecode> = Cache::new(CONTRACT_CACHE_SIZE);

/// Cached plain states
#[allow(clippy::type_complexity)]
pub static ref CACHED_PLAIN_STATES: (&'static Cache<Address, Account>, &'static Cache<AddressStorageKey, StorageValue>, &'static Cache<B256, Bytecode>) = (&PLAIN_ACCOUNTS, &PLAIN_STORAGES, &CONTRACT_CODES);
//// Cached plain states
////#[allow(clippy::type_complexity)]
////pub static ref CACHED_PLAIN_STATES: (&'static Cache<Address, Account>, &'static Cache<AddressStorageKey, StorageValue>, &'static Cache<B256, Bytecode>) = (&PLAIN_ACCOUNTS, &PLAIN_STORAGES, &CONTRACT_CODES);
}

impl CACHED_PLAIN_STATES {
pub fn insert_account(&self, k: Address, v: Account) {
PLAIN_ACCOUNTS.insert(k, v);
}
// impl CACHED_PLAIN_STATES {
pub fn insert_account(k: Address, v: Account) {
PLAIN_ACCOUNTS.insert(k, v);
}

/// Insert storage into the cache
pub fn insert_storage(&self, k: AddressStorageKey, v: U256) {
{
let mut map = PLAIN_STORAGES_MAPPING.lock().unwrap();
if let Some(set) = map.get_mut(&k.0) {
set.insert(k.1);
} else {
let mut s = HashSet::new();
s.insert(k.1);
map.insert(k.0, s);
}
/// Insert storage into the cache
pub fn insert_storage(k: AddressStorageKey, v: U256) {
{
let mut map = PLAIN_STORAGES_MAPPING.lock().unwrap();
if let Some(set) = map.get_mut(&k.0) {
set.insert(k.1);
} else {
let mut s = HashSet::new();
s.insert(k.1);
map.insert(k.0, s);
}
PLAIN_STORAGES.insert(k, v);
}
PLAIN_STORAGES.insert(k, v);
}
// }

// Implementing StateCache trait for CACHED_PLAIN_STATES
impl StateCache<Address, Account, AddressStorageKey, StorageValue, B256, Bytecode>
for CACHED_PLAIN_STATES
{
// Get account from cache
fn get_account(&self, k: &Address) -> Option<Account> {
// counter!("plain-cache.account.total").increment(1);
// match PLAIN_ACCOUNTS.get(k) {
// Some(r) => {
// counter!("plain-cache.account.hit").increment(1);
// Some(r)
// }
// None => None,
// }

PLAIN_ACCOUNTS.get(k)
}
// impl StateCache<Address, Account, AddressStorageKey, StorageValue, B256, Bytecode>
// for CACHED_PLAIN_STATES
// {
// Get account from cache
pub fn get_account(k: &Address) -> Option<Account> {
// counter!("plain-cache.account.total").increment(1);
// match PLAIN_ACCOUNTS.get(k) {
// Some(r) => {
// counter!("plain-cache.account.hit").increment(1);
// Some(r)
// }
// None => None,
// }

PLAIN_ACCOUNTS.get(k)
}

// Get storage from cache
fn get_storage(&self, k: &AddressStorageKey) -> Option<StorageValue> {
// counter!("plain-cache.storage.total").increment(1);
// match PLAIN_STORAGES.get(k) {
// Some(r) => {
// counter!("plain-cache.storage.hit").increment(1);
// Some(r)
// }
// None => None,
// }

PLAIN_STORAGES.get(k)
}
// Get storage from cache
pub fn get_storage(k: &AddressStorageKey) -> Option<StorageValue> {
// counter!("plain-cache.storage.total").increment(1);
// match PLAIN_STORAGES.get(k) {
// Some(r) => {
// counter!("plain-cache.storage.hit").increment(1);
// Some(r)
// }
// None => None,
// }

PLAIN_STORAGES.get(k)
}

// Get code from cache
fn get_code(&self, k: &B256) -> Option<Bytecode> {
// counter!("plain-cache.code.total").increment(1);
// match CONTRACT_CODES.get(k) {
// Some(r) => {
// counter!("plain-cache.code.hit").increment(1);
// Some(r)
// }
// None => None,
// }

CONTRACT_CODES.get(k)
}
// Get code from cache
pub fn get_code(k: &B256) -> Option<Bytecode> {
// counter!("plain-cache.code.total").increment(1);
// match CONTRACT_CODES.get(k) {
// Some(r) => {
// counter!("plain-cache.code.hit").increment(1);
// Some(r)
// }
// None => None,
// }

CONTRACT_CODES.get(k)
}

// Insert code into cache
fn insert_code(&self, k: B256, v: Bytecode) {
CONTRACT_CODES.insert(k, v);
}
// Insert code into cache
pub fn insert_code(k: B256, v: Bytecode) {
CONTRACT_CODES.insert(k, v);
}
// }

/// Write committed state to cache.
pub(crate) fn write_plain_state(bundle: BundleState) {
Expand Down Expand Up @@ -147,13 +147,15 @@ pub(crate) fn write_plain_state(bundle: BundleState) {
}

for (k, v) in storage.storage.clone() {
CACHED_PLAIN_STATES.insert_storage((storage.address, StorageKey::from(k)), v);
insert_storage((storage.address, StorageKey::from(k)), v);
}
}
}

/// Clear cached accounts and storages.
pub(crate) fn clear_plain_state() {
CACHED_PLAIN_STATES.0.clear();
CACHED_PLAIN_STATES.1.clear();
PLAIN_ACCOUNTS.clear();
PLAIN_STORAGES.clear();
let mut map = PLAIN_STORAGES_MAPPING.lock().unwrap();
map.clear();
}
4 changes: 2 additions & 2 deletions crates/chain-state/src/memory_overlay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use reth_storage_api::{
StateRootProvider, StorageRootProvider,
};
use reth_trie::{
cache::TrieCache, prefix_set::TriePrefixSetsMut, updates::TrieUpdates, AccountProof,
BranchNodeCompact, HashedPostState, HashedStorage, Nibbles,
prefix_set::TriePrefixSetsMut, updates::TrieUpdates, AccountProof, BranchNodeCompact,
HashedPostState, HashedStorage, Nibbles,
};
use std::{collections::HashMap, sync::OnceLock};

Expand Down
4 changes: 2 additions & 2 deletions crates/rpc/rpc-eth-types/src/cache/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use reth_primitives::{Account, Address, B256, U256};
use reth_revm::{database::StateProviderDatabase, db::CacheDB, DatabaseRef};
use reth_storage_api::StateProvider;
use reth_trie::{
cache::TrieCache, prefix_set::TriePrefixSetsMut, updates::TrieUpdates, BranchNodeCompact,
HashedPostState, HashedStorage, Nibbles,
prefix_set::TriePrefixSetsMut, updates::TrieUpdates, BranchNodeCompact, HashedPostState,
HashedStorage, Nibbles,
};

use revm::Database;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use reth_primitives::{Account, Address, BlockNumber, Bytecode, Bytes, B256, U256
use reth_storage_api::{StateProofProvider, StorageRootProvider};
use reth_storage_errors::provider::ProviderResult;
use reth_trie::{
cache::TrieCache, prefix_set::TriePrefixSetsMut, updates::TrieUpdates, AccountProof,
BranchNodeCompact, HashedPostState, HashedStorage, Nibbles,
prefix_set::TriePrefixSetsMut, updates::TrieUpdates, AccountProof, BranchNodeCompact,
HashedPostState, HashedStorage, Nibbles,
};
use std::collections::HashMap;

Expand Down
6 changes: 3 additions & 3 deletions crates/storage/provider/src/providers/state/latest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ use reth_primitives::{
use reth_storage_api::{StateProofProvider, StorageRootProvider};
use reth_storage_errors::provider::{ProviderError, ProviderResult};
use reth_trie::{
cache::TrieCache, prefix_set::TriePrefixSetsMut, proof::Proof, updates::TrieUpdates,
witness::TrieWitness, AccountProof, BranchNodeCompact, HashedPostState, HashedStorage, Nibbles,
StateRoot, StorageRoot,
prefix_set::TriePrefixSetsMut, proof::Proof, updates::TrieUpdates, witness::TrieWitness,
AccountProof, BranchNodeCompact, HashedPostState, HashedStorage, Nibbles, StateRoot,
StorageRoot,
};
use reth_trie_db::{DatabaseProof, DatabaseStateRoot, DatabaseStorageRoot, DatabaseTrieWitness};

Expand Down
4 changes: 2 additions & 2 deletions crates/storage/storage-api/src/trie.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use reth_primitives::{Account, Address, Bytes, B256, U256};
use reth_storage_errors::provider::ProviderResult;
use reth_trie::{
cache::TrieCache, prefix_set::TriePrefixSetsMut, updates::TrieUpdates, AccountProof,
BranchNodeCompact, HashedPostState, HashedStorage, Nibbles,
prefix_set::TriePrefixSetsMut, updates::TrieUpdates, AccountProof, HashedPostState,
HashedStorage,
};
use std::collections::HashMap;

Expand Down
Loading

0 comments on commit b55e45d

Please sign in to comment.