forked from TrueCloudLab/neoneo-go
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:
parent
fee7e2f223
commit
b52282c3c7
5 changed files with 24 additions and 75 deletions
|
@ -14,6 +14,7 @@ import (
|
||||||
|
|
||||||
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
|
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/neorpc"
|
"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"
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
||||||
"go.uber.org/atomic"
|
"go.uber.org/atomic"
|
||||||
)
|
)
|
||||||
|
@ -35,6 +36,11 @@ type Client struct {
|
||||||
opts Options
|
opts Options
|
||||||
requestF func(*neorpc.Request) (*neorpc.Response, error)
|
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
|
cacheLock sync.RWMutex
|
||||||
// cache stores RPC node related information the client is bound to.
|
// cache stores RPC node related information the client is bound to.
|
||||||
// cache is mostly filled in during Init(), but can also be updated
|
// 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.getNextRequestID = (cl).getRequestID
|
||||||
cl.opts = opts
|
cl.opts = opts
|
||||||
cl.requestF = cl.makeHTTPRequest
|
cl.requestF = cl.makeHTTPRequest
|
||||||
|
cl.reader = invoker.New(cl, nil)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,6 @@ package rpcclient
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/big"
|
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/core/native/nativenames"
|
"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 {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to get native RoleManagement hash: %w", err)
|
return nil, fmt.Errorf("failed to get native RoleManagement hash: %w", err)
|
||||||
}
|
}
|
||||||
result, err := c.InvokeFunction(rmHash, "getDesignatedByRole", []smartcontract.Parameter{
|
result, err := c.reader.Call(rmHash, "getDesignatedByRole", int64(role), index)
|
||||||
{
|
|
||||||
Type: smartcontract.IntegerType,
|
|
||||||
Value: big.NewInt(int64(role)),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Type: smartcontract.IntegerType,
|
|
||||||
Value: big.NewInt(int64(index)),
|
|
||||||
},
|
|
||||||
}, nil)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -80,16 +70,7 @@ func (c *Client) NNSResolve(nnsHash util.Uint160, name string, typ nns.RecordTyp
|
||||||
if typ == nns.CNAME {
|
if typ == nns.CNAME {
|
||||||
return "", errors.New("can't resolve CNAME record type")
|
return "", errors.New("can't resolve CNAME record type")
|
||||||
}
|
}
|
||||||
result, err := c.InvokeFunction(nnsHash, "resolve", []smartcontract.Parameter{
|
result, err := c.reader.Call(nnsHash, "resolve", name, int64(typ))
|
||||||
{
|
|
||||||
Type: smartcontract.StringType,
|
|
||||||
Value: name,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Type: smartcontract.IntegerType,
|
|
||||||
Value: big.NewInt(int64(typ)),
|
|
||||||
},
|
|
||||||
}, nil)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
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.
|
// NNSIsAvailable invokes `isAvailable` method on a NeoNameService contract with the specified hash.
|
||||||
func (c *Client) NNSIsAvailable(nnsHash util.Uint160, name string) (bool, error) {
|
func (c *Client) NNSIsAvailable(nnsHash util.Uint160, name string) (bool, error) {
|
||||||
result, err := c.InvokeFunction(nnsHash, "isAvailable", []smartcontract.Parameter{
|
result, err := c.reader.Call(nnsHash, "isAvailable", name)
|
||||||
{
|
|
||||||
Type: smartcontract.StringType,
|
|
||||||
Value: name,
|
|
||||||
},
|
|
||||||
}, nil)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
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 to terminate opened iterator session. See TraverseIterator and
|
||||||
// TerminateSession documentation for more details.
|
// TerminateSession documentation for more details.
|
||||||
func (c *Client) NNSGetAllRecords(nnsHash util.Uint160, name string) (uuid.UUID, result.Iterator, error) {
|
func (c *Client) NNSGetAllRecords(nnsHash util.Uint160, name string) (uuid.UUID, result.Iterator, error) {
|
||||||
res, err := c.InvokeFunction(nnsHash, "getAllRecords", []smartcontract.Parameter{
|
res, err := c.reader.Call(nnsHash, "getAllRecords", name)
|
||||||
{
|
|
||||||
Type: smartcontract.StringType,
|
|
||||||
Value: name,
|
|
||||||
},
|
|
||||||
}, nil)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return uuid.UUID{}, result.Iterator{}, err
|
return uuid.UUID{}, result.Iterator{}, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,14 +3,13 @@ package rpcclient
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
|
||||||
"github.com/nspcc-dev/neo-go/pkg/util"
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
||||||
)
|
)
|
||||||
|
|
||||||
// nepDecimals invokes `decimals` NEP* method on the specified contract.
|
// nepDecimals invokes `decimals` NEP* method on the specified contract.
|
||||||
func (c *Client) nepDecimals(tokenHash util.Uint160) (int64, error) {
|
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 {
|
if err != nil {
|
||||||
return 0, err
|
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.
|
// nepSymbol invokes `symbol` NEP* method on the specified contract.
|
||||||
func (c *Client) nepSymbol(tokenHash util.Uint160) (string, error) {
|
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 {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
@ -38,7 +37,7 @@ func (c *Client) nepSymbol(tokenHash util.Uint160) (string, error) {
|
||||||
|
|
||||||
// nepTotalSupply invokes `totalSupply` NEP* method on the specified contract.
|
// nepTotalSupply invokes `totalSupply` NEP* method on the specified contract.
|
||||||
func (c *Client) nepTotalSupply(tokenHash util.Uint160) (int64, error) {
|
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 {
|
if err != nil {
|
||||||
return 0, err
|
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.
|
// nepBalanceOf invokes `balanceOf` NEP* method on the specified contract.
|
||||||
func (c *Client) nepBalanceOf(tokenHash, acc util.Uint160, tokenID []byte) (int64, error) {
|
func (c *Client) nepBalanceOf(tokenHash, acc util.Uint160, tokenID []byte) (int64, error) {
|
||||||
params := []smartcontract.Parameter{{
|
params := []interface{}{acc}
|
||||||
Type: smartcontract.Hash160Type,
|
|
||||||
Value: acc,
|
|
||||||
}}
|
|
||||||
if tokenID != nil {
|
if tokenID != nil {
|
||||||
params = append(params, smartcontract.Parameter{
|
params = append(params, tokenID)
|
||||||
Type: smartcontract.ByteArrayType,
|
|
||||||
Value: tokenID,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
result, err := c.InvokeFunction(tokenHash, "balanceOf", params, nil)
|
result, err := c.reader.Call(tokenHash, "balanceOf", params...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -84,12 +84,7 @@ func (c *Client) CreateNEP11TransferTx(acc *wallet.Account, tokenHash util.Uint1
|
||||||
// traverse iterator values or TerminateSession to terminate opened iterator
|
// traverse iterator values or TerminateSession to terminate opened iterator
|
||||||
// session. See TraverseIterator and TerminateSession documentation for more details.
|
// session. See TraverseIterator and TerminateSession documentation for more details.
|
||||||
func (c *Client) NEP11TokensOf(tokenHash util.Uint160, owner util.Uint160) (uuid.UUID, result.Iterator, error) {
|
func (c *Client) NEP11TokensOf(tokenHash util.Uint160, owner util.Uint160) (uuid.UUID, result.Iterator, error) {
|
||||||
res, err := c.InvokeFunction(tokenHash, "tokensOf", []smartcontract.Parameter{
|
res, err := c.reader.Call(tokenHash, "tokensOf", owner)
|
||||||
{
|
|
||||||
Type: smartcontract.Hash160Type,
|
|
||||||
Value: owner,
|
|
||||||
},
|
|
||||||
}, nil)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return uuid.UUID{}, result.Iterator{}, err
|
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
|
// NEP11NDOwnerOf invokes `ownerOf` non-divisible NEP-11 method with the
|
||||||
// specified token ID on the specified contract.
|
// specified token ID on the specified contract.
|
||||||
func (c *Client) NEP11NDOwnerOf(tokenHash util.Uint160, tokenID []byte) (util.Uint160, error) {
|
func (c *Client) NEP11NDOwnerOf(tokenHash util.Uint160, tokenID []byte) (util.Uint160, error) {
|
||||||
result, err := c.InvokeFunction(tokenHash, "ownerOf", []smartcontract.Parameter{
|
result, err := c.reader.Call(tokenHash, "ownerOf", tokenID)
|
||||||
{
|
|
||||||
Type: smartcontract.ByteArrayType,
|
|
||||||
Value: tokenID,
|
|
||||||
},
|
|
||||||
}, nil)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return util.Uint160{}, err
|
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
|
// method to traverse iterator values or TerminateSession to terminate opened iterator session. See
|
||||||
// TraverseIterator and TerminateSession documentation for more details.
|
// TraverseIterator and TerminateSession documentation for more details.
|
||||||
func (c *Client) NEP11DOwnerOf(tokenHash util.Uint160, tokenID []byte) (uuid.UUID, result.Iterator, error) {
|
func (c *Client) NEP11DOwnerOf(tokenHash util.Uint160, tokenID []byte) (uuid.UUID, result.Iterator, error) {
|
||||||
res, err := c.InvokeFunction(tokenHash, "ownerOf", []smartcontract.Parameter{
|
res, err := c.reader.Call(tokenHash, "ownerOf", tokenID)
|
||||||
{
|
|
||||||
Type: smartcontract.ByteArrayType,
|
|
||||||
Value: tokenID,
|
|
||||||
},
|
|
||||||
}, nil)
|
|
||||||
sessID := res.Session
|
sessID := res.Session
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return sessID, result.Iterator{}, err
|
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
|
// NEP11Properties invokes `properties` optional NEP-11 method on the
|
||||||
// specified contract.
|
// specified contract.
|
||||||
func (c *Client) NEP11Properties(tokenHash util.Uint160, tokenID []byte) (*stackitem.Map, error) {
|
func (c *Client) NEP11Properties(tokenHash util.Uint160, tokenID []byte) (*stackitem.Map, error) {
|
||||||
result, err := c.InvokeFunction(tokenHash, "properties", []smartcontract.Parameter{{
|
result, err := c.reader.Call(tokenHash, "properties", tokenID)
|
||||||
Type: smartcontract.ByteArrayType,
|
|
||||||
Value: tokenID,
|
|
||||||
}}, nil)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
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 to terminate opened iterator session. See TraverseIterator and
|
||||||
// TerminateSession documentation for more details.
|
// TerminateSession documentation for more details.
|
||||||
func (c *Client) NEP11Tokens(tokenHash util.Uint160) (uuid.UUID, result.Iterator, error) {
|
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 {
|
if err != nil {
|
||||||
return uuid.UUID{}, result.Iterator{}, err
|
return uuid.UUID{}, result.Iterator{}, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/nspcc-dev/neo-go/pkg/core/native/nativenames"
|
"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"
|
"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) {
|
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 {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
@ -59,10 +58,7 @@ func (c *Client) IsBlocked(hash util.Uint160) (bool, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, fmt.Errorf("failed to get native Policy hash: %w", err)
|
return false, fmt.Errorf("failed to get native Policy hash: %w", err)
|
||||||
}
|
}
|
||||||
result, err := c.InvokeFunction(policyHash, "isBlocked", []smartcontract.Parameter{{
|
result, err := c.reader.Call(policyHash, "isBlocked", hash)
|
||||||
Type: smartcontract.Hash160Type,
|
|
||||||
Value: hash,
|
|
||||||
}}, nil)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue