mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-07-30 16:31:57 +00:00
General cleanup and rework
This commit is contained in:
@@ -4,6 +4,14 @@ version = "0.1.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
|
||||
[dependencies]
|
||||
error-chain = "0.11"
|
||||
base58 = "0.1.0"
|
||||
datastore = { path = "../datastore" }
|
||||
futures = "0.1.0"
|
||||
owning_ref = "0.3.3"
|
||||
multiaddr = "0.2"
|
||||
libp2p-peer = { path = "../libp2p-peer" }
|
||||
multihash = { path = "../multihash" }
|
||||
serde = "1.0"
|
||||
serde_derive = "1.0"
|
||||
|
||||
[dev-dependencies]
|
||||
tempfile = "2.2"
|
||||
|
154
libp2p-peerstore/src/json_peerstore.rs
Normal file
154
libp2p-peerstore/src/json_peerstore.rs
Normal file
@@ -0,0 +1,154 @@
|
||||
// Copyright 2017 Parity Technologies (UK) Ltd.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a
|
||||
// copy of this software and associated documentation files (the "Software"),
|
||||
// to deal in the Software without restriction, including without limitation
|
||||
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
// and/or sell copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
//! Implementation of the `Peerstore` trait that uses a single JSON file as backend.
|
||||
|
||||
use super::TTL;
|
||||
use PeerId;
|
||||
use base58::{FromBase58, ToBase58};
|
||||
use datastore::{Datastore, Query, JsonFileDatastore, JsonFileDatastoreEntry};
|
||||
use futures::{Future, Stream};
|
||||
use multiaddr::Multiaddr;
|
||||
use multihash::Multihash;
|
||||
use peer_info::{PeerInfo, AddAddrBehaviour};
|
||||
use peerstore::{Peerstore, PeerAccess};
|
||||
use std::io::Error as IoError;
|
||||
use std::iter;
|
||||
use std::path::PathBuf;
|
||||
use std::vec::IntoIter as VecIntoIter;
|
||||
|
||||
/// Peerstore backend that uses a Json file.
|
||||
pub struct JsonPeerstore {
|
||||
store: JsonFileDatastore<PeerInfo>,
|
||||
}
|
||||
|
||||
impl JsonPeerstore {
|
||||
/// Opens a new peerstore tied to a JSON file at the given path.
|
||||
///
|
||||
/// If the file exists, this function will open it. In any case, flushing the peerstore or
|
||||
/// destroying it will write to the file.
|
||||
#[inline]
|
||||
pub fn new<P>(path: P) -> Result<JsonPeerstore, IoError>
|
||||
where P: Into<PathBuf>
|
||||
{
|
||||
Ok(JsonPeerstore { store: JsonFileDatastore::new(path)? })
|
||||
}
|
||||
|
||||
/// Flushes the content of the peer store to the disk.
|
||||
///
|
||||
/// This function can only fail in case of a disk access error. If an error occurs, any change
|
||||
/// to the peerstore that was performed since the last successful flush will be lost. No data
|
||||
/// will be corrupted.
|
||||
#[inline]
|
||||
pub fn flush(&self) -> Result<(), IoError> {
|
||||
self.store.flush()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Peerstore for &'a JsonPeerstore {
|
||||
type PeerAccess = JsonPeerstoreAccess<'a>;
|
||||
type PeersIter = Box<Iterator<Item = PeerId>>;
|
||||
|
||||
#[inline]
|
||||
fn peer(self, peer_id: &PeerId) -> Option<Self::PeerAccess> {
|
||||
let hash = peer_id.clone().as_bytes().to_base58();
|
||||
self.store.lock(hash.into()).map(JsonPeerstoreAccess)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn peer_or_create(self, peer_id: &PeerId) -> Self::PeerAccess {
|
||||
let hash = peer_id.clone().as_bytes().to_base58();
|
||||
JsonPeerstoreAccess(self.store.lock_or_create(hash.into()))
|
||||
}
|
||||
|
||||
fn peers(self) -> Self::PeersIter {
|
||||
let query = self.store.query(Query {
|
||||
prefix: "".into(),
|
||||
filters: vec![],
|
||||
orders: vec![],
|
||||
skip: 0,
|
||||
limit: u64::max_value(),
|
||||
keys_only: true,
|
||||
});
|
||||
|
||||
let list = query.filter_map(|(key, _)| {
|
||||
// We filter out invalid elements. This can happen if the JSON storage file was
|
||||
// corrupted or manually modified by the user.
|
||||
match key.from_base58() {
|
||||
Ok(bytes) => Multihash::decode_bytes(bytes).ok(),
|
||||
Err(_) => return None,
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
.wait(); // Wait can never block for the JSON datastore.
|
||||
|
||||
// Need to handle I/O errors. Again we just ignore.
|
||||
if let Ok(list) = list {
|
||||
Box::new(list.into_iter()) as Box<_>
|
||||
} else {
|
||||
Box::new(iter::empty()) as Box<_>
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct JsonPeerstoreAccess<'a>(JsonFileDatastoreEntry<'a, PeerInfo>);
|
||||
|
||||
impl<'a> PeerAccess for JsonPeerstoreAccess<'a> {
|
||||
type AddrsIter = VecIntoIter<Multiaddr>;
|
||||
|
||||
#[inline]
|
||||
fn addrs(&self) -> Self::AddrsIter {
|
||||
self.0.addrs().cloned().collect::<Vec<_>>().into_iter()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn add_addr(&mut self, addr: Multiaddr, ttl: TTL) {
|
||||
self.0.add_addr(addr, ttl, AddAddrBehaviour::IgnoreTtlIfInferior);
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn set_addr_ttl(&mut self, addr: Multiaddr, ttl: TTL) {
|
||||
self.0.add_addr(addr, ttl, AddAddrBehaviour::OverwriteTtl);
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn clear_addrs(&mut self) {
|
||||
self.0.set_addrs(iter::empty());
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn get_pub_key(&self) -> Option<&[u8]> {
|
||||
self.0.public_key()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn set_pub_key(&mut self, key: Vec<u8>) {
|
||||
self.0.set_public_key(key);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
extern crate tempfile;
|
||||
peerstore_tests!(
|
||||
{::json_peerstore::JsonPeerstore::new(temp_file.path()).unwrap()}
|
||||
{let temp_file = self::tempfile::NamedTempFile::new().unwrap()}
|
||||
);
|
||||
}
|
@@ -1,9 +1,55 @@
|
||||
#[macro_use] extern crate error_chain;
|
||||
extern crate multiaddr;
|
||||
extern crate libp2p_peer as peer;
|
||||
// Copyright 2017 Parity Technologies (UK) Ltd.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a
|
||||
// copy of this software and associated documentation files (the "Software"),
|
||||
// to deal in the Software without restriction, including without limitation
|
||||
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
// and/or sell copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
mod memory_peerstore;
|
||||
//! # Peerstore
|
||||
//!
|
||||
//! The `peerstore` crate allows one to store information about a peer.
|
||||
//! It is similar to a key-value database, where the keys are multihashes (generally the hash of
|
||||
//! the public key of the peer, but that is not enforced by this crate) and the values are the
|
||||
//! public key and a list of multiaddresses with a time-to-live.
|
||||
//!
|
||||
//! This crate consists in a generic `Peerstore` trait and various backends.
|
||||
//!
|
||||
//! Note that the peerstore implementations do not consider information inside a peer store to be
|
||||
//! critical. In case of an error (eg. corrupted file, disk error, etc.) they will prefer to lose
|
||||
//! data rather than returning the error.
|
||||
|
||||
extern crate base58;
|
||||
extern crate datastore;
|
||||
extern crate futures;
|
||||
extern crate multiaddr;
|
||||
extern crate multihash;
|
||||
extern crate owning_ref;
|
||||
extern crate serde;
|
||||
#[macro_use]
|
||||
extern crate serde_derive;
|
||||
|
||||
pub use self::peerstore::{Peerstore, PeerAccess};
|
||||
|
||||
#[macro_use]
|
||||
mod peerstore_tests;
|
||||
|
||||
pub mod json_peerstore;
|
||||
pub mod memory_peerstore;
|
||||
mod peerstore;
|
||||
mod peer_info;
|
||||
|
||||
pub type PeerId = multihash::Multihash;
|
||||
pub type TTL = std::time::Duration;
|
||||
|
@@ -1,132 +1,123 @@
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::collections::hash_map;
|
||||
use multiaddr::Multiaddr;
|
||||
use peer::PeerId;
|
||||
use peer_info::PeerInfo;
|
||||
// Copyright 2017 Parity Technologies (UK) Ltd.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a
|
||||
// copy of this software and associated documentation files (the "Software"),
|
||||
// to deal in the Software without restriction, including without limitation
|
||||
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
// and/or sell copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
//! Implementation of the `Peerstore` trait that simple stores peers in memory.
|
||||
|
||||
use super::TTL;
|
||||
use peerstore::*;
|
||||
use PeerId;
|
||||
use multiaddr::Multiaddr;
|
||||
use multihash::Multihash;
|
||||
use owning_ref::OwningRefMut;
|
||||
use peer_info::{PeerInfo, AddAddrBehaviour};
|
||||
use peerstore::{Peerstore, PeerAccess};
|
||||
use std::collections::HashMap;
|
||||
use std::iter;
|
||||
use std::sync::{Mutex, MutexGuard};
|
||||
use std::vec::IntoIter as VecIntoIter;
|
||||
|
||||
pub struct MemoryPeerstore<T> {
|
||||
store: HashMap<PeerId, PeerInfo<T>>,
|
||||
/// Implementation of the `Peerstore` trait that simply stores the peer information in memory.
|
||||
pub struct MemoryPeerstore {
|
||||
store: Mutex<HashMap<Multihash, PeerInfo>>,
|
||||
}
|
||||
|
||||
impl<T> MemoryPeerstore<T> {
|
||||
pub fn new() -> MemoryPeerstore<T> {
|
||||
MemoryPeerstore {
|
||||
store: HashMap::new(),
|
||||
}
|
||||
impl MemoryPeerstore {
|
||||
/// Initializes a new `MemoryPeerstore`. The database is initially empty.
|
||||
#[inline]
|
||||
pub fn empty() -> MemoryPeerstore {
|
||||
MemoryPeerstore { store: Mutex::new(HashMap::new()) }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Peerstore<T> for MemoryPeerstore<T> {
|
||||
fn add_peer(&mut self, peer_id: PeerId, peer_info: PeerInfo<T>) {
|
||||
self.store.insert(peer_id, peer_info);
|
||||
impl Default for MemoryPeerstore {
|
||||
#[inline]
|
||||
fn default() -> MemoryPeerstore {
|
||||
MemoryPeerstore::empty()
|
||||
}
|
||||
/// Returns a list of peers in this Peerstore
|
||||
fn peers(&self) -> Vec<&PeerId> {
|
||||
// this is terrible but I honestly can't think of any other way than to hand off ownership
|
||||
// through this type of allocation or handing off the entire hashmap and letting people do what they
|
||||
// want with that
|
||||
self.store.keys().collect()
|
||||
}
|
||||
/// Returns the PeerInfo for a specific peer in this peer store, or None if it doesn't exist.
|
||||
fn peer_info(&self, peer_id: &PeerId) -> Option<&PeerInfo<T>> {
|
||||
self.store.get(peer_id)
|
||||
}
|
||||
|
||||
impl<'a> Peerstore for &'a MemoryPeerstore {
|
||||
type PeerAccess = MemoryPeerstoreAccess<'a>;
|
||||
type PeersIter = VecIntoIter<PeerId>;
|
||||
|
||||
fn peer(self, peer_id: &PeerId) -> Option<Self::PeerAccess> {
|
||||
let lock = self.store.lock().unwrap();
|
||||
OwningRefMut::new(lock)
|
||||
.try_map_mut(|n| n.get_mut(peer_id).ok_or(()))
|
||||
.ok()
|
||||
.map(MemoryPeerstoreAccess)
|
||||
}
|
||||
|
||||
/// Try to get a property for a given peer
|
||||
fn get_data(&self, peer_id: &PeerId, key: &str) -> Option<&T> {
|
||||
match self.store.get(peer_id) {
|
||||
None => None,
|
||||
Some(peer_info) => peer_info.get_data(key),
|
||||
}
|
||||
}
|
||||
/// Set a property for a given peer
|
||||
fn put_data(&mut self, peer_id: &PeerId, key: String, val: T) {
|
||||
match self.store.get_mut(peer_id) {
|
||||
None => (),
|
||||
Some(mut peer_info) => {
|
||||
peer_info.set_data(key, val);
|
||||
},
|
||||
}
|
||||
fn peer_or_create(self, peer_id: &PeerId) -> Self::PeerAccess {
|
||||
let lock = self.store.lock().unwrap();
|
||||
let r = OwningRefMut::new(lock)
|
||||
.map_mut(|n| n.entry(peer_id.clone()).or_insert_with(|| PeerInfo::new()));
|
||||
MemoryPeerstoreAccess(r)
|
||||
}
|
||||
|
||||
/// Adds an address to a peer
|
||||
fn add_addr(&mut self, peer_id: &PeerId, addr: Multiaddr, ttl: TTL) {
|
||||
match self.store.get_mut(peer_id) {
|
||||
None => (),
|
||||
Some(peer_info) => peer_info.add_addr(addr),
|
||||
}
|
||||
fn peers(self) -> Self::PeersIter {
|
||||
let lock = self.store.lock().unwrap();
|
||||
lock.keys().cloned().collect::<Vec<_>>().into_iter()
|
||||
}
|
||||
}
|
||||
|
||||
// Note: Rust doesn't provide a `MutexGuard::map` method, otherwise we could directly store a
|
||||
// `MutexGuard<'a, (&'a Multihash, &'a PeerInfo)>`.
|
||||
pub struct MemoryPeerstoreAccess<'a>(OwningRefMut<MutexGuard<'a, HashMap<Multihash, PeerInfo>>, PeerInfo>);
|
||||
|
||||
impl<'a> PeerAccess for MemoryPeerstoreAccess<'a> {
|
||||
type AddrsIter = VecIntoIter<Multiaddr>;
|
||||
|
||||
#[inline]
|
||||
fn addrs(&self) -> Self::AddrsIter {
|
||||
self.0.addrs().cloned().collect::<Vec<_>>().into_iter()
|
||||
}
|
||||
|
||||
// AddAddrs gives AddrManager addresses to use, with a given ttl
|
||||
// (time-to-live), after which the address is no longer valid.
|
||||
// If the manager has a longer TTL, the operation is a no-op for that address
|
||||
fn add_addrs(&mut self, peer_id: &PeerId, addrs: Vec<Multiaddr>, ttl: TTL) {
|
||||
match self.store.get_mut(peer_id) {
|
||||
None => (),
|
||||
Some(peer_info) => {
|
||||
for addr in addrs {
|
||||
peer_info.add_addr(addr)
|
||||
}
|
||||
},
|
||||
}
|
||||
#[inline]
|
||||
fn add_addr(&mut self, addr: Multiaddr, ttl: TTL) {
|
||||
self.0.add_addr(addr, ttl, AddAddrBehaviour::IgnoreTtlIfInferior);
|
||||
}
|
||||
|
||||
// SetAddr calls mgr.SetAddrs(p, addr, ttl)
|
||||
fn set_addr(&mut self, peer_id: &PeerId, addr: Multiaddr, ttl: TTL) {
|
||||
self.set_addrs(peer_id, vec![addr], ttl)
|
||||
#[inline]
|
||||
fn set_addr_ttl(&mut self, addr: Multiaddr, ttl: TTL) {
|
||||
self.0.add_addr(addr, ttl, AddAddrBehaviour::OverwriteTtl);
|
||||
}
|
||||
|
||||
// SetAddrs sets the ttl on addresses. This clears any TTL there previously.
|
||||
// This is used when we receive the best estimate of the validity of an address.
|
||||
fn set_addrs(&mut self, peer_id: &PeerId, addrs: Vec<Multiaddr>, ttl: TTL) {
|
||||
match self.store.get_mut(peer_id) {
|
||||
None => (),
|
||||
Some(peer_info) => peer_info.set_addrs(addrs),
|
||||
}
|
||||
#[inline]
|
||||
fn clear_addrs(&mut self) {
|
||||
self.0.set_addrs(iter::empty());
|
||||
}
|
||||
|
||||
/// Returns all known (and valid) addresses for a given peer
|
||||
fn addrs(&self, peer_id: &PeerId) -> &[Multiaddr] {
|
||||
match self.store.get(peer_id) {
|
||||
None => &[],
|
||||
Some(peer_info) => peer_info.get_addrs(),
|
||||
}
|
||||
#[inline]
|
||||
fn get_pub_key(&self) -> Option<&[u8]> {
|
||||
self.0.public_key()
|
||||
}
|
||||
|
||||
/// Removes all previously stored addresses
|
||||
fn clear_addrs(&mut self, peer_id: &PeerId) {
|
||||
match self.store.get_mut(peer_id) {
|
||||
None => (),
|
||||
Some(peer_info) => peer_info.set_addrs(vec![]),
|
||||
}
|
||||
}
|
||||
|
||||
/// Get public key for a peer
|
||||
fn get_pub_key(&self, peer_id: &PeerId) -> Option<&[u8]> {
|
||||
self.store.get(peer_id).map(|peer_info| peer_info.get_public_key())
|
||||
}
|
||||
|
||||
/// Set public key for a peer
|
||||
fn set_pub_key(&mut self, peer_id: &PeerId, key: Vec<u8>) {
|
||||
self.store.get_mut(peer_id).map(|peer_info| peer_info.set_public_key(key));
|
||||
#[inline]
|
||||
fn set_pub_key(&mut self, key: Vec<u8>) {
|
||||
self.0.set_public_key(key);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use peer::PeerId;
|
||||
use super::{PeerInfo, Peerstore, MemoryPeerstore};
|
||||
|
||||
#[test]
|
||||
fn insert_get_and_list() {
|
||||
let peer_id = PeerId::new(vec![1,2,3]);
|
||||
let peer_info = PeerInfo::new();
|
||||
let mut peer_store: MemoryPeerstore<u8> = MemoryPeerstore::new();
|
||||
peer_store.add_peer(peer_id.clone(), peer_info);
|
||||
peer_store.put_data(&peer_id, "test".into(), 123u8).unwrap();
|
||||
let got = peer_store.get_data(&peer_id, "test").expect("should be able to fetch");
|
||||
assert_eq!(*got, 123u8);
|
||||
}
|
||||
peerstore_tests!({
|
||||
::memory_peerstore::MemoryPeerstore::empty()
|
||||
});
|
||||
}
|
||||
|
@@ -1,41 +1,195 @@
|
||||
use std::time;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use multiaddr::Multiaddr;
|
||||
// Copyright 2017 Parity Technologies (UK) Ltd.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a
|
||||
// copy of this software and associated documentation files (the "Software"),
|
||||
// to deal in the Software without restriction, including without limitation
|
||||
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
// and/or sell copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
pub struct PeerInfo<T> {
|
||||
public_key: Vec<u8>,
|
||||
addrs: Vec<Multiaddr>,
|
||||
data: HashMap<String, T>,
|
||||
//! The objective of the `peerstore` crate is to provide a key-value storage. Keys are peer IDs,
|
||||
//! and the `PeerInfo` struct in this module is the value.
|
||||
//!
|
||||
//! Note that the `PeerInfo` struct implements `PartialOrd` so that it can be stored in a
|
||||
//! `Datastore`. This operation currently simply compares the public keys of the `PeerInfo`s.
|
||||
//! If the `PeerInfo` struct ever gets exposed to the public API of the crate, we may want to give
|
||||
//! more thoughts about this.
|
||||
|
||||
use TTL;
|
||||
use multiaddr::Multiaddr;
|
||||
use serde::{Serialize, Deserialize, Serializer, Deserializer};
|
||||
use serde::de::Error as DeserializerError;
|
||||
use serde::ser::SerializeStruct;
|
||||
use std::cmp::Ordering;
|
||||
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
||||
|
||||
/// Information about a peer.
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
||||
pub struct PeerInfo {
|
||||
// Adresses, and the time at which they will be considered expired.
|
||||
addrs: Vec<(Multiaddr, SystemTime)>,
|
||||
public_key: Option<Vec<u8>>,
|
||||
}
|
||||
|
||||
impl<T> PeerInfo<T> {
|
||||
pub fn new() -> PeerInfo<T> {
|
||||
PeerInfo {
|
||||
public_key: vec![],
|
||||
addrs: vec![],
|
||||
data: HashMap::new(),
|
||||
impl PeerInfo {
|
||||
/// Builds a new empty `PeerInfo`.
|
||||
#[inline]
|
||||
pub fn new() -> PeerInfo {
|
||||
PeerInfo { addrs: vec![], public_key: None }
|
||||
}
|
||||
|
||||
/// Returns the list of the non-expired addresses stored in this `PeerInfo`.
|
||||
///
|
||||
/// > **Note**: Keep in mind that this function is racy because addresses can expire between
|
||||
/// > the moment when you get them and the moment when you process them.
|
||||
// TODO: use -> impl Iterator eventually
|
||||
#[inline]
|
||||
pub fn addrs<'a>(&'a self) -> Box<Iterator<Item = &'a Multiaddr> + 'a> {
|
||||
let now = SystemTime::now();
|
||||
Box::new(self.addrs.iter().filter_map(move |&(ref addr, ref expires)| if *expires >= now {
|
||||
Some(addr)
|
||||
} else {
|
||||
None
|
||||
}))
|
||||
}
|
||||
|
||||
/// Sets the list of addresses and their time-to-live.
|
||||
///
|
||||
/// This removes all previously-stored addresses.
|
||||
#[inline]
|
||||
pub fn set_addrs<I>(&mut self, addrs: I)
|
||||
where I: IntoIterator<Item = (Multiaddr, TTL)>
|
||||
{
|
||||
let now = SystemTime::now();
|
||||
self.addrs = addrs.into_iter().map(move |(addr, ttl)| (addr, now + ttl)).collect();
|
||||
}
|
||||
|
||||
/// Adds a single address and its time-to-live.
|
||||
///
|
||||
/// If the peer info already knows about that address but with a longer TTL, then the operation
|
||||
/// is a no-op.
|
||||
pub fn add_addr(&mut self, addr: Multiaddr, ttl: TTL, behaviour: AddAddrBehaviour) {
|
||||
let expires = SystemTime::now() + ttl;
|
||||
|
||||
if let Some(&mut (_, ref mut existing_expires)) =
|
||||
self.addrs.iter_mut().find(|&&mut (ref a, _)| a == &addr)
|
||||
{
|
||||
if behaviour == AddAddrBehaviour::OverwriteTtl || *existing_expires < expires {
|
||||
*existing_expires = expires;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
self.addrs.push((addr, expires));
|
||||
}
|
||||
|
||||
/// Sets the public key stored in this `PeerInfo`.
|
||||
#[inline]
|
||||
pub fn set_public_key(&mut self, key: Vec<u8>) {
|
||||
self.public_key = Some(key);
|
||||
}
|
||||
|
||||
/// Returns the public key stored in this `PeerInfo`, if any.
|
||||
#[inline]
|
||||
pub fn public_key(&self) -> Option<&[u8]> {
|
||||
self.public_key.as_ref().map(|k| &**k)
|
||||
}
|
||||
}
|
||||
|
||||
/// Behaviour of the `add_addr` function.
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
||||
pub enum AddAddrBehaviour {
|
||||
/// Always overwrite the existing TTL.
|
||||
OverwriteTtl,
|
||||
/// Don't overwrite if the TTL is larger.
|
||||
IgnoreTtlIfInferior,
|
||||
}
|
||||
|
||||
impl Serialize for PeerInfo {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where S: Serializer
|
||||
{
|
||||
let mut s = serializer.serialize_struct("PeerInfo", 2)?;
|
||||
s.serialize_field(
|
||||
"addrs",
|
||||
&self.addrs
|
||||
.iter()
|
||||
.map(|&(ref addr, ref expires)| {
|
||||
let addr = addr.to_bytes();
|
||||
let from_epoch = expires.duration_since(UNIX_EPOCH)
|
||||
// This `unwrap_or` case happens if the user has their system time set to
|
||||
// before EPOCH. Times-to-live will be be longer than expected, but it's a very
|
||||
// improbable corner case and is not attackable in any way, so we don't really
|
||||
// care.
|
||||
.unwrap_or(Duration::new(0, 0));
|
||||
let secs = from_epoch.as_secs()
|
||||
.saturating_mul(1_000)
|
||||
.saturating_add(from_epoch.subsec_nanos() as u64 / 1_000_000);
|
||||
(addr, secs)
|
||||
})
|
||||
.collect::<Vec<_>>(),
|
||||
)?;
|
||||
s.serialize_field("public_key", &self.public_key)?;
|
||||
s.end()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Deserialize<'de> for PeerInfo {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where D: Deserializer<'de>
|
||||
{
|
||||
// We deserialize to an intermdiate struct first, then turn that struct into a `PeerInfo`.
|
||||
let interm = {
|
||||
#[derive(Deserialize)]
|
||||
struct Interm {
|
||||
addrs: Vec<(String, u64)>,
|
||||
public_key: Option<Vec<u8>>,
|
||||
}
|
||||
Interm::deserialize(deserializer)?
|
||||
};
|
||||
|
||||
let addrs = {
|
||||
let mut out = Vec::with_capacity(interm.addrs.len());
|
||||
for (addr, since_epoch) in interm.addrs {
|
||||
let addr = match Multiaddr::new(&addr) {
|
||||
Ok(a) => a,
|
||||
Err(err) => return Err(DeserializerError::custom(err)),
|
||||
};
|
||||
let expires = UNIX_EPOCH + Duration::from_millis(since_epoch);
|
||||
out.push((addr, expires));
|
||||
}
|
||||
out
|
||||
};
|
||||
|
||||
Ok(PeerInfo {
|
||||
addrs: addrs,
|
||||
public_key: interm.public_key,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialOrd for PeerInfo {
|
||||
#[inline]
|
||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||
// See module-level comment.
|
||||
match (&self.public_key, &other.public_key) {
|
||||
(&Some(ref my_pub), &Some(ref other_pub)) => {
|
||||
Some(my_pub.cmp(other_pub))
|
||||
}
|
||||
_ => {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
pub fn get_public_key(&self) -> &[u8] {
|
||||
&self.public_key
|
||||
}
|
||||
pub fn set_public_key(&mut self, key: Vec<u8>) {
|
||||
self.public_key = key;
|
||||
}
|
||||
pub fn get_addrs(&self) -> &[Multiaddr] {
|
||||
&self.addrs
|
||||
}
|
||||
pub fn set_addrs(&mut self, addrs: Vec<Multiaddr>) {
|
||||
self.addrs = addrs;
|
||||
}
|
||||
pub fn add_addr(&mut self, addr: Multiaddr) {
|
||||
self.addrs.push(addr); // TODO: This is stupid, a more advanced thing using TTLs need to be implemented
|
||||
self.addrs.dedup();
|
||||
}
|
||||
pub fn get_data(&self, key: &str) -> Option<&T> {
|
||||
self.data.get(key)
|
||||
}
|
||||
pub fn set_data(&mut self, key: String, val: T) -> Option<T> {
|
||||
self.data.insert(key, val)
|
||||
}
|
||||
}
|
||||
|
@@ -1,48 +1,104 @@
|
||||
// Copyright 2017 Parity Technologies (UK) Ltd.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a
|
||||
// copy of this software and associated documentation files (the "Software"),
|
||||
// to deal in the Software without restriction, including without limitation
|
||||
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
// and/or sell copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
use {PeerId, TTL};
|
||||
use multiaddr::Multiaddr;
|
||||
use peer::PeerId;
|
||||
use peer_info::PeerInfo;
|
||||
use super::TTL;
|
||||
|
||||
pub trait Peerstore<T> {
|
||||
/// Add a peer to this peer store
|
||||
fn add_peer(&mut self, peer_id: PeerId, peer_info: PeerInfo<T>);
|
||||
/// Implemented on objects that store peers.
|
||||
///
|
||||
/// Note that the methods of this trait take by ownership (ie. `self` instead of `&self` or
|
||||
/// `&mut self`). This was made so that the associated types could hold `self` internally and
|
||||
/// because Rust doesn't have higher-ranked trait bounds yet.
|
||||
///
|
||||
/// Therefore this trait should likely be implemented on `&'a ConcretePeerstore` instead of
|
||||
/// on `ConcretePeerstore`.
|
||||
pub trait Peerstore {
|
||||
/// Grants access to the a peer inside the peer store.
|
||||
type PeerAccess: PeerAccess;
|
||||
/// List of the peers in this peer store.
|
||||
type PeersIter: Iterator<Item = PeerId>;
|
||||
|
||||
/// Returns a list of peers in this Peerstore
|
||||
fn peers(&self) -> Vec<&PeerId>;
|
||||
/// Grants access to a peer by its ID.
|
||||
fn peer(self, peer_id: &PeerId) -> Option<Self::PeerAccess>;
|
||||
|
||||
/// Returns the PeerInfo for a specific peer in this peer store, or None if it doesn't exist.
|
||||
fn peer_info(&self, peer_id: &PeerId) -> Option<&PeerInfo<T>>;
|
||||
/// Grants access to a peer by its ID or creates it.
|
||||
fn peer_or_create(self, peer_id: &PeerId) -> Self::PeerAccess;
|
||||
|
||||
/// Try to get a property for a given peer
|
||||
fn get_data(&self, peer_id: &PeerId, key: &str) -> Option<&T>;
|
||||
|
||||
/// Set a property for a given peer
|
||||
fn put_data(&mut self, peer_id: &PeerId, key: String, val: T);
|
||||
|
||||
/// Adds an address to a peer
|
||||
fn add_addr(&mut self, peer_id: &PeerId, addr: Multiaddr, ttl: TTL);
|
||||
|
||||
// AddAddrs gives AddrManager addresses to use, with a given ttl
|
||||
// (time-to-live), after which the address is no longer valid.
|
||||
// If the manager has a longer TTL, the operation is a no-op for that address
|
||||
fn add_addrs(&mut self, peer_id: &PeerId, addrs: Vec<Multiaddr>, ttl: TTL);
|
||||
|
||||
// SetAddr calls mgr.SetAddrs(p, addr, ttl)
|
||||
fn set_addr(&mut self, peer_id: &PeerId, addr: Multiaddr, ttl: TTL);
|
||||
|
||||
// SetAddrs sets the ttl on addresses. This clears any TTL there previously.
|
||||
// This is used when we receive the best estimate of the validity of an address.
|
||||
fn set_addrs(&mut self, peer_id: &PeerId, addrs: Vec<Multiaddr>, ttl: TTL);
|
||||
|
||||
/// Returns all known (and valid) addresses for a given peer
|
||||
fn addrs(&self, peer_id: &PeerId) -> &[Multiaddr];
|
||||
|
||||
/// Removes all previously stored addresses
|
||||
fn clear_addrs(&mut self, peer_id: &PeerId);
|
||||
|
||||
/// Get public key for a peer
|
||||
fn get_pub_key(&self, peer_id: &PeerId) -> Option<&[u8]>;
|
||||
|
||||
/// Set public key for a peer
|
||||
fn set_pub_key(&mut self, peer_id: &PeerId, key: Vec<u8>);
|
||||
/// Returns a list of peers in this peer store.
|
||||
///
|
||||
/// Keep in mind that the trait implementation may allow new peers to be added or removed at
|
||||
/// any time. If that is the case, you have to take into account that this is only an
|
||||
/// indication.
|
||||
fn peers(self) -> Self::PeersIter;
|
||||
}
|
||||
|
||||
/// Implemented on objects that represent an open access to a peer stored in a peer store.
|
||||
///
|
||||
/// The exact semantics of "open access" depend on the trait implementation.
|
||||
pub trait PeerAccess {
|
||||
/// Iterator returned by `addrs`.
|
||||
type AddrsIter: Iterator<Item = Multiaddr>;
|
||||
|
||||
/// Returns all known and non-expired addresses for a given peer.
|
||||
///
|
||||
/// > **Note**: Keep in mind that this function is racy because addresses can expire between
|
||||
/// > the moment when you get them and the moment when you process them.
|
||||
fn addrs(&self) -> Self::AddrsIter;
|
||||
|
||||
/// Adds an address to a peer.
|
||||
///
|
||||
/// If the manager already has this address stored and with a longer TTL, then the operation
|
||||
/// is a no-op.
|
||||
fn add_addr(&mut self, addr: Multiaddr, ttl: TTL);
|
||||
|
||||
// Similar to calling `add_addr` multiple times in a row.
|
||||
#[inline]
|
||||
fn add_addrs<I>(&mut self, addrs: I, ttl: TTL)
|
||||
where I: IntoIterator<Item = Multiaddr>
|
||||
{
|
||||
for addr in addrs.into_iter() {
|
||||
self.add_addr(addr, ttl);
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets the TTL of an address of a peer. Adds the address if it is currently unknown.
|
||||
///
|
||||
/// Contrary to `add_addr`, this operation is never a no-op.
|
||||
#[inline]
|
||||
fn set_addr_ttl(&mut self, addr: Multiaddr, ttl: TTL);
|
||||
|
||||
// Similar to calling `set_addr_ttl` multiple times in a row.
|
||||
fn set_addrs_ttl<I>(&mut self, addrs: I, ttl: TTL)
|
||||
where I: IntoIterator<Item = Multiaddr>
|
||||
{
|
||||
for addr in addrs.into_iter() {
|
||||
self.add_addr(addr, ttl);
|
||||
}
|
||||
}
|
||||
|
||||
/// Removes all previously stored addresses.
|
||||
fn clear_addrs(&mut self);
|
||||
|
||||
/// Get the public key for the peer, if known.
|
||||
fn get_pub_key(&self) -> Option<&[u8]>;
|
||||
|
||||
/// Set public key for the peer.
|
||||
fn set_pub_key(&mut self, key: Vec<u8>);
|
||||
}
|
||||
|
139
libp2p-peerstore/src/peerstore_tests.rs
Normal file
139
libp2p-peerstore/src/peerstore_tests.rs
Normal file
@@ -0,0 +1,139 @@
|
||||
// Copyright 2017 Parity Technologies (UK) Ltd.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a
|
||||
// copy of this software and associated documentation files (the "Software"),
|
||||
// to deal in the Software without restriction, including without limitation
|
||||
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
// and/or sell copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
//! Contains the `peerstore_tests!` macro which will test an implementation of `Peerstore`.
|
||||
//! You need to pass as first parameter a way to create the `Peerstore` that you want to test.
|
||||
//!
|
||||
//! You can also pass as additional parameters a list of statements that will be inserted before
|
||||
//! each test. This allows you to have the peerstore builder use variables created by these
|
||||
//! statements.
|
||||
|
||||
#![cfg(test)]
|
||||
|
||||
macro_rules! peerstore_tests {
|
||||
({$create_peerstore:expr} $({$stmt:stmt})*) => {
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
use {Peerstore, PeerAccess};
|
||||
use multiaddr::Multiaddr;
|
||||
use multihash::Multihash;
|
||||
|
||||
#[test]
|
||||
fn initially_empty() {
|
||||
$($stmt;)*
|
||||
let peer_store = $create_peerstore;
|
||||
let peer_id = Multihash::encode_bytes(0, vec![1, 2, 3]).unwrap();
|
||||
assert_eq!(peer_store.peers().count(), 0);
|
||||
assert!(peer_store.peer(&peer_id).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn set_pub_key_then_retreive() {
|
||||
$($stmt;)*
|
||||
let peer_store = $create_peerstore;
|
||||
let peer_id = Multihash::encode_bytes(0, vec![1, 2, 3]).unwrap();
|
||||
|
||||
peer_store.peer_or_create(&peer_id).set_pub_key(vec![9, 8, 7]);
|
||||
|
||||
assert_eq!(peer_store.peer(&peer_id).unwrap().get_pub_key().unwrap(), &[9, 8, 7]);
|
||||
|
||||
assert_eq!(peer_store.peers().collect::<Vec<_>>(), &[peer_id.clone()]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn set_then_get_addr() {
|
||||
$($stmt;)*
|
||||
let peer_store = $create_peerstore;
|
||||
let peer_id = Multihash::encode_bytes(0, vec![1, 2, 3]).unwrap();
|
||||
let addr = Multiaddr::new("/ip4/0.0.0.0/tcp/0").unwrap();
|
||||
|
||||
peer_store.peer_or_create(&peer_id).add_addr(addr.clone(), Duration::from_millis(5000));
|
||||
|
||||
let addrs = peer_store.peer(&peer_id).unwrap().addrs().collect::<Vec<_>>();
|
||||
assert_eq!(addrs, &[addr]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn add_expired_addr() {
|
||||
// Add an already-expired address to a peer.
|
||||
$($stmt;)*
|
||||
let peer_store = $create_peerstore;
|
||||
let peer_id = Multihash::encode_bytes(0, vec![1, 2, 3]).unwrap();
|
||||
let addr = Multiaddr::new("/ip4/0.0.0.0/tcp/0").unwrap();
|
||||
|
||||
peer_store.peer_or_create(&peer_id).add_addr(addr.clone(), Duration::from_millis(0));
|
||||
thread::sleep(Duration::from_millis(2));
|
||||
|
||||
let addrs = peer_store.peer(&peer_id).unwrap().addrs();
|
||||
assert_eq!(addrs.count(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn clear_addrs() {
|
||||
$($stmt;)*
|
||||
let peer_store = $create_peerstore;
|
||||
let peer_id = Multihash::encode_bytes(0, vec![1, 2, 3]).unwrap();
|
||||
let addr = Multiaddr::new("/ip4/0.0.0.0/tcp/0").unwrap();
|
||||
|
||||
peer_store.peer_or_create(&peer_id).add_addr(addr.clone(), Duration::from_millis(5000));
|
||||
peer_store.peer(&peer_id).unwrap().clear_addrs();
|
||||
|
||||
let addrs = peer_store.peer(&peer_id).unwrap().addrs();
|
||||
assert_eq!(addrs.count(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_update_ttl() {
|
||||
$($stmt;)*
|
||||
let peer_store = $create_peerstore;
|
||||
let peer_id = Multihash::encode_bytes(0, vec![1, 2, 3]).unwrap();
|
||||
|
||||
let addr1 = Multiaddr::new("/ip4/0.0.0.0/tcp/0").unwrap();
|
||||
let addr2 = Multiaddr::new("/ip4/0.0.0.1/tcp/0").unwrap();
|
||||
|
||||
peer_store.peer_or_create(&peer_id).add_addr(addr1.clone(), Duration::from_millis(5000));
|
||||
peer_store.peer_or_create(&peer_id).add_addr(addr2.clone(), Duration::from_millis(5000));
|
||||
assert_eq!(peer_store.peer(&peer_id).unwrap().addrs().count(), 2);
|
||||
|
||||
// `add_addr` must not overwrite the TTL because it's already higher
|
||||
peer_store.peer_or_create(&peer_id).add_addr(addr1.clone(), Duration::from_millis(0));
|
||||
thread::sleep(Duration::from_millis(2));
|
||||
assert_eq!(peer_store.peer(&peer_id).unwrap().addrs().count(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn force_update_ttl() {
|
||||
$($stmt;)*
|
||||
let peer_store = $create_peerstore;
|
||||
let peer_id = Multihash::encode_bytes(0, vec![1, 2, 3]).unwrap();
|
||||
|
||||
let addr1 = Multiaddr::new("/ip4/0.0.0.0/tcp/0").unwrap();
|
||||
let addr2 = Multiaddr::new("/ip4/0.0.0.1/tcp/0").unwrap();
|
||||
|
||||
peer_store.peer_or_create(&peer_id).add_addr(addr1.clone(), Duration::from_millis(5000));
|
||||
peer_store.peer_or_create(&peer_id).add_addr(addr2.clone(), Duration::from_millis(5000));
|
||||
assert_eq!(peer_store.peer(&peer_id).unwrap().addrs().count(), 2);
|
||||
|
||||
peer_store.peer_or_create(&peer_id).set_addr_ttl(addr1.clone(), Duration::from_millis(0));
|
||||
thread::sleep(Duration::from_millis(2));
|
||||
assert_eq!(peer_store.peer(&peer_id).unwrap().addrs().count(), 1);
|
||||
}
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user