Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: log network address with KBucketKey #868

Merged
merged 1 commit into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions sn_networking/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ use libp2p_quic as quic;
use prometheus_client::registry::Registry;
use sn_protocol::{
messages::{Request, Response},
NetworkAddress, PrettyPrintRecordKey,
NetworkAddress, PrettyPrintKBucketKey,
};
use std::{
collections::{HashMap, HashSet},
Expand Down Expand Up @@ -299,8 +299,8 @@ impl NetworkBuilder {
let peer_id = PeerId::from(self.keypair.public());
info!("Node (PID: {}) with PeerId: {peer_id}", std::process::id());
info!(
"Self PeerID {peer_id} is represented as record_key {:?}",
PrettyPrintRecordKey::from(NetworkAddress::from_peer(peer_id).to_record_key())
"Self PeerID {peer_id} is represented as kbucket_key {:?}",
PrettyPrintKBucketKey(NetworkAddress::from_peer(peer_id).as_kbucket_key())
);

#[cfg(feature = "open-metrics")]
Expand Down
11 changes: 8 additions & 3 deletions sn_networking/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ pub enum Error {

#[cfg(test)]
mod tests {
use sn_protocol::{storage::ChunkAddress, NetworkAddress};
use sn_protocol::{storage::ChunkAddress, NetworkAddress, PrettyPrintKBucketKey};
use xor_name::XorName;

use super::*;
Expand All @@ -133,10 +133,15 @@ mod tests {
let mut rng = rand::thread_rng();
let xor_name = XorName::random(&mut rng);
let address = ChunkAddress::new(xor_name);
let record_key = NetworkAddress::from_chunk_address(address).to_record_key();
let network_address = NetworkAddress::from_chunk_address(address);
let record_key = network_address.to_record_key();
let pretty_record: PrettyPrintRecordKey = record_key.into();
let record_str = format!("{}", pretty_record);
let xor_name_str = format!("{:64x}", xor_name);
let xor_name_str = format!(
"{:64x}({:?})",
xor_name,
PrettyPrintKBucketKey(network_address.as_kbucket_key())
);
println!("record_str: {}", record_str);
println!("xor_name_str: {}", xor_name_str);
assert_eq!(record_str, xor_name_str);
Expand Down
8 changes: 4 additions & 4 deletions sn_networking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ use libp2p::{
use sn_protocol::{
messages::{Query, QueryResponse, Request, Response},
storage::{RecordHeader, RecordKind},
NetworkAddress, PrettyPrintRecordKey,
NetworkAddress, PrettyPrintKBucketKey, PrettyPrintRecordKey,
};
use sn_transfers::MainPubkey;
use sn_transfers::NanoTokens;
Expand Down Expand Up @@ -169,8 +169,8 @@ impl Network {
.map(|peer_id| {
format!(
"{peer_id:?}({:?})",
PrettyPrintRecordKey::from(
NetworkAddress::from_peer(*peer_id).to_record_key()
PrettyPrintKBucketKey(
NetworkAddress::from_peer(*peer_id).as_kbucket_key()
)
)
})
Expand Down Expand Up @@ -618,7 +618,7 @@ impl Network {
.map(|peer_id| {
format!(
"{peer_id:?}({:?})",
PrettyPrintRecordKey::from(NetworkAddress::from_peer(*peer_id).to_record_key())
PrettyPrintKBucketKey(NetworkAddress::from_peer(*peer_id).as_kbucket_key())
)
})
.collect();
Expand Down
6 changes: 2 additions & 4 deletions sn_node/src/replication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use libp2p::{
use sn_networking::{sort_peers_by_address, GetQuorum, CLOSE_GROUP_SIZE};
use sn_protocol::{
messages::{Cmd, Query, QueryResponse, Request, Response},
NetworkAddress, PrettyPrintRecordKey,
NetworkAddress, PrettyPrintKBucketKey, PrettyPrintRecordKey,
};
use std::collections::BTreeMap;
use tokio::task::JoinHandle;
Expand Down Expand Up @@ -72,9 +72,7 @@ impl Node {
.map(|peer_id| {
format!(
"{peer_id:?}({:?})",
PrettyPrintRecordKey::from(
NetworkAddress::from_peer(*peer_id).to_record_key()
)
PrettyPrintKBucketKey(NetworkAddress::from_peer(*peer_id).as_kbucket_key())
)
})
.collect();
Expand Down
1 change: 1 addition & 0 deletions sn_protocol/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ hex = "~0.4.3"
libp2p = { version="0.52", features = ["identify", "kad"] }
rmp-serde = "1.1.1"
serde = { version = "1.0.133", features = [ "derive", "rc" ]}
sha2 = "0.10.7"
sn_transfers = { path = "../sn_transfers", version = "0.14.3" }
sn_registers = { path = "../sn_registers", version = "0.3.2" }
thiserror = "1.0.23"
Expand Down
35 changes: 32 additions & 3 deletions sn_protocol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use libp2p::{
PeerId,
};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use sha2::{Digest, Sha256};
use std::fmt::{self, Debug, Display, Formatter};
use xor_name::XorName;

Expand Down Expand Up @@ -184,12 +185,15 @@ impl Debug for NetworkAddress {
"NetworkAddress::RegisterAddress({:?} - ",
register_address.xorname()
),
NetworkAddress::RecordKey(_) => "NetworkAddress::RecordKey(".to_string(),
NetworkAddress::RecordKey(bytes) => format!(
"NetworkAddress::RecordKey({:?} - ",
PrettyPrintRecordKey::from(RecordKey::new(bytes))
),
};
write!(
f,
"{name_str}{:?})",
PrettyPrintRecordKey::from(self.to_record_key()),
PrettyPrintKBucketKey(self.as_kbucket_key()),
)
}
}
Expand All @@ -216,6 +220,26 @@ impl Display for NetworkAddress {
}
}

/// Pretty print a `kad::KBucketKey` as a hex string.
#[derive(Clone)]
pub struct PrettyPrintKBucketKey(pub Key<Vec<u8>>);

impl std::fmt::Display for PrettyPrintKBucketKey {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
// The `KeyBytes` part of `KBucketKey` is private and no API to expose it.
// Hence here we have to carry out a hash manually to simulate its behaviour.
let generic_array = Sha256::digest(self.0.preimage());
let kbucket_key_b = Bytes::from(generic_array.to_vec());
write!(f, "{:64x}", kbucket_key_b)
}
}

impl std::fmt::Debug for PrettyPrintKBucketKey {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self)
}
}

/// Pretty print a `kad::RecordKey` as a hex string.
/// So clients can use the hex string for xorname and record keys interchangeably.
/// This makes errors actionable for clients.
Expand Down Expand Up @@ -258,7 +282,12 @@ impl std::fmt::Display for PrettyPrintRecordKey {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let b: Vec<u8> = self.0.as_ref().to_vec();
let record_key_b = Bytes::from(b);
write!(f, "{:64x}", record_key_b)
write!(
f,
"{:64x}({:?})",
record_key_b,
PrettyPrintKBucketKey(NetworkAddress::from_record_key(self.0.clone()).as_kbucket_key())
)
}
}

Expand Down