From 7368ff09ef8ed968c7dca33257dafd914bf4c4ce Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Thu, 10 Dec 2020 15:21:26 +0300 Subject: [PATCH] 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. --- pkg/rpc/response/result/invoke.go | 2 +- pkg/rpc/response/result/invoke_test.go | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/pkg/rpc/response/result/invoke.go b/pkg/rpc/response/result/invoke.go index 922dcdcad..bfd4c72ea 100644 --- a/pkg/rpc/response/result/invoke.go +++ b/pkg/rpc/response/result/invoke.go @@ -26,7 +26,7 @@ type Invoke struct { type invokeAux struct { State string `json:"state"` - GasConsumed fixedn.Fixed8 `json:"gasconsumed,string"` + GasConsumed fixedn.Fixed8 `json:"gasconsumed"` Script []byte `json:"script"` Stack json.RawMessage `json:"stack"` FaultException string `json:"exception,omitempty"` diff --git a/pkg/rpc/response/result/invoke_test.go b/pkg/rpc/response/result/invoke_test.go index 611831acb..f1975d42b 100644 --- a/pkg/rpc/response/result/invoke_test.go +++ b/pkg/rpc/response/result/invoke_test.go @@ -6,7 +6,6 @@ import ( "math/big" "testing" - "github.com/nspcc-dev/neo-go/pkg/encoding/fixedn" "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" "github.com/stretchr/testify/require" ) @@ -14,7 +13,7 @@ import ( func TestInvoke_MarshalJSON(t *testing.T) { result := &Invoke{ State: "HALT", - GasConsumed: int64(fixedn.Fixed8FromFloat(123.45)), + GasConsumed: 237626000, Script: []byte{10}, Stack: []stackitem.Item{stackitem.NewBigInteger(big.NewInt(1))}, FaultException: "", @@ -26,7 +25,7 @@ func TestInvoke_MarshalJSON(t *testing.T) { require.NoError(t, err) expected := `{ "state":"HALT", - "gasconsumed":"123.45", + "gasconsumed":"2.37626", "script":"` + base64.StdEncoding.EncodeToString(result.Script) + `", "stack":[ {"type":"Integer","value":"1"}