Skip to main content

TreeState

Struct TreeState 

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

Local spanning tree state for a node.

Contains this node’s declaration, coordinates, and view of peers’ tree positions. State is bounded by O(P × D) where P is peer count and D is tree depth.

Implementations§

Source§

impl TreeState

Source

pub fn new(my_node_addr: NodeAddr) -> Self

Create initial tree state for a node (as root candidate).

The node starts as its own root until it learns of a smaller node_addr. Initial sequence is 1 per protocol spec; timestamp is current Unix time.

Source

pub fn my_node_addr(&self) -> &NodeAddr

Get this node’s NodeAddr.

Source

pub fn my_declaration(&self) -> &ParentDeclaration

Get this node’s current declaration.

Source

pub fn my_coords(&self) -> &TreeCoordinate

Get this node’s current coordinates.

Source

pub fn root(&self) -> &NodeAddr

Get the current root.

Source

pub fn is_root(&self) -> bool

Check if this node is currently the root.

Source

pub fn peer_coords(&self, peer_id: &NodeAddr) -> Option<&TreeCoordinate>

Get coordinates for a peer, if known.

Source

pub fn peer_declaration(&self, peer_id: &NodeAddr) -> Option<&ParentDeclaration>

Get declaration for a peer, if known.

Source

pub fn peer_count(&self) -> usize

Number of known peers.

Source

pub fn peer_ids(&self) -> impl Iterator<Item = &NodeAddr>

Iterate over all peer node IDs.

Source

pub fn update_peer( &mut self, declaration: ParentDeclaration, ancestry: TreeCoordinate, ) -> bool

Add or update a peer’s tree state.

Returns true if the state was updated (new or fresher declaration).

Source

pub fn remove_peer(&mut self, peer_id: &NodeAddr)

Remove a peer from the tree state.

Source

pub fn set_parent( &mut self, parent_id: NodeAddr, sequence: u64, timestamp: u64, ) -> bool

Update this node’s parent selection.

Call this when switching parents. Updates the declaration and coordinates. Returns true if flap dampening was just engaged due to this switch. Only records a flap when the parent actually changes.

Source

pub fn recompute_coords(&mut self)

Update this node’s coordinates based on current parent’s ancestry.

Defensive: if extending the parent’s ancestry would put self at the minimum (because self is smaller than the parent’s root), the declaration is demoted to self-root in place. The caller is responsible for re-signing the declaration after this call (do set_parent → recompute_coords → sign_declaration, not set_parent → sign_declaration → recompute_coords).

Source

pub fn smallest_visible_root(&self) -> Option<NodeAddr>

Smallest root_id visible across known peers.

Source

pub fn should_be_root(&self) -> bool

Whether this node should be the tree root: either there are no peers, or our NodeAddr is <= every visible root.

Source

pub fn become_root(&mut self)

Promote self to root with an incremented sequence number.

Caller must sign_declaration afterwards before sending the result.

Source

pub fn distance_to_peer(&self, peer_id: &NodeAddr) -> Option<usize>

Calculate tree distance to a peer.

Source

pub fn find_next_hop(&self, dest_coords: &TreeCoordinate) -> Option<NodeAddr>

Find the best next hop toward a destination using greedy tree routing.

Returns the peer that minimizes tree distance to the destination, but only if that peer is strictly closer than we are (prevents routing loops at local minima). Tie-breaks equal distance by smallest node_addr.

Returns None if:

  • No peers have coordinates
  • Destination is in a different tree (different root)
  • No peer is closer to the destination than we are
Source

pub fn set_parent_hysteresis(&mut self, hysteresis: f64)

Set the parent hysteresis factor (0.0-1.0).

Source

pub fn set_hold_down(&mut self, secs: u64)

Set the hold-down duration after parent switches.

Source

pub fn set_flap_dampening( &mut self, threshold: u32, window_secs: u64, dampening_secs: u64, )

Configure flap dampening parameters.

Source

pub fn record_parent_switch(&mut self) -> bool

Record a parent switch for flap detection. Returns true if dampening was just engaged.

Source

pub fn is_flap_dampened(&self) -> bool

Check if flap dampening is currently active.

Source

pub fn evaluate_parent( &self, peer_costs: &HashMap<NodeAddr, f64>, ) -> Option<NodeAddr>

Evaluate whether to switch parents based on current peer tree state.

Uses effective_depth (depth + link_cost) for parent comparison. peer_costs maps each peer’s NodeAddr to its link cost (from local MMP measurements). Missing entries default to 1.0 (optimistic).

Returns Some(peer_node_addr) if a parent switch is recommended, or None if the current parent is adequate.

Source

pub fn handle_parent_lost( &mut self, peer_costs: &HashMap<NodeAddr, f64>, ) -> bool

Handle loss of current parent.

Tries to find an alternative parent among remaining peers. If none available, becomes its own root (increments sequence).

Returns true if the tree state changed (caller should re-announce).

Source

pub fn sign_declaration(&mut self, identity: &Identity) -> Result<(), TreeError>

Sign this node’s declaration with the given identity.

The identity’s node_addr must match this TreeState’s node_addr.

Source

pub fn is_declaration_signed(&self) -> bool

Check if this node’s declaration is signed.

Trait Implementations§

Source§

impl Debug for TreeState

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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.

Source§

impl<T> Instrument for T

Source§

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

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

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
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.
Source§

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

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

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
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

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