rpc: fix getaccountstate/getunspents for unknown addresses

Returning error string as a result (not an error) is utterly wrong, but C#
implementation just returns a zero balance for unknown addresses, so we should
follow that.
This commit is contained in:
Roman Khimov 2020-01-29 19:01:00 +03:00
parent 0ccc59628c
commit b1b660c779
2 changed files with 21 additions and 7 deletions

View file

@ -9,6 +9,7 @@ import (
"github.com/CityOfZion/neo-go/config" "github.com/CityOfZion/neo-go/config"
"github.com/CityOfZion/neo-go/pkg/core" "github.com/CityOfZion/neo-go/pkg/core"
"github.com/CityOfZion/neo-go/pkg/core/state"
"github.com/CityOfZion/neo-go/pkg/core/transaction" "github.com/CityOfZion/neo-go/pkg/core/transaction"
"github.com/CityOfZion/neo-go/pkg/io" "github.com/CityOfZion/neo-go/pkg/io"
"github.com/CityOfZion/neo-go/pkg/network" "github.com/CityOfZion/neo-go/pkg/network"
@ -320,7 +321,11 @@ func (s *Server) getAccountState(reqParams Params, unspents bool) (interface{},
return nil, errInvalidParams return nil, errInvalidParams
} else if scriptHash, err := param.GetUint160FromAddress(); err != nil { } else if scriptHash, err := param.GetUint160FromAddress(); err != nil {
return nil, errInvalidParams return nil, errInvalidParams
} else if as := s.chain.GetAccountState(scriptHash); as != nil { } else {
as := s.chain.GetAccountState(scriptHash)
if as == nil {
as = state.NewAccount(scriptHash)
}
if unspents { if unspents {
str, err := param.GetString() str, err := param.GetString()
if err != nil { if err != nil {
@ -330,8 +335,6 @@ func (s *Server) getAccountState(reqParams Params, unspents bool) (interface{},
} else { } else {
results = wrappers.NewAccountState(as) results = wrappers.NewAccountState(as)
} }
} else {
results = "Invalid public account address"
} }
return results, resultsErr return results, resultsErr
} }

View file

@ -50,9 +50,15 @@ var rpcTestCases = map[string][]rpcTestCase{
}, },
}, },
{ {
name: "negative", name: "positive null",
params: `["AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y"]`, params: `["AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y"]`,
result: func(e *executor) interface{} { return "Invalid public account address" }, result: func(e *executor) interface{} { return &GetAccountStateResponse{} },
check: func(t *testing.T, e *executor, result interface{}) {
res, ok := result.(*GetAccountStateResponse)
require.True(t, ok)
assert.Equal(t, 0, len(res.Result.Balances))
assert.Equal(t, false, res.Result.Frozen)
},
}, },
{ {
name: "no params", name: "no params",
@ -235,9 +241,14 @@ var rpcTestCases = map[string][]rpcTestCase{
}, },
}, },
{ {
name: "negative", name: "positive null",
params: `["AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y"]`, params: `["AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y"]`,
result: func(e *executor) interface{} { return "Invalid public account address" }, result: func(e *executor) interface{} { return &GetUnspents{} },
check: func(t *testing.T, e *executor, result interface{}) {
res, ok := result.(*GetUnspents)
require.True(t, ok)
require.Equal(t, 0, len(res.Result.Balance))
},
}, },
}, },
"getversion": { "getversion": {