Skip to content

Commit

Permalink
Merge pull request #3383 from onflow/bastian/update-atree-register-in…
Browse files Browse the repository at this point in the history
…lining-v1.0-5
  • Loading branch information
turbolent authored May 30, 2024
2 parents a187365 + 8b6f720 commit a1aa985
Show file tree
Hide file tree
Showing 6 changed files with 302 additions and 44 deletions.
215 changes: 215 additions & 0 deletions migrations/entitlements/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3518,3 +3518,218 @@ func TestIntersectionTypeWithIntersectionLegacyType(t *testing.T) {
require.Equal(t, expectedType, typeValue.Type)
})()
}

func TestUseAfterMigrationFailure(t *testing.T) {

t.Parallel()

locationRange := interpreter.EmptyLocationRange

ledger := NewTestLedger(nil, nil)

storageMapKey := interpreter.StringStorageMapKey("dict")
newTestValue := func() interpreter.Value {
return interpreter.NewUnmeteredStringValue("test")
}

const fooBarQualifiedIdentifier = "Foo.Bar"
testAddress := common.Address{0x42}
fooAddressLocation := common.NewAddressLocation(nil, testAddress, "Foo")

newStorageAndInterpreter := func(t *testing.T) (*runtime.Storage, *interpreter.Interpreter) {
storage := runtime.NewStorage(ledger, nil)
inter, err := interpreter.NewInterpreter(
nil,
utils.TestLocation,
&interpreter.Config{
Storage: storage,
// NOTE: disabled, because encoded and decoded values are expected to not match
AtreeValueValidationEnabled: false,
AtreeStorageValidationEnabled: true,
},
)
require.NoError(t, err)

return storage, inter
}

newCompositeType := func() *interpreter.CompositeStaticType {
return interpreter.NewCompositeStaticType(
nil,
fooAddressLocation,
fooBarQualifiedIdentifier,
common.NewTypeIDFromQualifiedName(
nil,
fooAddressLocation,
fooBarQualifiedIdentifier,
),
)
}

// Prepare
(func() {

storage, inter := newStorageAndInterpreter(t)

dictionaryStaticType := interpreter.NewDictionaryStaticType(
nil,
interpreter.PrimitiveStaticTypeMetaType,
interpreter.PrimitiveStaticTypeString,
)
dictValue := interpreter.NewDictionaryValue(inter, locationRange, dictionaryStaticType)

refType := interpreter.NewReferenceStaticType(
nil,
interpreter.UnauthorizedAccess,
newCompositeType(),
)
refType.HasLegacyIsAuthorized = true
refType.LegacyIsAuthorized = true

legacyRefType := &migrations.LegacyReferenceType{
ReferenceStaticType: refType,
}

optType := interpreter.NewOptionalStaticType(
nil,
legacyRefType,
)

legacyOptType := &migrations.LegacyOptionalType{
OptionalStaticType: optType,
}

typeValue := interpreter.NewUnmeteredTypeValue(legacyOptType)

dictValue.Insert(
inter,
locationRange,
typeValue,
newTestValue(),
)

// Note: ID is in the old format
assert.Equal(t,
common.TypeID("auth&A.4200000000000000.Foo.Bar?"),
legacyOptType.ID(),
)

storageMap := storage.GetStorageMap(
testAddress,
common.PathDomainStorage.Identifier(),
true,
)

storageMap.SetValue(inter,
storageMapKey,
dictValue.Transfer(
inter,
locationRange,
atree.Address(testAddress),
false,
nil,
nil,
true, // dictValue is standalone
),
)

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

err = storage.CheckHealth()
require.NoError(t, err)
})()

// Migrate
(func() {

storage, inter := newStorageAndInterpreter(t)

const importErrorMessage = "cannot import"

inter.SharedState.Config.ImportLocationHandler =
func(inter *interpreter.Interpreter, location common.Location) interpreter.Import {
panic(importErrorMessage)
}

migration, err := migrations.NewStorageMigration(inter, storage, "test", testAddress)
require.NoError(t, err)

reporter := newTestReporter()

migration.Migrate(
migration.NewValueMigrationsPathMigrator(
reporter,
NewEntitlementsMigration(inter),
),
)

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

// Assert

err = storage.CheckHealth()
require.NoError(t, err)

require.Len(t, reporter.errors, 1)

assert.ErrorContains(t, reporter.errors[0], importErrorMessage)

require.Empty(t, reporter.migrated)
})()

// Load
(func() {

storage, inter := newStorageAndInterpreter(t)

err := storage.CheckHealth()
require.NoError(t, err)

storageMap := storage.GetStorageMap(
testAddress,
common.PathDomainStorage.Identifier(),
false,
)
storedValue := storageMap.ReadValue(inter, storageMapKey)

require.IsType(t, &interpreter.DictionaryValue{}, storedValue)

dictValue := storedValue.(*interpreter.DictionaryValue)

refType := interpreter.NewReferenceStaticType(
nil,
interpreter.UnauthorizedAccess,
newCompositeType(),
)
refType.HasLegacyIsAuthorized = true
refType.LegacyIsAuthorized = true

optType := interpreter.NewOptionalStaticType(
nil,
refType,
)

typeValue := interpreter.NewUnmeteredTypeValue(optType)

// Note: ID is in the new format
assert.Equal(t,
common.TypeID("(&A.4200000000000000.Foo.Bar)?"),
optType.ID(),
)

assert.Equal(t, 1, dictValue.Count())

// Key did not get migrated, so is inaccessible using the "new" type value
_, ok := dictValue.Get(inter, locationRange, typeValue)
require.False(t, ok)

// But the key is still accessible using the "old" type value
legacyKey := migrations.LegacyKey(typeValue)

value, ok := dictValue.Get(inter, locationRange, legacyKey)
require.True(t, ok)
require.Equal(t, newTestValue(), value)
})()
}
64 changes: 33 additions & 31 deletions migrations/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ func (m *StorageMigration) MigrateNestedValue(
// The mutating iterator is only able to read new keys,
// as it recalculates the stored values' hashes.

m.migrateDictionaryKeys(
keys := m.migrateDictionaryKeys(
storageKey,
storageMapKey,
dictionary,
Expand All @@ -379,6 +379,7 @@ func (m *StorageMigration) MigrateNestedValue(
valueMigrations,
reporter,
allowMutation,
keys,
)

case *interpreter.PublishedValue:
Expand Down Expand Up @@ -468,14 +469,19 @@ func (m *StorageMigration) MigrateNestedValue(

}

type migratedDictionaryKey struct {
key interpreter.Value
migrated bool
}

func (m *StorageMigration) migrateDictionaryKeys(
storageKey interpreter.StorageKey,
storageMapKey interpreter.StorageMapKey,
dictionary *interpreter.DictionaryValue,
valueMigrations []ValueMigration,
reporter Reporter,
allowMutation bool,
) {
) (migratedKeys []migratedDictionaryKey) {
inter := m.interpreter

var existingKeys []interpreter.Value
Expand Down Expand Up @@ -505,6 +511,11 @@ func (m *StorageMigration) migrateDictionaryKeys(
)

if newKey == nil {
migratedKeys = append(migratedKeys, migratedDictionaryKey{
key: existingKey,
migrated: false,
})

continue
}

Expand All @@ -523,7 +534,7 @@ func (m *StorageMigration) migrateDictionaryKeys(

// Remove the old key-value pair

existingKey = legacyKey(existingKey)
existingKey = LegacyKey(existingKey)
existingKeyStorable, existingValueStorable := dictionary.RemoveWithoutTransfer(
inter,
emptyLocationRange,
Expand Down Expand Up @@ -645,8 +656,15 @@ func (m *StorageMigration) migrateDictionaryKeys(
newKey,
existingValue,
)

migratedKeys = append(migratedKeys, migratedDictionaryKey{
key: newKey,
migrated: true,
})
}
}

return
}

func (m *StorageMigration) migrateDictionaryValues(
Expand All @@ -656,37 +674,21 @@ func (m *StorageMigration) migrateDictionaryValues(
valueMigrations []ValueMigration,
reporter Reporter,
allowMutation bool,
migratedDictionaryKeys []migratedDictionaryKey,
) {

inter := m.interpreter

type keyValuePair struct {
key, value interpreter.Value
}

var existingKeysAndValues []keyValuePair

dictionary.Iterate(
inter,
emptyLocationRange,
func(key, value interpreter.Value) (resume bool) {

existingKeysAndValues = append(
existingKeysAndValues,
keyValuePair{
key: key,
value: value,
},
)
for _, migratedDictionaryKey := range migratedDictionaryKeys {

// Continue iteration
return true
},
)
existingKey := migratedDictionaryKey.key
if !migratedDictionaryKey.migrated {
existingKey = LegacyKey(existingKey)
}

for _, existingKeyAndValue := range existingKeysAndValues {
existingKey := existingKeyAndValue.key
existingValue := existingKeyAndValue.value
existingValue, ok := dictionary.Get(inter, emptyLocationRange, existingKey)
if !ok {
panic(errors.NewUnexpectedError("failed to get existing value for key: %s", existingKey))
}

newValue := m.MigrateNestedValue(
storageKey,
Expand Down Expand Up @@ -811,8 +813,8 @@ func (m *StorageMigration) migrate(
)
}

// legacyKey return the same type with the "old" hash/ID generation function.
func legacyKey(key interpreter.Value) interpreter.Value {
// LegacyKey return the same type with the "old" hash/ID generation function.
func LegacyKey(key interpreter.Value) interpreter.Value {
switch key := key.(type) {
case interpreter.TypeValue:
legacyType := legacyType(key.Type)
Expand Down
10 changes: 5 additions & 5 deletions migrations/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2485,12 +2485,12 @@ func TestDictionaryKeyConflict(t *testing.T) {
dictionaryValue,
)

// NOTE: use legacyKey to ensure the key is encoded in old format
// NOTE: use LegacyKey to ensure the key is encoded in old format

dictionaryValue.InsertWithoutTransfer(
inter,
emptyLocationRange,
legacyKey(dictionaryKey1),
LegacyKey(dictionaryKey1),
interpreter.NewArrayValue(
inter,
emptyLocationRange,
Expand All @@ -2503,7 +2503,7 @@ func TestDictionaryKeyConflict(t *testing.T) {
dictionaryValue.InsertWithoutTransfer(
inter,
emptyLocationRange,
legacyKey(dictionaryKey2),
LegacyKey(dictionaryKey2),
interpreter.NewArrayValue(
inter,
emptyLocationRange,
Expand All @@ -2516,7 +2516,7 @@ func TestDictionaryKeyConflict(t *testing.T) {
oldValue1, ok := dictionaryValue.Get(
inter,
emptyLocationRange,
legacyKey(dictionaryKey1),
LegacyKey(dictionaryKey1),
)
require.True(t, ok)

Expand All @@ -2535,7 +2535,7 @@ func TestDictionaryKeyConflict(t *testing.T) {
oldValue2, ok := dictionaryValue.Get(
inter,
emptyLocationRange,
legacyKey(dictionaryKey2),
LegacyKey(dictionaryKey2),
)
require.True(t, ok)

Expand Down
Loading

0 comments on commit a1aa985

Please sign in to comment.