-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
contracts: Add some helpers functions for testing purposes.
Signed-off-by: Evgenii Baidakov <[email protected]>
- Loading branch information
Showing
2 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. Domain will be registered in the `.neofs` top-level domain. | ||
// You custom contract names must not collide with system contract names. See rpc/nns/names.go. | ||
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: "[email protected]", | ||
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(nns.ID) | ||
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 := nns.AddressFromRecord(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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", "[email protected]"}}}) | ||
} | ||
|
||
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") | ||
} |