2022-09-01 07:49:22 +00:00
|
|
|
package morph
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2023-01-27 15:27:49 +00:00
|
|
|
"errors"
|
2022-09-01 07:49:22 +00:00
|
|
|
"fmt"
|
2022-09-01 11:07:34 +00:00
|
|
|
"strings"
|
2022-09-01 07:49:22 +00:00
|
|
|
"text/tabwriter"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-contract/nns"
|
2024-01-31 14:26:26 +00:00
|
|
|
morphUtil "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/modules/morph/util"
|
2023-03-09 12:53:59 +00:00
|
|
|
morphClient "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client"
|
2022-09-01 07:49:22 +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-01 11:07:34 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/unwrap"
|
2024-01-11 11:04:56 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
2022-09-01 07:49:22 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
|
2022-09-01 11:07:34 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
2022-09-01 07:49:22 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/vmstate"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
const lastGlagoliticLetter = 41
|
|
|
|
|
|
|
|
type contractDumpInfo struct {
|
|
|
|
hash util.Uint160
|
|
|
|
name string
|
|
|
|
version string
|
|
|
|
}
|
|
|
|
|
|
|
|
func dumpContractHashes(cmd *cobra.Command, _ []string) error {
|
2024-01-31 14:26:26 +00:00
|
|
|
c, err := morphUtil.GetN3Client(viper.GetViper())
|
2022-09-01 07:49:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't create N3 client: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-10-24 10:06:30 +00:00
|
|
|
r := management.NewReader(invoker.New(c, nil))
|
|
|
|
cs, err := r.GetContractByID(1)
|
2022-09-01 07:49:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-09-01 11:07:34 +00:00
|
|
|
zone, _ := cmd.Flags().GetString(customZoneFlag)
|
|
|
|
if zone != "" {
|
|
|
|
return dumpCustomZoneHashes(cmd, cs.Hash, zone, c)
|
|
|
|
}
|
|
|
|
|
2024-02-01 07:06:05 +00:00
|
|
|
infos := []contractDumpInfo{{name: morphUtil.NNSContract, hash: cs.Hash}}
|
2022-09-01 07:49:22 +00:00
|
|
|
|
|
|
|
irSize := 0
|
|
|
|
for ; irSize < lastGlagoliticLetter; irSize++ {
|
2024-02-01 13:38:20 +00:00
|
|
|
ok, err := morphUtil.NNSIsAvailable(c, cs.Hash, morphUtil.GetAlphabetNNSDomain(irSize))
|
2022-09-01 07:49:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bw := io.NewBufBinWriter()
|
|
|
|
|
|
|
|
if irSize != 0 {
|
|
|
|
bw.Reset()
|
|
|
|
for i := 0; i < irSize; i++ {
|
|
|
|
emit.AppCall(bw.BinWriter, cs.Hash, "resolve", callflag.ReadOnly,
|
2024-02-01 13:38:20 +00:00
|
|
|
morphUtil.GetAlphabetNNSDomain(i),
|
2022-09-01 07:49:22 +00:00
|
|
|
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++ {
|
|
|
|
info := contractDumpInfo{name: fmt.Sprintf("alphabet %d", i)}
|
2024-02-01 06:43:51 +00:00
|
|
|
if h, err := morphUtil.ParseNNSResolveResult(alphaRes.Stack[i]); err == nil {
|
2022-09-01 07:49:22 +00:00
|
|
|
info.hash = h
|
|
|
|
}
|
|
|
|
infos = append(infos, info)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-01 12:21:51 +00:00
|
|
|
for _, ctrName := range morphUtil.ContractList {
|
2022-09-01 07:49:22 +00:00
|
|
|
bw.Reset()
|
|
|
|
emit.AppCall(bw.BinWriter, cs.Hash, "resolve", callflag.ReadOnly,
|
2024-02-01 06:58:12 +00:00
|
|
|
morphUtil.DomainOf(ctrName), int64(nns.TXT))
|
2022-09-01 07:49:22 +00:00
|
|
|
|
|
|
|
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 {
|
2024-02-01 06:43:51 +00:00
|
|
|
if h, err := morphUtil.ParseNNSResolveResult(res.Stack[0]); err == nil {
|
2022-09-01 07:49:22 +00:00
|
|
|
info.hash = h
|
|
|
|
}
|
|
|
|
}
|
|
|
|
infos = append(infos, info)
|
|
|
|
}
|
|
|
|
|
2022-09-01 11:07:34 +00:00
|
|
|
fillContractVersion(cmd, c, infos)
|
|
|
|
printContractInfo(cmd, infos)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-31 14:26:26 +00:00
|
|
|
func dumpCustomZoneHashes(cmd *cobra.Command, nnsHash util.Uint160, zone string, c morphUtil.Client) error {
|
2022-09-01 11:07:34 +00:00
|
|
|
const nnsMaxTokens = 100
|
|
|
|
|
2023-01-27 15:27:49 +00:00
|
|
|
inv := invoker.New(c, nil)
|
2022-09-01 07:49:22 +00:00
|
|
|
|
2022-09-01 11:07:34 +00:00
|
|
|
if !strings.HasPrefix(zone, ".") {
|
|
|
|
zone = "." + zone
|
|
|
|
}
|
|
|
|
|
|
|
|
var infos []contractDumpInfo
|
2023-01-27 15:27:49 +00:00
|
|
|
processItem := func(item stackitem.Item) {
|
|
|
|
bs, err := item.TryBytes()
|
2022-09-01 11:07:34 +00:00
|
|
|
if err != nil {
|
2023-01-27 15:27:49 +00:00
|
|
|
cmd.PrintErrf("Invalid NNS record: %v\n", err)
|
|
|
|
return
|
2022-09-01 07:49:22 +00:00
|
|
|
}
|
2022-09-01 11:07:34 +00:00
|
|
|
|
2023-03-09 12:53:59 +00:00
|
|
|
if !bytes.HasSuffix(bs, []byte(zone)) || bytes.HasPrefix(bs, []byte(morphClient.NNSGroupKeyName)) {
|
2023-01-27 15:27:49 +00:00
|
|
|
// Related https://github.com/nspcc-dev/neofs-contract/issues/316.
|
|
|
|
return
|
2022-09-01 11:07:34 +00:00
|
|
|
}
|
|
|
|
|
2024-02-01 06:43:51 +00:00
|
|
|
h, err := morphUtil.NNSResolveHash(inv, nnsHash, string(bs))
|
2022-09-01 11:07:34 +00:00
|
|
|
if err != nil {
|
2023-01-27 15:27:49 +00:00
|
|
|
cmd.PrintErrf("Could not resolve name %s: %v\n", string(bs), err)
|
|
|
|
return
|
2022-09-01 11:07:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
infos = append(infos, contractDumpInfo{
|
|
|
|
hash: h,
|
|
|
|
name: strings.TrimSuffix(string(bs), zone),
|
|
|
|
})
|
2022-09-01 07:49:22 +00:00
|
|
|
}
|
|
|
|
|
2024-01-11 11:04:56 +00:00
|
|
|
script, err := smartcontract.CreateCallAndPrefetchIteratorScript(nnsHash, "tokens", nnsMaxTokens)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("create prefetch script: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
arr, sessionID, iter, err := unwrap.ArrayAndSessionIterator(inv.Run(script))
|
2023-01-27 15:27:49 +00:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, unwrap.ErrNoSessionID) {
|
|
|
|
items, err := unwrap.Array(inv.CallAndExpandIterator(nnsHash, "tokens", nnsMaxTokens))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't get a list of NNS domains: %w", err)
|
|
|
|
}
|
|
|
|
if len(items) == nnsMaxTokens {
|
|
|
|
cmd.PrintErrln("Provided RPC endpoint doesn't support sessions, some hashes might be lost.")
|
|
|
|
}
|
|
|
|
for i := range items {
|
|
|
|
processItem(items[i])
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2024-01-11 11:04:56 +00:00
|
|
|
for i := range arr {
|
|
|
|
processItem(arr[i])
|
|
|
|
}
|
|
|
|
|
2023-01-27 15:27:49 +00:00
|
|
|
defer func() {
|
|
|
|
_ = inv.TerminateSession(sessionID)
|
|
|
|
}()
|
|
|
|
|
|
|
|
items, err := inv.TraverseIterator(sessionID, &iter, nnsMaxTokens)
|
|
|
|
for err == nil && len(items) != 0 {
|
|
|
|
for i := range items {
|
|
|
|
processItem(items[i])
|
|
|
|
}
|
|
|
|
items, err = inv.TraverseIterator(sessionID, &iter, nnsMaxTokens)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error during NNS domains iteration: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-01 11:07:34 +00:00
|
|
|
fillContractVersion(cmd, c, infos)
|
|
|
|
printContractInfo(cmd, infos)
|
2022-09-01 07:49:22 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2022-09-01 11:07:34 +00:00
|
|
|
|
|
|
|
func printContractInfo(cmd *cobra.Command, infos []contractDumpInfo) {
|
|
|
|
if len(infos) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
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())))
|
|
|
|
}
|
|
|
|
_ = tw.Flush()
|
|
|
|
|
|
|
|
cmd.Print(buf.String())
|
|
|
|
}
|
|
|
|
|
2024-01-31 14:26:26 +00:00
|
|
|
func fillContractVersion(cmd *cobra.Command, c morphUtil.Client, infos []contractDumpInfo) {
|
2022-09-01 11:07:34 +00:00
|
|
|
bw := io.NewBufBinWriter()
|
|
|
|
sub := io.NewBufBinWriter()
|
|
|
|
for i := range infos {
|
|
|
|
if infos[i].hash.Equals(util.Uint160{}) {
|
|
|
|
emit.Int(bw.BinWriter, 0)
|
|
|
|
} else {
|
|
|
|
sub.Reset()
|
|
|
|
emit.AppCall(sub.BinWriter, infos[i].hash, "version", callflag.NoneFlag)
|
|
|
|
if sub.Err != nil {
|
|
|
|
panic(fmt.Errorf("BUG: can't create version script: %w", bw.Err))
|
|
|
|
}
|
|
|
|
|
|
|
|
script := sub.Bytes()
|
|
|
|
emit.Instruction(bw.BinWriter, opcode.TRY, []byte{byte(3 + len(script) + 2), 0})
|
|
|
|
bw.BinWriter.WriteBytes(script)
|
|
|
|
emit.Instruction(bw.BinWriter, opcode.ENDTRY, []byte{2 + 1})
|
|
|
|
emit.Opcodes(bw.BinWriter, opcode.PUSH0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
emit.Opcodes(bw.BinWriter, opcode.NOP) // for the last ENDTRY target
|
|
|
|
if bw.Err != nil {
|
|
|
|
panic(fmt.Errorf("BUG: can't create version script: %w", bw.Err))
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := c.InvokeScript(bw.Bytes(), nil)
|
|
|
|
if err != nil {
|
|
|
|
cmd.Printf("Can't fetch version from NNS: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if res.State == vmstate.Halt.String() {
|
|
|
|
for i := range res.Stack {
|
|
|
|
infos[i].version = parseContractVersion(res.Stack[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|