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§
Sourcefn add_or_update(&mut self, node: Node, verified: bool)
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 updateverified
- 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, thelast_verified
andlast_seen
properties of NodeWrapper will be updated. If false, onlylast_seen
will be updated.
Sourcefn count(&self) -> (usize, usize)
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)
Sourcefn get_all_unverified(&self) -> Vec<NodeWrapper>
fn get_all_unverified(&self) -> Vec<NodeWrapper>
Return a copy of the records for all the unverified Nodes
Sourcefn get_all_verified(&self) -> Vec<NodeWrapper>
fn get_all_verified(&self) -> Vec<NodeWrapper>
Return a copy of the records for all verified Nodes
Sourcefn get_nearest_nodes(&self, id: &Id, exclude: Option<&Id>) -> Vec<Node>
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 nearid
and you don’t want to see it, you can exclude it by providing its Id here.
Sourcefn prune(&mut self, grace_period: Duration, unverified_grace_period: Duration)
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 theirlast_verified
property is None or less than(now - grace_period)
.unverified_grace_period
- Previously seen (but not verified) Nodes are dropped if theirlast_seen
property is less than(now - unverified_grace_period)
Sourcefn set_id(&mut self, our_id: Id)
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.