[#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 is used for retrieving committee address and verification script.
ConsensusAcc *wallet.Account ConsensusAcc *wallet.Account
Wallets []*wallet.Wallet Wallets []*wallet.Wallet
// Accounts contains simple signature accounts in the same order as in Wallets.
Accounts []*wallet.Account
Hashes []util.Uint256 Hashes []util.Uint256
WaitDuration time.Duration WaitDuration time.Duration
PollInterval 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 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{ initCtx := &initializeContext{
Client: c, Client: c,
ConsensusAcc: consensusAcc, ConsensusAcc: consensusAcc,
CommitteeAcc: committeeAcc, CommitteeAcc: committeeAcc,
Wallets: wallets, Wallets: wallets,
Accounts: accounts,
WaitDuration: time.Second * 30, WaitDuration: time.Second * 30,
PollInterval: time.Second, PollInterval: time.Second,
Command: cmd, Command: cmd,

View file

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

View file

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

View file

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

View file

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

View file

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