eigen_trust/lib.rs
1//! # Eigen Trust
2//!
3//! A library for managing trust in a distributed network with zero-knowledge
4//! features.
5//!
6//! ## Main characteristics:
7//! **Self-policing** - the shared ethics of the user population is defined and
8//! enforced by the peers themselves and not by some central authority.
9//!
10//! **Minimal** - computation, infrastructure, storage, and message complexity
11//! are reduced to a minimum.
12//!
13//! **Incorruptible** - Reputation should be obtained by consistent good
14//! behavior through several transactions. This is enforced for all users, so no
15//! one can cheat the system and obtain a higher reputation. It is also
16//! resistant to malicious collectives.
17//!
18//! ## Usage:
19//! ```rust
20//! use eigen_trust::{EigenError, Keypair, LevelFilter, Multiaddr, Node, NodeConfig, PeerId};
21//! use std::str::FromStr;
22//!
23//! const BOOTSTRAP_PEERS: [(&str, &str, f64); 2] = [
24//! (
25//! "/ip4/127.0.0.1/tcp/58584",
26//! "12D3KooWLyTCx9j2FMcsHe81RMoDfhXbdyyFgNGQMdcrnhShTvQh",
27//! 0.5,
28//! ),
29//! (
30//! "/ip4/127.0.0.1/tcp/58601",
31//! "12D3KooWKBKXsLwbmVBySEmbKayJzfWp3tPCKrnDCsmNy9prwjvy",
32//! 0.5,
33//! ),
34//! ];
35//!
36//! const DEFAULT_ADDRESS: &str = "/ip4/0.0.0.0/tcp/0";
37//!
38//! struct Config;
39//! impl NodeConfig for Config {
40//! const INTERVAL: u64 = 2;
41//! const NUM_CONNECTIONS: usize = 12;
42//! const PRE_TRUST_WEIGHT: f64 = 0.2;
43//! }
44//!
45//! #[tokio::main]
46//! async fn main() -> Result<(), EigenError> {
47//! let local_key = Keypair::generate_ed25519();
48//! let local_address =
49//! Multiaddr::from_str(DEFAULT_ADDRESS).map_err(|_| EigenError::InvalidAddress)?;
50//!
51//! let mut bootstrap_nodes = Vec::new();
52//! for info in BOOTSTRAP_PEERS.iter() {
53//! let peer_addr = Multiaddr::from_str(info.0).map_err(|_| EigenError::InvalidAddress)?;
54//! let peer_id = PeerId::from_str(info.1).map_err(|_| EigenError::InvalidPeerId)?;
55//!
56//! bootstrap_nodes.push((peer_id, peer_addr, info.2));
57//! }
58//!
59//! let node = Node::<Config>::new(local_key, local_address, bootstrap_nodes)?;
60//! node.main_loop(Some(1)).await?;
61//!
62//! Ok(())
63//! }
64//! ```
65//!
66//! ## Implementation
67//! The library is implemented according to the original [Eigen Trust paper](http://ilpubs.stanford.edu:8090/562/1/2002-56.pdf).
68//! It is developed under the Ethereum Foundation grant.
69
70#![allow(clippy::tabs_in_doc_comments)]
71#![deny(
72 future_incompatible,
73 nonstandard_style,
74 missing_docs,
75 deprecated,
76 unreachable_code,
77 unreachable_patterns,
78 absolute_paths_not_starting_with_crate,
79 unsafe_code,
80 clippy::unwrap_used,
81 clippy::panic,
82 clippy::unnecessary_cast,
83 clippy::cast_lossless,
84 clippy::cast_possible_truncation,
85 clippy::cast_possible_wrap,
86 clippy::cast_precision_loss,
87 clippy::cast_sign_loss
88)]
89#![warn(trivial_casts)]
90#![forbid(unsafe_code)]
91
92/// The module for epoch-related calculations, like seconds until the next
93/// epoch, current epoch, etc.
94mod epoch;
95/// The module for the node setup, running the main loop, and handling network
96/// events.
97mod node;
98/// The module for the peer related functionalities, like:
99/// - Adding/removing neighbors
100/// - Calculating the global trust score
101/// - Calculating local scores toward neighbors for a given epoch
102/// - Keeping track of neighbors scores towards us
103mod peer;
104/// The module for defining the request-response protocol.
105mod protocol;
106
107pub use epoch::Epoch;
108pub use libp2p::{identity::Keypair, Multiaddr, PeerId};
109pub use log::LevelFilter;
110pub use node::{Node, NodeConfig};
111pub use peer::Peer;
112
113/// The crate-wide error variants.
114#[derive(Debug)]
115#[repr(u8)]
116pub enum EigenError {
117 /// Invalid keypair passed into node config.
118 InvalidKeypair,
119 /// Invalid multiaddress passed into node config.
120 InvalidAddress,
121 /// Invalid peer id passed.
122 InvalidPeerId,
123 /// Invalid trust score passed into node config.
124 InvalidNumNeighbours,
125 /// Node failed to start listening on specified address. Usually because the
126 /// address is already in use.
127 ListenFailed,
128 /// Node failed to connect to a neighbor.
129 DialError,
130 /// Max number of neighbors reached for a peer.
131 MaxNeighboursReached,
132 /// Failed to calculate current epoch.
133 EpochError,
134}