rpc: move all RPC client's cached fields to a separate cache structure

No functional changes.
This commit is contained in:
AnnaShaleva 2022-02-21 13:13:39 +03:00
parent 7d6f087337
commit 7c1862a9ac
7 changed files with 38 additions and 34 deletions

View file

@ -28,15 +28,16 @@ const (
// Client represents the middleman for executing JSON RPC calls // Client represents the middleman for executing JSON RPC calls
// to remote NEO RPC nodes. // to remote NEO RPC nodes.
type Client struct { type Client struct {
cli *http.Client cli *http.Client
endpoint *url.URL endpoint *url.URL
network netmode.Magic ctx context.Context
stateRootInHeader bool opts Options
initDone bool requestF func(*request.Raw) (*response.Raw, error)
ctx context.Context
opts Options // cache stores RPC node related information client is bound to.
requestF func(*request.Raw) (*response.Raw, error) // cache is mostly filled in during Init(), but can also be updated
cache cache // during regular Client lifecycle.
cache cache
} }
// Options defines options for the RPC client. // Options defines options for the RPC client.
@ -56,6 +57,9 @@ type Options struct {
// cache stores cache values for the RPC client methods. // cache stores cache values for the RPC client methods.
type cache struct { type cache struct {
initDone bool
network netmode.Magic
stateRootInHeader bool
calculateValidUntilBlock calculateValidUntilBlockCache calculateValidUntilBlock calculateValidUntilBlockCache
nativeHashes map[string]util.Uint160 nativeHashes map[string]util.Uint160
} }
@ -119,11 +123,11 @@ func (c *Client) Init() error {
if err != nil { if err != nil {
return fmt.Errorf("failed to get network magic: %w", err) return fmt.Errorf("failed to get network magic: %w", err)
} }
c.network = version.Protocol.Network c.cache.network = version.Protocol.Network
c.stateRootInHeader = version.Protocol.StateRootInHeader c.cache.stateRootInHeader = version.Protocol.StateRootInHeader
if version.Protocol.MillisecondsPerBlock == 0 { if version.Protocol.MillisecondsPerBlock == 0 {
c.network = version.Magic c.cache.network = version.Magic
c.stateRootInHeader = version.StateRootInHeader c.cache.stateRootInHeader = version.StateRootInHeader
} }
neoContractHash, err := c.GetContractStateByAddressOrName(nativenames.Neo) neoContractHash, err := c.GetContractStateByAddressOrName(nativenames.Neo)
if err != nil { if err != nil {
@ -140,7 +144,7 @@ func (c *Client) Init() error {
return fmt.Errorf("failed to get Policy contract scripthash: %w", err) return fmt.Errorf("failed to get Policy contract scripthash: %w", err)
} }
c.cache.nativeHashes[nativenames.Policy] = policyContractHash.Hash c.cache.nativeHashes[nativenames.Policy] = policyContractHash.Hash
c.initDone = true c.cache.initDone = true
return nil return nil
} }

View file

@ -46,7 +46,7 @@ func (c *Client) NEP11TokenInfo(tokenHash util.Uint160) (*wallet.Token, error) {
// given account and sends it to the network returning just a hash of it. // given account and sends it to the network returning just a hash of it.
func (c *Client) TransferNEP11(acc *wallet.Account, to util.Uint160, func (c *Client) TransferNEP11(acc *wallet.Account, to util.Uint160,
tokenHash util.Uint160, tokenID string, data interface{}, gas int64, cosigners []SignerAccount) (util.Uint256, error) { tokenHash util.Uint160, tokenID string, data interface{}, gas int64, cosigners []SignerAccount) (util.Uint256, error) {
if !c.initDone { if !c.cache.initDone {
return util.Uint256{}, errNetworkNotInitialized return util.Uint256{}, errNetworkNotInitialized
} }
tx, err := c.CreateNEP11TransferTx(acc, tokenHash, gas, cosigners, to, tokenID, data) tx, err := c.CreateNEP11TransferTx(acc, tokenHash, gas, cosigners, to, tokenID, data)
@ -144,7 +144,7 @@ func (c *Client) NEP11NDOwnerOf(tokenHash util.Uint160, tokenID []byte) (util.Ui
// sends it to the network returning just a hash of it. // sends it to the network returning just a hash of it.
func (c *Client) TransferNEP11D(acc *wallet.Account, to util.Uint160, func (c *Client) TransferNEP11D(acc *wallet.Account, to util.Uint160,
tokenHash util.Uint160, amount int64, tokenID []byte, data interface{}, gas int64, cosigners []SignerAccount) (util.Uint256, error) { tokenHash util.Uint160, amount int64, tokenID []byte, data interface{}, gas int64, cosigners []SignerAccount) (util.Uint256, error) {
if !c.initDone { if !c.cache.initDone {
return util.Uint256{}, errNetworkNotInitialized return util.Uint256{}, errNetworkNotInitialized
} }
from, err := address.StringToUint160(acc.Address) from, err := address.StringToUint160(acc.Address)

View file

@ -142,7 +142,7 @@ func (c *Client) CreateTxFromScript(script []byte, acc *wallet.Account, sysFee,
// impossible (e.g. due to locked cosigner's account) an error is returned. // impossible (e.g. due to locked cosigner's account) an error is returned.
func (c *Client) TransferNEP17(acc *wallet.Account, to util.Uint160, token util.Uint160, func (c *Client) TransferNEP17(acc *wallet.Account, to util.Uint160, token util.Uint160,
amount int64, gas int64, data interface{}, cosigners []SignerAccount) (util.Uint256, error) { amount int64, gas int64, data interface{}, cosigners []SignerAccount) (util.Uint256, error) {
if !c.initDone { if !c.cache.initDone {
return util.Uint256{}, errNetworkNotInitialized return util.Uint256{}, errNetworkNotInitialized
} }
@ -156,7 +156,7 @@ func (c *Client) TransferNEP17(acc *wallet.Account, to util.Uint160, token util.
// MultiTransferNEP17 is similar to TransferNEP17, buf allows to have multiple recipients. // MultiTransferNEP17 is similar to TransferNEP17, buf allows to have multiple recipients.
func (c *Client) MultiTransferNEP17(acc *wallet.Account, gas int64, recipients []TransferTarget, cosigners []SignerAccount) (util.Uint256, error) { func (c *Client) MultiTransferNEP17(acc *wallet.Account, gas int64, recipients []TransferTarget, cosigners []SignerAccount) (util.Uint256, error) {
if !c.initDone { if !c.cache.initDone {
return util.Uint256{}, errNetworkNotInitialized return util.Uint256{}, errNetworkNotInitialized
} }

View file

@ -10,7 +10,7 @@ import (
// GetFeePerByte invokes `getFeePerByte` method on a native Policy contract. // GetFeePerByte invokes `getFeePerByte` method on a native Policy contract.
func (c *Client) GetFeePerByte() (int64, error) { func (c *Client) GetFeePerByte() (int64, error) {
if !c.initDone { if !c.cache.initDone {
return 0, errNetworkNotInitialized return 0, errNetworkNotInitialized
} }
return c.invokeNativePolicyMethod("getFeePerByte") return c.invokeNativePolicyMethod("getFeePerByte")
@ -18,7 +18,7 @@ func (c *Client) GetFeePerByte() (int64, error) {
// GetExecFeeFactor invokes `getExecFeeFactor` method on a native Policy contract. // GetExecFeeFactor invokes `getExecFeeFactor` method on a native Policy contract.
func (c *Client) GetExecFeeFactor() (int64, error) { func (c *Client) GetExecFeeFactor() (int64, error) {
if !c.initDone { if !c.cache.initDone {
return 0, errNetworkNotInitialized return 0, errNetworkNotInitialized
} }
return c.invokeNativePolicyMethod("getExecFeeFactor") return c.invokeNativePolicyMethod("getExecFeeFactor")
@ -26,7 +26,7 @@ func (c *Client) GetExecFeeFactor() (int64, error) {
// GetStoragePrice invokes `getStoragePrice` method on a native Policy contract. // GetStoragePrice invokes `getStoragePrice` method on a native Policy contract.
func (c *Client) GetStoragePrice() (int64, error) { func (c *Client) GetStoragePrice() (int64, error) {
if !c.initDone { if !c.cache.initDone {
return 0, errNetworkNotInitialized return 0, errNetworkNotInitialized
} }
return c.invokeNativePolicyMethod("getStoragePrice") return c.invokeNativePolicyMethod("getStoragePrice")
@ -43,7 +43,7 @@ func (c *Client) GetMaxNotValidBeforeDelta() (int64, error) {
// invokeNativePolicy method invokes Get* method on a native Policy contract. // invokeNativePolicy method invokes Get* method on a native Policy contract.
func (c *Client) invokeNativePolicyMethod(operation string) (int64, error) { func (c *Client) invokeNativePolicyMethod(operation string) (int64, error) {
if !c.initDone { if !c.cache.initDone {
return 0, errNetworkNotInitialized return 0, errNetworkNotInitialized
} }
return c.invokeNativeGetMethod(c.cache.nativeHashes[nativenames.Policy], operation) return c.invokeNativeGetMethod(c.cache.nativeHashes[nativenames.Policy], operation)
@ -63,7 +63,7 @@ func (c *Client) invokeNativeGetMethod(hash util.Uint160, operation string) (int
// IsBlocked invokes `isBlocked` method on native Policy contract. // IsBlocked invokes `isBlocked` method on native Policy contract.
func (c *Client) IsBlocked(hash util.Uint160) (bool, error) { func (c *Client) IsBlocked(hash util.Uint160) (bool, error) {
if !c.initDone { if !c.cache.initDone {
return false, errNetworkNotInitialized return false, errNetworkNotInitialized
} }
result, err := c.InvokeFunction(c.cache.nativeHashes[nativenames.Policy], "isBlocked", []smartcontract.Parameter{{ result, err := c.InvokeFunction(c.cache.nativeHashes[nativenames.Policy], "isBlocked", []smartcontract.Parameter{{

View file

@ -94,7 +94,7 @@ func (c *Client) getBlock(params request.RawParams) (*block.Block, error) {
err error err error
b *block.Block b *block.Block
) )
if !c.initDone { if !c.cache.initDone {
return nil, errNetworkNotInitialized return nil, errNetworkNotInitialized
} }
if err = c.performRequest("getblock", params, &resp); err != nil { if err = c.performRequest("getblock", params, &resp); err != nil {
@ -127,7 +127,7 @@ func (c *Client) getBlockVerbose(params request.RawParams) (*result.Block, error
resp = &result.Block{} resp = &result.Block{}
err error err error
) )
if !c.initDone { if !c.cache.initDone {
return nil, errNetworkNotInitialized return nil, errNetworkNotInitialized
} }
if err = c.performRequest("getblock", params, resp); err != nil { if err = c.performRequest("getblock", params, resp); err != nil {
@ -157,7 +157,7 @@ func (c *Client) GetBlockHeader(hash util.Uint256) (*block.Header, error) {
resp []byte resp []byte
h *block.Header h *block.Header
) )
if !c.initDone { if !c.cache.initDone {
return nil, errNetworkNotInitialized return nil, errNetworkNotInitialized
} }
if err := c.performRequest("getblockheader", params, &resp); err != nil { if err := c.performRequest("getblockheader", params, &resp); err != nil {
@ -407,7 +407,7 @@ func (c *Client) GetRawTransaction(hash util.Uint256) (*transaction.Transaction,
resp []byte resp []byte
err error err error
) )
if !c.initDone { if !c.cache.initDone {
return nil, errNetworkNotInitialized return nil, errNetworkNotInitialized
} }
if err = c.performRequest("getrawtransaction", params, &resp); err != nil { if err = c.performRequest("getrawtransaction", params, &resp); err != nil {
@ -430,7 +430,7 @@ func (c *Client) GetRawTransactionVerbose(hash util.Uint256) (*result.Transactio
resp = &result.TransactionOutputRaw{} resp = &result.TransactionOutputRaw{}
err error err error
) )
if !c.initDone { if !c.cache.initDone {
return nil, errNetworkNotInitialized return nil, errNetworkNotInitialized
} }
if err = c.performRequest("getrawtransaction", params, resp); err != nil { if err = c.performRequest("getrawtransaction", params, resp); err != nil {
@ -771,7 +771,7 @@ func getSigners(sender *wallet.Account, cosigners []SignerAccount) ([]transactio
// Note: client should be initialized before SignAndPushP2PNotaryRequest call. // Note: client should be initialized before SignAndPushP2PNotaryRequest call.
func (c *Client) SignAndPushP2PNotaryRequest(mainTx *transaction.Transaction, fallbackScript []byte, fallbackSysFee int64, fallbackNetFee int64, fallbackValidFor uint32, acc *wallet.Account) (*payload.P2PNotaryRequest, error) { func (c *Client) SignAndPushP2PNotaryRequest(mainTx *transaction.Transaction, fallbackScript []byte, fallbackSysFee int64, fallbackNetFee int64, fallbackValidFor uint32, acc *wallet.Account) (*payload.P2PNotaryRequest, error) {
var err error var err error
if !c.initDone { if !c.cache.initDone {
return nil, errNetworkNotInitialized return nil, errNetworkNotInitialized
} }
notaryHash, err := c.GetNativeContractHash(nativenames.Notary) notaryHash, err := c.GetNativeContractHash(nativenames.Notary)
@ -991,12 +991,12 @@ func (c *Client) AddNetworkFee(tx *transaction.Transaction, extraFee int64, accs
// GetNetwork returns the network magic of the RPC node client connected to. // GetNetwork returns the network magic of the RPC node client connected to.
func (c *Client) GetNetwork() netmode.Magic { func (c *Client) GetNetwork() netmode.Magic {
return c.network return c.cache.network
} }
// StateRootInHeader returns true if state root is contained in block header. // StateRootInHeader returns true if state root is contained in block header.
func (c *Client) StateRootInHeader() bool { func (c *Client) StateRootInHeader() bool {
return c.stateRootInHeader return c.cache.stateRootInHeader
} }
// GetNativeContractHash returns native contract hash by its name. // GetNativeContractHash returns native contract hash by its name.

View file

@ -1914,7 +1914,7 @@ func TestGetNetwork(t *testing.T) {
} }
// network was not initialised // network was not initialised
require.Equal(t, netmode.Magic(0), c.GetNetwork()) require.Equal(t, netmode.Magic(0), c.GetNetwork())
require.Equal(t, false, c.initDone) require.Equal(t, false, c.cache.initDone)
}) })
t.Run("good", func(t *testing.T) { t.Run("good", func(t *testing.T) {

View file

@ -143,7 +143,7 @@ func TestWSClientEvents(t *testing.T) {
wsc, err := NewWS(context.TODO(), httpURLtoWS(srv.URL), Options{}) wsc, err := NewWS(context.TODO(), httpURLtoWS(srv.URL), Options{})
require.NoError(t, err) require.NoError(t, err)
wsc.network = netmode.UnitTestNet wsc.cache.network = netmode.UnitTestNet
for range events { for range events {
select { select {
case _, ok = <-wsc.Notifications: case _, ok = <-wsc.Notifications:
@ -315,7 +315,7 @@ func TestWSFilteredSubscriptions(t *testing.T) {
})) }))
wsc, err := NewWS(context.TODO(), httpURLtoWS(srv.URL), Options{}) wsc, err := NewWS(context.TODO(), httpURLtoWS(srv.URL), Options{})
require.NoError(t, err) require.NoError(t, err)
wsc.network = netmode.UnitTestNet wsc.cache.network = netmode.UnitTestNet
c.clientCode(t, wsc) c.clientCode(t, wsc)
wsc.Close() wsc.Close()
}) })