feat(net): add test-utils and NoopNetwork (#1204)

This commit is contained in:
Matthias Seitz
2023-02-07 16:37:33 +01:00
committed by GitHub
parent 27b07f57f0
commit df6e3143c3
3 changed files with 70 additions and 3 deletions

View File

@ -19,3 +19,6 @@ serde = { version = "1.0", features = ["derive"] }
async-trait = "0.1" async-trait = "0.1"
thiserror = "1.0.37" thiserror = "1.0.37"
tokio = { version = "1.21.2", features = ["sync"] } tokio = { version = "1.21.2", features = ["sync"] }
[features]
test-utils = []

View File

@ -10,18 +10,22 @@
//! Provides abstractions for the reth-network crate. //! Provides abstractions for the reth-network crate.
use async_trait::async_trait; use async_trait::async_trait;
use reth_eth_wire::DisconnectReason;
use reth_primitives::{NodeRecord, PeerId, H256, U256}; use reth_primitives::{NodeRecord, PeerId, H256, U256};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::net::SocketAddr; use std::net::SocketAddr;
pub use error::NetworkError;
pub use reputation::{Reputation, ReputationChangeKind};
/// Network Error /// Network Error
pub mod error; pub mod error;
/// Reputation score /// Reputation score
pub mod reputation; pub mod reputation;
pub use error::NetworkError; #[cfg(feature = "test-utils")]
pub use reputation::{Reputation, ReputationChangeKind}; /// Implementation of network traits for testing purposes.
use reth_eth_wire::DisconnectReason; pub mod test_utils;
/// Provides general purpose information about the network. /// Provides general purpose information about the network.
#[async_trait] #[async_trait]

View File

@ -0,0 +1,60 @@
use crate::{
EthProtocolInfo, NetworkError, NetworkInfo, NetworkStatus, PeerKind, Peers, PeersInfo,
ReputationChangeKind,
};
use async_trait::async_trait;
use reth_eth_wire::{DisconnectReason, ProtocolVersion};
use reth_primitives::{rpc::Chain::Mainnet, NodeRecord, PeerId};
use std::net::{IpAddr, SocketAddr};
/// A type that implements all network trait that does nothing.
///
/// Intended for testing purposes where network is not used.
#[derive(Debug, Clone, Default)]
pub struct NoopNetwork;
#[async_trait]
impl NetworkInfo for NoopNetwork {
fn local_addr(&self) -> SocketAddr {
(IpAddr::from(std::net::Ipv4Addr::UNSPECIFIED), 30303).into()
}
async fn network_status(&self) -> Result<NetworkStatus, NetworkError> {
Ok(NetworkStatus {
client_version: "reth-test".to_string(),
protocol_version: ProtocolVersion::V5 as u64,
eth_protocol_info: EthProtocolInfo {
difficulty: Default::default(),
head: Default::default(),
network: 1,
genesis: Default::default(),
},
})
}
fn chain_id(&self) -> u64 {
Mainnet.into()
}
}
impl PeersInfo for NoopNetwork {
fn num_connected_peers(&self) -> usize {
0
}
fn local_node_record(&self) -> NodeRecord {
NodeRecord::new(self.local_addr(), PeerId::random())
}
}
impl Peers for NoopNetwork {
fn add_peer_kind(&self, _peer: PeerId, _kind: PeerKind, _addr: SocketAddr) {}
fn remove_peer(&self, _peer: PeerId, _kind: PeerKind) {}
fn disconnect_peer(&self, _peer: PeerId) {}
fn disconnect_peer_with_reason(&self, _peer: PeerId, _reason: DisconnectReason) {}
fn reputation_change(&self, _peer_id: PeerId, _kind: ReputationChangeKind) {}
}