From ca59b54d9f840df45e58ebeb2e7aed6dd58e3505 Mon Sep 17 00:00:00 2001 From: Evgenii Baidakov Date: Fri, 20 Sep 2024 14:44:38 +0400 Subject: [PATCH] contracts: Add some helpers functions for testing purposes. Signed-off-by: Evgenii Baidakov --- tests/deploys.go | 84 +++++++++++++++++++++++++++++++++++++++++++ tests/deploys_test.go | 71 ++++++++++++++++++++++++++++++++++++ 2 files changed, 155 insertions(+) create mode 100644 tests/deploys.go create mode 100644 tests/deploys_test.go diff --git a/tests/deploys.go b/tests/deploys.go new file mode 100644 index 00000000..39f63087 --- /dev/null +++ b/tests/deploys.go @@ -0,0 +1,84 @@ +package tests + +import ( + "testing" + "time" + + "github.com/nspcc-dev/neo-go/pkg/neotest" + "github.com/nspcc-dev/neo-go/pkg/util" + "github.com/nspcc-dev/neofs-contract/rpc/nns" + "github.com/stretchr/testify/require" +) + +type ( + // RegisterContractInNNSParams is a param struct for RegisterContractInNNS func. + RegisterContractInNNSParams struct { + // Name is a contract name. Name must not be a top level domain. For instance: netmap. + // The final registered domain name will be concatenated with `.neofs`. + Name string + ContractHash util.Uint160 + Email string + Refresh int64 + Retry int64 + // Expire in milliseconds + Expire int64 + TTL int64 + } +) + +// NewRegisterContractInNNSParams is a constructor for RegisterContractInNNSParams. +func NewRegisterContractInNNSParams(name string, contractHash util.Uint160) RegisterContractInNNSParams { + return RegisterContractInNNSParams{ + Name: name, + ContractHash: contractHash, + Email: "ops@nspcc.ru", + Refresh: 3600, + Retry: 600, + Expire: int64(365 * 24 * time.Hour / time.Millisecond), // 1 year. + TTL: 3600, + } +} + +func nnsContractInvoker(t *testing.T, e *neotest.Executor) *neotest.ContractInvoker { + nnsHash, err := e.Chain.GetContractScriptHash(1) + require.NoError(t, err) + + return e.CommitteeInvoker(nnsHash) +} + +func getContractHash(t *testing.T, e *neotest.Executor, name string) util.Uint160 { + nnsInv := nnsContractInvoker(t, e) + resolveResult, err := nnsInv.TestInvoke(t, "resolve", name, nns.TXT) + require.NoError(t, err) + + contractHashBytes, err := resolveResult.Pop().Array()[0].TryBytes() + require.NoError(t, err) + + netMapContractHash, err := util.Uint160DecodeStringLE(string(contractHashBytes)) + require.NoError(t, err) + + return netMapContractHash +} + +// RegisterContractInNNS make registration of the contract hash in nns contract. +func RegisterContractInNNS(t *testing.T, e *neotest.Executor, params RegisterContractInNNSParams) { + nnsHash, err := e.Chain.GetContractScriptHash(1) + require.NoError(t, err) + + nnsInv := e.CommitteeInvoker(nnsHash) + + nnsInv.Invoke(t, true, "register", params.Name+".neofs", e.CommitteeHash, params.Email, params.Refresh, params.Retry, params.Email, params.TTL) + nnsInv.Invoke(t, nil, "addRecord", params.Name+".neofs", nns.TXT, params.ContractHash.StringLE()) +} + +// TickEpoch increments epoch value by one. +func TickEpoch(t *testing.T, e *neotest.Executor, signers ...neotest.Signer) { + netMapContractHash := getContractHash(t, e, "netmap.neofs") + netMapInvoker := e.NewInvoker(netMapContractHash, signers...) + + epochResult, err := netMapInvoker.TestInvoke(t, "epoch") + require.NoError(t, err) + + epoch := epochResult.Pop().BigInt().Int64() + netMapInvoker.Invoke(t, nil, "newEpoch", epoch+1) +} diff --git a/tests/deploys_test.go b/tests/deploys_test.go new file mode 100644 index 00000000..bb564169 --- /dev/null +++ b/tests/deploys_test.go @@ -0,0 +1,71 @@ +package tests + +import ( + "testing" + + "github.com/nspcc-dev/neo-go/pkg/core/state" + "github.com/nspcc-dev/neo-go/pkg/neotest" + "github.com/nspcc-dev/neo-go/pkg/neotest/chain" + "github.com/nspcc-dev/neofs-contract/contracts" + "github.com/stretchr/testify/require" +) + +func TestDeploys(t *testing.T) { + bc, acc := chain.NewSingle(t) + e := neotest.NewExecutor(t, bc, acc, acc) + + fsContracts, err := contracts.GetFS() + require.NoError(t, err) + + for _, fsContract := range fsContracts { + switch fsContract.Manifest.Name { + case "NameService": + deployNNS(t, e, fsContract) + case "NeoFS Netmap": + deployNetMap(t, e, fsContract, acc) + } + } +} + +func deployNNS(t *testing.T, e *neotest.Executor, fsContract contracts.Contract) { + nnsContract := neotest.Contract{ + Hash: state.CreateContractHash(e.CommitteeHash, fsContract.NEF.Checksum, fsContract.Manifest.Name), + NEF: &fsContract.NEF, + Manifest: &fsContract.Manifest, + } + + e.DeployContract(t, &nnsContract, []any{[]any{[]any{"neofs", "ops@nspcc.io"}}}) +} + +func deployNetMap(t *testing.T, e *neotest.Executor, fsContract contracts.Contract, signer neotest.Signer) { + netMapContract := neotest.Contract{ + Hash: state.CreateContractHash(e.CommitteeHash, fsContract.NEF.Checksum, fsContract.Manifest.Name), + NEF: &fsContract.NEF, + Manifest: &fsContract.Manifest, + } + + netMapContractDeployData := []any{ + false, // notaryDisabled is false + nil, + nil, + nil, + []any{}, + } + + e.DeployContract(t, &netMapContract, netMapContractDeployData) + + params := NewRegisterContractInNNSParams("netmap", netMapContract.Hash) + RegisterContractInNNS(t, e, params) + + // check the contract registered in nns. + netMapContractHash := getContractHash(t, e, "netmap.neofs") + require.Equal(t, netMapContract.Hash, netMapContractHash) + + netMapInvoker := e.NewInvoker(netMapContract.Hash, signer) + + var epoch int64 + netMapInvoker.Invoke(t, epoch, "epoch") + + TickEpoch(t, e, signer) + netMapInvoker.Invoke(t, epoch+1, "epoch") +}