diff --git a/cmd/neofs-adm/README.md b/cmd/neofs-adm/README.md index 735272e88..26e33bda8 100644 --- a/cmd/neofs-adm/README.md +++ b/cmd/neofs-adm/README.md @@ -70,6 +70,8 @@ credentials: # passwords for consensus node / alphabet wallets #### Network maintenance +- `set-config` Add/update configuration values in the Netmap contract. + - `force-new-epoch` increments NeoFS epoch number and executes new epoch handlers in NeoFS nodes. diff --git a/cmd/neofs-adm/internal/modules/morph/dump.go b/cmd/neofs-adm/internal/modules/morph/config.go similarity index 69% rename from cmd/neofs-adm/internal/modules/morph/dump.go rename to cmd/neofs-adm/internal/modules/morph/config.go index db0cf5477..d471a23d0 100644 --- a/cmd/neofs-adm/internal/modules/morph/dump.go +++ b/cmd/neofs-adm/internal/modules/morph/config.go @@ -6,6 +6,8 @@ import ( "encoding/hex" "errors" "fmt" + "strconv" + "strings" "text/tabwriter" "github.com/nspcc-dev/neo-go/pkg/io" @@ -220,3 +222,89 @@ func dumpNetworkConfig(cmd *cobra.Command, _ []string) error { func invalidConfigValueErr(key []byte) error { return fmt.Errorf("invalid %s config value from netmap contract", key) } + +func setConfigCmd(cmd *cobra.Command, args []string) error { + if len(args) == 0 { + return errors.New("empty config pairs") + } + + wCtx, err := newInitializeContext(cmd, viper.GetViper()) + if err != nil { + return fmt.Errorf("can't initialize context: %w", err) + } + + cs, err := wCtx.Client.GetContractStateByID(1) + if err != nil { + return fmt.Errorf("can't get NNS contract info: %w", err) + } + + nmHash, err := nnsResolveHash(wCtx.Client, cs.Hash, netmapContract+".neofs") + if err != nil { + return fmt.Errorf("can't get netmap contract hash: %w", err) + } + + bw := io.NewBufBinWriter() + for _, arg := range args { + k, v, err := parseConfigPair(arg) + if err != nil { + return err + } + + // In NeoFS this is done via Notary contract. Here, however, we can form the + // transaction locally. The first `nil` argument is required only for notary + // disabled environment which is not supported by that command. + emit.AppCall(bw.BinWriter, nmHash, "setConfig", callflag.All, nil, k, v) + if bw.Err != nil { + return fmt.Errorf("can't form raw transaction: %w", bw.Err) + } + } + + err = wCtx.sendCommitteeTx(bw.Bytes(), -1, true) + if err != nil { + return err + } + + return wCtx.awaitTx() +} + +func parseConfigPair(kvStr string) (key string, val interface{}, err error) { + kv := strings.SplitN(kvStr, "=", 2) + if len(kv) != 2 { + return "", nil, fmt.Errorf("invalid parameter format: must be 'key=val', got: %s", kvStr) + } + + key = kv[0] + valRaw := kv[1] + + switch key { + case netmapAuditFeeKey, netmapBasicIncomeRateKey, + netmapContainerFeeKey, netmapContainerAliasFeeKey, + netmapEigenTrustIterationsKey, + netmapEpochKey, netmapInnerRingCandidateFeeKey, + netmapMaxObjectSizeKey, netmapWithdrawFeeKey: + val, err = strconv.ParseInt(valRaw, 10, 64) + if err != nil { + err = fmt.Errorf("could not parse %s's value '%s' as int: %w", key, valRaw, err) + } + case netmapEigenTrustAlphaKey: + // just check that it could + // be parsed correctly + _, err = strconv.ParseFloat(kv[1], 64) + if err != nil { + err = fmt.Errorf("could not parse %s's value '%s' as float: %w", key, valRaw, err) + } + + val = valRaw + case netmapHomomorphicHashDisabledKey: + val, err = strconv.ParseBool(valRaw) + if err != nil { + err = fmt.Errorf("could not parse %s's value '%s' as bool: %w", key, valRaw, err) + } + default: + // print some warning that user + // want to add some unknown config? + val = valRaw + } + + return +} diff --git a/cmd/neofs-adm/internal/modules/morph/root.go b/cmd/neofs-adm/internal/modules/morph/root.go index f647af704..5f87d0c98 100644 --- a/cmd/neofs-adm/internal/modules/morph/root.go +++ b/cmd/neofs-adm/internal/modules/morph/root.go @@ -125,6 +125,17 @@ var ( RunE: removeNodesCmd, } + setConfig = &cobra.Command{ + Use: "set-config key1=val1 [key2=val2 ...]", + DisableFlagsInUseLine: true, + Short: "Add/update global config value in the NeoFS network", + PreRun: func(cmd *cobra.Command, _ []string) { + _ = viper.BindPFlag(alphabetWalletsFlag, cmd.Flags().Lookup(alphabetWalletsFlag)) + _ = viper.BindPFlag(endpointFlag, cmd.Flags().Lookup(endpointFlag)) + }, + RunE: setConfigCmd, + } + setPolicy = &cobra.Command{ Use: "set-policy [ExecFeeFactor=] [StoragePrice=] [FeePerByte=]", DisableFlagsInUseLine: true, @@ -245,6 +256,10 @@ func init() { RootCmd.AddCommand(dumpNetworkConfigCmd) dumpNetworkConfigCmd.Flags().StringP(endpointFlag, "r", "", "N3 RPC node endpoint") + RootCmd.AddCommand(setConfig) + setConfig.Flags().String(alphabetWalletsFlag, "", "path to alphabet wallets dir") + setConfig.Flags().StringP(endpointFlag, "r", "", "N3 RPC node endpoint") + RootCmd.AddCommand(dumpBalancesCmd) dumpBalancesCmd.Flags().StringP(endpointFlag, "r", "", "N3 RPC node endpoint") dumpBalancesCmd.Flags().BoolP(dumpBalancesStorageFlag, "s", false, "dump balances of storage nodes from the current netmap")