From 8790602f69d11d88fc1c4e04e5d4d44be60ebbfb Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Fri, 9 Sep 2022 19:36:16 +0300 Subject: [PATCH] nns: ensure records with the same type are not repeated Port https://github.com/nspcc-dev/neofs-contract/pull/170. --- examples/nft-nd-nns/nns.go | 6 +++++- examples/nft-nd-nns/nns_test.go | 13 ++++--------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/examples/nft-nd-nns/nns.go b/examples/nft-nd-nns/nns.go index 74ac349ab..d9e71799c 100644 --- a/examples/nft-nd-nns/nns.go +++ b/examples/nft-nd-nns/nns.go @@ -359,8 +359,12 @@ func AddRecord(name string, typ RecordType, data string) { tokenID := checkRecord(ctx, name, typ, data) recordsPrefix := getRecordsByTypePrefix(tokenID, name, typ) var id byte - records := storage.Find(ctx, recordsPrefix, storage.KeysOnly) + records := storage.Find(ctx, recordsPrefix, storage.ValuesOnly|storage.DeserializeValues) for iterator.Next(records) { + r := iterator.Value(records).(RecordState) + if r.Name == name && r.Type == typ && r.Data == data { + panic("record already exists") + } id++ } if id > maxRecordID { diff --git a/examples/nft-nd-nns/nns_test.go b/examples/nft-nd-nns/nns_test.go index 664b08aaf..6c2d44af8 100644 --- a/examples/nft-nd-nns/nns_test.go +++ b/examples/nft-nd-nns/nns_test.go @@ -232,11 +232,8 @@ func TestSetAddGetRecord(t *testing.T) { c.Invoke(t, stackitem.NewArray([]stackitem.Item{}), "getRecords", "neo.com", int64(nns.A)) c.Invoke(t, stackitem.Null{}, "addRecord", "neo.com", int64(nns.A), "1.2.3.4") c.Invoke(t, stackitem.NewArray([]stackitem.Item{stackitem.Make("1.2.3.4")}), "getRecords", "neo.com", int64(nns.A)) - c.Invoke(t, stackitem.Null{}, "addRecord", "neo.com", int64(nns.A), "1.2.3.4") // Duplicating record. - c.Invoke(t, stackitem.NewArray([]stackitem.Item{ - stackitem.Make("1.2.3.4"), - stackitem.Make("1.2.3.4"), - }), "getRecords", "neo.com", int64(nns.A)) + c.InvokeFail(t, "record already exists", "addRecord", "neo.com", int64(nns.A), "1.2.3.4") // Duplicating record. + c.Invoke(t, stackitem.NewArray([]stackitem.Item{stackitem.Make("1.2.3.4")}), "getRecords", "neo.com", int64(nns.A)) c.Invoke(t, stackitem.Null{}, "addRecord", "neo.com", int64(nns.AAAA), "2001:0201:1f1f:0000:0000:0100:11a0:11df") c.Invoke(t, stackitem.Null{}, "addRecord", "neo.com", int64(nns.CNAME), "nspcc.ru") c.Invoke(t, stackitem.Null{}, "addRecord", "neo.com", int64(nns.TXT), "sometext") @@ -265,10 +262,7 @@ func TestSetAddGetRecord(t *testing.T) { c.Invoke(t, stackitem.NewArray([]stackitem.Item{stackitem.Make("nspcc.ru")}), "getRecords", "neo.com", int64(nns.CNAME)) c.Invoke(t, stackitem.Null{}, "deleteRecords", "neo.com", int64(nns.CNAME)) c.Invoke(t, stackitem.NewArray([]stackitem.Item{}), "getRecords", "neo.com", int64(nns.CNAME)) - c.Invoke(t, stackitem.NewArray([]stackitem.Item{ - stackitem.Make("1.2.3.4"), - stackitem.Make("1.2.3.4"), - }), "getRecords", "neo.com", int64(nns.A)) + c.Invoke(t, stackitem.NewArray([]stackitem.Item{stackitem.Make("1.2.3.4")}), "getRecords", "neo.com", int64(nns.A)) t.Run("SetRecord_compatibility", func(t *testing.T) { // tests are got from the NNS C# implementation and changed accordingly to non-native implementation behavior @@ -331,6 +325,7 @@ func TestSetAddGetRecord(t *testing.T) { c.InvokeFail(t, "", "addRecord", args...) } else { c.Invoke(t, stackitem.Null{}, "addRecord", args...) + c.Invoke(t, stackitem.Null{}, "deleteRecords", "neo.com", int64(testCase.Type)) // clear records after test to avoid duplicating records. } }) }