Skip to content

Commit

Permalink
contracts: Add some helpers functions for testing purposes.
Browse files Browse the repository at this point in the history
Signed-off-by: Evgenii Baidakov <evgenii@nspcc.io>
  • Loading branch information
smallhive committed Sep 20, 2024
1 parent db6dbb9 commit 5b68028
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions contracts/deploys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package contracts

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 (
// RegContractNNSParams is a param struct for RegContractNNS func.
RegContractNNSParams 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
}
)

const (
msPerYear = 365 * 24 * time.Hour / time.Millisecond
)

// NewRegContractNNSParams is a constructor for RegContractNNSParams.
func NewRegContractNNSParams(name string, contractHash util.Uint160) RegContractNNSParams {
return RegContractNNSParams{
Name: name,
ContractHash: contractHash,
Email: "ops@nspcc.ru",
Refresh: 3600,
Retry: 600,
Expire: int64(10 * msPerYear),
TTL: 3600,
}
}

// RegContractNNS make registration of the contract hash in nns contract.
func RegContractNNS(t *testing.T, e *neotest.Executor, params RegContractNNSParams) {
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())
}

// TickEpoch increase epoch value by one.
func TickEpoch(t *testing.T, e *neotest.Executor, signers ...neotest.Signer) {
nnsHash, err := e.Chain.GetContractScriptHash(1)
require.NoError(t, err)

nnsInv := e.CommitteeInvoker(nnsHash)
resolveResult, err := nnsInv.TestInvoke(t, "resolve", "netmap.neofs", 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)

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)
}

0 comments on commit 5b68028

Please sign in to comment.