2021-07-28 14:10:02 +00:00
|
|
|
package morph
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
|
2021-10-22 10:31:44 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2021-08-03 12:05:10 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm"
|
2021-07-28 14:10:02 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
|
2021-08-03 12:05:10 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2022-03-27 17:30:08 +00:00
|
|
|
"github.com/nspcc-dev/neofs-contract/nns"
|
2021-07-28 14:10:02 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
2021-07-28 14:36:15 +00:00
|
|
|
const lastGlagoliticLetter = 41
|
|
|
|
|
2022-05-11 18:35:49 +00:00
|
|
|
const forceConfigSet = "force"
|
|
|
|
|
2021-10-22 10:31:44 +00:00
|
|
|
type contractDumpInfo struct {
|
|
|
|
hash util.Uint160
|
|
|
|
name string
|
|
|
|
version string
|
|
|
|
}
|
|
|
|
|
2021-07-28 14:10:02 +00:00
|
|
|
func dumpContractHashes(cmd *cobra.Command, _ []string) error {
|
|
|
|
c, err := getN3Client(viper.GetViper())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't create N3 client: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cs, err := c.GetContractStateByID(1)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-22 10:31:44 +00:00
|
|
|
infos := []contractDumpInfo{{name: nnsContract, hash: cs.Hash}}
|
|
|
|
|
2021-07-28 14:36:15 +00:00
|
|
|
irSize := 0
|
|
|
|
for ; irSize < lastGlagoliticLetter; irSize++ {
|
2022-04-07 08:10:35 +00:00
|
|
|
ok, err := nnsIsAvailable(c, cs.Hash, getAlphabetNNSDomain(irSize))
|
2021-07-28 14:36:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-28 14:10:02 +00:00
|
|
|
buf := bytes.NewBuffer(nil)
|
2021-12-03 09:01:30 +00:00
|
|
|
bw := io.NewBufBinWriter()
|
2021-09-30 14:41:06 +00:00
|
|
|
|
2021-07-28 14:36:15 +00:00
|
|
|
if irSize != 0 {
|
|
|
|
bw.Reset()
|
|
|
|
for i := 0; i < irSize; i++ {
|
|
|
|
emit.AppCall(bw.BinWriter, cs.Hash, "resolve", callflag.ReadOnly,
|
|
|
|
getAlphabetNNSDomain(i),
|
|
|
|
int64(nns.TXT))
|
|
|
|
}
|
|
|
|
|
|
|
|
alphaRes, err := c.InvokeScript(bw.Bytes(), nil)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't fetch info from NNS: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < irSize; i++ {
|
2021-10-22 10:31:44 +00:00
|
|
|
info := contractDumpInfo{name: fmt.Sprintf("alphabet %d", i)}
|
2021-10-07 13:14:52 +00:00
|
|
|
if h, err := parseNNSResolveResult(alphaRes.Stack[i]); err == nil {
|
2021-10-22 10:31:44 +00:00
|
|
|
info.hash = h
|
2021-07-28 14:36:15 +00:00
|
|
|
}
|
2021-10-22 10:31:44 +00:00
|
|
|
infos = append(infos, info)
|
2021-07-28 14:36:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-03 09:01:30 +00:00
|
|
|
for _, ctrName := range contractList {
|
|
|
|
bw.Reset()
|
|
|
|
emit.AppCall(bw.BinWriter, cs.Hash, "resolve", callflag.ReadOnly,
|
|
|
|
ctrName+".neofs", int64(nns.TXT))
|
|
|
|
|
|
|
|
res, err := c.InvokeScript(bw.Bytes(), nil)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't fetch info from NNS: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
info := contractDumpInfo{name: ctrName}
|
|
|
|
if len(res.Stack) != 0 {
|
|
|
|
if h, err := parseNNSResolveResult(res.Stack[0]); err == nil {
|
|
|
|
info.hash = h
|
|
|
|
}
|
2021-07-28 14:10:02 +00:00
|
|
|
}
|
2021-10-22 10:31:44 +00:00
|
|
|
infos = append(infos, info)
|
|
|
|
}
|
2021-07-28 14:10:02 +00:00
|
|
|
|
2021-10-22 10:31:44 +00:00
|
|
|
bw.Reset()
|
|
|
|
for i := range infos {
|
2021-12-07 12:50:11 +00:00
|
|
|
if infos[i].hash.Equals(util.Uint160{}) {
|
2021-10-22 10:31:44 +00:00
|
|
|
emit.Int(bw.BinWriter, 0)
|
|
|
|
} else {
|
|
|
|
emit.AppCall(bw.BinWriter, infos[i].hash, "version", callflag.NoneFlag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-03 09:01:30 +00:00
|
|
|
res, err := c.InvokeScript(bw.Bytes(), nil)
|
2021-10-22 10:31:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't fetch info from NNS: %w", err)
|
2021-07-28 14:10:02 +00:00
|
|
|
}
|
|
|
|
|
2021-10-22 10:31:44 +00:00
|
|
|
if res.State == vm.HaltState.String() {
|
|
|
|
for i := range res.Stack {
|
|
|
|
infos[i].version = parseContractVersion(res.Stack[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tw := tabwriter.NewWriter(buf, 0, 2, 2, ' ', 0)
|
|
|
|
for _, info := range infos {
|
|
|
|
if info.version == "" {
|
|
|
|
info.version = "unknown"
|
|
|
|
}
|
|
|
|
_, _ = tw.Write([]byte(fmt.Sprintf("%s\t(%s):\t%s\n",
|
|
|
|
info.name, info.version, info.hash.StringLE())))
|
|
|
|
}
|
2021-07-28 14:10:02 +00:00
|
|
|
_ = tw.Flush()
|
2021-10-22 10:31:44 +00:00
|
|
|
|
2021-07-28 14:10:02 +00:00
|
|
|
cmd.Print(buf.String())
|
|
|
|
|
|
|
|
return nil
|
2021-10-22 10:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func parseContractVersion(item stackitem.Item) string {
|
|
|
|
bi, err := item.TryInteger()
|
|
|
|
if err != nil || bi.Sign() == 0 || !bi.IsInt64() {
|
|
|
|
return "unknown"
|
|
|
|
}
|
|
|
|
|
|
|
|
v := bi.Int64()
|
|
|
|
major := v / 1_000_000
|
|
|
|
minor := (v % 1_000_000) / 1000
|
|
|
|
patch := v % 1_000
|
|
|
|
return fmt.Sprintf("v%d.%d.%d", major, minor, patch)
|
2021-07-28 14:10:02 +00:00
|
|
|
}
|
2021-08-03 12:05:10 +00:00
|
|
|
|
|
|
|
func dumpNetworkConfig(cmd *cobra.Command, _ []string) error {
|
2021-10-18 12:14:32 +00:00
|
|
|
c, err := 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
|
|
|
}
|
|
|
|
|
2021-10-18 12:14:32 +00:00
|
|
|
cs, err := c.GetContractStateByID(1)
|
2021-08-03 12:05:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't get NNS contract info: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-10-18 12:14:32 +00:00
|
|
|
nmHash, err := nnsResolveHash(c, cs.Hash, netmapContract+".neofs")
|
2021-08-03 12:05:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't get netmap contract hash: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-04-07 12:47:13 +00:00
|
|
|
res, err := invokeFunction(c, nmHash, "listConfig", nil, nil)
|
2021-08-03 12:05:10 +00:00
|
|
|
if err != nil || res.State != vm.HaltState.String() || len(res.Stack) == 0 {
|
|
|
|
return errors.New("can't fetch list of network config keys from the netmap contract")
|
|
|
|
}
|
|
|
|
|
|
|
|
arr, ok := res.Stack[0].Value().([]stackitem.Item)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("invalid ListConfig response from netmap contract")
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
tw := tabwriter.NewWriter(buf, 0, 2, 2, ' ', 0)
|
|
|
|
|
|
|
|
for _, param := range arr {
|
|
|
|
tuple, ok := param.Value().([]stackitem.Item)
|
|
|
|
if !ok || len(tuple) != 2 {
|
|
|
|
return errors.New("invalid ListConfig response from netmap contract")
|
|
|
|
}
|
|
|
|
|
|
|
|
k, err := tuple[0].TryBytes()
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("invalid config key from netmap contract")
|
|
|
|
}
|
|
|
|
|
|
|
|
v, err := tuple[1].TryBytes()
|
|
|
|
if err != nil {
|
2022-05-06 20:33:50 +00:00
|
|
|
return invalidConfigValueErr(k)
|
2021-08-03 12:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch string(k) {
|
2021-08-03 17:57:47 +00:00
|
|
|
case netmapAuditFeeKey, netmapBasicIncomeRateKey,
|
2021-10-18 12:01:38 +00:00
|
|
|
netmapContainerFeeKey, netmapContainerAliasFeeKey,
|
|
|
|
netmapEigenTrustIterationsKey,
|
2021-08-03 17:57:47 +00:00
|
|
|
netmapEpochKey, netmapInnerRingCandidateFeeKey,
|
|
|
|
netmapMaxObjectSizeKey, netmapWithdrawFeeKey:
|
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)))
|
2021-08-03 17:57:47 +00:00
|
|
|
case netmapEigenTrustAlphaKey:
|
2021-08-03 12:05:10 +00:00
|
|
|
_, _ = tw.Write([]byte(fmt.Sprintf("%s:\t%s (str)\n", k, v)))
|
2022-05-06 20:33:50 +00:00
|
|
|
case netmapHomomorphicHashDisabledKey:
|
|
|
|
vBool, err := tuple[1].TryBool()
|
|
|
|
if err != nil {
|
|
|
|
return invalidConfigValueErr(k)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _ = tw.Write([]byte(fmt.Sprintf("%s:\t%t (bool)\n", k, vBool)))
|
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
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
forceFlag, _ := cmd.Flags().GetBool(forceConfigSet)
|
|
|
|
|
|
|
|
bw := io.NewBufBinWriter()
|
|
|
|
for _, arg := range args {
|
|
|
|
k, v, err := parseConfigPair(arg, forceFlag)
|
|
|
|
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, force bool) (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
|
2022-05-06 20:33:50 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
2022-05-06 20:33:50 +00:00
|
|
|
|
|
|
|
func invalidConfigValueErr(key []byte) error {
|
|
|
|
return fmt.Errorf("invalid %s config value from netmap contract", key)
|
|
|
|
}
|