2024-02-01 12:55:33 +00:00
|
|
|
package policy
|
2022-04-04 15:36:10 +00:00
|
|
|
|
|
|
|
import (
|
2023-11-02 13:20:37 +00:00
|
|
|
"bytes"
|
2024-03-11 14:55:50 +00:00
|
|
|
"errors"
|
2022-04-04 15:36:10 +00:00
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2023-11-02 13:20:37 +00:00
|
|
|
"text/tabwriter"
|
2022-04-04 15:36:10 +00:00
|
|
|
|
2024-02-02 12:36:14 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/modules/morph/helper"
|
2023-11-02 13:20:37 +00:00
|
|
|
commonCmd "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/internal/common"
|
2022-04-04 15:36:10 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
2023-11-02 13:20:37 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/invoker"
|
2022-11-23 14:44:03 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/policy"
|
2022-04-04 15:36:10 +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"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
execFeeParam = "ExecFeeFactor"
|
|
|
|
storagePriceParam = "StoragePrice"
|
|
|
|
setFeeParam = "FeePerByte"
|
|
|
|
)
|
|
|
|
|
2024-03-11 14:55:50 +00:00
|
|
|
var errInvalidParameterFormat = errors.New("invalid parameter format, must be Parameter=Value")
|
|
|
|
|
2024-02-01 12:55:33 +00:00
|
|
|
func SetPolicyCmd(cmd *cobra.Command, args []string) error {
|
2024-02-02 12:36:14 +00:00
|
|
|
wCtx, err := helper.NewInitializeContext(cmd, viper.GetViper())
|
2022-04-04 15:36:10 +00:00
|
|
|
if err != nil {
|
2024-07-01 15:58:37 +00:00
|
|
|
return fmt.Errorf("can't initialize context: %w", err)
|
2022-04-04 15:36:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bw := io.NewBufBinWriter()
|
|
|
|
for i := range args {
|
2023-02-27 14:19:35 +00:00
|
|
|
k, v, found := strings.Cut(args[i], "=")
|
|
|
|
if !found {
|
2024-03-11 14:55:50 +00:00
|
|
|
return errInvalidParameterFormat
|
2022-04-04 15:36:10 +00:00
|
|
|
}
|
|
|
|
|
2023-02-27 14:19:35 +00:00
|
|
|
switch k {
|
2022-04-04 15:36:10 +00:00
|
|
|
case execFeeParam, storagePriceParam, setFeeParam:
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("parameter must be one of %s, %s and %s", execFeeParam, storagePriceParam, setFeeParam)
|
|
|
|
}
|
|
|
|
|
2023-02-27 14:19:35 +00:00
|
|
|
value, err := strconv.ParseUint(v, 10, 32)
|
2022-04-04 15:36:10 +00:00
|
|
|
if err != nil {
|
2024-05-07 22:08:50 +00:00
|
|
|
return fmt.Errorf("can't parse parameter value '%s': %w", args[i], err)
|
2022-04-04 15:36:10 +00:00
|
|
|
}
|
|
|
|
|
2023-02-27 14:19:35 +00:00
|
|
|
emit.AppCall(bw.BinWriter, policy.Hash, "set"+k, callflag.All, int64(value))
|
2022-04-04 15:36:10 +00:00
|
|
|
}
|
|
|
|
|
2024-02-01 11:30:59 +00:00
|
|
|
if err := wCtx.SendCommitteeTx(bw.Bytes(), false); err != nil {
|
2022-04-04 15:36:10 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-02-01 11:30:59 +00:00
|
|
|
return wCtx.AwaitTx()
|
2022-04-04 15:36:10 +00:00
|
|
|
}
|
2023-11-02 13:20:37 +00:00
|
|
|
|
|
|
|
func dumpPolicyCmd(cmd *cobra.Command, _ []string) error {
|
2024-02-02 12:36:14 +00:00
|
|
|
c, err := helper.GetN3Client(viper.GetViper())
|
2023-11-02 13:20:37 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "can't create N3 client:", err)
|
|
|
|
|
|
|
|
inv := invoker.New(c, nil)
|
|
|
|
policyContract := policy.NewReader(inv)
|
|
|
|
|
|
|
|
execFee, err := policyContract.GetExecFeeFactor()
|
|
|
|
commonCmd.ExitOnErr(cmd, "can't get execution fee factor:", err)
|
|
|
|
|
|
|
|
feePerByte, err := policyContract.GetFeePerByte()
|
|
|
|
commonCmd.ExitOnErr(cmd, "can't get fee per byte:", err)
|
|
|
|
|
|
|
|
storagePrice, err := policyContract.GetStoragePrice()
|
|
|
|
commonCmd.ExitOnErr(cmd, "can't get storage price:", err)
|
|
|
|
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
tw := tabwriter.NewWriter(buf, 0, 2, 2, ' ', 0)
|
|
|
|
|
|
|
|
_, _ = tw.Write([]byte(fmt.Sprintf("Execution Fee Factor:\t%d (int)\n", execFee)))
|
|
|
|
_, _ = tw.Write([]byte(fmt.Sprintf("Fee Per Byte:\t%d (int)\n", feePerByte)))
|
|
|
|
_, _ = tw.Write([]byte(fmt.Sprintf("Storage Price:\t%d (int)\n", storagePrice)))
|
|
|
|
|
|
|
|
_ = tw.Flush()
|
|
|
|
cmd.Print(buf.String())
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|