[#687] neofs-adm: check for initialization stage completion

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2021-07-20 17:51:45 +03:00 committed by Alex Vanin
parent c3f7ccaee6
commit 8ea67ec565
3 changed files with 51 additions and 3 deletions

View file

@ -29,6 +29,7 @@ type initializeContext struct {
Hashes []util.Uint256 Hashes []util.Uint256
WaitDuration time.Duration WaitDuration time.Duration
PollInterval time.Duration PollInterval time.Duration
Command *cobra.Command
} }
func initializeSideChainCmd(cmd *cobra.Command, args []string) error { func initializeSideChainCmd(cmd *cobra.Command, args []string) error {
@ -38,7 +39,7 @@ func initializeSideChainCmd(cmd *cobra.Command, args []string) error {
return err return err
} }
initCtx, err := newInitializeContext(viper.GetViper()) initCtx, err := newInitializeContext(cmd, viper.GetViper())
if err != nil { if err != nil {
return fmt.Errorf("initialization error: %w", err) return fmt.Errorf("initialization error: %w", err)
} }
@ -68,7 +69,7 @@ func initializeSideChainCmd(cmd *cobra.Command, args []string) error {
return nil return nil
} }
func newInitializeContext(v *viper.Viper) (*initializeContext, error) { func newInitializeContext(cmd *cobra.Command, v *viper.Viper) (*initializeContext, error) {
walletDir := v.GetString(alphabetWalletsFlag) walletDir := v.GetString(alphabetWalletsFlag)
wallets, err := openAlphabetWallets(walletDir) wallets, err := openAlphabetWallets(walletDir)
if err != nil { if err != nil {
@ -97,6 +98,7 @@ func newInitializeContext(v *viper.Viper) (*initializeContext, error) {
Wallets: wallets, Wallets: wallets,
WaitDuration: time.Second * 30, WaitDuration: time.Second * 30,
PollInterval: time.Second, PollInterval: time.Second,
Command: cmd,
} }
return initCtx, nil return initCtx, nil
@ -139,6 +141,8 @@ func openAlphabetWallets(walletDir string) ([]*wallet.Wallet, error) {
} }
func (c *initializeContext) awaitTx() error { func (c *initializeContext) awaitTx() error {
c.Command.Println("Waiting for transactions to persist...")
tick := time.NewTicker(c.PollInterval) tick := time.NewTicker(c.PollInterval)
defer tick.Stop() defer tick.Stop()
@ -149,6 +153,10 @@ func (c *initializeContext) awaitTx() error {
loop: loop:
for i := range c.Hashes { for i := range c.Hashes {
_, err := c.Client.GetApplicationLog(c.Hashes[i], &at)
if err == nil {
continue loop
}
for { for {
select { select {
case <-tick.C: case <-tick.C:

View file

@ -11,6 +11,13 @@ import (
) )
func (c *initializeContext) setNotaryAndAlphabetNodes() error { func (c *initializeContext) setNotaryAndAlphabetNodes() error {
if ok, err := c.setRolesFinished(); ok || err != nil {
if err == nil {
c.Command.Println("Stage 2: already performed.")
}
return err
}
designateHash, err := c.Client.GetNativeContractHash(nativenames.Designation) designateHash, err := c.Client.GetNativeContractHash(nativenames.Designation)
if err != nil { if err != nil {
return fmt.Errorf("can't fetch %s hash: %w", nativenames.Designation, err) return fmt.Errorf("can't fetch %s hash: %w", nativenames.Designation, err)
@ -38,3 +45,13 @@ func (c *initializeContext) setNotaryAndAlphabetNodes() error {
return c.awaitTx() return c.awaitTx()
} }
func (c *initializeContext) setRolesFinished() (bool, error) {
height, err := c.Client.GetBlockCount()
if err != nil {
return false, err
}
pubs, err := c.Client.GetDesignatedByRole(noderoles.NeoFSAlphabet, height)
return len(pubs) == len(c.Wallets), err
}

View file

@ -17,6 +17,14 @@ const (
) )
func (c *initializeContext) transferFunds() error { func (c *initializeContext) transferFunds() error {
ok, err := c.transferFundsFinished()
if ok || err != nil {
if err == nil {
c.Command.Println("Stage 1: already performed.")
}
return err
}
gasHash, err := c.Client.GetNativeContractHash(nativenames.Gas) gasHash, err := c.Client.GetNativeContractHash(nativenames.Gas)
if err != nil { if err != nil {
return fmt.Errorf("can't fetch %s hash: %w", nativenames.Gas, err) return fmt.Errorf("can't fetch %s hash: %w", nativenames.Gas, err)
@ -48,7 +56,7 @@ func (c *initializeContext) transferFunds() error {
client.TransferTarget{ client.TransferTarget{
Token: gasHash, Token: gasHash,
Address: c.CommitteeAcc.Contract.ScriptHash(), Address: c.CommitteeAcc.Contract.ScriptHash(),
Amount: gasInitialTotalSupply - initialAlphabetGASAmount*int64(len(c.Wallets)), Amount: (gasInitialTotalSupply - initialAlphabetGASAmount*int64(len(c.Wallets))) / 2,
}, },
client.TransferTarget{ client.TransferTarget{
Token: neoHash, Token: neoHash,
@ -75,6 +83,21 @@ func (c *initializeContext) transferFunds() error {
return c.awaitTx() return c.awaitTx()
} }
func (c *initializeContext) transferFundsFinished() (bool, error) {
gasHash, err := c.Client.GetNativeContractHash(nativenames.Gas)
if err != nil {
return false, err
}
acc, err := getWalletAccount(c.Wallets[0], singleAccountName)
if err != nil {
return false, err
}
res, err := c.Client.NEP17BalanceOf(gasHash, acc.Contract.ScriptHash())
return res > initialAlphabetGASAmount/2, err
}
func (c *initializeContext) multiSignAndSend(tx *transaction.Transaction, accType string) error { func (c *initializeContext) multiSignAndSend(tx *transaction.Transaction, accType string) error {
if err := c.multiSign(tx, accType); err != nil { if err := c.multiSign(tx, accType); err != nil {
return err return err