Skip to content

fix(ids): %x formatting of ID and ShortID #3956

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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open

Conversation

ARR4N
Copy link
Contributor

@ARR4N ARR4N commented May 12, 2025

Why this should be merged

fmt.Sprintf("%x", ids.ID{}) returns the hex encoding of the cb58 encoding, not of the raw bytes.

How this works

Implement the fmt.Formatter interface for both ids.ID and ids.ShortID.

How this was tested

Unit test of formatting with %v, %s and %q (equivalent to String()), plus %x and %#x (equivalent to Hex()).

Need to be documented in RELEASES.md?

Not sure. Maybe. I dunno ¯\_(ツ)_/¯

@Copilot Copilot AI review requested due to automatic review settings May 12, 2025 11:09
@ARR4N ARR4N requested a review from StephenButtolph as a code owner May 12, 2025 11:09
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes the formatting behavior for ID and ShortID by implementing the fmt.Formatter interface, ensuring that the %x and %#x verbs produce the expected hex output instead of the cb58 encoding.

  • Implemented the fmt.Formatter interface for both ID and ShortID via a shared generic function.
  • Added unit tests to verify the behavior with multiple format specifiers and a benchmark to demonstrate stack allocation in %q formatting.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
ids/format_test.go Added tests to confirm proper formatting for various specifiers.
ids/format.go Implemented fmt.Formatter interface for ID and ShortID types.

@@ -0,0 +1,40 @@
package ids
Copy link
Contributor

Choose a reason for hiding this comment

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

why not put this in id.go ? it's only 40 LOC and the other formatting stuff are there.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It would also need to be in short.go and then the common implementation would be separated from at least one use case. Here they naturally stay together and provide the reader with context.

for _, tt := range tests {
t.Run(tt.id.String(), func(t *testing.T) {
for format, want := range tt.want {
if got := fmt.Sprintf(format, tt.id); got != want {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm curious to know the use case for this PR. We don't pass ID into Sprintf but use the logger, and there we use zap.Stringer.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In an EVM implementation ids.ID is typically derived from a common.Hash (both [32]byte), which is idiomatically rendered as hex. My errors that include a block hash/ID use %#x.

@ARR4N ARR4N requested a review from yacovm May 12, 2025 11:47
Comment on lines +25 to +28
// format implements the [fmt.Formatter] interface for [ID] and [ShortID].
func format[T interface {
idForFormatting
ID | ShortID
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we constrain the types to be either ID or ShortID?

@joshua-kim joshua-kim moved this to In Progress 🏗️ in avalanchego May 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: In Progress 🏗️
Development

Successfully merging this pull request may close these issues.

3 participants