From b52282c3c77a01654133a4841a7119e58d878c81 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Mon, 1 Aug 2022 13:37:07 +0300 Subject: [PATCH] rpcclient: use Invoker internally for external APIs It's not a big improvement, but it allows to test Invoker better. --- pkg/rpcclient/client.go | 7 +++++++ pkg/rpcclient/native.go | 37 ++++--------------------------------- pkg/rpcclient/nep.go | 19 ++++++------------- pkg/rpcclient/nep11.go | 28 +++++----------------------- pkg/rpcclient/policy.go | 8 ++------ 5 files changed, 24 insertions(+), 75 deletions(-) diff --git a/pkg/rpcclient/client.go b/pkg/rpcclient/client.go index 1b70593fd..6e1fc7dc1 100644 --- a/pkg/rpcclient/client.go +++ b/pkg/rpcclient/client.go @@ -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 } diff --git a/pkg/rpcclient/native.go b/pkg/rpcclient/native.go index 4b7d49ccb..2bbb49e7e 100644 --- a/pkg/rpcclient/native.go +++ b/pkg/rpcclient/native.go @@ -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 } diff --git a/pkg/rpcclient/nep.go b/pkg/rpcclient/nep.go index e761e0352..7e0a5abbf 100644 --- a/pkg/rpcclient/nep.go +++ b/pkg/rpcclient/nep.go @@ -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 } diff --git a/pkg/rpcclient/nep11.go b/pkg/rpcclient/nep11.go index 0877a54ef..f5e908b2b 100644 --- a/pkg/rpcclient/nep11.go +++ b/pkg/rpcclient/nep11.go @@ -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 } diff --git a/pkg/rpcclient/policy.go b/pkg/rpcclient/policy.go index 481848526..b2b6372eb 100644 --- a/pkg/rpcclient/policy.go +++ b/pkg/rpcclient/policy.go @@ -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 }