[#995] neofs-adm: split out tx context

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2021-11-30 14:45:40 +03:00 committed by Alex Vanin
parent 02be6c83a6
commit fba8890224
4 changed files with 38 additions and 15 deletions

View file

@ -21,7 +21,7 @@ import (
) )
type initializeContext struct { type initializeContext struct {
Client *client.Client clientContext
// CommitteeAcc is used for retrieving committee address and verification script. // CommitteeAcc is used for retrieving committee address and verification script.
CommitteeAcc *wallet.Account CommitteeAcc *wallet.Account
// ConsensusAcc is used for retrieving committee address and verification script. // ConsensusAcc is used for retrieving committee address and verification script.
@ -29,9 +29,6 @@ type initializeContext struct {
Wallets []*wallet.Wallet Wallets []*wallet.Wallet
// Accounts contains simple signature accounts in the same order as in Wallets. // Accounts contains simple signature accounts in the same order as in Wallets.
Accounts []*wallet.Account Accounts []*wallet.Account
Hashes []util.Uint256
WaitDuration time.Duration
PollInterval time.Duration
Contracts map[string]*contractState Contracts map[string]*contractState
Command *cobra.Command Command *cobra.Command
ContractPath string ContractPath string
@ -158,13 +155,15 @@ func newInitializeContext(cmd *cobra.Command, v *viper.Viper) (*initializeContex
} }
initCtx := &initializeContext{ initCtx := &initializeContext{
clientContext: clientContext{
Client: c, Client: c,
WaitDuration: time.Second * 30,
PollInterval: time.Second,
},
ConsensusAcc: consensusAcc, ConsensusAcc: consensusAcc,
CommitteeAcc: committeeAcc, CommitteeAcc: committeeAcc,
Wallets: wallets, Wallets: wallets,
Accounts: accounts, Accounts: accounts,
WaitDuration: time.Second * 30,
PollInterval: time.Second,
Command: cmd, Command: cmd,
Contracts: make(map[string]*contractState), Contracts: make(map[string]*contractState),
ContractPath: ctrPath, ContractPath: ctrPath,
@ -222,11 +221,15 @@ func openAlphabetWallets(walletDir string) ([]*wallet.Wallet, error) {
} }
func (c *initializeContext) awaitTx() error { func (c *initializeContext) awaitTx() error {
return c.clientContext.awaitTx(c.Command)
}
func (c *clientContext) awaitTx(cmd *cobra.Command) error {
if len(c.Hashes) == 0 { if len(c.Hashes) == 0 {
return nil return nil
} }
c.Command.Println("Waiting for transactions to persist...") cmd.Println("Waiting for transactions to persist...")
tick := time.NewTicker(c.PollInterval) tick := time.NewTicker(c.PollInterval)
defer tick.Stop() defer tick.Stop()

View file

@ -87,13 +87,7 @@ func (c *initializeContext) multiSignAndSend(tx *transaction.Transaction, accTyp
return err return err
} }
h, err := c.Client.SendRawTransaction(tx) return c.sendTx(tx, c.Command, false)
if err != nil {
return err
}
c.Hashes = append(c.Hashes, h)
return nil
} }
func (c *initializeContext) multiSign(tx *transaction.Transaction, accType string) error { func (c *initializeContext) multiSign(tx *transaction.Transaction, accType string) error {

View file

@ -3,11 +3,22 @@ package morph
import ( import (
"context" "context"
"errors" "errors"
"time"
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/rpc/client" "github.com/nspcc-dev/neo-go/pkg/rpc/client"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
type clientContext struct {
Client *client.Client
Hashes []util.Uint256
WaitDuration time.Duration
PollInterval time.Duration
}
func getN3Client(v *viper.Viper) (*client.Client, error) { func getN3Client(v *viper.Viper) (*client.Client, error) {
ctx := context.Background() // FIXME(@fyrchik): timeout context ctx := context.Background() // FIXME(@fyrchik): timeout context
endpoint := v.GetString(endpointFlag) endpoint := v.GetString(endpointFlag)
@ -23,3 +34,17 @@ func getN3Client(v *viper.Viper) (*client.Client, error) {
} }
return c, nil return c, nil
} }
func (c *clientContext) sendTx(tx *transaction.Transaction, cmd *cobra.Command, await bool) error {
h, err := c.Client.SendRawTransaction(tx)
if err != nil {
return err
}
c.Hashes = append(c.Hashes, h)
if await {
return c.awaitTx(cmd)
}
return nil
}

View file

@ -0,0 +1 @@
package morph