2022-03-23 19:21:07 +00:00
|
|
|
package nns_test
|
2021-10-23 11:28:25 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
nns "github.com/nspcc-dev/neo-go/examples/nft-nd-nns"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/compiler"
|
2021-10-23 12:36:26 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop/storage"
|
2021-10-23 11:28:25 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/neotest"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/neotest/chain"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2021-10-23 12:36:26 +00:00
|
|
|
func newNSClient(t *testing.T) *neotest.ContractInvoker {
|
2021-10-23 11:28:25 +00:00
|
|
|
bc, acc := chain.NewSingle(t)
|
2021-11-03 12:22:08 +00:00
|
|
|
e := neotest.NewExecutor(t, bc, acc, acc)
|
2022-03-23 19:21:07 +00:00
|
|
|
c := neotest.CompileFile(t, e.CommitteeHash, ".", "nns.yml")
|
2021-10-23 11:28:25 +00:00
|
|
|
e.DeployContract(t, c, nil)
|
|
|
|
|
2021-11-16 14:35:39 +00:00
|
|
|
return e.CommitteeInvoker(c.Hash)
|
2021-10-23 11:28:25 +00:00
|
|
|
}
|
|
|
|
|
2021-10-23 12:36:26 +00:00
|
|
|
func TestNameService_Price(t *testing.T) {
|
|
|
|
const (
|
|
|
|
minPrice = int64(0)
|
|
|
|
maxPrice = int64(10000_00000000)
|
|
|
|
)
|
|
|
|
|
|
|
|
c := newNSClient(t)
|
|
|
|
|
|
|
|
t.Run("set, not signed by committee", func(t *testing.T) {
|
|
|
|
acc := c.NewAccount(t)
|
2021-11-03 10:44:46 +00:00
|
|
|
cAcc := c.WithSigners(acc)
|
2021-10-23 12:36:26 +00:00
|
|
|
cAcc.InvokeFail(t, "not witnessed by committee", "setPrice", minPrice+1)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("get, default value", func(t *testing.T) {
|
|
|
|
c.Invoke(t, defaultNameServiceDomainPrice, "getPrice")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("set, too small value", func(t *testing.T) {
|
|
|
|
c.InvokeFail(t, "The price is out of range.", "setPrice", minPrice-1)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("set, too large value", func(t *testing.T) {
|
|
|
|
c.InvokeFail(t, "The price is out of range.", "setPrice", maxPrice+1)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("set, success", func(t *testing.T) {
|
|
|
|
txSet := c.PrepareInvoke(t, "setPrice", int64(defaultNameServiceDomainPrice+1))
|
|
|
|
txGet := c.PrepareInvoke(t, "getPrice")
|
|
|
|
c.AddBlockCheckHalt(t, txSet, txGet)
|
|
|
|
c.CheckHalt(t, txSet.Hash(), stackitem.Null{})
|
|
|
|
c.CheckHalt(t, txGet.Hash(), stackitem.Make(defaultNameServiceDomainPrice+1))
|
|
|
|
|
|
|
|
// Get in the next block.
|
|
|
|
c.Invoke(t, stackitem.Make(defaultNameServiceDomainPrice+1), "getPrice")
|
|
|
|
})
|
|
|
|
}
|
2021-10-23 11:28:25 +00:00
|
|
|
|
|
|
|
func TestNonfungible(t *testing.T) {
|
2021-10-23 12:36:26 +00:00
|
|
|
c := newNSClient(t)
|
2021-10-23 11:28:25 +00:00
|
|
|
|
2021-11-03 10:44:46 +00:00
|
|
|
c.Signers = []neotest.Signer{c.NewAccount(t)}
|
2021-10-23 12:36:26 +00:00
|
|
|
c.Invoke(t, "NNS", "symbol")
|
|
|
|
c.Invoke(t, 0, "decimals")
|
|
|
|
c.Invoke(t, 0, "totalSupply")
|
2021-10-23 11:28:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddRoot(t *testing.T) {
|
2021-10-23 12:36:26 +00:00
|
|
|
c := newNSClient(t)
|
2021-10-23 11:28:25 +00:00
|
|
|
|
|
|
|
t.Run("invalid format", func(t *testing.T) {
|
2021-10-23 12:36:26 +00:00
|
|
|
c.InvokeFail(t, "invalid root format", "addRoot", "")
|
2021-10-23 11:28:25 +00:00
|
|
|
})
|
|
|
|
t.Run("not signed by committee", func(t *testing.T) {
|
2021-10-23 12:36:26 +00:00
|
|
|
acc := c.NewAccount(t)
|
2021-11-03 10:44:46 +00:00
|
|
|
c := c.WithSigners(acc)
|
2021-10-23 12:36:26 +00:00
|
|
|
c.InvokeFail(t, "not witnessed by committee", "addRoot", "some")
|
2021-10-23 11:28:25 +00:00
|
|
|
})
|
|
|
|
|
2021-10-23 12:36:26 +00:00
|
|
|
c.Invoke(t, stackitem.Null{}, "addRoot", "some")
|
2021-10-23 11:28:25 +00:00
|
|
|
t.Run("already exists", func(t *testing.T) {
|
2021-10-23 12:36:26 +00:00
|
|
|
c.InvokeFail(t, "already exists", "addRoot", "some")
|
2021-10-23 11:28:25 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExpiration(t *testing.T) {
|
2021-10-23 12:36:26 +00:00
|
|
|
c := newNSClient(t)
|
|
|
|
e := c.Executor
|
2021-10-23 11:28:25 +00:00
|
|
|
bc := e.Chain
|
|
|
|
|
|
|
|
acc := e.NewAccount(t)
|
2021-11-03 10:44:46 +00:00
|
|
|
cAcc := c.WithSigners(acc)
|
2021-10-23 11:28:25 +00:00
|
|
|
|
2021-10-23 12:36:26 +00:00
|
|
|
c.Invoke(t, stackitem.Null{}, "addRoot", "com")
|
2021-11-03 10:44:46 +00:00
|
|
|
cAcc.Invoke(t, true, "register", "first.com", acc.ScriptHash())
|
2021-10-23 12:36:26 +00:00
|
|
|
cAcc.Invoke(t, stackitem.Null{}, "setRecord", "first.com", int64(nns.TXT), "sometext")
|
2021-10-23 11:28:25 +00:00
|
|
|
b1 := e.TopBlock(t)
|
|
|
|
|
2021-11-03 10:44:46 +00:00
|
|
|
tx := cAcc.PrepareInvoke(t, "register", "second.com", acc.ScriptHash())
|
2021-10-23 12:36:26 +00:00
|
|
|
b2 := e.NewUnsignedBlock(t, tx)
|
2021-10-23 11:28:25 +00:00
|
|
|
b2.Index = b1.Index + 1
|
|
|
|
b2.PrevHash = b1.Hash()
|
|
|
|
b2.Timestamp = b1.Timestamp + 10000
|
|
|
|
require.NoError(t, bc.AddBlock(e.SignBlock(b2)))
|
|
|
|
e.CheckHalt(t, tx.Hash())
|
|
|
|
|
2021-10-23 12:36:26 +00:00
|
|
|
tx = cAcc.PrepareInvoke(t, "isAvailable", "first.com")
|
|
|
|
b3 := e.NewUnsignedBlock(t, tx)
|
2021-10-23 11:28:25 +00:00
|
|
|
b3.Index = b2.Index + 1
|
|
|
|
b3.PrevHash = b2.Hash()
|
|
|
|
b3.Timestamp = b1.Timestamp + (millisecondsInYear + 1)
|
|
|
|
require.NoError(t, bc.AddBlock(e.SignBlock(b3)))
|
|
|
|
e.CheckHalt(t, tx.Hash(), stackitem.NewBool(true))
|
|
|
|
|
2021-10-23 12:36:26 +00:00
|
|
|
tx = cAcc.PrepareInvoke(t, "isAvailable", "second.com")
|
|
|
|
b4 := e.NewUnsignedBlock(t, tx)
|
2021-10-23 11:28:25 +00:00
|
|
|
b4.Index = b3.Index + 1
|
|
|
|
b4.PrevHash = b3.Hash()
|
|
|
|
b4.Timestamp = b3.Timestamp + 1000
|
|
|
|
require.NoError(t, bc.AddBlock(e.SignBlock(b4)))
|
|
|
|
e.CheckHalt(t, tx.Hash(), stackitem.NewBool(false))
|
|
|
|
|
2021-10-23 12:36:26 +00:00
|
|
|
tx = cAcc.PrepareInvoke(t, "getRecord", "first.com", int64(nns.TXT))
|
|
|
|
b5 := e.NewUnsignedBlock(t, tx)
|
2021-10-23 11:28:25 +00:00
|
|
|
b5.Index = b4.Index + 1
|
|
|
|
b5.PrevHash = b4.Hash()
|
|
|
|
b5.Timestamp = b4.Timestamp + 1000
|
|
|
|
require.NoError(t, bc.AddBlock(e.SignBlock(b5)))
|
|
|
|
e.CheckFault(t, tx.Hash(), "name has expired")
|
2022-07-13 15:54:19 +00:00
|
|
|
|
|
|
|
cAcc.Invoke(t, true, "register", "first.com", acc.ScriptHash()) // Re-register.
|
|
|
|
cAcc.Invoke(t, stackitem.Null{}, "resolve", "first.com", int64(nns.TXT))
|
2021-10-23 11:28:25 +00:00
|
|
|
}
|
|
|
|
|
2022-09-05 14:30:38 +00:00
|
|
|
const (
|
|
|
|
millisecondsInYear = 365 * 24 * 3600 * 1000
|
|
|
|
maxDomainNameFragmentLength = 63
|
|
|
|
)
|
2021-10-23 11:28:25 +00:00
|
|
|
|
|
|
|
func TestRegisterAndRenew(t *testing.T) {
|
2021-10-23 12:36:26 +00:00
|
|
|
c := newNSClient(t)
|
|
|
|
e := c.Executor
|
|
|
|
|
|
|
|
c.InvokeFail(t, "root not found", "isAvailable", "neo.com")
|
|
|
|
c.Invoke(t, stackitem.Null{}, "addRoot", "org")
|
|
|
|
c.InvokeFail(t, "root not found", "isAvailable", "neo.com")
|
|
|
|
c.Invoke(t, stackitem.Null{}, "addRoot", "com")
|
|
|
|
c.Invoke(t, true, "isAvailable", "neo.com")
|
|
|
|
c.InvokeWithFeeFail(t, "GAS limit exceeded", defaultNameServiceSysfee, "register", "neo.org", e.CommitteeHash)
|
|
|
|
c.InvokeFail(t, "invalid domain name format", "register", "docs.neo.org", e.CommitteeHash)
|
|
|
|
c.InvokeFail(t, "invalid domain name format", "register", "\nneo.com'", e.CommitteeHash)
|
|
|
|
c.InvokeFail(t, "invalid domain name format", "register", "neo.com\n", e.CommitteeHash)
|
|
|
|
c.InvokeWithFeeFail(t, "GAS limit exceeded", defaultNameServiceSysfee, "register", "neo.org", e.CommitteeHash)
|
|
|
|
c.InvokeWithFeeFail(t, "GAS limit exceeded", defaultNameServiceDomainPrice, "register", "neo.com", e.CommitteeHash)
|
2022-09-05 14:30:38 +00:00
|
|
|
var maxLenFragment string
|
|
|
|
for i := 0; i < maxDomainNameFragmentLength; i++ {
|
|
|
|
maxLenFragment += "q"
|
|
|
|
}
|
|
|
|
c.Invoke(t, true, "isAvailable", maxLenFragment+".com")
|
|
|
|
c.Invoke(t, true, "register", maxLenFragment+".com", e.CommitteeHash)
|
|
|
|
c.InvokeFail(t, "invalid domain name format", "register", maxLenFragment+"q.com", e.CommitteeHash)
|
2021-10-23 12:36:26 +00:00
|
|
|
|
|
|
|
c.Invoke(t, true, "isAvailable", "neo.com")
|
2022-09-05 14:30:38 +00:00
|
|
|
c.Invoke(t, 1, "balanceOf", e.CommitteeHash)
|
2021-10-23 12:36:26 +00:00
|
|
|
c.Invoke(t, true, "register", "neo.com", e.CommitteeHash)
|
2021-10-23 11:28:25 +00:00
|
|
|
topBlock := e.TopBlock(t)
|
|
|
|
expectedExpiration := topBlock.Timestamp + millisecondsInYear
|
2021-10-23 12:36:26 +00:00
|
|
|
c.Invoke(t, false, "register", "neo.com", e.CommitteeHash)
|
|
|
|
c.Invoke(t, false, "isAvailable", "neo.com")
|
2021-10-23 11:28:25 +00:00
|
|
|
|
2022-09-05 14:30:43 +00:00
|
|
|
t.Run("domain names with hyphen", func(t *testing.T) {
|
|
|
|
c.InvokeFail(t, "invalid domain name format", "register", "-testdomain.com", e.CommitteeHash)
|
|
|
|
c.InvokeFail(t, "invalid domain name format", "register", "testdomain-.com", e.CommitteeHash)
|
|
|
|
c.Invoke(t, true, "register", "test-domain.com", e.CommitteeHash)
|
|
|
|
})
|
|
|
|
|
2021-10-23 11:28:25 +00:00
|
|
|
props := stackitem.NewMap()
|
|
|
|
props.Add(stackitem.Make("name"), stackitem.Make("neo.com"))
|
|
|
|
props.Add(stackitem.Make("expiration"), stackitem.Make(expectedExpiration))
|
2022-09-05 14:31:03 +00:00
|
|
|
props.Add(stackitem.Make("admin"), stackitem.Null{}) // no admin was set
|
2021-10-23 12:36:26 +00:00
|
|
|
c.Invoke(t, props, "properties", "neo.com")
|
2022-09-05 14:30:43 +00:00
|
|
|
c.Invoke(t, 3, "balanceOf", e.CommitteeHash)
|
2021-10-23 12:36:26 +00:00
|
|
|
c.Invoke(t, e.CommitteeHash.BytesBE(), "ownerOf", []byte("neo.com"))
|
2021-10-23 11:28:25 +00:00
|
|
|
|
|
|
|
t.Run("invalid token ID", func(t *testing.T) {
|
2021-10-23 12:36:26 +00:00
|
|
|
c.InvokeFail(t, "token not found", "properties", "not.exists")
|
|
|
|
c.InvokeFail(t, "token not found", "ownerOf", "not.exists")
|
2023-04-03 10:34:24 +00:00
|
|
|
c.InvokeFail(t, "invalid conversion", "properties", []any{})
|
|
|
|
c.InvokeFail(t, "invalid conversion", "ownerOf", []any{})
|
2021-10-23 11:28:25 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// Renew
|
|
|
|
expectedExpiration += millisecondsInYear
|
2021-10-23 12:36:26 +00:00
|
|
|
c.Invoke(t, expectedExpiration, "renew", "neo.com")
|
2021-10-23 11:28:25 +00:00
|
|
|
|
|
|
|
props.Add(stackitem.Make("expiration"), stackitem.Make(expectedExpiration))
|
2021-10-23 12:36:26 +00:00
|
|
|
c.Invoke(t, props, "properties", "neo.com")
|
2021-10-23 11:28:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetGetRecord(t *testing.T) {
|
2021-10-23 12:36:26 +00:00
|
|
|
c := newNSClient(t)
|
|
|
|
e := c.Executor
|
2021-10-23 11:28:25 +00:00
|
|
|
|
|
|
|
acc := e.NewAccount(t)
|
2021-11-03 10:44:46 +00:00
|
|
|
cAcc := c.WithSigners(acc)
|
2021-10-23 12:36:26 +00:00
|
|
|
c.Invoke(t, stackitem.Null{}, "addRoot", "com")
|
2021-10-23 11:28:25 +00:00
|
|
|
|
|
|
|
t.Run("set before register", func(t *testing.T) {
|
2021-10-23 12:36:26 +00:00
|
|
|
c.InvokeFail(t, "token not found", "setRecord", "neo.com", int64(nns.TXT), "sometext")
|
2021-10-23 11:28:25 +00:00
|
|
|
})
|
2021-10-23 12:36:26 +00:00
|
|
|
c.Invoke(t, true, "register", "neo.com", e.CommitteeHash)
|
2021-10-23 11:28:25 +00:00
|
|
|
t.Run("invalid parameters", func(t *testing.T) {
|
2021-10-23 12:36:26 +00:00
|
|
|
c.InvokeFail(t, "unsupported record type", "setRecord", "neo.com", int64(0xFF), "1.2.3.4")
|
|
|
|
c.InvokeFail(t, "invalid record", "setRecord", "neo.com", int64(nns.A), "not.an.ip.address")
|
2021-10-23 11:28:25 +00:00
|
|
|
})
|
|
|
|
t.Run("invalid witness", func(t *testing.T) {
|
2021-10-23 12:36:26 +00:00
|
|
|
cAcc.InvokeFail(t, "not witnessed by admin", "setRecord", "neo.com", int64(nns.A), "1.2.3.4")
|
2021-10-23 11:28:25 +00:00
|
|
|
})
|
2021-10-23 12:36:26 +00:00
|
|
|
c.Invoke(t, stackitem.Null{}, "getRecord", "neo.com", int64(nns.A))
|
|
|
|
c.Invoke(t, stackitem.Null{}, "setRecord", "neo.com", int64(nns.A), "1.2.3.4")
|
|
|
|
c.Invoke(t, "1.2.3.4", "getRecord", "neo.com", int64(nns.A))
|
|
|
|
c.Invoke(t, stackitem.Null{}, "setRecord", "neo.com", int64(nns.A), "1.2.3.4")
|
|
|
|
c.Invoke(t, "1.2.3.4", "getRecord", "neo.com", int64(nns.A))
|
|
|
|
c.Invoke(t, stackitem.Null{}, "setRecord", "neo.com", int64(nns.AAAA), "2001:0201:1f1f:0000:0000:0100:11a0:11df")
|
|
|
|
c.Invoke(t, stackitem.Null{}, "setRecord", "neo.com", int64(nns.CNAME), "nspcc.ru")
|
|
|
|
c.Invoke(t, stackitem.Null{}, "setRecord", "neo.com", int64(nns.TXT), "sometext")
|
2021-10-23 11:28:25 +00:00
|
|
|
|
|
|
|
// Delete record.
|
|
|
|
t.Run("invalid witness", func(t *testing.T) {
|
2021-10-23 12:36:26 +00:00
|
|
|
cAcc.InvokeFail(t, "not witnessed by admin", "deleteRecord", "neo.com", int64(nns.CNAME))
|
2021-10-23 11:28:25 +00:00
|
|
|
})
|
2021-10-23 12:36:26 +00:00
|
|
|
c.Invoke(t, "nspcc.ru", "getRecord", "neo.com", int64(nns.CNAME))
|
|
|
|
c.Invoke(t, stackitem.Null{}, "deleteRecord", "neo.com", int64(nns.CNAME))
|
|
|
|
c.Invoke(t, stackitem.Null{}, "getRecord", "neo.com", int64(nns.CNAME))
|
|
|
|
c.Invoke(t, "1.2.3.4", "getRecord", "neo.com", int64(nns.A))
|
2021-10-23 11:28:25 +00:00
|
|
|
|
|
|
|
t.Run("SetRecord_compatibility", func(t *testing.T) {
|
2022-04-20 18:30:09 +00:00
|
|
|
// tests are got from the NNS C# implementation and changed accordingly to non-native implementation behavior
|
2021-10-23 11:28:25 +00:00
|
|
|
testCases := []struct {
|
|
|
|
Type nns.RecordType
|
|
|
|
Name string
|
|
|
|
ShouldFail bool
|
|
|
|
}{
|
|
|
|
{Type: nns.A, Name: "0.0.0.0", ShouldFail: true},
|
|
|
|
{Type: nns.A, Name: "1.1.0.1"},
|
|
|
|
{Type: nns.A, Name: "10.10.10.10", ShouldFail: true},
|
|
|
|
{Type: nns.A, Name: "255.255.255.255", ShouldFail: true},
|
|
|
|
{Type: nns.A, Name: "192.168.1.1", ShouldFail: true},
|
|
|
|
{Type: nns.A, Name: "1a", ShouldFail: true},
|
|
|
|
{Type: nns.A, Name: "256.0.0.0", ShouldFail: true},
|
|
|
|
{Type: nns.A, Name: "01.01.01.01", ShouldFail: true},
|
|
|
|
{Type: nns.A, Name: "00.0.0.0", ShouldFail: true},
|
|
|
|
{Type: nns.A, Name: "0.0.0.-1", ShouldFail: true},
|
|
|
|
{Type: nns.A, Name: "0.0.0.0.1", ShouldFail: true},
|
|
|
|
{Type: nns.A, Name: "11111111.11111111.11111111.11111111", ShouldFail: true},
|
|
|
|
{Type: nns.A, Name: "11111111.11111111.11111111.11111111", ShouldFail: true},
|
|
|
|
{Type: nns.A, Name: "ff.ff.ff.ff", ShouldFail: true},
|
|
|
|
{Type: nns.A, Name: "0.0.256", ShouldFail: true},
|
|
|
|
{Type: nns.A, Name: "0.0.0", ShouldFail: true},
|
|
|
|
{Type: nns.A, Name: "0.257", ShouldFail: true},
|
|
|
|
{Type: nns.A, Name: "1.1", ShouldFail: true},
|
|
|
|
{Type: nns.A, Name: "257", ShouldFail: true},
|
|
|
|
{Type: nns.A, Name: "1", ShouldFail: true},
|
|
|
|
// {2000} & {2001} & ]2002, 3ffe[ & {3fff} are valid values for IPv6 fragment0
|
|
|
|
{Type: nns.AAAA, Name: "2002:db8::8:800:200c:417a", ShouldFail: true},
|
|
|
|
{Type: nns.AAAA, Name: "3ffd:1b8::8:800:200c:417a"},
|
|
|
|
{Type: nns.AAAA, Name: "3ffd::101"},
|
|
|
|
{Type: nns.AAAA, Name: "2003::1"},
|
|
|
|
{Type: nns.AAAA, Name: "2003::"},
|
|
|
|
{Type: nns.AAAA, Name: "2002:db8:0:0:8:800:200c:417a", ShouldFail: true},
|
|
|
|
{Type: nns.AAAA, Name: "3ffd:db8:0:0:8:800:200c:417a"},
|
|
|
|
{Type: nns.AAAA, Name: "3ffd:0:0:0:0:0:0:101"},
|
|
|
|
{Type: nns.AAAA, Name: "2002:0:0:0:0:0:0:101", ShouldFail: true},
|
|
|
|
{Type: nns.AAAA, Name: "3ffd:0:0:0:0:0:0:101"},
|
|
|
|
{Type: nns.AAAA, Name: "2001:200:0:0:0:0:0:1"},
|
|
|
|
{Type: nns.AAAA, Name: "0:0:0:0:0:0:0:1", ShouldFail: true},
|
|
|
|
{Type: nns.AAAA, Name: "2002:0:0:0:0:0:0:1", ShouldFail: true},
|
|
|
|
{Type: nns.AAAA, Name: "2001:200:0:0:0:0:0:0"},
|
|
|
|
{Type: nns.AAAA, Name: "2002:0:0:0:0:0:0:0", ShouldFail: true},
|
|
|
|
{Type: nns.AAAA, Name: "2002:DB8::8:800:200C:417A", ShouldFail: true},
|
|
|
|
{Type: nns.AAAA, Name: "3FFD:1B8::8:800:200C:417A"},
|
|
|
|
{Type: nns.AAAA, Name: "3FFD::101"},
|
|
|
|
{Type: nns.AAAA, Name: "3fFD::101"},
|
|
|
|
{Type: nns.AAAA, Name: "2002:DB8:0:0:8:800:200C:417A", ShouldFail: true},
|
|
|
|
{Type: nns.AAAA, Name: "3FFD:DB8:0:0:8:800:200C:417A"},
|
|
|
|
{Type: nns.AAAA, Name: "3FFD:0:0:0:0:0:0:101"},
|
|
|
|
{Type: nns.AAAA, Name: "3FFD::ffff:1.01.1.01", ShouldFail: true},
|
|
|
|
{Type: nns.AAAA, Name: "2001:DB8:0:0:8:800:200C:4Z", ShouldFail: true},
|
|
|
|
{Type: nns.AAAA, Name: "2001::13.1.68.3", ShouldFail: true},
|
|
|
|
}
|
|
|
|
for _, testCase := range testCases {
|
2023-04-03 10:34:24 +00:00
|
|
|
args := []any{"neo.com", int64(testCase.Type), testCase.Name}
|
2021-10-23 11:28:25 +00:00
|
|
|
t.Run(testCase.Name, func(t *testing.T) {
|
2021-10-23 12:36:26 +00:00
|
|
|
if testCase.ShouldFail {
|
|
|
|
c.InvokeFail(t, "", "setRecord", args...)
|
|
|
|
} else {
|
|
|
|
c.Invoke(t, stackitem.Null{}, "setRecord", args...)
|
|
|
|
}
|
2021-10-23 11:28:25 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetAdmin(t *testing.T) {
|
2021-10-23 12:36:26 +00:00
|
|
|
c := newNSClient(t)
|
|
|
|
e := c.Executor
|
2021-10-23 11:28:25 +00:00
|
|
|
|
|
|
|
owner := e.NewAccount(t)
|
2021-11-03 10:44:46 +00:00
|
|
|
cOwner := c.WithSigners(owner)
|
2021-10-23 11:28:25 +00:00
|
|
|
admin := e.NewAccount(t)
|
2021-11-03 10:44:46 +00:00
|
|
|
cAdmin := c.WithSigners(admin)
|
2021-10-23 11:28:25 +00:00
|
|
|
guest := e.NewAccount(t)
|
2021-11-03 10:44:46 +00:00
|
|
|
cGuest := c.WithSigners(guest)
|
2021-10-23 12:36:26 +00:00
|
|
|
|
|
|
|
c.Invoke(t, stackitem.Null{}, "addRoot", "com")
|
2021-10-23 11:28:25 +00:00
|
|
|
|
2021-11-03 10:44:46 +00:00
|
|
|
cOwner.Invoke(t, true, "register", "neo.com", owner.ScriptHash())
|
2022-09-05 14:31:03 +00:00
|
|
|
expectedExpiration := e.TopBlock(t).Timestamp + millisecondsInYear
|
2021-11-03 10:44:46 +00:00
|
|
|
cGuest.InvokeFail(t, "not witnessed", "setAdmin", "neo.com", admin.ScriptHash())
|
2021-10-23 11:28:25 +00:00
|
|
|
|
|
|
|
// Must be witnessed by both owner and admin.
|
2021-11-03 10:44:46 +00:00
|
|
|
cOwner.InvokeFail(t, "not witnessed by admin", "setAdmin", "neo.com", admin.ScriptHash())
|
|
|
|
cAdmin.InvokeFail(t, "not witnessed by owner", "setAdmin", "neo.com", admin.ScriptHash())
|
|
|
|
cc := c.WithSigners(owner, admin)
|
|
|
|
cc.Invoke(t, stackitem.Null{}, "setAdmin", "neo.com", admin.ScriptHash())
|
2022-09-05 14:31:03 +00:00
|
|
|
props := stackitem.NewMap()
|
|
|
|
props.Add(stackitem.Make("name"), stackitem.Make("neo.com"))
|
|
|
|
props.Add(stackitem.Make("expiration"), stackitem.Make(expectedExpiration))
|
|
|
|
props.Add(stackitem.Make("admin"), stackitem.Make(admin.ScriptHash().BytesBE()))
|
|
|
|
c.Invoke(t, props, "properties", "neo.com")
|
2021-10-23 11:28:25 +00:00
|
|
|
|
|
|
|
t.Run("set and delete by admin", func(t *testing.T) {
|
2021-10-23 12:36:26 +00:00
|
|
|
cAdmin.Invoke(t, stackitem.Null{}, "setRecord", "neo.com", int64(nns.TXT), "sometext")
|
|
|
|
cGuest.InvokeFail(t, "not witnessed by admin", "deleteRecord", "neo.com", int64(nns.TXT))
|
|
|
|
cAdmin.Invoke(t, stackitem.Null{}, "deleteRecord", "neo.com", int64(nns.TXT))
|
2021-10-23 11:28:25 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("set admin to null", func(t *testing.T) {
|
2021-10-23 12:36:26 +00:00
|
|
|
cAdmin.Invoke(t, stackitem.Null{}, "setRecord", "neo.com", int64(nns.TXT), "sometext")
|
|
|
|
cOwner.Invoke(t, stackitem.Null{}, "setAdmin", "neo.com", nil)
|
|
|
|
cAdmin.InvokeFail(t, "not witnessed by admin", "deleteRecord", "neo.com", int64(nns.TXT))
|
2021-10-23 11:28:25 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTransfer(t *testing.T) {
|
2021-10-23 12:36:26 +00:00
|
|
|
c := newNSClient(t)
|
|
|
|
e := c.Executor
|
2021-10-23 11:28:25 +00:00
|
|
|
|
|
|
|
from := e.NewAccount(t)
|
2021-11-03 10:44:46 +00:00
|
|
|
cFrom := c.WithSigners(from)
|
2021-10-23 11:28:25 +00:00
|
|
|
to := e.NewAccount(t)
|
2021-11-03 10:44:46 +00:00
|
|
|
cTo := c.WithSigners(to)
|
2021-10-23 11:28:25 +00:00
|
|
|
|
2021-10-23 12:36:26 +00:00
|
|
|
c.Invoke(t, stackitem.Null{}, "addRoot", "com")
|
2021-11-03 10:44:46 +00:00
|
|
|
cFrom.Invoke(t, true, "register", "neo.com", from.ScriptHash())
|
2021-10-23 12:36:26 +00:00
|
|
|
cFrom.Invoke(t, stackitem.Null{}, "setRecord", "neo.com", int64(nns.A), "1.2.3.4")
|
2021-11-03 10:44:46 +00:00
|
|
|
cFrom.InvokeFail(t, "token not found", "transfer", to.ScriptHash(), "not.exists", nil)
|
|
|
|
c.Invoke(t, false, "transfer", to.ScriptHash(), "neo.com", nil)
|
|
|
|
cFrom.Invoke(t, true, "transfer", to.ScriptHash(), "neo.com", nil)
|
2021-10-23 12:36:26 +00:00
|
|
|
cFrom.Invoke(t, 1, "totalSupply")
|
2021-11-03 10:44:46 +00:00
|
|
|
cFrom.Invoke(t, to.ScriptHash().BytesBE(), "ownerOf", "neo.com")
|
2021-10-23 11:28:25 +00:00
|
|
|
|
|
|
|
// without onNEP11Transfer
|
2021-10-23 12:36:26 +00:00
|
|
|
ctr := neotest.CompileSource(t, e.CommitteeHash,
|
2021-10-23 11:28:25 +00:00
|
|
|
strings.NewReader(`package foo
|
|
|
|
func Main() int { return 0 }`),
|
|
|
|
&compiler.Options{Name: "foo"})
|
2021-10-23 12:36:26 +00:00
|
|
|
e.DeployContract(t, ctr, nil)
|
|
|
|
cTo.InvokeFail(t, "method not found", "transfer", ctr.Hash, []byte("neo.com"), nil)
|
2021-10-23 11:28:25 +00:00
|
|
|
|
|
|
|
// with onNEP11Transfer
|
2021-10-23 12:36:26 +00:00
|
|
|
ctr = neotest.CompileSource(t, e.CommitteeHash,
|
2021-10-23 11:28:25 +00:00
|
|
|
strings.NewReader(`package foo
|
|
|
|
import "github.com/nspcc-dev/neo-go/pkg/interop"
|
2023-04-03 10:34:24 +00:00
|
|
|
func OnNEP11Payment(from interop.Hash160, amount int, token []byte, data any) {}`),
|
2021-10-23 11:28:25 +00:00
|
|
|
&compiler.Options{Name: "foo"})
|
2021-10-23 12:36:26 +00:00
|
|
|
e.DeployContract(t, ctr, nil)
|
|
|
|
cTo.Invoke(t, true, "transfer", ctr.Hash, []byte("neo.com"), nil)
|
|
|
|
cFrom.Invoke(t, 1, "totalSupply")
|
|
|
|
cFrom.Invoke(t, ctr.Hash.BytesBE(), "ownerOf", []byte("neo.com"))
|
2021-10-23 11:28:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTokensOf(t *testing.T) {
|
2021-10-23 12:36:26 +00:00
|
|
|
c := newNSClient(t)
|
|
|
|
e := c.Executor
|
2021-10-23 11:28:25 +00:00
|
|
|
|
|
|
|
acc1 := e.NewAccount(t)
|
2021-11-03 10:44:46 +00:00
|
|
|
cAcc1 := c.WithSigners(acc1)
|
2021-10-23 11:28:25 +00:00
|
|
|
acc2 := e.NewAccount(t)
|
2021-11-03 10:44:46 +00:00
|
|
|
cAcc2 := c.WithSigners(acc2)
|
2021-10-23 11:28:25 +00:00
|
|
|
|
2021-10-23 12:36:26 +00:00
|
|
|
c.Invoke(t, stackitem.Null{}, "addRoot", "com")
|
2021-11-03 10:44:46 +00:00
|
|
|
cAcc1.Invoke(t, true, "register", "neo.com", acc1.ScriptHash())
|
|
|
|
cAcc2.Invoke(t, true, "register", "nspcc.com", acc2.ScriptHash())
|
2021-10-23 11:28:25 +00:00
|
|
|
|
2021-11-03 10:44:46 +00:00
|
|
|
testTokensOf(t, c, [][]byte{[]byte("neo.com")}, acc1.ScriptHash().BytesBE())
|
|
|
|
testTokensOf(t, c, [][]byte{[]byte("nspcc.com")}, acc2.ScriptHash().BytesBE())
|
2021-10-23 12:36:26 +00:00
|
|
|
testTokensOf(t, c, [][]byte{[]byte("neo.com"), []byte("nspcc.com")})
|
|
|
|
testTokensOf(t, c, [][]byte{}, util.Uint160{}.BytesBE()) // empty hash is a valid hash still
|
2021-10-23 11:28:25 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 10:34:24 +00:00
|
|
|
func testTokensOf(t *testing.T, c *neotest.ContractInvoker, result [][]byte, args ...any) {
|
2021-10-23 11:28:25 +00:00
|
|
|
method := "tokensOf"
|
|
|
|
if len(args) == 0 {
|
|
|
|
method = "tokens"
|
|
|
|
}
|
2021-10-23 12:36:26 +00:00
|
|
|
s, err := c.TestInvoke(t, method, args...)
|
2021-10-23 11:28:25 +00:00
|
|
|
if result == nil {
|
|
|
|
require.Error(t, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
2021-10-23 12:36:26 +00:00
|
|
|
iter := s.Pop().Interop().Value().(*storage.Iterator)
|
2021-10-23 11:28:25 +00:00
|
|
|
arr := make([]stackitem.Item, 0, len(result))
|
2021-10-23 12:36:26 +00:00
|
|
|
for i := range result {
|
|
|
|
require.True(t, iter.Next())
|
|
|
|
require.Equal(t, result[i], iter.Value().Value())
|
2021-10-23 11:28:25 +00:00
|
|
|
arr = append(arr, stackitem.Make(result[i]))
|
|
|
|
}
|
2021-10-23 12:36:26 +00:00
|
|
|
require.False(t, iter.Next())
|
2021-10-23 11:28:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestResolve(t *testing.T) {
|
2021-10-23 12:36:26 +00:00
|
|
|
c := newNSClient(t)
|
|
|
|
e := c.Executor
|
2021-10-23 11:28:25 +00:00
|
|
|
|
|
|
|
acc := e.NewAccount(t)
|
2021-11-03 10:44:46 +00:00
|
|
|
cAcc := c.WithSigners(acc)
|
2021-10-23 12:36:26 +00:00
|
|
|
|
|
|
|
c.Invoke(t, stackitem.Null{}, "addRoot", "com")
|
2021-11-03 10:44:46 +00:00
|
|
|
cAcc.Invoke(t, true, "register", "neo.com", acc.ScriptHash())
|
2021-10-23 12:36:26 +00:00
|
|
|
cAcc.Invoke(t, stackitem.Null{}, "setRecord", "neo.com", int64(nns.A), "1.2.3.4")
|
|
|
|
cAcc.Invoke(t, stackitem.Null{}, "setRecord", "neo.com", int64(nns.CNAME), "alias.com")
|
|
|
|
|
2021-11-03 10:44:46 +00:00
|
|
|
cAcc.Invoke(t, true, "register", "alias.com", acc.ScriptHash())
|
2021-10-23 12:36:26 +00:00
|
|
|
cAcc.Invoke(t, stackitem.Null{}, "setRecord", "alias.com", int64(nns.TXT), "sometxt")
|
2021-10-23 11:28:25 +00:00
|
|
|
|
2021-10-23 12:36:26 +00:00
|
|
|
c.Invoke(t, "1.2.3.4", "resolve", "neo.com", int64(nns.A))
|
|
|
|
c.Invoke(t, "alias.com", "resolve", "neo.com", int64(nns.CNAME))
|
|
|
|
c.Invoke(t, "sometxt", "resolve", "neo.com", int64(nns.TXT))
|
2022-09-05 14:31:00 +00:00
|
|
|
c.Invoke(t, "sometxt", "resolve", "neo.com.", int64(nns.TXT))
|
|
|
|
c.InvokeFail(t, "invalid domain name format", "resolve", "neo.com..", int64(nns.TXT))
|
2021-10-23 12:36:26 +00:00
|
|
|
c.Invoke(t, stackitem.Null{}, "resolve", "neo.com", int64(nns.AAAA))
|
2021-10-23 11:28:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultNameServiceDomainPrice = 10_0000_0000
|
|
|
|
defaultNameServiceSysfee = 6000_0000
|
|
|
|
)
|