Skip to content

Commit

Permalink
Add 'discovered' relation
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex6323 committed Nov 5, 2021
1 parent ceb5b8f commit f59c8be
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 18 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions bee-network/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Security -->

## 0.3.0 - 2021-11-xx

- New PeerRelation variant: Discovered

## 0.2.2 - 2021-08-26

### Changed
Expand Down
2 changes: 1 addition & 1 deletion bee-network/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bee-network"
version = "0.2.2"
version = "0.3.0"
authors = [ "IOTA Stiftung" ]
edition = "2021"
description = """
Expand Down
28 changes: 22 additions & 6 deletions bee-network/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ pub struct NetworkConfig {
pub(crate) bind_multiaddr: Multiaddr,
pub(crate) reconnect_interval_secs: u64,
pub(crate) max_unknown_peers: usize,
pub(crate) max_discovered_peers: usize,
pub(crate) static_peers: HashSet<Peer>,
}

Expand Down Expand Up @@ -155,6 +156,11 @@ impl NetworkConfig {
self.max_unknown_peers
}

/// Returns the maximum number of discovered peers that are allowed to connect.
pub fn max_discovered_peers(&self) -> usize {
self.max_discovered_peers
}

/// Returns the statically configured peers.
pub fn static_peers(&self) -> &HashSet<Peer> {
&self.static_peers
Expand Down Expand Up @@ -184,6 +190,7 @@ impl Default for NetworkConfig {
bind_multiaddr: DEFAULT_BIND_MULTIADDR.parse().unwrap(),
reconnect_interval_secs: DEFAULT_RECONNECT_INTERVAL_SECS,
max_unknown_peers: DEFAULT_MAX_UNKNOWN_PEERS,
max_discovered_peers: DEFAULT_MAX_DISCOVERED_PEERS,
static_peers: Default::default(),
}
}
Expand All @@ -196,7 +203,8 @@ pub struct NetworkConfigBuilder {
bind_multiaddr: Option<Multiaddr>,
reconnect_interval_secs: Option<u64>,
max_unknown_peers: Option<usize>,
peering: PeeringConfigBuilder,
max_discovered_peers: Option<usize>,
peering: ManualPeeringConfigBuilder,
}

impl NetworkConfigBuilder {
Expand Down Expand Up @@ -280,6 +288,12 @@ impl NetworkConfigBuilder {
self
}

/// Specifies the maximum number of gossip connections with discovered peers.
pub fn with_max_discovered_peers(mut self, n: usize) -> Self {
self.max_discovered_peers.replace(n);
self
}

/// Builds the network config.
pub fn finish(self) -> Result<NetworkConfig, Error> {
Ok(NetworkConfig {
Expand All @@ -290,6 +304,7 @@ impl NetworkConfigBuilder {
.unwrap_or_else(|| DEFAULT_BIND_MULTIADDR.parse().unwrap()),
reconnect_interval_secs: self.reconnect_interval_secs.unwrap_or(DEFAULT_RECONNECT_INTERVAL_SECS),
max_unknown_peers: self.max_unknown_peers.unwrap_or(DEFAULT_MAX_UNKNOWN_PEERS),
max_discovered_peers: self.max_discovered_peers.unwrap_or(DEFAULT_MAX_DISCOVERED_PEERS),
static_peers: self.peering.finish()?.peers,
})
}
Expand Down Expand Up @@ -335,13 +350,14 @@ impl InMemoryNetworkConfigBuilder {
.unwrap_or_else(|| DEFAULT_BIND_MULTIADDR_MEM.parse().unwrap()),
reconnect_interval_secs: DEFAULT_RECONNECT_INTERVAL_SECS,
max_unknown_peers: DEFAULT_MAX_UNKNOWN_PEERS,
max_discovered_peers: DEFAULT_MAX_DISCOVERED_PEERS,
static_peers: Default::default(),
}
}
}

#[derive(Clone)]
pub struct PeeringConfig {
pub struct ManualPeeringConfig {
pub peers: HashSet<Peer>,
}

Expand All @@ -365,12 +381,12 @@ impl std::hash::Hash for Peer {
}

#[derive(Default, Deserialize)]
pub struct PeeringConfigBuilder {
pub struct ManualPeeringConfigBuilder {
pub peers: Option<Vec<PeerBuilder>>,
}

impl PeeringConfigBuilder {
pub fn finish(self) -> Result<PeeringConfig, Error> {
impl ManualPeeringConfigBuilder {
pub fn finish(self) -> Result<ManualPeeringConfig, Error> {
let peers = match self.peers {
None => Default::default(),
Some(peer_builders) => {
Expand All @@ -393,7 +409,7 @@ impl PeeringConfigBuilder {
}
};

Ok(PeeringConfig { peers })
Ok(ManualPeeringConfig { peers })
}
}

Expand Down
14 changes: 14 additions & 0 deletions bee-network/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub mod global {
static RECONNECT_INTERVAL_SECS: OnceCell<u64> = OnceCell::new();
static NETWORK_ID: OnceCell<u64> = OnceCell::new();
static MAX_UNKNOWN_PEERS: OnceCell<usize> = OnceCell::new();
static MAX_DISCOVERED_PEERS: OnceCell<usize> = OnceCell::new();

pub fn set_reconnect_interval_secs(reconnect_interval_secs: u64) {
if cfg!(test) {
Expand Down Expand Up @@ -69,6 +70,17 @@ pub mod global {
pub fn max_unknown_peers() -> usize {
*MAX_UNKNOWN_PEERS.get().expect("oncecell get")
}

pub fn set_max_discovered_peers(max_discovered_peers: usize) {
if cfg!(test) {
let _ = MAX_DISCOVERED_PEERS.set(max_discovered_peers);
} else {
MAX_DISCOVERED_PEERS.set(max_discovered_peers).expect("oncecell set");
}
}
pub fn max_discovered_peers() -> usize {
*MAX_DISCOVERED_PEERS.get().expect("oncecell get")
}
}

/// Initializes a "standalone" version of the network layer.
Expand Down Expand Up @@ -150,12 +162,14 @@ fn init(
bind_multiaddr,
reconnect_interval_secs,
max_unknown_peers,
max_discovered_peers,
static_peers: peers,
} = config;

global::set_reconnect_interval_secs(reconnect_interval_secs);
global::set_network_id(network_id);
global::set_max_unknown_peers(max_unknown_peers);
global::set_max_discovered_peers(max_discovered_peers);

let (command_sender, command_receiver) = command_channel();
let (internal_command_sender, internal_command_receiver) = command_channel();
Expand Down
4 changes: 4 additions & 0 deletions bee-network/src/peer/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,8 @@ pub enum Error {
/// A failure due to hitting the maximum number of allowed unknown peers.
#[error("Tried to add more unknown peers than defined in the config ({0}).")]
ExceedsUnknownPeerLimit(usize),

/// A failure due to hitting the maximum number of allowed unknown peers.
#[error("Tried to add more discovered peers than defined in the config ({0}).")]
ExceedsDiscoveredPeerLimit(usize),
}
29 changes: 25 additions & 4 deletions bee-network/src/peer/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,18 @@ pub struct PeerInfo {
/// Describes the relation with a peer.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum PeerRelation {
/// Represents a persistent peer. If the connection to such a peer drops, the network will try to reconnect.
/// Represents a known peer.
///
/// If the connection to such a peer drops, the network will try to reconnect.
Known,
/// Represents an ephemeral peer. If the connection to such a peer drops, the network won't try to reconnect.
/// Represents an unknown peer.
///
/// If the connection to such a peer drops, the network won't try to reconnect.
Unknown,
/// Represents a discovered peer.
///
/// If the connection to such a peer drops, the network won't try to reconnect.
Discovered,
}

impl PeerRelation {
Expand All @@ -34,15 +42,25 @@ impl PeerRelation {
matches!(self, Self::Unknown)
}

/// Sets the relation to `PeerRelation::Known`.
/// Returns whether the peer is discovered.
pub fn is_discovered(&self) -> bool {
matches!(self, Self::Discovered)
}

/// Sets the relation to "known".
pub fn set_known(&mut self) {
*self = Self::Known;
}

/// Sets the relation to `PeerRelation::Unknown`.
/// Sets the relation to "unknown".
pub fn set_unknown(&mut self) {
*self = Self::Unknown;
}

/// Sets the relation to "discovered".
pub fn set_discovered(&mut self) {
*self = Self::Discovered;
}
}

#[cfg(test)]
Expand All @@ -59,5 +77,8 @@ mod tests {

pr.set_unknown();
assert!(pr.is_unknown());

pr.set_discovered();
assert!(pr.is_discovered())
}
}
21 changes: 16 additions & 5 deletions bee-network/src/peer/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,11 @@ impl PeerList {
self.peers.iter().fold(
0,
|count, (_, (info, state))| {
if predicate(info, state) { count + 1 } else { count }
if predicate(info, state) {
count + 1
} else {
count
}
},
)
}
Expand Down Expand Up @@ -277,6 +281,10 @@ impl PeerList {
&& self.filter_count(|info, _| info.relation.is_unknown()) >= global::max_unknown_peers()
{
Err(Error::ExceedsUnknownPeerLimit(global::max_unknown_peers()))
} else if !self.contains(peer_id)
&& self.filter_count(|info, _| info.relation.is_discovered()) >= global::max_discovered_peers()
{
Err(Error::ExceedsDiscoveredPeerLimit(global::max_discovered_peers()))
} else {
// All checks passed! Accept that peer.
Ok(())
Expand Down Expand Up @@ -314,6 +322,10 @@ impl PeerList {
&& self.filter_count(|info, _| info.relation.is_unknown()) >= global::max_unknown_peers()
{
Err(Error::ExceedsUnknownPeerLimit(global::max_unknown_peers()))
} else if peer_info.relation.is_discovered()
&& self.filter_count(|info, _| info.relation.is_discovered()) >= global::max_discovered_peers()
{
Err(Error::ExceedsDiscoveredPeerLimit(global::max_discovered_peers()))
} else {
// All checks passed! Allow dialing that peer.
Ok(())
Expand Down Expand Up @@ -363,13 +375,12 @@ mod tests {
let mut pl = PeerList::new(local_id);

for i in 1..=3 {
assert!(
pl.insert_peer(
assert!(pl
.insert_peer(
gen_random_peer_id(),
gen_deterministic_peer_info(i, PeerRelation::Known)
)
.is_ok()
);
.is_ok());
assert_eq!(pl.len(), i as usize);
}
}
Expand Down
3 changes: 2 additions & 1 deletion bee-protocol/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ keywords = [ "iota", "tangle", "bee", "framework", "protocol" ]
homepage = "https://www.iota.org"

[dependencies]
bee-autopeering = { version = "0.1.0", path = "../bee-autopeering", default-features = false }
bee-message = { version = "0.1.5", path = "../bee-message", default-features = false, features = [ "serde" ] }
bee-network = { version = "0.2.2", path = "../bee-network", default-features = false }
bee-network = { version = "0.3.0", path = "../bee-network", default-features = false }

0 comments on commit f59c8be

Please sign in to comment.