pub struct PeerManager { /* private fields */ }Expand description
The PeerManager provides functionality to add, find and delete peers. It wraps synchronous WAL-enabled SQLite database access and provides an async interface to the rest of the code base.
Every database call is dispatched to tokio’s blocking thread pool by [BlockingPeerStorage], so
a contended peer database can never park a tokio worker thread. See that type’s documentation for
why that matters.
Implementations§
Source§impl PeerManager
impl PeerManager
Sourcepub fn new(
database: CommsDatabase,
transport_protocols: Vec<TransportProtocol>,
) -> Result<PeerManager, PeerManagerError>
pub fn new( database: CommsDatabase, transport_protocols: Vec<TransportProtocol>, ) -> Result<PeerManager, PeerManagerError>
Constructs a new empty PeerManager
Sourcepub fn this_peer_identity(&self) -> ThisPeerIdentity
pub fn this_peer_identity(&self) -> ThisPeerIdentity
Get this peer’s identity
Sourcepub async fn count(&self) -> usize
pub async fn count(&self) -> usize
Get the number of peers in the PeerManager - any error will translate to a size of zero
Sourcepub async fn add_or_update_peer(
&self,
peer: Peer,
) -> Result<PeerId, PeerManagerError>
pub async fn add_or_update_peer( &self, peer: Peer, ) -> Result<PeerId, PeerManagerError>
Adds a peer to the routing table of the PeerManager if the peer does not already exist. When a peer already exist, the stored version will be replaced with the newly provided peer.
Sourcepub async fn soft_delete_peer(
&self,
node_id: &NodeId,
) -> Result<(), PeerManagerError>
pub async fn soft_delete_peer( &self, node_id: &NodeId, ) -> Result<(), PeerManagerError>
The peer with the specified node id will be soft deleted (marked as deleted)
Sourcepub async fn get_peers_by_node_ids(
&self,
node_ids: &[NodeId],
) -> Result<Vec<Peer>, PeerManagerError>
pub async fn get_peers_by_node_ids( &self, node_ids: &[NodeId], ) -> Result<Vec<Peer>, PeerManagerError>
Get all peers based on a list of their node_ids
Sourcepub async fn get_peer_public_keys_by_node_ids(
&self,
node_ids: &[NodeId],
) -> Result<Vec<CommsPublicKey>, PeerManagerError>
pub async fn get_peer_public_keys_by_node_ids( &self, node_ids: &[NodeId], ) -> Result<Vec<CommsPublicKey>, PeerManagerError>
Get all peers based on a list of their node_ids
Sourcepub async fn get_banned_peers(&self) -> Result<Vec<Peer>, PeerManagerError>
pub async fn get_banned_peers(&self) -> Result<Vec<Peer>, PeerManagerError>
Get all banned peers
Sourcepub async fn find_by_node_id(
&self,
node_id: &NodeId,
) -> Result<Option<Peer>, PeerManagerError>
pub async fn find_by_node_id( &self, node_id: &NodeId, ) -> Result<Option<Peer>, PeerManagerError>
Find the peer with the provided NodeID
Sourcepub async fn get_seed_peers(&self) -> Result<Vec<Peer>, PeerManagerError>
pub async fn get_seed_peers(&self) -> Result<Vec<Peer>, PeerManagerError>
gets all seed peers
Sourcepub async fn find_by_public_key(
&self,
public_key: &CommsPublicKey,
) -> Result<Option<Peer>, PeerManagerError>
pub async fn find_by_public_key( &self, public_key: &CommsPublicKey, ) -> Result<Option<Peer>, PeerManagerError>
Find the peer with the provided PublicKey
Sourcepub async fn find_all_starts_with(
&self,
partial: &[u8],
) -> Result<Vec<Peer>, PeerManagerError>
pub async fn find_all_starts_with( &self, partial: &[u8], ) -> Result<Vec<Peer>, PeerManagerError>
Find the peer with the provided substring. This currently only compares the given bytes to the NodeId
Sourcepub async fn exists(
&self,
public_key: &CommsPublicKey,
) -> Result<bool, PeerManagerError>
pub async fn exists( &self, public_key: &CommsPublicKey, ) -> Result<bool, PeerManagerError>
Check if a peer exist using the specified public_key
Sourcepub async fn exists_node_id(
&self,
node_id: &NodeId,
) -> Result<bool, PeerManagerError>
pub async fn exists_node_id( &self, node_id: &NodeId, ) -> Result<bool, PeerManagerError>
Check if a peer exist using the specified node_id
Sourcepub async fn all(
&self,
features: Option<PeerFeatures>,
) -> Result<Vec<Peer>, PeerManagerError>
pub async fn all( &self, features: Option<PeerFeatures>, ) -> Result<Vec<Peer>, PeerManagerError>
Returns all peers
Sourcepub async fn get_available_dial_candidates(
&self,
exclude_node_ids: &[NodeId],
limit: Option<usize>,
exclude_failed: bool,
randomize: bool,
) -> Result<Vec<Peer>, PeerManagerError>
pub async fn get_available_dial_candidates( &self, exclude_node_ids: &[NodeId], limit: Option<usize>, exclude_failed: bool, randomize: bool, ) -> Result<Vec<Peer>, PeerManagerError>
Get available dial candidates that are communication nodes, not banned, not deleted, reachable optionally not failed, optionally at random, and not in the excluded node IDs list
Sourcepub async fn discovery_syncing(
&self,
n: usize,
excluded_peers: &[NodeId],
features: Option<PeerFeatures>,
external_addresses_only: bool,
max_n: usize,
) -> Result<Vec<Peer>, PeerManagerError>
pub async fn discovery_syncing( &self, n: usize, excluded_peers: &[NodeId], features: Option<PeerFeatures>, external_addresses_only: bool, max_n: usize, ) -> Result<Vec<Peer>, PeerManagerError>
Return “good” peers for syncing Criteria:
- Peer is not banned
- Peer has been seen within a defined time span (1 week)
- Returns at most
max_npeers (the caller’s configured serve cap); amax_nof 0 falls back to the peer manager default
Sourcepub async fn add_or_update_online_peer(
&self,
pubkey: &CommsPublicKey,
node_id: &NodeId,
addresses: &[Multiaddr],
peer_features: &PeerFeatures,
source: &PeerAddressSource,
) -> Result<Peer, PeerManagerError>
pub async fn add_or_update_online_peer( &self, pubkey: &CommsPublicKey, node_id: &NodeId, addresses: &[Multiaddr], peer_features: &PeerFeatures, source: &PeerAddressSource, ) -> Result<Peer, PeerManagerError>
Adds or updates a peer and sets the last connection as successful. If the peer is marked as offline, it will be unmarked.
Sourcepub async fn direct_identity_node_id(
&self,
node_id: &NodeId,
) -> Result<Option<Peer>, PeerManagerError>
pub async fn direct_identity_node_id( &self, node_id: &NodeId, ) -> Result<Option<Peer>, PeerManagerError>
Get a peer matching the given node ID
Sourcepub async fn direct_identity_public_key(
&self,
public_key: &CommsPublicKey,
) -> Result<Option<Peer>, PeerManagerError>
pub async fn direct_identity_public_key( &self, public_key: &CommsPublicKey, ) -> Result<Option<Peer>, PeerManagerError>
Get a peer matching the given public key
Sourcepub async fn get_not_banned_or_deleted_peers(
&self,
) -> Result<Vec<Peer>, PeerManagerError>
pub async fn get_not_banned_or_deleted_peers( &self, ) -> Result<Vec<Peer>, PeerManagerError>
Fetch all peers (except banned ones)
Sourcepub async fn random_peers(
&self,
n: usize,
excluded: &[NodeId],
flags: Option<PeerFlags>,
known_good: bool,
) -> Result<Vec<Peer>, PeerManagerError>
pub async fn random_peers( &self, n: usize, excluded: &[NodeId], flags: Option<PeerFlags>, known_good: bool, ) -> Result<Vec<Peer>, PeerManagerError>
Fetch n random peers that are Communication Nodes and have at least one external address
Sourcepub async fn unban_peer(&self, node_id: &NodeId) -> Result<(), PeerManagerError>
pub async fn unban_peer(&self, node_id: &NodeId) -> Result<(), PeerManagerError>
Unbans the peer if it is banned. This function is idempotent.
Sourcepub async fn unban_all_peers(&self) -> Result<usize, PeerManagerError>
pub async fn unban_all_peers(&self) -> Result<usize, PeerManagerError>
Unbans the peer if it is banned. This function is idempotent.
pub async fn reset_offline_non_wallet_peers( &self, ) -> Result<usize, PeerManagerError>
Sourcepub async fn ban_peer(
&self,
public_key: &CommsPublicKey,
duration: Duration,
reason: String,
) -> Result<NodeId, PeerManagerError>
pub async fn ban_peer( &self, public_key: &CommsPublicKey, duration: Duration, reason: String, ) -> Result<NodeId, PeerManagerError>
Ban the peer for a length of time specified by the duration
Sourcepub async fn ban_peer_by_node_id(
&self,
node_id: &NodeId,
duration: Duration,
reason: String,
) -> Result<NodeId, PeerManagerError>
pub async fn ban_peer_by_node_id( &self, node_id: &NodeId, duration: Duration, reason: String, ) -> Result<NodeId, PeerManagerError>
Ban the peer for a length of time specified by the duration
Sourcepub async fn is_peer_banned(
&self,
node_id: &NodeId,
) -> Result<bool, PeerManagerError>
pub async fn is_peer_banned( &self, node_id: &NodeId, ) -> Result<bool, PeerManagerError>
Get the ban status of a peer
Sourcepub async fn get_peer_features(
&self,
node_id: &NodeId,
) -> Result<PeerFeatures, PeerManagerError>
pub async fn get_peer_features( &self, node_id: &NodeId, ) -> Result<PeerFeatures, PeerManagerError>
Get the peer’s features
Sourcepub async fn get_peer_multi_addresses(
&self,
node_id: &NodeId,
) -> Result<MultiaddressesWithStats, PeerManagerError>
pub async fn get_peer_multi_addresses( &self, node_id: &NodeId, ) -> Result<MultiaddressesWithStats, PeerManagerError>
Get a peer’s multiaddresses
Sourcepub async fn get_peers_multi_addresses(
&self,
node_ids: &[NodeId],
) -> Result<Vec<(NodeId, MultiaddressesWithStats)>, PeerManagerError>
pub async fn get_peers_multi_addresses( &self, node_ids: &[NodeId], ) -> Result<Vec<(NodeId, MultiaddressesWithStats)>, PeerManagerError>
Get multiple peers’ multiaddresses
Sourcepub async fn set_peer_metadata(
&self,
node_id: &NodeId,
key: u8,
data: Vec<u8>,
) -> Result<Option<Vec<u8>>, PeerManagerError>
pub async fn set_peer_metadata( &self, node_id: &NodeId, key: u8, data: Vec<u8>, ) -> Result<Option<Vec<u8>>, PeerManagerError>
This will store metadata inside of the metadata field in the peer provided by the nodeID. It will return None if the value was empty and the old value if the value was updated
Trait Implementations§
Source§impl Clone for PeerManager
impl Clone for PeerManager
Auto Trait Implementations§
impl !Freeze for PeerManager
impl !RefUnwindSafe for PeerManager
impl !UnwindSafe for PeerManager
impl Send for PeerManager
impl Sync for PeerManager
impl Unpin for PeerManager
impl UnsafeUnpin for PeerManager
Blanket Implementations§
Source§impl<T> AggregateExpressionMethods for T
impl<T> AggregateExpressionMethods for T
Source§fn aggregate_distinct(self) -> Self::Outputwhere
Self: DistinctDsl,
fn aggregate_distinct(self) -> Self::Outputwhere
Self: DistinctDsl,
DISTINCT modifier for aggregate functions Read moreSource§fn aggregate_all(self) -> Self::Outputwhere
Self: AllDsl,
fn aggregate_all(self) -> Self::Outputwhere
Self: AllDsl,
ALL modifier for aggregate functions Read moreSource§fn aggregate_filter<P>(self, f: P) -> Self::Output
fn aggregate_filter<P>(self, f: P) -> Self::Output
Source§fn aggregate_order<O>(self, o: O) -> Self::Outputwhere
Self: OrderAggregateDsl<O>,
fn aggregate_order<O>(self, o: O) -> Self::Outputwhere
Self: OrderAggregateDsl<O>,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<'a, T> DefaultFeatures<'a> for T
impl<'a, T> DefaultFeatures<'a> for T
Source§fn clone_boxed(&self) -> Box<dyn DefaultFeatures<'a>>
fn clone_boxed(&self) -> Box<dyn DefaultFeatures<'a>>
Box
behind a trait object of this trait.Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be
downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further
downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSend for T
impl<T> DowncastSend for T
Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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 moreSource§impl<T> IntoSql for T
impl<T> IntoSql for T
Source§fn into_sql<T>(self) -> Self::Expression
fn into_sql<T>(self) -> Self::Expression
self to an expression for Diesel’s query builder. Read moreSource§fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expression
fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expression
&self to an expression for Diesel’s query builder. Read moreSource§impl<'a, T> NonSyncFeatures<'a> for Twhere
T: 'a + Clone,
impl<'a, T> NonSyncFeatures<'a> for Twhere
T: 'a + Clone,
Source§fn clone_boxed(&self) -> Box<dyn NonSyncFeatures<'a>>
fn clone_boxed(&self) -> Box<dyn NonSyncFeatures<'a>>
Box
behind a trait object of this trait.