rpc: marshal GAS correctly

When using ",string" in JSON struct tag, value is first unmarshaled to
a numeric value (float64 in our case), then to our real type via
`UnmarshalJSON()`, which can lead to rounding errors.
This commit is contained in:
Evgenii Stratonikov 2020-12-10 15:21:26 +03:00
parent 1fee268f95
commit 7368ff09ef
2 changed files with 3 additions and 4 deletions

View file

@ -26,7 +26,7 @@ type Invoke struct {
type invokeAux struct { type invokeAux struct {
State string `json:"state"` State string `json:"state"`
GasConsumed fixedn.Fixed8 `json:"gasconsumed,string"` GasConsumed fixedn.Fixed8 `json:"gasconsumed"`
Script []byte `json:"script"` Script []byte `json:"script"`
Stack json.RawMessage `json:"stack"` Stack json.RawMessage `json:"stack"`
FaultException string `json:"exception,omitempty"` FaultException string `json:"exception,omitempty"`

View file

@ -6,7 +6,6 @@ import (
"math/big" "math/big"
"testing" "testing"
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem" "github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -14,7 +13,7 @@ import (
func TestInvoke_MarshalJSON(t *testing.T) { func TestInvoke_MarshalJSON(t *testing.T) {
result := &Invoke{ result := &Invoke{
State: "HALT", State: "HALT",
GasConsumed: int64(fixedn.Fixed8FromFloat(123.45)), GasConsumed: 237626000,
Script: []byte{10}, Script: []byte{10},
Stack: []stackitem.Item{stackitem.NewBigInteger(big.NewInt(1))}, Stack: []stackitem.Item{stackitem.NewBigInteger(big.NewInt(1))},
FaultException: "", FaultException: "",
@ -26,7 +25,7 @@ func TestInvoke_MarshalJSON(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
expected := `{ expected := `{
"state":"HALT", "state":"HALT",
"gasconsumed":"123.45", "gasconsumed":"2.37626",
"script":"` + base64.StdEncoding.EncodeToString(result.Script) + `", "script":"` + base64.StdEncoding.EncodeToString(result.Script) + `",
"stack":[ "stack":[
{"type":"Integer","value":"1"} {"type":"Integer","value":"1"}