From 816ccac1ffc04d1b8ab19bba8f6d6e09ea030038 Mon Sep 17 00:00:00 2001 From: folex <0xdxdy@gmail.com> Date: Fri, 27 Mar 2020 19:57:05 +0300 Subject: [PATCH] weighted: logs --- protocols/kad/src/kbucket.rs | 1 - protocols/kad/src/kbucket/bucket.rs | 28 +++++++++++++++++++++++++-- protocols/kad/src/kbucket/weighted.rs | 1 + 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/protocols/kad/src/kbucket.rs b/protocols/kad/src/kbucket.rs index 70fd8733..f0cbb93c 100644 --- a/protocols/kad/src/kbucket.rs +++ b/protocols/kad/src/kbucket.rs @@ -76,7 +76,6 @@ mod weighted; pub use entry::*; pub use sub_bucket::*; -use arrayvec::self; use bucket::KBucket; use libp2p_core::identity::ed25519::{Keypair, PublicKey}; use std::collections::{VecDeque}; diff --git a/protocols/kad/src/kbucket/bucket.rs b/protocols/kad/src/kbucket/bucket.rs index 4b385fab..58da1498 100644 --- a/protocols/kad/src/kbucket/bucket.rs +++ b/protocols/kad/src/kbucket/bucket.rs @@ -95,6 +95,18 @@ pub enum InsertResult { Full, } +impl std::fmt::Display for InsertResult { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let descritpion = match self { + InsertResult::Inserted => "InsertResult::Inserted", + InsertResult::Pending { .. } => "InsertResult::Pending", + InsertResult::Full => "InsertResult::Full", + }; + + write!(f, "{}", descritpion) + } +} + /// The result of applying a pending node to a bucket, possibly /// replacing an existing node. #[derive(Debug, Clone, PartialEq, Eq)] @@ -213,11 +225,23 @@ where /// the new node is added as the last element of the bucket. /// pub fn insert(&mut self, node: Node, status: NodeStatus) -> InsertResult { - if node.weight > 0 { + let peer_id = bs58::encode(node.key.as_ref()).into_string(); + let weight = node.weight; + + let result = if node.weight > 0 { self.weighted.insert(node, status) } else { self.swamp.insert(node, status) - } + }; + + println!( + "Bucket: inserting node {} weight {} -> {}", + peer_id, + weight, + result + ); + + result } fn is_full(&self, weighted: bool) -> bool { diff --git a/protocols/kad/src/kbucket/weighted.rs b/protocols/kad/src/kbucket/weighted.rs index 7c6dc749..db7a5503 100644 --- a/protocols/kad/src/kbucket/weighted.rs +++ b/protocols/kad/src/kbucket/weighted.rs @@ -318,6 +318,7 @@ where keys.into_iter() .map(move |w| { + println!("Weighted: iterating through {} weight", w); map.get(&w) .into_iter() .map(|bucket| bucket.iter().map(|(n, s)| (&n.inner, s)))