From 9290a7e1fef3a198935d15206960c3b66fc75870 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Fri, 2 Sep 2022 10:59:09 +0300 Subject: [PATCH] [#1748] neofs-adm: Remove `addRecord` from `nnsRegisterDomainScript` Signed-off-by: Evgenii Stratonikov --- .../internal/modules/morph/deploy.go | 10 +++-- .../modules/morph/initialize_deploy.go | 13 ++++-- .../internal/modules/morph/initialize_nns.go | 42 +++++++++---------- 3 files changed, 37 insertions(+), 28 deletions(-) diff --git a/cmd/neofs-adm/internal/modules/morph/deploy.go b/cmd/neofs-adm/internal/modules/morph/deploy.go index 8a394227..f5fed24d 100644 --- a/cmd/neofs-adm/internal/modules/morph/deploy.go +++ b/cmd/neofs-adm/internal/modules/morph/deploy.go @@ -138,13 +138,17 @@ func deployContractCmd(cmd *cobra.Command, args []string) error { emit.AppCall(bw.BinWriter, nnsCs.Hash, "addRecord", callflag.All, domain, int64(nns.TXT), cs.Hash.StringLE()) } else { - s, err := c.nnsRegisterDomainScript(nnsCs.Hash, cs.Hash, domain, false) + s, ok, err := c.nnsRegisterDomainScript(nnsCs.Hash, cs.Hash, domain, false) if err != nil { return err } - if len(s) != 0 { + if !ok { newRecord = true - bw.WriteBytes(s) + if len(s) != 0 { + bw.WriteBytes(s) + } + emit.AppCall(w.BinWriter, nnsCs.Hash, "addRecord", callflag.All, + domain, int64(nns.TXT), cs.Hash.StringLE()) } } if bw.Err != nil { diff --git a/cmd/neofs-adm/internal/modules/morph/initialize_deploy.go b/cmd/neofs-adm/internal/modules/morph/initialize_deploy.go index df88989d..56787b3c 100644 --- a/cmd/neofs-adm/internal/modules/morph/initialize_deploy.go +++ b/cmd/neofs-adm/internal/modules/morph/initialize_deploy.go @@ -26,6 +26,7 @@ import ( "github.com/nspcc-dev/neo-go/pkg/vm/emit" "github.com/nspcc-dev/neo-go/pkg/vm/opcode" "github.com/nspcc-dev/neo-go/pkg/vm/vmstate" + "github.com/nspcc-dev/neofs-contract/nns" "github.com/nspcc-dev/neofs-node/pkg/innerring" morphClient "github.com/nspcc-dev/neofs-node/pkg/morph/client" "github.com/spf13/viper" @@ -263,13 +264,17 @@ func (c *initializeContext) updateContracts() error { if method == deployMethodName { // same actions are done in initializeContext.setNNS, can be unified domain := ctrName + ".neofs" - script, err := c.nnsRegisterDomainScript(nnsHash, cs.Hash, domain, true) + script, ok, err := c.nnsRegisterDomainScript(nnsHash, cs.Hash, domain, true) if err != nil { return err } - if script != nil { - totalGasCost += defaultRegisterSysfee + native.GASFactor - w.WriteBytes(script) + if !ok { + if script != nil { + totalGasCost += defaultRegisterSysfee + native.GASFactor + w.WriteBytes(script) + } + emit.AppCall(w.BinWriter, nnsHash, "addRecord", callflag.All, + domain, int64(nns.TXT), cs.Hash.StringLE()) } c.Command.Printf("NNS: Set %s -> %s\n", domain, cs.Hash.StringLE()) } diff --git a/cmd/neofs-adm/internal/modules/morph/initialize_nns.go b/cmd/neofs-adm/internal/modules/morph/initialize_nns.go index 8b588ec4..cddd15ed 100644 --- a/cmd/neofs-adm/internal/modules/morph/initialize_nns.go +++ b/cmd/neofs-adm/internal/modules/morph/initialize_nns.go @@ -126,24 +126,24 @@ func getAlphabetNNSDomain(i int) string { return alphabetContract + strconv.FormatUint(uint64(i), 10) + ".neofs" } -func (c *initializeContext) nnsRegisterDomainScript(nnsHash, expectedHash util.Uint160, domain string, setPrice bool) ([]byte, error) { +func (c *initializeContext) nnsRegisterDomainScript(nnsHash, expectedHash util.Uint160, domain string, setPrice bool) ([]byte, bool, error) { ok, err := nnsIsAvailable(c.Client, nnsHash, domain) if err != nil { - return nil, err + return nil, false, err } - bw := io.NewBufBinWriter() if ok { + bw := io.NewBufBinWriter() var price *big.Int if setPrice { res, err := invokeFunction(c.Client, nnsHash, "getPrice", nil, nil) if err != nil || res.State != vmstate.Halt.String() || len(res.Stack) == 0 { - return nil, errors.New("could not get NNS's price") + return nil, false, errors.New("could not get NNS's price") } price, err = res.Stack[0].TryInteger() if err != nil { - return nil, fmt.Errorf("unexpected `GetPrice` stack returned: %w", err) + return nil, false, fmt.Errorf("unexpected `GetPrice` stack returned: %w", err) } // set minimal registration price @@ -160,32 +160,32 @@ func (c *initializeContext) nnsRegisterDomainScript(nnsHash, expectedHash util.U // set registration price back emit.AppCall(bw.BinWriter, nnsHash, "setPrice", callflag.All, price) } - } else { - s, err := nnsResolveHash(c.Client, nnsHash, domain) - if err != nil { - return nil, err - } - if s == expectedHash { - return nil, nil + + if bw.Err != nil { + panic(bw.Err) } + return bw.Bytes(), false, nil } - emit.AppCall(bw.BinWriter, nnsHash, "addRecord", callflag.All, - domain, int64(nns.TXT), expectedHash.StringLE()) - - if bw.Err != nil { - panic(bw.Err) + s, err := nnsResolveHash(c.Client, nnsHash, domain) + if err != nil { + return nil, false, err } - return bw.Bytes(), nil + return nil, s == expectedHash, nil } func (c *initializeContext) nnsRegisterDomain(nnsHash, expectedHash util.Uint160, domain string) error { - script, err := c.nnsRegisterDomainScript(nnsHash, expectedHash, domain, true) - if script == nil { + script, ok, err := c.nnsRegisterDomainScript(nnsHash, expectedHash, domain, true) + if ok || err != nil { return err } + + w := io.NewBufBinWriter() + w.WriteBytes(script) + emit.AppCall(w.BinWriter, nnsHash, "addRecord", callflag.All, + domain, int64(nns.TXT), expectedHash.StringLE()) sysFee := int64(defaultRegisterSysfee + native.GASFactor) - return c.sendCommitteeTx(script, sysFee, true) + return c.sendCommitteeTx(w.Bytes(), sysFee, true) } func (c *initializeContext) nnsRootRegistered(nnsHash util.Uint160, zone string) (bool, error) {