From ce4254798032d3cc616cf05f2925a69f9a319a2c Mon Sep 17 00:00:00 2001 From: Anton Nikiforov Date: Thu, 1 Feb 2024 17:53:00 +0300 Subject: [PATCH] [#932] adm: Move `remove-nodes` to package `node` Signed-off-by: Anton Nikiforov --- .../internal/modules/morph/epoch.go | 22 +-------------- .../internal/modules/morph/initialize_test.go | 5 ++-- .../morph/{remove_node.go => node/remove.go} | 6 ++-- .../internal/modules/morph/node/root.go | 28 +++++++++++++++++++ .../internal/modules/morph/root.go | 21 ++------------ .../internal/modules/morph/util/netmap.go | 20 +++++++++++++ 6 files changed, 57 insertions(+), 45 deletions(-) rename cmd/frostfs-adm/internal/modules/morph/{remove_node.go => node/remove.go} (92%) create mode 100644 cmd/frostfs-adm/internal/modules/morph/node/root.go diff --git a/cmd/frostfs-adm/internal/modules/morph/epoch.go b/cmd/frostfs-adm/internal/modules/morph/epoch.go index 851f757b0..69d4d3151 100644 --- a/cmd/frostfs-adm/internal/modules/morph/epoch.go +++ b/cmd/frostfs-adm/internal/modules/morph/epoch.go @@ -1,17 +1,12 @@ package morph import ( - "errors" "fmt" "strings" util2 "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/modules/morph/util" "github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/rpcclient/management" - "github.com/nspcc-dev/neo-go/pkg/rpcclient/unwrap" - "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/spf13/cobra" "github.com/spf13/viper" ) @@ -34,7 +29,7 @@ func forceNewEpochCmd(cmd *cobra.Command, _ []string) error { } bw := io.NewBufBinWriter() - if err := emitNewEpochCall(bw, wCtx, nmHash); err != nil { + if err := util2.EmitNewEpochCall(bw, wCtx, nmHash); err != nil { return err } @@ -47,18 +42,3 @@ func forceNewEpochCmd(cmd *cobra.Command, _ []string) error { } return err } - -func emitNewEpochCall(bw *io.BufBinWriter, wCtx *util2.InitializeContext, nmHash util.Uint160) error { - curr, err := unwrap.Int64(wCtx.ReadOnlyInvoker.Call(nmHash, "epoch")) - if err != nil { - return errors.New("can't fetch current epoch from the netmap contract") - } - - newEpoch := curr + 1 - wCtx.Command.Printf("Current epoch: %d, increase to %d.\n", curr, newEpoch) - - // In NeoFS this is done via Notary contract. Here, however, we can form the - // transaction locally. - emit.AppCall(bw.BinWriter, nmHash, "newEpoch", callflag.All, newEpoch) - return bw.Err -} diff --git a/cmd/frostfs-adm/internal/modules/morph/initialize_test.go b/cmd/frostfs-adm/internal/modules/morph/initialize_test.go index f0ad8044c..02af6458c 100644 --- a/cmd/frostfs-adm/internal/modules/morph/initialize_test.go +++ b/cmd/frostfs-adm/internal/modules/morph/initialize_test.go @@ -10,6 +10,7 @@ import ( "time" cmdConfig "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/modules/morph/config" + "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/modules/morph/node" "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/util" "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/innerring" @@ -88,8 +89,8 @@ func testInitialize(t *testing.T, committeeSize int) { require.NoError(t, err) pub := hex.EncodeToString(pk.PublicKey().Bytes()) - require.NoError(t, removeNodes.Flags().Set(util.LocalDumpFlag, dumpPath)) - require.NoError(t, removeNodesCmd(removeNodes, []string{pub})) + require.NoError(t, node.RemoveCmd.Flags().Set(util.LocalDumpFlag, dumpPath)) + require.NoError(t, node.RemoveNodesCmd(node.RemoveCmd, []string{pub})) }) } diff --git a/cmd/frostfs-adm/internal/modules/morph/remove_node.go b/cmd/frostfs-adm/internal/modules/morph/node/remove.go similarity index 92% rename from cmd/frostfs-adm/internal/modules/morph/remove_node.go rename to cmd/frostfs-adm/internal/modules/morph/node/remove.go index e63a5dee9..0e305c1f4 100644 --- a/cmd/frostfs-adm/internal/modules/morph/remove_node.go +++ b/cmd/frostfs-adm/internal/modules/morph/node/remove.go @@ -1,4 +1,4 @@ -package morph +package node import ( "errors" @@ -15,7 +15,7 @@ import ( "github.com/spf13/viper" ) -func removeNodesCmd(cmd *cobra.Command, args []string) error { +func RemoveNodesCmd(cmd *cobra.Command, args []string) error { if len(args) == 0 { return errors.New("at least one node key must be provided") } @@ -52,7 +52,7 @@ func removeNodesCmd(cmd *cobra.Command, args []string) error { int64(netmapcontract.NodeStateOffline), nodeKeys[i].Bytes()) } - if err := emitNewEpochCall(bw, wCtx, nmHash); err != nil { + if err := util.EmitNewEpochCall(bw, wCtx, nmHash); err != nil { return err } diff --git a/cmd/frostfs-adm/internal/modules/morph/node/root.go b/cmd/frostfs-adm/internal/modules/morph/node/root.go new file mode 100644 index 000000000..e301a1bc3 --- /dev/null +++ b/cmd/frostfs-adm/internal/modules/morph/node/root.go @@ -0,0 +1,28 @@ +package node + +import ( + "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/modules/morph/util" + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +var RemoveCmd = &cobra.Command{ + Use: "remove-nodes key1 [key2 [...]]", + Short: "Remove storage nodes from the netmap", + Long: `Move nodes to the Offline state in the candidates list and tick an epoch to update the netmap`, + PreRun: func(cmd *cobra.Command, _ []string) { + _ = viper.BindPFlag(util.AlphabetWalletsFlag, cmd.Flags().Lookup(util.AlphabetWalletsFlag)) + _ = viper.BindPFlag(util.EndpointFlag, cmd.Flags().Lookup(util.EndpointFlag)) + }, + RunE: RemoveNodesCmd, +} + +func initRemoveNodesCmd() { + RemoveCmd.Flags().String(util.AlphabetWalletsFlag, "", util.AlphabetWalletsFlagDesc) + RemoveCmd.Flags().StringP(util.EndpointFlag, util.EndpointFlagShort, "", util.EndpointFlagDesc) + RemoveCmd.Flags().String(util.LocalDumpFlag, "", "Path to the blocks dump file") +} + +func init() { + initRemoveNodesCmd() +} diff --git a/cmd/frostfs-adm/internal/modules/morph/root.go b/cmd/frostfs-adm/internal/modules/morph/root.go index 908c4183f..23c413bff 100644 --- a/cmd/frostfs-adm/internal/modules/morph/root.go +++ b/cmd/frostfs-adm/internal/modules/morph/root.go @@ -7,6 +7,7 @@ import ( "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/modules/morph/container" "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/netmap" + "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/modules/morph/node" "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" @@ -107,17 +108,6 @@ var ( RunE: forceNewEpochCmd, } - removeNodes = &cobra.Command{ - Use: "remove-nodes key1 [key2 [...]]", - Short: "Remove storage nodes from the netmap", - Long: `Move nodes to the Offline state in the candidates list and tick an epoch to update the netmap`, - PreRun: func(cmd *cobra.Command, _ []string) { - _ = viper.BindPFlag(util.AlphabetWalletsFlag, cmd.Flags().Lookup(util.AlphabetWalletsFlag)) - _ = viper.BindPFlag(util.EndpointFlag, cmd.Flags().Lookup(util.EndpointFlag)) - }, - RunE: removeNodesCmd, - } - dumpContractHashesCmd = &cobra.Command{ Use: "dump-hashes", Short: "Dump deployed contract hashes", @@ -144,7 +134,7 @@ func init() { initDeployCmd() initGenerateStorageCmd() initForceNewEpochCmd() - initRemoveNodesCmd() + RootCmd.AddCommand(node.RemoveCmd) RootCmd.AddCommand(policy.Set) RootCmd.AddCommand(policy.Dump) initDumpContractHashesCmd() @@ -191,13 +181,6 @@ func initDumpContractHashesCmd() { dumpContractHashesCmd.Flags().String(customZoneFlag, "", "Custom zone to search.") } -func initRemoveNodesCmd() { - RootCmd.AddCommand(removeNodes) - removeNodes.Flags().String(util.AlphabetWalletsFlag, "", util.AlphabetWalletsFlagDesc) - removeNodes.Flags().StringP(util.EndpointFlag, util.EndpointFlagShort, "", util.EndpointFlagDesc) - removeNodes.Flags().String(util.LocalDumpFlag, "", "Path to the blocks dump file") -} - func initForceNewEpochCmd() { RootCmd.AddCommand(forceNewEpoch) forceNewEpoch.Flags().String(util.AlphabetWalletsFlag, "", util.AlphabetWalletsFlagDesc) diff --git a/cmd/frostfs-adm/internal/modules/morph/util/netmap.go b/cmd/frostfs-adm/internal/modules/morph/util/netmap.go index c9dc2e3b5..34c527d0b 100644 --- a/cmd/frostfs-adm/internal/modules/morph/util/netmap.go +++ b/cmd/frostfs-adm/internal/modules/morph/util/netmap.go @@ -5,6 +5,11 @@ import ( "fmt" "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client/netmap" + "github.com/nspcc-dev/neo-go/pkg/io" + "github.com/nspcc-dev/neo-go/pkg/rpcclient/unwrap" + "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/stackitem" "github.com/spf13/viper" ) @@ -47,3 +52,18 @@ func ParseConfigFromNetmapContract(arr []stackitem.Item) (map[string][]byte, err func InvalidConfigValueErr(key string) error { return fmt.Errorf("invalid %s config value from netmap contract", key) } + +func EmitNewEpochCall(bw *io.BufBinWriter, wCtx *InitializeContext, nmHash util.Uint160) error { + curr, err := unwrap.Int64(wCtx.ReadOnlyInvoker.Call(nmHash, "epoch")) + if err != nil { + return errors.New("can't fetch current epoch from the netmap contract") + } + + newEpoch := curr + 1 + wCtx.Command.Printf("Current epoch: %d, increase to %d.\n", curr, newEpoch) + + // In NeoFS this is done via Notary contract. Here, however, we can form the + // transaction locally. + emit.AppCall(bw.BinWriter, nmHash, "newEpoch", callflag.All, newEpoch) + return bw.Err +}