BwWeightedGenerator

Struct BwWeightedGenerator 

Source
pub struct BwWeightedGenerator { /* private fields */ }
Expand description

Bandwidth-weighted node generator.

Implements bandwidth-weighted random selection of relay nodes. Selection probability is proportional to each relay’s bandwidth weight.

§Weight Calculation

The weight for each relay is calculated as:

weight = measured_bandwidth × flag_weight_multiplier

Where flag_weight_multiplier depends on the relay’s flags and position:

FlagsMiddle PositionGuard PositionExit Position
Neither Guard nor ExitWmmWgmWem
Guard onlyWmgWggWeg
Exit onlyWmeWgeWee
Guard + ExitWmdWgdWed

§Selection Algorithm

  1. Filter routers through all restrictions
  2. Calculate weighted bandwidth for each remaining router
  3. Build cumulative weight distribution
  4. Generate random value in [0, total_weight)
  5. Select router where cumulative weight exceeds random value

§Example

use vanguards_rs::node_selection::{BwWeightedGenerator, FlagsRestriction, NodeRestrictionList, Position};

let restriction = FlagsRestriction::new(
    vec!["Fast".to_string(), "Stable".to_string(), "Valid".to_string()],
    vec!["Authority".to_string()],
);
let restrictions = NodeRestrictionList::new(vec![Box::new(restriction)]);

let generator = BwWeightedGenerator::new(routers, restrictions, weights, Position::Middle)?;
let selected = generator.generate()?;
println!("Selected relay: {}", selected.fingerprint);

§See Also

Implementations§

Source§

impl BwWeightedGenerator

Source

pub fn new( sorted_routers: Vec<RouterStatusEntry>, restrictions: NodeRestrictionList, bw_weights: HashMap<String, i64>, position: Position, ) -> Result<Self>

Creates a new bandwidth-weighted generator.

§Arguments
  • sorted_routers - Routers sorted by measured bandwidth (descending)
  • restrictions - Restrictions to filter routers
  • bw_weights - Consensus bandwidth weights (Wmm, Wmg, Wme, Wmd, etc.)
  • position - Circuit position for weight calculation
§Errors

Returns Error::NoNodesRemain if all routers are filtered out.

§Example
let generator = BwWeightedGenerator::new(routers, restrictions, weights, Position::Middle)?;
Source

pub fn repair_exits(&mut self)

Repairs exit node weights for rendezvous point selection.

Exit nodes got their weights set based on middle position, but they can still be used as rendezvous points in cannibalized circuits. This method recalculates their weights using exit position weights and tracks a separate exit_total.

Note: We deliberately don’t re-normalize weight_total since we don’t want to lower the upper bound of other nodes. But we do want a separate exit_total for use with Exit nodes.

Source

pub fn generate(&self) -> Result<&RouterStatusEntry>

Generates a randomly selected router using bandwidth-weighted selection.

Selection probability is proportional to each router’s bandwidth weight.

§Returns

A reference to the selected router.

§Errors

Returns Error::NoNodesRemain if the router list is empty or total weight is zero.

Source

pub fn weight_total(&self) -> f64

Returns the total weight of all routers.

Source

pub fn exit_total(&self) -> f64

Returns the total weight of exit-flagged routers.

Source

pub fn router_count(&self) -> usize

Returns the number of routers after restrictions.

Source

pub fn routers(&self) -> &[RouterStatusEntry]

Returns a reference to the filtered routers.

Source

pub fn node_weights(&self) -> &[f64]

Returns a reference to the node weights.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more