Skip to content

Commit

Permalink
apply code suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
nbaztec committed Sep 15, 2023
1 parent 3419520 commit 2afe4eb
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ etc/**/*.zbin
!.vscode/launch.json
!.vscode/tasks.json

*.log
*.log
.cache
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ era_test_node --log=error --log-file-path=run.log run

## 📃 Caching

The node will cache certain network request by default to disk in the `.cache` directory. Alternatively the caching can be disabled tor set to in-memory only
The node will cache certain network request by default to disk in the `.cache` directory. Alternatively the caching can be disabled or set to in-memory only
via the `--cache=none|memory|disk` parameter.

```bash
Expand Down
54 changes: 51 additions & 3 deletions src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,19 @@ impl Cache {

if let CacheConfig::Disk { dir, reset } = &config {
if *reset {
fs::remove_dir_all(Path::new(dir)).unwrap_or_else(|err| {
log::error!("failed removing cache from disk: {:?}", err)
});
for cache_type in [
CACHE_TYPE_BLOCKS_FULL,
CACHE_TYPE_BLOCKS_MIN,
CACHE_TYPE_BLOCK_RAW_TRANSACTIONS,
CACHE_TYPE_TRANSACTIONS,
] {
fs::remove_dir_all(Path::new(dir).join(cache_type)).unwrap_or_else(|err| {
log::warn!("failed removing directory {:?}: {:?}", Path::new(dir).join(cache_type), err)
});
}

fs::remove_dir(Path::new(dir))
.unwrap_or_else(|err| log::warn!("failed removing cache directory: {:?}", err));
}

for cache_type in [
Expand Down Expand Up @@ -492,4 +502,42 @@ mod tests {
assert_eq!(None, new_cache.get_block_raw_transactions(&0));
assert_eq!(None, new_cache.get_transaction(&H256::zero()));
}

#[test]
fn test_cache_config_disk_only_resets_created_data_on_disk() {
let cache_dir = TempDir::new("cache-test").expect("failed creating temporary dir");
let cache_dir_path = cache_dir
.path()
.to_str()
.expect("invalid dir name")
.to_string();
let mut cache = Cache::new(CacheConfig::Disk {
dir: cache_dir_path.clone(),
reset: true,
});

cache.insert_transaction(H256::zero(), Default::default());
let cached_tx_file = cache_dir
.path()
.join(CACHE_TYPE_TRANSACTIONS)
.join(format!("{:#x}", H256::zero()));
assert!(
cached_tx_file.exists(),
"cached transaction did not exist on disk"
);

let random_file_path = cache_dir.path().join("foobar.txt");
_ = File::create(&random_file_path).expect("failed creating random file");

Cache::new(CacheConfig::Disk {
dir: cache_dir_path,
reset: true,
});

assert!(
!cached_tx_file.exists(),
"cached transaction was not reset on disk"
);
assert!(random_file_path.exists(), "random file was reset from disk");
}
}
4 changes: 2 additions & 2 deletions src/hardhat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl<S: Send + Sync + 'static + ForkSource + std::fmt::Debug> HardhatNamespaceT
inner_guard
.fork_storage
.set_value(balance_key, u256_to_h256(balance));
log::debug!(
log::info!(
"👷 Balance for address {:?} has been manually set to {} Wei",
address,
balance
Expand Down Expand Up @@ -114,7 +114,7 @@ impl<S: Send + Sync + 'static + ForkSource + std::fmt::Debug> HardhatNamespaceT
}
deployment_nonce = nonce;
let enforced_full_nonce = nonces_to_full_nonce(account_nonce, deployment_nonce);
log::debug!(
log::info!(
"👷 Nonces for address {:?} have been set to {}",
address,
nonce
Expand Down
4 changes: 4 additions & 0 deletions src/http_fork_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ impl ForkSource for HttpForkSource {
.read()
.map(|guard| guard.get_transaction(&hash).cloned())
{
log::debug!("using cached transaction for {hash}");
return Ok(Some(transaction));
}

Expand Down Expand Up @@ -97,6 +98,7 @@ impl ForkSource for HttpForkSource {
.read()
.map(|guard| guard.get_block_raw_transactions(&number).cloned())
{
log::debug!("using cached raw transactions for block {block_number}");
return Ok(transaction);
}

Expand Down Expand Up @@ -131,6 +133,7 @@ impl ForkSource for HttpForkSource {
.read()
.map(|guard| guard.get_block(&hash, full_transactions).cloned())
{
log::debug!("using cached block for {hash}");
return Ok(Some(block));
}

Expand Down Expand Up @@ -167,6 +170,7 @@ impl ForkSource for HttpForkSource {
.and_then(|hash| guard.get_block(hash, full_transactions).cloned())
})
}) {
log::debug!("using cached block for {block_number}");
return Ok(Some(block));
}

Expand Down

0 comments on commit 2afe4eb

Please sign in to comment.