From f7a8f51c669968a6db1dd5caa49be36d45b5663a Mon Sep 17 00:00:00 2001 From: Anton Nikiforov Date: Thu, 1 Feb 2024 16:58:48 +0300 Subject: [PATCH] [#932] adm: Move command `deposit-notary` to package `notary` Signed-off-by: Anton Nikiforov --- .../internal/modules/morph/generate.go | 18 ++------- .../modules/morph/{ => notary}/notary.go | 21 ++++++---- .../internal/modules/morph/notary/root.go | 28 +++++++++++++ .../internal/modules/morph/root.go | 40 +++++-------------- .../internal/modules/morph/util/const.go | 2 + .../internal/modules/morph/util/util.go | 12 ++++++ 6 files changed, 68 insertions(+), 53 deletions(-) rename cmd/frostfs-adm/internal/modules/morph/{ => notary}/notary.go (86%) create mode 100644 cmd/frostfs-adm/internal/modules/morph/notary/root.go diff --git a/cmd/frostfs-adm/internal/modules/morph/generate.go b/cmd/frostfs-adm/internal/modules/morph/generate.go index 2258eb832..57c40ec9f 100644 --- a/cmd/frostfs-adm/internal/modules/morph/generate.go +++ b/cmd/frostfs-adm/internal/modules/morph/generate.go @@ -11,7 +11,6 @@ import ( "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/innerring" "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/io" "github.com/nspcc-dev/neo-go/pkg/rpcclient/gas" "github.com/nspcc-dev/neo-go/pkg/smartcontract" @@ -139,7 +138,7 @@ func generateStorageCreds(cmd *cobra.Command, _ []string) error { func refillGas(cmd *cobra.Command, gasFlag string, createWallet bool) (err error) { // storage wallet path is not part of the config - storageWalletPath, _ := cmd.Flags().GetString(storageWalletFlag) + storageWalletPath, _ := cmd.Flags().GetString(morphUtil.StorageWalletFlag) // wallet address is not part of the config walletAddress, _ := cmd.Flags().GetString(walletAddressFlag) @@ -152,7 +151,7 @@ func refillGas(cmd *cobra.Command, gasFlag string, createWallet bool) (err error } } else { if storageWalletPath == "" { - return fmt.Errorf("missing wallet path (use '--%s ')", storageWalletFlag) + return fmt.Errorf("missing wallet path (use '--%s ')", morphUtil.StorageWalletFlag) } var w *wallet.Wallet @@ -190,7 +189,7 @@ func refillGas(cmd *cobra.Command, gasFlag string, createWallet bool) (err error gasStr := viper.GetString(gasFlag) - gasAmount, err := parseGASAmount(gasStr) + gasAmount, err := morphUtil.ParseGASAmount(gasStr) if err != nil { return err } @@ -214,14 +213,3 @@ func refillGas(cmd *cobra.Command, gasFlag string, createWallet bool) (err error return wCtx.AwaitTx() } - -func parseGASAmount(s string) (fixedn.Fixed8, error) { - gasAmount, err := fixedn.Fixed8FromString(s) - if err != nil { - return 0, fmt.Errorf("invalid GAS amount %s: %w", s, err) - } - if gasAmount <= 0 { - return 0, fmt.Errorf("GAS amount must be positive (got %d)", gasAmount) - } - return gasAmount, nil -} diff --git a/cmd/frostfs-adm/internal/modules/morph/notary.go b/cmd/frostfs-adm/internal/modules/morph/notary/notary.go similarity index 86% rename from cmd/frostfs-adm/internal/modules/morph/notary.go rename to cmd/frostfs-adm/internal/modules/morph/notary/notary.go index d05307705..ecf9331b5 100644 --- a/cmd/frostfs-adm/internal/modules/morph/notary.go +++ b/cmd/frostfs-adm/internal/modules/morph/notary/notary.go @@ -1,4 +1,4 @@ -package morph +package notary import ( "fmt" @@ -21,9 +21,14 @@ import ( "github.com/spf13/viper" ) -// defaultNotaryDepositLifetime is an amount of blocks notary deposit stays valid. -// https://github.com/nspcc-dev/neo-go/blob/master/pkg/core/native/notary.go#L48 -const defaultNotaryDepositLifetime = 5760 +const ( + // defaultNotaryDepositLifetime is an amount of blocks notary deposit stays valid. + // https://github.com/nspcc-dev/neo-go/blob/master/pkg/core/native/notary.go#L48 + defaultNotaryDepositLifetime = 5760 + + walletAccountFlag = "account" + notaryDepositTillFlag = "till" +) func depositNotary(cmd *cobra.Command, _ []string) error { w, err := openWallet(cmd) @@ -55,11 +60,11 @@ func depositNotary(cmd *cobra.Command, _ []string) error { return fmt.Errorf("can't unlock account: %v", err) } - gasStr, err := cmd.Flags().GetString(refillGasAmountFlag) + gasStr, err := cmd.Flags().GetString(morphUtil.RefillGasAmountFlag) if err != nil { return err } - gasAmount, err := parseGASAmount(gasStr) + gasAmount, err := morphUtil.ParseGASAmount(gasStr) if err != nil { return err } @@ -121,11 +126,11 @@ func transferGas(cmd *cobra.Command, acc *wallet.Account, accHash util.Uint160, } func openWallet(cmd *cobra.Command) (*wallet.Wallet, error) { - p, err := cmd.Flags().GetString(storageWalletFlag) + p, err := cmd.Flags().GetString(morphUtil.StorageWalletFlag) if err != nil { return nil, err } else if p == "" { - return nil, fmt.Errorf("missing wallet path (use '--%s ')", storageWalletFlag) + return nil, fmt.Errorf("missing wallet path (use '--%s ')", morphUtil.StorageWalletFlag) } w, err := wallet.NewWalletFromFile(p) diff --git a/cmd/frostfs-adm/internal/modules/morph/notary/root.go b/cmd/frostfs-adm/internal/modules/morph/notary/root.go new file mode 100644 index 000000000..9a4164c7b --- /dev/null +++ b/cmd/frostfs-adm/internal/modules/morph/notary/root.go @@ -0,0 +1,28 @@ +package notary + +import ( + "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/modules/morph/util" + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +var DepositCmd = &cobra.Command{ + Use: "deposit-notary", + Short: "Deposit GAS for notary service", + PreRun: func(cmd *cobra.Command, _ []string) { + _ = viper.BindPFlag(util.EndpointFlag, cmd.Flags().Lookup(util.EndpointFlag)) + }, + RunE: depositNotary, +} + +func initDepositoryNotaryCmd() { + DepositCmd.Flags().StringP(util.EndpointFlag, util.EndpointFlagShort, "", util.EndpointFlagDesc) + DepositCmd.Flags().String(util.StorageWalletFlag, "", "Path to storage node wallet") + DepositCmd.Flags().String(walletAccountFlag, "", "Wallet account address") + DepositCmd.Flags().String(util.RefillGasAmountFlag, "", "Amount of GAS to deposit") + DepositCmd.Flags().String(notaryDepositTillFlag, "", "Notary deposit duration in blocks") +} + +func init() { + initDepositoryNotaryCmd() +} diff --git a/cmd/frostfs-adm/internal/modules/morph/root.go b/cmd/frostfs-adm/internal/modules/morph/root.go index ac757428a..fdc9bb32c 100644 --- a/cmd/frostfs-adm/internal/modules/morph/root.go +++ b/cmd/frostfs-adm/internal/modules/morph/root.go @@ -4,6 +4,7 @@ import ( "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/modules/morph/ape" "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/modules/morph/balance" "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/modules/morph/frostfsid" + "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/modules/morph/notary" "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/modules/morph/policy" "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/modules/morph/proxy" "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/modules/morph/util" @@ -12,8 +13,8 @@ import ( ) const ( - alphabetSizeFlag = "size" - storageWalletFlag = "storage-wallet" + alphabetSizeFlag = "size" + storageWalletLabelFlag = "label" storageGasCLIFlag = "initial-gas" storageGasConfigFlag = "storage.initial_gas" @@ -35,9 +36,6 @@ const ( containerDumpFlag = "dump" containerContractFlag = "container-contract" containerIDsFlag = "cid" - refillGasAmountFlag = "gas" - walletAccountFlag = "account" - notaryDepositTillFlag = "till" walletAddressFlag = "wallet-address" ) @@ -94,10 +92,10 @@ var ( PreRun: func(cmd *cobra.Command, _ []string) { _ = viper.BindPFlag(util.AlphabetWalletsFlag, cmd.Flags().Lookup(util.AlphabetWalletsFlag)) _ = viper.BindPFlag(util.EndpointFlag, cmd.Flags().Lookup(util.EndpointFlag)) - _ = viper.BindPFlag(refillGasAmountFlag, cmd.Flags().Lookup(refillGasAmountFlag)) + _ = viper.BindPFlag(util.RefillGasAmountFlag, cmd.Flags().Lookup(util.RefillGasAmountFlag)) }, RunE: func(cmd *cobra.Command, args []string) error { - return refillGas(cmd, refillGasAmountFlag, false) + return refillGas(cmd, util.RefillGasAmountFlag, false) }, } @@ -190,15 +188,6 @@ var ( RunE: listContainers, } - depositNotaryCmd = &cobra.Command{ - Use: "deposit-notary", - Short: "Deposit GAS for notary service", - PreRun: func(cmd *cobra.Command, _ []string) { - _ = viper.BindPFlag(util.EndpointFlag, cmd.Flags().Lookup(util.EndpointFlag)) - }, - RunE: depositNotary, - } - netmapCandidatesCmd = &cobra.Command{ Use: "netmap-candidates", Short: "List netmap candidates nodes", @@ -228,7 +217,7 @@ func init() { initRestoreContainersCmd() initListContainersCmd() initRefillGasCmd() - initDepositoryNotaryCmd() + RootCmd.AddCommand(notary.DepositCmd) initNetmapCandidatesCmd() RootCmd.AddCommand(ape.Cmd) @@ -243,23 +232,14 @@ func initNetmapCandidatesCmd() { netmapCandidatesCmd.Flags().StringP(util.EndpointFlag, util.EndpointFlagShort, "", util.EndpointFlagDesc) } -func initDepositoryNotaryCmd() { - RootCmd.AddCommand(depositNotaryCmd) - depositNotaryCmd.Flags().StringP(util.EndpointFlag, util.EndpointFlagShort, "", util.EndpointFlagDesc) - depositNotaryCmd.Flags().String(storageWalletFlag, "", "Path to storage node wallet") - depositNotaryCmd.Flags().String(walletAccountFlag, "", "Wallet account address") - depositNotaryCmd.Flags().String(refillGasAmountFlag, "", "Amount of GAS to deposit") - depositNotaryCmd.Flags().String(notaryDepositTillFlag, "", "Notary deposit duration in blocks") -} - func initRefillGasCmd() { RootCmd.AddCommand(refillGasCmd) refillGasCmd.Flags().String(util.AlphabetWalletsFlag, "", util.AlphabetWalletsFlagDesc) refillGasCmd.Flags().StringP(util.EndpointFlag, util.EndpointFlagShort, "", util.EndpointFlagDesc) - refillGasCmd.Flags().String(storageWalletFlag, "", "Path to storage node wallet") + refillGasCmd.Flags().String(util.StorageWalletFlag, "", "Path to storage node wallet") refillGasCmd.Flags().String(walletAddressFlag, "", "Address of wallet") - refillGasCmd.Flags().String(refillGasAmountFlag, "", "Additional amount of GAS to transfer") - refillGasCmd.MarkFlagsMutuallyExclusive(walletAddressFlag, storageWalletFlag) + refillGasCmd.Flags().String(util.RefillGasAmountFlag, "", "Additional amount of GAS to transfer") + refillGasCmd.MarkFlagsMutuallyExclusive(walletAddressFlag, util.StorageWalletFlag) } func initListContainersCmd() { @@ -330,7 +310,7 @@ func initGenerateStorageCmd() { RootCmd.AddCommand(generateStorageCmd) generateStorageCmd.Flags().String(util.AlphabetWalletsFlag, "", util.AlphabetWalletsFlagDesc) generateStorageCmd.Flags().StringP(util.EndpointFlag, util.EndpointFlagShort, "", util.EndpointFlagDesc) - generateStorageCmd.Flags().String(storageWalletFlag, "", "Path to new storage node wallet") + generateStorageCmd.Flags().String(util.StorageWalletFlag, "", "Path to new storage node wallet") generateStorageCmd.Flags().String(storageGasCLIFlag, "", "Initial amount of GAS to transfer") generateStorageCmd.Flags().StringP(storageWalletLabelFlag, "l", "", "Wallet label") } diff --git a/cmd/frostfs-adm/internal/modules/morph/util/const.go b/cmd/frostfs-adm/internal/modules/morph/util/const.go index f100ebc61..a532eb2ab 100644 --- a/cmd/frostfs-adm/internal/modules/morph/util/const.go +++ b/cmd/frostfs-adm/internal/modules/morph/util/const.go @@ -23,6 +23,8 @@ const ( ContractsURLFlagDesc = "URL to archive with compiled FrostFS contracts" EpochDurationInitFlag = "network.epoch_duration" MaxObjectSizeInitFlag = "network.max_object_size" + RefillGasAmountFlag = "gas" + StorageWalletFlag = "storage-wallet" SingleAccountName = "single" CommitteeAccountName = "committee" diff --git a/cmd/frostfs-adm/internal/modules/morph/util/util.go b/cmd/frostfs-adm/internal/modules/morph/util/util.go index a6f94049b..1fd9695c3 100644 --- a/cmd/frostfs-adm/internal/modules/morph/util/util.go +++ b/cmd/frostfs-adm/internal/modules/morph/util/util.go @@ -15,6 +15,7 @@ import ( "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/innerring" "github.com/nspcc-dev/neo-go/pkg/core/transaction" "github.com/nspcc-dev/neo-go/pkg/crypto/keys" + "github.com/nspcc-dev/neo-go/pkg/encoding/fixedn" "github.com/nspcc-dev/neo-go/pkg/rpcclient/actor" "github.com/nspcc-dev/neo-go/pkg/wallet" "github.com/spf13/viper" @@ -162,3 +163,14 @@ func readContractsFromArchive(file io.Reader, names []string) (map[string]*Contr func GetAlphabetNNSDomain(i int) string { return AlphabetContract + strconv.FormatUint(uint64(i), 10) + ".frostfs" } + +func ParseGASAmount(s string) (fixedn.Fixed8, error) { + gasAmount, err := fixedn.Fixed8FromString(s) + if err != nil { + return 0, fmt.Errorf("invalid GAS amount %s: %w", s, err) + } + if gasAmount <= 0 { + return 0, fmt.Errorf("GAS amount must be positive (got %d)", gasAmount) + } + return gasAmount, nil +}