From 5b5ff8cb3393f071a43d449e2800a09d2b94aef6 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Thu, 18 Aug 2022 15:17:16 +0300 Subject: [PATCH] [#261] nns: Set expiration time based on arguments Signed-off-by: Evgenii Stratonikov --- CHANGELOG.md | 3 +++ nns/nns_contract.go | 7 ++++--- tests/container_test.go | 4 ++-- tests/nns_test.go | 32 ++++++++++++++++++++++++++++---- 4 files changed, 37 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 76c1fb1..4c240ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,9 @@ Changelog for NeoFS Contract ## Unrelease +### Updated +- NNS contract now sets domain expiration based on `register` arguments (#262) + ## [0.15.5] - 2022-08-23 ### Updated diff --git a/nns/nns_contract.go b/nns/nns_contract.go index 1d0cf43..47d0fc0 100644 --- a/nns/nns_contract.go +++ b/nns/nns_contract.go @@ -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) diff --git a/tests/container_test.go b/tests/container_test.go index 3b73a34..9a265e1 100644 --- a/tests/container_test.go +++ b/tests/container_test.go @@ -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{}) diff --git a/tests/nns_test.go b/tests/nns_test.go index fd3afd7..a06b5ac 100644 --- a/tests/nns_test.go +++ b/tests/nns_test.go @@ -4,6 +4,7 @@ import ( "fmt" "math/big" "path" + "strings" "testing" "time" @@ -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) @@ -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") @@ -320,7 +344,7 @@ 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")