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"
|
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-01-31 14:26:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/modules/morph/util"
|
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-02-01 12:55:33 +00:00
|
|
|
func SetPolicyCmd(cmd *cobra.Command, args []string) error {
|
2024-02-01 12:21:51 +00:00
|
|
|
wCtx, err := util.NewInitializeContext(cmd, viper.GetViper())
|
2022-04-04 15:36:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't to initialize context: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
bw := io.NewBufBinWriter()
|
|
|
|
for i := range args {
|
2023-02-27 14:19:35 +00:00
|
|
|
k, v, found := strings.Cut(args[i], "=")
|
|
|
|
if !found {
|
2022-04-04 15:36:10 +00:00
|
|
|
return fmt.Errorf("invalid parameter format, must be Parameter=Value")
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
return fmt.Errorf("can't parse parameter value '%s': %w", args[1], err)
|
|
|
|
}
|
|
|
|
|
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-01-31 14:26:26 +00:00
|
|
|
c, err := util.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
|
|
|
|
}
|