Skip to content

Commit

Permalink
Format some logs better, ref ethereum#150
Browse files Browse the repository at this point in the history
  • Loading branch information
lithp committed Oct 26, 2021
1 parent c6b77ab commit d18796d
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 11 deletions.
28 changes: 24 additions & 4 deletions trin-core/src/portalnet/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,26 @@ impl PortalnetEvents {
/// Receives a request from the talkreq handler and sends a response back
pub async fn process_discv5_requests(mut self) {
while let Some(event) = self.protocol_receiver.recv().await {
debug!("Got discv5 event {:?}", event);

let request = match event {
Discv5Event::TalkRequest(r) => r,
_ => continue,
Discv5Event::NodeInserted { node_id, replaced } => {
match replaced {
Some(old_node_id) => {
debug!(
"Received NodeInserted(node_id={}, replaced={})",
node_id, old_node_id,
);
}
None => {
debug!("Received NodeInserted(node_id={})", node_id);
}
}
continue;
}
event => {
debug!("Got discv5 event {:?}", event);
continue;
}
};

match std::str::from_utf8(request.protocol()) {
Expand All @@ -82,7 +97,12 @@ impl PortalnetEvents {
self.process_utp_byte_stream().await;
}
_ => {
warn!("Non supported protocol : {}", protocol);
warn!(
"Received TalkRequest on uknown protocol from={} protocol={} body={}",
request.node_id(),
hex::encode(request.protocol()),
hex::encode(request.body()),
);
}
},
Err(_) => warn!("Invalid utf8 protocol decode"),
Expand Down
13 changes: 9 additions & 4 deletions trin-core/src/portalnet/overlay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,20 @@ impl OverlayProtocol {
};

let response = match request {
Request::Ping(Ping { .. }) => {
debug!("Got overlay ping request {:?}", request);
Request::Ping(ping) => {
debug!("Received {}", ping);
let enr_seq = self.discovery.read_with_warn().await.local_enr().seq();
let payload = CustomPayload::new(self.data_radius().await, None);
Response::Pong(Pong {
let pong = Pong {
enr_seq,
payload: Some(payload),
})
};
debug!("Sending {}", pong);

Response::Pong(pong)
}
Request::FindNodes(FindNodes { distances }) => {
debug!("Received FindNodes(distances={:?})", distances);
let distances64: Vec<u64> = distances.iter().map(|x| (*x).into()).collect();
let enrs = self.nodes_by_distance(distances64).await;
Response::Nodes(Nodes {
Expand Down Expand Up @@ -284,6 +288,7 @@ impl OverlayProtocol {
enr_seq,
payload: Some(payload),
};
debug!("Sending {} dest={}", msg, enr.node_id());
Ok(self
.discovery
.read_with_warn()
Expand Down
57 changes: 57 additions & 0 deletions trin-core/src/portalnet/types.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::convert::{TryFrom, TryInto};
use std::fmt;
use std::net::SocketAddr;
use std::ops::{Deref, DerefMut};
use std::str::FromStr;
Expand Down Expand Up @@ -194,12 +195,68 @@ pub struct Ping {
pub payload: Option<CustomPayload>,
}

impl fmt::Display for Ping {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.payload {
None => {
write!(f, "Ping(enr_seq={})", self.enr_seq)
}
Some(ref custom_payload) => match custom_payload.payload {
None => {
write!(
f,
"Ping(enr_seq={}, radius={})",
self.enr_seq, custom_payload.data_radius,
)
}
Some(ref bytelist) => {
write!(
f,
"Ping(enr_seq={}, radius={}, payload={})",
self.enr_seq,
custom_payload.data_radius,
hex::encode(bytelist.as_ssz_bytes())
)
}
},
}
}
}

#[derive(Debug, PartialEq, Clone, Encode, Decode)]
pub struct Pong {
pub enr_seq: u64,
pub payload: Option<CustomPayload>,
}

impl fmt::Display for Pong {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.payload {
None => {
write!(f, "Pong(enr_seq={})", self.enr_seq)
}
Some(ref custom_payload) => match custom_payload.payload {
None => {
write!(
f,
"Pong(enr_seq={}, radius={})",
self.enr_seq, custom_payload.data_radius,
)
}
Some(ref bytelist) => {
write!(
f,
"Pong(enr_seq={}, radius={}, payload={})",
self.enr_seq,
custom_payload.data_radius,
hex::encode(bytelist.as_ssz_bytes())
)
}
},
}
}
}

#[derive(Debug, PartialEq, Clone, Encode, Decode)]
pub struct FindNodes {
// TODO: Make this an ssz list
Expand Down
7 changes: 4 additions & 3 deletions trin-state/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ pub struct StateEvents {
impl StateEvents {
pub async fn process_requests(mut self) {
while let Some(talk_request) = self.event_rx.recv().await {
debug!("Got state request {:?}", talk_request);

let reply = match self
.network
.write_with_warn()
Expand All @@ -24,7 +22,10 @@ impl StateEvents {
.process_one_request(&talk_request)
.await
{
Ok(r) => Message::Response(r).to_bytes(),
Ok(r) => {
debug!("Sending reply: {:?}", r);
Message::Response(r).to_bytes()
}
Err(e) => {
error!("failed to process portal state event: {}", e);
e.into_bytes()
Expand Down

0 comments on commit d18796d

Please # to comment.