From cd005e54a418a3fd04b9277be351688b7b87f6f5 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Wed, 6 Oct 2021 14:59:08 +0300 Subject: [PATCH] [#135] tests: check container nice-name registration Signed-off-by: Evgenii Stratonikov --- tests/balance_test.go | 18 +++++++ tests/basic.go | 5 +- tests/container_test.go | 102 ++++++++++++++++++++++++++++++++++++++++ tests/netmap_test.go | 20 ++++++++ 4 files changed, 144 insertions(+), 1 deletion(-) create mode 100644 tests/balance_test.go create mode 100644 tests/container_test.go create mode 100644 tests/netmap_test.go diff --git a/tests/balance_test.go b/tests/balance_test.go new file mode 100644 index 0000000..84eac3b --- /dev/null +++ b/tests/balance_test.go @@ -0,0 +1,18 @@ +package tests + +import ( + "testing" + + "github.com/nspcc-dev/neo-go/pkg/core" + "github.com/nspcc-dev/neo-go/pkg/util" +) + +const balancePath = "../balance" + +func deployBalanceContract(t *testing.T, bc *core.Blockchain, addrNetmap, addrContainer util.Uint160) util.Uint160 { + args := make([]interface{}, 3) + args[0] = false + args[1] = addrNetmap + args[2] = addrContainer + return DeployContract(t, bc, balancePath, args) +} diff --git a/tests/basic.go b/tests/basic.go index 851ac15..9fb5cde 100644 --- a/tests/basic.go +++ b/tests/basic.go @@ -174,7 +174,10 @@ func newDeployTx(t *testing.T, bc *core.Blockchain, ctrPath string, data interfa tx := transaction.New(buf.Bytes(), 100*native.GASFactor) tx.Nonce = nonce() tx.ValidUntilBlock = bc.BlockHeight() + 1 - tx.Signers = []transaction.Signer{{Account: CommitteeAcc.Contract.ScriptHash()}} + tx.Signers = []transaction.Signer{{ + Account: CommitteeAcc.Contract.ScriptHash(), + Scopes: transaction.Global, + }} require.NoError(t, addNetworkFee(bc, tx, CommitteeAcc)) require.NoError(t, CommitteeAcc.SignTx(netmode.UnitTestNet, tx)) return tx, c.Hash diff --git a/tests/container_test.go b/tests/container_test.go new file mode 100644 index 0000000..f9007d3 --- /dev/null +++ b/tests/container_test.go @@ -0,0 +1,102 @@ +package tests + +import ( + "crypto/sha256" + "testing" + + "github.com/mr-tron/base58" + "github.com/nspcc-dev/neo-go/pkg/core" + "github.com/nspcc-dev/neo-go/pkg/util" + "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" + "github.com/nspcc-dev/neo-go/pkg/wallet" + "github.com/nspcc-dev/neofs-contract/nns" + "github.com/stretchr/testify/require" +) + +const containerPath = "../container" + +func deployContainerContract(t *testing.T, bc *core.Blockchain, addrNetmap, addrBalance, addrNNS util.Uint160) util.Uint160 { + args := make([]interface{}, 6) + args[0] = int64(0) + args[1] = addrNetmap + args[2] = addrBalance + args[3] = util.Uint160{} // not needed for now + args[4] = addrNNS + args[5] = "neofs" + return DeployContract(t, bc, containerPath, args) +} + +func prepareContainerContract(t *testing.T, bc *core.Blockchain) util.Uint160 { + addrNNS := DeployContract(t, bc, nnsPath, nil) + + ctrNetmap, err := ContractInfo(CommitteeAcc.Contract.ScriptHash(), netmapPath) + require.NoError(t, err) + + ctrBalance, err := ContractInfo(CommitteeAcc.Contract.ScriptHash(), balancePath) + require.NoError(t, err) + + ctrContainer, err := ContractInfo(CommitteeAcc.Contract.ScriptHash(), containerPath) + require.NoError(t, err) + + deployNetmapContract(t, bc, ctrBalance.Hash, ctrContainer.Hash, "ContainerFee", []byte{}) + deployBalanceContract(t, bc, ctrNetmap.Hash, ctrContainer.Hash) + return deployContainerContract(t, bc, ctrNetmap.Hash, ctrBalance.Hash, addrNNS) +} + +func setContainerOwner(c []byte, owner *wallet.Account) { + copy(c[7:], owner.Contract.ScriptHash().BytesBE()) +} + +func TestContainerPut(t *testing.T) { + bc := NewChain(t) + h := prepareContainerContract(t, bc) + + acc := NewAccount(t, bc) + dummySig := make([]byte, 64) + dummyPub := make([]byte, 33) + dummyToken := make([]byte, 100) + container := make([]byte, 100) + setContainerOwner(container, acc) + containerID := sha256.Sum256(container) + + tx := PrepareInvoke(t, bc, CommitteeAcc, h, "put", container, dummySig, dummyPub, dummyToken) + AddBlockCheckHalt(t, bc, tx) + + t.Run("with nice names", func(t *testing.T) { + nnsHash := contracts[nnsPath].Hash + + tx = PrepareInvoke(t, bc, CommitteeAcc, h, "putNamed", + container, dummySig, dummyPub, dummyToken, "mycnt", "") + AddBlockCheckHalt(t, bc, tx) + + tx = PrepareInvoke(t, bc, CommitteeAcc, nnsHash, "resolve", "mycnt.neofs", int64(nns.TXT)) + CheckTestInvoke(t, bc, tx, stackitem.NewArray([]stackitem.Item{ + stackitem.NewByteArray([]byte(base58.Encode(containerID[:]))), + })) + + tx = PrepareInvoke(t, bc, CommitteeAcc, h, "delete", containerID[:], dummySig, dummyToken) + AddBlockCheckHalt(t, bc, tx) + + tx = PrepareInvoke(t, bc, CommitteeAcc, nnsHash, "resolve", "mycnt.neofs", int64(nns.TXT)) + CheckTestInvoke(t, bc, tx, stackitem.Null{}) + + t.Run("register in advance", func(t *testing.T) { + container[len(container)-1] = 10 + containerID = sha256.Sum256(container) + + tx = PrepareInvoke(t, bc, []*wallet.Account{CommitteeAcc, acc}, nnsHash, "register", + "second.neofs", acc.Contract.ScriptHash(), + "whateveriwant@world.com", int64(0), int64(0), int64(0), int64(0)) + AddBlockCheckHalt(t, bc, tx) + + tx = PrepareInvoke(t, bc, []*wallet.Account{CommitteeAcc, acc}, h, "putNamed", + container, dummySig, dummyPub, dummyToken, "second", "neofs") + AddBlockCheckHalt(t, bc, tx) + + tx = PrepareInvoke(t, bc, CommitteeAcc, nnsHash, "resolve", "second.neofs", int64(nns.TXT)) + CheckTestInvoke(t, bc, tx, stackitem.NewArray([]stackitem.Item{ + stackitem.NewByteArray([]byte(base58.Encode(containerID[:]))), + })) + }) + }) +} diff --git a/tests/netmap_test.go b/tests/netmap_test.go new file mode 100644 index 0000000..cf9d472 --- /dev/null +++ b/tests/netmap_test.go @@ -0,0 +1,20 @@ +package tests + +import ( + "testing" + + "github.com/nspcc-dev/neo-go/pkg/core" + "github.com/nspcc-dev/neo-go/pkg/util" +) + +const netmapPath = "../netmap" + +func deployNetmapContract(t *testing.T, bc *core.Blockchain, addrBalance, addrContainer util.Uint160, config ...interface{}) util.Uint160 { + args := make([]interface{}, 5) + args[0] = false + args[1] = addrBalance + args[2] = addrContainer + args[3] = []interface{}{CommitteeAcc.PrivateKey().PublicKey().Bytes()} + args[4] = append([]interface{}{}, config...) + return DeployContract(t, bc, netmapPath, args) +}