Skip to content

Commit 2194fef

Browse files
apollo_network_benchmark: added network manager creation logic (#10611)
1 parent 0869cd5 commit 2194fef

File tree

4 files changed

+81
-2
lines changed

4 files changed

+81
-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: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,73 @@
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+
24+
let mut network_config = NetworkConfig {
25+
port: args.runner.p2p_port,
26+
secret_key: Some(peer_private_key.to_vec().into()),
27+
..Default::default()
28+
};
29+
30+
// disable Kademlia discovery
31+
network_config.discovery_config.heartbeat_interval = Duration::from_secs(u64::MAX);
32+
33+
if !args.runner.bootstrap.is_empty() {
34+
let bootstrap_peers: Vec<Multiaddr> = args
35+
.runner
36+
.bootstrap
37+
.iter()
38+
.map(|s| Multiaddr::from_str(s.trim()).unwrap())
39+
.collect();
40+
network_config.bootstrap_peer_multiaddr = Some(bootstrap_peers);
41+
}
42+
43+
network_config
44+
}
45+
1446
/// Creates a new BroadcastNetworkStressTestNode instance
1547
pub async fn new(args: NodeArgs) -> Self {
16-
Self { args }
48+
// Create network configuration
49+
let network_config = Self::create_network_config(&args);
50+
// Create network manager
51+
let network_manager = NetworkManager::new(network_config, None, None);
52+
Self { args, network_manager: Some(network_manager) }
53+
}
54+
55+
/// Starts the network manager in the background
56+
pub async fn start_network_manager(&mut self) -> BoxFuture<'static, ()> {
57+
let network_manager =
58+
self.network_manager.take().expect("Network manager should be available");
59+
async move {
60+
let _ = network_manager.run().await;
61+
}
62+
.boxed()
1763
}
1864

1965
/// Gets all the tasks that need to be run
2066
async fn get_tasks(&mut self) -> Vec<BoxFuture<'static, ()>> {
21-
Vec::new()
67+
let mut tasks = Vec::new();
68+
tasks.push(self.start_network_manager().await);
69+
70+
tasks
2271
}
2372

2473
/// Unified run function that handles both simple and network reset modes
@@ -65,3 +114,17 @@ pub async fn race_and_kill_tasks(spawned_tasks: Vec<JoinHandle<()>>) {
65114
task.abort();
66115
}
67116
}
117+
118+
fn create_peer_private_key(peer_index: u64) -> [u8; 32] {
119+
let array = peer_index.to_le_bytes();
120+
assert_eq!(array.len(), 8);
121+
let mut private_key = [0u8; 32];
122+
private_key[0..8].copy_from_slice(&array);
123+
124+
// Log the secret key
125+
let peer_private_key_hex =
126+
private_key.iter().map(|byte| format!("{byte:02x}")).collect::<String>();
127+
info!("Secret Key: {peer_private_key_hex:#?}");
128+
129+
private_key
130+
}

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)