From c81008764a12d808c7fd5617f6dede44e535498d Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Thu, 29 Jul 2021 16:35:57 +0300 Subject: [PATCH] [#732] neofs-adm: fetch native hashes once Retrieve list of native contracts during initialization in a single query. Signed-off-by: Evgenii Stratonikov --- .../internal/modules/morph/generate.go | 5 +---- .../internal/modules/morph/initialize.go | 16 ++++++++++++++ .../modules/morph/initialize_deploy.go | 4 ++-- .../modules/morph/initialize_register.go | 10 ++------- .../modules/morph/initialize_roles.go | 7 +------ .../modules/morph/initialize_transfer.go | 21 ++++--------------- 6 files changed, 26 insertions(+), 37 deletions(-) diff --git a/cmd/neofs-adm/internal/modules/morph/generate.go b/cmd/neofs-adm/internal/modules/morph/generate.go index 3c0d28312..92fffdac5 100644 --- a/cmd/neofs-adm/internal/modules/morph/generate.go +++ b/cmd/neofs-adm/internal/modules/morph/generate.go @@ -159,10 +159,7 @@ func generateStorageCreds(cmd *cobra.Command, args []string) error { return err } - gasHash, err := wCtx.Client.GetNativeContractHash(nativenames.Gas) - if err != nil { - return err - } + gasHash := wCtx.nativeHash(nativenames.Gas) bw := io.NewBufBinWriter() emit.AppCall(bw.BinWriter, gasHash, "transfer", callflag.All, diff --git a/cmd/neofs-adm/internal/modules/morph/initialize.go b/cmd/neofs-adm/internal/modules/morph/initialize.go index 6e0f7553b..44d9e07a2 100644 --- a/cmd/neofs-adm/internal/modules/morph/initialize.go +++ b/cmd/neofs-adm/internal/modules/morph/initialize.go @@ -32,6 +32,7 @@ type initializeContext struct { Contracts map[string]*contractState Command *cobra.Command ContractPath string + Natives map[string]util.Uint160 } func initializeSideChainCmd(cmd *cobra.Command, args []string) error { @@ -129,6 +130,16 @@ func newInitializeContext(cmd *cobra.Command, v *viper.Viper) (*initializeContex return nil, fmt.Errorf("missing contracts path: %w", err) } + ns, err := c.GetNativeContracts() + if err != nil { + return nil, fmt.Errorf("can't get native contract hashes: %w", err) + } + + nativeHashes := make(map[string]util.Uint160, len(ns)) + for i := range ns { + nativeHashes[ns[i].Manifest.Name] = ns[i].Hash + } + initCtx := &initializeContext{ Client: c, ConsensusAcc: consensusAcc, @@ -139,11 +150,16 @@ func newInitializeContext(cmd *cobra.Command, v *viper.Viper) (*initializeContex Command: cmd, Contracts: make(map[string]*contractState), ContractPath: ctrPath, + Natives: nativeHashes, } return initCtx, nil } +func (c *initializeContext) nativeHash(name string) util.Uint160 { + return c.Natives[name] +} + func openAlphabetWallets(walletDir string) ([]*wallet.Wallet, error) { walletFiles, err := ioutil.ReadDir(walletDir) if err != nil { diff --git a/cmd/neofs-adm/internal/modules/morph/initialize_deploy.go b/cmd/neofs-adm/internal/modules/morph/initialize_deploy.go index 5fa3e9637..252cd1ef9 100644 --- a/cmd/neofs-adm/internal/modules/morph/initialize_deploy.go +++ b/cmd/neofs-adm/internal/modules/morph/initialize_deploy.go @@ -87,7 +87,7 @@ func (c *initializeContext) deployNNS() error { Scopes: transaction.CalledByEntry, } - mgmtHash, _ := c.Client.GetNativeContractHash(nativenames.Management) + mgmtHash := c.nativeHash(nativenames.Management) res, err := c.Client.InvokeFunction(mgmtHash, "deploy", params, []transaction.Signer{signer}) if err != nil { return fmt.Errorf("can't deploy contract: %w", err) @@ -109,7 +109,7 @@ func (c *initializeContext) deployNNS() error { } func (c *initializeContext) deployContracts() error { - mgmtHash, _ := c.Client.GetNativeContractHash(nativenames.Management) + mgmtHash := c.nativeHash(nativenames.Management) sender := c.CommitteeAcc.Contract.ScriptHash() for _, ctrName := range contractList { cs, err := c.readContract(ctrName) diff --git a/cmd/neofs-adm/internal/modules/morph/initialize_register.go b/cmd/neofs-adm/internal/modules/morph/initialize_register.go index bb68e625f..464339230 100644 --- a/cmd/neofs-adm/internal/modules/morph/initialize_register.go +++ b/cmd/neofs-adm/internal/modules/morph/initialize_register.go @@ -22,10 +22,7 @@ import ( const initialAlphabetNEOAmount = native.NEOTotalSupply func (c *initializeContext) registerCandidates() error { - neoHash, err := c.Client.GetNativeContractHash(nativenames.Neo) - if err != nil { - return err - } + neoHash := c.nativeHash(nativenames.Neo) res, err := c.Client.InvokeFunction(neoHash, "getCandidates", []smartcontract.Parameter{}, nil) if err != nil { @@ -72,10 +69,7 @@ func (c *initializeContext) registerCandidates() error { } func (c *initializeContext) transferNEOToAlphabetContracts() error { - neoHash, err := c.Client.GetNativeContractHash(nativenames.Neo) - if err != nil { - return err - } + neoHash := c.nativeHash(nativenames.Neo) ok, err := c.transferNEOFinished(neoHash) if ok || err != nil { diff --git a/cmd/neofs-adm/internal/modules/morph/initialize_roles.go b/cmd/neofs-adm/internal/modules/morph/initialize_roles.go index 232d95701..705e64374 100644 --- a/cmd/neofs-adm/internal/modules/morph/initialize_roles.go +++ b/cmd/neofs-adm/internal/modules/morph/initialize_roles.go @@ -1,8 +1,6 @@ package morph import ( - "fmt" - "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/io" @@ -18,10 +16,7 @@ func (c *initializeContext) setNotaryAndAlphabetNodes() error { return err } - designateHash, err := c.Client.GetNativeContractHash(nativenames.Designation) - if err != nil { - return fmt.Errorf("can't fetch %s hash: %w", nativenames.Designation, err) - } + designateHash := c.nativeHash(nativenames.Designation) var pubs []interface{} for _, w := range c.Wallets { diff --git a/cmd/neofs-adm/internal/modules/morph/initialize_transfer.go b/cmd/neofs-adm/internal/modules/morph/initialize_transfer.go index 4496ce333..b5e185417 100644 --- a/cmd/neofs-adm/internal/modules/morph/initialize_transfer.go +++ b/cmd/neofs-adm/internal/modules/morph/initialize_transfer.go @@ -28,14 +28,8 @@ func (c *initializeContext) transferFunds() error { return err } - gasHash, err := c.Client.GetNativeContractHash(nativenames.Gas) - if err != nil { - return fmt.Errorf("can't fetch %s hash: %w", nativenames.Gas, err) - } - neoHash, err := c.Client.GetNativeContractHash(nativenames.Neo) - if err != nil { - return fmt.Errorf("can't fetch %s hash: %w", nativenames.Neo, err) - } + gasHash := c.nativeHash(nativenames.Gas) + neoHash := c.nativeHash(nativenames.Neo) var transfers []client.TransferTarget for _, w := range c.Wallets { @@ -87,10 +81,7 @@ func (c *initializeContext) transferFunds() error { } func (c *initializeContext) transferFundsFinished() (bool, error) { - gasHash, err := c.Client.GetNativeContractHash(nativenames.Gas) - if err != nil { - return false, err - } + gasHash := c.nativeHash(nativenames.Gas) acc, err := getWalletAccount(c.Wallets[0], singleAccountName) if err != nil { @@ -150,11 +141,7 @@ func (c *initializeContext) multiSign(tx *transaction.Transaction, accType strin } func (c *initializeContext) transferGASToProxy() error { - gasHash, err := c.Client.GetNativeContractHash(nativenames.Gas) - if err != nil { - return fmt.Errorf("can't fetch %s hash: %w", nativenames.Gas, err) - } - + gasHash := c.nativeHash(nativenames.Gas) cs, err := c.readContract(proxyContract) if err != nil { return err