From 6af52c46d8e67b7d051a85888dc186c49f3dc73c Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Thu, 3 Nov 2022 15:44:59 +0300 Subject: [PATCH] [#2091] morph/client: Do not use deprecated methods Signed-off-by: Evgenii Stratonikov --- .../processors/governance/processor.go | 5 +-- pkg/morph/client/client.go | 25 +++++++------ pkg/morph/client/constructor.go | 37 +++++-------------- pkg/morph/client/multi.go | 10 ++--- pkg/morph/client/notary.go | 21 +++-------- 5 files changed, 33 insertions(+), 65 deletions(-) diff --git a/pkg/innerring/processors/governance/processor.go b/pkg/innerring/processors/governance/processor.go index e18a389f0..845994ebc 100644 --- a/pkg/innerring/processors/governance/processor.go +++ b/pkg/innerring/processors/governance/processor.go @@ -116,10 +116,7 @@ func New(p *Params) (*Processor, error) { } // result is cached by neo-go, so we can pre-calc it - designate, err := p.MainnetClient.GetDesignateHash() - if err != nil { - return nil, fmt.Errorf("could not get designate hash: %w", err) - } + designate := p.MainnetClient.GetDesignateHash() return &Processor{ log: p.Log, diff --git a/pkg/morph/client/client.go b/pkg/morph/client/client.go index 272cb2549..804e80d4d 100644 --- a/pkg/morph/client/client.go +++ b/pkg/morph/client/client.go @@ -9,14 +9,15 @@ import ( "time" lru "github.com/hashicorp/golang-lru" - "github.com/nspcc-dev/neo-go/pkg/core/native/nativenames" "github.com/nspcc-dev/neo-go/pkg/core/native/noderoles" "github.com/nspcc-dev/neo-go/pkg/core/transaction" "github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/encoding/fixedn" "github.com/nspcc-dev/neo-go/pkg/rpcclient" "github.com/nspcc-dev/neo-go/pkg/rpcclient/actor" + "github.com/nspcc-dev/neo-go/pkg/rpcclient/gas" "github.com/nspcc-dev/neo-go/pkg/rpcclient/nep17" + "github.com/nspcc-dev/neo-go/pkg/rpcclient/rolemgmt" sc "github.com/nspcc-dev/neo-go/pkg/smartcontract" "github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger" "github.com/nspcc-dev/neo-go/pkg/util" @@ -50,13 +51,14 @@ type Client struct { client *rpcclient.WSClient // neo-go websocket client rpcActor *actor.Actor // neo-go RPC actor gasToken *nep17.Token // neo-go GAS token wrapper + rolemgmt *rolemgmt.Contract // neo-go Designation contract wrapper acc *wallet.Account // neo account accAddr util.Uint160 // account's address signer *transaction.Signer - notary *notary + notary *notaryInfo cfg cfg @@ -366,15 +368,8 @@ func (c *Client) NeoFSAlphabetList() (res keys.PublicKeys, err error) { } // GetDesignateHash returns hash of the native `RoleManagement` contract. -func (c *Client) GetDesignateHash() (res util.Uint160, err error) { - c.switchLock.RLock() - defer c.switchLock.RUnlock() - - if c.inactive { - return util.Uint160{}, ErrConnectionLost - } - - return c.client.GetNativeContractHash(nativenames.Designation) +func (c *Client) GetDesignateHash() util.Uint160 { + return rolemgmt.Hash } func (c *Client) roleList(r noderoles.Role) (keys.PublicKeys, error) { @@ -383,7 +378,7 @@ func (c *Client) roleList(r noderoles.Role) (keys.PublicKeys, error) { return nil, fmt.Errorf("can't get chain height: %w", err) } - return c.client.GetDesignatedByRole(r, height) + return c.rolemgmt.GetDesignatedByRole(r, height) } // tries to resolve sc.Parameter from the arg. @@ -524,3 +519,9 @@ func (c *Client) inactiveMode() { c.cfg.inactiveModeCb() } } + +func (c *Client) setActor(act *actor.Actor) { + c.rpcActor = act + c.gasToken = nep17.New(act, gas.Hash) + c.rolemgmt = rolemgmt.New(act) +} diff --git a/pkg/morph/client/constructor.go b/pkg/morph/client/constructor.go index 378e28735..958af3320 100644 --- a/pkg/morph/client/constructor.go +++ b/pkg/morph/client/constructor.go @@ -8,12 +8,10 @@ import ( "time" lru "github.com/hashicorp/golang-lru" - "github.com/nspcc-dev/neo-go/pkg/core/native/nativenames" "github.com/nspcc-dev/neo-go/pkg/core/transaction" "github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/rpcclient" "github.com/nspcc-dev/neo-go/pkg/rpcclient/actor" - "github.com/nspcc-dev/neo-go/pkg/rpcclient/nep17" "github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/wallet" "github.com/nspcc-dev/neofs-node/pkg/util/logger" @@ -119,6 +117,7 @@ func New(key *keys.PrivateKey, opts ...Option) (*Client, error) { cli.endpoints.init(cfg.endpoints) var err error + var act *actor.Actor if cfg.singleCli != nil { // return client in single RPC node mode that uses // predefined WS client @@ -129,51 +128,42 @@ func New(key *keys.PrivateKey, opts ...Option) (*Client, error) { // inactive mode will be enabled cli.client = cfg.singleCli - cli.rpcActor, err = newActor(cfg.singleCli, acc, *cfg) + act, err = newActor(cfg.singleCli, acc, *cfg) if err != nil { return nil, fmt.Errorf("could not create RPC actor: %w", err) } - - cli.gasToken, err = newGasToken(cli.client, cli.rpcActor) - if err != nil { - return nil, fmt.Errorf("could not create gas token actor: %w", err) - } } else { - cli.client, cli.rpcActor, cli.gasToken, err = cli.newCli(cli.endpoints.list[0].Address) + cli.client, act, err = cli.newCli(cli.endpoints.list[0].Address) if err != nil { return nil, fmt.Errorf("could not create RPC client: %w", err) } } + cli.setActor(act) go cli.notificationLoop() return cli, nil } -func (c *Client) newCli(endpoint string) (*rpcclient.WSClient, *actor.Actor, *nep17.Token, error) { +func (c *Client) newCli(endpoint string) (*rpcclient.WSClient, *actor.Actor, error) { cli, err := rpcclient.NewWS(c.cfg.ctx, endpoint, rpcclient.Options{ DialTimeout: c.cfg.dialTimeout, }) if err != nil { - return nil, nil, nil, fmt.Errorf("WS client creation: %w", err) + return nil, nil, fmt.Errorf("WS client creation: %w", err) } err = cli.Init() if err != nil { - return nil, nil, nil, fmt.Errorf("WS client initialization: %w", err) + return nil, nil, fmt.Errorf("WS client initialization: %w", err) } act, err := newActor(cli, c.acc, c.cfg) if err != nil { - return nil, nil, nil, fmt.Errorf("RPC actor creation: %w", err) + return nil, nil, fmt.Errorf("RPC actor creation: %w", err) } - gas, err := newGasToken(cli, act) - if err != nil { - return nil, nil, nil, fmt.Errorf("gas token actor: %w", err) - } - - return cli, act, gas, nil + return cli, act, nil } func newActor(ws *rpcclient.WSClient, acc *wallet.Account, cfg cfg) (*actor.Actor, error) { @@ -188,15 +178,6 @@ func newActor(ws *rpcclient.WSClient, acc *wallet.Account, cfg cfg) (*actor.Acto }}) } -func newGasToken(cli *rpcclient.WSClient, a *actor.Actor) (*nep17.Token, error) { - gasHash, err := cli.GetNativeContractHash(nativenames.Gas) - if err != nil { - return nil, fmt.Errorf("gas contract hash: %w", err) - } - - return nep17.New(a, gasHash), nil -} - func newClientCache() cache { c, _ := lru.New(100) // returns error only if size is negative return cache{ diff --git a/pkg/morph/client/multi.go b/pkg/morph/client/multi.go index d72c0794c..cdd532676 100644 --- a/pkg/morph/client/multi.go +++ b/pkg/morph/client/multi.go @@ -36,7 +36,7 @@ func (c *Client) switchRPC() bool { // Iterate endpoints in the order of decreasing priority. for c.endpoints.curr = range c.endpoints.list { newEndpoint := c.endpoints.list[c.endpoints.curr].Address - cli, act, gas, err := c.newCli(newEndpoint) + cli, act, err := c.newCli(newEndpoint) if err != nil { c.logger.Warn("could not establish connection to the switched RPC node", zap.String("endpoint", newEndpoint), @@ -62,8 +62,7 @@ func (c *Client) switchRPC() bool { } c.client = cli - c.rpcActor = act - c.gasToken = gas + c.setActor(act) if c.cfg.switchInterval != 0 && !c.switchIsActive.Load() && c.endpoints.list[c.endpoints.curr].Priority != c.endpoints.list[0].Priority { @@ -165,7 +164,7 @@ mainLoop: tryE := e.Address - cli, act, gas, err := c.newCli(tryE) + cli, act, err := c.newCli(tryE) if err != nil { c.logger.Warn("could not create client to the higher priority node", zap.String("endpoint", tryE), @@ -188,8 +187,7 @@ mainLoop: c.client.Close() c.cache.invalidate() c.client = cli - c.rpcActor = act - c.gasToken = gas + c.setActor(act) c.endpoints.curr = i c.switchLock.Unlock() diff --git a/pkg/morph/client/notary.go b/pkg/morph/client/notary.go index 2c0cbed52..2cdf04e8c 100644 --- a/pkg/morph/client/notary.go +++ b/pkg/morph/client/notary.go @@ -14,6 +14,7 @@ import ( "github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/encoding/fixedn" "github.com/nspcc-dev/neo-go/pkg/neorpc" + "github.com/nspcc-dev/neo-go/pkg/rpcclient/notary" "github.com/nspcc-dev/neo-go/pkg/smartcontract" sc "github.com/nspcc-dev/neo-go/pkg/smartcontract" "github.com/nspcc-dev/neo-go/pkg/util" @@ -24,7 +25,7 @@ import ( ) type ( - notary struct { + notaryInfo struct { txValidTime uint32 // minimum amount of blocks when mainTx will be valid roundTime uint32 // extra amount of blocks to synchronize sidechain height diff of inner ring nodes fallbackTime uint32 // mainTx's ValidUntilBlock - fallbackTime + 1 is when fallbackTx is sent @@ -97,19 +98,13 @@ func (c *Client) EnableNotarySupport(opts ...NotaryOption) error { } } - notaryCfg := ¬ary{ + notaryCfg := ¬aryInfo{ proxy: cfg.proxy, txValidTime: cfg.txValidTime, roundTime: cfg.roundTime, fallbackTime: cfg.fallbackTime, alphabetSource: cfg.alphabetSource, - } - - var err error - - notaryCfg.notary, err = c.client.GetNativeContractHash(nativenames.Notary) - if err != nil { - return fmt.Errorf("can't get notary contract script hash: %w", err) + notary: notary.Hash, } c.notary = notaryCfg @@ -132,7 +127,7 @@ func (c *Client) ProbeNotary() (res bool) { return false } - _, err := c.client.GetNativeContractHash(nativenames.Notary) + _, err := c.client.GetContractStateByAddressOrName(nativenames.Notary) return err == nil } @@ -421,11 +416,7 @@ func (c *Client) NotarySignAndInvokeTX(mainTx *transaction.Transaction) error { } func (c *Client) notaryInvokeAsCommittee(method string, nonce, vub uint32, args ...interface{}) error { - designate, err := c.GetDesignateHash() - if err != nil { - return err - } - + designate := c.GetDesignateHash() return c.notaryInvoke(true, true, designate, nonce, &vub, method, args...) }