Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide NNS domain expiration in register #262

Merged
merged 2 commits into from
Aug 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ Changelog for NeoFS Contract

## Unrelease

### Updated
- NNS contract now sets domain expiration based on `register` arguments (#262)

### Fixed
- NNS `renew` now can only be done by the domain owner

## [0.15.5] - 2022-08-23

### Updated
Expand Down
8 changes: 5 additions & 3 deletions nns/nns_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,9 +319,10 @@ func Register(name string, owner interop.Hash160, email string, refresh, retry,
updateTotalSupply(ctx, +1)
}
ns := NameState{
Owner: owner,
Name: name,
Expiration: int64(runtime.GetTime()) + millisecondsInYear,
Owner: owner,
Name: name,
// NNS expiration is in milliseconds
Expiration: int64(runtime.GetTime() + expire*1000),
}
putNameStateWithKey(ctx, tokenKey, ns)
putSoaRecord(ctx, name, email, refresh, retry, expire, ttl)
Expand All @@ -338,6 +339,7 @@ func Renew(name string) int64 {
runtime.BurnGas(GetPrice())
ctx := storage.GetContext()
ns := getNameState(ctx, []byte(name))
ns.checkAdmin()
ns.Expiration += millisecondsInYear
putNameState(ctx, ns)
return ns.Expiration
Expand Down
4 changes: 2 additions & 2 deletions tests/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,11 @@ func TestContainerPut(t *testing.T) {

cNNS.Invoke(t, true, "register",
"cdn", c.CommitteeHash,
"whateveriwant@world.com", int64(0), int64(0), int64(0), int64(0))
"whateveriwant@world.com", int64(0), int64(0), int64(100_000), int64(0))

cNNS.Invoke(t, true, "register",
"domain.cdn", c.CommitteeHash,
"whateveriwant@world.com", int64(0), int64(0), int64(0), int64(0))
"whateveriwant@world.com", int64(0), int64(0), int64(100_000), int64(0))

balanceMint(t, cBal, acc, (containerFee+containerAliasFee)*1, []byte{})

Expand Down
35 changes: 30 additions & 5 deletions 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"
"strings"
"testing"
"time"

Expand All @@ -25,7 +26,8 @@ func newNNSInvoker(t *testing.T, addRoot bool) *neotest.ContractInvoker {

c := e.CommitteeInvoker(ctr.Hash)
if addRoot {
refresh, retry, expire, ttl := int64(101), int64(102), int64(103), int64(104)
// Set expiration big enough to pass all tests.
refresh, retry, expire, ttl := int64(101), int64(102), int64(msPerYear/1000*100), int64(104)
c.Invoke(t, true, "register",
"com", c.CommitteeHash,
"myemail@nspcc.ru", refresh, retry, expire, ttl)
Expand Down Expand Up @@ -252,14 +254,36 @@ func TestNNSGetAllRecords(t *testing.T) {
func TestExpiration(t *testing.T) {
c := newNNSInvoker(t, true)

refresh, retry, expire, ttl := int64(101), int64(102), int64(103), int64(104)
refresh, retry, expire, ttl := int64(101), int64(102), int64(msPerYear/1000*10), int64(104)
c.Invoke(t, true, "register",
"testdomain.com", c.CommitteeHash,
"myemail@nspcc.ru", refresh, retry, expire, ttl)

checkProperties := func(t *testing.T, expiration uint64) {
expected := stackitem.NewMapWithValue([]stackitem.MapElement{
{Key: stackitem.Make("name"), Value: stackitem.Make("testdomain.com")},
{Key: stackitem.Make("expiration"), Value: stackitem.Make(expiration)}})
s, err := c.TestInvoke(t, "properties", "testdomain.com")
require.NoError(t, err)
require.Equal(t, expected.Value(), s.Top().Item().Value())
}

top := c.TopBlock(t)
expiration := top.Timestamp + uint64(expire*1000)
checkProperties(t, expiration)

b := c.NewUnsignedBlock(t)
b.Timestamp = c.TopBlock(t).Timestamp + uint64(msPerYear) - 1
b.Timestamp = expiration - 2 // test invoke is done with +1 timestamp
require.NoError(t, c.Chain.AddBlock(c.SignBlock(b)))
checkProperties(t, expiration)

b = c.NewUnsignedBlock(t)
b.Timestamp = expiration - 1
require.NoError(t, c.Chain.AddBlock(c.SignBlock(b)))

_, err := c.TestInvoke(t, "properties", "testdomain.com")
require.Error(t, err)
require.True(t, strings.Contains(err.Error(), "name has expired"))

c.InvokeFail(t, "name has expired", "getAllRecords", "testdomain.com")
c.InvokeFail(t, "name has expired", "ownerOf", "testdomain.com")
Expand Down Expand Up @@ -320,10 +344,11 @@ func TestNNSRenew(t *testing.T) {

const msPerYear = 365 * 24 * time.Hour / time.Millisecond
b := c.TopBlock(t)
ts := b.Timestamp + 2*uint64(msPerYear)
ts := b.Timestamp + uint64(expire*1000) + uint64(msPerYear)

cAcc := c.WithSigners(acc)
cAcc.Invoke(t, ts, "renew", "testdomain.com")
cAcc.InvokeFail(t, "not witnessed by admin", "renew", "testdomain.com")
c1.Invoke(t, ts, "renew", "testdomain.com")
expected := stackitem.NewMapWithValue([]stackitem.MapElement{
{Key: stackitem.Make("name"), Value: stackitem.Make("testdomain.com")},
{Key: stackitem.Make("expiration"), Value: stackitem.Make(ts)}})
Expand Down