2021-10-06 11:59:08 +00:00
|
|
|
package tests
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
2021-10-26 11:28:14 +00:00
|
|
|
"path"
|
2021-10-06 11:59:08 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/mr-tron/base58"
|
2021-10-26 11:28:14 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/neotest"
|
2021-10-06 11:59:08 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2021-11-29 12:21:40 +00:00
|
|
|
"github.com/nspcc-dev/neofs-contract/common"
|
2021-10-18 11:44:06 +00:00
|
|
|
"github.com/nspcc-dev/neofs-contract/container"
|
2021-10-06 11:59:08 +00:00
|
|
|
"github.com/nspcc-dev/neofs-contract/nns"
|
|
|
|
)
|
|
|
|
|
|
|
|
const containerPath = "../container"
|
|
|
|
|
2021-10-18 10:45:10 +00:00
|
|
|
const (
|
|
|
|
containerFee = 0_0100_0000
|
|
|
|
containerAliasFee = 0_0050_0000
|
|
|
|
)
|
2021-10-18 11:12:26 +00:00
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
func deployContainerContract(t *testing.T, e *neotest.Executor, addrNetmap, addrBalance, addrNNS util.Uint160) util.Uint160 {
|
2021-10-06 11:59:08 +00:00
|
|
|
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"
|
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
c := neotest.CompileFile(t, e.CommitteeHash, containerPath, path.Join(containerPath, "config.yml"))
|
|
|
|
e.DeployContract(t, c, args)
|
|
|
|
return c.Hash
|
|
|
|
}
|
2021-10-06 11:59:08 +00:00
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
func newContainerInvoker(t *testing.T) (*neotest.ContractInvoker, *neotest.ContractInvoker) {
|
|
|
|
e := newExecutor(t)
|
2021-10-06 11:59:08 +00:00
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
ctrNNS := neotest.CompileFile(t, e.CommitteeHash, nnsPath, path.Join(nnsPath, "config.yml"))
|
|
|
|
ctrNetmap := neotest.CompileFile(t, e.CommitteeHash, netmapPath, path.Join(netmapPath, "config.yml"))
|
|
|
|
ctrBalance := neotest.CompileFile(t, e.CommitteeHash, balancePath, path.Join(balancePath, "config.yml"))
|
|
|
|
ctrContainer := neotest.CompileFile(t, e.CommitteeHash, containerPath, path.Join(containerPath, "config.yml"))
|
2021-10-06 11:59:08 +00:00
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
e.DeployContract(t, ctrNNS, nil)
|
|
|
|
deployNetmapContract(t, e, ctrBalance.Hash, ctrContainer.Hash,
|
2021-10-18 10:45:10 +00:00
|
|
|
container.RegistrationFeeKey, int64(containerFee),
|
|
|
|
container.AliasFeeKey, int64(containerAliasFee))
|
2021-10-26 11:28:14 +00:00
|
|
|
deployBalanceContract(t, e, ctrNetmap.Hash, ctrContainer.Hash)
|
|
|
|
deployContainerContract(t, e, ctrNetmap.Hash, ctrBalance.Hash, ctrNNS.Hash)
|
|
|
|
return e.CommitteeInvoker(ctrContainer.Hash), e.CommitteeInvoker(ctrBalance.Hash)
|
2021-10-06 11:59:08 +00:00
|
|
|
}
|
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
func setContainerOwner(c []byte, acc neotest.Signer) {
|
|
|
|
owner, _ := base58.Decode(address.Uint160ToString(acc.ScriptHash()))
|
2021-10-20 09:43:30 +00:00
|
|
|
copy(c[6:], owner)
|
2021-10-06 11:59:08 +00:00
|
|
|
}
|
|
|
|
|
2021-10-11 07:39:49 +00:00
|
|
|
type testContainer struct {
|
|
|
|
id [32]byte
|
|
|
|
value, sig, pub, token []byte
|
|
|
|
}
|
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
func dummyContainer(owner neotest.Signer) testContainer {
|
2021-10-11 07:39:49 +00:00
|
|
|
value := randomBytes(100)
|
|
|
|
value[1] = 0 // zero offset
|
|
|
|
setContainerOwner(value, owner)
|
|
|
|
|
|
|
|
return testContainer{
|
|
|
|
id: sha256.Sum256(value),
|
|
|
|
value: value,
|
|
|
|
sig: randomBytes(64),
|
|
|
|
pub: randomBytes(33),
|
|
|
|
token: randomBytes(42),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-06 11:59:08 +00:00
|
|
|
func TestContainerPut(t *testing.T) {
|
2021-10-26 11:28:14 +00:00
|
|
|
c, cBal := newContainerInvoker(t)
|
2021-10-06 11:59:08 +00:00
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
acc := c.NewAccount(t)
|
|
|
|
cnt := dummyContainer(acc)
|
2021-10-11 07:39:49 +00:00
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
putArgs := []interface{}{cnt.value, cnt.sig, cnt.pub, cnt.token}
|
|
|
|
c.InvokeFail(t, "insufficient balance to create container", "put", putArgs...)
|
2021-10-18 11:12:26 +00:00
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
balanceMint(t, cBal, acc, containerFee*1, []byte{})
|
2021-10-18 11:12:26 +00:00
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
cAcc := c.WithSigners(acc)
|
2021-11-29 12:21:40 +00:00
|
|
|
cAcc.InvokeFail(t, common.ErrAlphabetWitnessFailed, "put", putArgs...)
|
2021-10-18 11:12:26 +00:00
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
c.Invoke(t, stackitem.Null{}, "put", putArgs...)
|
2021-10-06 11:59:08 +00:00
|
|
|
|
|
|
|
t.Run("with nice names", func(t *testing.T) {
|
2021-10-26 11:28:14 +00:00
|
|
|
ctrNNS := neotest.CompileFile(t, c.CommitteeHash, nnsPath, path.Join(nnsPath, "config.yml"))
|
|
|
|
nnsHash := ctrNNS.Hash
|
2021-10-06 11:59:08 +00:00
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
balanceMint(t, cBal, acc, containerFee*1, []byte{})
|
2021-10-18 11:12:26 +00:00
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
putArgs := []interface{}{cnt.value, cnt.sig, cnt.pub, cnt.token, "mycnt", ""}
|
2021-10-18 10:45:10 +00:00
|
|
|
t.Run("no fee for alias", func(t *testing.T) {
|
2021-10-26 11:28:14 +00:00
|
|
|
c.InvokeFail(t, "insufficient balance to create container", "putNamed", putArgs...)
|
2021-10-18 10:45:10 +00:00
|
|
|
})
|
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
balanceMint(t, cBal, acc, containerAliasFee*1, []byte{})
|
|
|
|
c.Invoke(t, stackitem.Null{}, "putNamed", putArgs...)
|
2021-10-18 10:45:10 +00:00
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
expected := stackitem.NewArray([]stackitem.Item{
|
|
|
|
stackitem.NewByteArray([]byte(base58.Encode(cnt.id[:]))),
|
|
|
|
})
|
|
|
|
cNNS := c.CommitteeInvoker(nnsHash)
|
|
|
|
cNNS.Invoke(t, expected, "resolve", "mycnt.neofs", int64(nns.TXT))
|
2021-10-06 11:59:08 +00:00
|
|
|
|
2021-10-21 15:59:59 +00:00
|
|
|
t.Run("name is already taken", func(t *testing.T) {
|
2021-10-26 11:28:14 +00:00
|
|
|
c.InvokeFail(t, "name is already taken", "putNamed", putArgs...)
|
2021-10-21 15:59:59 +00:00
|
|
|
})
|
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
c.Invoke(t, stackitem.Null{}, "delete", cnt.id[:], cnt.sig, cnt.token)
|
|
|
|
cNNS.Invoke(t, stackitem.Null{}, "resolve", "mycnt.neofs", int64(nns.TXT))
|
2021-10-06 11:59:08 +00:00
|
|
|
|
|
|
|
t.Run("register in advance", func(t *testing.T) {
|
2021-10-26 11:28:14 +00:00
|
|
|
cnt.value[len(cnt.value)-1] = 10
|
|
|
|
cnt.id = sha256.Sum256(cnt.value)
|
2021-10-06 11:59:08 +00:00
|
|
|
|
2021-12-08 12:39:18 +00:00
|
|
|
cNNS.Invoke(t, true, "register",
|
|
|
|
"cdn", c.CommitteeHash,
|
|
|
|
"whateveriwant@world.com", int64(0), int64(0), int64(0), int64(0))
|
2021-10-18 11:32:17 +00:00
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
cNNS.Invoke(t, true, "register",
|
2021-12-08 12:39:18 +00:00
|
|
|
"domain.cdn", c.CommitteeHash,
|
2021-10-06 11:59:08 +00:00
|
|
|
"whateveriwant@world.com", int64(0), int64(0), int64(0), int64(0))
|
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
balanceMint(t, cBal, acc, (containerFee+containerAliasFee)*1, []byte{})
|
2021-10-18 11:12:26 +00:00
|
|
|
|
2021-12-08 12:39:18 +00:00
|
|
|
putArgs := []interface{}{cnt.value, cnt.sig, cnt.pub, cnt.token, "domain", "cdn"}
|
2021-10-26 11:28:14 +00:00
|
|
|
c2 := c.WithSigners(c.Committee, acc)
|
|
|
|
c2.Invoke(t, stackitem.Null{}, "putNamed", putArgs...)
|
2021-10-06 11:59:08 +00:00
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
expected = stackitem.NewArray([]stackitem.Item{
|
|
|
|
stackitem.NewByteArray([]byte(base58.Encode(cnt.id[:])))})
|
2021-12-08 12:39:18 +00:00
|
|
|
cNNS.Invoke(t, expected, "resolve", "domain.cdn", int64(nns.TXT))
|
2021-10-06 11:59:08 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2021-10-19 07:59:40 +00:00
|
|
|
|
|
|
|
func TestContainerDelete(t *testing.T) {
|
2021-10-26 11:28:14 +00:00
|
|
|
c, cBal := newContainerInvoker(t)
|
2021-10-19 07:59:40 +00:00
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
acc := c.NewAccount(t)
|
|
|
|
cnt := dummyContainer(acc)
|
2021-10-19 07:59:40 +00:00
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
balanceMint(t, cBal, acc, containerFee*1, []byte{})
|
|
|
|
c.Invoke(t, stackitem.Null{}, "put", cnt.value, cnt.sig, cnt.pub, cnt.token)
|
2021-10-19 07:59:40 +00:00
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
cAcc := c.WithSigners(acc)
|
2021-11-29 12:21:40 +00:00
|
|
|
cAcc.InvokeFail(t, common.ErrAlphabetWitnessFailed, "delete",
|
2021-10-26 11:28:14 +00:00
|
|
|
cnt.id[:], cnt.sig, cnt.token)
|
2021-10-19 07:59:40 +00:00
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
c.Invoke(t, stackitem.Null{}, "delete", cnt.id[:], cnt.sig, cnt.token)
|
2021-10-19 07:59:40 +00:00
|
|
|
|
2021-10-25 12:43:57 +00:00
|
|
|
t.Run("missing container", func(t *testing.T) {
|
2021-10-26 11:28:14 +00:00
|
|
|
id := cnt.id
|
2021-10-25 12:43:57 +00:00
|
|
|
id[0] ^= 0xFF
|
2021-10-26 11:28:14 +00:00
|
|
|
c.Invoke(t, stackitem.Null{}, "delete", cnt.id[:], cnt.sig, cnt.token)
|
2021-10-25 12:43:57 +00:00
|
|
|
})
|
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
c.InvokeFail(t, container.NotFoundError, "get", cnt.id[:])
|
2021-10-11 07:42:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestContainerOwner(t *testing.T) {
|
2021-10-26 11:28:14 +00:00
|
|
|
c, cBal := newContainerInvoker(t)
|
2021-10-11 07:42:14 +00:00
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
acc := c.NewAccount(t)
|
|
|
|
cnt := dummyContainer(acc)
|
2021-10-11 07:42:14 +00:00
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
balanceMint(t, cBal, acc, containerFee*1, []byte{})
|
|
|
|
c.Invoke(t, stackitem.Null{}, "put", cnt.value, cnt.sig, cnt.pub, cnt.token)
|
2021-10-11 07:42:14 +00:00
|
|
|
|
|
|
|
t.Run("missing container", func(t *testing.T) {
|
2021-10-26 11:28:14 +00:00
|
|
|
id := cnt.id
|
2021-10-11 07:42:14 +00:00
|
|
|
id[0] ^= 0xFF
|
2021-10-26 11:28:14 +00:00
|
|
|
c.InvokeFail(t, container.NotFoundError, "owner", id[:])
|
2021-10-11 07:42:14 +00:00
|
|
|
})
|
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
owner, _ := base58.Decode(address.Uint160ToString(acc.ScriptHash()))
|
|
|
|
c.Invoke(t, stackitem.NewBuffer(owner), "owner", cnt.id[:])
|
2021-10-11 07:42:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestContainerGet(t *testing.T) {
|
2021-10-26 11:28:14 +00:00
|
|
|
c, cBal := newContainerInvoker(t)
|
|
|
|
|
|
|
|
acc := c.NewAccount(t)
|
|
|
|
cnt := dummyContainer(acc)
|
2021-10-11 07:42:14 +00:00
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
balanceMint(t, cBal, acc, containerFee*1, []byte{})
|
2021-10-11 07:42:14 +00:00
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
c.Invoke(t, stackitem.Null{}, "put", cnt.value, cnt.sig, cnt.pub, cnt.token)
|
2021-10-11 07:42:14 +00:00
|
|
|
|
|
|
|
t.Run("missing container", func(t *testing.T) {
|
2021-10-26 11:28:14 +00:00
|
|
|
id := cnt.id
|
2021-10-11 07:42:14 +00:00
|
|
|
id[0] ^= 0xFF
|
2021-10-26 11:28:14 +00:00
|
|
|
c.InvokeFail(t, container.NotFoundError, "get", id[:])
|
2021-10-11 07:42:14 +00:00
|
|
|
})
|
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
expected := stackitem.NewStruct([]stackitem.Item{
|
|
|
|
stackitem.NewByteArray(cnt.value),
|
|
|
|
stackitem.NewByteArray(cnt.sig),
|
|
|
|
stackitem.NewByteArray(cnt.pub),
|
|
|
|
stackitem.NewByteArray(cnt.token),
|
|
|
|
})
|
|
|
|
c.Invoke(t, expected, "get", cnt.id[:])
|
2021-10-19 07:59:40 +00:00
|
|
|
}
|
2021-10-19 08:23:35 +00:00
|
|
|
|
|
|
|
type eacl struct {
|
|
|
|
value []byte
|
|
|
|
sig []byte
|
|
|
|
pub []byte
|
|
|
|
token []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func dummyEACL(containerID [32]byte) eacl {
|
|
|
|
e := make([]byte, 50)
|
|
|
|
copy(e[6:], containerID[:])
|
|
|
|
return eacl{
|
|
|
|
value: e,
|
|
|
|
sig: randomBytes(64),
|
|
|
|
pub: randomBytes(33),
|
|
|
|
token: randomBytes(42),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContainerSetEACL(t *testing.T) {
|
2021-10-26 11:28:14 +00:00
|
|
|
c, cBal := newContainerInvoker(t)
|
2021-10-19 08:23:35 +00:00
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
acc := c.NewAccount(t)
|
|
|
|
cnt := dummyContainer(acc)
|
|
|
|
balanceMint(t, cBal, acc, containerFee*1, []byte{})
|
2021-10-19 08:23:35 +00:00
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
c.Invoke(t, stackitem.Null{}, "put", cnt.value, cnt.sig, cnt.pub, cnt.token)
|
2021-10-19 08:23:35 +00:00
|
|
|
|
2021-10-11 07:42:14 +00:00
|
|
|
t.Run("missing container", func(t *testing.T) {
|
2021-10-26 11:28:14 +00:00
|
|
|
id := cnt.id
|
2021-10-11 07:42:14 +00:00
|
|
|
id[0] ^= 0xFF
|
|
|
|
e := dummyEACL(id)
|
2021-10-26 11:28:14 +00:00
|
|
|
c.InvokeFail(t, container.NotFoundError, "setEACL", e.value, e.sig, e.pub, e.token)
|
2021-10-11 07:42:14 +00:00
|
|
|
})
|
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
e := dummyEACL(cnt.id)
|
|
|
|
setArgs := []interface{}{e.value, e.sig, e.pub, e.token}
|
|
|
|
cAcc := c.WithSigners(acc)
|
2021-11-29 12:21:40 +00:00
|
|
|
cAcc.InvokeFail(t, common.ErrAlphabetWitnessFailed, "setEACL", setArgs...)
|
2021-10-19 08:23:35 +00:00
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
c.Invoke(t, stackitem.Null{}, "setEACL", setArgs...)
|
2021-10-19 08:23:35 +00:00
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
expected := stackitem.NewStruct([]stackitem.Item{
|
2021-10-19 08:23:35 +00:00
|
|
|
stackitem.NewByteArray(e.value),
|
|
|
|
stackitem.NewByteArray(e.sig),
|
|
|
|
stackitem.NewByteArray(e.pub),
|
|
|
|
stackitem.NewByteArray(e.token),
|
2021-10-26 11:28:14 +00:00
|
|
|
})
|
|
|
|
c.Invoke(t, expected, "eACL", cnt.id[:])
|
2021-10-19 08:23:35 +00:00
|
|
|
}
|