Skip to content

Commit

Permalink
[nspcc-dev#266] nns: Return empty Array from getRecords instead of Null
Browse files Browse the repository at this point in the history
And adjust method usages along the way.

Signed-off-by: Anna Shaleva <[email protected]>
  • Loading branch information
AnnaShaleva committed Sep 13, 2022
1 parent 72e0695 commit ae237ff
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
8 changes: 4 additions & 4 deletions container/container_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,8 @@ func checkNiceNameAvailable(nnsContractAddr interop.Hash160, domain string) bool
}

res := contract.Call(nnsContractAddr, "getRecords",
contract.ReadStates|contract.AllowCall, domain, 16 /* TXT */)
if res != nil {
contract.ReadStates|contract.AllowCall, domain, 16 /* TXT */).([]string)
if len(res) > 0 {
panic("name is already taken")
}

Expand Down Expand Up @@ -351,8 +351,8 @@ func Delete(containerID []byte, signature interop.Signature, token []byte) {
// by other means (expiration, manual), thus leading to failing `deleteRecord`
// and inability to delete a container. We should also check if we own the record in case.
nnsContractAddr := storage.Get(ctx, nnsContractKey).(interop.Hash160)
res := contract.Call(nnsContractAddr, "getRecords", contract.ReadStates|contract.AllowCall, domain, 16 /* TXT */)
if res != nil && std.Base58Encode(containerID) == string(res.([]interface{})[0].(string)) {
res := contract.Call(nnsContractAddr, "getRecords", contract.ReadStates|contract.AllowCall, domain, 16 /* TXT */).([]string)
if len(res) > 0 && std.Base58Encode(containerID) == res[0] {
contract.Call(nnsContractAddr, "deleteRecords", contract.All, domain, 16 /* TXT */)
}
}
Expand Down
4 changes: 2 additions & 2 deletions nns/nns_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,11 +542,11 @@ func putNameStateWithKey(ctx storage.Context, tokenKey []byte, ns NameState) {
storage.Put(ctx, nameKey, nsBytes)
}

// getRecordsByType returns domain record.
// getRecordsByType returns domain record. It returns empty array if no records found.
func getRecordsByType(ctx storage.Context, tokenId []byte, name string, typ RecordType) []string {
recordsKey := getRecordsKeyByType(tokenId, name, typ)

var result []string
result := []string{}
records := storage.Find(ctx, recordsKey, storage.ValuesOnly|storage.DeserializeValues)
for iterator.Next(records) {
r := iterator.Value(records).(RecordState)
Expand Down
17 changes: 17 additions & 0 deletions tests/nns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,23 @@ func TestNNSGetAllRecords(t *testing.T) {
require.False(t, iter.Next())
}

func TestNNSGetRecords(t *testing.T) {
c := newNNSInvoker(t, true)

refresh, retry, expire, ttl := int64(101), int64(102), int64(103), int64(104)
c.Invoke(t, true, "register",
"testdomain.com", c.CommitteeHash,
"[email protected]", refresh, retry, expire, ttl)

txtData := "first TXT record"
c.Invoke(t, stackitem.Null{}, "addRecord", "testdomain.com", int64(nns.TXT), txtData)
c.Invoke(t, stackitem.Null{}, "addRecord", "testdomain.com", int64(nns.A), "1.2.3.4")

c.Invoke(t, stackitem.NewArray([]stackitem.Item{stackitem.Make(txtData)}), "getRecords", "testdomain.com", int64(nns.TXT))
// Check empty result.
c.Invoke(t, stackitem.NewArray([]stackitem.Item{}), "getRecords", "testdomain.com", int64(nns.AAAA))
}

func TestExpiration(t *testing.T) {
c := newNNSInvoker(t, true)

Expand Down

0 comments on commit ae237ff

Please sign in to comment.