2021-10-01 19:24:05 +00:00
|
|
|
package tests
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
2021-10-26 11:28:14 +00:00
|
|
|
"path"
|
2021-10-01 19:24:05 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop/storage"
|
2021-10-26 11:28:14 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/neotest"
|
2021-10-01 19:24:05 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
|
|
|
"github.com/nspcc-dev/neofs-contract/nns"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
const nnsPath = "../nns"
|
|
|
|
|
2021-10-04 13:18:05 +00:00
|
|
|
const msPerYear = 365 * 24 * time.Hour / time.Millisecond
|
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
func newNNSInvoker(t *testing.T, addRoot bool) *neotest.ContractInvoker {
|
|
|
|
e := newExecutor(t)
|
|
|
|
ctr := neotest.CompileFile(t, e.CommitteeHash, nnsPath, path.Join(nnsPath, "config.yml"))
|
|
|
|
e.DeployContract(t, ctr, nil)
|
|
|
|
|
|
|
|
c := e.CommitteeInvoker(ctr.Hash)
|
|
|
|
if addRoot {
|
|
|
|
refresh, retry, expire, ttl := int64(101), int64(102), int64(103), int64(104)
|
|
|
|
c.Invoke(t, true, "register",
|
|
|
|
"com", c.CommitteeHash,
|
|
|
|
"myemail@nspcc.ru", refresh, retry, expire, ttl)
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
2021-10-01 19:24:05 +00:00
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
func TestNNSGeneric(t *testing.T) {
|
|
|
|
c := newNNSInvoker(t, false)
|
2021-10-01 19:24:05 +00:00
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
c.Invoke(t, "NNS", "symbol")
|
|
|
|
c.Invoke(t, 0, "decimals")
|
|
|
|
c.Invoke(t, 0, "totalSupply")
|
2021-10-01 19:24:05 +00:00
|
|
|
}
|
|
|
|
|
2021-10-04 10:26:23 +00:00
|
|
|
func TestNNSRegisterTLD(t *testing.T) {
|
2021-10-26 11:28:14 +00:00
|
|
|
c := newNNSInvoker(t, false)
|
2021-10-01 19:24:05 +00:00
|
|
|
|
2021-10-04 10:26:23 +00:00
|
|
|
refresh, retry, expire, ttl := int64(101), int64(102), int64(103), int64(104)
|
2021-10-26 11:28:14 +00:00
|
|
|
|
|
|
|
c.InvokeFail(t, "invalid domain name format", "register",
|
|
|
|
"0com", c.CommitteeHash,
|
2021-10-04 10:26:23 +00:00
|
|
|
"email@nspcc.ru", refresh, retry, expire, ttl)
|
2021-10-01 19:24:05 +00:00
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
acc := c.NewAccount(t)
|
|
|
|
cAcc := c.WithSigners(acc)
|
|
|
|
cAcc.InvokeFail(t, "not witnessed by committee", "register",
|
|
|
|
"com", acc.ScriptHash(),
|
2021-10-04 10:26:23 +00:00
|
|
|
"email@nspcc.ru", refresh, retry, expire, ttl)
|
2021-10-01 19:24:05 +00:00
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
c.Invoke(t, true, "register",
|
|
|
|
"com", c.CommitteeHash,
|
2021-10-04 10:26:23 +00:00
|
|
|
"email@nspcc.ru", refresh, retry, expire, ttl)
|
2021-10-01 19:24:05 +00:00
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
c.InvokeFail(t, "TLD already exists", "register",
|
|
|
|
"com", c.CommitteeHash,
|
2021-10-04 10:26:23 +00:00
|
|
|
"email@nspcc.ru", refresh, retry, expire, ttl)
|
2021-10-01 19:24:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNNSRegister(t *testing.T) {
|
2021-10-26 11:28:14 +00:00
|
|
|
c := newNNSInvoker(t, false)
|
2021-10-01 19:24:05 +00:00
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
accTop := c.NewAccount(t)
|
2021-10-04 10:26:23 +00:00
|
|
|
refresh, retry, expire, ttl := int64(101), int64(102), int64(103), int64(104)
|
2021-10-26 11:28:14 +00:00
|
|
|
c1 := c.WithSigners(c.Committee, accTop)
|
|
|
|
c1.Invoke(t, true, "register",
|
|
|
|
"com", accTop.ScriptHash(),
|
2021-10-04 10:26:23 +00:00
|
|
|
"myemail@nspcc.ru", refresh, retry, expire, ttl)
|
2021-10-01 19:24:05 +00:00
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
acc := c.NewAccount(t)
|
|
|
|
c2 := c.WithSigners(c.Committee, acc)
|
|
|
|
c2.InvokeFail(t, "not witnessed by admin", "register",
|
|
|
|
"testdomain.com", acc.ScriptHash(),
|
2021-10-01 19:24:05 +00:00
|
|
|
"myemail@nspcc.ru", refresh, retry, expire, ttl)
|
2021-10-04 11:47:42 +00:00
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
c3 := c.WithSigners(accTop, acc)
|
|
|
|
c3.Invoke(t, true, "register",
|
|
|
|
"testdomain.com", acc.ScriptHash(),
|
2021-10-04 11:47:42 +00:00
|
|
|
"myemail@nspcc.ru", refresh, retry, expire, ttl)
|
2021-10-01 19:24:05 +00:00
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
b := c.TopBlock(t)
|
|
|
|
expected := stackitem.NewArray([]stackitem.Item{stackitem.NewBuffer(
|
2021-10-01 19:24:05 +00:00
|
|
|
[]byte(fmt.Sprintf("testdomain.com myemail@nspcc.ru %d %d %d %d %d",
|
2021-10-26 11:28:14 +00:00
|
|
|
b.Timestamp, refresh, retry, expire, ttl)))})
|
|
|
|
c.Invoke(t, expected, "getRecords", "testdomain.com", int64(nns.SOA))
|
2021-10-01 19:24:05 +00:00
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
cAcc := c.WithSigners(acc)
|
|
|
|
cAcc.Invoke(t, stackitem.Null{}, "addRecord",
|
2021-10-01 19:24:05 +00:00
|
|
|
"testdomain.com", int64(nns.TXT), "first TXT record")
|
2021-11-17 14:10:07 +00:00
|
|
|
cAcc.InvokeFail(t, "record already exists", "addRecord",
|
|
|
|
"testdomain.com", int64(nns.TXT), "first TXT record")
|
2021-10-26 11:28:14 +00:00
|
|
|
cAcc.Invoke(t, stackitem.Null{}, "addRecord",
|
2021-10-01 19:24:05 +00:00
|
|
|
"testdomain.com", int64(nns.TXT), "second TXT record")
|
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
expected = stackitem.NewArray([]stackitem.Item{
|
2021-10-01 19:24:05 +00:00
|
|
|
stackitem.NewByteArray([]byte("first TXT record")),
|
2021-10-26 11:28:14 +00:00
|
|
|
stackitem.NewByteArray([]byte("second TXT record"))})
|
|
|
|
c.Invoke(t, expected, "getRecords", "testdomain.com", int64(nns.TXT))
|
2021-10-01 19:24:05 +00:00
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
cAcc.Invoke(t, stackitem.Null{}, "setRecord",
|
2021-10-01 19:24:05 +00:00
|
|
|
"testdomain.com", int64(nns.TXT), int64(0), "replaced first")
|
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
expected = stackitem.NewArray([]stackitem.Item{
|
2021-10-01 19:24:05 +00:00
|
|
|
stackitem.NewByteArray([]byte("replaced first")),
|
2021-10-26 11:28:14 +00:00
|
|
|
stackitem.NewByteArray([]byte("second TXT record"))})
|
|
|
|
c.Invoke(t, expected, "getRecords", "testdomain.com", int64(nns.TXT))
|
2021-10-01 19:24:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNNSUpdateSOA(t *testing.T) {
|
2021-10-26 11:28:14 +00:00
|
|
|
c := newNNSInvoker(t, true)
|
2021-10-01 19:24:05 +00:00
|
|
|
|
2021-10-04 10:26:23 +00:00
|
|
|
refresh, retry, expire, ttl := int64(101), int64(102), int64(103), int64(104)
|
2021-10-26 11:28:14 +00:00
|
|
|
c.Invoke(t, true, "register",
|
|
|
|
"testdomain.com", c.CommitteeHash,
|
2021-10-04 10:26:23 +00:00
|
|
|
"myemail@nspcc.ru", refresh, retry, expire, ttl)
|
2021-10-01 19:24:05 +00:00
|
|
|
|
|
|
|
refresh *= 2
|
|
|
|
retry *= 2
|
|
|
|
expire *= 2
|
|
|
|
ttl *= 2
|
2021-10-26 11:28:14 +00:00
|
|
|
c.Invoke(t, stackitem.Null{}, "updateSOA",
|
2021-10-01 19:24:05 +00:00
|
|
|
"testdomain.com", "newemail@nspcc.ru", refresh, retry, expire, ttl)
|
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
b := c.TopBlock(t)
|
|
|
|
expected := stackitem.NewArray([]stackitem.Item{stackitem.NewBuffer(
|
2021-10-01 19:24:05 +00:00
|
|
|
[]byte(fmt.Sprintf("testdomain.com newemail@nspcc.ru %d %d %d %d %d",
|
2021-10-26 11:28:14 +00:00
|
|
|
b.Timestamp, refresh, retry, expire, ttl)))})
|
|
|
|
c.Invoke(t, expected, "getRecords", "testdomain.com", int64(nns.SOA))
|
2021-10-01 19:24:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNNSGetAllRecords(t *testing.T) {
|
2021-10-26 11:28:14 +00:00
|
|
|
c := newNNSInvoker(t, true)
|
2021-10-01 19:24:05 +00:00
|
|
|
|
2021-10-04 10:26:23 +00:00
|
|
|
refresh, retry, expire, ttl := int64(101), int64(102), int64(103), int64(104)
|
2021-10-26 11:28:14 +00:00
|
|
|
c.Invoke(t, true, "register",
|
|
|
|
"testdomain.com", c.CommitteeHash,
|
2021-10-01 19:24:05 +00:00
|
|
|
"myemail@nspcc.ru", refresh, retry, expire, ttl)
|
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
c.Invoke(t, stackitem.Null{}, "addRecord", "testdomain.com", int64(nns.TXT), "first TXT record")
|
|
|
|
c.Invoke(t, stackitem.Null{}, "addRecord", "testdomain.com", int64(nns.A), "1.2.3.4")
|
2021-10-01 19:24:05 +00:00
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
b := c.TopBlock(t)
|
2021-10-01 19:24:05 +00:00
|
|
|
expSOA := fmt.Sprintf("testdomain.com myemail@nspcc.ru %d %d %d %d %d",
|
|
|
|
b.Timestamp, refresh, retry, expire, ttl)
|
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
s, err := c.TestInvoke(t, "getAllRecords", "testdomain.com")
|
2021-10-01 19:24:05 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
iter := s.Pop().Value().(*storage.Iterator)
|
2021-10-01 19:24:05 +00:00
|
|
|
require.True(t, iter.Next())
|
|
|
|
require.Equal(t, stackitem.NewStruct([]stackitem.Item{
|
|
|
|
stackitem.Make("testdomain.com"), stackitem.Make(int64(nns.A)),
|
|
|
|
stackitem.Make("1.2.3.4"), stackitem.Make(new(big.Int)),
|
|
|
|
}), iter.Value())
|
|
|
|
|
|
|
|
require.True(t, iter.Next())
|
|
|
|
require.Equal(t, stackitem.NewStruct([]stackitem.Item{
|
|
|
|
stackitem.Make("testdomain.com"), stackitem.Make(int64(nns.SOA)),
|
|
|
|
stackitem.NewBuffer([]byte(expSOA)), stackitem.Make(new(big.Int)),
|
|
|
|
}), iter.Value())
|
|
|
|
|
|
|
|
require.True(t, iter.Next())
|
|
|
|
require.Equal(t, stackitem.NewStruct([]stackitem.Item{
|
|
|
|
stackitem.Make("testdomain.com"), stackitem.Make(int64(nns.TXT)),
|
|
|
|
stackitem.Make("first TXT record"), stackitem.Make(new(big.Int)),
|
|
|
|
}), iter.Value())
|
|
|
|
|
|
|
|
require.False(t, iter.Next())
|
|
|
|
}
|
|
|
|
|
2021-10-04 13:18:05 +00:00
|
|
|
func TestExpiration(t *testing.T) {
|
2021-10-26 11:28:14 +00:00
|
|
|
c := newNNSInvoker(t, true)
|
2021-10-04 13:18:05 +00:00
|
|
|
|
|
|
|
refresh, retry, expire, ttl := int64(101), int64(102), int64(103), int64(104)
|
2021-10-26 11:28:14 +00:00
|
|
|
c.Invoke(t, true, "register",
|
|
|
|
"testdomain.com", c.CommitteeHash,
|
2021-10-04 13:18:05 +00:00
|
|
|
"myemail@nspcc.ru", refresh, retry, expire, ttl)
|
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
b := c.NewUnsignedBlock(t)
|
|
|
|
b.Timestamp = c.TopBlock(t).Timestamp + uint64(msPerYear) - 1
|
|
|
|
require.NoError(t, c.Chain.AddBlock(c.SignBlock(b)))
|
2021-10-04 13:18:05 +00:00
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
c.InvokeFail(t, "name has expired", "getAllRecords", "testdomain.com")
|
|
|
|
c.InvokeFail(t, "name has expired", "ownerOf", "testdomain.com")
|
2021-10-04 13:18:05 +00:00
|
|
|
}
|
|
|
|
|
2021-10-01 19:24:05 +00:00
|
|
|
func TestNNSSetAdmin(t *testing.T) {
|
2021-10-26 11:28:14 +00:00
|
|
|
c := newNNSInvoker(t, true)
|
2021-10-01 19:24:05 +00:00
|
|
|
|
2021-10-04 10:26:23 +00:00
|
|
|
refresh, retry, expire, ttl := int64(101), int64(102), int64(103), int64(104)
|
2021-10-26 11:28:14 +00:00
|
|
|
c.Invoke(t, true, "register",
|
|
|
|
"testdomain.com", c.CommitteeHash,
|
2021-10-01 19:24:05 +00:00
|
|
|
"myemail@nspcc.ru", refresh, retry, expire, ttl)
|
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
acc := c.NewAccount(t)
|
|
|
|
cAcc := c.WithSigners(acc)
|
|
|
|
cAcc.InvokeFail(t, "not witnessed by admin", "addRecord",
|
2021-10-01 19:24:05 +00:00
|
|
|
"testdomain.com", int64(nns.TXT), "won't be added")
|
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
c1 := c.WithSigners(c.Committee, acc)
|
|
|
|
c1.Invoke(t, stackitem.Null{}, "setAdmin", "testdomain.com", acc.ScriptHash())
|
2021-10-01 19:24:05 +00:00
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
cAcc.Invoke(t, stackitem.Null{}, "addRecord",
|
2021-10-01 19:24:05 +00:00
|
|
|
"testdomain.com", int64(nns.TXT), "will be added")
|
|
|
|
}
|
|
|
|
|
2021-10-04 10:26:23 +00:00
|
|
|
func TestNNSIsAvailable(t *testing.T) {
|
2021-10-26 11:28:14 +00:00
|
|
|
c := newNNSInvoker(t, false)
|
2021-10-04 10:26:23 +00:00
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
c.Invoke(t, true, "isAvailable", "com")
|
|
|
|
c.InvokeFail(t, "TLD not found", "isAvailable", "domain.com")
|
2021-10-04 10:26:23 +00:00
|
|
|
|
|
|
|
refresh, retry, expire, ttl := int64(101), int64(102), int64(103), int64(104)
|
2021-10-26 11:28:14 +00:00
|
|
|
c.Invoke(t, true, "register",
|
|
|
|
"com", c.CommitteeHash,
|
2021-10-04 10:26:23 +00:00
|
|
|
"myemail@nspcc.ru", refresh, retry, expire, ttl)
|
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
c.Invoke(t, false, "isAvailable", "com")
|
|
|
|
c.Invoke(t, true, "isAvailable", "domain.com")
|
2021-10-04 10:26:23 +00:00
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
acc := c.NewAccount(t)
|
|
|
|
c1 := c.WithSigners(c.Committee, acc)
|
|
|
|
c1.Invoke(t, true, "register",
|
|
|
|
"domain.com", acc.ScriptHash(),
|
2021-10-04 10:26:23 +00:00
|
|
|
"myemail@nspcc.ru", refresh, retry, expire, ttl)
|
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
c.Invoke(t, false, "isAvailable", "domain.com")
|
2021-10-04 10:26:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNNSRenew(t *testing.T) {
|
2021-10-26 11:28:14 +00:00
|
|
|
c := newNNSInvoker(t, true)
|
2021-10-04 10:26:23 +00:00
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
acc := c.NewAccount(t)
|
|
|
|
c1 := c.WithSigners(c.Committee, acc)
|
2021-10-01 19:24:05 +00:00
|
|
|
refresh, retry, expire, ttl := int64(101), int64(102), int64(103), int64(104)
|
2021-10-26 11:28:14 +00:00
|
|
|
c1.Invoke(t, true, "register",
|
|
|
|
"testdomain.com", c.CommitteeHash,
|
2021-10-04 10:26:23 +00:00
|
|
|
"myemail@nspcc.ru", refresh, retry, expire, ttl)
|
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
const msPerYear = 365 * 24 * time.Hour / time.Millisecond
|
|
|
|
b := c.TopBlock(t)
|
|
|
|
ts := b.Timestamp + 2*uint64(msPerYear)
|
2021-10-01 19:24:05 +00:00
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
cAcc := c.WithSigners(acc)
|
|
|
|
cAcc.Invoke(t, ts, "renew", "testdomain.com")
|
|
|
|
expected := stackitem.NewMapWithValue([]stackitem.MapElement{
|
2021-10-01 19:24:05 +00:00
|
|
|
{stackitem.Make("name"), stackitem.Make("testdomain.com")},
|
2021-10-26 11:28:14 +00:00
|
|
|
{stackitem.Make("expiration"), stackitem.Make(ts)}})
|
|
|
|
cAcc.Invoke(t, expected, "properties", "testdomain.com")
|
2021-10-01 19:24:05 +00:00
|
|
|
}
|
2021-10-16 09:47:52 +00:00
|
|
|
|
|
|
|
func TestNNSResolve(t *testing.T) {
|
2021-10-26 11:28:14 +00:00
|
|
|
c := newNNSInvoker(t, true)
|
2021-10-16 09:47:52 +00:00
|
|
|
|
|
|
|
refresh, retry, expire, ttl := int64(101), int64(102), int64(103), int64(104)
|
2021-10-26 11:28:14 +00:00
|
|
|
c.Invoke(t, true, "register",
|
|
|
|
"test.com", c.CommitteeHash,
|
2021-10-16 09:47:52 +00:00
|
|
|
"myemail@nspcc.ru", refresh, retry, expire, ttl)
|
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
c.Invoke(t, stackitem.Null{}, "addRecord",
|
2021-10-16 09:47:52 +00:00
|
|
|
"test.com", int64(nns.TXT), "expected result")
|
|
|
|
|
|
|
|
records := stackitem.NewArray([]stackitem.Item{stackitem.Make("expected result")})
|
2021-10-26 11:28:14 +00:00
|
|
|
c.Invoke(t, records, "resolve", "test.com", int64(nns.TXT))
|
|
|
|
c.Invoke(t, records, "resolve", "test.com.", int64(nns.TXT))
|
|
|
|
c.InvokeFail(t, "invalid domain name format", "resolve", "test.com..", int64(nns.TXT))
|
2021-10-16 09:47:52 +00:00
|
|
|
}
|