use std::cmp::Ordering;
use std::net::SocketAddr;
use std::time::{Duration, Instant};
use tracing::trace;
use super::constants::{BAD_CONDITION, NODE_CONTACT_INTERVAL_SECS};
use super::node_id::NodeId;
#[derive(Clone, Debug)]
pub struct DhtNode {
id: NodeId,
addr: SocketAddr,
rtt_ms: u64,
condition: u32,
last_contact: Option<Instant>,
}
impl DhtNode {
pub fn new(id: NodeId, addr: SocketAddr) -> Self {
DhtNode {
id,
addr,
rtt_ms: 0,
condition: 1,
last_contact: None,
}
}
pub fn with_random_id(addr: SocketAddr) -> Self {
DhtNode {
id: NodeId::random(),
addr,
rtt_ms: 0,
condition: 0,
last_contact: None,
}
}
pub fn id(&self) -> &NodeId {
&self.id
}
pub fn addr(&self) -> SocketAddr {
self.addr
}
pub fn set_addr(&mut self, addr: SocketAddr) {
self.addr = addr;
}
pub fn rtt_ms(&self) -> u64 {
self.rtt_ms
}
pub fn update_rtt(&mut self, rtt_ms: u64) {
self.rtt_ms = rtt_ms;
}
pub fn condition(&self) -> u32 {
self.condition
}
pub fn is_good(&self) -> bool {
!self.is_bad() && !self.is_questionable()
}
pub fn is_bad(&self) -> bool {
self.condition >= BAD_CONDITION
}
pub fn is_questionable(&self) -> bool {
if self.is_bad() {
return false;
}
match self.last_contact {
Some(t) => t.elapsed() >= Duration::from_secs(NODE_CONTACT_INTERVAL_SECS),
None => true,
}
}
pub fn mark_good(&mut self) {
self.condition = 0;
}
pub fn mark_bad(&mut self) {
self.condition = BAD_CONDITION;
}
pub fn update_last_contact(&mut self) {
self.last_contact = Some(Instant::now());
}
pub fn mark_contacted(&mut self) {
self.mark_good();
self.update_last_contact();
}
pub fn timeout(&mut self) {
self.condition += 1;
trace!(node_id = %self.id, condition = self.condition, "DHT node timeout");
}
pub fn last_contact(&self) -> Option<Instant> {
self.last_contact
}
pub fn set_last_contact(&mut self, instant: Option<Instant>) {
self.last_contact = instant;
}
}
impl PartialEq for DhtNode {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
}
}
impl Eq for DhtNode {}
impl Ord for DhtNode {
fn cmp(&self, other: &Self) -> Ordering {
self.id.cmp(&other.id)
}
}
impl PartialOrd for DhtNode {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl std::fmt::Display for DhtNode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"DhtNode ID={}, Addr={}, Condition={}, RTT={}ms",
self.id, self.addr, self.condition, self.rtt_ms
)
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::net::{IpAddr, Ipv4Addr};
fn test_addr() -> SocketAddr {
SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 6881)
}
#[test]
fn new_node_has_condition_1() {
let node = DhtNode::new(NodeId::ZERO, test_addr());
assert_eq!(node.condition(), 1);
assert!(!node.is_good()); }
#[test]
fn random_id_node_has_condition_0() {
let node = DhtNode::with_random_id(test_addr());
assert_eq!(node.condition(), 0);
}
#[test]
fn mark_good_resets_condition() {
let mut node = DhtNode::new(NodeId::ZERO, test_addr());
node.timeout();
node.timeout();
assert_eq!(node.condition(), 3);
node.mark_good();
assert_eq!(node.condition(), 0);
}
#[test]
fn mark_bad_sets_condition() {
let mut node = DhtNode::new(NodeId::ZERO, test_addr());
node.mark_bad();
assert!(node.is_bad());
assert_eq!(node.condition(), BAD_CONDITION);
}
#[test]
fn timeout_increments_condition() {
let mut node = DhtNode::new(NodeId::ZERO, test_addr());
for _ in 0..BAD_CONDITION {
node.timeout();
}
assert!(node.is_bad());
}
#[test]
fn update_last_contact_makes_node_good() {
let mut node = DhtNode::new(NodeId::ZERO, test_addr());
node.mark_good();
node.update_last_contact();
assert!(node.is_good());
}
#[test]
fn equality_by_id() {
let a = DhtNode::new(NodeId::ZERO, test_addr());
let b = DhtNode::new(
NodeId::ZERO,
SocketAddr::new(IpAddr::V4(Ipv4Addr::new(192, 168, 1, 1)), 9999),
);
assert_eq!(a, b); }
#[test]
fn inequality_by_id() {
let a = DhtNode::new(NodeId::ZERO, test_addr());
let b = DhtNode::new(NodeId::MAX, test_addr());
assert_ne!(a, b);
}
#[test]
fn rtt_update() {
let mut node = DhtNode::new(NodeId::ZERO, test_addr());
node.update_rtt(42);
assert_eq!(node.rtt_ms(), 42);
}
#[test]
fn questionable_when_no_contact() {
let mut node = DhtNode::new(NodeId::ZERO, test_addr());
node.mark_good();
assert!(node.is_questionable());
}
#[test]
fn not_questionable_when_recently_contacted() {
let mut node = DhtNode::new(NodeId::ZERO, test_addr());
node.mark_good();
node.update_last_contact();
assert!(!node.is_questionable());
assert!(node.is_good());
}
#[test]
fn display_format() {
let node = DhtNode::new(NodeId::ZERO, test_addr());
let s = format!("{}", node);
assert!(s.contains("DhtNode"));
assert!(s.contains("0000")); }
}