[#1727] neofs-adm: Allow to register custom TLD for contracts

Signed-off-by: Evgenii Stratonikov <evgeniy@morphbits.ru>
This commit is contained in:
Evgenii Stratonikov 2022-08-24 14:25:02 +03:00 committed by fyrchik
parent ddbe129cb2
commit 61f0d85834
3 changed files with 71 additions and 24 deletions

View file

@ -14,6 +14,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag" "github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
"github.com/nspcc-dev/neo-go/pkg/vm/emit" "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/opcode"
"github.com/nspcc-dev/neofs-contract/nns"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@ -109,13 +110,53 @@ func deployContractCmd(cmd *cobra.Command, args []string) error {
emit.AppCallNoArgs(w.BinWriter, callHash, method, callflag.All) emit.AppCallNoArgs(w.BinWriter, callHash, method, callflag.All)
emit.Opcodes(w.BinWriter, opcode.DROP) // contract state on stack emit.Opcodes(w.BinWriter, opcode.DROP) // contract state on stack
if !isUpdate { if !isUpdate {
s, err := c.nnsRegisterDomainScript(nnsCs.Hash, cs.Hash, domain) bw := io.NewBufBinWriter()
emit.Instruction(bw.BinWriter, opcode.INITSSLOT, []byte{1})
emit.AppCall(bw.BinWriter, nnsCs.Hash, "getPrice", callflag.All)
emit.Opcodes(bw.BinWriter, opcode.STSFLD0)
emit.AppCall(bw.BinWriter, nnsCs.Hash, "setPrice", callflag.All, 1)
start := bw.Len()
newRecord := false
ok, err := c.nnsRootRegistered(nnsCs.Hash, zone)
if err != nil {
return err
} else if !ok {
newRecord = true
emit.AppCall(bw.BinWriter, nnsCs.Hash, "register", callflag.All,
zone, c.CommitteeAcc.Contract.ScriptHash(),
"ops@nspcc.ru", int64(3600), int64(600), int64(defaultExpirationTime), int64(3600))
emit.Opcodes(bw.BinWriter, opcode.ASSERT)
emit.AppCall(bw.BinWriter, nnsCs.Hash, "register", callflag.All,
domain, c.CommitteeAcc.Contract.ScriptHash(),
"ops@nspcc.ru", int64(3600), int64(600), int64(defaultExpirationTime), int64(3600))
emit.Opcodes(bw.BinWriter, opcode.ASSERT)
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)
if err != nil { if err != nil {
return err return err
} }
if len(s) != 0 { if len(s) != 0 {
newRecord = true
bw.WriteBytes(s)
}
}
if bw.Err != nil {
panic(fmt.Errorf("BUG: can't create deployment script: %w", w.Err))
} else if bw.Len() != start {
w.WriteBytes(bw.Bytes())
emit.Opcodes(w.BinWriter, opcode.LDSFLD0, opcode.PUSH1, opcode.PACK)
emit.AppCallNoArgs(w.BinWriter, nnsCs.Hash, "setPrice", callflag.All)
if newRecord {
c.Command.Printf("NNS: Set %s -> %s\n", domain, cs.Hash.StringLE()) c.Command.Printf("NNS: Set %s -> %s\n", domain, cs.Hash.StringLE())
w.WriteBytes(s) }
} }
} }

View file

@ -263,7 +263,7 @@ func (c *initializeContext) updateContracts() error {
if method == deployMethodName { if method == deployMethodName {
// same actions are done in initializeContext.setNNS, can be unified // same actions are done in initializeContext.setNNS, can be unified
domain := ctrName + ".neofs" domain := ctrName + ".neofs"
script, err := c.nnsRegisterDomainScript(nnsHash, cs.Hash, domain) script, err := c.nnsRegisterDomainScript(nnsHash, cs.Hash, domain, true)
if err != nil { if err != nil {
return err return err
} }

View file

@ -4,6 +4,7 @@ import (
"encoding/hex" "encoding/hex"
"errors" "errors"
"fmt" "fmt"
"math/big"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@ -33,7 +34,7 @@ func (c *initializeContext) setNNS() error {
return err return err
} }
ok, err := c.nnsRootRegistered(nnsCs.Hash) ok, err := c.nnsRootRegistered(nnsCs.Hash, "neofs")
if err != nil { if err != nil {
return err return err
} else if !ok { } else if !ok {
@ -125,26 +126,29 @@ func getAlphabetNNSDomain(i int) string {
return alphabetContract + strconv.FormatUint(uint64(i), 10) + ".neofs" return alphabetContract + strconv.FormatUint(uint64(i), 10) + ".neofs"
} }
func (c *initializeContext) nnsRegisterDomainScript(nnsHash, expectedHash util.Uint160, domain string) ([]byte, error) { func (c *initializeContext) nnsRegisterDomainScript(nnsHash, expectedHash util.Uint160, domain string, setPrice bool) ([]byte, error) {
ok, err := nnsIsAvailable(c.Client, nnsHash, domain) ok, err := nnsIsAvailable(c.Client, nnsHash, domain)
if err != nil { if err != nil {
return nil, err return nil, err
} }
bw := io.NewBufBinWriter()
if ok {
var price *big.Int
if setPrice {
res, err := invokeFunction(c.Client, nnsHash, "getPrice", nil, nil) res, err := invokeFunction(c.Client, nnsHash, "getPrice", nil, nil)
if err != nil || res.State != vmstate.Halt.String() || len(res.Stack) == 0 { if err != nil || res.State != vmstate.Halt.String() || len(res.Stack) == 0 {
return nil, errors.New("could not get NNS's price") return nil, errors.New("could not get NNS's price")
} }
price, err := res.Stack[0].TryInteger() price, err = res.Stack[0].TryInteger()
if err != nil { if err != nil {
return nil, fmt.Errorf("unexpected `GetPrice` stack returned: %w", err) return nil, fmt.Errorf("unexpected `GetPrice` stack returned: %w", err)
} }
bw := io.NewBufBinWriter()
if ok {
// set minimal registration price // set minimal registration price
emit.AppCall(bw.BinWriter, nnsHash, "setPrice", callflag.All, 1) emit.AppCall(bw.BinWriter, nnsHash, "setPrice", callflag.All, 1)
}
// register domain // register domain
emit.AppCall(bw.BinWriter, nnsHash, "register", callflag.All, emit.AppCall(bw.BinWriter, nnsHash, "register", callflag.All,
@ -152,8 +156,10 @@ func (c *initializeContext) nnsRegisterDomainScript(nnsHash, expectedHash util.U
"ops@nspcc.ru", int64(3600), int64(600), int64(defaultExpirationTime), int64(3600)) "ops@nspcc.ru", int64(3600), int64(600), int64(defaultExpirationTime), int64(3600))
emit.Opcodes(bw.BinWriter, opcode.ASSERT) emit.Opcodes(bw.BinWriter, opcode.ASSERT)
if setPrice {
// set registration price back // set registration price back
emit.AppCall(bw.BinWriter, nnsHash, "setPrice", callflag.All, price) emit.AppCall(bw.BinWriter, nnsHash, "setPrice", callflag.All, price)
}
} else { } else {
s, err := nnsResolveHash(c.Client, nnsHash, domain) s, err := nnsResolveHash(c.Client, nnsHash, domain)
if err != nil { if err != nil {
@ -174,7 +180,7 @@ func (c *initializeContext) nnsRegisterDomainScript(nnsHash, expectedHash util.U
} }
func (c *initializeContext) nnsRegisterDomain(nnsHash, expectedHash util.Uint160, domain string) error { func (c *initializeContext) nnsRegisterDomain(nnsHash, expectedHash util.Uint160, domain string) error {
script, err := c.nnsRegisterDomainScript(nnsHash, expectedHash, domain) script, err := c.nnsRegisterDomainScript(nnsHash, expectedHash, domain, true)
if script == nil { if script == nil {
return err return err
} }
@ -182,8 +188,8 @@ func (c *initializeContext) nnsRegisterDomain(nnsHash, expectedHash util.Uint160
return c.sendCommitteeTx(script, sysFee, true) return c.sendCommitteeTx(script, sysFee, true)
} }
func (c *initializeContext) nnsRootRegistered(nnsHash util.Uint160) (bool, error) { func (c *initializeContext) nnsRootRegistered(nnsHash util.Uint160, zone string) (bool, error) {
params := []interface{}{"name.neofs"} params := []interface{}{"name." + zone}
res, err := invokeFunction(c.Client, nnsHash, "isAvailable", params, nil) res, err := invokeFunction(c.Client, nnsHash, "isAvailable", params, nil)
if err != nil { if err != nil {
return false, err return false, err