[#135] frostfsid: Add migration tests

Signed-off-by: Alexander Chuprov <a.chuprov@yadro.com>
This commit is contained in:
Alexander Chuprov 2024-12-05 17:14:56 +03:00
parent c102a44c56
commit 8b32e7c2e7
Signed by: achuprov
GPG key ID: 2D916FFD803B0EDD
7 changed files with 107 additions and 0 deletions

2
.gitignore vendored
View file

@ -2,6 +2,8 @@
*.nef
*.manifest.json
config.json
!tests/testdata/**/*.nef
!tests/testdata/**/config.json
/vendor/
.idea
/bin/

View file

@ -2,6 +2,7 @@ package tests
import (
"errors"
"fmt"
"path"
"testing"
@ -649,6 +650,52 @@ func TestAdditionalKeyFromPrimarySubject(t *testing.T) {
invoker.Invoke(t, stackitem.Null{}, addSubjectKeyMethod, subjAKeyAddr, subjDPrimaryKey.PublicKey().Bytes())
}
const frostfsidContractName = "frostfsid"
func TestFrostfsid_Migration(t *testing.T) {
e := newExecutor(t)
acc, err := wallet.NewAccount()
require.NoError(t, err)
args := make([]any, 5)
args[0] = acc.ScriptHash()
c := loadCompiledContract(t, e.CommitteeHash, fmt.Sprintf(contractPathFormat, v0_19_2, frostfsidContractName, frostfsidContractName),
fmt.Sprintf(manifestPathFormat, v0_19_2, frostfsidContractName))
e.DeployContract(t, c, args)
updateContract(t, c, e, c.Hash, args)
inv := testFrostFSIDInvoker{
e: e,
contractHash: c.Hash,
owner: acc,
}
newSigner(t, e.CommitteeInvoker(c.Hash), acc)
require.NoError(t, err)
invoker := inv.OwnerInvoker()
subjAPrimaryKey, err := keys.NewPrivateKey()
require.NoError(t, err)
subjBPrimaryKey, err := keys.NewPrivateKey()
require.NoError(t, err)
subjBKeyAddr := subjBPrimaryKey.PublicKey().GetScriptHash()
invoker.Invoke(t, stackitem.Null{}, createSubjectMethod, defaultNamespace, subjAPrimaryKey.PublicKey().Bytes())
invoker.Invoke(t, stackitem.Null{}, createSubjectMethod, defaultNamespace, subjBPrimaryKey.PublicKey().Bytes())
invoker.Invoke(t, stackitem.Null{}, addSubjectKeyMethod, subjBKeyAddr, subjAPrimaryKey.PublicKey().Bytes())
c1 := loadCompiledContract(t, e.CommitteeHash, fmt.Sprintf(contractPathFormat, v0_21_1, frostfsidContractName, frostfsidContractName),
fmt.Sprintf(manifestPathFormat, v0_21_1, frostfsidContractName))
updateContractFail(t, c1, e, c.Hash, args, "frostfsid contract contains duplicate keys")
invoker.Invoke(t, stackitem.Null{}, deleteSubjectMethod, subjBKeyAddr)
updateContract(t, c1, e, c.Hash, args)
updateContract(t, c1, e, c.Hash, args)
}
func checkPublicKeyResult(t *testing.T, s *vm.Stack, err error, key *keys.PrivateKey) {
if key == nil {
require.ErrorContains(t, err, "not found")

File diff suppressed because one or more lines are too long

Binary file not shown.

File diff suppressed because one or more lines are too long

Binary file not shown.

View file

@ -7,6 +7,7 @@ import (
"testing"
"time"
json "github.com/nspcc-dev/go-ordered-json"
"github.com/nspcc-dev/neo-go/pkg/config"
"github.com/nspcc-dev/neo-go/pkg/consensus"
"github.com/nspcc-dev/neo-go/pkg/core"
@ -20,6 +21,8 @@ import (
"github.com/nspcc-dev/neo-go/pkg/rpcclient"
"github.com/nspcc-dev/neo-go/pkg/services/rpcsrv"
"github.com/nspcc-dev/neo-go/pkg/services/stateroot"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/nef"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
@ -226,3 +229,56 @@ func runRPC(ctx context.Context, t *testing.T, chain *core.Blockchain, walletPat
}
}
}
func loadCompiledContract(t testing.TB, sender util.Uint160, nefPath, manifestPath string) *neotest.Contract {
nefBytes, err := os.ReadFile(nefPath)
require.NoError(t, err)
f, err := nef.FileFromBytes(nefBytes)
require.NoError(t, err)
manifestBytes, err := os.ReadFile(manifestPath)
require.NoError(t, err)
m := new(manifest.Manifest)
err = json.Unmarshal(manifestBytes, m)
require.NoError(t, err)
hash := state.CreateContractHash(sender, f.Checksum, m.Name)
return &neotest.Contract{
Hash: hash,
NEF: &f,
Manifest: m,
}
}
const contractPathFormat = "testdata/migration/%s/%s/%s_contract.nef"
const manifestPathFormat = "testdata/migration/%s/%s/config.json"
const v0_19_2 = "0.19.2"
const v0_21_1 = "0.21.1"
func updateContract(t testing.TB, c *neotest.Contract, e *neotest.Executor, contractHash util.Uint160, data any) {
neb, err := c.NEF.Bytes()
require.NoError(t, err)
rawManifest, err := json.Marshal(c.Manifest)
require.NoError(t, err)
inv := e.NewInvoker(contractHash, e.Committee)
inv.Invoke(t, nil, "update", neb, rawManifest, data)
}
func updateContractFail(t testing.TB, c *neotest.Contract, e *neotest.Executor, contractHash util.Uint160, data any, faultException string) {
neb, err := c.NEF.Bytes()
require.NoError(t, err)
rawManifest, err := json.Marshal(c.Manifest)
require.NoError(t, err)
inv := e.NewInvoker(contractHash, e.Committee)
inv.InvokeFail(t, faultException, "update", neb, rawManifest, data)
}