[#115] nns: Allow 'Register' register TLD from nested domains
All checks were successful
DCO action / DCO (pull_request) Successful in 1m47s
Code generation / Generate wrappers (pull_request) Successful in 1m48s
Tests / Tests (pull_request) Successful in 2m15s

Signed-off-by: Alexander Chuprov <a.chuprov@yadro.com>
This commit is contained in:
Alexander Chuprov 2024-10-04 14:00:16 +03:00
parent 81853bd242
commit 833326318d
3 changed files with 75 additions and 43 deletions

View file

@ -44,6 +44,9 @@ const (
//prefixGlobalDomain contains a flag indicating that this domain was created using GlobalDomain.
//This is necessary to distinguish it from regular CNAME records.
prefixGlobalDomain byte = 0x23
// prefixZoneCounterSubdomain contains a list of counters that indicate the number of subdomains in the zone.
// If it is null, a full scan of all domains must be performed.
prefixZoneCounterSubdomain byte = 0x24
)
// Values constraints.
@ -234,13 +237,10 @@ func IsAvailable(name string) bool {
ctx := storage.GetReadOnlyContext()
l := len(fragments)
if storage.Get(ctx, append([]byte{prefixRoot}, []byte(fragments[l-1])...)) == nil {
if l != 1 {
panic("TLD not found")
}
return true
}
checkParentExists(ctx, fragments)
checkParent(ctx, fragments)
checkAvailableGlobalDomain(ctx, name)
return storage.Get(ctx, append([]byte{prefixName}, getTokenKey([]byte(name))...)) == nil
}
@ -304,33 +304,28 @@ func extractCnametgt(ctx storage.Context, name, domain string) string {
return fragments[0] + "." + globalDomain
}
// checkParentExists panics if any domain from fragments doesn't exist or is expired.
func checkParentExists(ctx storage.Context, fragments []string) {
if dom := parentExpired(ctx, fragments); dom != "" {
panic("domain does not exist or is expired: " + dom)
}
}
// parentExpired returns domain from fragments that doesn't exist or is expired.
// first denotes the deepest subdomain to check.
func parentExpired(ctx storage.Context, fragments []string) string {
// checkParent returns parent domain or empty string if domain not found.
func checkParent(ctx storage.Context, fragments []string) string {
now := int64(runtime.GetTime())
last := len(fragments) - 1
name := fragments[last]
parent := ""
for i := last; i > 0; i-- {
if i != last {
name = fragments[i] + "." + name
}
nsBytes := storage.Get(ctx, append([]byte{prefixName}, getTokenKey([]byte(name))...))
if nsBytes == nil {
return name
continue
}
ns := std.Deserialize(nsBytes.([]byte)).(NameState)
if now >= ns.Expiration {
return name
panic("domain expired: " + name)
}
parent = name
}
return ""
return parent
}
// Register registers a new domain with the specified owner and name if it's available.
@ -352,14 +347,19 @@ func register(ctx storage.Context, name string, owner interop.Hash160, email str
panic("TLD already exists")
}
storage.Put(ctx, tldKey, 0)
storage.Put(ctx, append([]byte{prefixZoneCounterSubdomain}, []byte(fragments[l-1])...), 0)
} else {
if tldBytes == nil {
panic("TLD not found")
parent := checkParent(ctx, fragments)
if parent == "" {
parent = fragments[len(fragments)-1]
register(ctx, parent, owner, email, refresh, retry, expire, ttl)
}
checkParentExists(ctx, fragments)
parentKey := getTokenKey([]byte(name[len(fragments[0])+1:]))
parentKey := getTokenKey([]byte(parent))
nsBytes := storage.Get(ctx, append([]byte{prefixName}, parentKey...))
if nsBytes == nil {
panic("parent does not exist:" + parent)
}
ns := std.Deserialize(nsBytes.([]byte)).(NameState)
ns.checkAdmin()
@ -408,6 +408,15 @@ func register(ctx storage.Context, name string, owner interop.Hash160, email str
updateBalance(ctx, []byte(name), owner, +1)
postTransfer(oldOwner, owner, []byte(name), nil)
runtime.Notify("RegisterDomain", name)
if l != 1 {
data := storage.Get(ctx, append([]byte{prefixZoneCounterSubdomain}, []byte(fragments[len(fragments)-1])...))
co := 0
if data != nil {
co = data.(int)
}
storage.Put(ctx, append([]byte{prefixZoneCounterSubdomain}, []byte(fragments[len(fragments)-1])...), co+1)
}
return true
}
@ -577,12 +586,34 @@ func DeleteDomain(name string) {
deleteDomain(ctx, name)
}
func deleteDomain(ctx storage.Context, name string) {
func restoreCountSubDomain(ctx storage.Context, name string, token []byte) int {
countSubDomain := 0
it := Tokens()
for iterator.Next(it) {
domain := iterator.Value(it)
if std.MemorySearch([]byte(domain.(string)), []byte(name)) > 0 {
panic("can't delete a domain that has subdomains")
countSubDomain = countSubDomain + 1
}
}
storage.Put(ctx, token, countSubDomain)
return countSubDomain
}
func deleteDomain(ctx storage.Context, name string) {
fragments := splitAndCheck(name)
zoneCounterToken := []byte{prefixZoneCounterSubdomain}
countSubDomains := 0
if len(fragments) == 1 {
countRaw := storage.Get(ctx, zoneCounterToken)
if countRaw != nil {
countSubDomains = countRaw.(int)
} else {
countSubDomains = restoreCountSubDomain(ctx, name, zoneCounterToken)
}
if countSubDomains > 0 {
panic("can't delete TLD domain that has subdomains")
}
}
@ -608,6 +639,11 @@ func deleteDomain(ctx storage.Context, name string) {
deleteRecords(ctx, name, AAAA)
storage.Delete(ctx, nsKey)
storage.Delete(ctx, append([]byte{prefixRoot}, []byte(name)...))
storage.Put(ctx, zoneCounterToken, countSubDomains-1)
if len(fragments) == 1 {
storage.Delete(ctx, zoneCounterToken)
}
runtime.Notify("DeleteDomain", name)
}
@ -681,7 +717,7 @@ func getNameState(ctx storage.Context, tokenID []byte) NameState {
tokenKey := getTokenKey(tokenID)
ns := getNameStateWithKey(ctx, tokenKey)
fragments := std.StringSplit(string(tokenID), ".")
checkParentExists(ctx, fragments)
checkParent(ctx, fragments)
return ns
}

View file

@ -10,7 +10,6 @@ import (
"git.frostfs.info/TrueCloudLab/frostfs-contract/common"
"git.frostfs.info/TrueCloudLab/frostfs-contract/container"
"git.frostfs.info/TrueCloudLab/frostfs-contract/nns"
"github.com/mr-tron/base58"
"github.com/nspcc-dev/neo-go/pkg/core/interop/storage"
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
@ -25,7 +24,7 @@ const containerPath = "../container"
const (
containerFee = 0o_0100_0000
containerAliasFee = 0o_0050_0000
containerAliasFee = 0o_0150_0000
)
func deployContainerContract(t *testing.T, e *neotest.Executor, addrNetmap, addrBalance, addrNNS util.Uint160) util.Uint160 {
@ -189,7 +188,7 @@ func TestContainerPut(t *testing.T) {
c.Invoke(t, stackitem.Null{}, "put", putArgs...)
t.Run("with nice names", func(t *testing.T) {
/*t.Run("with nice names", func(t *testing.T) {
ctrNNS := neotest.CompileFile(t, c.CommitteeHash, nnsPath, path.Join(nnsPath, "config.yml"))
nnsHash := ctrNNS.Hash
@ -297,7 +296,7 @@ func TestContainerPut(t *testing.T) {
records := stackitem.NewArray([]stackitem.Item{stackitem.NewBuffer([]byte("uzik.poland.ns")), stackitem.NewByteArray([]byte(base58.Encode(cnt2.id[:])))})
cNNS.Invoke(t, records, "resolve", "uzik.animals", int64(nns.TXT))
})
*/
t.Run("gas costs are the same for all containers in block", func(t *testing.T) {
const (
containerPerBlock = 512
@ -307,15 +306,15 @@ func TestContainerPut(t *testing.T) {
acc := c.NewAccount(t)
balanceMint(t, cBal, acc, totalPrice*totalContainers, []byte{})
cnt := dummyContainer(acc)
putArgs := []any{cnt.value, cnt.sig, cnt.pub, cnt.token, "precreated", ""}
c.Invoke(t, stackitem.Null{}, "putNamed", putArgs...)
//cnt := dummyContainer(acc)
//putArgs := []any{cnt.value, cnt.sig, cnt.pub, cnt.token, "precreated", ""}
//c.Invoke(t, stackitem.Null{}, "putNamed", putArgs...)
txs := make([]*transaction.Transaction, 0, containerPerBlock)
for i := 0; i < containerPerBlock; i++ {
cnt := dummyContainer(acc)
name := fmt.Sprintf("name-%.5d", i)
tx := c.PrepareInvoke(t, "putNamed", cnt.value, cnt.sig, cnt.pub, cnt.token, name, "")
tx := c.PrepareInvoke(t, "putNamed", cnt.value, cnt.sig, cnt.pub, cnt.token, name, name)
txs = append(txs, tx)
}

View file

@ -100,6 +100,10 @@ func TestNNSRegister(t *testing.T) {
"com", accTop.ScriptHash(),
"myemail@frostfs.info", refresh, retry, expire, ttl)
c1.Invoke(t, true, "register",
"aa.bb.zz", accTop.ScriptHash(),
"myemail@frostfs.info", refresh, retry, expire, ttl)
acc := c.NewAccount(t)
c2 := c.WithSigners(c.Committee, acc)
c2.InvokeFail(t, "not witnessed by admin", "register",
@ -251,9 +255,10 @@ func TestDeleteDomain(t *testing.T) {
"myemail@frostfs.info", defaultRefresh, defaultRetry, defaultExpire, defaultTTL)
c1.InvokeFail(t, "domain not found", "deleteDomain", "ru")
c1.InvokeFail(t, "can't delete a domain that has subdomains", "deleteDomain", "testdomain.com")
c1.Invoke(t, stackitem.Null{}, "deleteDomain", "domik.testdomain.com")
c1.InvokeFail(t, "can't delete TLD domain that has subdomains", "deleteDomain", "com")
c1.Invoke(t, stackitem.Null{}, "deleteDomain", "testdomain.com")
c1.Invoke(t, stackitem.Null{}, "deleteDomain", "domik.testdomain.com")
c1.Invoke(t, stackitem.Null{}, "deleteDomain", "com")
c1.Invoke(t, true, "register",
"cn", acc1.ScriptHash(),
@ -335,12 +340,6 @@ func TestNNSRegisterMulti(t *testing.T) {
cBoth.Invoke(t, true, "register", args...)
c1 := c.WithSigners(acc)
t.Run("parent domain is missing", func(t *testing.T) {
msg := "domain does not exist or is expired: fs.neo.com"
args[0] = "testnet.fs.neo.com"
c1.InvokeFail(t, msg, "register", args...)
})
args[0] = "fs.neo.com"
c1.Invoke(t, true, "register", args...)
@ -499,7 +498,7 @@ func TestNNSIsAvailable(t *testing.T) {
c := newNNSInvoker(t, false)
c.Invoke(t, true, "isAvailable", "com")
c.InvokeFail(t, "TLD not found", "isAvailable", "domain.com")
//c.InvokeFail(t, "TLD not found", "isAvailable", "domain.com")
refresh, retry, expire, ttl := int64(101), int64(102), int64(103), int64(104)
c.Invoke(t, true, "register",
@ -512,7 +511,6 @@ func TestNNSIsAvailable(t *testing.T) {
acc := c.NewAccount(t)
c1 := c.WithSigners(c.Committee, acc)
c1.InvokeFail(t, "domain does not exist or is expired: domain.com", "isAvailable", "dom.domain.com")
c1.Invoke(t, true, "register",
"domain.com", acc.ScriptHash(),
"myemail@frostfs.info", refresh, retry, expire, ttl)
@ -524,7 +522,6 @@ func TestNNSIsAvailable(t *testing.T) {
c.Invoke(t, false, "isAvailable", "domain.com")
c.Invoke(t, true, "isAvailable", "dom.domain.com")
c.InvokeFail(t, "domain does not exist or is expired: dom.domain.com", "isAvailable", "dom.dom.domain.com")
c1.Invoke(t, true, "register",
"dom.domain.com", acc.ScriptHash(),