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

Use Marshal and UnmarshalJSON for HexBytes #239

Merged
merged 9 commits into from
Jul 29, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions libs/bytes/bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,28 @@ func (bz HexBytes) Format(s fmt.State, verb rune) {
s.Write([]byte(fmt.Sprintf("%X", []byte(bz))))
}
}

// Matches the hexbytes MarshalJSON of tendermint/tendermint. Overrides the
// default []byte Marshal implementation. This is basically the point of hex bytes.
func (bz HexBytes) MarshalJSON() ([]byte, error) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We should treat this as an app-hash breaking change due to potentially wide-ranging impact.

jewei1997 marked this conversation as resolved.
Show resolved Hide resolved
s := strings.ToUpper(hex.EncodeToString(bz))
jbz := make([]byte, len(s)+2)
jbz[0] = '"'
copy(jbz[1:], s)
jbz[len(jbz)-1] = '"'
return jbz, nil
}

// Matches the hexbytes UnmarshalJSON of tendermint/tendermint. Overrides the
// default []byte Marshal implementation. This is basically the point of hex bytes.
func (bz *HexBytes) UnmarshalJSON(data []byte) error {
if len(data) < 2 || data[0] != '"' || data[len(data)-1] != '"' {
return fmt.Errorf("invalid hex string: %s", data)
}
bz2, err := hex.DecodeString(string(data[1 : len(data)-1]))
if err != nil {
return err
}
*bz = bz2
return nil
}
38 changes: 38 additions & 0 deletions libs/bytes/bytes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,41 @@ func TestHexBytes_String(t *testing.T) {
t.Fatal(err)
}
}

// Define a struct to match the JSON structure
type ValidatorsHash struct {
NextValidatorsHash HexBytes `json:"next_validators_hash"`
}

func TestMarshalBasic(t *testing.T) {
var vh ValidatorsHash
vh.NextValidatorsHash = []byte("abc")
bz, err := json.Marshal(vh)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, string(bz), "{\"next_validators_hash\":\"616263\"}")
}

func TestUnmarshalBasic(t *testing.T) {
jsonData := []byte(`{"next_validators_hash":"616263"}`)
var vh ValidatorsHash
err := json.Unmarshal(jsonData, &vh)
if err != nil {
t.Fatalf("Error unmarshalling JSON: %v", err)
return
}
assert.Equal(t, string(vh.NextValidatorsHash), "abc")
}

func TestUnmarshalExample(t *testing.T) {
jsonData := []byte(`{"next_validators_hash":"20021C2FB4B2DDFF6E8C484A2ED5862910E3AD7074FC6AD1C972AD34891AE3A4"}`)
expectedLength := 32
var vh ValidatorsHash
err := json.Unmarshal(jsonData, &vh)
if err != nil {
t.Fatalf("Error unmarshalling JSON: %v", err)
return
}
assert.Equal(t, expectedLength, len(vh.NextValidatorsHash))
}
20 changes: 0 additions & 20 deletions light/mbt/doc.go

This file was deleted.

122 changes: 0 additions & 122 deletions light/mbt/driver_test.go

This file was deleted.

Loading
Loading