Skip to content

Commit

Permalink
[nspcc-dev#266] nns: Restrict the maximum number of records with the …
Browse files Browse the repository at this point in the history
…same type

Signed-off-by: Anna Shaleva <[email protected]>
  • Loading branch information
AnnaShaleva committed Sep 13, 2022
1 parent 419b7d7 commit 0606349
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
6 changes: 6 additions & 0 deletions nns/nns_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ const (
maxDomainNameLength = 255
// maxTXTRecordLength is the maximum length of the TXT domain record.
maxTXTRecordLength = 255
// maxRecordID is the maximum value of record ID (the upper bound for the number
// of records with the same type).
maxRecordID = 255
)

// Other constants.
Expand Down Expand Up @@ -582,6 +585,9 @@ func addRecord(ctx storage.Context, tokenId []byte, name string, typ RecordType,
panic("record already exists")
}
}
if id > maxRecordID {
panic("maximum number of records reached")
}

if typ == CNAME && id != 0 {
panic("you shouldn't have more than one CNAME record")
Expand Down
22 changes: 21 additions & 1 deletion tests/nns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"math/big"
"path"
"strconv"
"strings"
"testing"
"time"
Expand All @@ -17,7 +18,10 @@ import (

const nnsPath = "../nns"

const msPerYear = 365 * 24 * time.Hour / time.Millisecond
const (
msPerYear = 365 * 24 * time.Hour / time.Millisecond
maxRecordID = 255 // value from the contract.
)

func newNNSInvoker(t *testing.T, addRoot bool) *neotest.ContractInvoker {
e := newExecutor(t)
Expand Down Expand Up @@ -400,3 +404,19 @@ func TestNNSResolve(t *testing.T) {
// Empty result.
c.Invoke(t, stackitem.NewArray([]stackitem.Item{}), "resolve", "test.com", int64(nns.CNAME))
}

func TestNNSAddRecord(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)
for i := 0; i <= maxRecordID+1; i++ {
if i == maxRecordID+1 {
c.InvokeFail(t, "maximum number of records reached", "addRecord", "testdomain.com", int64(nns.TXT), strconv.Itoa(i))
} else {
c.Invoke(t, stackitem.Null{}, "addRecord", "testdomain.com", int64(nns.TXT), strconv.Itoa(i))
}
}
}

0 comments on commit 0606349

Please sign in to comment.