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: implement Debug for PingError so messages display #1659

Merged
merged 2 commits into from
Feb 1, 2025

Conversation

KolbyML
Copy link
Member

@KolbyML KolbyML commented Feb 1, 2025

What was wrong?

The default derived Debug implementation printed the message as bytes

How was it fixed?

Implement Debug manually to decode the Ping Message, there is no point in decoding it unless we print it so I think this strikes a pretty good balance.

Copy link
Collaborator

@morph-dev morph-dev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Few small comments.

:shipit:

.map_err(|_| {
ssz::DecodeError::BytesInvalid(format!("Invalid utf8 string: {:?}", self.message))
})
.map_err(|_| std::fmt::Error)?;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this map_err completely invalidate previous one and basically always return std::fmt::Error?

How about we do something like this:

let message = String::from_utf8(&self.message)
    .unwrap_or_else(|_| format!("Invalid utf8 string: {}", hex_encode(&self.message)));

f.debug_struct("PingError")
    .field("error_code", &self.error_code)
    .field("message", &message)
    .finish()

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch

@@ -6,7 +8,7 @@ use ssz_types::{typenum::U300, VariableList};
use crate::types::portal_wire::CustomPayload;

/// Used to respond to pings which the node can't handle
#[derive(PartialEq, Debug, Clone, Encode, Decode)]
#[derive(PartialEq, Clone, Encode, Decode)]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: In my opinion, if structure implements PartialEq, it should also implement Eq if it applies for it.

@KolbyML KolbyML force-pushed the add-debug-for-discovery branch from 6655266 to 3ce5401 Compare February 1, 2025 19:37
@@ -30,6 +32,18 @@ impl PingError {
}
}

impl Debug for PingError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let message = String::from_utf8(self.message.to_vec())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: you should be able to use &*self.message here as well (instead of calling to_vec())

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That doesn't compile String::from_utf8 only takes a Vec

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

&* gives you a slice

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like milos was looking at str::from_utf8 well I was looking at String::from_utf8

@KolbyML KolbyML merged commit eec13c6 into ethereum:master Feb 1, 2025
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants