Skip to content

Commit 23c111c

Browse files
apollo_network: added network manager NetworkManager docs (#8971)
1 parent e19eb5a commit 23c111c

File tree

1 file changed

+82
-0
lines changed
  • crates/apollo_network/src/network_manager

1 file changed

+82
-0
lines changed

crates/apollo_network/src/network_manager/mod.rs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,9 +1089,69 @@ fn send_now<Item>(
10891089
}
10901090
}
10911091

1092+
/// Concrete network manager implementation using libp2p Swarm.
1093+
///
1094+
/// This is the main network manager type used in production. It wraps
1095+
/// [`GenericNetworkManager`] with a concrete libp2p swarm implementation.
10921096
pub type NetworkManager = GenericNetworkManager<Swarm<mixed_behaviour::MixedBehaviour>>;
10931097

10941098
impl NetworkManager {
1099+
/// Creates a new network manager with the specified configuration.
1100+
///
1101+
/// This method initializes all networking components including:
1102+
/// - libp2p swarm with TCP transport, DNS resolution, and security protocols
1103+
/// - SQMR protocol for query-response communication
1104+
/// - GossipSub for message broadcasting
1105+
/// - Kademlia DHT for peer discovery
1106+
/// - Peer management and reputation systems
1107+
///
1108+
/// # Arguments
1109+
///
1110+
/// * `config` - Network configuration parameters
1111+
/// * `node_version` - Optional version string for identification (e.g., "apollo-node/1.0.0")
1112+
/// * `metrics` - Optional metrics collection instance
1113+
///
1114+
/// # Returns
1115+
///
1116+
/// A configured [`NetworkManager`] ready to run.
1117+
///
1118+
/// # Examples
1119+
///
1120+
/// ```rust,no_run
1121+
/// use apollo_network::metrics::NetworkMetrics;
1122+
/// use apollo_network::network_manager::NetworkManager;
1123+
/// use apollo_network::NetworkConfig;
1124+
/// use starknet_api::core::ChainId;
1125+
///
1126+
/// let config = NetworkConfig { port: 10000, chain_id: ChainId::Mainnet, ..Default::default() };
1127+
///
1128+
/// let network_manager = NetworkManager::new(
1129+
/// config,
1130+
/// Some("my-starknet-node/1.0.0".to_string()),
1131+
/// None, // metrics
1132+
/// );
1133+
/// ```
1134+
///
1135+
/// # Transport Configuration
1136+
///
1137+
/// The network manager is configured with:
1138+
/// - **TCP Transport**: Primary transport protocol
1139+
/// - **DNS Resolution**: For resolving domain names in multiaddresses
1140+
/// - **Noise Protocol**: For connection encryption and authentication
1141+
/// - **Yamux**: For connection multiplexing
1142+
///
1143+
/// # Identity Generation
1144+
///
1145+
/// If a secret key is provided in the config, it's used to deterministically
1146+
/// generate the peer ID. Otherwise, a random Ed25519 keypair is generated.
1147+
///
1148+
/// # Panics
1149+
///
1150+
/// Panics if:
1151+
/// - The provided secret key is invalid
1152+
/// - Failed to bind to the specified port
1153+
/// - Transport configuration fails
1154+
/// - The advertised multiaddress contains a different peer ID than generated
10951155
pub fn new(
10961156
config: NetworkConfig,
10971157
node_version: Option<String>,
@@ -1167,6 +1227,28 @@ impl NetworkManager {
11671227
)
11681228
}
11691229

1230+
/// Returns the local peer ID as a string.
1231+
///
1232+
/// The peer ID is derived from the node's cryptographic identity and serves
1233+
/// as a unique identifier in the network. Other nodes use this ID to
1234+
/// establish connections and route messages.
1235+
///
1236+
/// # Returns
1237+
///
1238+
/// A string representation of the local peer ID in the format expected
1239+
/// by libp2p multiaddresses (e.g., "12D3KooWQYHvEJzuBP...").
1240+
///
1241+
/// # Examples
1242+
///
1243+
/// ```rust,no_run
1244+
/// use apollo_network::network_manager::NetworkManager;
1245+
/// use apollo_network::NetworkConfig;
1246+
///
1247+
/// let network_manager = NetworkManager::new(NetworkConfig::default(), None, None);
1248+
///
1249+
/// let peer_id = network_manager.get_local_peer_id();
1250+
/// println!("Local peer ID: {}", peer_id);
1251+
/// ```
11701252
pub fn get_local_peer_id(&self) -> String {
11711253
self.swarm.local_peer_id().to_string()
11721254
}

0 commit comments

Comments
 (0)