nns: Add GetDomains #108

Closed
achuprov wants to merge 1 commit from achuprov/frostfs-contract:feat/add_GetSubDomain into master
4 changed files with 60 additions and 1 deletions
Showing only changes of commit bee4c93a56 - Show all commits

View file

@ -2,7 +2,7 @@ name: "NameService"
supportedstandards: ["NEP-11"]
safemethods: ["balanceOf", "decimals", "symbol", "totalSupply", "tokensOf", "ownerOf",
"tokens", "properties", "roots", "getPrice", "isAvailable", "getRecords",
"resolve", "version"]
"resolve", "version", "getDomains"]
events:
- name: RegisterDomain
parameters:

View file

@ -1070,3 +1070,14 @@ func getAllRecords(ctx storage.Context, name string) iterator.Iterator {
recordsKey := getRecordsKey(tokenID, name)
return storage.Find(ctx, recordsKey, storage.ValuesOnly|storage.DeserializeValues)
}
// GetDomains returns a iterator over all domains.
func GetDomains() iterator.Iterator {
ctx := storage.GetReadOnlyContext()
return getDomains(ctx)
}
func getDomains(ctx storage.Context) iterator.Iterator {
it := storage.Find(ctx, []byte{prefixName}, storage.ValuesOnly|storage.DeserializeValues)
return it
}

View file

@ -85,6 +85,20 @@ func New(actor Actor, hash util.Uint160) *Contract {
return &Contract{ContractReader{nep11ndt.NonDivisibleReader, actor, hash}, nep11ndt.BaseWriter, actor, hash}
}
// GetDomains invokes `getDomains` method of contract.
func (c *ContractReader) GetDomains() (uuid.UUID, result.Iterator, error) {
return unwrap.SessionIterator(c.invoker.Call(c.hash, "getDomains"))
}
// GetDomainsExpanded is similar to GetDomains (uses the same contract
// method), but can be useful if the server used doesn't support sessions and
// doesn't expand iterators. It creates a script that will get the specified
// number of result items from the iterator right in the VM and return them to
// you. It's only limited by VM stack and GAS available for RPC invocations.
func (c *ContractReader) GetDomainsExpanded(_numOfIteratorItems int) ([]stackitem.Item, error) {
return unwrap.Array(c.invoker.CallAndExpandIterator(c.hash, "getDomains", _numOfIteratorItems))
}
// GetPrice invokes `getPrice` method of contract.
func (c *ContractReader) GetPrice() (*big.Int, error) {
return unwrap.BigInt(c.invoker.Call(c.hash, "getPrice"))

View file

@ -232,6 +232,40 @@ func TestGlobalDomain(t *testing.T) {
c.InvokeFail(t, "global domain is already taken", "isAvailable", "dom.testdomain.com")
}
func TestGetDomains(t *testing.T) {
c := newNNSInvoker(t, false)
accTop := c.NewAccount(t)
refresh, retry, expire, ttl := int64(101), int64(102), int64(103), int64(104)
c1 := c.WithSigners(c.Committee, accTop)
domains := make(map[string]*stackitem.Struct)
c1.Invoke(t, true, "register",
"com", accTop.ScriptHash(), "myemail@frostfs.info", refresh, retry, expire, ttl)
achuprov marked this conversation as resolved Outdated

I wonder why domik.dom.com and com.com are returned and com.com.com and com.com.com.com are not?

I wonder why `domik.dom.com` and `com.com` are returned and `com.com.com` and `com.com.com.com` are not?

You are right. I've fixed the typo

You are right. I've fixed the typo
domains["com"] = stackitem.NewStruct([]stackitem.Item{stackitem.Make(accTop.ScriptHash()), stackitem.Make("com"), stackitem.Make(big.Word(int64(c1.TopBlock(t).Timestamp) + expire*1000)), stackitem.Null{}})
c1.Invoke(t, true, "register",
"net", accTop.ScriptHash(), "myemail@frostfs.info", refresh, retry, expire, ttl)
domains["net"] = stackitem.NewStruct([]stackitem.Item{stackitem.Make(accTop.ScriptHash()), stackitem.Make("net"), stackitem.Make(big.Word(int64(c1.TopBlock(t).Timestamp) + expire*1000)), stackitem.Null{}})
c1.Invoke(t, true, "register",
"com.com", accTop.ScriptHash(), "myemail@frostfs.info", refresh, retry, expire, ttl)
domains["com.com"] = stackitem.NewStruct([]stackitem.Item{stackitem.Make(accTop.ScriptHash()), stackitem.Make("com.com"), stackitem.Make(big.Word(int64(c1.TopBlock(t).Timestamp) + expire*1000)), stackitem.Null{}})
stack, _ := c1.TestInvoke(t, "getDomains")
iter := stack.Pop().Value().(*storage.Iterator)
require.True(t, iter.Next())
require.Equal(t, domains["net"], iter.Value())
require.True(t, iter.Next())
require.Equal(t, domains["com.com"], iter.Value())
require.True(t, iter.Next())
require.Equal(t, domains["com"], iter.Value())
}
func TestTLDRecord(t *testing.T) {
c := newNNSInvoker(t, true)
c.Invoke(t, stackitem.Null{}, "addRecord",