Trait NodeStorage

Source
pub trait NodeStorage: DynClone + Send {
    // Required methods
    fn add_or_update(&mut self, node: Node, verified: bool);
    fn clear(&mut self);
    fn count(&self) -> (usize, usize);
    fn get_all_unverified(&self) -> Vec<NodeWrapper>;
    fn get_all_verified(&self) -> Vec<NodeWrapper>;
    fn get_nearest_nodes(&self, id: &Id, exclude: Option<&Id>) -> Vec<Node>;
    fn prune(
        &mut self,
        grace_period: Duration,
        unverified_grace_period: Duration,
    );
    fn set_id(&mut self, our_id: Id);
}
Expand description

Trait for things that can store DHT nodes

DHT uses an object implementing this trait to keep track of other nodes on the network. The default implementation is NodeBucketStorage but DHT can accept any object that implements this trait.

Required Methods§

Source

fn add_or_update(&mut self, node: Node, verified: bool)

Add a Node to storage, or update the record of a Node already in storage.

§Parameters
  • node - The Node to add or update
  • verified - Set true if the caller knows the Node is live and can communicate with it (i.e. we’ve sent a request and received a response). If true, the last_verified and last_seen properties of NodeWrapper will be updated. If false, only last_seen will be updated.
Source

fn clear(&mut self)

Erase all Nodes from storage, resetting this object to its empty state

Source

fn count(&self) -> (usize, usize)

Return the number of Nodes that have been verified (last_verified property is not None) and the number that haven’t.

Returned as a tuple of (unverified count, verified count)

Source

fn get_all_unverified(&self) -> Vec<NodeWrapper>

Return a copy of the records for all the unverified Nodes

Source

fn get_all_verified(&self) -> Vec<NodeWrapper>

Return a copy of the records for all verified Nodes

Source

fn get_nearest_nodes(&self, id: &Id, exclude: Option<&Id>) -> Vec<Node>

Return a copy of the nearest nodes to the provided Id.

§Parameters
  • id - the target id. Nodes that are near this Id (based on XOR distance metric) should be returned.
  • exclude - If there’s a Node that you know will be near id and you don’t want to see it, you can exclude it by providing its Id here.
Source

fn prune(&mut self, grace_period: Duration, unverified_grace_period: Duration)

Prune (remove) records of Nodes tht haven’t been seen/verified recently.node_wrapper

§Parameters
  • grace_period - Previously verified Nodes are dropped if their last_verified property is None or less than (now - grace_period).
  • unverified_grace_period - Previously seen (but not verified) Nodes are dropped if their last_seen property is less than (now - unverified_grace_period)
Source

fn set_id(&mut self, our_id: Id)

Set our own DHT node’s Id.

This is used by implementations that use XOR distance from our own Id as part of their internal storage algorithm (the normal DHT bucket storage does this). But implementations are free to ignore this if they want.

Some implementations may reset (similar to invoking clear()) when this method is called.

Implementors§