rpcclient: use Invoker internally for external APIs

It's not a big improvement, but it allows to test Invoker better.
This commit is contained in:
Roman Khimov 2022-08-01 13:37:07 +03:00
parent fee7e2f223
commit b52282c3c7
5 changed files with 24 additions and 75 deletions

View file

@ -14,6 +14,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
"github.com/nspcc-dev/neo-go/pkg/neorpc"
"github.com/nspcc-dev/neo-go/pkg/rpcclient/invoker"
"github.com/nspcc-dev/neo-go/pkg/util"
"go.uber.org/atomic"
)
@ -35,6 +36,11 @@ type Client struct {
opts Options
requestF func(*neorpc.Request) (*neorpc.Response, error)
// reader is an Invoker that has no signers and uses current state,
// it's used to implement various getters. It'll be removed eventually,
// but for now it keeps Client's API compatibility.
reader *invoker.Invoker
cacheLock sync.RWMutex
// cache stores RPC node related information the client is bound to.
// cache is mostly filled in during Init(), but can also be updated
@ -128,6 +134,7 @@ func initClient(ctx context.Context, cl *Client, endpoint string, opts Options)
cl.getNextRequestID = (cl).getRequestID
cl.opts = opts
cl.requestF = cl.makeHTTPRequest
cl.reader = invoker.New(cl, nil)
return nil
}

View file

