[][src]Module tari_comms::peer_manager

The peer list maintained by the Peer Manager are used when constructing outbound messages. Peers can be added and removed from the list and can be found via their NodeId, Public key or Net Address. A subset of peers can be requested from the Peer Manager based on a specific Broadcast Strategy.

In an application a single Peer Manager should be initialized and passed around the application contained in an Arc. All the functions in peer manager are thread-safe.

If the Peer Manager is instantiated with a provided DataStore it will provide persistence via the provided DataStore implementation.

# use tari_comms::peer_manager::{NodeId, Peer, PeerManager, PeerFlags, PeerFeatures};
# use tari_comms::types::CommsPublicKey;
# use tari_comms::connection::{NetAddress, NetAddressesWithStats};
# use tari_crypto::keys::PublicKey;
# use tari_storage::lmdb_store::LMDBBuilder;
# use lmdb_zero::db;
# use std::sync::Arc;
# use tari_storage::LMDBWrapper;
# use tari_storage::lmdb_store::LMDBConfig;

let mut rng = rand::rngs::OsRng;
let (dest_sk, pk) = CommsPublicKey::random_keypair(&mut rng);
let node_id = NodeId::from_key(&pk).unwrap();
let net_addresses = NetAddressesWithStats::from("1.2.3.4:8000".parse::<NetAddress>().unwrap());
let peer = Peer::new(
    pk,
    node_id.clone(),
    net_addresses,
    PeerFlags::default(),
    PeerFeatures::COMMUNICATION_NODE,
    Default::default(),
);
let database_name = "pm_peer_database";
let datastore = LMDBBuilder::new()
    .set_path("/tmp/")
    .set_env_config(LMDBConfig::default())
    .set_max_number_of_databases(1)
    .add_database(database_name, lmdb_zero::db::CREATE)
    .build()
    .unwrap();
let peer_database = datastore.get_handle(database_name).unwrap();
let peer_database = LMDBWrapper::new(Arc::new(peer_database));
let peer_manager = PeerManager::new(peer_database).unwrap();

peer_manager.add_peer(peer.clone());

let returned_peer = peer_manager.find_by_node_id(&node_id).unwrap();

Re-exports

pub use node_id::NodeId;

Modules

node_id

Structs

NodeIdentity

The public and private identity of this node on the network

Peer

A Peer represents a communication peer that is identified by a Public Key and NodeId. The Peer struct maintains a collection of the NetAddressesWithStats that this Peer can be reached by. The struct also maintains a set of flags describing the status of the Peer.

PeerFeatures
PeerFlags
PeerManager

The PeerManager consist of a routing table of previously discovered peers. It also provides functionality to add, find and delete peers.

PeerQuery

Represents a query which can be performed on the peer database

PeerStorage

PeerStorage provides a mechanism to keep a datastore and a local copy of all peers in sync and allow fast searches using the node_id, public key or net_address of a peer.

Enums

NodeIdentityError
PeerManagerError
PeerQuerySortBy

Sort options for PeerQuery

Type Definitions

PeerId

Represents a local peer id. This number is meaningless outside of this node.