From ed09e356b21e2e33348970846f20d2f3cf10669d Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Fri, 9 Sep 2022 19:40:35 +0300 Subject: [PATCH] [#266] nns: Return empty Array from getRecords instead of Null And adjust method usages along the way. Signed-off-by: Anna Shaleva --- container/container_contract.go | 8 ++++---- nns/nns_contract.go | 4 ++-- tests/nns_test.go | 17 +++++++++++++++++ 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/container/container_contract.go b/container/container_contract.go index c3721238..abf210b0 100644 --- a/container/container_contract.go +++ b/container/container_contract.go @@ -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") } @@ -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 */) } } diff --git a/nns/nns_contract.go b/nns/nns_contract.go index ed2ea2a4..add8f6b8 100644 --- a/nns/nns_contract.go +++ b/nns/nns_contract.go @@ -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) diff --git a/tests/nns_test.go b/tests/nns_test.go index 91fc7ec5..6b1b69d1 100644 --- a/tests/nns_test.go +++ b/tests/nns_test.go @@ -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, + "myemail@nspcc.ru", 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)