Skip to content

Commit 54c2611

Browse files
apollo_network_benchmark: added network manager creation logic
1 parent a65b9ee commit 54c2611

File tree

4 files changed

+77
-2
lines changed

4 files changed

+77
-2
lines changed

Cargo.lock

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

crates/apollo_network_benchmark/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ license-file.workspace = true
99
testing = []
1010

1111
[dependencies]
12+
apollo_network.workspace = true
1213
clap = { workspace = true, features = ["derive", "env"] }
1314
futures.workspace = true
1415
lazy_static.workspace = true
16+
libp2p = { workspace = true, features = ["identify"] }
1517
metrics-exporter-prometheus.workspace = true
1618
tokio = { workspace = true, features = ["full", "sync"] }
1719
tokio-metrics = { workspace = true, features = ["metrics-rs-integration", "rt"] }

crates/apollo_network_benchmark/src/bin/broadcast_network_stress_test_node/stress_test_node.rs

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,75 @@
1+
use std::str::FromStr;
12
use std::time::Duration;
23

4+
use apollo_network::network_manager::NetworkManager;
5+
use apollo_network::NetworkConfig;
36
use apollo_network_benchmark::node_args::NodeArgs;
47
use futures::future::{select_all, BoxFuture};
8+
use futures::FutureExt;
9+
use libp2p::Multiaddr;
510
use tokio::task::JoinHandle;
611
use tracing::{info, warn};
712

813
/// The main stress test node that manages network communication and monitoring
914
pub struct BroadcastNetworkStressTestNode {
1015
args: NodeArgs,
16+
network_manager: Option<NetworkManager>,
1117
}
1218

1319
impl BroadcastNetworkStressTestNode {
20+
/// Creates network configuration from arguments
21+
fn create_network_config(args: &NodeArgs) -> NetworkConfig {
22+
let peer_private_key = create_peer_private_key(args.runner.id);
23+
let peer_private_key_hex =
24+
peer_private_key.iter().map(|byte| format!("{byte:02x}")).collect::<String>();
25+
info!("Secret Key: {peer_private_key_hex:#?}");
26+
27+
let mut network_config = NetworkConfig {
28+
port: args.runner.p2p_port,
29+
secret_key: Some(peer_private_key.to_vec().into()),
30+
..Default::default()
31+
};
32+
33+
network_config.discovery_config.heartbeat_interval = Duration::from_secs(99999999);
34+
35+
if !args.runner.bootstrap.is_empty() {
36+
let bootstrap_peers: Vec<Multiaddr> = args
37+
.runner
38+
.bootstrap
39+
.iter()
40+
.map(|s| Multiaddr::from_str(s.trim()).unwrap())
41+
.collect();
42+
network_config.bootstrap_peer_multiaddr = Some(bootstrap_peers);
43+
}
44+
45+
network_config
46+
}
47+
1448
/// Creates a new BroadcastNetworkStressTestNode instance
1549
pub async fn new(args: NodeArgs) -> Self {
16-
Self { args }
50+
// Create network configuration
51+
let network_config = Self::create_network_config(&args);
52+
// Create network manager
53+
let network_manager = NetworkManager::new(network_config, None, None);
54+
Self { args, network_manager: Some(network_manager) }
55+
}
56+
57+
/// Starts the network manager in the background
58+
pub async fn start_network_manager(&mut self) -> BoxFuture<'static, ()> {
59+
let network_manager =
60+
self.network_manager.take().expect("Network manager should be available");
61+
async move {
62+
let _ = network_manager.run().await;
63+
}
64+
.boxed()
1765
}
1866

1967
/// Gets all the tasks that need to be run
2068
async fn get_tasks(&mut self) -> Vec<BoxFuture<'static, ()>> {
21-
Vec::new()
69+
let mut tasks = Vec::new();
70+
tasks.push(self.start_network_manager().await);
71+
72+
tasks
2273
}
2374

2475
/// Unified run function that handles both simple and network reset modes
@@ -65,3 +116,11 @@ pub async fn race_and_kill_tasks(spawned_tasks: Vec<JoinHandle<()>>) {
65116
task.abort();
66117
}
67118
}
119+
120+
fn create_peer_private_key(peer_index: u64) -> [u8; 32] {
121+
let array = peer_index.to_le_bytes();
122+
assert_eq!(array.len(), 8);
123+
let mut private_key = [0u8; 32];
124+
private_key[0..8].copy_from_slice(&array);
125+
private_key
126+
}

crates/apollo_network_benchmark/src/node_args.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,21 @@ use clap::Parser;
44
#[command(version, about, long_about = None)]
55
/// Arguments from the runner, not meant to be set by the user.
66
pub struct RunnerArgs {
7+
/// ID for Prometheus logging
8+
#[arg(short, long, env)]
9+
pub id: u64,
10+
711
/// The port to run the Prometheus metrics server on
812
#[arg(long, env)]
913
pub metric_port: u16,
14+
15+
/// The port to run the P2P network on
16+
#[arg(short, env, long)]
17+
pub p2p_port: u16,
18+
19+
/// The addresses of the bootstrap peers (can specify multiple)
20+
#[arg(long, env, value_delimiter = ',')]
21+
pub bootstrap: Vec<String>,
1022
}
1123

1224
#[derive(Parser, Debug, Clone)]

0 commit comments

Comments
 (0)