2024-02-01 14:28:10 +00:00
|
|
|
package config
|
2021-07-28 14:10:02 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-08-03 12:05:10 +00:00
|
|
|
"encoding/binary"
|
|
|
|
"encoding/hex"
|
2021-07-28 14:10:02 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2022-05-11 18:35:49 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2021-07-28 14:10:02 +00:00
|
|
|
"text/tabwriter"
|
|
|
|
|
2024-02-02 12:26:57 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/modules/morph/constants"
|
2024-02-02 12:36:14 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/modules/morph/helper"
|
2023-04-11 11:59:24 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client/netmap"
|
2021-07-28 14:10:02 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
2022-09-05 12:45:38 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/invoker"
|
2023-10-24 10:06:30 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/management"
|
2022-09-05 12:45:38 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/unwrap"
|
2021-07-28 14:10:02 +00:00
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
2022-05-11 18:35:49 +00:00
|
|
|
const forceConfigSet = "force"
|
|
|
|
|
2021-08-03 12:05:10 +00:00
|
|
|
func dumpNetworkConfig(cmd *cobra.Command, _ []string) error {
|
2024-02-02 12:36:14 +00:00
|
|
|
c, err := helper.GetN3Client(viper.GetViper())
|
2021-08-03 12:05:10 +00:00
|
|
|
if err != nil {
|
2021-10-18 12:14:32 +00:00
|
|
|
return fmt.Errorf("can't create N3 client: %w", err)
|
2021-08-03 12:05:10 +00:00
|
|
|
}
|
|
|
|
|
2022-09-05 12:45:38 +00:00
|
|
|
inv := invoker.New(c, nil)
|
2023-10-24 10:06:30 +00:00
|
|
|
r := management.NewReader(inv)
|
2022-09-05 12:45:38 +00:00
|
|
|
|
2023-10-24 10:06:30 +00:00
|
|
|
cs, err := r.GetContractByID(1)
|
2021-08-03 12:05:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't get NNS contract info: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-02-02 12:36:14 +00:00
|
|
|
nmHash, err := helper.NNSResolveHash(inv, cs.Hash, helper.DomainOf(constants.NetmapContract))
|
2021-08-03 12:05:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't get netmap contract hash: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-09-05 12:45:38 +00:00
|
|
|
arr, err := unwrap.Array(inv.Call(nmHash, "listConfig"))
|
|
|
|
if err != nil {
|
2021-08-03 12:05:10 +00:00
|
|
|
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)
|
|
|
|
|
2024-02-02 12:36:14 +00:00
|
|
|
m, err := helper.ParseConfigFromNetmapContract(arr)
|
2023-04-13 14:42:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for k, v := range m {
|
|
|
|
switch k {
|
2023-08-03 11:18:56 +00:00
|
|
|
case netmap.ContainerFeeConfig, netmap.ContainerAliasFeeConfig,
|
2023-04-11 11:59:24 +00:00
|
|
|
netmap.EpochDurationConfig, netmap.IrCandidateFeeConfig,
|
2024-04-08 09:27:30 +00:00
|
|
|
netmap.MaxObjectSizeConfig, netmap.WithdrawFeeConfig,
|
|
|
|
netmap.MaxECDataCountConfig, netmap.MaxECParityCountConfig:
|
2021-08-03 12:05:10 +00:00
|
|
|
nbuf := make([]byte, 8)
|
|
|
|
copy(nbuf[:], v)
|
|
|
|
n := binary.LittleEndian.Uint64(nbuf)
|
|
|
|
_, _ = tw.Write([]byte(fmt.Sprintf("%s:\t%d (int)\n", k, n)))
|
2023-04-11 11:59:24 +00:00
|
|
|
case netmap.HomomorphicHashingDisabledKey, netmap.MaintenanceModeAllowedConfig:
|
2023-04-13 14:42:59 +00:00
|
|
|
if len(v) == 0 || len(v) > 1 {
|
2024-02-02 12:36:14 +00:00
|
|
|
return helper.InvalidConfigValueErr(k)
|
2022-05-06 20:33:50 +00:00
|
|
|
}
|
2023-04-13 14:42:59 +00:00
|
|
|
_, _ = tw.Write([]byte(fmt.Sprintf("%s:\t%t (bool)\n", k, v[0] == 1)))
|
2021-08-03 12:05:10 +00:00
|
|
|
default:
|
|
|
|
_, _ = tw.Write([]byte(fmt.Sprintf("%s:\t%s (hex)\n", k, hex.EncodeToString(v))))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = tw.Flush()
|
|
|
|
cmd.Print(buf.String())
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-05-11 18:35:49 +00:00
|
|
|
|
2024-02-01 14:28:10 +00:00
|
|
|
func SetConfigCmd(cmd *cobra.Command, args []string) error {
|
2022-05-11 18:35:49 +00:00
|
|
|
if len(args) == 0 {
|
|
|
|
return errors.New("empty config pairs")
|
|
|
|
}
|
|
|
|
|
2024-02-02 12:36:14 +00:00
|
|
|
wCtx, err := helper.NewInitializeContext(cmd, viper.GetViper())
|
2022-05-11 18:35:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't initialize context: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-10-24 10:06:30 +00:00
|
|
|
r := management.NewReader(wCtx.ReadOnlyInvoker)
|
|
|
|
cs, err := r.GetContractByID(1)
|
2022-05-11 18:35:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't get NNS contract info: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-02-02 12:36:14 +00:00
|
|
|
nmHash, err := helper.NNSResolveHash(wCtx.ReadOnlyInvoker, cs.Hash, helper.DomainOf(constants.NetmapContract))
|
2022-05-11 18:35:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't get netmap contract hash: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
forceFlag, _ := cmd.Flags().GetBool(forceConfigSet)
|
|
|
|
bw := io.NewBufBinWriter()
|
2024-03-31 18:36:57 +00:00
|
|
|
prm := make(map[string]any)
|
2022-05-11 18:35:49 +00:00
|
|
|
for _, arg := range args {
|
|
|
|
k, v, err := parseConfigPair(arg, forceFlag)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-03-31 18:36:57 +00:00
|
|
|
prm[k] = v
|
|
|
|
}
|
|
|
|
|
2024-04-08 09:27:30 +00:00
|
|
|
if err := validateConfig(prm, forceFlag); err != nil {
|
2024-03-31 18:36:57 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range prm {
|
2022-05-11 18:35:49 +00:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-01 11:30:59 +00:00
|
|
|
err = wCtx.SendConsensusTx(bw.Bytes())
|
2022-05-11 18:35:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-02-01 11:30:59 +00:00
|
|
|
return wCtx.AwaitTx()
|
2022-05-11 18:35:49 +00:00
|
|
|
}
|
|
|
|
|
2024-04-08 09:27:30 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-03-31 18:36:57 +00:00
|
|
|
for k, v := range args {
|
2024-06-07 13:22:46 +00:00
|
|
|
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)
|
|
|
|
}
|
2024-04-08 09:27:30 +00:00
|
|
|
|
2024-06-07 13:22:46 +00:00
|
|
|
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)
|
|
|
|
}
|
2024-04-08 09:27:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if sumEC > 256 && !forceFlag {
|
|
|
|
return fmt.Errorf("the sum of %s and %s must be <= 256, got %d",
|
|
|
|
netmap.MaxECDataCountConfig, netmap.MaxECParityCountConfig, sumEC)
|
2024-03-31 18:36:57 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-21 11:42:45 +00:00
|
|
|
func parseConfigPair(kvStr string, force bool) (key string, val any, err error) {
|
2023-02-27 14:19:35 +00:00
|
|
|
k, v, found := strings.Cut(kvStr, "=")
|
|
|
|
if !found {
|
2022-05-11 18:35:49 +00:00
|
|
|
return "", nil, fmt.Errorf("invalid parameter format: must be 'key=val', got: %s", kvStr)
|
|
|
|
}
|
|
|
|
|
2023-02-27 14:19:35 +00:00
|
|
|
key = k
|
|
|
|
valRaw := v
|
2022-05-11 18:35:49 +00:00
|
|
|
|
|
|
|
switch key {
|
2023-08-03 11:18:56 +00:00
|
|
|
case netmap.ContainerFeeConfig, netmap.ContainerAliasFeeConfig,
|
2023-04-11 11:59:24 +00:00
|
|
|
netmap.EpochDurationConfig, netmap.IrCandidateFeeConfig,
|
2024-04-08 09:27:30 +00:00
|
|
|
netmap.MaxObjectSizeConfig, netmap.WithdrawFeeConfig,
|
|
|
|
netmap.MaxECDataCountConfig, netmap.MaxECParityCountConfig:
|
2022-05-11 18:35:49 +00:00
|
|
|
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)
|
|
|
|
}
|
2023-04-11 11:59:24 +00:00
|
|
|
case netmap.HomomorphicHashingDisabledKey, netmap.MaintenanceModeAllowedConfig:
|
2022-05-06 20:33:50 +00:00
|
|
|
val, err = strconv.ParseBool(valRaw)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("could not parse %s's value '%s' as bool: %w", key, valRaw, err)
|
|
|
|
}
|
|
|
|
|
2022-05-11 18:35:49 +00:00
|
|
|
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
|
|
|
|
}
|