Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some helpers functions for testing purposes. #435

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions tests/deploys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package tests

import (
"testing"
"time"

"github.com/nspcc-dev/neo-go/pkg/neotest"
"github.com/nspcc-dev/neo-go/pkg/util"
"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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think @roman-khimov prefers

func InferHash(sg ContractStateGetter) (util.Uint160, error) {
or at least const from this package

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, 16)
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", 16, params.ContractHash.StringLE())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we use

?

}

// 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)
}
71 changes: 71 additions & 0 deletions tests/deploys_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
Loading