From 1cc44e9974874ec36b45f3270e623b2fd2eefeea Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Wed, 22 Feb 2023 18:15:47 +0400 Subject: [PATCH] container: Test migration Signed-off-by: Leonard Lyubich --- alphabet/migration_test.go | 2 +- container/migration_test.go | 74 ++++++++++++++++++++++++++++- tests/migration/blockchain_local.go | 6 ++- tests/migration/deploy.go | 9 +++- tests/migration/storage.go | 14 +++++- 5 files changed, 99 insertions(+), 6 deletions(-) diff --git a/alphabet/migration_test.go b/alphabet/migration_test.go index aa81f332..d9030f36 100644 --- a/alphabet/migration_test.go +++ b/alphabet/migration_test.go @@ -42,7 +42,7 @@ func TestMigration_TestNet_0_16_0_to_0_17_0(t *testing.T) { migration.AddedSingleItem("proxyScriptHash", proxyContract.BytesBE()), ), SuperiorContracts: []migration.SuperiorContractConfig{ - migration.DeployAndCatchSuperiorContract("netmap", deployCfg.SetArgI(0, false), + migration.DeployAndCatchSuperiorContract("netmap", deployCfg.ReplaceArgI(0, false), func(cfg *migration.UpdateConfig, addr util.Uint160) { cfg.SetArgI(1, addr) }, diff --git a/container/migration_test.go b/container/migration_test.go index 92b95715..c34c267d 100644 --- a/container/migration_test.go +++ b/container/migration_test.go @@ -1 +1,73 @@ -package container +package container_test + +import ( + "math/rand" + "testing" + + "github.com/nspcc-dev/neo-go/pkg/util" + "github.com/nspcc-dev/neofs-contract/tests/migration" +) + +func randContractAddress() (res util.Uint160) { + rand.Read(res[:]) + return +} + +func TestMigration_TestNet_0_16_0_to_0_17_0(t *testing.T) { + const rootDomain = "neofs1" // must not equal to 'neofs' + + deployCfg := migration.DeployWithArguments( + true, // Non-notary mode + util.Uint160{}, // Netmap contract + util.Uint160{}, // Balance contract + util.Uint160{}, // NeoFSID contract + util.Uint160{}, // NNS contract + rootDomain, // NNS root domain + ) + + netmapContract := randContractAddress() + balanceContract := randContractAddress() + neoFSIDContract := randContractAddress() + nnsContract := randContractAddress() + + validUpdCfg := migration.UpdateWithArguments( + false, + netmapContract, + balanceContract, + neoFSIDContract, + nnsContract, + rootDomain, + ) + + migration.TestRemoteStorageMigration(t, "https://rpc1.morph.t5.fs.neo.org:51331", "container", migration.Options{ + OnNNSDeploy: func(cfg *migration.DeployConfig, nnsContract util.Uint160) { + cfg.SetArgI(4, nnsContract) + }, + DeployConfig: deployCfg, + UpdateFailures: []migration.UpdateFailScenario{ + migration.InvalidConfigFailure("update to non-notary mode is not supported anymore", + validUpdCfg.ReplaceArgI(0, true), + ), + }, + ValidUpdateConfig: validUpdCfg, + UpdateModel: migration.MultiGroupUpdateModel( + migration.RemovedSingleItem("notary"), + migration.PersistedSingleItem("netmapScriptHash"), + migration.PersistedSingleItem("balanceScriptHash"), + migration.PersistedSingleItem("identityScriptHash"), + migration.PersistedSingleItem("nnsScriptHash"), + migration.PersistedSingleItem("nnsRoot"), + migration.PersistedItemsWithPrefix("o"), + migration.PersistedItemsWithPrefix("x"), + migration.PersistedItemsWithPrefix("cnr"), + migration.PersistedItemsWithPrefix("est"), + migration.PersistedItemsWithPrefix("nnsHasAlias"), + migration.ReplacedItemsWithKeyLength(32, func(prevKey []byte) []byte { + return append([]byte{'x'}, prevKey...) + }), + migration.ReplacedItemsWithKeyLength(25+32, func(prevKey []byte) []byte { + return append([]byte{'o'}, prevKey...) + }), + ), + }) +} diff --git a/tests/migration/blockchain_local.go b/tests/migration/blockchain_local.go index 53546df2..9fce7a8a 100644 --- a/tests/migration/blockchain_local.go +++ b/tests/migration/blockchain_local.go @@ -72,7 +72,7 @@ type localBlockchain struct { // testing.TB. // // See also nns.Register. -func newLocalBlockChain(tb testing.TB, nnsSourcesDir string) *localBlockchain { +func newLocalBlockChain(tb testing.TB, nnsSourcesDir string, onNNSDeploy func(util.Uint160)) *localBlockchain { memStore := storage.NewMemoryStore() useDefaultConfig := func(*config.Blockchain) {} @@ -86,6 +86,10 @@ func newLocalBlockChain(tb testing.TB, nnsSourcesDir string) *localBlockchain { nnsContract := res.deployContract(res.compileContractSourceCode(nnsSourcesDir), true, DeployConfig{}) + if onNNSDeploy != nil { + onNNSDeploy(nnsContract) + } + nnsContractInvoker := res.contractInvoker(nnsContract) nnsContractInvoker.InvokeAndCheck(tb, checkSingleTrueInStack, "register", diff --git a/tests/migration/deploy.go b/tests/migration/deploy.go index 516682f4..2804a787 100644 --- a/tests/migration/deploy.go +++ b/tests/migration/deploy.go @@ -18,8 +18,13 @@ func DeployWithArguments(args ...interface{}) DeployConfig { } } -// SetArgI returns copy of the DeployConfig with changed i-th argument. -func (x DeployConfig) SetArgI(i int, v interface{}) (res DeployConfig) { +// SetArgI overrides i-th argument with the given value. +func (x *DeployConfig) SetArgI(i int, v interface{}) { + x.args[i] = v +} + +// ReplaceArgI returns copy of the DeployConfig with changed i-th argument. +func (x DeployConfig) ReplaceArgI(i int, v interface{}) (res DeployConfig) { res.args = make([]interface{}, len(x.args)) copy(res.args, x.args) res.args[i] = v diff --git a/tests/migration/storage.go b/tests/migration/storage.go index a83a0e14..d5af3cc5 100644 --- a/tests/migration/storage.go +++ b/tests/migration/storage.go @@ -4,6 +4,7 @@ import ( "bytes" "testing" + "github.com/nspcc-dev/neo-go/pkg/util" "github.com/stretchr/testify/require" ) @@ -13,6 +14,10 @@ type Options struct { // overriding the default one in step 1. NNSSourceCodeDir string + // OnNNSDeploy allows to modify DeployConfig dynamically after NeoFS NNS + // contract deployment. + OnNNSDeploy func(*DeployConfig, util.Uint160) + // Group of NeoFS contracts on which the tested one depends. If set, all // superior contracts are pre-handled locally before step 3 in order. SuperiorContracts []SuperiorContractConfig @@ -72,7 +77,14 @@ func TestRemoteStorageMigration(tb testing.TB, blockChainRPCEndpoint, contractNa opts.NNSSourceCodeDir = SameLevelFolder("nns") } - bLocal := newLocalBlockChain(tb, opts.NNSSourceCodeDir) + var onNNSDeploy func(util.Uint160) + if opts.OnNNSDeploy != nil { + onNNSDeploy = func(addr util.Uint160) { + opts.OnNNSDeploy(&opts.DeployConfig, addr) + } + } + + bLocal := newLocalBlockChain(tb, opts.NNSSourceCodeDir, onNNSDeploy) // 2. remoteTestContractState := bRemote.getNeoFSContractByName(contractName)