diff --git a/Cargo.lock b/Cargo.lock index c23bd61159..596efcd86d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2917,6 +2917,27 @@ dependencies = [ "typenum", ] +[[package]] +name = "csv" +version = "1.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acdc4883a9c96732e4733212c01447ebd805833b7275a73ca3ee080fd77afdaf" +dependencies = [ + "csv-core", + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "csv-core" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5efa2b3d7902f4b634a20cae3c9c4e6209dc4779feb6863329607560143efa70" +dependencies = [ + "memchr", +] + [[package]] name = "ctr" version = "0.9.2" @@ -7359,6 +7380,8 @@ dependencies = [ "async-stream", "async-trait", "bytes", + "clap", + "csv", "deadqueue", "defaultmap", "derive_more 0.99.18", diff --git a/Cargo.toml b/Cargo.toml index 53bfa5ade9..085177cd2d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -106,6 +106,7 @@ clap = "4.5.4" colored = "2.1.0" const_format = "0.2.30" criterion = "0.5.1" +csv = "1.3.1" deadqueue = "0.2.4" defaultmap = "0.5.0" derive_more = "0.99.17" diff --git a/crates/papyrus_network/Cargo.toml b/crates/papyrus_network/Cargo.toml index 04a22212a7..8a1497b0de 100644 --- a/crates/papyrus_network/Cargo.toml +++ b/crates/papyrus_network/Cargo.toml @@ -12,6 +12,8 @@ testing = [] async-stream.workspace = true async-trait.workspace = true bytes.workspace = true +clap.workspace = true +csv.workspace = true derive_more.workspace = true futures.workspace = true lazy_static.workspace = true @@ -54,5 +56,8 @@ tokio = { workspace = true, features = ["full", "sync", "test-util"] } tokio-stream.workspace = true void.workspace = true +[build-dependencies] + + [lints] workspace = true diff --git a/crates/papyrus_network/src/bin/network_stress_test/bootstrap_test_config.json b/crates/papyrus_network/src/bin/network_stress_test/bootstrap_test_config.json new file mode 100644 index 0000000000..a5fdce5ea8 --- /dev/null +++ b/crates/papyrus_network/src/bin/network_stress_test/bootstrap_test_config.json @@ -0,0 +1,102 @@ +{ + "buffer_size": { + "description": "The buffer size for the network receiver.", + "privacy": "Public", + "value": 1000 + }, + "message_size": { + "description": "The size of the payload for the test messages.", + "privacy": "Public", + "value": 1000 + }, + "network_config.advertised_multiaddr": { + "description": "The external address other peers see this node. If this is set, the node will not try to find out which addresses it has and will write this address as external instead", + "privacy": "Public", + "value": "" + }, + "network_config.advertised_multiaddr.#is_none": { + "description": "Flag for an optional field.", + "privacy": "TemporaryValue", + "value": true + }, + "network_config.bootstrap_peer_multiaddr": { + "description": "The multiaddress of the peer node. It should include the peer's id. For more info: https://docs.libp2p.io/concepts/fundamentals/peers/", + "privacy": "Public", + "value": "" + }, + "network_config.bootstrap_peer_multiaddr.#is_none": { + "description": "Flag for an optional field.", + "privacy": "TemporaryValue", + "value": true + }, + "network_config.chain_id": { + "description": "The chain to follow. For more details see https://docs.starknet.io/documentation/architecture_and_concepts/Blocks/transactions/#chain-id.", + "privacy": "Public", + "value": "SN_MAIN" + }, + "network_config.discovery_config.bootstrap_dial_retry_config.base_delay_millis": { + "description": "The base delay in milliseconds for the exponential backoff strategy.", + "privacy": "Public", + "value": 2 + }, + "network_config.discovery_config.bootstrap_dial_retry_config.factor": { + "description": "The factor for the exponential backoff strategy.", + "privacy": "Public", + "value": 5 + }, + "network_config.discovery_config.bootstrap_dial_retry_config.max_delay_seconds": { + "description": "The maximum delay in seconds for the exponential backoff strategy.", + "privacy": "Public", + "value": 5 + }, + "network_config.discovery_config.heartbeat_interval": { + "description": "The interval between each discovery (Kademlia) query in milliseconds.", + "privacy": "Public", + "value": 100 + }, + "network_config.idle_connection_timeout": { + "description": "Amount of time in seconds that a connection with no active sessions will stay alive.", + "privacy": "Public", + "value": 120 + }, + "network_config.peer_manager_config.malicious_timeout_seconds": { + "description": "The duration in seconds a peer is blacklisted after being marked as malicious.", + "privacy": "Public", + "value": 31536000 + }, + "network_config.peer_manager_config.unstable_timeout_millis": { + "description": "The duration in milliseconds a peer blacklisted after being reported as unstable.", + "privacy": "Public", + "value": 1000 + }, + "network_config.quic_port": { + "description": "The port that the node listens on for incoming quic connections.", + "privacy": "Public", + "value": 10001 + }, + "network_config.secret_key": { + "description": "The secret key used for building the peer id. If it's an empty string a random one will be used.", + "privacy": "Private", + "value": "0x0000000000000000000000000000000000000000000000000000000000000000" + }, + "network_config.session_timeout": { + "description": "Maximal time in seconds that each session can take before failing on timeout.", + "privacy": "Public", + "value": 120 + }, + "network_config.tcp_port": { + "description": "The port that the node listens on for incoming tcp connections.", + "privacy": "Public", + "value": 10000 + }, + "num_messages": { + "description": "The amount of messages to send and receive.", + "privacy": "Public", + "value": 10000 + }, + "output_path": { + "description": "The path of the output file.", + "privacy": "Public", + "value": "crates/papyrus_network/src/bin/network_stress_test/bootstrap_output.csv" + } +} \ No newline at end of file diff --git a/crates/papyrus_network/src/bin/network_stress_test/converters.rs b/crates/papyrus_network/src/bin/network_stress_test/converters.rs index d3b8ae628b..b0eca85211 100644 --- a/crates/papyrus_network/src/bin/network_stress_test/converters.rs +++ b/crates/papyrus_network/src/bin/network_stress_test/converters.rs @@ -1,21 +1,37 @@ +use std::mem::size_of; +use std::str::FromStr; use std::time::{Duration, SystemTime}; -struct StressTestMessage { - id: u32, - payload: Vec, - time: SystemTime, +use libp2p::PeerId; + +pub const METADATA_SIZE: usize = size_of::() + size_of::() + size_of::() + 38; + +#[derive(Debug, Clone)] +pub struct StressTestMessage { + pub id: u32, + pub payload: Vec, + pub time: SystemTime, + pub peer_id: String, +} + +impl StressTestMessage { + pub fn new(id: u32, payload: Vec, peer_id: String) -> Self { + StressTestMessage { id, payload, time: SystemTime::now(), peer_id } + } } impl From for Vec { fn from(value: StressTestMessage) -> Self { - let StressTestMessage { id, mut payload, time } = value; + let StressTestMessage { id, mut payload, time, peer_id } = value; let id = id.to_be_bytes().to_vec(); let time = time.duration_since(SystemTime::UNIX_EPOCH).unwrap(); let seconds = time.as_secs().to_be_bytes().to_vec(); let nanos = time.subsec_nanos().to_be_bytes().to_vec(); + let peer_id = PeerId::from_str(&peer_id).unwrap().to_bytes(); payload.extend(id); payload.extend(seconds); payload.extend(nanos); + payload.extend(peer_id); payload } } @@ -24,12 +40,13 @@ impl From> for StressTestMessage { // This auto implements TryFrom> for StressTestMessage fn from(mut value: Vec) -> Self { let vec_size = value.len(); - let payload_size = vec_size - 12; + let payload_size = vec_size - METADATA_SIZE; let id_and_time = value.split_off(payload_size); let id = u32::from_be_bytes(id_and_time[0..4].try_into().unwrap()); let seconds = u64::from_be_bytes(id_and_time[4..12].try_into().unwrap()); let nanos = u32::from_be_bytes(id_and_time[12..16].try_into().unwrap()); let time = SystemTime::UNIX_EPOCH + Duration::new(seconds, nanos); - StressTestMessage { id, payload: value, time } + let peer_id = PeerId::from_bytes(&id_and_time[16..]).unwrap().to_string(); + StressTestMessage { id, payload: value, time, peer_id } } } diff --git a/crates/papyrus_network/src/bin/network_stress_test/main.rs b/crates/papyrus_network/src/bin/network_stress_test/main.rs index 1591c86df9..546b84bc40 100644 --- a/crates/papyrus_network/src/bin/network_stress_test/main.rs +++ b/crates/papyrus_network/src/bin/network_stress_test/main.rs @@ -1,3 +1,89 @@ +use std::time::SystemTime; +use std::vec; + +use clap::Command; +use converters::{StressTestMessage, METADATA_SIZE}; +use futures::StreamExt; +use libp2p::gossipsub::Topic; +use papyrus_config::loading::load_and_process_config; +use papyrus_network::network_manager::{BroadcastTopicClientTrait, NetworkManager}; +use tokio::time::timeout; +use utils::{Record, TestConfig, BOOTSTRAP_CONFIG_FILE_PATH}; + mod converters; +mod utils; -fn main() {} +#[tokio::main] +async fn main() { + let args = std::env::args().collect::>(); + let default_path = BOOTSTRAP_CONFIG_FILE_PATH.to_string(); + let config_path = args.get(1).unwrap_or(&default_path); + let file = std::fs::File::open(config_path).unwrap(); + let TestConfig { network_config, buffer_size, message_size, num_messages, output_path } = + load_and_process_config(file, Command::new("Stress Test"), vec![]).unwrap(); + let mut network_manager = NetworkManager::new(network_config, None); + let peer_id = network_manager.get_local_peer_id(); + let mut network_channels = network_manager + .register_broadcast_topic::( + Topic::new("stress_test_topic".to_string()), + buffer_size, + ) + .unwrap(); + let mut output_vector = Vec::::new(); + tokio::select! { + _ = network_manager.run() => {} + _ = async { + let mut i = 0; + tokio::time::sleep(std::time::Duration::from_secs(20)).await; + loop { + let message = StressTestMessage::new(i, vec![0; message_size - METADATA_SIZE], peer_id.clone()); + network_channels.broadcast_topic_client.broadcast_message(message).await.unwrap(); + i += 1; + if i == num_messages { + println!("Finished sending messages"); + futures::future::pending::<()>().await; + } + } + } => {} + _ = async { + let mut i = 0; + loop { + let maybe_response = timeout( + std::time::Duration::from_secs(120), + network_channels.broadcasted_messages_receiver.next(), + ).await; + match maybe_response { + Err(_) => { + println!("Timeout on message {}", i); + break; + } + Ok(None) => break, + Ok(Some((received_message, _report_callback))) => { + let received_message = received_message.unwrap(); + output_vector.push(Record { + peer_id: received_message.peer_id, + id: received_message.id, + start_time: received_message.time, + end_time: SystemTime::now(), + duration: SystemTime::now() + .duration_since(received_message.time) + .unwrap() + .as_micros(), + }); + i += 1; + if i == num_messages * 4 { + tokio::time::sleep(std::time::Duration::from_secs(20)).await; + break; + } + } + } + } + } => { + println!("Finished receiving messages"); + let mut wtr = csv::Writer::from_path(output_path).unwrap(); + for record in output_vector { + wtr.serialize(record).unwrap(); + } + } + } +} diff --git a/crates/papyrus_network/src/bin/network_stress_test/test_config.json b/crates/papyrus_network/src/bin/network_stress_test/test_config.json new file mode 100644 index 0000000000..dc9852fe3b --- /dev/null +++ b/crates/papyrus_network/src/bin/network_stress_test/test_config.json @@ -0,0 +1,102 @@ +{ + "buffer_size": { + "description": "The buffer size for the network receiver.", + "privacy": "Public", + "value": 1000 + }, + "message_size": { + "description": "The size of the payload for the test messages.", + "privacy": "Public", + "value": 1000 + }, + "network_config.advertised_multiaddr": { + "description": "The external address other peers see this node. If this is set, the node will not try to find out which addresses it has and will write this address as external instead", + "privacy": "Public", + "value": "" + }, + "network_config.advertised_multiaddr.#is_none": { + "description": "Flag for an optional field.", + "privacy": "TemporaryValue", + "value": true + }, + "network_config.bootstrap_peer_multiaddr": { + "description": "The multiaddress of the peer node. It should include the peer's id. For more info: https://docs.libp2p.io/concepts/fundamentals/peers/", + "privacy": "Public", + "value": "/ip4/10.20.20.26/tcp/10000/p2p/12D3KooWDpJ7As7BWAwRMfu1VU2WCqNjvq387JEYKDBj4kx6nXTN" + }, + "network_config.bootstrap_peer_multiaddr.#is_none": { + "description": "Flag for an optional field.", + "privacy": "TemporaryValue", + "value": false + }, + "network_config.chain_id": { + "description": "The chain to follow. For more details see https://docs.starknet.io/documentation/architecture_and_concepts/Blocks/transactions/#chain-id.", + "privacy": "Public", + "value": "SN_MAIN" + }, + "network_config.discovery_config.bootstrap_dial_retry_config.base_delay_millis": { + "description": "The base delay in milliseconds for the exponential backoff strategy.", + "privacy": "Public", + "value": 2 + }, + "network_config.discovery_config.bootstrap_dial_retry_config.factor": { + "description": "The factor for the exponential backoff strategy.", + "privacy": "Public", + "value": 5 + }, + "network_config.discovery_config.bootstrap_dial_retry_config.max_delay_seconds": { + "description": "The maximum delay in seconds for the exponential backoff strategy.", + "privacy": "Public", + "value": 5 + }, + "network_config.discovery_config.heartbeat_interval": { + "description": "The interval between each discovery (Kademlia) query in milliseconds.", + "privacy": "Public", + "value": 100 + }, + "network_config.idle_connection_timeout": { + "description": "Amount of time in seconds that a connection with no active sessions will stay alive.", + "privacy": "Public", + "value": 120 + }, + "network_config.peer_manager_config.malicious_timeout_seconds": { + "description": "The duration in seconds a peer is blacklisted after being marked as malicious.", + "privacy": "Public", + "value": 31536000 + }, + "network_config.peer_manager_config.unstable_timeout_millis": { + "description": "The duration in milliseconds a peer blacklisted after being reported as unstable.", + "privacy": "Public", + "value": 1000 + }, + "network_config.quic_port": { + "description": "The port that the node listens on for incoming quic connections.", + "privacy": "Public", + "value": 10003 + }, + "network_config.secret_key": { + "description": "The secret key used for building the peer id. If it's an empty string a random one will be used.", + "privacy": "Private", + "value": "" + }, + "network_config.session_timeout": { + "description": "Maximal time in seconds that each session can take before failing on timeout.", + "privacy": "Public", + "value": 120 + }, + "network_config.tcp_port": { + "description": "The port that the node listens on for incoming tcp connections.", + "privacy": "Public", + "value": 10002 + }, + "num_messages": { + "description": "The amount of messages to send and receive.", + "privacy": "Public", + "value": 10000 + }, + "output_path": { + "description": "The path of the output file.", + "privacy": "Public", + "value": "crates/papyrus_network/src/bin/network_stress_test/output.csv" + } +} \ No newline at end of file diff --git a/crates/papyrus_network/src/bin/network_stress_test/utils.rs b/crates/papyrus_network/src/bin/network_stress_test/utils.rs new file mode 100644 index 0000000000..734b512ec0 --- /dev/null +++ b/crates/papyrus_network/src/bin/network_stress_test/utils.rs @@ -0,0 +1,132 @@ +use std::collections::{BTreeMap, HashSet}; +use std::str::FromStr; +use std::time::{SystemTime, UNIX_EPOCH}; +use std::vec; + +use libp2p::identity::Keypair; +use libp2p::Multiaddr; +use papyrus_config::dumping::{append_sub_config_name, ser_param, SerializeConfig}; +use papyrus_config::{ParamPath, ParamPrivacyInput, SerializedParam}; +use papyrus_network::NetworkConfig; +use serde::{Deserialize, Serialize, Serializer}; + +pub const BOOTSTRAP_CONFIG_FILE_PATH: &str = + "crates/papyrus_network/src/bin/network_stress_test/bootstrap_test_config.json"; +pub const BOOTSTRAP_OUTPUT_FILE_PATH: &str = + "crates/papyrus_network/src/bin/network_stress_test/bootstrap_output.csv"; +pub const DEFAULT_CONFIG_FILE_PATH: &str = + "crates/papyrus_network/src/bin/network_stress_test/test_config.json"; +pub const DEFAULT_OUTPUT_FILE_PATH: &str = + "crates/papyrus_network/src/bin/network_stress_test/output.csv"; + +#[derive(Debug, Deserialize, Serialize)] +pub struct TestConfig { + pub network_config: NetworkConfig, + pub buffer_size: usize, + pub message_size: usize, + pub num_messages: u32, + pub output_path: String, +} + +impl SerializeConfig for TestConfig { + fn dump(&self) -> BTreeMap { + let mut config = BTreeMap::from_iter([ + ser_param( + "buffer_size", + &self.buffer_size, + "The buffer size for the network receiver.", + ParamPrivacyInput::Public, + ), + ser_param( + "message_size", + &self.message_size, + "The size of the payload for the test messages.", + ParamPrivacyInput::Public, + ), + ser_param( + "num_messages", + &self.num_messages, + "The amount of messages to send and receive.", + ParamPrivacyInput::Public, + ), + ser_param( + "output_path", + &self.output_path, + "The path of the output file.", + ParamPrivacyInput::Public, + ), + ]); + config.extend(append_sub_config_name(self.network_config.dump(), "network_config")); + config + } +} + +impl Default for TestConfig { + fn default() -> Self { + Self { + network_config: NetworkConfig::default(), + buffer_size: 1000, + message_size: 1000, + num_messages: 10000, + output_path: BOOTSTRAP_OUTPUT_FILE_PATH.to_string(), + } + } +} + +impl TestConfig { + #[allow(dead_code)] + pub fn create_config_files() { + let secret_key = vec![0; 32]; + let keypair = Keypair::ed25519_from_bytes(secret_key.clone()).unwrap(); + let peer_id = keypair.public().to_peer_id(); + + let _ = TestConfig { + network_config: NetworkConfig { + tcp_port: 10000, + quic_port: 10001, + secret_key: Some(secret_key), + ..Default::default() + }, + ..Default::default() + } + .dump_to_file(&vec![], &HashSet::new(), BOOTSTRAP_CONFIG_FILE_PATH); + let _ = TestConfig { + network_config: NetworkConfig { + tcp_port: 10002, + quic_port: 10003, + bootstrap_peer_multiaddr: Some( + Multiaddr::from_str(&format!("/ip4/127.0.0.1/tcp/10000/p2p/{}", peer_id)) + .unwrap(), + ), + ..Default::default() + }, + output_path: DEFAULT_OUTPUT_FILE_PATH.to_string(), + ..Default::default() + } + .dump_to_file(&vec![], &HashSet::new(), DEFAULT_CONFIG_FILE_PATH); + } +} + +#[derive(Debug, Deserialize, Serialize)] +pub struct Record { + pub peer_id: String, + pub id: u32, + #[serde(serialize_with = "serialize_system_time_as_u128_millis")] + pub start_time: SystemTime, + #[serde(serialize_with = "serialize_system_time_as_u128_millis")] + pub end_time: SystemTime, + pub duration: u128, +} + +pub fn serialize_system_time_as_u128_millis( + time: &SystemTime, + serializer: S, +) -> Result +where + S: Serializer, +{ + let duration_since_epoch = + time.duration_since(UNIX_EPOCH).map_err(serde::ser::Error::custom)?; + let millis = duration_since_epoch.as_millis(); + serializer.serialize_u128(millis) +} diff --git a/crates/papyrus_network/src/lib.rs b/crates/papyrus_network/src/lib.rs index 2d0954b128..136010f92e 100644 --- a/crates/papyrus_network/src/lib.rs +++ b/crates/papyrus_network/src/lib.rs @@ -107,7 +107,7 @@ impl SerializeConfig for NetworkConfig { ParamPrivacyInput::Private, )]); config.extend(ser_optional_param( - &self.bootstrap_peer_multiaddr, + &self.advertised_multiaddr, Multiaddr::empty(), "advertised_multiaddr", "The external address other peers see this node. If this is set, the node will not \