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

Rewrite owned intersection types in migration #3118

Merged
merged 6 commits into from
Feb 22, 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
526 changes: 258 additions & 268 deletions migrations/entitlements/migration.go

Large diffs are not rendered by default.

1,285 changes: 645 additions & 640 deletions migrations/entitlements/migration_test.go

Large diffs are not rendered by default.

15 changes: 10 additions & 5 deletions migrations/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,18 +252,23 @@ func (m *StorageMigration) MigrateNestedValue(
// Read the keys first, so the iteration wouldn't be affected
// by the modification of the nested values.
var existingKeysAndValues []keyValuePair
dictionary.Iterate(m.interpreter, func(key, value interpreter.Value) (resume bool) {

iterator := dictionary.Iterator()

for {
key, value := iterator.Next(nil)
if key == nil {
break
}

existingKeysAndValues = append(
existingKeysAndValues,
keyValuePair{
key: key,
value: value,
},
)

// continue iteration
return true
})
}

for _, existingKeyAndValue := range existingKeysAndValues {
existingKey := existingKeyAndValue.key
Expand Down
146 changes: 0 additions & 146 deletions migrations/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import (
"github.com/onflow/cadence/runtime/common"
"github.com/onflow/cadence/runtime/interpreter"
"github.com/onflow/cadence/runtime/stdlib"
"github.com/onflow/cadence/runtime/tests/checker"
. "github.com/onflow/cadence/runtime/tests/runtime_utils"
"github.com/onflow/cadence/runtime/tests/utils"
)
Expand Down Expand Up @@ -948,148 +947,3 @@ func TestContractMigration(t *testing.T) {
value,
)
}

func TestMigrationPanics(t *testing.T) {

t.Parallel()

t.Run("composite dictionary", func(t *testing.T) {
t.Parallel()

ledger := NewTestLedger(nil, nil)
account := common.Address{0x42}

t.Run("prepare", func(t *testing.T) {

fooContractLocation := common.NewAddressLocation(nil, account, "Foo")

storage := runtime.NewStorage(ledger, nil)
locationRange := interpreter.EmptyLocationRange

contractChecker, err := checker.ParseAndCheckWithOptions(t, `
access(all) contract Foo {
access(all) struct Bar {}
}`,
checker.ParseAndCheckOptions{
Location: fooContractLocation,
},
)
require.NoError(t, err)

inter, err := interpreter.NewInterpreter(
nil,
utils.TestLocation,
&interpreter.Config{
Storage: storage,
AtreeValueValidationEnabled: false,
AtreeStorageValidationEnabled: false,
ImportLocationHandler: func(inter *interpreter.Interpreter, location common.Location) interpreter.Import {
program := interpreter.ProgramFromChecker(contractChecker)
subInterpreter, err := inter.NewSubInterpreter(program, location)
if err != nil {
panic(err)
}

return interpreter.InterpreterImport{
Interpreter: subInterpreter,
}
},
},
)
require.NoError(t, err)

const fooBarQualifiedIdentifier = "Foo.Bar"

dictionaryAnyStructStaticType :=
interpreter.NewDictionaryStaticType(
nil,
interpreter.PrimitiveStaticTypeString,
interpreter.NewCompositeStaticTypeComputeTypeID(
nil,
fooContractLocation,
fooBarQualifiedIdentifier,
),
)

storedValue := interpreter.NewDictionaryValue(
inter,
emptyLocationRange,
dictionaryAnyStructStaticType,
)

transferredValue := storedValue.Transfer(
inter,
locationRange,
atree.Address(account),
false,
nil,
nil,
)

inter.WriteStored(
account,
common.PathDomainStorage.Identifier(),
interpreter.StringStorageMapKey("dictionary_value"),
transferredValue,
)

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

t.Run("migrate", func(t *testing.T) {
storage := runtime.NewStorage(ledger, nil)

inter, err := interpreter.NewInterpreter(
nil,
utils.TestLocation,
&interpreter.Config{
Storage: storage,
AtreeValueValidationEnabled: false,
AtreeStorageValidationEnabled: false,
ImportLocationHandler: func(_ *interpreter.Interpreter, _ common.Location) interpreter.Import {
// During the migration, treat the imported `Foo` contract as un-migrated.
panic(errors.New("type checking error in Foo"))
},
},
)
require.NoError(t, err)

migration := NewStorageMigration(inter, storage)

reporter := newTestReporter()

migration.Migrate(
&AddressSliceIterator{
Addresses: []common.Address{
account,
},
},
migration.NewValueMigrationsPathMigrator(
reporter,
testStringMigration{},
testInt8Migration{},
testCapMigration{},
),
)

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

expectedErrors := map[struct {
interpreter.StorageKey
interpreter.StorageMapKey
}][]string{
{
StorageKey: interpreter.StorageKey{
Key: common.PathDomainStorage.Identifier(),
Address: account,
},
StorageMapKey: interpreter.StringStorageMapKey("dictionary_value"),
}: {"StorageMigration"},
}

assert.Equal(t, expectedErrors, reporter.errored)
})
})
}
Loading
Loading