[#1820] neofs-adm: Add wallet-address flag in refill command

Signed-off-by: Alex Vanin <a.vanin@yadro.com>
This commit is contained in:
Alexey Vanin 2022-09-28 11:19:57 +03:00 committed by fyrchik
parent 1edc048870
commit 76cfcc242c
3 changed files with 47 additions and 30 deletions

View file

@ -11,6 +11,7 @@ Changelog for NeoFS Node
- Validate storage node configuration before node startup (#1805) - Validate storage node configuration before node startup (#1805)
- `neofs-node -check` command to check the configuration file (#1805) - `neofs-node -check` command to check the configuration file (#1805)
- `flush-cache` control service command to flush write-cache (#1806) - `flush-cache` control service command to flush write-cache (#1806)
- `wallet-address` flag in `neofs-adm morph refill-gas` command (#1820)
### Changed ### Changed

View file

@ -8,10 +8,12 @@ import (
"github.com/nspcc-dev/neo-go/pkg/core/native/nativenames" "github.com/nspcc-dev/neo-go/pkg/core/native/nativenames"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn" "github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
"github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/smartcontract" "github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag" "github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/vm/emit" "github.com/nspcc-dev/neo-go/pkg/vm/emit"
"github.com/nspcc-dev/neo-go/pkg/vm/opcode" "github.com/nspcc-dev/neo-go/pkg/vm/opcode"
"github.com/nspcc-dev/neo-go/pkg/wallet" "github.com/nspcc-dev/neo-go/pkg/wallet"
@ -133,44 +135,55 @@ func generateStorageCreds(cmd *cobra.Command, _ []string) error {
return refillGas(cmd, storageGasConfigFlag, true) return refillGas(cmd, storageGasConfigFlag, true)
} }
func refillGas(cmd *cobra.Command, gasFlag string, createWallet bool) error { func refillGas(cmd *cobra.Command, gasFlag string, createWallet bool) (err error) {
// storage wallet path is not part of the config // storage wallet path is not part of the config
storageWalletPath, err := cmd.Flags().GetString(storageWalletFlag) storageWalletPath, _ := cmd.Flags().GetString(storageWalletFlag)
if err != nil { // wallet address is not part of the config
return err walletAddress, _ := cmd.Flags().GetString(walletAddressFlag)
}
if storageWalletPath == "" {
return fmt.Errorf("missing wallet path (use '--%s <out.json>')", storageWalletFlag)
}
var w *wallet.Wallet var gasReceiver util.Uint160
if createWallet { if len(walletAddress) != 0 {
w, err = wallet.NewWallet(storageWalletPath) gasReceiver, err = address.StringToUint160(walletAddress)
} else {
w, err = wallet.NewWalletFromFile(storageWalletPath)
}
if err != nil {
return fmt.Errorf("can't create wallet: %w", err)
}
if createWallet {
var password string
label, _ := cmd.Flags().GetString(storageWalletLabelFlag)
password, err := config.GetStoragePassword(viper.GetViper(), label)
if err != nil { if err != nil {
return fmt.Errorf("can't fetch password: %w", err) return fmt.Errorf("invalid wallet address %s: %w", walletAddress, err)
}
} else {
if storageWalletPath == "" {
return fmt.Errorf("missing wallet path (use '--%s <out.json>')", storageWalletFlag)
} }
if label == "" { var w *wallet.Wallet
label = singleAccountName
if createWallet {
w, err = wallet.NewWallet(storageWalletPath)
} else {
w, err = wallet.NewWalletFromFile(storageWalletPath)
} }
if err := w.CreateAccount(label, password); err != nil { if err != nil {
return fmt.Errorf("can't create account: %w", err) return fmt.Errorf("can't create wallet: %w", err)
} }
if createWallet {
var password string
label, _ := cmd.Flags().GetString(storageWalletLabelFlag)
password, err := config.GetStoragePassword(viper.GetViper(), label)
if err != nil {
return fmt.Errorf("can't fetch password: %w", err)
}
if label == "" {
label = singleAccountName
}
if err := w.CreateAccount(label, password); err != nil {
return fmt.Errorf("can't create account: %w", err)
}
}
gasReceiver = w.Accounts[0].Contract.ScriptHash()
} }
gasStr := viper.GetString(gasFlag) gasStr := viper.GetString(gasFlag)
@ -189,7 +202,7 @@ func refillGas(cmd *cobra.Command, gasFlag string, createWallet bool) error {
bw := io.NewBufBinWriter() bw := io.NewBufBinWriter()
emit.AppCall(bw.BinWriter, gasHash, "transfer", callflag.All, emit.AppCall(bw.BinWriter, gasHash, "transfer", callflag.All,
wCtx.CommitteeAcc.Contract.ScriptHash(), w.Accounts[0].Contract.ScriptHash(), int64(gasAmount), nil) wCtx.CommitteeAcc.Contract.ScriptHash(), gasReceiver, int64(gasAmount), nil)
emit.Opcodes(bw.BinWriter, opcode.ASSERT) emit.Opcodes(bw.BinWriter, opcode.ASSERT)
if bw.Err != nil { if bw.Err != nil {
return fmt.Errorf("BUG: invalid transfer arguments: %w", bw.Err) return fmt.Errorf("BUG: invalid transfer arguments: %w", bw.Err)

View file

@ -40,6 +40,7 @@ const (
notaryDepositTillFlag = "till" notaryDepositTillFlag = "till"
localDumpFlag = "local-dump" localDumpFlag = "local-dump"
protoConfigPath = "protocol" protoConfigPath = "protocol"
walletAddressFlag = "wallet-address"
) )
var ( var (
@ -296,7 +297,9 @@ func init() {
refillGasCmd.Flags().String(alphabetWalletsFlag, "", "path to alphabet wallets dir") refillGasCmd.Flags().String(alphabetWalletsFlag, "", "path to alphabet wallets dir")
refillGasCmd.Flags().StringP(endpointFlag, "r", "", "N3 RPC node endpoint") refillGasCmd.Flags().StringP(endpointFlag, "r", "", "N3 RPC node endpoint")
refillGasCmd.Flags().String(storageWalletFlag, "", "path to storage node wallet") refillGasCmd.Flags().String(storageWalletFlag, "", "path to storage node wallet")
refillGasCmd.Flags().String(walletAddressFlag, "", "address of wallet")
refillGasCmd.Flags().String(refillGasAmountFlag, "", "additional amount of GAS to transfer") refillGasCmd.Flags().String(refillGasAmountFlag, "", "additional amount of GAS to transfer")
refillGasCmd.MarkFlagsMutuallyExclusive(walletAddressFlag, storageWalletFlag)
RootCmd.AddCommand(cmdSubnet) RootCmd.AddCommand(cmdSubnet)