[#732] neofs-adm: fetch native hashes once

Retrieve list of native contracts during initialization
in a single query.

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2021-07-29 16:35:57 +03:00 committed by Alex Vanin
parent 6a40adcfca
commit c81008764a
6 changed files with 26 additions and 37 deletions

View file

@ -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,

View file

@ -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 {

View file

@ -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)

View file

@ -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 {

View file

@ -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 {

View file

@ -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