Skip to content

Commit

Permalink
Merge pull request #3411 from onflow/bastian/migrate-path-cap-domain
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolent authored Jun 11, 2024
2 parents 145dea1 + a97d108 commit 31317a9
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 10 deletions.
12 changes: 12 additions & 0 deletions migrations/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,18 @@ func (m *StorageMigration) Migrate(migrator StorageMapKeyMigrator) {
stdlib.CapabilityControllerStorageDomain,
migrator,
)

accountStorage.MigrateStringKeys(
m.interpreter,
stdlib.PathCapabilityStorageDomain,
migrator,
)

accountStorage.MigrateUint64Keys(
m.interpreter,
stdlib.AccountCapabilityStorageDomain,
migrator,
)
}

func (m *StorageMigration) NewValueMigrationsPathMigrator(
Expand Down
102 changes: 92 additions & 10 deletions migrations/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,12 @@ func (testCapConMigration) Migrate(
value.BorrowType,
value.CapabilityID+10,
), nil

case interpreter.UInt64Value:
return value + 10, nil

case interpreter.NilValue:
return value, nil
}

return nil, nil
Expand Down Expand Up @@ -798,28 +804,49 @@ func TestCapConMigration(t *testing.T) {
})
require.NoError(t, err)

storageMap := storage.GetStorageMap(
capConStorageMap := storage.GetStorageMap(
testAddress,
stdlib.CapabilityControllerStorageDomain,
false,
)

assert.Equal(t, uint64(2), storageMap.Count())
assert.Equal(t, uint64(2), capConStorageMap.Count())

controller1 := storageMap.ReadValue(nil, interpreter.Uint64StorageMapKey(1))
controller1 := capConStorageMap.ReadValue(nil, interpreter.Uint64StorageMapKey(1))
require.IsType(t, &interpreter.StorageCapabilityControllerValue{}, controller1)
assert.Equal(t,
interpreter.UInt64Value(1),
controller1.(*interpreter.StorageCapabilityControllerValue).CapabilityID,
)

controller2 := storageMap.ReadValue(nil, interpreter.Uint64StorageMapKey(2))
controller2 := capConStorageMap.ReadValue(nil, interpreter.Uint64StorageMapKey(2))
require.IsType(t, &interpreter.AccountCapabilityControllerValue{}, controller2)
assert.Equal(t,
interpreter.UInt64Value(2),
controller2.(*interpreter.AccountCapabilityControllerValue).CapabilityID,
)

pathCapStorageMap := storage.GetStorageMap(
testAddress,
stdlib.PathCapabilityStorageDomain,
false,
)

pathCapSet := pathCapStorageMap.ReadValue(nil, interpreter.StringStorageMapKey("foo"))
require.IsType(t, &interpreter.DictionaryValue{}, pathCapSet)
capSetDict := pathCapSet.(*interpreter.DictionaryValue)
assert.Equal(t, 1, capSetDict.Count())
assert.True(t, bool(capSetDict.ContainsKey(inter, emptyLocationRange, interpreter.UInt64Value(1))))

accountCapStorageMap := storage.GetStorageMap(
testAddress,
stdlib.AccountCapabilityStorageDomain,
false,
)

accountCapEntry := accountCapStorageMap.ReadValue(nil, interpreter.Uint64StorageMapKey(2))
assert.NotNil(t, accountCapEntry)

// Migrate

migration, err := NewStorageMigration(inter, storage, "test", testAddress)
Expand All @@ -839,34 +866,89 @@ func TestCapConMigration(t *testing.T) {

// Assert

assert.Len(t, reporter.migrated, 2)

require.Empty(t, reporter.errors)
assert.Equal(t,
map[struct {
interpreter.StorageKey
interpreter.StorageMapKey
}][]string{
{
StorageKey: interpreter.StorageKey{
Address: testAddress,
Key: stdlib.CapabilityControllerStorageDomain,
},
StorageMapKey: interpreter.Uint64StorageMapKey(1),
}: {"testCapConMigration"},
{
StorageKey: interpreter.StorageKey{
Address: testAddress,
Key: stdlib.CapabilityControllerStorageDomain,
},
StorageMapKey: interpreter.Uint64StorageMapKey(2),
}: {"testCapConMigration"},
{
StorageKey: interpreter.StorageKey{
Address: testAddress,
Key: stdlib.PathCapabilityStorageDomain,
},
StorageMapKey: interpreter.StringStorageMapKey("foo"),
}: {"testCapConMigration", "testCapConMigration"},
{
StorageKey: interpreter.StorageKey{
Address: testAddress,
Key: stdlib.AccountCapabilityStorageDomain,
},
StorageMapKey: interpreter.Uint64StorageMapKey(2),
}: {"testCapConMigration"},
},
reporter.migrated,
)

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

storageMap = storage.GetStorageMap(
capConStorageMap = storage.GetStorageMap(
testAddress,
stdlib.CapabilityControllerStorageDomain,
false,
)

assert.Equal(t, uint64(2), storageMap.Count())
assert.Equal(t, uint64(2), capConStorageMap.Count())

controller1 = storageMap.ReadValue(nil, interpreter.Uint64StorageMapKey(1))
controller1 = capConStorageMap.ReadValue(nil, interpreter.Uint64StorageMapKey(1))
require.IsType(t, &interpreter.StorageCapabilityControllerValue{}, controller1)
assert.Equal(t,
interpreter.UInt64Value(11),
controller1.(*interpreter.StorageCapabilityControllerValue).CapabilityID,
)

controller2 = storageMap.ReadValue(nil, interpreter.Uint64StorageMapKey(2))
controller2 = capConStorageMap.ReadValue(nil, interpreter.Uint64StorageMapKey(2))
require.IsType(t, &interpreter.AccountCapabilityControllerValue{}, controller2)
assert.Equal(t,
interpreter.UInt64Value(12),
controller2.(*interpreter.AccountCapabilityControllerValue).CapabilityID,
)

pathCapStorageMap = storage.GetStorageMap(
testAddress,
stdlib.PathCapabilityStorageDomain,
false,
)

pathCapSet = pathCapStorageMap.ReadValue(nil, interpreter.StringStorageMapKey("foo"))
require.IsType(t, &interpreter.DictionaryValue{}, pathCapSet)
capSetDict = pathCapSet.(*interpreter.DictionaryValue)
assert.Equal(t, 1, capSetDict.Count())
assert.True(t, bool(capSetDict.ContainsKey(inter, emptyLocationRange, interpreter.UInt64Value(11))))

accountCapStorageMap = storage.GetStorageMap(
testAddress,
stdlib.AccountCapabilityStorageDomain,
false,
)

accountCapEntry = accountCapStorageMap.ReadValue(nil, interpreter.Uint64StorageMapKey(2))
assert.NotNil(t, accountCapEntry)
}

func TestContractMigration(t *testing.T) {
Expand Down

0 comments on commit 31317a9

Please sign in to comment.