-
Notifications
You must be signed in to change notification settings - Fork 17
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
smallhive
wants to merge
1
commit into
master
Choose a base branch
from
contracts-tests-helpers
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+154
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,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) | ||||
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()) | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||||
} |
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", "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") | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
neofs-contract/rpc/nns/hashes.go
Line 34 in db6dbb9