From 9e56012760787301c0f79d7703c9218e7eb2eed8 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Wed, 28 Jul 2021 17:36:15 +0300 Subject: [PATCH] [#726] neofs-adm: set alphabet contract addresses in NNS Signed-off-by: Evgenii Stratonikov --- cmd/neofs-adm/internal/modules/morph/dump.go | 37 +++++++++++++++++++ .../internal/modules/morph/initialize_nns.go | 37 +++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/cmd/neofs-adm/internal/modules/morph/dump.go b/cmd/neofs-adm/internal/modules/morph/dump.go index 70d6e177..6ca27a78 100644 --- a/cmd/neofs-adm/internal/modules/morph/dump.go +++ b/cmd/neofs-adm/internal/modules/morph/dump.go @@ -14,6 +14,8 @@ import ( "github.com/spf13/viper" ) +const lastGlagoliticLetter = 41 + func dumpContractHashes(cmd *cobra.Command, _ []string) error { c, err := getN3Client(viper.GetViper()) if err != nil { @@ -40,8 +42,43 @@ func dumpContractHashes(cmd *cobra.Command, _ []string) error { return errors.New("invalid response from NNS contract: length mismatch") } + irSize := 0 + for ; irSize < lastGlagoliticLetter; irSize++ { + ok, err := c.NNSIsAvailable(cs.Hash, getAlphabetNNSDomain(irSize)) + if err != nil { + return err + } else if ok { + break + } + } + buf := bytes.NewBuffer(nil) tw := tabwriter.NewWriter(buf, 0, 2, 2, ' ', 0) + + 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++ { + ctrHash := "hash is invalid" + bs, err := alphaRes.Stack[i].TryBytes() + if err == nil { + ctrHash = string(bs) // hashes are stored as hex-encoded LE string + } + + _, _ = tw.Write([]byte(fmt.Sprintf("alphabet %d:\t%s\n", i, ctrHash))) + } + } + for i := range contractList { ctrHash := "hash is invalid" bs, err := res.Stack[i].TryBytes() diff --git a/cmd/neofs-adm/internal/modules/morph/initialize_nns.go b/cmd/neofs-adm/internal/modules/morph/initialize_nns.go index 13f41b17..51ab2f01 100644 --- a/cmd/neofs-adm/internal/modules/morph/initialize_nns.go +++ b/cmd/neofs-adm/internal/modules/morph/initialize_nns.go @@ -2,6 +2,7 @@ package morph import ( "fmt" + "strconv" nns "github.com/nspcc-dev/neo-go/examples/nft-nd-nns" "github.com/nspcc-dev/neo-go/pkg/core/native" @@ -45,6 +46,38 @@ func (c *initializeContext) setNNS() error { } } + alphaCs, err := c.readContract(ctrPath, alphabetContract) + if err != nil { + return fmt.Errorf("can't read alphabet contract: %w", err) + } + for i, w := range c.Wallets { + acc, err := getWalletAccount(w, singleAccountName) + if err != nil { + return err + } + + alphaCs.Hash = state.CreateContractHash(acc.Contract.ScriptHash(), alphaCs.NEF.Checksum, alphaCs.Manifest.Name) + + domain := getAlphabetNNSDomain(i) + if ok, err := c.nnsDomainAvailable(h, domain); err != nil { + return err + } else if !ok { + continue + } + + bw := io.NewBufBinWriter() + emit.AppCall(bw.BinWriter, h, "register", callflag.All, + domain, c.CommitteeAcc.Contract.ScriptHash()) + emit.Opcodes(bw.BinWriter, opcode.ASSERT) + emit.AppCall(bw.BinWriter, h, "setRecord", callflag.All, + domain, int64(nns.TXT), alphaCs.Hash.StringLE()) + + sysFee := int64(defaultRegisterSysfee + native.GASFactor) + if err := c.sendCommitteeTx(bw.Bytes(), sysFee); err != nil { + return err + } + } + for _, ctrName := range contractList { cs, err := c.readContract(ctrPath, ctrName) if err != nil { @@ -98,3 +131,7 @@ func (c *initializeContext) nnsDomainAvailable(nnsHash util.Uint160, domain stri } return true, nil } + +func getAlphabetNNSDomain(i int) string { + return alphabetContract + strconv.FormatUint(uint64(i), 10) + ".neofs" +}