rpc: adjust getunclaimedgas RPC-call

Part of #1130
This commit is contained in:
Anna Shaleva 2020-07-09 17:25:26 +03:00
parent 3a3cd0353d
commit 2ab23dc56a
5 changed files with 80 additions and 18 deletions

View file

@ -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.

View file

@ -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),
}
},
},
},

View file

@ -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
}

View file

@ -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.

View file

@ -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)
},
},
},