[#165] nns: disallow duplicating records

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2021-11-17 17:10:07 +03:00 committed by Alex Vanin
parent 370720c83b
commit 99c81fe26a
2 changed files with 8 additions and 1 deletions

View file

@ -584,9 +584,14 @@ func addRecord(ctx storage.Context, tokenId []byte, name string, typ RecordType,
recordsKey := getRecordsKeyByType(tokenId, name, typ) recordsKey := getRecordsKeyByType(tokenId, name, typ)
var id byte var id byte
records := storage.Find(ctx, recordsKey, storage.KeysOnly) records := storage.Find(ctx, recordsKey, storage.ValuesOnly|storage.DeserializeValues)
for iterator.Next(records) { for iterator.Next(records) {
id++ id++
r := iterator.Value(records).(RecordState)
if r.Name == name && r.Type == typ && r.Data == data {
panic("record already exists")
}
} }
if typ == CNAME && id != 0 { if typ == CNAME && id != 0 {

View file

@ -95,6 +95,8 @@ func TestNNSRegister(t *testing.T) {
cAcc := c.WithSigners(acc) cAcc := c.WithSigners(acc)
cAcc.Invoke(t, stackitem.Null{}, "addRecord", cAcc.Invoke(t, stackitem.Null{}, "addRecord",
"testdomain.com", int64(nns.TXT), "first TXT record") "testdomain.com", int64(nns.TXT), "first TXT record")
cAcc.InvokeFail(t, "record already exists", "addRecord",
"testdomain.com", int64(nns.TXT), "first TXT record")
cAcc.Invoke(t, stackitem.Null{}, "addRecord", cAcc.Invoke(t, stackitem.Null{}, "addRecord",
"testdomain.com", int64(nns.TXT), "second TXT record") "testdomain.com", int64(nns.TXT), "second TXT record")