[#102] nns: Support global domain
Signed-off-by: Alexander Chuprov <a.chuprov@yadro.com>
This commit is contained in:
parent
49e5270f67
commit
d0f6a5ef19
4 changed files with 252 additions and 3 deletions
|
@ -2,7 +2,7 @@ name: "NameService"
|
||||||
supportedstandards: ["NEP-11"]
|
supportedstandards: ["NEP-11"]
|
||||||
safemethods: ["balanceOf", "decimals", "symbol", "totalSupply", "tokensOf", "ownerOf",
|
safemethods: ["balanceOf", "decimals", "symbol", "totalSupply", "tokensOf", "ownerOf",
|
||||||
"tokens", "properties", "roots", "getPrice", "isAvailable", "getRecords",
|
"tokens", "properties", "roots", "getPrice", "isAvailable", "getRecords",
|
||||||
"resolve", "version"]
|
"resolve", "version","getGlobalDomain","checkAvailableGlobalDomain"]
|
||||||
events:
|
events:
|
||||||
- name: Transfer
|
- name: Transfer
|
||||||
parameters:
|
parameters:
|
||||||
|
|
|
@ -69,6 +69,12 @@ const (
|
||||||
errInvalidDomainName = "invalid domain name format"
|
errInvalidDomainName = "invalid domain name format"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// Cnametgt is a special TXT record ensuring all created subdomains point to the global domain - the value of this variable.
|
||||||
|
//It is guaranteed that two domains cannot point to the same global domain.
|
||||||
|
Cnametgt = "cnametgt"
|
||||||
|
)
|
||||||
|
|
||||||
// RecordState is a type that registered entities are saved to.
|
// RecordState is a type that registered entities are saved to.
|
||||||
type RecordState struct {
|
type RecordState struct {
|
||||||
Name string
|
Name string
|
||||||
|
@ -231,9 +237,78 @@ func IsAvailable(name string) bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
checkParentExists(ctx, fragments)
|
checkParentExists(ctx, fragments)
|
||||||
|
if !checkAvailableGlobalDomain(ctx, fragments) {
|
||||||
|
panic("global domain is already taken")
|
||||||
|
}
|
||||||
return storage.Get(ctx, append([]byte{prefixName}, getTokenKey([]byte(name))...)) == nil
|
return storage.Get(ctx, append([]byte{prefixName}, getTokenKey([]byte(name))...)) == nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CheckAvailableGlobalDomain - triggers a panic if the global domain name is occupied.
|
||||||
|
func CheckAvailableGlobalDomain(name string) {
|
||||||
|
fragments := splitAndCheck(name)
|
||||||
|
ctx := storage.GetReadOnlyContext()
|
||||||
|
if !checkAvailableGlobalDomain(ctx, fragments) {
|
||||||
|
panic("global domain is already taken")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkAvailableGlobalDomain(ctx storage.Context, fragments []string) bool {
|
||||||
|
globalDomain := getGlobalDomain(ctx, fragments)
|
||||||
|
if globalDomain == "" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
nsBytes := storage.Get(ctx, append([]byte{prefixName}, getTokenKey([]byte(globalDomain))...))
|
||||||
|
if nsBytes != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetGlobalDomain(name string) string {
|
||||||
|
fragments := splitAndCheck(name)
|
||||||
|
ctx := storage.GetReadOnlyContext()
|
||||||
|
return getGlobalDomain(ctx, fragments)
|
||||||
|
}
|
||||||
|
|
||||||
|
func getGlobalDomain(ctx storage.Context, fragments []string) string {
|
||||||
|
name := ""
|
||||||
|
for i := 1; i < len(fragments); i++ {
|
||||||
|
if name != "" {
|
||||||
|
name = name + "."
|
||||||
|
}
|
||||||
|
name = name + fragments[i]
|
||||||
|
}
|
||||||
|
|
||||||
|
if name == "" {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
tokenID := []byte(tokenIDFromName(name))
|
||||||
|
records := getRecordsByType(ctx, tokenID, name, TXT)
|
||||||
|
|
||||||
|
if records == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
globalDomain := ""
|
||||||
|
for _, name := range records {
|
||||||
|
fragments := std.StringSplit(name, "=")
|
||||||
|
if len(fragments) != 2 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if fragments[0] == Cnametgt {
|
||||||
|
globalDomain = fragments[1]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if globalDomain == "" {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return fragments[0] + "." + globalDomain
|
||||||
|
}
|
||||||
|
|
||||||
// checkParentExists panics if any domain from fragments doesn't exist or is expired.
|
// checkParentExists panics if any domain from fragments doesn't exist or is expired.
|
||||||
func checkParentExists(ctx storage.Context, fragments []string) {
|
func checkParentExists(ctx storage.Context, fragments []string) {
|
||||||
if dom := parentExpired(ctx, fragments); dom != "" {
|
if dom := parentExpired(ctx, fragments); dom != "" {
|
||||||
|
@ -263,12 +338,16 @@ func parentExpired(ctx storage.Context, fragments []string) string {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
// Register registers a new domain with the specified owner and name if it's available.
|
|
||||||
func Register(name string, owner interop.Hash160, email string, refresh, retry, expire, ttl int) bool {
|
func Register(name string, owner interop.Hash160, email string, refresh, retry, expire, ttl int) bool {
|
||||||
|
ctx := storage.GetContext()
|
||||||
|
return register(ctx, name, owner, email, refresh, retry, expire, ttl)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Register registers a new domain with the specified owner and name if it's available.
|
||||||
|
func register(ctx storage.Context, name string, owner interop.Hash160, email string, refresh, retry, expire, ttl int) bool {
|
||||||
fragments := splitAndCheck(name)
|
fragments := splitAndCheck(name)
|
||||||
l := len(fragments)
|
l := len(fragments)
|
||||||
tldKey := append([]byte{prefixRoot}, []byte(fragments[l-1])...)
|
tldKey := append([]byte{prefixRoot}, []byte(fragments[l-1])...)
|
||||||
ctx := storage.GetContext()
|
|
||||||
tldBytes := storage.Get(ctx, tldKey)
|
tldBytes := storage.Get(ctx, tldKey)
|
||||||
if l == 1 {
|
if l == 1 {
|
||||||
checkCommittee()
|
checkCommittee()
|
||||||
|
@ -325,6 +404,10 @@ func Register(name string, owner interop.Hash160, email string, refresh, retry,
|
||||||
// NNS expiration is in milliseconds
|
// NNS expiration is in milliseconds
|
||||||
Expiration: int64(runtime.GetTime() + expire*1000),
|
Expiration: int64(runtime.GetTime() + expire*1000),
|
||||||
}
|
}
|
||||||
|
if !checkAvailableGlobalDomain(ctx, fragments) {
|
||||||
|
panic("global domain is already taken")
|
||||||
|
}
|
||||||
|
|
||||||
putNameStateWithKey(ctx, tokenKey, ns)
|
putNameStateWithKey(ctx, tokenKey, ns)
|
||||||
putSoaRecord(ctx, name, email, refresh, retry, expire, ttl)
|
putSoaRecord(ctx, name, email, refresh, retry, expire, ttl)
|
||||||
updateBalance(ctx, []byte(name), owner, +1)
|
updateBalance(ctx, []byte(name), owner, +1)
|
||||||
|
@ -332,6 +415,28 @@ func Register(name string, owner interop.Hash160, email string, refresh, retry,
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func DeleteDomain(name string) {
|
||||||
|
ctx := storage.GetContext()
|
||||||
|
deleteDomain(ctx, name)
|
||||||
|
}
|
||||||
|
|
||||||
|
func deleteDomain(ctx storage.Context, name string) {
|
||||||
|
nameKey := append([]byte{prefixName}, getTokenKey([]byte(name))...)
|
||||||
|
tldBytes := storage.Get(ctx, nameKey)
|
||||||
|
if tldBytes == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
tokenId := []byte(tokenIDFromName(name))
|
||||||
|
globalDomain := getRecordsByType(ctx, tokenId, name, CNAME)
|
||||||
|
if len(globalDomain) > 0 {
|
||||||
|
DeleteRecords(name, CNAME)
|
||||||
|
deleteDomain(ctx, globalDomain[0])
|
||||||
|
}
|
||||||
|
DeleteRecords(name, TXT)
|
||||||
|
storage.Delete(ctx, nameKey)
|
||||||
|
}
|
||||||
|
|
||||||
// Renew increases domain expiration date.
|
// Renew increases domain expiration date.
|
||||||
func Renew(name string) int64 {
|
func Renew(name string) int64 {
|
||||||
checkDomainNameLength(name)
|
checkDomainNameLength(name)
|
||||||
|
@ -427,6 +532,13 @@ func DeleteRecords(name string, typ RecordType) {
|
||||||
ns.checkAdmin()
|
ns.checkAdmin()
|
||||||
recordsKey := getRecordsKeyByType(tokenID, name, typ)
|
recordsKey := getRecordsKeyByType(tokenID, name, typ)
|
||||||
records := storage.Find(ctx, recordsKey, storage.KeysOnly)
|
records := storage.Find(ctx, recordsKey, storage.KeysOnly)
|
||||||
|
|
||||||
|
if typ == TXT {
|
||||||
|
fragments := splitAndCheck(name)
|
||||||
|
globalDomain := getGlobalDomain(ctx, fragments)
|
||||||
|
deleteDomain(ctx, globalDomain)
|
||||||
|
}
|
||||||
|
|
||||||
for iterator.Next(records) {
|
for iterator.Next(records) {
|
||||||
r := iterator.Value(records).(string)
|
r := iterator.Value(records).(string)
|
||||||
storage.Delete(ctx, r)
|
storage.Delete(ctx, r)
|
||||||
|
@ -574,6 +686,27 @@ func addRecord(ctx storage.Context, tokenId []byte, name string, typ RecordType,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fragments := splitAndCheck(name)
|
||||||
|
globalDomain := getGlobalDomain(ctx, fragments)
|
||||||
|
if globalDomain != "" {
|
||||||
|
var id byte
|
||||||
|
recordKey := getIdRecordKey(tokenId, string(tokenId), SOA, id)
|
||||||
|
|
||||||
|
recBytes := storage.Get(ctx, recordKey)
|
||||||
|
if recBytes == nil {
|
||||||
|
panic("no SOA record found for the global domain")
|
||||||
|
}
|
||||||
|
rec := std.Deserialize(recBytes.([]byte)).(RecordState)
|
||||||
|
split := std.StringSplitNonEmpty(rec.Data, " ")
|
||||||
|
if len(split) != 7 {
|
||||||
|
panic("invalid SOA record for the global domain")
|
||||||
|
}
|
||||||
|
|
||||||
|
register(ctx, globalDomain, OwnerOf(tokenId), split[1], std.Atoi(split[3], 10), std.Atoi(split[4], 10),
|
||||||
|
std.Atoi(split[5], 10), std.Atoi(split[6], 10))
|
||||||
|
|
||||||
|
putCnameRecord(ctx, name, globalDomain)
|
||||||
|
}
|
||||||
if typ == CNAME && id != 0 {
|
if typ == CNAME && id != 0 {
|
||||||
panic("you shouldn't have more than one CNAME record")
|
panic("you shouldn't have more than one CNAME record")
|
||||||
}
|
}
|
||||||
|
@ -614,6 +747,21 @@ func putSoaRecord(ctx storage.Context, name, email string, refresh, retry, expir
|
||||||
storage.Put(ctx, recordKey, recBytes)
|
storage.Put(ctx, recordKey, recBytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// putCnameRecord stores CNAME domain record.
|
||||||
|
func putCnameRecord(ctx storage.Context, name, data string) {
|
||||||
|
var id byte
|
||||||
|
tokenId := []byte(tokenIDFromName(name))
|
||||||
|
recordKey := getIdRecordKey(tokenId, name, CNAME, id)
|
||||||
|
rs := RecordState{
|
||||||
|
Name: name,
|
||||||
|
Type: CNAME,
|
||||||
|
ID: id,
|
||||||
|
Data: data,
|
||||||
|
}
|
||||||
|
recBytes := std.Serialize(rs)
|
||||||
|
storage.Put(ctx, recordKey, recBytes)
|
||||||
|
}
|
||||||
|
|
||||||
// updateSoaSerial stores soa domain record.
|
// updateSoaSerial stores soa domain record.
|
||||||
func updateSoaSerial(ctx storage.Context, tokenId []byte) {
|
func updateSoaSerial(ctx storage.Context, tokenId []byte) {
|
||||||
var id byte
|
var id byte
|
||||||
|
|
|
@ -165,6 +165,14 @@ func checkContainerList(t *testing.T, c *neotest.ContractInvoker, expected [][]b
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
// default SOA record field values
|
||||||
|
defaultRefresh = 3600 // 1 hour
|
||||||
|
defaultRetry = 600 // 10 min
|
||||||
|
defaultExpire = 3600 * 24 * 365 * 10 // 10 years
|
||||||
|
defaultTTL = 3600 // 1 hour
|
||||||
|
)
|
||||||
|
|
||||||
func TestContainerPut(t *testing.T) {
|
func TestContainerPut(t *testing.T) {
|
||||||
c, cBal, _ := newContainerInvoker(t)
|
c, cBal, _ := newContainerInvoker(t)
|
||||||
|
|
||||||
|
@ -233,6 +241,64 @@ func TestContainerPut(t *testing.T) {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("create global domain", func(t *testing.T) {
|
||||||
|
ctrNNS := neotest.CompileFile(t, c.CommitteeHash, nnsPath, path.Join(nnsPath, "config.yml"))
|
||||||
|
nnsHash := ctrNNS.Hash
|
||||||
|
cNNS := c.CommitteeInvoker(nnsHash)
|
||||||
|
cNNS.Invoke(t, true, "register",
|
||||||
|
"animals", c.CommitteeHash,
|
||||||
|
"whateveriwant@world.com", int64(0), int64(0), int64(100_000), int64(0))
|
||||||
|
|
||||||
|
cNNS.Invoke(t, true, "register",
|
||||||
|
"ns", c.CommitteeHash,
|
||||||
|
"whateveriwant@world.com", int64(0), int64(0), int64(100_000), int64(0))
|
||||||
|
|
||||||
|
cNNS.Invoke(t, true, "register",
|
||||||
|
"poland.ns", c.CommitteeHash,
|
||||||
|
"whateveriwant@world.com", int64(0), int64(0), int64(100_000), int64(0))
|
||||||
|
|
||||||
|
cNNS.Invoke(t, true, "register",
|
||||||
|
"sweden.ns", c.CommitteeHash,
|
||||||
|
"whateveriwant@world.com", int64(0), int64(0), int64(100_000), int64(0))
|
||||||
|
|
||||||
|
cNNS.Invoke(t, stackitem.Null{}, "addRecord",
|
||||||
|
"poland.ns", int64(nns.TXT), nns.Cnametgt+"=animals")
|
||||||
|
|
||||||
|
cNNS.Invoke(t, stackitem.Null{}, "addRecord", "poland.ns", int64(nns.TXT), "random-record")
|
||||||
|
|
||||||
|
cNNS.Invoke(t, stackitem.Null{}, "addRecord", "poland.ns", int64(nns.TXT), "ne-qqq=random-record2")
|
||||||
|
cNNS.Invoke(t, stackitem.Null{}, "addRecord", "sweden.ns", int64(nns.TXT), nns.Cnametgt+"=animals")
|
||||||
|
|
||||||
|
balanceMint(t, cBal, acc, (containerFee+containerAliasFee)*5, []byte{})
|
||||||
|
cNNS.Invoke(t, true, "isAvailable", "bober.animals")
|
||||||
|
putArgs := []any{cnt.value, cnt.sig, cnt.pub, cnt.token, "bober", "poland.ns"}
|
||||||
|
c3 := c.WithSigners(c.Committee, acc)
|
||||||
|
c3.Invoke(t, stackitem.Null{}, "putNamed", putArgs...)
|
||||||
|
b := c3.TopBlock(t)
|
||||||
|
|
||||||
|
expected := stackitem.NewArray([]stackitem.Item{stackitem.NewBuffer(
|
||||||
|
[]byte(fmt.Sprintf("bober.animals ops@frostfs.info %d %d %d %d %d",
|
||||||
|
b.Timestamp, defaultRefresh, defaultRetry, defaultExpire, defaultTTL)))})
|
||||||
|
cNNS.Invoke(t, expected, "getRecords", "bober.animals", int64(nns.SOA))
|
||||||
|
|
||||||
|
expected = stackitem.NewArray([]stackitem.Item{
|
||||||
|
stackitem.NewBuffer([]byte("bober.animals")),
|
||||||
|
})
|
||||||
|
|
||||||
|
cNNS.Invoke(t, false, "isAvailable", "bober.animals")
|
||||||
|
|
||||||
|
cNNS.Invoke(t, expected, "getRecords", "bober.poland.ns", int64(nns.CNAME))
|
||||||
|
|
||||||
|
putArgs = []any{cnt.value, cnt.sig, cnt.pub, cnt.token, "bober", "sweden.ns"}
|
||||||
|
|
||||||
|
c3.InvokeFail(t, "global domain is already taken", "putNamed", putArgs...)
|
||||||
|
cNNS.InvokeFail(t, "global domain is already taken", "checkAvailableGlobalDomain", "bober.poland.ns")
|
||||||
|
|
||||||
|
c3.Invoke(t, stackitem.Null{}, "delete", cnt.id[:], cnt.sig, cnt.pub, cnt.token)
|
||||||
|
cNNS.Invoke(t, true, "isAvailable", "bober.animals")
|
||||||
|
cNNS.Invoke(t, stackitem.Null{}, "checkAvailableGlobalDomain", "bober.poland.ns")
|
||||||
|
})
|
||||||
|
|
||||||
t.Run("gas costs are the same for all containers in block", func(t *testing.T) {
|
t.Run("gas costs are the same for all containers in block", func(t *testing.T) {
|
||||||
const (
|
const (
|
||||||
containerPerBlock = 512
|
containerPerBlock = 512
|
||||||
|
|
|
@ -99,6 +99,10 @@ func TestNNSRegister(t *testing.T) {
|
||||||
"com", accTop.ScriptHash(),
|
"com", accTop.ScriptHash(),
|
||||||
"myemail@frostfs.info", refresh, retry, expire, ttl)
|
"myemail@frostfs.info", refresh, retry, expire, ttl)
|
||||||
|
|
||||||
|
c1.Invoke(t, true, "register",
|
||||||
|
"globaldomain.com", accTop.ScriptHash(),
|
||||||
|
"myemail@frostfs.info", refresh, retry, expire, ttl)
|
||||||
|
|
||||||
acc := c.NewAccount(t)
|
acc := c.NewAccount(t)
|
||||||
c2 := c.WithSigners(c.Committee, acc)
|
c2 := c.WithSigners(c.Committee, acc)
|
||||||
c2.InvokeFail(t, "not witnessed by admin", "register",
|
c2.InvokeFail(t, "not witnessed by admin", "register",
|
||||||
|
@ -157,6 +161,21 @@ func TestNNSRegister(t *testing.T) {
|
||||||
stackitem.NewByteArray([]byte("second TXT record")),
|
stackitem.NewByteArray([]byte("second TXT record")),
|
||||||
})
|
})
|
||||||
c.Invoke(t, expected, "getRecords", "testdomain.com", int64(nns.TXT))
|
c.Invoke(t, expected, "getRecords", "testdomain.com", int64(nns.TXT))
|
||||||
|
|
||||||
|
c3.Invoke(t, stackitem.Null{}, "addRecord",
|
||||||
|
"testdomain.com", int64(nns.TXT), nns.Cnametgt+"=globaldomain.com")
|
||||||
|
c.Invoke(t, true, "isAvailable", "dom.testdomain.com")
|
||||||
|
|
||||||
|
c3.Invoke(t, true, "register",
|
||||||
|
"dom.testdomain.com", acc.ScriptHash(),
|
||||||
|
"myemail@frostfs.info", refresh, retry, expire, ttl)
|
||||||
|
|
||||||
|
c3.Invoke(t, stackitem.Null{}, "addRecord",
|
||||||
|
"dom.testdomain.com", int64(nns.TXT), "CID")
|
||||||
|
|
||||||
|
c.InvokeFail(t, "global domain is already taken", "isAvailable", "dom.testdomain.com")
|
||||||
|
c.Invoke(t, false, "isAvailable", "dom.globaldomain.com")
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTLDRecord(t *testing.T) {
|
func TestTLDRecord(t *testing.T) {
|
||||||
|
@ -365,6 +384,10 @@ func TestNNSIsAvailable(t *testing.T) {
|
||||||
"domain.com", acc.ScriptHash(),
|
"domain.com", acc.ScriptHash(),
|
||||||
"myemail@frostfs.info", refresh, retry, expire, ttl)
|
"myemail@frostfs.info", refresh, retry, expire, ttl)
|
||||||
|
|
||||||
|
c1.Invoke(t, true, "register",
|
||||||
|
"globaldomain.com", acc.ScriptHash(),
|
||||||
|
"myemail@frostfs.info", refresh, retry, expire, ttl)
|
||||||
|
|
||||||
c.Invoke(t, false, "isAvailable", "domain.com")
|
c.Invoke(t, false, "isAvailable", "domain.com")
|
||||||
|
|
||||||
c.Invoke(t, true, "isAvailable", "dom.domain.com")
|
c.Invoke(t, true, "isAvailable", "dom.domain.com")
|
||||||
|
@ -376,6 +399,18 @@ func TestNNSIsAvailable(t *testing.T) {
|
||||||
c.Invoke(t, false, "isAvailable", "dom.domain.com")
|
c.Invoke(t, false, "isAvailable", "dom.domain.com")
|
||||||
c.Invoke(t, true, "isAvailable", "dom.dom.domain.com")
|
c.Invoke(t, true, "isAvailable", "dom.dom.domain.com")
|
||||||
|
|
||||||
|
c1.Invoke(t, stackitem.Null{}, "addRecord",
|
||||||
|
"dom.domain.com", int64(nns.TXT), nns.Cnametgt+"=globaldomain.com")
|
||||||
|
c.Invoke(t, true, "isAvailable", "dom.dom.domain.com")
|
||||||
|
c.Invoke(t, stackitem.Null{}, "checkAvailableGlobalDomain", "dom.domain.com")
|
||||||
|
|
||||||
|
c1.Invoke(t, true, "register",
|
||||||
|
"dom.globaldomain.com", acc.ScriptHash(),
|
||||||
|
"myemail@frostfs.info", refresh, retry, expire, ttl)
|
||||||
|
|
||||||
|
c.InvokeFail(t, "global domain is already taken", "isAvailable", "dom.dom.domain.com")
|
||||||
|
c.InvokeFail(t, "global domain is already taken", "checkAvailableGlobalDomain", "dom.dom.domain.com")
|
||||||
|
|
||||||
c.InvokeFail(t, "domain name too long", "isAvailable", getTooLongDomainName(255))
|
c.InvokeFail(t, "domain name too long", "isAvailable", getTooLongDomainName(255))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue