Skip to content

Commit

Permalink
implementing network testing
Browse files Browse the repository at this point in the history
  • Loading branch information
netsirius committed Dec 17, 2023
1 parent f145b71 commit 3672377
Show file tree
Hide file tree
Showing 6 changed files with 640 additions and 8 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions crates/core/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ pub struct NodeConfig {
pub peer_id: PeerId,
// optional local info, in case this is an initial bootstrap node
/// IP to bind to the listener
pub(crate) local_ip: Option<IpAddr>,
pub local_ip: Option<IpAddr>,
/// socket port to bind to the listener
pub(crate) local_port: Option<u16>,
pub local_port: Option<u16>,
/// IP dialers should connect to
pub(crate) public_ip: Option<IpAddr>,
/// socket port dialers should connect to
Expand Down
16 changes: 15 additions & 1 deletion crates/core/src/node/testing_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,9 @@ impl SimNetwork {
.max_hops_to_live(self.ring_max_htl)
.rnd_if_htl_above(self.rnd_if_htl_above)
.max_number_of_connections(self.max_connections)
.with_key(pair.public().into());
.with_key(pair.public().into())
.with_ip(Ipv6Addr::LOCALHOST)
.with_port(get_free_port().unwrap());

let peer = PeerId::from(id);
self.event_listener.add_node(label.clone(), peer);
Expand Down Expand Up @@ -594,6 +596,18 @@ impl SimNetwork {
peers
}

pub fn build_p2p_peers(&mut self) -> Vec<(NodeLabel, NodeConfig)> {
let gw = self.gateways.drain(..).map(|(n, c)| (n, c.label));
let mut peers = vec![];
for (builder, label) in gw.chain(self.nodes.drain(..)).collect::<Vec<_>>() {
self.labels.push((label.clone(), builder.peer_key));
peers.push((label, builder.config));
}
self.labels.sort_by(|(a, _), (b, _)| a.cmp(b));
peers.sort_by(|(a, _), (b, _)| a.cmp(b));
peers
}

pub fn get_locations_by_node(&self) -> HashMap<NodeLabel, PeerKeyLocation> {
let mut locations_by_node: HashMap<NodeLabel, PeerKeyLocation> = HashMap::new();

Expand Down
1 change: 1 addition & 0 deletions crates/fdev/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ toml = { version = "0.8", features = ["default", "preserve_order"] }
tracing = { workspace = true }
tracing-subscriber = { workspace = true, features = ["env-filter", "fmt"] }
xz2 = "0.1"
libp2p-identity = { features = ["ed25519", "rand"], version = "0.2.7" }

# internal
freenet = { path = "../core" }
Expand Down
6 changes: 3 additions & 3 deletions crates/fdev/src/testing.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{path::PathBuf, time::Duration};

use anyhow::Error;
use freenet::dev_tool::SimNetwork;
use freenet::dev_tool::{PeerCliConfig, SimNetwork};

mod multiple_process;
mod network;
Expand Down Expand Up @@ -109,7 +109,7 @@ pub enum TestMode {
/// Runs multiple simulated nodes in multiple processes.
MultiProcess(multiple_process::MultiProcessConfig),
/// Runs multiple simulated nodes in multiple processes and multiple machines.
Network,
Network(network::NetworkProcessConfig),
}

pub(crate) async fn test_framework(base_config: TestConfig) -> anyhow::Result<(), Error> {
Expand All @@ -125,7 +125,7 @@ pub(crate) async fn test_framework(base_config: TestConfig) -> anyhow::Result<()
let res = match &base_config.command {
TestMode::SingleProcess => single_process::run(&base_config).await,
TestMode::MultiProcess(config) => multiple_process::run(&base_config, config).await,
TestMode::Network => network::run(&base_config).await,
TestMode::Network(config) => network::run(&base_config, config).await,
};
if let Some(server) = server {
server.abort();
Expand Down
Loading

0 comments on commit 3672377

Please sign in to comment.