Skip to content

Commit

Permalink
no storage cache
Browse files Browse the repository at this point in the history
  • Loading branch information
forcodedancing committed Oct 21, 2024
1 parent be922b0 commit dbdbdd4
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 55 deletions.
54 changes: 27 additions & 27 deletions crates/storage/provider/src/providers/state/cache/cache_writer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::providers::state::cache::plain_state::{PLAIN_ACCOUNTS, PLAIN_STORAGES};
use crate::providers::state::cache::plain_state::PLAIN_ACCOUNTS;
use alloy_primitives::StorageKey;
use quick_cache::sync::Cache;
use reth_chain_state::ExecutedBlock;
Expand Down Expand Up @@ -29,11 +29,11 @@ impl<'a, TX> PlainCacheWriter<'a, TX> {
super::plain_state::PLAIN_ACCOUNTS.len(),
block.block.number
);
info!(
"STORAGE_CACHE_SZ {}, block number {}",
super::plain_state::PLAIN_STORAGES.len(),
block.block.number
);
// info!(
// "STORAGE_CACHE_SZ {}, block number {}",
// super::plain_state::PLAIN_STORAGES.len(),
// block.block.number
// );
};

let bundle_state = block.execution_outcome().clone().bundle;
Expand All @@ -52,11 +52,11 @@ impl<'a, TX> PlainCacheWriter<'a, TX> {
last_block,
super::plain_state::PLAIN_ACCOUNTS.len(),
);
info!(
"block number {}, P_STORAGE_CACHE_SZ {}",
last_block,
super::plain_state::PLAIN_STORAGES.len(),
);
// info!(
// "block number {}, P_STORAGE_CACHE_SZ {}",
// last_block,
// super::plain_state::PLAIN_STORAGES.len(),
// );
}
// Update account cache
for (address, account_info) in &change_set.accounts {
Expand All @@ -77,26 +77,26 @@ impl<'a, TX> PlainCacheWriter<'a, TX> {
}
}

let mut should_wipe = false;
for storage in &change_set.storage {
if storage.wipe_storage {
should_wipe = true;
break;
}

for (k, v) in storage.storage.clone() {
super::plain_state::PLAIN_STORAGES
.insert((storage.address, StorageKey::from(k)), v);
}
}
if should_wipe {
super::plain_state::PLAIN_STORAGES.clear();
}
// let mut should_wipe = false;
// for storage in &change_set.storage {
// if storage.wipe_storage {
// should_wipe = true;
// break;
// }
//
// for (k, v) in storage.storage.clone() {
// super::plain_state::PLAIN_STORAGES
// .insert((storage.address, StorageKey::from(k)), v);
// }
// }
// if should_wipe {
// super::plain_state::PLAIN_STORAGES.clear();
// }
}
}

/// Clear cached accounts and storages.
pub fn clear_plain_state() {
PLAIN_ACCOUNTS.clear();
PLAIN_STORAGES.clear();
//PLAIN_STORAGES.clear();
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,12 @@ impl StateProvider for CachedStateProvider {
) -> ProviderResult<Option<StorageValue>> {
let key = (address, storage_key);
// Check cache first
if let Some(v) = crate::providers::state::cache::plain_state::get_storage(&key) {
return Ok(Some(v))
}
// if let Some(v) = crate::providers::state::cache::plain_state::get_storage(&key) {
// return Ok(Some(v))
// }
// Fallback to underlying provider
if let Some(value) = StateProvider::storage(&self.underlying, address, storage_key)? {
crate::providers::state::cache::plain_state::insert_storage(key, value);
//crate::providers::state::cache::plain_state::insert_storage(key, value);
return Ok(Some(value))
}
Ok(None)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,29 +167,13 @@ impl<'b, TX: DbTx> StateProvider for CachedStateProviderRef<'b, TX> {
account: Address,
storage_key: StorageKey,
) -> ProviderResult<Option<StorageValue>> {
if !self.cache_enabled {
let mut cursor = self.tx.cursor_dup_read::<tables::PlainStorageState>()?;
if let Some(entry) = cursor.seek_by_key_subkey(account, storage_key)? {
if entry.key == storage_key {
return Ok(Some(entry.value))
}
}
return Ok(None)
}

let key = (account, storage_key);
if let Some(v) = crate::providers::state::cache::plain_state::get_storage(&key) {
return Ok(Some(v))
}

let mut cursor = self.tx.cursor_dup_read::<tables::PlainStorageState>()?;
if let Some(entry) = cursor.seek_by_key_subkey(account, storage_key)? {
if entry.key == storage_key {
crate::providers::state::cache::plain_state::insert_storage(key, entry.value);
return Ok(Some(entry.value))
}
}
Ok(None)
return Ok(None)
}

/// Get account code by its hash
Expand Down
14 changes: 7 additions & 7 deletions crates/storage/provider/src/providers/state/cache/plain_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ lazy_static! {
pub static ref PLAIN_ACCOUNTS: Cache<Address, Account> = Cache::new(ACCOUNT_CACHE_SIZE);

/// Storage cache
pub static ref PLAIN_STORAGES: Cache<AddressStorageKey, StorageValue> = Cache::new(STORAGE_CACHE_SIZE);
///pub static ref PLAIN_STORAGES: Cache<AddressStorageKey, StorageValue> = Cache::new(STORAGE_CACHE_SIZE);
/// Contract cache
/// The size of contract is large and the hot contracts should be limited.
Expand All @@ -31,19 +31,19 @@ pub(crate) fn insert_account(k: Address, v: Account) {
}

/// Insert storage into the cache
pub(crate) fn insert_storage(k: AddressStorageKey, v: U256) {
PLAIN_STORAGES.insert(k, v);
}
///pub(crate) fn insert_storage(k: AddressStorageKey, v: U256) {
// PLAIN_STORAGES.insert(k, v);
// }

// Get account from cache
pub(crate) fn get_account(k: &Address) -> Option<Account> {
PLAIN_ACCOUNTS.get(k)
}

// Get storage from cache
pub(crate) fn get_storage(k: &AddressStorageKey) -> Option<StorageValue> {
PLAIN_STORAGES.get(k)
}
// pub(crate) fn get_storage(k: &AddressStorageKey) -> Option<StorageValue> {
// PLAIN_STORAGES.get(k)
// }

// Get code from cache
pub(crate) fn get_code(k: &B256) -> Option<Bytecode> {
Expand Down

0 comments on commit dbdbdd4

Please sign in to comment.