Skip to content

Commit 23d102c

Browse files
authored
test: reuse cache for fork mode tests (#4897)
1 parent be20ca8 commit 23d102c

File tree

9 files changed

+44
-67
lines changed

9 files changed

+44
-67
lines changed

.github/workflows/hardhat-core-ci.yml

+7
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,13 @@ jobs:
197197

198198
- uses: Swatinem/rust-cache@v2
199199

200+
- name: Cache EDR RPC cache
201+
uses: actions/cache@v2
202+
with:
203+
path: |
204+
**/edr-cache
205+
key: edr-rs-rpc-cache-v1
206+
200207
- name: Run cargo test
201208
uses: actions-rs/cargo@v1
202209
env:

Cargo.lock

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/edr_eth/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ url = "2.4.1"
3333
[dev-dependencies]
3434
anyhow = "1.0.75"
3535
assert-json-diff = "2.0.2"
36+
edr_defaults = { version = "0.2.0-dev", path = "../edr_defaults" }
3637
lazy_static = "1.4.0"
3738
mockito = { version = "1.0.2", default-features = false }
3839
paste = { version = "1.0.14", default-features = false }

crates/edr_eth/tests/receipt.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
#[cfg(feature = "test-remote")]
22
mod remote {
3-
use lazy_static::lazy_static;
43
use serial_test::serial;
5-
use tempfile::TempDir;
6-
7-
lazy_static! {
8-
// Use same cache dir for all tests
9-
static ref CACHE_DIR: TempDir = TempDir::new().unwrap();
10-
}
114

125
macro_rules! impl_test_remote_block_receipt_root {
136
($(
@@ -21,7 +14,7 @@ mod remote {
2114
use edr_eth::{remote::{RpcClient, PreEip1898BlockSpec}, trie::ordered_trie_root};
2215
use edr_test_utils::env::get_alchemy_url;
2316

24-
let client = RpcClient::new(&get_alchemy_url(), CACHE_DIR.path().into(), None).expect("url ok");
17+
let client = RpcClient::new(&get_alchemy_url(), edr_defaults::CACHE_DIR.into(), None).expect("url ok");
2518

2619
let block = client
2720
.get_block_by_number_with_transaction_data(PreEip1898BlockSpec::Number($block_number))

crates/edr_evm/tests/blockchain.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,7 @@ use edr_evm::{
1515
test_utils::dummy_eip155_transaction,
1616
HashSet, LocalBlock, Log, SpecId, SyncBlock,
1717
};
18-
use lazy_static::lazy_static;
1918
use serial_test::serial;
20-
use tempfile::TempDir;
21-
22-
lazy_static! {
23-
// Use same cache dir for all tests
24-
static ref CACHE_DIR: TempDir = TempDir::new().unwrap();
25-
}
2619

2720
#[cfg(feature = "test-remote")]
2821
const REMOTE_BLOCK_NUMBER: u64 = 10_496_585;
@@ -48,8 +41,8 @@ async fn create_forked_dummy_blockchain(
4841
use edr_test_utils::env::get_alchemy_url;
4942
use parking_lot::Mutex;
5043

51-
let cache_dir = CACHE_DIR.path().into();
52-
let rpc_client = RpcClient::new(&get_alchemy_url(), cache_dir, None).expect("url ok");
44+
let rpc_client =
45+
RpcClient::new(&get_alchemy_url(), edr_defaults::CACHE_DIR.into(), None).expect("url ok");
5346

5447
Box::new(
5548
ForkedBlockchain::new(

crates/edr_evm/tests/transaction.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@ mod alchemy {
1414
};
1515
use edr_evm::ExecutableTransaction;
1616
use edr_test_utils::env::get_alchemy_url;
17-
use tempfile::TempDir;
1817

19-
let tempdir = TempDir::new().unwrap();
20-
let client = RpcClient::new(&get_alchemy_url(), tempdir.path().into(), None).expect("url ok");
18+
let client = RpcClient::new(&get_alchemy_url(), edr_defaults::CACHE_DIR.into(), None).expect("url ok");
2119

2220
let block = client
2321
.get_block_by_number_with_transaction_data(PreEip1898BlockSpec::Number($block_number))

crates/edr_provider/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ lru = "0.12.2"
2929
anyhow = "1.0.75"
3030
edr_test_utils = { version = "0.2.0-dev", path = "../edr_test_utils" }
3131
paste = { version = "1.0.14", default-features = false }
32+
serial_test = "2.0.0"
3233
tempfile = "3.7.1"
3334

3435
[build-dependencies]

crates/edr_provider/src/data.rs

+24-39
Original file line numberDiff line numberDiff line change
@@ -2365,7 +2365,6 @@ pub(crate) mod test_utils {
23652365
use anyhow::anyhow;
23662366
use edr_eth::transaction::{Eip155TransactionRequest, TransactionKind, TransactionRequest};
23672367
use edr_test_utils::env::get_alchemy_url;
2368-
use tempfile::TempDir;
23692368

23702369
use super::*;
23712370
use crate::{
@@ -2397,8 +2396,6 @@ pub(crate) mod test_utils {
23972396
}
23982397

23992398
pub(crate) struct ProviderTestFixture {
2400-
// We need to keep the tempdir and runtime alive for the duration of the test
2401-
_cache_dir: TempDir,
24022399
_runtime: runtime::Runtime,
24032400
pub config: ProviderConfig,
24042401
pub provider_data: ProviderData<Infallible>,
@@ -2425,21 +2422,19 @@ pub(crate) mod test_utils {
24252422
}
24262423
});
24272424

2428-
let cache_dir = TempDir::new()?;
2429-
let config = create_test_config_with_fork(cache_dir.path().to_path_buf(), fork);
2425+
let config = create_test_config_with_fork(fork);
24302426

24312427
let runtime = runtime::Builder::new_multi_thread()
24322428
.worker_threads(1)
24332429
.enable_all()
24342430
.thread_name("provider-data-test")
24352431
.build()?;
24362432

2437-
Self::new(runtime, cache_dir, config)
2433+
Self::new(runtime, config)
24382434
}
24392435

24402436
pub fn new(
24412437
runtime: tokio::runtime::Runtime,
2442-
cache_dir: TempDir,
24432438
mut config: ProviderConfig,
24442439
) -> anyhow::Result<Self> {
24452440
let logger = Box::<NoopLogger>::default();
@@ -2466,7 +2461,6 @@ pub(crate) mod test_utils {
24662461
provider_data.impersonate_account(impersonated_account);
24672462

24682463
Ok(Self {
2469-
_cache_dir: cache_dir,
24702464
_runtime: runtime,
24712465
config,
24722466
provider_data,
@@ -2539,13 +2533,14 @@ mod tests {
25392533
use edr_evm::{hex, MineOrdering, RemoteBlock, TransactionError};
25402534
use edr_test_utils::env::get_alchemy_url;
25412535
use serde_json::json;
2542-
use tempfile::TempDir;
25432536

25442537
use super::{test_utils::ProviderTestFixture, *};
25452538
use crate::{
25462539
data::inspector::tests::{deploy_console_log_contract, ConsoleLogTransaction},
25472540
requests::eth::resolve_call_request,
2548-
test_utils::{create_test_config_with_fork, one_ether, FORK_BLOCK_NUMBER},
2541+
test_utils::{
2542+
create_test_config, create_test_config_with_fork, one_ether, FORK_BLOCK_NUMBER,
2543+
},
25492544
MemPoolConfig, MiningConfig, ProviderConfig,
25502545
};
25512546

@@ -3052,8 +3047,7 @@ mod tests {
30523047

30533048
#[test]
30543049
fn mine_and_commit_block_fifo_ordering() -> anyhow::Result<()> {
3055-
let cache_dir = TempDir::new()?;
3056-
let default_config = create_test_config_with_fork(cache_dir.path().to_path_buf(), None);
3050+
let default_config = create_test_config();
30573051
let config = ProviderConfig {
30583052
mining: MiningConfig {
30593053
mem_pool: MemPoolConfig {
@@ -3070,7 +3064,7 @@ mod tests {
30703064
.thread_name("provider-data-test")
30713065
.build()?;
30723066

3073-
let mut fixture = ProviderTestFixture::new(runtime, cache_dir, config)?;
3067+
let mut fixture = ProviderTestFixture::new(runtime, config)?;
30743068

30753069
let transaction1 = fixture.signed_dummy_transaction(0, None)?;
30763070
let transaction2 = fixture.signed_dummy_transaction(1, None)?;
@@ -3144,8 +3138,7 @@ mod tests {
31443138

31453139
#[test]
31463140
fn mine_and_commit_block_rewards_miner() -> anyhow::Result<()> {
3147-
let cache_dir = TempDir::new()?;
3148-
let default_config = create_test_config_with_fork(cache_dir.path().to_path_buf(), None);
3141+
let default_config = create_test_config();
31493142
let config = ProviderConfig {
31503143
hardfork: SpecId::BERLIN,
31513144
..default_config
@@ -3157,7 +3150,7 @@ mod tests {
31573150
.thread_name("provider-data-test")
31583151
.build()?;
31593152

3160-
let mut fixture = ProviderTestFixture::new(runtime, cache_dir, config)?;
3153+
let mut fixture = ProviderTestFixture::new(runtime, config)?;
31613154

31623155
let miner = fixture.provider_data.beneficiary;
31633156
let previous_miner_balance = fixture
@@ -3559,16 +3552,11 @@ mod tests {
35593552
.thread_name("provider-data-test")
35603553
.build()?;
35613554

3562-
let cache_dir = TempDir::new()?;
3563-
3564-
let default_config = create_test_config_with_fork(
3565-
cache_dir.path().to_path_buf(),
3566-
Some(ForkConfig {
3567-
json_rpc_url: get_alchemy_url(),
3568-
block_number: Some(EIP_1559_ACTIVATION_BLOCK),
3569-
http_headers: None,
3570-
}),
3571-
);
3555+
let default_config = create_test_config_with_fork(Some(ForkConfig {
3556+
json_rpc_url: get_alchemy_url(),
3557+
block_number: Some(EIP_1559_ACTIVATION_BLOCK),
3558+
http_headers: None,
3559+
}));
35723560

35733561
let config = ProviderConfig {
35743562
block_gas_limit: 1_000_000,
@@ -3579,7 +3567,7 @@ mod tests {
35793567
..default_config
35803568
};
35813569

3582-
let mut fixture = ProviderTestFixture::new(runtime, cache_dir, config)?;
3570+
let mut fixture = ProviderTestFixture::new(runtime, config)?;
35833571

35843572
let default_call = CallRequest {
35853573
from: Some(fixture.nth_local_account(0)?),
@@ -3677,6 +3665,7 @@ mod tests {
36773665
)+) => {
36783666
$(
36793667
paste::item! {
3668+
#[serial_test::serial]
36803669
#[test]
36813670
fn [<full_block_ $name>]() -> anyhow::Result<()> {
36823671
let url = $url;
@@ -3727,15 +3716,20 @@ mod tests {
37273716
}
37283717

37293718
fn run_full_block(url: String, block_number: u64, chain_id: u64) -> anyhow::Result<()> {
3719+
let default_config = create_test_config_with_fork(Some(ForkConfig {
3720+
json_rpc_url: url.clone(),
3721+
block_number: Some(block_number - 1),
3722+
http_headers: None,
3723+
}));
3724+
37303725
let runtime = runtime::Builder::new_multi_thread()
37313726
.worker_threads(1)
37323727
.enable_all()
37333728
.thread_name("provider-data-test")
37343729
.build()?;
37353730

3736-
let cache_dir = TempDir::new()?;
37373731
let replay_block = {
3738-
let rpc_client = RpcClient::new(&url, cache_dir.path().to_path_buf(), None)?;
3732+
let rpc_client = RpcClient::new(&url, default_config.cache_dir.clone(), None)?;
37393733

37403734
let block = runtime.block_on(rpc_client.get_block_by_number_with_transaction_data(
37413735
PreEip1898BlockSpec::Number(block_number),
@@ -3751,15 +3745,6 @@ mod tests {
37513745
.hardfork_at_block_number(block_number)
37523746
.ok_or(anyhow!("Unsupported block number"))?;
37533747

3754-
let default_config = create_test_config_with_fork(
3755-
cache_dir.path().to_path_buf(),
3756-
Some(ForkConfig {
3757-
json_rpc_url: url,
3758-
block_number: Some(block_number - 1),
3759-
http_headers: None,
3760-
}),
3761-
);
3762-
37633748
let replay_header = replay_block.header();
37643749
let block_gas_limit = replay_header.gas_limit;
37653750

@@ -3781,7 +3766,7 @@ mod tests {
37813766
..default_config
37823767
};
37833768

3784-
let mut fixture = ProviderTestFixture::new(runtime, cache_dir, config)?;
3769+
let mut fixture = ProviderTestFixture::new(runtime, config)?;
37853770

37863771
for transaction in replay_block.transactions() {
37873772
fixture

crates/edr_provider/src/test_utils.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{convert::Infallible, path::PathBuf, time::SystemTime};
1+
use std::{convert::Infallible, time::SystemTime};
22

33
use edr_eth::{
44
block::BlobGas, signature::secret_key_from_str, trie::KECCAK_NULL_RLP, Address, HashMap,
@@ -19,18 +19,15 @@ pub const TEST_SECRET_KEY_SIGN_TYPED_DATA_V4: &str =
1919
pub const FORK_BLOCK_NUMBER: u64 = 18_725_000;
2020

2121
/// Constructs a test config with a single account with 1 ether
22-
pub fn create_test_config(cache_dir: PathBuf) -> ProviderConfig {
23-
create_test_config_with_fork(cache_dir, None)
22+
pub fn create_test_config() -> ProviderConfig {
23+
create_test_config_with_fork(None)
2424
}
2525

2626
pub fn one_ether() -> U256 {
2727
U256::from(10).pow(U256::from(18))
2828
}
2929

30-
pub fn create_test_config_with_fork(
31-
cache_dir: PathBuf,
32-
fork: Option<ForkConfig>,
33-
) -> ProviderConfig {
30+
pub fn create_test_config_with_fork(fork: Option<ForkConfig>) -> ProviderConfig {
3431
ProviderConfig {
3532
accounts: vec![
3633
AccountConfig {
@@ -65,7 +62,7 @@ pub fn create_test_config_with_fork(
6562
min_gas_price: U256::ZERO,
6663
mining: MiningConfig::default(),
6764
network_id: 123,
68-
cache_dir,
65+
cache_dir: edr_defaults::CACHE_DIR.into(),
6966
}
7067
}
7168

0 commit comments

Comments
 (0)