Skip to content

Commit f18b7fa

Browse files
apollo_network: added network manager register_sqmr_protocol_client docs (#8969)
* apollo_network: added misconduct score AddAssign docs * apollo_network: added network manager NetworkError docs * apollo_network: added network manager GenericNetworkManager docs * apollo_network: added network manager GenericNetworkManager::run docs * apollo_network: added network manager register_sqmr_protocol_server docs * apollo_network: added network manager register_sqmr_protocol_client docs
1 parent 8ab1314 commit f18b7fa

File tree

1 file changed

+102
-5
lines changed
  • crates/apollo_network/src/network_manager

1 file changed

+102
-5
lines changed

crates/apollo_network/src/network_manager/mod.rs

Lines changed: 102 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -355,11 +355,108 @@ impl<SwarmT: SwarmTrait> GenericNetworkManager<SwarmT> {
355355
SqmrServerReceiver { receiver: Box::new(inbound_payload_receiver) }
356356
}
357357

358-
/// Register a new subscriber for sending a single query and receiving multiple responses.
359-
/// Panics if the given protocol is already subscribed.
360-
// TODO(Shahak): Support multiple protocols where they're all different versions of the same
361-
// protocol.
362-
// TODO(Shahak): Seperate query and response buffer sizes.
358+
/// Registers this node as a client for an SQMR protocol.
359+
///
360+
/// This method sets up the node to send queries to other peers for a specific protocol
361+
/// and receive multiple responses back. The protocol follows the Single Query Multiple
362+
/// Response (SQMR) pattern.
363+
///
364+
/// # Type Parameters
365+
///
366+
/// * `Query` - The type of queries this client will send
367+
/// * `Response` - The type of responses this client expects to receive
368+
///
369+
/// # Arguments
370+
///
371+
/// * `protocol` - The protocol identifier (e.g., "/starknet/blocks/1.0.0")
372+
/// * `buffer_size` - Size of the internal buffer for responses
373+
///
374+
/// # Returns
375+
///
376+
/// An [`SqmrClientSender`] that can be used to send queries and receive responses.
377+
///
378+
/// # Panics
379+
///
380+
/// Panics if the protocol has already been registered as a client.
381+
///
382+
/// # Examples
383+
///
384+
/// ```rust,no_run
385+
/// use apollo_network::network_manager::NetworkManager;
386+
/// use apollo_network::NetworkConfig;
387+
/// use futures::StreamExt;
388+
/// use serde::{Deserialize, Serialize};
389+
///
390+
/// // Example types for demonstration
391+
/// #[derive(Serialize, Deserialize, Clone)]
392+
/// struct BlockQuery {
393+
/// start_height: u64,
394+
/// end_height: u64,
395+
/// }
396+
/// #[derive(Serialize, Deserialize, Clone)]
397+
/// struct Block {
398+
/// height: u64,
399+
/// hash: String,
400+
/// }
401+
///
402+
/// impl From<BlockQuery> for Vec<u8> {
403+
/// fn from(query: BlockQuery) -> Vec<u8> {
404+
/// query.start_height.to_string().into_bytes()
405+
/// }
406+
/// }
407+
/// impl TryFrom<Vec<u8>> for Block {
408+
/// type Error = Box<dyn std::error::Error + Send + Sync>;
409+
/// fn try_from(bytes: Vec<u8>) -> Result<Self, Self::Error> {
410+
/// Ok(Block { height: 1000, hash: String::from_utf8(bytes)? })
411+
/// }
412+
/// }
413+
///
414+
/// // Helper function
415+
/// fn process_block(block: Block) {
416+
/// println!("Processing block {}", block.height);
417+
/// }
418+
///
419+
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
420+
/// let mut network_manager = NetworkManager::new(NetworkConfig::default(), None, None);
421+
///
422+
/// // Register as a client for block requests
423+
/// let mut client = network_manager.register_sqmr_protocol_client::<BlockQuery, Block>(
424+
/// "/starknet/blocks/1.0.0".to_string(),
425+
/// 100, // buffer size
426+
/// );
427+
///
428+
/// // Send a query and process responses
429+
/// let query = BlockQuery { start_height: 1000, end_height: 1010 };
430+
/// let mut response_manager = client.send_new_query(query).await?;
431+
///
432+
/// while let Some(response_result) = response_manager.next().await {
433+
/// match response_result {
434+
/// Ok(block) => {
435+
/// // Process received block
436+
/// process_block(block);
437+
/// }
438+
/// Err(e) => {
439+
/// // Handle error, optionally report peer
440+
/// eprintln!("Invalid response: {}", e);
441+
/// response_manager.report_peer();
442+
/// break;
443+
/// }
444+
/// }
445+
/// }
446+
/// # Ok(())
447+
/// # }
448+
/// ```
449+
///
450+
/// # Protocol Registration
451+
///
452+
/// Once registered, the client can send queries to any peer that supports this protocol.
453+
/// Each query creates a new session that can receive multiple responses from the target peer.
454+
///
455+
/// # Buffer Management
456+
///
457+
/// The `buffer_size` parameter controls how many responses can be buffered per query
458+
/// before backpressure is applied. This should be sized according to the expected
459+
/// number of responses per query for the specific protocol.
363460
pub fn register_sqmr_protocol_client<Query, Response>(
364461
&mut self,
365462
protocol: String,

0 commit comments

Comments
 (0)