Skip to content

Commit ee07397

Browse files
authored
refactor(l1): bring all test modules in the rpc crate into a single test_util module (#5213)
**Motivation** Stop having all the dummy() functions that create non-working components spread all around the code. **Description** This pr brings all test utils in the rpc crate into a single test_utils module, including all the dummy() functions that were spread all around the code. This pr only focuses in the rpc crate to tackle issue #5070 since it's the only one that had a test_utils module and required having a dummy instance of a component to be created for testing. Closes #5070
1 parent 7ff0ff2 commit ee07397

File tree

17 files changed

+385
-393
lines changed

17 files changed

+385
-393
lines changed

crates/l2/tee/quote-gen/Cargo.lock

Lines changed: 17 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/networking/p2p/network.rs

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -93,36 +93,6 @@ impl P2PContext {
9393
tx_broadcaster,
9494
})
9595
}
96-
97-
#[cfg(any(test, feature = "test-utils"))]
98-
/// Creates a dummy P2PContext for tests
99-
/// This should only be used in tests as it won't be able to connect to the p2p network
100-
pub async fn dummy(peer_table: PeerTable) -> P2PContext {
101-
use ethrex_storage::EngineType;
102-
103-
let storage = Store::new("./temp", EngineType::InMemory).expect("Failed to create Store");
104-
let blockchain: Arc<Blockchain> = Arc::new(Blockchain::default_with_store(storage.clone()));
105-
let local_node = Node::from_enode_url(
106-
"enode://d860a01f9722d78051619d1e2351aba3f43f943f6f00718d1b9baa4101932a1f5011f16bb2b1bb35db20d6fe28fa0bf09636d26a87d31de9ec6203eeedb1f666@18.138.108.67:30303",
107-
).expect("Bad enode url");
108-
let (channel_broadcast_send_end, _) =
109-
tokio::sync::broadcast::channel::<(tokio::task::Id, Arc<Message>)>(100000);
110-
P2PContext {
111-
tracker: TaskTracker::default(),
112-
signer: SecretKey::from_byte_array(&[0xcd; 32]).expect("32 bytes, within curve order"),
113-
table: peer_table.clone(),
114-
storage,
115-
blockchain: blockchain.clone(),
116-
broadcast: channel_broadcast_send_end,
117-
local_node: local_node.clone(),
118-
client_version: "".to_string(),
119-
#[cfg(feature = "l2")]
120-
based_context: None,
121-
tx_broadcaster: TxBroadcaster::spawn(peer_table.clone(), blockchain, 1000)
122-
.await
123-
.expect("Failed to spawn tx broadcaster"),
124-
}
125-
}
12696
}
12797

12898
#[derive(Debug, thiserror::Error)]

crates/networking/p2p/peer_handler.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#[cfg(any(test, feature = "test-utils"))]
2-
use crate::discv4::peer_table::TARGET_PEERS;
31
use crate::rlpx::initiator::RLPxInitiator;
42
use crate::{
53
discv4::peer_table::{PeerData, PeerTable, PeerTableError},
@@ -154,14 +152,6 @@ impl PeerHandler {
154152
}
155153
}
156154

157-
#[cfg(any(test, feature = "test-utils"))]
158-
/// Creates a dummy PeerHandler for tests where interacting with peers is not needed
159-
/// This should only be used in tests as it won't be able to interact with the node's connected peers
160-
pub async fn dummy() -> PeerHandler {
161-
let peer_table = PeerTable::spawn(TARGET_PEERS);
162-
PeerHandler::new(peer_table.clone(), RLPxInitiator::dummy(peer_table).await)
163-
}
164-
165155
async fn make_request(
166156
// TODO: We should receive the PeerHandler (or self) instead, but since it is not yet spawnified it cannot be shared
167157
// Fix this to avoid passing the PeerTable as a parameter

crates/networking/p2p/rlpx/initiator.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ use spawned_concurrency::{
1515
use std::time::Duration;
1616
use tracing::{debug, error, info};
1717

18-
#[cfg(any(test, feature = "test-utils"))]
19-
use crate::discv4::peer_table::PeerTable;
20-
2118
#[derive(Debug, thiserror::Error)]
2219
pub enum RLPxInitiatorError {
2320
#[error(transparent)]
@@ -78,15 +75,6 @@ impl RLPxInitiator {
7875
self.lookup_interval
7976
}
8077
}
81-
82-
#[cfg(any(test, feature = "test-utils"))]
83-
/// Creates a dummy GenServer for tests
84-
/// This should only be used in tests
85-
pub async fn dummy(peer_table: PeerTable) -> GenServerHandle<RLPxInitiator> {
86-
info!("Starting RLPx Initiator");
87-
let state = RLPxInitiator::new(P2PContext::dummy(peer_table).await);
88-
RLPxInitiator::start(state)
89-
}
9078
}
9179

9280
#[derive(Debug, Clone)]

crates/networking/p2p/sync.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ use ethrex_common::{
2424
types::{AccountState, Block, BlockHash, BlockHeader},
2525
};
2626
use ethrex_rlp::{decode::RLPDecode, encode::RLPEncode, error::RLPDecodeError};
27-
#[cfg(any(test, feature = "test-utils"))]
28-
use ethrex_storage::EngineType;
2927
use ethrex_storage::{STATE_TRIE_SEGMENTS, Store, error::StoreError};
3028
use ethrex_trie::trie_sorted::TrieGenerationError;
3129
use ethrex_trie::{Trie, TrieError};
@@ -122,22 +120,6 @@ impl Syncer {
122120
}
123121
}
124122

