Skip to content

Commit

Permalink
neofsid: Test migration
Browse files Browse the repository at this point in the history
Signed-off-by: Leonard Lyubich <ctulhurider@gmail.com>
  • Loading branch information
cthulhu-rider committed Feb 22, 2023
1 parent 1cc44e9 commit bce47f4
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 11 deletions.
1 change: 1 addition & 0 deletions alphabet/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
)

func TestMigration_TestNet_0_16_0_to_0_17_0(t *testing.T) {
t.Skip("not working until neo-go#2926 will be resolved")
deployCfg := migration.DeployWithArguments(
true, // Non-notary mode
util.Uint160{}, // Netmap contract
Expand Down
1 change: 1 addition & 0 deletions audit/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
)

func TestMigration_TestNet_0_16_0_to_0_17_0(t *testing.T) {
t.Skip("not working until neo-go#2926 will be resolved")
deployCfg := migration.DeployWithArguments(
true, // Non-notary mode
util.Uint160{}, // Netmap contract
Expand Down
1 change: 1 addition & 0 deletions balance/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
)

func TestMigration_TestNet_0_16_0_to_0_17_0(t *testing.T) {
t.Skip("not working until neo-go#2926 will be resolved")
deployCfg := migration.DeployWithArguments(
true, // Non-notary mode
util.Uint160{}, // Netmap contract
Expand Down
1 change: 1 addition & 0 deletions container/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func randContractAddress() (res util.Uint160) {
}

func TestMigration_TestNet_0_16_0_to_0_17_0(t *testing.T) {
t.Skip("not working until neo-go#2926 will be resolved")
const rootDomain = "neofs1" // must not equal to 'neofs'

deployCfg := migration.DeployWithArguments(
Expand Down
39 changes: 39 additions & 0 deletions neofsid/migration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package neofsid_test

import (
"testing"

"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neofs-contract/tests/migration"
)

func TestMigration_TestNet_0_16_0_to_0_17_0(t *testing.T) {
t.Skip("not working until neo-go#2926 will be resolved")
deployCfg := migration.DeployWithArguments(
true, // Non-notary mode
util.Uint160{}, // Netmap contract
util.Uint160{}, // Container contract
)

validUpdCfg := migration.UpdateWithArguments(
false,
util.Uint160{},
util.Uint160{},
)

migration.TestRemoteStorageMigration(t, "https://rpc1.morph.t5.fs.neo.org:51331", "neofsid", migration.Options{
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.PersistedSingleItem("netmapScriptHash"),
migration.RemovedSingleItem("containerScriptHash"),
migration.RemovedSingleItem("notary"),
migration.PersistedItemsWithPrefix("o"),
),
})
}
35 changes: 32 additions & 3 deletions tests/migration/blockchain_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ type Contract interface {
UnregisterContractInNNS(name string)
// iterates over all storage items of the tested contract.
iterateStorage(func(key, value []byte))
// saves given value by specified key of the tested contract storage.
putToStorage(key, value []byte)
// reads value by specified key from the tested contract storage.
getFromStorage(key []byte) []byte
}

// ContractFunc is a function over Contract.
Expand All @@ -52,7 +56,20 @@ func newLocalContract(b *localBlockchain, addr util.Uint160) Contract {
}
}

// iterate iterates over all storage items bound to the underlying contract ID.
// putToStorage saves value bound to the underlying contract ID in the storage
// by specified key.
func (x *contract) putToStorage(key, value []byte) {
x.localBlockchain.putToStorage(x.contractID, key, value)
}

// getFromStorage reads value bound to the underlying contract ID from the
// storage by specified key.
func (x *contract) getFromStorage(key []byte) []byte {
return x.localBlockchain.getFromStorage(x.contractID, key)
}

// iterate iterates over all saved values bound to the underlying contract ID in
// the storage.
func (x *contract) iterateStorage(f func(key, value []byte)) {
x.localBlockchain.iterateStorage(x.contractID, f)
}
Expand Down Expand Up @@ -133,8 +150,20 @@ func (x *localBlockchain) resolveContractIDFromAddress(addr util.Uint160) int32
return contractState.ID
}

// putToStorage stores given value by specified key in storage of the contract
// referenced by the given ID.
func (x *localBlockchain) putToStorage(contractID int32, key, value []byte) {
// FIXME: do exact action after neo-go#2926
}

// getFromStorage reads value by the given key from storage of the contract
// referenced by the given ID.
func (x *localBlockchain) getFromStorage(contractID int32, key []byte) []byte {
return x.exec.Chain.GetStorageItem(contractID, key)
}

// iterateStorage iterates over all storage items of the contract referenced by
// the given address.
// the given ID.
func (x *localBlockchain) iterateStorage(contractID int32, f func(key, value []byte)) {
storageKey := contractStorageKey(native.ManagementContractID, native.MakeContractKey(x.exec.ContractHash(x.tb, contractID)))

Expand Down Expand Up @@ -162,7 +191,7 @@ func (x *localBlockchain) iterateStorage(contractID int32, f func(key, value []b
}

for i := range kvs {
f(kvs[i].Key, kvs[i].Value)
f(unwrapStorageKey(kvs[i].Key), kvs[i].Value)
}
}

Expand Down
20 changes: 12 additions & 8 deletions tests/migration/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ func TestRemoteStorageMigration(tb testing.TB, blockChainRPCEndpoint, contractNa

bRemote.iterateContractStorage(remoteTestContractState.Hash, func(item kv) {
pastItems = append(pastItems, item)
testContractService.putToStorage(item.k, item.v)
})

// 4.
Expand Down Expand Up @@ -134,15 +135,18 @@ func TestRemoteStorageMigration(tb testing.TB, blockChainRPCEndpoint, contractNa
contractUpd.try(opts.ValidUpdateConfig, "")

// 6.
testContractService.iterateStorage(func(key, value []byte) {
found := false
for i := 0; !found && i < len(newItems); i++ {
found = bytes.Equal(key, newItems[i].k)
if found {
require.Equal(tb, newItems[i].v, value, "got another value of the item '%s'", key)
for i := range newItems {
postUpdVal := testContractService.getFromStorage(newItems[i].k)
require.Equal(tb, newItems[i].v, postUpdVal, "got another post-update value of the item '%s'", newItems[i].k)
}

testContractService.iterateStorage(func(key, _ []byte) {
for i := 0; i < len(newItems); i++ {
if bytes.Equal(key, newItems[i].k) {
return
}
}

require.True(tb, found, "detected unknown item after update '%s', did you forget to include it in the model?", key)
require.Fail(tb, "detected unknown item after update, did you forget to include it in the model?",
"key '%s'", key)
})
}
5 changes: 5 additions & 0 deletions tests/migration/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ func contractStorageKey(id int32, key []byte) []byte {
return res
}

// contractStorageKey is a reverse operation to contractStorageKey.
func unwrapStorageKey(globalKey []byte) []byte {
return globalKey[4:]
}

// SameLevelFolder returns filesystem path to the named directory which is
// same-level to the current one.
func SameLevelFolder(name string) string {
Expand Down

0 comments on commit bce47f4

Please sign in to comment.