package config import ( "bytes" "encoding/binary" "encoding/hex" "errors" "fmt" "strconv" "strings" "text/tabwriter" "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/modules/morph/constants" "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/modules/morph/helper" "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/invoker" "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/vm/emit" "github.com/spf13/cobra" "github.com/spf13/viper" ) const forceConfigSet = "force" func dumpNetworkConfig(cmd *cobra.Command, _ []string) error { c, err := helper.GetN3Client(viper.GetViper()) if err != nil { return fmt.Errorf("can't create N3 client: %w", err) } inv := invoker.New(c, nil) r := management.NewReader(inv) cs, err := r.GetContractByID(1) if err != nil { return fmt.Errorf("can't get NNS contract info: %w", err) } nmHash, err := helper.NNSResolveHash(inv, cs.Hash, helper.DomainOf(constants.NetmapContract)) if err != nil { return fmt.Errorf("can't get netmap contract hash: %w", err) } arr, err := unwrap.Array(inv.Call(nmHash, "listConfig")) if err != nil { return errors.New("can't fetch list of network config keys from the netmap contract") } buf := bytes.NewBuffer(nil) tw := tabwriter.NewWriter(buf, 0, 2, 2, ' ', 0) m, err := helper.ParseConfigFromNetmapContract(arr) if err != nil { return err } for k, v := range m { switch k { case netmap.ContainerFeeConfig, netmap.ContainerAliasFeeConfig, netmap.EpochDurationConfig, netmap.IrCandidateFeeConfig, netmap.MaxObjectSizeConfig, netmap.WithdrawFeeConfig, netmap.MaxECDataCountConfig, netmap.MaxECParityCountConfig: nbuf := make([]byte, 8) copy(nbuf[:], v) n := binary.LittleEndian.Uint64(nbuf) _, _ = tw.Write([]byte(fmt.Sprintf("%s:\t%d (int)\n", k, n))) case netmap.HomomorphicHashingDisabledKey, netmap.MaintenanceModeAllowedConfig: if len(v) == 0 || len(v) > 1 { return helper.InvalidConfigValueErr(k) } _, _ = tw.Write([]byte(fmt.Sprintf("%s:\t%t (bool)\n", k, v[0] == 1))) default: _, _ = tw.Write([]byte(fmt.Sprintf("%s:\t%s (hex)\n", k, hex.EncodeToString(v)))) } } _ = tw.Flush() cmd.Print(buf.String()) return nil } func SetConfigCmd(cmd *cobra.Command, args []string) error { if len(args) == 0 { return errors.New("empty config pairs") } wCtx, err := helper.NewInitializeContext(cmd, viper.GetViper()) if err != nil { return fmt.Errorf("can't initialize context: %w", err) } r := management.NewReader(wCtx.ReadOnlyInvoker) cs, err := r.GetContractByID(1) if err != nil { return fmt.Errorf("can't get NNS contract info: %w", err) } nmHash, err := helper.NNSResolveHash(wCtx.ReadOnlyInvoker, cs.Hash, helper.DomainOf(constants.NetmapContract)) if err != nil { return fmt.Errorf("can't get netmap contract hash: %w", err) } forceFlag, _ := cmd.Flags().GetBool(forceConfigSet) bw := io.NewBufBinWriter() prm := make(map[string]any) for _, arg := range args { k, v, err := parseConfigPair(arg, forceFlag) if err != nil { return err } prm[k] = v } if err := validateConfig(prm, forceFlag); err != nil { return err } for k, v := range prm { // 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.SendConsensusTx(bw.Bytes()) if err != nil { return err } return wCtx.AwaitTx() } func validateConfig(args map[string]any, forceFlag bool) error { var sumEC int64 _, okData := args[netmap.MaxECDataCountConfig] _, okParity := args[netmap.MaxECParityCountConfig] if okData != okParity { return fmt.Errorf("both %s and %s must be present in the configuration", netmap.MaxECDataCountConfig, netmap.MaxECParityCountConfig) } for k, v := range args { switch k { case netmap.ContainerFeeConfig, netmap.ContainerAliasFeeConfig, netmap.EpochDurationConfig, netmap.IrCandidateFeeConfig, netmap.MaxObjectSizeConfig, netmap.WithdrawFeeConfig, netmap.MaxECDataCountConfig, netmap.MaxECParityCountConfig: value, ok := v.(int64) if !ok { return fmt.Errorf("%s has an invalid type. Expected type: int", k) } if value < 0 { return fmt.Errorf("%s must be >= 0, got %v", k, v) } if k == netmap.MaxECDataCountConfig || k == netmap.MaxECParityCountConfig { sumEC += value } case netmap.HomomorphicHashingDisabledKey, netmap.MaintenanceModeAllowedConfig: _, ok := v.(bool) if !ok { return fmt.Errorf("%s has an invalid type. Expected type: bool", k) } } } if sumEC > 256 && !forceFlag { return fmt.Errorf("the sum of %s and %s must be <= 256, got %d", netmap.MaxECDataCountConfig, netmap.MaxECParityCountConfig, sumEC) } return nil } func parseConfigPair(kvStr string, force bool) (key string, val any, err error) { k, v, found := strings.Cut(kvStr, "=") if !found { return "", nil, fmt.Errorf("invalid parameter format: must be 'key=val', got: %s", kvStr) } key = k valRaw := v switch key { case netmap.ContainerFeeConfig, netmap.ContainerAliasFeeConfig, netmap.EpochDurationConfig, netmap.IrCandidateFeeConfig, netmap.MaxObjectSizeConfig, netmap.WithdrawFeeConfig, netmap.MaxECDataCountConfig, netmap.MaxECParityCountConfig: 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 netmap.HomomorphicHashingDisabledKey, netmap.MaintenanceModeAllowedConfig: 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: if !force { return "", nil, fmt.Errorf( "'%s' key is not well-known, use '--%s' flag if want to set it anyway", key, forceConfigSet) } val = valRaw } return }