125-
#[cfg(any(test, feature = "test-utils"))]
126-
/// Creates a dummy Syncer for tests where syncing is not needed
127-
/// This should only be used in tests as it won't be able to connect to the p2p network
128-
pub async fn dummy() -> Self {
129-
Self {
130-
snap_enabled: Arc::new(AtomicBool::new(false)),
131-
peers: PeerHandler::dummy().await,
132-
// This won't be used
133-
cancel_token: CancellationToken::new(),
134-
blockchain: Arc::new(Blockchain::default_with_store(
135-
Store::new("", EngineType::InMemory).expect("Failed to start Store Engine"),
136-
)),
137-
datadir: ".".into(),
138-
}
139-
}
140-
141123
/// Starts a sync cycle, updating the state with all blocks between the current head and the sync head
142124
/// Will perform either full or snap sync depending on the manager's `snap_mode`
143125
/// In full mode, all blocks will be fetched via p2p eth requests and executed to rebuild the state

crates/networking/p2p/sync_manager.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,6 @@ impl SyncManager {
6767
sync_manager
6868
}
6969

70-
#[cfg(any(test, feature = "test-utils"))]
71-
/// Creates a dummy SyncManager for tests where syncing is not needed
72-
/// This should only be used in tests as it won't be able to connect to the p2p network
73-
pub async fn dummy() -> Self {
74-
Self {
75-
snap_enabled: Arc::new(AtomicBool::new(false)),
76-
syncer: Arc::new(Mutex::new(Syncer::dummy().await)),
77-
last_fcu_head: Arc::new(Mutex::new(H256::zero())),
78-
store: Store::new("temp.db", ethrex_storage::EngineType::InMemory)
79-
.expect("Failed to start Storage Engine"),
80-
}
81-
}
82-
8370
/// Sets the latest fcu head and starts the next sync cycle if the syncer is currently inactive
8471
pub fn sync_to_head(&self, fcu_head: H256) {
8572
self.set_head(fcu_head);

crates/networking/rpc/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ envy = "0.4.2"
4646
thiserror.workspace = true
4747
secp256k1.workspace = true
4848
uuid.workspace = true
49+
hex-literal = "0.4.1"
4950

5051
[dev-dependencies]
5152
hex-literal = "0.4.1"

crates/networking/rpc/eth/filter.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,14 +263,13 @@ mod tests {
263263
logs::{AddressFilter, LogsFilter, TopicFilter},
264264
},
265265
rpc::{FILTER_DURATION, map_http_requests},
266-
utils::test_utils::{self, default_context_with_storage, start_test_api},
266+
test_utils::{TEST_GENESIS, default_context_with_storage, start_test_api},
267267
};
268268
use crate::{types::block_identifier::BlockIdentifier, utils::RpcRequest};
269269
use ethrex_common::types::Genesis;
270270
use ethrex_storage::{EngineType, Store};
271271

272272
use serde_json::{Value, json};
273-
use test_utils::TEST_GENESIS;
274273

275274
#[tokio::test]
276275
async fn filter_request_smoke_test_valid_params() {

crates/networking/rpc/eth/gas_price.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ impl RpcHandler for GasPrice {
5757
#[cfg(test)]
5858
mod tests {
5959
use super::GasPrice;
60-
use crate::eth::test_utils::{
60+
use crate::test_utils::{
6161
BASE_PRICE_IN_WEI, add_eip1559_tx_blocks, add_legacy_tx_blocks, add_mixed_tx_blocks,
6262
setup_store,
6363
};
6464

65-
use crate::utils::test_utils::default_context_with_storage;
65+
use crate::test_utils::default_context_with_storage;
6666
use crate::{
6767
rpc::{RpcHandler, map_http_requests},
6868
utils::{RpcRequest, parse_json_hex},

crates/networking/rpc/eth/gas_tip_estimator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ impl Default for GasTipEstimator {
125125
#[cfg(test)]
126126
mod tests {
127127
use super::*;
128-
use crate::eth::test_utils::{
128+
use crate::test_utils::{
129129
BASE_PRICE_IN_WEI, add_eip1559_tx_blocks, add_empty_blocks, add_legacy_tx_blocks,
130130
add_mixed_tx_blocks, setup_store,
131131
};

0 commit comments

Comments
 (0)