From 2ab23dc56af801d702ebac31906e94ba4ca57ff0 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Thu, 9 Jul 2020 17:25:26 +0300 Subject: [PATCH] rpc: adjust `getunclaimedgas` RPC-call Part of #1130 --- pkg/rpc/client/rpc.go | 13 ++---- pkg/rpc/client/rpc_test.go | 15 +++++-- pkg/rpc/response/result/unclaimed_gas.go | 50 ++++++++++++++++++++++++ pkg/rpc/server/server.go | 9 ++++- pkg/rpc/server/server_test.go | 11 ++++-- 5 files changed, 80 insertions(+), 18 deletions(-) create mode 100644 pkg/rpc/response/result/unclaimed_gas.go diff --git a/pkg/rpc/client/rpc.go b/pkg/rpc/client/rpc.go index d734d1104..00165bd47 100644 --- a/pkg/rpc/client/rpc.go +++ b/pkg/rpc/client/rpc.go @@ -2,7 +2,6 @@ package client import ( "encoding/hex" - "strconv" "github.com/nspcc-dev/neo-go/pkg/core" "github.com/nspcc-dev/neo-go/pkg/core/block" @@ -308,19 +307,15 @@ func (c *Client) GetTransactionHeight(hash util.Uint256) (uint32, error) { } // GetUnclaimedGas returns unclaimed GAS amount for the specified address. -func (c *Client) GetUnclaimedGas(address string) (util.Fixed8, error) { +func (c *Client) GetUnclaimedGas(address string) (result.UnclaimedGas, error) { var ( params = request.NewRawParams(address) - resp string + resp result.UnclaimedGas ) if err := c.performRequest("getunclaimedgas", params, &resp); err != nil { - return 0, err + return resp, err } - i, err := strconv.ParseInt(resp, 10, 64) - if err != nil { - return 0, err - } - return util.Fixed8(i), nil + return resp, nil } // GetValidators returns the current NEO consensus nodes information and voting status. diff --git a/pkg/rpc/client/rpc_test.go b/pkg/rpc/client/rpc_test.go index cf611e0d8..38365491c 100644 --- a/pkg/rpc/client/rpc_test.go +++ b/pkg/rpc/client/rpc_test.go @@ -4,6 +4,7 @@ import ( "context" "encoding/base64" "encoding/hex" + "math/big" "net/http" "net/http/httptest" "strings" @@ -25,6 +26,7 @@ import ( "github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest" "github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/vm/opcode" + "github.com/pkg/errors" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -573,11 +575,18 @@ var rpcClientTestCases = map[string][]rpcClientTestCase{ { name: "positive", invoke: func(c *Client) (interface{}, error) { - return c.GetUnclaimedGas("AGofsxAUDwt52KjaB664GYsqVAkULYvKNt") + return c.GetUnclaimedGas("NMipL5VsNoLUBUJKPKLhxaEbPQVCZnyJyB") }, - serverResponse: `{"jsonrpc":"2.0","id":1,"result":"897299680935"}`, + serverResponse: `{"jsonrpc":"2.0","id":1,"result":{"address":"NMipL5VsNoLUBUJKPKLhxaEbPQVCZnyJyB","unclaimed":"897299680935"}}`, result: func(c *Client) interface{} { - return util.Fixed8(897299680935) + addr, err := address.StringToUint160("NMipL5VsNoLUBUJKPKLhxaEbPQVCZnyJyB") + if err != nil { + panic(errors.Wrap(err, "failed to parse UnclaimedGas address")) + } + return result.UnclaimedGas{ + Address: addr, + Unclaimed: *big.NewInt(897299680935), + } }, }, }, diff --git a/pkg/rpc/response/result/unclaimed_gas.go b/pkg/rpc/response/result/unclaimed_gas.go new file mode 100644 index 000000000..af051acdd --- /dev/null +++ b/pkg/rpc/response/result/unclaimed_gas.go @@ -0,0 +1,50 @@ +package result + +import ( + "encoding/json" + "errors" + "math/big" + + "github.com/nspcc-dev/neo-go/pkg/encoding/address" + "github.com/nspcc-dev/neo-go/pkg/util" +) + +// UnclaimedGas response wrapper +type UnclaimedGas struct { + Address util.Uint160 + Unclaimed big.Int +} + +// unclaimedGas is an auxiliary struct for JSON marhsalling +type unclaimedGas struct { + Address string `json:"address"` + Unclaimed string `json:"unclaimed"` +} + +// MarshalJSON implements json.Marshaler interface. +func (g UnclaimedGas) MarshalJSON() ([]byte, error) { + gas := &unclaimedGas{ + Address: address.Uint160ToString(g.Address), + Unclaimed: g.Unclaimed.String(), + } + return json.Marshal(gas) +} + +// UnmarshalJSON implements json.Unmarshaler interface. +func (g *UnclaimedGas) UnmarshalJSON(data []byte) error { + gas := new(unclaimedGas) + if err := json.Unmarshal(data, gas); err != nil { + return err + } + uncl, ok := new(big.Int).SetString(gas.Unclaimed, 10) + if !ok { + return errors.New("failed to convert unclaimed gas") + } + g.Unclaimed = *uncl + addr, err := address.StringToUint160(gas.Address) + if err != nil { + return err + } + g.Address = addr + return nil +} diff --git a/pkg/rpc/server/server.go b/pkg/rpc/server/server.go index a597bfe7e..c79ee9fe2 100644 --- a/pkg/rpc/server/server.go +++ b/pkg/rpc/server/server.go @@ -784,10 +784,15 @@ func (s *Server) getUnclaimedGas(ps request.Params) (interface{}, *response.Erro neo, neoHeight := s.chain.GetGoverningTokenBalance(u) if neo.Sign() == 0 { - return "0", nil + return result.UnclaimedGas{ + Address: u, + }, nil } gas := s.chain.CalculateClaimable(neo, neoHeight, s.chain.BlockHeight()+1) // +1 as in C#, for the next block. - return gas.String(), nil + return result.UnclaimedGas{ + Address: u, + Unclaimed: *gas, + }, nil } // getValidators returns the current NEO consensus nodes information and voting status. diff --git a/pkg/rpc/server/server_test.go b/pkg/rpc/server/server_test.go index 59100f6c3..d30fdb31f 100644 --- a/pkg/rpc/server/server_test.go +++ b/pkg/rpc/server/server_test.go @@ -560,13 +560,16 @@ var rpcTestCases = map[string][]rpcTestCase{ name: "positive", params: `["` + testchain.MultisigAddress() + `"]`, result: func(*executor) interface{} { - var s string - return &s + return &result.UnclaimedGas{} }, check: func(t *testing.T, e *executor, resp interface{}) { - s, ok := resp.(*string) + actual, ok := resp.(*result.UnclaimedGas) require.True(t, ok) - assert.Equal(t, "36000", *s) + expected := result.UnclaimedGas{ + Address: testchain.MultisigScriptHash(), + Unclaimed: *big.NewInt(36000), + } + assert.Equal(t, expected, *actual) }, }, },