pub fn pick_node(
        &self,
        subnet: SubnetId,
        version: RegistryVersion,
    ) -> Result<(NodeId, NodeRecord), Error> {
        // Retrieve `subnet`'s nodes.
        let nodes = self
            .registry
            .get_node_ids_on_subnet(subnet, version)
            .map_err(|e| Error::RegistryGetSubnetInfoFailed(subnet, e))?
            .filter(|nodes| !nodes.is_empty())
            .ok_or(Error::MissingSubnet(subnet))?;

        // Compute the individual and total weight of all nodes with explicit weights
        // (nodes of operators for which we've recorded at least one roundtrip time).
        let mut node_weights = vec![0; nodes.len()];
        let mut total_weight = 0;
        let mut weighted_nodes = 0;
        for (i, node) in nodes.iter().enumerate() {
            if let Some(node_operator) =
                get_node_operator_id(node, self.registry.as_ref(), &version, &self.log)
            {
                if let Some(node_weight) = self.weight(&node_operator) {
                    node_weights[i] = node_weight;
                    total_weight += node_weight;
                    weighted_nodes += 1;
                }
            }
        }

        // Mean weight to assign to nodes that we don't have explicit weights for.
        let mean_weight = if weighted_nodes > 0 {
            total_weight / weighted_nodes
        } else {
            1
        };

        // Cumulative node weights, to be used for weighted random selection.
        let cumulative_weights: Vec<u64> = node_weights
            .into_iter()
            .map(|weight| if weight != 0 { weight } else { mean_weight })
            .scan(0, |accumulator, weight| {
                (*accumulator) += weight;
                Some(*accumulator)
            })
            .collect();
        let total_weight = *cumulative_weights.last().unwrap();

        // Pick a random node by weight.
        let node_index = cumulative_weights
            .binary_search(&(self.gen_range)(1, total_weight + 1))
            .unwrap_or_else(|e| e);

        let node = nodes[node_index];
        let node_record = self
            .registry
            .get_transport_info(node, version)
            .map_err(|e| Error::RegistryGetNodeInfoFailed(node, e))?;

        match node_record {
            Some(node_record) => Ok((node, node_record)),
            None => Err(Error::MissingXNetEndpoint(node)),
        }
    }
/// Picks a random node on `subnet` (as defined at registry version
/// `version`) weighted by proximity (nodes belonging to operators with
/// lower RTT are picked with higher probability).
///
/// E.g. given  mean RTTs of `[0.1s, 0.5s. 1s]` the computed weights
/// (`[10_000, 2_000, 1_000]`) would result in cumulative weights `[10_000,
/// 12_000, 13_000]`. We then use a random value in the `1..=13_000` range
/// to select one of the nodes:
///
///  * a random value in the `1..=10_000` range will select the first node;
///  * a value in the `10_001..=12_000` range will select the second node;
///  * and a value in the `12_001..=13_000` range will result in selecting
///    the third node.
Picks a random node on `subnet` (as defined at registry version
`version`) weighted by proximity (nodes belonging to operators with
lower RTT are picked with higher probability).




