[#732] neofs-adm: fetch single accounts during the initialization

Simplify code and perform error checking before the actual work.

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2021-07-30 14:26:44 +03:00 committed by Alex Vanin
parent 018256def8
commit 8e71773c4a
6 changed files with 19 additions and 41 deletions

View file

@ -26,6 +26,8 @@ type initializeContext struct {
// ConsensusAcc is used for retrieving committee address and verification script.
ConsensusAcc *wallet.Account
Wallets []*wallet.Wallet
// Accounts contains simple signature accounts in the same order as in Wallets.
Accounts []*wallet.Account
Hashes []util.Uint256
WaitDuration time.Duration
PollInterval time.Duration
@ -134,11 +136,21 @@ func newInitializeContext(cmd *cobra.Command, v *viper.Viper) (*initializeContex
nativeHashes[ns[i].Manifest.Name] = ns[i].Hash
}
accounts := make([]*wallet.Account, len(wallets))
for i, w := range wallets {
acc, err := getWalletAccount(w, singleAccountName)
if err != nil {
return nil, fmt.Errorf("wallet %s is invalid (no single account): %w", w.Path(), err)
}
accounts[i] = acc
}
initCtx := &initializeContext{
Client: c,
ConsensusAcc: consensusAcc,
CommitteeAcc: committeeAcc,
Wallets: wallets,
Accounts: accounts,
WaitDuration: time.Second * 30,
PollInterval: time.Second,
Command: cmd,

View file

@ -122,12 +122,7 @@ func (c *initializeContext) deployContracts() error {
var keysParam []smartcontract.Parameter
// alphabet contracts should be deployed by individual nodes to get different hashes.
for i, w := range c.Wallets {
acc, err := getWalletAccount(w, singleAccountName)
if err != nil {
return err
}
for i, acc := range c.Accounts {
cs, err := c.readContract(alphabetContract)
if err != nil {
return err

View file

@ -45,12 +45,7 @@ func (c *initializeContext) setNNS() error {
if err != nil {
return fmt.Errorf("can't read alphabet contract: %w", err)
}
for i, w := range c.Wallets {
acc, err := getWalletAccount(w, singleAccountName)
if err != nil {
return err
}
for i, acc := range c.Accounts {
alphaCs.Hash = state.CreateContractHash(acc.Contract.ScriptHash(), alphaCs.NEF.Checksum, alphaCs.Manifest.Name)
domain := getAlphabetNNSDomain(i)

View file

@ -41,12 +41,7 @@ func (c *initializeContext) registerCandidates() error {
}
sysGas := regPrice + native.GASFactor // + 1 GAS
for _, w := range c.Wallets {
acc, err := getWalletAccount(w, singleAccountName)
if err != nil {
return err
}
for _, acc := range c.Accounts {
w := io.NewBufBinWriter()
emit.AppCall(w.BinWriter, neoHash, "registerCandidate", callflag.States, acc.PrivateKey().PublicKey().Bytes())
emit.Opcodes(w.BinWriter, opcode.ASSERT)
@ -84,12 +79,7 @@ func (c *initializeContext) transferNEOToAlphabetContracts() error {
amount := initialAlphabetNEOAmount / len(c.Wallets)
bw := io.NewBufBinWriter()
for _, w := range c.Wallets {
acc, err := getWalletAccount(w, singleAccountName)
if err != nil {
return err
}
for _, acc := range c.Accounts {
h := state.CreateContractHash(acc.Contract.ScriptHash(), cs.NEF.Checksum, cs.Manifest.Name)
emit.AppCall(bw.BinWriter, neoHash, "transfer", callflag.All,
c.CommitteeAcc.Contract.ScriptHash(), h, int64(amount), nil)

View file

@ -19,12 +19,7 @@ func (c *initializeContext) setNotaryAndAlphabetNodes() error {
designateHash := c.nativeHash(nativenames.Designation)
var pubs []interface{}
for _, w := range c.Wallets {
acc, err := getWalletAccount(w, singleAccountName)
if err != nil {
return err
}
for _, acc := range c.Accounts {
pubs = append(pubs, acc.PrivateKey().PublicKey().Bytes())
}

View file

@ -32,12 +32,7 @@ func (c *initializeContext) transferFunds() error {
neoHash := c.nativeHash(nativenames.Neo)
var transfers []client.TransferTarget
for _, w := range c.Wallets {
acc, err := getWalletAccount(w, singleAccountName)
if err != nil {
return err
}
for _, acc := range c.Accounts {
to := acc.Contract.ScriptHash()
transfers = append(transfers,
client.TransferTarget{
@ -82,11 +77,7 @@ func (c *initializeContext) transferFunds() error {
func (c *initializeContext) transferFundsFinished() (bool, error) {
gasHash := c.nativeHash(nativenames.Gas)
acc, err := getWalletAccount(c.Wallets[0], singleAccountName)
if err != nil {
return false, err
}
acc := c.Accounts[0]
res, err := c.Client.NEP17BalanceOf(gasHash, acc.Contract.ScriptHash())
return res > initialAlphabetGASAmount/2, err