Skip to content
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

Generalize static type migrations #3033

Merged
merged 2 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions migrations/entitlements/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (

"github.com/onflow/cadence"
"github.com/onflow/cadence/migrations"
"github.com/onflow/cadence/migrations/account_type"
"github.com/onflow/cadence/migrations/statictypes"
"github.com/onflow/cadence/runtime"
"github.com/onflow/cadence/runtime/ast"
"github.com/onflow/cadence/runtime/common"
Expand Down Expand Up @@ -1935,7 +1935,7 @@ func TestConvertMigratedAccountTypes(t *testing.T) {
),
)

newValue, err := account_type.NewAccountTypeMigration().Migrate(
newValue, err := statictypes.NewStaticTypeMigration().Migrate(
interpreter.AddressPath{
Address: common.ZeroAddress,
Path: interpreter.EmptyPathValue,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* limitations under the License.
*/

package account_type
package statictypes

import (
"fmt"
Expand Down Expand Up @@ -61,7 +61,7 @@ func (t *testReporter) Error(
) {
}

func TestTypeValueMigration(t *testing.T) {
func TestAccountTypeInTypeValueMigration(t *testing.T) {
t.Parallel()

account := common.Address{0x42}
Expand Down Expand Up @@ -370,7 +370,7 @@ func TestTypeValueMigration(t *testing.T) {
},
migration.NewValueMigrationsPathMigrator(
reporter,
NewAccountTypeMigration(),
NewStaticTypeMigration(),
),
)

Expand Down Expand Up @@ -445,7 +445,7 @@ func storeTypeValue(
)
}

func TestNestedTypeValueMigration(t *testing.T) {
func TestAccountTypeInNestedTypeValueMigration(t *testing.T) {
t.Parallel()

account := common.Address{0x42}
Expand Down Expand Up @@ -694,7 +694,7 @@ func TestNestedTypeValueMigration(t *testing.T) {
},
migration.NewValueMigrationsPathMigrator(
nil,
NewAccountTypeMigration(),
NewStaticTypeMigration(),
),
)

Expand Down Expand Up @@ -726,7 +726,7 @@ func TestNestedTypeValueMigration(t *testing.T) {
}
}

func TestValuesWithStaticTypeMigration(t *testing.T) {
func TestMigratingValuesWithAccountStaticType(t *testing.T) {

t.Parallel()

Expand Down Expand Up @@ -840,7 +840,7 @@ func TestValuesWithStaticTypeMigration(t *testing.T) {
},
migration.NewValueMigrationsPathMigrator(
nil,
NewAccountTypeMigration(),
NewStaticTypeMigration(),
),
)

Expand Down Expand Up @@ -874,7 +874,7 @@ func TestValuesWithStaticTypeMigration(t *testing.T) {

var testAddress = common.Address{0x42}

func TestRehash(t *testing.T) {
func TestAccountTypeRehash(t *testing.T) {

t.Parallel()

Expand Down Expand Up @@ -981,7 +981,7 @@ func TestRehash(t *testing.T) {
},
migration.NewValueMigrationsPathMigrator(
reporter,
NewAccountTypeMigration(),
NewStaticTypeMigration(),
),
)

Expand Down
214 changes: 214 additions & 0 deletions migrations/statictypes/composite_type_migration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
/*
* Cadence - The resource-oriented smart contract programming language
*
* Copyright Dapper Labs, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package statictypes

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/onflow/cadence/migrations"
"github.com/onflow/cadence/runtime"
"github.com/onflow/cadence/runtime/common"
"github.com/onflow/cadence/runtime/errors"
"github.com/onflow/cadence/runtime/interpreter"
. "github.com/onflow/cadence/runtime/tests/runtime_utils"
"github.com/onflow/cadence/runtime/tests/utils"
)

func TestCompositeAndInterfaceTypeMigration(t *testing.T) {
t.Parallel()

pathDomain := common.PathDomainPublic

type testCase struct {
storedType interpreter.StaticType
expectedType interpreter.StaticType
}

newCompositeType := func() interpreter.StaticType {
return interpreter.NewCompositeStaticType(
nil,
nil,
"Bar",
common.NewTypeIDFromQualifiedName(
nil,
fooAddressLocation,
fooBarQualifiedIdentifier,
),
)
}

newInterfaceType := func() interpreter.StaticType {
return interpreter.NewInterfaceStaticType(
nil,
nil,
"Baz",
common.NewTypeIDFromQualifiedName(
nil,
fooAddressLocation,
fooBazQualifiedIdentifier,
),
)
}

testCases := map[string]testCase{
// base cases
"compositeToInterface": {
storedType: newCompositeType(),
expectedType: newInterfaceType(),
},
"interfaceToComposite": {
storedType: newInterfaceType(),
expectedType: newCompositeType(),
},
// optional
"optional": {
storedType: interpreter.NewOptionalStaticType(nil, newInterfaceType()),
expectedType: interpreter.NewOptionalStaticType(nil, newCompositeType()),
},
// array
"array": {
storedType: interpreter.NewConstantSizedStaticType(nil, newInterfaceType(), 3),
expectedType: interpreter.NewConstantSizedStaticType(nil, newCompositeType(), 3),
},
// dictionary
"dictionary": {
storedType: interpreter.NewDictionaryStaticType(nil, newInterfaceType(), newInterfaceType()),
expectedType: interpreter.NewDictionaryStaticType(nil, newCompositeType(), newCompositeType()),
},
}

// Store values

ledger := NewTestLedger(nil, nil)
storage := runtime.NewStorage(ledger, nil)

inter, err := interpreter.NewInterpreter(
nil,
utils.TestLocation,
&interpreter.Config{
Storage: storage,
AtreeValueValidationEnabled: false,
AtreeStorageValidationEnabled: true,
},
)
require.NoError(t, err)

for name, testCase := range testCases {
storeTypeValue(
inter,
testAddress,
pathDomain,
name,
testCase.storedType,
)
}

err = storage.Commit(inter, true)
require.NoError(t, err)

// Migrate

migration := migrations.NewStorageMigration(inter, storage)

reporter := newTestReporter()

barStaticType := newCompositeType()
bazStaticType := newInterfaceType()

migration.Migrate(
&migrations.AddressSliceIterator{
Addresses: []common.Address{
testAddress,
},
},
migration.NewValueMigrationsPathMigrator(
reporter,
NewStaticTypeMigration().
WithCompositeTypeConverter(
func(staticType *interpreter.CompositeStaticType) interpreter.StaticType {
if staticType.Equal(barStaticType) {
return bazStaticType
} else {
panic(errors.NewUnreachableError())
}
},
).
WithInterfaceTypeConverter(
func(staticType *interpreter.InterfaceStaticType) interpreter.StaticType {
if staticType.Equal(bazStaticType) {
return barStaticType
} else {
panic(errors.NewUnreachableError())
}
},
),
),
)

err = migration.Commit()
require.NoError(t, err)

// Check reported migrated paths
for identifier, test := range testCases {
addressPath := interpreter.AddressPath{
Address: testAddress,
Path: interpreter.PathValue{
Domain: pathDomain,
Identifier: identifier,
},
}

if test.expectedType == nil {
assert.NotContains(t, reporter.migratedPaths, addressPath)
} else {
assert.Contains(t, reporter.migratedPaths, addressPath)
}
}

// Assert the migrated values.
// Traverse through the storage and see if the values are updated now.

storageMap := storage.GetStorageMap(testAddress, pathDomain.Identifier(), false)
require.NotNil(t, storageMap)
require.Greater(t, storageMap.Count(), uint64(0))

iterator := storageMap.Iterator(inter)

for key, value := iterator.Next(); key != nil; key, value = iterator.Next() {
identifier := string(key.(interpreter.StringAtreeValue))

t.Run(identifier, func(t *testing.T) {
testCase, ok := testCases[identifier]
require.True(t, ok)

var expectedType interpreter.StaticType
if testCase.expectedType != nil {
expectedType = testCase.expectedType
} else {
expectedType = testCase.storedType
}

expectedValue := interpreter.NewTypeValue(nil, expectedType)
utils.AssertValuesEqual(t, inter, expectedValue, value)
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* limitations under the License.
*/

package account_type
package statictypes

import (
"github.com/onflow/cadence/runtime/common"
Expand Down
Loading
Loading