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

Add JSON marshalling to Result #1627

Merged
merged 1 commit into from
Oct 3, 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
44 changes: 44 additions & 0 deletions chain/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package chain

import (
"encoding/json"

"github.com/ava-labs/hypersdk/codec"
"github.com/ava-labs/hypersdk/consts"
"github.com/ava-labs/hypersdk/fees"
Expand All @@ -21,6 +23,48 @@ type Result struct {
Fee uint64
}

type ResultJSON struct {
Success bool `json:"success"`
Error codec.Bytes `json:"error"`

Outputs []codec.Bytes `json:"outputs"`
Units fees.Dimensions `json:"units"`
Fee uint64 `json:"fee"`
}

func (r Result) MarshalJSON() ([]byte, error) {
outputs := make([]codec.Bytes, len(r.Outputs))
for i, output := range r.Outputs {
outputs[i] = output
}
resultJSON := ResultJSON{
Success: r.Success,
Error: r.Error,
Outputs: outputs,
Units: r.Units,
Fee: r.Fee,
}

return json.Marshal(resultJSON)
}

func (r *Result) UnmarshalJSON(data []byte) error {
var resultJSON ResultJSON
if err := json.Unmarshal(data, &resultJSON); err != nil {
return err
}

r.Success = resultJSON.Success
r.Error = resultJSON.Error
r.Outputs = make([][]byte, len(resultJSON.Outputs))
for i, output := range resultJSON.Outputs {
r.Outputs[i] = output
}
r.Units = resultJSON.Units
r.Fee = resultJSON.Fee
return nil
}

func (r *Result) Size() int {
outputSize := consts.Uint8Len // actions
for _, actionOutput := range r.Outputs {
Expand Down
41 changes: 41 additions & 0 deletions chain/result_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (C) 2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package chain

import (
"encoding/json"
"fmt"
"testing"

"github.com/stretchr/testify/require"

"github.com/ava-labs/hypersdk/codec"
"github.com/ava-labs/hypersdk/fees"
)

func TestResultJSON(t *testing.T) {
require := require.New(t)
errStrBytes := []byte("error")
outputBytes := []byte("output")
units := fees.Dimensions{1, 2, 3, 4, 5}
unitsJSON, err := json.Marshal(units)
require.NoError(err)
result := Result{
Success: true,
Error: errStrBytes,
Outputs: [][]byte{outputBytes},
Units: units,
Fee: 4,
}

resultJSON, err := json.Marshal(result)
require.NoError(err)

expectedJSON := fmt.Sprintf(`{"error":%q,"fee":4,"outputs":[%q],"success":true,"units":%s}`, codec.Bytes(errStrBytes), codec.Bytes(outputBytes), string(unitsJSON))
Copy link
Contributor

Choose a reason for hiding this comment

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

(No action required) Maybe just round trip the Result type and compare the original and the unmarshaled values?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'd prefer to include the expectedJSON check as well to make sure it's clear that it's returning reasonable JSON output. THis makes it more explicit that we don't have any unexpected values like JSON keys that start with capital letters because there are no annotations.

Certainly may remove this at some point, but I think this is reasonable to include as we are cleaning up the JSON output.

require.JSONEq(expectedJSON, string(resultJSON))

var unmarshalledResult Result
require.NoError(json.Unmarshal(resultJSON, &unmarshalledResult))
require.Equal(result, unmarshalledResult)
}
6 changes: 5 additions & 1 deletion codec/hex.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@ func LoadHex(s string, expectedSize int) ([]byte, error) {

type Bytes []byte

func (b Bytes) String() string {
return ToHex(b)
}

// MarshalText returns the hex representation of b.
func (b Bytes) MarshalText() ([]byte, error) {
return []byte(ToHex(b)), nil
return []byte(b.String()), nil
}

// UnmarshalText sets b to the bytes represented by text.
Expand Down
Loading