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

Also migrate path-capability and account-capability storage maps #3411

Merged
merged 2 commits into from
Jun 11, 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
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
Loading