use std::time::Duration;
use crate::{Bin, NetworkId, ProximityOrder};
pub trait SwarmSpec {
fn network_id(&self) -> NetworkId;
fn max_proximity_order(&self) -> ProximityOrder {
ProximityOrder::MAX
}
fn saturation_peers(&self) -> u8 {
8
}
fn over_saturation_peers(&self) -> u8 {
18
}
fn bootnode_over_saturation_peers(&self) -> u8 {
20
}
fn neighborhood_low_watermark(&self) -> u8 {
2
}
fn clock_skew_tolerance(&self) -> Duration {
Duration::from_secs(6 * 60 * 60)
}
fn bin_count(&self) -> usize {
usize::from(self.max_proximity_order().get()) + 1
}
fn max_bin(&self) -> Bin {
Bin::from(self.max_proximity_order())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct StaticSpec {
network_id: NetworkId,
}
impl StaticSpec {
pub const fn new(network_id: NetworkId) -> Self {
Self { network_id }
}
}
impl SwarmSpec for StaticSpec {
fn network_id(&self) -> NetworkId {
self.network_id
}
}
pub const MAINNET: StaticSpec = StaticSpec::new(NetworkId::MAINNET);
pub const TESTNET: StaticSpec = StaticSpec::new(NetworkId::TESTNET);
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn defaults_match_bee() {
let s = MAINNET;
assert_eq!(s.network_id(), NetworkId::MAINNET);
assert_eq!(s.max_proximity_order(), ProximityOrder::MAX);
assert_eq!(s.saturation_peers(), 8);
assert_eq!(s.over_saturation_peers(), 18);
assert_eq!(s.bootnode_over_saturation_peers(), 20);
assert_eq!(s.neighborhood_low_watermark(), 2);
assert_eq!(s.clock_skew_tolerance(), Duration::from_secs(21600));
assert_eq!(s.bin_count(), 32);
assert_eq!(s.max_bin(), Bin::MAX);
}
#[test]
fn testnet_distinct_from_mainnet() {
assert_ne!(MAINNET.network_id(), TESTNET.network_id());
}
#[test]
fn override_saturation_via_custom_impl() {
struct Tight;
impl SwarmSpec for Tight {
fn network_id(&self) -> NetworkId {
NetworkId::TESTNET
}
fn saturation_peers(&self) -> u8 {
4
}
}
assert_eq!(Tight.saturation_peers(), 4);
assert_eq!(Tight.over_saturation_peers(), 18); }
}