Picks a random node on ` subnet ` ( as defined at registry version ` version ` ) weighted by proximity ( nodes belonging to operators with lower RTT are picked with higher probability ) .
pub fn pick_node(
        &self,
        subnet: SubnetId,
        version: RegistryVersion,
    ) -> Result<(NodeId, NodeRecord), Error> {
        // Retrieve `subnet`'s nodes.
        let nodes = self
            .registry
            .get_node_ids_on_subnet(subnet, version)
            .map_err(|e| Error::RegistryGetSubnetInfoFailed(subnet, e))?
            .filter(|nodes| !nodes.is_empty())
            .ok_or(Error::MissingSubnet(subnet))?;
        // Compute the individual and total weight of all nodes with explicit weights
        // (nodes of operators for which we've recorded at least one roundtrip time).
        let mut node_weights = vec![0; nodes.len()];
        let mut total_weight = 0;
        let mut weighted_nodes = 0;
        for (i, node) in nodes.iter().enumerate() {
            if let Some(node_operator) =
                get_node_operator_id(node, self.registry.as_ref(), &version, &self.log)
            {
                if let Some(node_weight) = self.weight(&node_operator) {
                    node_weights[i] = node_weight;
                    total_weight += node_weight;
                    weighted_nodes += 1;
                }
            }
        }
        // Mean weight to assign to nodes that we don't have explicit weights for.
        let mean_weight = if weighted_nodes > 0 {
            total_weight / weighted_nodes
        } else {
            1
        };
        // Cumulative node weights, to be used for weighted random selection.
        let cumulative_weights: Vec<u64> = node_weights
            .into_iter()
            .map(|weight| if weight != 0 { weight } else { mean_weight })
            .scan(0, |accumulator, weight| {
                (*accumulator) += weight;
                Some(*accumulator)
            })
            .collect();
        let total_weight = *cumulative_weights.last().unwrap();
        // Pick a random node by weight.
        let node_index = cumulative_weights
            .binary_search(&(self.gen_range)(1, total_weight + 1))
            .unwrap_or_else(|e| e);
        let node = nodes[node_index];
        let node_record = self
            .registry
            .get_transport_info(node, version)
            .map_err(|e| Error::RegistryGetNodeInfoFailed(node, e))?;
        match node_record {
            Some(node_record) => Ok((node, node_record)),
            None => Err(Error::MissingXNetEndpoint(node)),
        }
    }
pub fn pick_node ( & self , subnet : SubnetId , version : RegistryVersion , ) -> Result < ( NodeId , NodeRecord ) , Error > { // Retrieve `subnet`'s nodes. let nodes = self . registry . get_node_ids_on_subnet ( subnet , version ) . map_err ( | e | Error :: RegistryGetSubnetInfoFailed ( subnet , e ) ) ? . filter ( | nodes | ! nodes . is_empty ( ) ) . ok_or ( Error :: MissingSubnet ( subnet ) ) ? ; // Compute the individual and total weight of all nodes with explicit weights // (nodes of operators for which we've recorded at least one roundtrip time). let mut node_weights = vec ! [ 0 nodes len ( ) ] ; let mut total_weight = 0 ; let mut weighted_nodes = 0 ; for ( i , node ) in nodes . iter ( ) . enumerate ( ) { if let Some ( node_operator ) = get_node_operator_id ( node , self . registry . as_ref ( ) , & version , & self . log ) { if let Some ( node_weight ) = self . weight ( & node_operator ) { node_weights [ i ] = node_weight ; total_weight += node_weight ; weighted_nodes += 1 ; } } } // Mean weight to assign to nodes that we don't have explicit weights for. let mean_weight = if weighted_nodes > 0 { total_weight / weighted_nodes } else { 1 } ; // Cumulative node weights, to be used for weighted random selection. let cumulative_weights : Vec < u64 > = node_weights . into_iter ( ) . map ( | weight | if weight != 0 { weight } else { mean_weight } ) . scan ( 0 , | accumulator , weight | { ( * accumulator ) += weight ; Some ( * accumulator ) } ) . collect ( ) ; let total_weight = * cumulative_weights . last ( ) . unwrap ( ) ; // Pick a random node by weight. let node_index = cumulative_weights . binary_search ( & ( self . gen_range ) ( 1 , total_weight + 1 ) ) . unwrap_or_else ( | e | e ) ; let node = nodes [ node_index ] ; let node_record = self . registry . get_transport_info ( node , version ) . map_err ( | e | Error :: RegistryGetNodeInfoFailed ( node , e ) ) ? ; match node_record { Some ( node_record ) => Ok ( ( node , node_record ) ) , None => Err ( Error :: MissingXNetEndpoint ( node ) ) , } }
Picks a random node on `subnet` (as defined at registry version
`version`) weighted by proximity (nodes belonging to operators with
lower RTT are picked with higher probability).
Picks a random node on ` subnet ` ( as defined at registry version ` version ` ) weighted by proximity ( nodes belonging to operators with lower RTT are picked with higher probability ) .
&self
subnet
SubnetId
version
RegistryVersion
&self
subnet
SubnetId
version
RegistryVersion