@ -5,7 +5,6 @@ package rpcclient
import (
"errors"
"fmt"
"math/big"
"github.com/google/uuid"
"github.com/nspcc-dev/neo-go/pkg/core/native/nativenames"
@ -55,16 +54,7 @@ func (c *Client) GetDesignatedByRole(role noderoles.Role, index uint32) (keys.Pu
if err != nil {
return nil, fmt.Errorf("failed to get native RoleManagement hash: %w", err)
}
result, err := c.InvokeFunction(rmHash, "getDesignatedByRole", []smartcontract.Parameter{
{
Type: smartcontract.IntegerType,
Value: big.NewInt(int64(role)),
},
{
Type: smartcontract.IntegerType,
Value: big.NewInt(int64(index)),
},
}, nil)
result, err := c.reader.Call(rmHash, "getDesignatedByRole", int64(role), index)
if err != nil {
return nil, err
}
@ -80,16 +70,7 @@ func (c *Client) NNSResolve(nnsHash util.Uint160, name string, typ nns.RecordTyp
if typ == nns.CNAME {
return "", errors.New("can't resolve CNAME record type")
}
result, err := c.InvokeFunction(nnsHash, "resolve", []smartcontract.Parameter{
{
Type: smartcontract.StringType,
Value: name,
},
{
Type: smartcontract.IntegerType,
Value: big.NewInt(int64(typ)),
},
}, nil)
result, err := c.reader.Call(nnsHash, "resolve", name, int64(typ))
if err != nil {
return "", err
}
@ -102,12 +83,7 @@ func (c *Client) NNSResolve(nnsHash util.Uint160, name string, typ nns.RecordTyp
// NNSIsAvailable invokes `isAvailable` method on a NeoNameService contract with the specified hash.
func (c *Client) NNSIsAvailable(nnsHash util.Uint160, name string) (bool, error) {
result, err := c.InvokeFunction(nnsHash, "isAvailable", []smartcontract.Parameter{
{
Type: smartcontract.StringType,
Value: name,
},
}, nil)
result, err := c.reader.Call(nnsHash, "isAvailable", name)
if err != nil {
return false, err
}
@ -124,12 +100,7 @@ func (c *Client) NNSIsAvailable(nnsHash util.Uint160, name string) (bool, error)
// TerminateSession to terminate opened iterator session. See TraverseIterator and
// TerminateSession documentation for more details.
func (c *Client) NNSGetAllRecords(nnsHash util.Uint160, name string) (uuid.UUID, result.Iterator, error) {
res, err := c.InvokeFunction(nnsHash, "getAllRecords", []smartcontract.Parameter{
{
Type: smartcontract.StringType,
Value: name,
},
}, nil)
res, err := c.reader.Call(nnsHash, "getAllRecords", name)
if err != nil {
return uuid.UUID{}, result.Iterator{}, err
}

View file

@ -3,14 +3,13 @@ package rpcclient
import (
"fmt"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/wallet"
)
// nepDecimals invokes `decimals` NEP* method on the specified contract.
func (c *Client) nepDecimals(tokenHash util.Uint160) (int64, error) {
result, err := c.InvokeFunction(tokenHash, "decimals", []smartcontract.Parameter{}, nil)
result, err := c.reader.Call(tokenHash, "decimals")
if err != nil {
return 0, err
}
@ -24,7 +23,7 @@ func (c *Client) nepDecimals(tokenHash util.Uint160) (int64, error) {
// nepSymbol invokes `symbol` NEP* method on the specified contract.
func (c *Client) nepSymbol(tokenHash util.Uint160) (string, error) {
result, err := c.InvokeFunction(tokenHash, "symbol", []smartcontract.Parameter{}, nil)
result, err := c.reader.Call(tokenHash, "symbol")
if err != nil {
return "", err
}
@ -38,7 +37,7 @@ func (c *Client) nepSymbol(tokenHash util.Uint160) (string, error) {
// nepTotalSupply invokes `totalSupply` NEP* method on the specified contract.
func (c *Client) nepTotalSupply(tokenHash util.Uint160) (int64, error) {
result, err := c.InvokeFunction(tokenHash, "totalSupply", []smartcontract.Parameter{}, nil)
result, err := c.reader.Call(tokenHash, "totalSupply")
if err != nil {
return 0, err
}
@ -52,17 +51,11 @@ func (c *Client) nepTotalSupply(tokenHash util.Uint160) (int64, error) {
// nepBalanceOf invokes `balanceOf` NEP* method on the specified contract.
func (c *Client) nepBalanceOf(tokenHash, acc util.Uint160, tokenID []byte) (int64, error) {
params := []smartcontract.Parameter{{
Type: smartcontract.Hash160Type,
Value: acc,
}}
params := []interface{}{acc}
if tokenID != nil {
params = append(params, smartcontract.Parameter{
Type: smartcontract.ByteArrayType,
Value: tokenID,
})
params = append(params, tokenID)
}
result, err := c.InvokeFunction(tokenHash, "balanceOf", params, nil)
result, err := c.reader.Call(tokenHash, "balanceOf", params...)
if err != nil {
return 0, err
}

View file

@ -84,12 +84,7 @@ func (c *Client) CreateNEP11TransferTx(acc *wallet.Account, tokenHash util.Uint1
// traverse iterator values or TerminateSession to terminate opened iterator
// session. See TraverseIterator and TerminateSession documentation for more details.
func (c *Client) NEP11TokensOf(tokenHash util.Uint160, owner util.Uint160) (uuid.UUID, result.Iterator, error) {
res, err := c.InvokeFunction(tokenHash, "tokensOf", []smartcontract.Parameter{
{
Type: smartcontract.Hash160Type,
Value: owner,
},
}, nil)
res, err := c.reader.Call(tokenHash, "tokensOf", owner)
if err != nil {
return uuid.UUID{}, result.Iterator{}, err
}
@ -136,12 +131,7 @@ func (c *Client) NEP11UnpackedTokensOf(tokenHash util.Uint160, owner util.Uint16
// NEP11NDOwnerOf invokes `ownerOf` non-divisible NEP-11 method with the
// specified token ID on the specified contract.
func (c *Client) NEP11NDOwnerOf(tokenHash util.Uint160, tokenID []byte) (util.Uint160, error) {
result, err := c.InvokeFunction(tokenHash, "ownerOf", []smartcontract.Parameter{
{
Type: smartcontract.ByteArrayType,
Value: tokenID,
},
}, nil)
result, err := c.reader.Call(tokenHash, "ownerOf", tokenID)
if err != nil {
return util.Uint160{}, err
}
@ -186,12 +176,7 @@ func (c *Client) NEP11DBalanceOf(tokenHash, owner util.Uint160, tokenID []byte)
// method to traverse iterator values or TerminateSession to terminate opened iterator session. See
// TraverseIterator and TerminateSession documentation for more details.
func (c *Client) NEP11DOwnerOf(tokenHash util.Uint160, tokenID []byte) (uuid.UUID, result.Iterator, error) {
res, err := c.InvokeFunction(tokenHash, "ownerOf", []smartcontract.Parameter{
{
Type: smartcontract.ByteArrayType,
Value: tokenID,
},
}, nil)
res, err := c.reader.Call(tokenHash, "ownerOf", tokenID)
sessID := res.Session
if err != nil {
return sessID, result.Iterator{}, err
@ -241,10 +226,7 @@ func (c *Client) NEP11DUnpackedOwnerOf(tokenHash util.Uint160, tokenID []byte) (
// NEP11Properties invokes `properties` optional NEP-11 method on the
// specified contract.
func (c *Client) NEP11Properties(tokenHash util.Uint160, tokenID []byte) (*stackitem.Map, error) {
result, err := c.InvokeFunction(tokenHash, "properties", []smartcontract.Parameter{{
Type: smartcontract.ByteArrayType,
Value: tokenID,
}}, nil)
result, err := c.reader.Call(tokenHash, "properties", tokenID)
if err != nil {
return nil, err
}
@ -262,7 +244,7 @@ func (c *Client) NEP11Properties(tokenHash util.Uint160, tokenID []byte) (*stack
// TerminateSession to terminate opened iterator session. See TraverseIterator and
// TerminateSession documentation for more details.
func (c *Client) NEP11Tokens(tokenHash util.Uint160) (uuid.UUID, result.Iterator, error) {
res, err := c.InvokeFunction(tokenHash, "tokens", []smartcontract.Parameter{}, nil)
res, err := c.reader.Call(tokenHash, "tokens")
if err != nil {
return uuid.UUID{}, result.Iterator{}, err
}

View file

@ -4,7 +4,6 @@ import (
"fmt"
"github.com/nspcc-dev/neo-go/pkg/core/native/nativenames"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/util"
)
@ -42,7 +41,7 @@ func (c *Client) invokeNativePolicyMethod(operation string) (int64, error) {
}
func (c *Client) invokeNativeGetMethod(hash util.Uint160, operation string) (int64, error) {
result, err := c.InvokeFunction(hash, operation, []smartcontract.Parameter{}, nil)
result, err := c.reader.Call(hash, operation)
if err != nil {
return 0, err
}
@ -59,10 +58,7 @@ func (c *Client) IsBlocked(hash util.Uint160) (bool, error) {
if err != nil {
return false, fmt.Errorf("failed to get native Policy hash: %w", err)
}
result, err := c.InvokeFunction(policyHash, "isBlocked", []smartcontract.Parameter{{
Type: smartcontract.Hash160Type,
Value: hash,
}}, nil)
result, err := c.reader.Call(policyHash, "isBlocked", hash)
if err != nil {
return false, err
}